blob: 75a36d79a5fb393f8b08042635ee2b3c349ab26e [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
141#include <stdio.h>
142#include <assert.h>
143
David Benjamin39482a12014-07-20 13:30:15 -0400144#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700145#include <openssl/dh.h>
146#include <openssl/engine.h>
147#include <openssl/lhash.h>
148#include <openssl/mem.h>
149#include <openssl/obj.h>
150#include <openssl/rand.h>
151#include <openssl/x509v3.h>
152
153#include "ssl_locl.h"
154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155/* Some error codes are special. Ensure the make_errors.go script never
156 * regresses this. */
157OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
158 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
159 ssl_alert_reason_code_mismatch);
David Benjamine1136082014-09-20 12:28:58 -0400160
Adam Langleyfcf25832014-12-18 17:42:32 -0800161int SSL_clear(SSL *s) {
162 if (s->method == NULL) {
163 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
164 return 0;
165 }
Adam Langley95c29f32014-06-20 12:00:00 -0700166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (ssl_clear_bad_session(s)) {
168 SSL_SESSION_free(s->session);
169 s->session = NULL;
170 }
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 s->hit = 0;
173 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 if (s->renegotiate) {
176 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
177 return 0;
178 }
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Adam Langleyfcf25832014-12-18 17:42:32 -0800180 /* SSL_clear may be called before or after the |s| is initialized in either
181 * accept or connect state. In the latter case, SSL_clear should preserve the
182 * half and reset |s->state| accordingly. */
183 if (s->handshake_func != NULL) {
184 if (s->server) {
185 SSL_set_accept_state(s);
186 } else {
187 SSL_set_connect_state(s);
188 }
189 } else {
190 assert(s->state == 0);
191 }
Adam Langley95c29f32014-06-20 12:00:00 -0700192
David Benjamin62fd1622015-01-11 13:30:01 -0500193 /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
194 * |SSL_clear| because it is per-connection state rather than configuration
195 * state. Per-connection state should be on |s->s3| and |s->d1| so it is
196 * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
197 * |ssl3_new|. */
198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 s->rwstate = SSL_NOTHING;
200 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 if (s->init_buf != NULL) {
203 BUF_MEM_free(s->init_buf);
204 s->init_buf = NULL;
205 }
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjamin62fd1622015-01-11 13:30:01 -0500207 s->packet = NULL;
208 s->packet_length = 0;
209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin62fd1622015-01-11 13:30:01 -0500212 if (s->next_proto_negotiated) {
213 OPENSSL_free(s->next_proto_negotiated);
214 s->next_proto_negotiated = NULL;
215 s->next_proto_negotiated_len = 0;
216 }
217
218 /* The s->d1->mtu is simultaneously configuration (preserved across
219 * clear) and connection-specific state (gets reset).
220 *
221 * TODO(davidben): Avoid this. */
222 unsigned mtu = 0;
223 if (s->d1 != NULL) {
224 mtu = s->d1->mtu;
225 }
226
227 s->method->ssl_free(s);
228 if (!s->method->ssl_new(s)) {
229 return 0;
230 }
231 s->enc_method = ssl3_get_enc_method(s->version);
232 assert(s->enc_method != NULL);
233
234 if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
235 s->d1->mtu = mtu;
236 }
237
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 s->client_version = s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 return 1;
241}
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243SSL *SSL_new(SSL_CTX *ctx) {
244 SSL *s;
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 if (ctx == NULL) {
247 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
248 return NULL;
249 }
250 if (ctx->method == NULL) {
251 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
252 return NULL;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
256 if (s == NULL) {
257 goto err;
258 }
259 memset(s, 0, sizeof(SSL));
Adam Langley95c29f32014-06-20 12:00:00 -0700260
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 s->min_version = ctx->min_version;
262 s->max_version = ctx->max_version;
David Benjamin1eb367c2014-12-12 18:17:51 -0500263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 s->options = ctx->options;
265 s->mode = ctx->mode;
266 s->max_cert_list = ctx->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 if (ctx->cert != NULL) {
269 /* Earlier library versions used to copy the pointer to the CERT, not its
270 * contents; only when setting new parameters for the per-SSL copy,
271 * ssl_cert_new would be called (and the direct reference to the
272 * per-SSL_CTX settings would be lost, but those still were indirectly
273 * accessed for various purposes, and for that reason they used to be known
274 * as s->ctx->default_cert). Now we don't look at the SSL_CTX's CERT after
275 * having duplicated it once. */
Adam Langley95c29f32014-06-20 12:00:00 -0700276
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 s->cert = ssl_cert_dup(ctx->cert);
278 if (s->cert == NULL) {
279 goto err;
280 }
281 } else {
282 s->cert = NULL; /* Cannot really happen (see SSL_CTX_new) */
283 }
Adam Langley95c29f32014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 s->read_ahead = ctx->read_ahead;
286 s->msg_callback = ctx->msg_callback;
287 s->msg_callback_arg = ctx->msg_callback_arg;
288 s->verify_mode = ctx->verify_mode;
289 s->sid_ctx_length = ctx->sid_ctx_length;
290 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
291 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
292 s->verify_callback = ctx->default_verify_callback;
293 s->generate_session_id = ctx->generate_session_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700294
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 s->param = X509_VERIFY_PARAM_new();
296 if (!s->param) {
297 goto err;
298 }
299 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
300 s->quiet_shutdown = ctx->quiet_shutdown;
301 s->max_send_fragment = ctx->max_send_fragment;
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
304 s->ctx = ctx;
305 s->tlsext_debug_cb = 0;
306 s->tlsext_debug_arg = NULL;
307 s->tlsext_ticket_expected = 0;
308 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
309 s->initial_ctx = ctx;
310 if (ctx->tlsext_ecpointformatlist) {
311 s->tlsext_ecpointformatlist = BUF_memdup(
312 ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
313 if (!s->tlsext_ecpointformatlist) {
314 goto err;
315 }
316 s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
317 }
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 if (ctx->tlsext_ellipticcurvelist) {
320 s->tlsext_ellipticcurvelist =
321 BUF_memdup(ctx->tlsext_ellipticcurvelist,
322 ctx->tlsext_ellipticcurvelist_length * 2);
323 if (!s->tlsext_ellipticcurvelist) {
324 goto err;
325 }
326 s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
327 }
328 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 if (s->ctx->alpn_client_proto_list) {
331 s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
332 s->ctx->alpn_client_proto_list_len);
333 if (s->alpn_client_proto_list == NULL) {
334 goto err;
335 }
336 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
337 }
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 s->verify_result = X509_V_OK;
340 s->method = ctx->method;
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 if (!s->method->ssl_new(s)) {
343 goto err;
344 }
345 s->enc_method = ssl3_get_enc_method(s->version);
346 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langleyfcf25832014-12-18 17:42:32 -0800348 s->references = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700349
David Benjamin62fd1622015-01-11 13:30:01 -0500350 s->rwstate = SSL_NOTHING;
351 s->rstate = SSL_ST_READ_HEADER;
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langleyfcf25832014-12-18 17:42:32 -0800353 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700354
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 s->psk_identity_hint = NULL;
356 if (ctx->psk_identity_hint) {
357 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
358 if (s->psk_identity_hint == NULL) {
359 goto err;
360 }
361 }
362 s->psk_client_callback = ctx->psk_client_callback;
363 s->psk_server_callback = ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700364
David Benjamin02ddbfd2015-01-11 13:09:11 -0500365 s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
366 if (ctx->tlsext_channel_id_private) {
367 s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
368 }
369
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
371 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200372
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 return s;
374
Adam Langley95c29f32014-06-20 12:00:00 -0700375err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 if (s != NULL) {
David Benjamin710d2272015-01-11 20:07:59 -0500377 SSL_free(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 }
379 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return NULL;
382}
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langleyfcf25832014-12-18 17:42:32 -0800384int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
385 unsigned int sid_ctx_len) {
386 if (sid_ctx_len > sizeof ctx->sid_ctx) {
387 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
388 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
389 return 0;
390 }
391 ctx->sid_ctx_length = sid_ctx_len;
392 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
398 unsigned int sid_ctx_len) {
399 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
400 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
401 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
402 return 0;
403 }
404 ssl->sid_ctx_length = sid_ctx_len;
405 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 return 1;
408}
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
411 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
412 ctx->generate_session_id = cb;
413 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
414 return 1;
415}
Adam Langley95c29f32014-06-20 12:00:00 -0700416
Adam Langleyfcf25832014-12-18 17:42:32 -0800417int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
418 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
419 ssl->generate_session_id = cb;
420 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
421 return 1;
422}
Adam Langley95c29f32014-06-20 12:00:00 -0700423
Adam Langleyfcf25832014-12-18 17:42:32 -0800424int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
425 unsigned int id_len) {
426 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
427 * can "construct" a session to give us the desired check - ie. to find if
428 * there's a session in the hash table that would conflict with any new
429 * session built out of this id/id_len and the ssl_version in use by this
430 * SSL. */
431 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700432
Adam Langleyfcf25832014-12-18 17:42:32 -0800433 if (id_len > sizeof r.session_id) {
434 return 0;
435 }
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 r.ssl_version = ssl->version;
438 r.session_id_length = id_len;
439 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700440
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
442 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
443 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
444 return p != NULL;
445}
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langleyfcf25832014-12-18 17:42:32 -0800447int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
448 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
449}
450
451int SSL_set_purpose(SSL *s, int purpose) {
452 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
453}
454
455int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
456 return X509_VERIFY_PARAM_set_trust(s->param, trust);
457}
458
459int SSL_set_trust(SSL *s, int trust) {
460 return X509_VERIFY_PARAM_set_trust(s->param, trust);
461}
462
463int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
464 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
465}
466
467int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
468 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
469}
Adam Langley95c29f32014-06-20 12:00:00 -0700470
Adam Langley858a88d2014-06-20 12:00:00 -0700471void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800472 struct ssl_cipher_preference_list_st *cipher_list) {
473 sk_SSL_CIPHER_free(cipher_list->ciphers);
474 OPENSSL_free(cipher_list->in_group_flags);
475 OPENSSL_free(cipher_list);
476}
Adam Langley858a88d2014-06-20 12:00:00 -0700477
Adam Langleyfcf25832014-12-18 17:42:32 -0800478struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
479 struct ssl_cipher_preference_list_st *cipher_list) {
480 struct ssl_cipher_preference_list_st *ret = NULL;
481 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700482
Adam Langleyfcf25832014-12-18 17:42:32 -0800483 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
484 if (!ret) {
485 goto err;
486 }
487
488 ret->ciphers = NULL;
489 ret->in_group_flags = NULL;
490 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
491 if (!ret->ciphers) {
492 goto err;
493 }
494 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
495 if (!ret->in_group_flags) {
496 goto err;
497 }
498
499 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700500
501err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800502 if (ret && ret->ciphers) {
503 sk_SSL_CIPHER_free(ret->ciphers);
504 }
505 if (ret) {
506 OPENSSL_free(ret);
507 }
508 return NULL;
509}
Adam Langley858a88d2014-06-20 12:00:00 -0700510
Adam Langleyfcf25832014-12-18 17:42:32 -0800511struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
512 STACK_OF(SSL_CIPHER) * ciphers) {
513 struct ssl_cipher_preference_list_st *ret = NULL;
514 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
517 if (!ret) {
518 goto err;
519 }
520 ret->ciphers = NULL;
521 ret->in_group_flags = NULL;
522 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
523 if (!ret->ciphers) {
524 goto err;
525 }
526 ret->in_group_flags = OPENSSL_malloc(n);
527 if (!ret->in_group_flags) {
528 goto err;
529 }
530 memset(ret->in_group_flags, 0, n);
531 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700532
533err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 if (ret && ret->ciphers) {
535 sk_SSL_CIPHER_free(ret->ciphers);
536 }
537 if (ret) {
538 OPENSSL_free(ret);
539 }
540 return NULL;
541}
Adam Langley858a88d2014-06-20 12:00:00 -0700542
Adam Langleyfcf25832014-12-18 17:42:32 -0800543X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langleyfcf25832014-12-18 17:42:32 -0800547void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549void SSL_free(SSL *s) {
550 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700551
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 if (s == NULL) {
553 return;
554 }
Adam Langley95c29f32014-06-20 12:00:00 -0700555
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);
557 if (i > 0) {
558 return;
559 }
Adam Langley95c29f32014-06-20 12:00:00 -0700560
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 if (s->param) {
562 X509_VERIFY_PARAM_free(s->param);
563 }
Adam Langley95c29f32014-06-20 12:00:00 -0700564
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700566
Adam Langleyfcf25832014-12-18 17:42:32 -0800567 if (s->bbio != NULL) {
568 /* If the buffering BIO is in place, pop it off */
569 if (s->bbio == s->wbio) {
570 s->wbio = BIO_pop(s->wbio);
571 }
572 BIO_free(s->bbio);
573 s->bbio = NULL;
574 }
Adam Langley95c29f32014-06-20 12:00:00 -0700575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 if (s->rbio != NULL) {
577 BIO_free_all(s->rbio);
578 }
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 if (s->wbio != NULL && s->wbio != s->rbio) {
581 BIO_free_all(s->wbio);
582 }
Adam Langley95c29f32014-06-20 12:00:00 -0700583
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 if (s->init_buf != NULL) {
585 BUF_MEM_free(s->init_buf);
586 }
Adam Langley95c29f32014-06-20 12:00:00 -0700587
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 /* add extra stuff */
589 if (s->cipher_list != NULL) {
590 ssl_cipher_preference_list_free(s->cipher_list);
591 }
592 if (s->cipher_list_by_id != NULL) {
593 sk_SSL_CIPHER_free(s->cipher_list_by_id);
594 }
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 if (s->session != NULL) {
597 ssl_clear_bad_session(s);
598 SSL_SESSION_free(s->session);
599 }
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 if (s->cert != NULL) {
604 ssl_cert_free(s->cert);
605 }
Adam Langley0289c732014-06-20 12:00:00 -0700606
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 if (s->tlsext_hostname) {
608 OPENSSL_free(s->tlsext_hostname);
609 }
610 if (s->initial_ctx) {
611 SSL_CTX_free(s->initial_ctx);
612 }
613 if (s->tlsext_ecpointformatlist) {
614 OPENSSL_free(s->tlsext_ecpointformatlist);
615 }
616 if (s->tlsext_ellipticcurvelist) {
617 OPENSSL_free(s->tlsext_ellipticcurvelist);
618 }
619 if (s->alpn_client_proto_list) {
620 OPENSSL_free(s->alpn_client_proto_list);
621 }
622 if (s->tlsext_channel_id_private) {
623 EVP_PKEY_free(s->tlsext_channel_id_private);
624 }
625 if (s->psk_identity_hint) {
626 OPENSSL_free(s->psk_identity_hint);
627 }
628 if (s->client_CA != NULL) {
629 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
630 }
631 if (s->next_proto_negotiated) {
632 OPENSSL_free(s->next_proto_negotiated);
633 }
634 if (s->srtp_profiles) {
635 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
636 }
Adam Langley95c29f32014-06-20 12:00:00 -0700637
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 if (s->method != NULL) {
639 s->method->ssl_free(s);
640 }
641 if (s->ctx) {
642 SSL_CTX_free(s->ctx);
643 }
Adam Langley95c29f32014-06-20 12:00:00 -0700644
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 OPENSSL_free(s);
646}
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
649 /* If the output buffering BIO is still in place, remove it. */
650 if (s->bbio != NULL) {
651 if (s->wbio == s->bbio) {
652 s->wbio = s->wbio->next_bio;
653 s->bbio->next_bio = NULL;
654 }
655 }
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 if (s->rbio != NULL && s->rbio != rbio) {
658 BIO_free_all(s->rbio);
659 }
660 if (s->wbio != NULL && s->wbio != wbio && s->rbio != s->wbio) {
661 BIO_free_all(s->wbio);
662 }
663 s->rbio = rbio;
664 s->wbio = wbio;
665}
Adam Langley95c29f32014-06-20 12:00:00 -0700666
Adam Langleyfcf25832014-12-18 17:42:32 -0800667BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langleyfcf25832014-12-18 17:42:32 -0800669BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langleyfcf25832014-12-18 17:42:32 -0800671int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700672
Adam Langleyfcf25832014-12-18 17:42:32 -0800673int SSL_get_rfd(const SSL *s) {
674 int ret = -1;
675 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700676
Adam Langleyfcf25832014-12-18 17:42:32 -0800677 b = SSL_get_rbio(s);
678 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
679 if (r != NULL) {
680 BIO_get_fd(r, &ret);
681 }
682 return ret;
683}
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langleyfcf25832014-12-18 17:42:32 -0800685int SSL_get_wfd(const SSL *s) {
686 int ret = -1;
687 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700688
Adam Langleyfcf25832014-12-18 17:42:32 -0800689 b = SSL_get_wbio(s);
690 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
691 if (r != NULL) {
692 BIO_get_fd(r, &ret);
693 }
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Adam Langleyfcf25832014-12-18 17:42:32 -0800695 return ret;
696}
Adam Langley95c29f32014-06-20 12:00:00 -0700697
Adam Langleyfcf25832014-12-18 17:42:32 -0800698int SSL_set_fd(SSL *s, int fd) {
699 int ret = 0;
700 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 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_fd, ERR_R_BUF_LIB);
706 goto err;
707 }
708 BIO_set_fd(bio, fd, BIO_NOCLOSE);
709 SSL_set_bio(s, bio, bio);
710 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700711
Adam Langley95c29f32014-06-20 12:00:00 -0700712err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800713 return ret;
714}
Adam Langley95c29f32014-06-20 12:00:00 -0700715
Adam Langleyfcf25832014-12-18 17:42:32 -0800716int SSL_set_wfd(SSL *s, int fd) {
717 int ret = 0;
718 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700719
Adam Langleyfcf25832014-12-18 17:42:32 -0800720 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
721 (int)BIO_get_fd(s->rbio, NULL) != fd) {
722 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700723
Adam Langleyfcf25832014-12-18 17:42:32 -0800724 if (bio == NULL) {
725 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
726 goto err;
727 }
728 BIO_set_fd(bio, fd, BIO_NOCLOSE);
729 SSL_set_bio(s, SSL_get_rbio(s), bio);
730 } else {
731 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
732 }
733
734 ret = 1;
735
Adam Langley95c29f32014-06-20 12:00:00 -0700736err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800737 return ret;
738}
Adam Langley95c29f32014-06-20 12:00:00 -0700739
Adam Langleyfcf25832014-12-18 17:42:32 -0800740int SSL_set_rfd(SSL *s, int fd) {
741 int ret = 0;
742 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700743
Adam Langleyfcf25832014-12-18 17:42:32 -0800744 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
745 (int)BIO_get_fd(s->wbio, NULL) != fd) {
746 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700747
Adam Langleyfcf25832014-12-18 17:42:32 -0800748 if (bio == NULL) {
749 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
750 goto err;
751 }
752 BIO_set_fd(bio, fd, BIO_NOCLOSE);
753 SSL_set_bio(s, bio, SSL_get_wbio(s));
754 } else {
755 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
756 }
757 ret = 1;
758
Adam Langley95c29f32014-06-20 12:00:00 -0700759err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800760 return ret;
761}
Adam Langley95c29f32014-06-20 12:00:00 -0700762
763/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800764size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
765 size_t ret = 0;
766
767 if (s->s3 != NULL) {
768 ret = s->s3->tmp.finish_md_len;
769 if (count > ret) {
770 count = ret;
771 }
772 memcpy(buf, s->s3->tmp.finish_md, count);
773 }
774
775 return ret;
776}
Adam Langley95c29f32014-06-20 12:00:00 -0700777
778/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800779size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
780 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700781
Adam Langleyfcf25832014-12-18 17:42:32 -0800782 if (s->s3 != NULL) {
783 ret = s->s3->tmp.peer_finish_md_len;
784 if (count > ret) {
785 count = ret;
786 }
787 memcpy(buf, s->s3->tmp.peer_finish_md, count);
788 }
Adam Langley95c29f32014-06-20 12:00:00 -0700789
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 return ret;
791}
Adam Langley95c29f32014-06-20 12:00:00 -0700792
Adam Langleyfcf25832014-12-18 17:42:32 -0800793int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700794
Adam Langleyfcf25832014-12-18 17:42:32 -0800795int SSL_get_verify_depth(const SSL *s) {
796 return X509_VERIFY_PARAM_get_depth(s->param);
797}
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Adam Langleyfcf25832014-12-18 17:42:32 -0800799int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
800 return s->verify_callback;
801}
Adam Langley95c29f32014-06-20 12:00:00 -0700802
Adam Langleyfcf25832014-12-18 17:42:32 -0800803int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langleyfcf25832014-12-18 17:42:32 -0800805int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
806 return X509_VERIFY_PARAM_get_depth(ctx->param);
807}
Adam Langley95c29f32014-06-20 12:00:00 -0700808
Adam Langleyfcf25832014-12-18 17:42:32 -0800809int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
810 return ctx->default_verify_callback;
811}
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813void SSL_set_verify(SSL *s, int mode,
814 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
815 s->verify_mode = mode;
816 if (callback != NULL) {
817 s->verify_callback = callback;
818 }
819}
Adam Langley95c29f32014-06-20 12:00:00 -0700820
Adam Langleyfcf25832014-12-18 17:42:32 -0800821void SSL_set_verify_depth(SSL *s, int depth) {
822 X509_VERIFY_PARAM_set_depth(s->param, depth);
823}
Adam Langley95c29f32014-06-20 12:00:00 -0700824
Adam Langleyfcf25832014-12-18 17:42:32 -0800825void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829int SSL_pending(const SSL *s) {
830 /* SSL_pending cannot work properly if read-ahead is enabled
831 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
832 * impossible to fix since SSL_pending cannot report errors that may be
833 * observed while scanning the new data. (Note that SSL_pending() is often
834 * used as a boolean value, so we'd better not return -1.). */
835 return s->method->ssl_pending(s);
836}
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838X509 *SSL_get_peer_certificate(const SSL *s) {
839 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700840
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 if (s == NULL || s->session == NULL) {
842 r = NULL;
843 } else {
844 r = s->session->peer;
845 }
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 if (r == NULL) {
848 return NULL;
849 }
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 return X509_up_ref(r);
852}
853
854STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
855 STACK_OF(X509) * r;
856
857 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
858 r = NULL;
859 } else {
860 r = s->session->sess_cert->cert_chain;
861 }
862
863 /* If we are a client, cert_chain includes the peer's own certificate; if we
864 * are a server, it does not. */
865 return r;
866}
Adam Langley95c29f32014-06-20 12:00:00 -0700867
Adam Langley95c29f32014-06-20 12:00:00 -0700868/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800869int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
870 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
871 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
872 SSL_R_NO_CERTIFICATE_ASSIGNED);
873 return 0;
874 }
875
876 if (ctx->cert->key->privatekey == NULL) {
877 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
878 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
879 return 0;
880 }
881
882 return X509_check_private_key(ctx->cert->key->x509,
883 ctx->cert->key->privatekey);
884}
Adam Langley95c29f32014-06-20 12:00:00 -0700885
886/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800887int SSL_check_private_key(const SSL *ssl) {
888 if (ssl == NULL) {
889 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
890 return 0;
891 }
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 if (ssl->cert == NULL) {
894 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
895 SSL_R_NO_CERTIFICATE_ASSIGNED);
896 return 0;
897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 if (ssl->cert->key->x509 == NULL) {
900 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
901 SSL_R_NO_CERTIFICATE_ASSIGNED);
902 return 0;
903 }
David Benjamin0b145c22014-11-26 20:10:09 -0500904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 if (ssl->cert->key->privatekey == NULL) {
906 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
907 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
908 return 0;
909 }
Adam Langley95c29f32014-06-20 12:00:00 -0700910
Adam Langleyfcf25832014-12-18 17:42:32 -0800911 return X509_check_private_key(ssl->cert->key->x509,
912 ssl->cert->key->privatekey);
913}
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langleyfcf25832014-12-18 17:42:32 -0800915int SSL_accept(SSL *s) {
916 if (s->handshake_func == 0) {
917 /* Not properly initialized yet */
918 SSL_set_accept_state(s);
919 }
David Benjamin0b145c22014-11-26 20:10:09 -0500920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 if (s->handshake_func != s->method->ssl_accept) {
922 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
923 return -1;
924 }
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langleyfcf25832014-12-18 17:42:32 -0800926 return s->handshake_func(s);
927}
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929int SSL_connect(SSL *s) {
930 if (s->handshake_func == 0) {
931 /* Not properly initialized yet */
932 SSL_set_connect_state(s);
933 }
Adam Langley95c29f32014-06-20 12:00:00 -0700934
Adam Langleyfcf25832014-12-18 17:42:32 -0800935 if (s->handshake_func != s->method->ssl_connect) {
936 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
937 return -1;
938 }
Adam Langley95c29f32014-06-20 12:00:00 -0700939
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 return s->handshake_func(s);
941}
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langleyfcf25832014-12-18 17:42:32 -0800943long SSL_get_default_timeout(const SSL *s) {
944 return SSL_DEFAULT_SESSION_TIMEOUT;
945}
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langleyfcf25832014-12-18 17:42:32 -0800947int SSL_read(SSL *s, void *buf, int num) {
948 if (s->handshake_func == 0) {
949 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
950 return -1;
951 }
Adam Langley95c29f32014-06-20 12:00:00 -0700952
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
954 s->rwstate = SSL_NOTHING;
955 return 0;
956 }
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 return s->method->ssl_read(s, buf, num);
959}
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961int SSL_peek(SSL *s, void *buf, int num) {
962 if (s->handshake_func == 0) {
963 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
964 return -1;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
968 return 0;
969 }
Adam Langley95c29f32014-06-20 12:00:00 -0700970
Adam Langleyfcf25832014-12-18 17:42:32 -0800971 return s->method->ssl_peek(s, buf, num);
972}
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974int SSL_write(SSL *s, const void *buf, int num) {
975 if (s->handshake_func == 0) {
976 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
977 return -1;
978 }
Adam Langley95c29f32014-06-20 12:00:00 -0700979
Adam Langleyfcf25832014-12-18 17:42:32 -0800980 if (s->shutdown & SSL_SENT_SHUTDOWN) {
981 s->rwstate = SSL_NOTHING;
982 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
983 return -1;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langleyfcf25832014-12-18 17:42:32 -0800986 return s->method->ssl_write(s, buf, num);
987}
Adam Langley95c29f32014-06-20 12:00:00 -0700988
Adam Langleyfcf25832014-12-18 17:42:32 -0800989int SSL_shutdown(SSL *s) {
990 /* Note that this function behaves differently from what one might expect.
991 * Return values are 0 for no success (yet), 1 for success; but calling it
992 * once is usually not enough, even if blocking I/O is used (see
993 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -0700994
Adam Langleyfcf25832014-12-18 17:42:32 -0800995 if (s->handshake_func == 0) {
996 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
997 return -1;
998 }
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langleyfcf25832014-12-18 17:42:32 -08001000 if (!SSL_in_init(s)) {
1001 return s->method->ssl_shutdown(s);
1002 }
Adam Langley95c29f32014-06-20 12:00:00 -07001003
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 return 1;
1005}
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007int SSL_renegotiate(SSL *s) {
David Benjamin2cdace92015-01-24 13:30:02 -05001008 if (SSL_IS_DTLS(s)) {
1009 /* Renegotiation is not supported for DTLS. */
1010 OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1011 return 0;
1012 }
1013
Adam Langleyfcf25832014-12-18 17:42:32 -08001014 if (s->renegotiate == 0) {
1015 s->renegotiate = 1;
1016 }
Adam Langley95c29f32014-06-20 12:00:00 -07001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 s->new_session = 1;
1019 return s->method->ssl_renegotiate(s);
1020}
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langleyfcf25832014-12-18 17:42:32 -08001022int SSL_renegotiate_pending(SSL *s) {
1023 /* becomes true when negotiation is requested; false again once a handshake
1024 * has finished */
1025 return s->renegotiate != 0;
1026}
Adam Langley95c29f32014-06-20 12:00:00 -07001027
Adam Langleyfcf25832014-12-18 17:42:32 -08001028long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1029 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031 switch (cmd) {
1032 case SSL_CTRL_GET_READ_AHEAD:
1033 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -07001034
Adam Langleyfcf25832014-12-18 17:42:32 -08001035 case SSL_CTRL_SET_READ_AHEAD:
1036 l = s->read_ahead;
1037 s->read_ahead = larg;
1038 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001039
Adam Langleyfcf25832014-12-18 17:42:32 -08001040 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1041 s->msg_callback_arg = parg;
1042 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001043
Adam Langleyfcf25832014-12-18 17:42:32 -08001044 case SSL_CTRL_OPTIONS:
1045 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 case SSL_CTRL_CLEAR_OPTIONS:
1048 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001049
Adam Langleyfcf25832014-12-18 17:42:32 -08001050 case SSL_CTRL_MODE:
1051 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001052
Adam Langleyfcf25832014-12-18 17:42:32 -08001053 case SSL_CTRL_CLEAR_MODE:
1054 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001055
Adam Langleyfcf25832014-12-18 17:42:32 -08001056 case SSL_CTRL_GET_MAX_CERT_LIST:
1057 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001058
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 case SSL_CTRL_SET_MAX_CERT_LIST:
1060 l = s->max_cert_list;
1061 s->max_cert_list = larg;
1062 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001063
Adam Langleyfcf25832014-12-18 17:42:32 -08001064 case SSL_CTRL_SET_MTU:
1065 if (larg < (long)dtls1_min_mtu()) {
1066 return 0;
1067 }
1068 if (SSL_IS_DTLS(s)) {
1069 s->d1->mtu = larg;
1070 return larg;
1071 }
1072 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001073
Adam Langleyfcf25832014-12-18 17:42:32 -08001074 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1075 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1076 return 0;
1077 }
1078 s->max_send_fragment = larg;
1079 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001080
Adam Langleyfcf25832014-12-18 17:42:32 -08001081 case SSL_CTRL_GET_RI_SUPPORT:
1082 if (s->s3) {
1083 return s->s3->send_connection_binding;
1084 }
1085 return 0;
1086
1087 case SSL_CTRL_CERT_FLAGS:
1088 return s->cert->cert_flags |= larg;
1089
1090 case SSL_CTRL_CLEAR_CERT_FLAGS:
1091 return s->cert->cert_flags &= ~larg;
1092
1093 case SSL_CTRL_GET_RAW_CIPHERLIST:
1094 if (parg) {
1095 if (s->cert->ciphers_raw == NULL) {
1096 return 0;
1097 }
1098 *(uint8_t **)parg = s->cert->ciphers_raw;
1099 return (int)s->cert->ciphers_rawlen;
1100 }
1101
1102 /* Passing a NULL |parg| returns the size of a single
1103 * cipher suite value. */
1104 return 2;
1105
1106 default:
1107 return s->method->ssl_ctrl(s, cmd, larg, parg);
1108 }
1109}
1110
1111long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1112 switch (cmd) {
1113 case SSL_CTRL_SET_MSG_CALLBACK:
1114 s->msg_callback =
1115 (void (*)(int write_p, int version, int content_type, const void *buf,
1116 size_t len, SSL *ssl, void *arg))(fp);
1117 return 1;
1118
1119 default:
1120 return s->method->ssl_callback_ctrl(s, cmd, fp);
1121 }
1122}
1123
1124LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1125
1126long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1127 long l;
1128
1129 switch (cmd) {
1130 case SSL_CTRL_GET_READ_AHEAD:
1131 return ctx->read_ahead;
1132
1133 case SSL_CTRL_SET_READ_AHEAD:
1134 l = ctx->read_ahead;
1135 ctx->read_ahead = larg;
1136 return l;
1137
1138 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1139 ctx->msg_callback_arg = parg;
1140 return 1;
1141
1142 case SSL_CTRL_GET_MAX_CERT_LIST:
1143 return ctx->max_cert_list;
1144
1145 case SSL_CTRL_SET_MAX_CERT_LIST:
1146 l = ctx->max_cert_list;
1147 ctx->max_cert_list = larg;
1148 return l;
1149
1150 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1151 l = ctx->session_cache_size;
1152 ctx->session_cache_size = larg;
1153 return l;
1154
1155 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1156 return ctx->session_cache_size;
1157
1158 case SSL_CTRL_SET_SESS_CACHE_MODE:
1159 l = ctx->session_cache_mode;
1160 ctx->session_cache_mode = larg;
1161 return l;
1162
1163 case SSL_CTRL_GET_SESS_CACHE_MODE:
1164 return ctx->session_cache_mode;
1165
1166 case SSL_CTRL_SESS_NUMBER:
1167 return lh_SSL_SESSION_num_items(ctx->sessions);
1168
1169 case SSL_CTRL_SESS_CONNECT:
1170 return ctx->stats.sess_connect;
1171
1172 case SSL_CTRL_SESS_CONNECT_GOOD:
1173 return ctx->stats.sess_connect_good;
1174
1175 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1176 return ctx->stats.sess_connect_renegotiate;
1177
1178 case SSL_CTRL_SESS_ACCEPT:
1179 return ctx->stats.sess_accept;
1180
1181 case SSL_CTRL_SESS_ACCEPT_GOOD:
1182 return ctx->stats.sess_accept_good;
1183
1184 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1185 return ctx->stats.sess_accept_renegotiate;
1186
1187 case SSL_CTRL_SESS_HIT:
1188 return ctx->stats.sess_hit;
1189
1190 case SSL_CTRL_SESS_CB_HIT:
1191 return ctx->stats.sess_cb_hit;
1192
1193 case SSL_CTRL_SESS_MISSES:
1194 return ctx->stats.sess_miss;
1195
1196 case SSL_CTRL_SESS_TIMEOUTS:
1197 return ctx->stats.sess_timeout;
1198
1199 case SSL_CTRL_SESS_CACHE_FULL:
1200 return ctx->stats.sess_cache_full;
1201
1202 case SSL_CTRL_OPTIONS:
1203 return ctx->options |= larg;
1204
1205 case SSL_CTRL_CLEAR_OPTIONS:
1206 return ctx->options &= ~larg;
1207
1208 case SSL_CTRL_MODE:
1209 return ctx->mode |= larg;
1210
1211 case SSL_CTRL_CLEAR_MODE:
1212 return ctx->mode &= ~larg;
1213
1214 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1215 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1216 return 0;
1217 }
1218 ctx->max_send_fragment = larg;
1219 return 1;
1220
1221 case SSL_CTRL_CERT_FLAGS:
1222 return ctx->cert->cert_flags |= larg;
1223
1224 case SSL_CTRL_CLEAR_CERT_FLAGS:
1225 return ctx->cert->cert_flags &= ~larg;
1226
1227 default:
1228 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1229 }
1230}
1231
1232long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1233 switch (cmd) {
1234 case SSL_CTRL_SET_MSG_CALLBACK:
1235 ctx->msg_callback =
1236 (void (*)(int write_p, int version, int content_type, const void *buf,
1237 size_t len, SSL *ssl, void *arg))(fp);
1238 return 1;
1239
1240 default:
1241 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1242 }
1243}
1244
1245int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1246 long l;
1247 const SSL_CIPHER *a = in_a;
1248 const SSL_CIPHER *b = in_b;
1249 const long a_id = a->id;
1250 const long b_id = b->id;
1251
1252 l = a_id - b_id;
1253 if (l == 0L) {
1254 return 0;
1255 } else {
1256 return (l > 0) ? 1 : -1;
1257 }
1258}
1259
1260int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1261 long l;
1262 const long a_id = (*ap)->id;
1263 const long b_id = (*bp)->id;
1264
1265 l = a_id - b_id;
1266 if (l == 0) {
1267 return 0;
1268 } else {
1269 return (l > 0) ? 1 : -1;
1270 }
1271}
1272
1273/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001274 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001275STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1276 if (s == NULL) {
1277 return NULL;
1278 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001279
Adam Langleyfcf25832014-12-18 17:42:32 -08001280 if (s->cipher_list != NULL) {
1281 return s->cipher_list->ciphers;
1282 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001283
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1285 s->ctx->cipher_list_tls11 != NULL) {
1286 return s->ctx->cipher_list_tls11->ciphers;
1287 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001288
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1290 return s->ctx->cipher_list->ciphers;
1291 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001292
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 return NULL;
1294}
Adam Langley95c29f32014-06-20 12:00:00 -07001295
Adam Langleyfcf25832014-12-18 17:42:32 -08001296/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001297 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001298STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1299 if (s == NULL) {
1300 return NULL;
1301 }
Adam Langley95c29f32014-06-20 12:00:00 -07001302
Adam Langleyfcf25832014-12-18 17:42:32 -08001303 if (s->cipher_list_by_id != NULL) {
1304 return s->cipher_list_by_id;
1305 }
Adam Langley95c29f32014-06-20 12:00:00 -07001306
Adam Langleyfcf25832014-12-18 17:42:32 -08001307 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1308 return s->ctx->cipher_list_by_id;
1309 }
Adam Langley95c29f32014-06-20 12:00:00 -07001310
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 return NULL;
1312}
Adam Langley95c29f32014-06-20 12:00:00 -07001313
Adam Langleyfcf25832014-12-18 17:42:32 -08001314/* The old interface to get the same thing as SSL_get_ciphers() */
1315const char *SSL_get_cipher_list(const SSL *s, int n) {
1316 const SSL_CIPHER *c;
1317 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001318
Adam Langleyfcf25832014-12-18 17:42:32 -08001319 if (s == NULL) {
1320 return NULL;
1321 }
Adam Langley95c29f32014-06-20 12:00:00 -07001322
Adam Langleyfcf25832014-12-18 17:42:32 -08001323 sk = SSL_get_ciphers(s);
1324 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1325 return NULL;
1326 }
Adam Langley95c29f32014-06-20 12:00:00 -07001327
Adam Langleyfcf25832014-12-18 17:42:32 -08001328 c = sk_SSL_CIPHER_value(sk, n);
1329 if (c == NULL) {
1330 return NULL;
1331 }
Adam Langley95c29f32014-06-20 12:00:00 -07001332
Adam Langleyfcf25832014-12-18 17:42:32 -08001333 return c->name;
1334}
David Benjamin5491e3f2014-09-29 19:33:09 -04001335
Adam Langleyfcf25832014-12-18 17:42:32 -08001336/* specify the ciphers to be used by default by the SSL_CTX */
1337int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1338 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001339
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1341 &ctx->cipher_list_by_id, str, ctx->cert);
1342 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1343 * a cipher matching the given rule string (for example if the rule string
1344 * specifies a cipher which has been disabled). This is not an error as far
1345 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1346 * ctx->cipher_list_by_id has been updated. */
1347 if (sk == NULL) {
1348 return 0;
1349 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1350 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1351 return 0;
1352 }
Adam Langley95c29f32014-06-20 12:00:00 -07001353
Adam Langleyfcf25832014-12-18 17:42:32 -08001354 return 1;
1355}
David Benjamin39482a12014-07-20 13:30:15 -04001356
Adam Langleyfcf25832014-12-18 17:42:32 -08001357int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1358 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Adam Langleyfcf25832014-12-18 17:42:32 -08001360 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str,
1361 ctx->cert);
1362 if (sk == NULL) {
1363 return 0;
1364 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1365 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1366 SSL_R_NO_CIPHER_MATCH);
1367 return 0;
1368 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001369
Adam Langleyfcf25832014-12-18 17:42:32 -08001370 return 1;
1371}
Adam Langley95c29f32014-06-20 12:00:00 -07001372
Adam Langleyfcf25832014-12-18 17:42:32 -08001373/* specify the ciphers to be used by the SSL */
1374int SSL_set_cipher_list(SSL *s, const char *str) {
1375 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001376
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1378 &s->cipher_list_by_id, str, s->cert);
David Benjamin39482a12014-07-20 13:30:15 -04001379
Adam Langleyfcf25832014-12-18 17:42:32 -08001380 /* see comment in SSL_CTX_set_cipher_list */
1381 if (sk == NULL) {
1382 return 0;
1383 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1384 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1385 return 0;
1386 }
David Benjamin39482a12014-07-20 13:30:15 -04001387
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 return 1;
1389}
Adam Langley95c29f32014-06-20 12:00:00 -07001390
Adam Langleyfcf25832014-12-18 17:42:32 -08001391int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1392 size_t i;
1393 const SSL_CIPHER *c;
1394 CERT *ct = s->cert;
1395 uint8_t *q;
1396 /* Set disabled masks for this session */
1397 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001398
Adam Langleyfcf25832014-12-18 17:42:32 -08001399 if (sk == NULL) {
1400 return 0;
1401 }
1402 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001403
Adam Langleyfcf25832014-12-18 17:42:32 -08001404 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1405 c = sk_SSL_CIPHER_value(sk, i);
1406 /* Skip disabled ciphers */
1407 if (c->algorithm_ssl & ct->mask_ssl ||
1408 c->algorithm_mkey & ct->mask_k ||
1409 c->algorithm_auth & ct->mask_a) {
1410 continue;
1411 }
1412 s2n(ssl3_get_cipher_value(c), p);
1413 }
1414
1415 /* If all ciphers were disabled, return the error to the caller. */
1416 if (p == q) {
1417 return 0;
1418 }
1419
1420 /* Add SCSVs. */
1421 if (!s->renegotiate) {
1422 s2n(SSL3_CK_SCSV & 0xffff, p);
1423 }
1424
1425 if (s->fallback_scsv) {
1426 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1427 }
1428
1429 return p - q;
1430}
1431
1432STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1433 CBS cipher_suites = *cbs;
1434 const SSL_CIPHER *c;
1435 STACK_OF(SSL_CIPHER) * sk;
1436
1437 if (s->s3) {
1438 s->s3->send_connection_binding = 0;
1439 }
1440
1441 if (CBS_len(&cipher_suites) % 2 != 0) {
1442 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1443 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1444 return NULL;
1445 }
1446
1447 sk = sk_SSL_CIPHER_new_null();
1448 if (sk == NULL) {
1449 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1450 goto err;
1451 }
1452
1453 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1454 &s->cert->ciphers_rawlen)) {
1455 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1456 goto err;
1457 }
1458
1459 while (CBS_len(&cipher_suites) > 0) {
1460 uint16_t cipher_suite;
1461
1462 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1463 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1464 goto err;
1465 }
1466
1467 /* Check for SCSV. */
1468 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1469 /* SCSV is fatal if renegotiating. */
1470 if (s->renegotiate) {
1471 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1472 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1473 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1474 goto err;
1475 }
1476 s->s3->send_connection_binding = 1;
1477 continue;
1478 }
1479
1480 /* Check for FALLBACK_SCSV. */
1481 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1482 uint16_t max_version = ssl3_get_max_server_version(s);
1483 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1484 : (uint16_t)s->version < max_version) {
1485 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1486 SSL_R_INAPPROPRIATE_FALLBACK);
1487 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1488 goto err;
1489 }
1490 continue;
1491 }
1492
1493 c = ssl3_get_cipher_by_value(cipher_suite);
1494 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1495 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1496 goto err;
1497 }
1498 }
1499
1500 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001501
Adam Langley95c29f32014-06-20 12:00:00 -07001502err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001503 if (sk != NULL) {
1504 sk_SSL_CIPHER_free(sk);
1505 }
1506 return NULL;
1507}
Adam Langley95c29f32014-06-20 12:00:00 -07001508
1509
Adam Langleyfcf25832014-12-18 17:42:32 -08001510/* return a servername extension value if provided in Client Hello, or NULL. So
1511 * far, only host_name types are defined (RFC 3546). */
1512const char *SSL_get_servername(const SSL *s, const int type) {
1513 if (type != TLSEXT_NAMETYPE_host_name) {
1514 return NULL;
1515 }
Adam Langley95c29f32014-06-20 12:00:00 -07001516
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1518 : s->tlsext_hostname;
1519}
Adam Langley95c29f32014-06-20 12:00:00 -07001520
Adam Langleyfcf25832014-12-18 17:42:32 -08001521int SSL_get_servername_type(const SSL *s) {
1522 if (s->session &&
1523 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1524 return TLSEXT_NAMETYPE_host_name;
1525 }
Adam Langley95c29f32014-06-20 12:00:00 -07001526
Adam Langleyfcf25832014-12-18 17:42:32 -08001527 return -1;
1528}
Adam Langley95c29f32014-06-20 12:00:00 -07001529
Adam Langleyfcf25832014-12-18 17:42:32 -08001530void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1531 ctx->signed_cert_timestamps_enabled = 1;
1532}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001533
Adam Langleyfcf25832014-12-18 17:42:32 -08001534int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1535 ssl->signed_cert_timestamps_enabled = 1;
1536 return 1;
1537}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001538
Adam Langleyfcf25832014-12-18 17:42:32 -08001539void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1540 ctx->ocsp_stapling_enabled = 1;
1541}
David Benjamin6c7aed02014-08-27 16:42:38 -04001542
Adam Langleyfcf25832014-12-18 17:42:32 -08001543int SSL_enable_ocsp_stapling(SSL *ssl) {
1544 ssl->ocsp_stapling_enabled = 1;
1545 return 1;
1546}
David Benjamin6c7aed02014-08-27 16:42:38 -04001547
Adam Langleyfcf25832014-12-18 17:42:32 -08001548void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1549 size_t *out_len) {
1550 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001551
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 *out_len = 0;
1553 *out = NULL;
1554 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1555 return;
1556 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001557
Adam Langleyfcf25832014-12-18 17:42:32 -08001558 *out = session->tlsext_signed_cert_timestamp_list;
1559 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1560}
David Benjamin6c7aed02014-08-27 16:42:38 -04001561
Adam Langleyfcf25832014-12-18 17:42:32 -08001562void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1563 size_t *out_len) {
1564 SSL_SESSION *session = ssl->session;
1565
1566 *out_len = 0;
1567 *out = NULL;
1568 if (ssl->server || !session || !session->ocsp_response) {
1569 return;
1570 }
1571 *out = session->ocsp_response;
1572 *out_len = session->ocsp_response_length;
1573}
David Benjamin6c7aed02014-08-27 16:42:38 -04001574
Adam Langley95c29f32014-06-20 12:00:00 -07001575/* SSL_select_next_proto implements the standard protocol selection. It is
1576 * expected that this function is called from the callback set by
1577 * SSL_CTX_set_next_proto_select_cb.
1578 *
1579 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1580 * strings. The length byte itself is not included in the length. A byte
1581 * string of length 0 is invalid. No byte string may be truncated.
1582 *
1583 * The current, but experimental algorithm for selecting the protocol is:
1584 *
1585 * 1) If the server doesn't support NPN then this is indicated to the
1586 * callback. In this case, the client application has to abort the connection
1587 * or have a default application level protocol.
1588 *
1589 * 2) If the server supports NPN, but advertises an empty list then the
1590 * client selects the first protcol in its list, but indicates via the
1591 * API that this fallback case was enacted.
1592 *
1593 * 3) Otherwise, the client finds the first protocol in the server's list
1594 * that it supports and selects this protocol. This is because it's
1595 * assumed that the server has better information about which protocol
1596 * a client should use.
1597 *
1598 * 4) If the client doesn't support any of the server's advertised
1599 * protocols, then this is treated the same as case 2.
1600 *
1601 * It returns either
1602 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1603 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1604 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001605int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1606 unsigned int server_len, const uint8_t *client,
1607 unsigned int client_len) {
1608 unsigned int i, j;
1609 const uint8_t *result;
1610 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001611
Adam Langleyfcf25832014-12-18 17:42:32 -08001612 /* For each protocol in server preference order, see if we support it. */
1613 for (i = 0; i < server_len;) {
1614 for (j = 0; j < client_len;) {
1615 if (server[i] == client[j] &&
1616 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1617 /* We found a match */
1618 result = &server[i];
1619 status = OPENSSL_NPN_NEGOTIATED;
1620 goto found;
1621 }
1622 j += client[j];
1623 j++;
1624 }
1625 i += server[i];
1626 i++;
1627 }
Adam Langley95c29f32014-06-20 12:00:00 -07001628
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 /* There's no overlap between our protocols and the server's list. */
1630 result = client;
1631 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001632
Adam Langleyfcf25832014-12-18 17:42:32 -08001633found:
1634 *out = (uint8_t *)result + 1;
1635 *outlen = result[0];
1636 return status;
1637}
Adam Langley95c29f32014-06-20 12:00:00 -07001638
Adam Langley95c29f32014-06-20 12:00:00 -07001639/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1640 * requested protocol for this connection and returns 0. If the client didn't
1641 * request any protocol, then *data is set to NULL.
1642 *
1643 * Note that the client can request any protocol it chooses. The value returned
1644 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 * provided by the callback. */
1646void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1647 unsigned *len) {
1648 *data = s->next_proto_negotiated;
1649 if (!*data) {
1650 *len = 0;
1651 } else {
1652 *len = s->next_proto_negotiated_len;
1653 }
Adam Langley95c29f32014-06-20 12:00:00 -07001654}
1655
1656/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1657 * TLS server needs a list of supported protocols for Next Protocol
1658 * Negotiation. The returned list must be in wire format. The list is returned
1659 * by setting |out| to point to it and |outlen| to its length. This memory will
1660 * not be modified, but one should assume that the SSL* keeps a reference to
1661 * it.
1662 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1664 * Otherwise, no such extension will be included in the ServerHello. */
1665void SSL_CTX_set_next_protos_advertised_cb(
1666 SSL_CTX *ctx,
1667 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1668 void *arg) {
1669 ctx->next_protos_advertised_cb = cb;
1670 ctx->next_protos_advertised_cb_arg = arg;
1671}
Adam Langley95c29f32014-06-20 12:00:00 -07001672
1673/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1674 * client needs to select a protocol from the server's provided list. |out|
1675 * must be set to point to the selected protocol (which may be within |in|).
1676 * The length of the protocol name must be written into |outlen|. The server's
1677 * advertised protocols are provided in |in| and |inlen|. The callback can
1678 * assume that |in| is syntactically valid.
1679 *
1680 * The client must select a protocol. It is fatal to the connection if this
1681 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1682 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001683void SSL_CTX_set_next_proto_select_cb(
1684 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1685 const uint8_t *in, unsigned int inlen, void *arg),
1686 void *arg) {
1687 ctx->next_proto_select_cb = cb;
1688 ctx->next_proto_select_cb_arg = arg;
1689}
Adam Langley95c29f32014-06-20 12:00:00 -07001690
Adam Langley95c29f32014-06-20 12:00:00 -07001691/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1692 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1693 * length-prefixed strings).
1694 *
1695 * Returns 0 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001696int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1697 unsigned protos_len) {
1698 if (ctx->alpn_client_proto_list) {
1699 OPENSSL_free(ctx->alpn_client_proto_list);
1700 }
Adam Langley95c29f32014-06-20 12:00:00 -07001701
Adam Langleyfcf25832014-12-18 17:42:32 -08001702 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1703 if (!ctx->alpn_client_proto_list) {
1704 return 1;
1705 }
1706 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001707
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 return 0;
1709}
Adam Langley95c29f32014-06-20 12:00:00 -07001710
1711/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1712 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1713 * length-prefixed strings).
1714 *
1715 * Returns 0 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001716int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1717 if (ssl->alpn_client_proto_list) {
1718 OPENSSL_free(ssl->alpn_client_proto_list);
1719 }
Adam Langley95c29f32014-06-20 12:00:00 -07001720
Adam Langleyfcf25832014-12-18 17:42:32 -08001721 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1722 if (!ssl->alpn_client_proto_list) {
1723 return 1;
1724 }
1725 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001726
Adam Langleyfcf25832014-12-18 17:42:32 -08001727 return 0;
1728}
Adam Langley95c29f32014-06-20 12:00:00 -07001729
1730/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1731 * during ClientHello processing in order to select an ALPN protocol from the
1732 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001733void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1734 int (*cb)(SSL *ssl, const uint8_t **out,
1735 uint8_t *outlen, const uint8_t *in,
1736 unsigned int inlen, void *arg),
1737 void *arg) {
1738 ctx->alpn_select_cb = cb;
1739 ctx->alpn_select_cb_arg = arg;
1740}
Adam Langley95c29f32014-06-20 12:00:00 -07001741
1742/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1743 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1744 * including the leading length-prefix byte). If the server didn't respond with
1745 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001746void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1747 unsigned *len) {
1748 *data = NULL;
1749 if (ssl->s3) {
1750 *data = ssl->s3->alpn_selected;
1751 }
1752 if (*data == NULL) {
1753 *len = 0;
1754 } else {
1755 *len = ssl->s3->alpn_selected_len;
1756 }
1757}
Adam Langley95c29f32014-06-20 12:00:00 -07001758
Adam Langleyfcf25832014-12-18 17:42:32 -08001759int SSL_export_keying_material(SSL *s, uint8_t *out, size_t olen,
1760 const char *label, size_t llen, const uint8_t *p,
1761 size_t plen, int use_context) {
1762 if (s->version < TLS1_VERSION) {
1763 return -1;
1764 }
Adam Langley95c29f32014-06-20 12:00:00 -07001765
Adam Langleyfcf25832014-12-18 17:42:32 -08001766 return s->enc_method->export_keying_material(s, out, olen, label, llen, p,
1767 plen, use_context);
1768}
Adam Langley95c29f32014-06-20 12:00:00 -07001769
Adam Langleyfcf25832014-12-18 17:42:32 -08001770static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1771 uint32_t hash =
1772 ((uint32_t)a->session_id[0]) ||
1773 ((uint32_t)a->session_id[1] << 8) ||
1774 ((uint32_t)a->session_id[2] << 16) ||
1775 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001776
Adam Langleyfcf25832014-12-18 17:42:32 -08001777 return hash;
1778}
Adam Langley95c29f32014-06-20 12:00:00 -07001779
1780/* NB: If this function (or indeed the hash function which uses a sort of
1781 * coarser function than this one) is changed, ensure
1782 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1783 * able to construct an SSL_SESSION that will collide with any existing session
1784 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001785static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1786 if (a->ssl_version != b->ssl_version) {
1787 return 1;
1788 }
Adam Langley95c29f32014-06-20 12:00:00 -07001789
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 if (a->session_id_length != b->session_id_length) {
1791 return 1;
1792 }
Adam Langley95c29f32014-06-20 12:00:00 -07001793
Adam Langleyfcf25832014-12-18 17:42:32 -08001794 return memcmp(a->session_id, b->session_id, a->session_id_length);
1795}
Adam Langley95c29f32014-06-20 12:00:00 -07001796
Adam Langleyfcf25832014-12-18 17:42:32 -08001797SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1798 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001799
Adam Langleyfcf25832014-12-18 17:42:32 -08001800 if (meth == NULL) {
1801 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1802 return NULL;
1803 }
Adam Langley95c29f32014-06-20 12:00:00 -07001804
Adam Langleyfcf25832014-12-18 17:42:32 -08001805 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1806 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1807 goto err;
1808 }
Adam Langley95c29f32014-06-20 12:00:00 -07001809
Adam Langleyfcf25832014-12-18 17:42:32 -08001810 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1811 if (ret == NULL) {
1812 goto err;
1813 }
Adam Langley95c29f32014-06-20 12:00:00 -07001814
Adam Langleyfcf25832014-12-18 17:42:32 -08001815 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001818
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 ret->cert_store = NULL;
1820 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1821 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1822 ret->session_cache_head = NULL;
1823 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001824
Adam Langleyfcf25832014-12-18 17:42:32 -08001825 /* We take the system default */
1826 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001827
Adam Langleyfcf25832014-12-18 17:42:32 -08001828 ret->new_session_cb = 0;
1829 ret->remove_session_cb = 0;
1830 ret->get_session_cb = 0;
1831 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001832
Adam Langleyfcf25832014-12-18 17:42:32 -08001833 memset((char *)&ret->stats, 0, sizeof(ret->stats));
Adam Langley95c29f32014-06-20 12:00:00 -07001834
Adam Langleyfcf25832014-12-18 17:42:32 -08001835 ret->references = 1;
1836 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001837
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001839
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 ret->app_verify_callback = 0;
1841 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1844 ret->read_ahead = 0;
1845 ret->msg_callback = 0;
1846 ret->msg_callback_arg = NULL;
1847 ret->verify_mode = SSL_VERIFY_NONE;
1848 ret->sid_ctx_length = 0;
1849 ret->default_verify_callback = NULL;
1850 ret->cert = ssl_cert_new();
1851 if (ret->cert == NULL) {
1852 goto err;
1853 }
Adam Langley95c29f32014-06-20 12:00:00 -07001854
Adam Langleyfcf25832014-12-18 17:42:32 -08001855 ret->default_passwd_callback = 0;
1856 ret->default_passwd_callback_userdata = NULL;
1857 ret->client_cert_cb = 0;
1858 ret->app_gen_cookie_cb = 0;
1859 ret->app_verify_cookie_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001860
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1862 if (ret->sessions == NULL) {
1863 goto err;
1864 }
1865 ret->cert_store = X509_STORE_new();
1866 if (ret->cert_store == NULL) {
1867 goto err;
1868 }
Adam Langley95c29f32014-06-20 12:00:00 -07001869
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 ssl_create_cipher_list(ret->method, &ret->cipher_list,
1871 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST,
1872 ret->cert);
1873 if (ret->cipher_list == NULL ||
1874 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1875 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1876 goto err2;
1877 }
Adam Langley95c29f32014-06-20 12:00:00 -07001878
Adam Langleyfcf25832014-12-18 17:42:32 -08001879 ret->param = X509_VERIFY_PARAM_new();
1880 if (!ret->param) {
1881 goto err;
1882 }
Adam Langley95c29f32014-06-20 12:00:00 -07001883
Adam Langleyfcf25832014-12-18 17:42:32 -08001884 ret->client_CA = sk_X509_NAME_new_null();
1885 if (ret->client_CA == NULL) {
1886 goto err;
1887 }
Adam Langley95c29f32014-06-20 12:00:00 -07001888
Adam Langleyfcf25832014-12-18 17:42:32 -08001889 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001890
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001892
Adam Langleyfcf25832014-12-18 17:42:32 -08001893 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001894
Adam Langleyfcf25832014-12-18 17:42:32 -08001895 ret->tlsext_servername_callback = 0;
1896 ret->tlsext_servername_arg = NULL;
1897 /* Setup RFC4507 ticket keys */
1898 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1899 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1900 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1901 ret->options |= SSL_OP_NO_TICKET;
1902 }
Adam Langley95c29f32014-06-20 12:00:00 -07001903
Adam Langleyfcf25832014-12-18 17:42:32 -08001904 ret->tlsext_status_cb = 0;
1905 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001906
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 ret->next_protos_advertised_cb = 0;
1908 ret->next_proto_select_cb = 0;
1909 ret->psk_identity_hint = NULL;
1910 ret->psk_client_callback = NULL;
1911 ret->psk_server_callback = NULL;
1912
1913 /* Default is to connect to non-RI servers. When RI is more widely deployed
1914 * might change this. */
1915 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1916
1917 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1918 * uses of SSL_METHOD. */
1919 if (meth->version != 0) {
1920 SSL_CTX_set_max_version(ret, meth->version);
1921 SSL_CTX_set_min_version(ret, meth->version);
1922 }
1923
1924 return ret;
1925
Adam Langley95c29f32014-06-20 12:00:00 -07001926err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001927 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001928err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001929 if (ret != NULL) {
1930 SSL_CTX_free(ret);
1931 }
1932 return NULL;
1933}
Adam Langley95c29f32014-06-20 12:00:00 -07001934
Adam Langleyfcf25832014-12-18 17:42:32 -08001935void SSL_CTX_free(SSL_CTX *a) {
1936 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001937
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 if (a == NULL) {
1939 return;
1940 }
Adam Langley95c29f32014-06-20 12:00:00 -07001941
Adam Langleyfcf25832014-12-18 17:42:32 -08001942 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1943 if (i > 0) {
1944 return;
1945 }
Adam Langley95c29f32014-06-20 12:00:00 -07001946
Adam Langleyfcf25832014-12-18 17:42:32 -08001947 if (a->param) {
1948 X509_VERIFY_PARAM_free(a->param);
1949 }
Adam Langley95c29f32014-06-20 12:00:00 -07001950
Adam Langleyfcf25832014-12-18 17:42:32 -08001951 /* Free internal session cache. However: the remove_cb() may reference the
1952 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1953 * sessions were flushed. As the ex_data handling routines might also touch
1954 * the session cache, the most secure solution seems to be: empty (flush) the
1955 * cache, then free ex_data, then finally free the cache. (See ticket
1956 * [openssl.org #212].) */
1957 if (a->sessions != NULL) {
1958 SSL_CTX_flush_sessions(a, 0);
1959 }
Adam Langley95c29f32014-06-20 12:00:00 -07001960
Adam Langleyfcf25832014-12-18 17:42:32 -08001961 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001962
Adam Langleyfcf25832014-12-18 17:42:32 -08001963 if (a->sessions != NULL) {
1964 lh_SSL_SESSION_free(a->sessions);
1965 }
1966 if (a->cert_store != NULL) {
1967 X509_STORE_free(a->cert_store);
1968 }
1969 if (a->cipher_list != NULL) {
1970 ssl_cipher_preference_list_free(a->cipher_list);
1971 }
1972 if (a->cipher_list_by_id != NULL) {
1973 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1974 }
1975 if (a->cipher_list_tls11 != NULL) {
1976 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1977 }
1978 if (a->cert != NULL) {
1979 ssl_cert_free(a->cert);
1980 }
1981 if (a->client_CA != NULL) {
1982 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1983 }
1984 if (a->extra_certs != NULL) {
1985 sk_X509_pop_free(a->extra_certs, X509_free);
1986 }
1987 if (a->srtp_profiles) {
1988 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1989 }
1990 if (a->psk_identity_hint) {
1991 OPENSSL_free(a->psk_identity_hint);
1992 }
1993 if (a->tlsext_ecpointformatlist) {
1994 OPENSSL_free(a->tlsext_ecpointformatlist);
1995 }
1996 if (a->tlsext_ellipticcurvelist) {
1997 OPENSSL_free(a->tlsext_ellipticcurvelist);
1998 }
1999 if (a->alpn_client_proto_list != NULL) {
2000 OPENSSL_free(a->alpn_client_proto_list);
2001 }
2002 if (a->tlsext_channel_id_private) {
2003 EVP_PKEY_free(a->tlsext_channel_id_private);
2004 }
2005 if (a->keylog_bio) {
2006 BIO_free(a->keylog_bio);
2007 }
Adam Langley95c29f32014-06-20 12:00:00 -07002008
Adam Langleyfcf25832014-12-18 17:42:32 -08002009 OPENSSL_free(a);
2010}
Adam Langley95c29f32014-06-20 12:00:00 -07002011
Adam Langleyfcf25832014-12-18 17:42:32 -08002012void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
2013 ctx->default_passwd_callback = cb;
2014}
Adam Langley95c29f32014-06-20 12:00:00 -07002015
Adam Langleyfcf25832014-12-18 17:42:32 -08002016void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
2017 ctx->default_passwd_callback_userdata = u;
2018}
Adam Langley95c29f32014-06-20 12:00:00 -07002019
Adam Langleyfcf25832014-12-18 17:42:32 -08002020void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
2021 int (*cb)(X509_STORE_CTX *, void *),
2022 void *arg) {
2023 ctx->app_verify_callback = cb;
2024 ctx->app_verify_arg = arg;
2025}
Adam Langley95c29f32014-06-20 12:00:00 -07002026
Adam Langleyfcf25832014-12-18 17:42:32 -08002027void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
2028 int (*cb)(int, X509_STORE_CTX *)) {
2029 ctx->verify_mode = mode;
2030 ctx->default_verify_callback = cb;
2031}
Adam Langley95c29f32014-06-20 12:00:00 -07002032
Adam Langleyfcf25832014-12-18 17:42:32 -08002033void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
2034 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2035}
Adam Langley1258b6a2014-06-20 12:00:00 -07002036
Adam Langleyfcf25832014-12-18 17:42:32 -08002037void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
2038 void *arg) {
2039 ssl_cert_set_cert_cb(c->cert, cb, arg);
2040}
David Benjamin859ec3c2014-09-02 16:29:36 -04002041
Adam Langleyfcf25832014-12-18 17:42:32 -08002042void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
2043 ssl_cert_set_cert_cb(s->cert, cb, arg);
2044}
Adam Langley95c29f32014-06-20 12:00:00 -07002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046static int ssl_has_key(SSL *s, size_t idx) {
2047 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2048 return cpk->x509 && cpk->privatekey;
2049}
David Benjamin033e5f42014-11-13 18:47:41 -05002050
David Benjaminf31e6812014-11-13 18:05:55 -05002051void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
Adam Langleyfcf25832014-12-18 17:42:32 -08002052 unsigned long *out_mask_a) {
2053 CERT *c = s->cert;
2054 int rsa_enc, rsa_sign, dh_tmp;
2055 unsigned long mask_k, mask_a;
2056 int have_ecc_cert, ecdsa_ok;
2057 int have_ecdh_tmp;
2058 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002059
Adam Langleyfcf25832014-12-18 17:42:32 -08002060 if (c == NULL) {
2061 /* TODO(davidben): Is this codepath possible? */
2062 *out_mask_k = 0;
2063 *out_mask_a = 0;
2064 return;
2065 }
Adam Langley95c29f32014-06-20 12:00:00 -07002066
Adam Langleyfcf25832014-12-18 17:42:32 -08002067 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002068
Adam Langleyfcf25832014-12-18 17:42:32 -08002069 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2070 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2071 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2072 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2073 mask_k = 0;
2074 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 if (rsa_enc) {
2077 mask_k |= SSL_kRSA;
2078 }
2079 if (dh_tmp) {
2080 mask_k |= SSL_kEDH;
2081 }
2082 if (rsa_enc || rsa_sign) {
2083 mask_a |= SSL_aRSA;
2084 }
Adam Langley95c29f32014-06-20 12:00:00 -07002085
Adam Langleyfcf25832014-12-18 17:42:32 -08002086 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002087
Adam Langleyfcf25832014-12-18 17:42:32 -08002088 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2089 * key usage extension and on the client's curve preferences. */
2090 if (have_ecc_cert) {
2091 x = c->pkeys[SSL_PKEY_ECC].x509;
2092 /* This call populates extension flags (ex_flags). */
2093 X509_check_purpose(x, -1, 0);
2094 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2095 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2096 : 1;
2097 if (!tls1_check_ec_cert(s, x)) {
2098 ecdsa_ok = 0;
2099 }
2100 if (ecdsa_ok) {
2101 mask_a |= SSL_aECDSA;
2102 }
2103 }
Adam Langley95c29f32014-06-20 12:00:00 -07002104
Adam Langleyfcf25832014-12-18 17:42:32 -08002105 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2106 * key, check it. */
2107 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
2108 mask_k |= SSL_kEECDH;
2109 }
Adam Langley95c29f32014-06-20 12:00:00 -07002110
Adam Langleyfcf25832014-12-18 17:42:32 -08002111 /* PSK requires a server callback. */
2112 if (s->psk_server_callback != NULL) {
2113 mask_k |= SSL_kPSK;
2114 mask_a |= SSL_aPSK;
2115 }
Adam Langley95c29f32014-06-20 12:00:00 -07002116
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 *out_mask_k = mask_k;
2118 *out_mask_a = mask_a;
2119}
Adam Langley95c29f32014-06-20 12:00:00 -07002120
2121/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2122#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002123 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002124
Adam Langleyfcf25832014-12-18 17:42:32 -08002125int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2126 unsigned long alg_a;
2127 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2128 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002129
Adam Langleyfcf25832014-12-18 17:42:32 -08002130 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002131
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 /* This call populates the ex_flags field correctly */
2133 X509_check_purpose(x, -1, 0);
2134 if (x->sig_alg && x->sig_alg->algorithm) {
2135 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2136 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2137 }
2138 if (alg_a & SSL_aECDSA) {
2139 /* key usage, if present, must allow signing */
2140 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2141 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2142 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2143 return 0;
2144 }
2145 }
Adam Langley95c29f32014-06-20 12:00:00 -07002146
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 return 1; /* all checks are ok */
2148}
Adam Langley95c29f32014-06-20 12:00:00 -07002149
Adam Langleyfcf25832014-12-18 17:42:32 -08002150static int ssl_get_server_cert_index(const SSL *s) {
2151 int idx;
2152 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2153 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2154 idx = SSL_PKEY_RSA_SIGN;
2155 }
2156 if (idx == -1) {
2157 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2158 }
2159 return idx;
2160}
Adam Langley95c29f32014-06-20 12:00:00 -07002161
Adam Langleyfcf25832014-12-18 17:42:32 -08002162CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2163 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002164
Adam Langleyfcf25832014-12-18 17:42:32 -08002165 /* This may or may not be an error. */
2166 if (i < 0) {
2167 return NULL;
2168 }
Adam Langley95c29f32014-06-20 12:00:00 -07002169
Adam Langleyfcf25832014-12-18 17:42:32 -08002170 /* May be NULL. */
2171 return &s->cert->pkeys[i];
2172}
Adam Langley95c29f32014-06-20 12:00:00 -07002173
Adam Langleyfcf25832014-12-18 17:42:32 -08002174EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2175 unsigned long alg_a;
2176 CERT *c;
2177 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002178
Adam Langleyfcf25832014-12-18 17:42:32 -08002179 alg_a = cipher->algorithm_auth;
2180 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002181
Adam Langleyfcf25832014-12-18 17:42:32 -08002182 if (alg_a & SSL_aRSA) {
2183 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2184 idx = SSL_PKEY_RSA_SIGN;
2185 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2186 idx = SSL_PKEY_RSA_ENC;
2187 }
2188 } else if ((alg_a & SSL_aECDSA) &&
2189 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2190 idx = SSL_PKEY_ECC;
2191 }
Adam Langley95c29f32014-06-20 12:00:00 -07002192
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 if (idx == -1) {
2194 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2195 return NULL;
2196 }
Adam Langley95c29f32014-06-20 12:00:00 -07002197
Adam Langleyfcf25832014-12-18 17:42:32 -08002198 return c->pkeys[idx].privatekey;
2199}
Adam Langley95c29f32014-06-20 12:00:00 -07002200
Adam Langleyfcf25832014-12-18 17:42:32 -08002201void ssl_update_cache(SSL *s, int mode) {
2202 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07002203
Adam Langleyfcf25832014-12-18 17:42:32 -08002204 /* If the session_id_length is 0, we are not supposed to cache it, and it
2205 * would be rather hard to do anyway :-) */
2206 if (s->session->session_id_length == 0) {
2207 return;
2208 }
Adam Langley95c29f32014-06-20 12:00:00 -07002209
Adam Langleyfcf25832014-12-18 17:42:32 -08002210 i = s->initial_ctx->session_cache_mode;
2211 if ((i & mode) && !s->hit &&
2212 ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2213 SSL_CTX_add_session(s->initial_ctx, s->session)) &&
2214 s->initial_ctx->new_session_cb != NULL) {
David Benjamin33639842015-02-09 03:34:47 -05002215 if (!s->initial_ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002216 SSL_SESSION_free(s->session);
2217 }
2218 }
Adam Langley95c29f32014-06-20 12:00:00 -07002219
Adam Langleyfcf25832014-12-18 17:42:32 -08002220 /* auto flush every 255 connections */
2221 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
2222 if ((((mode & SSL_SESS_CACHE_CLIENT)
2223 ? s->initial_ctx->stats.sess_connect_good
2224 : s->initial_ctx->stats.sess_accept_good) &
2225 0xff) == 0xff) {
2226 SSL_CTX_flush_sessions(s->initial_ctx, (unsigned long)time(NULL));
2227 }
2228 }
2229}
Adam Langley95c29f32014-06-20 12:00:00 -07002230
Adam Langleyfcf25832014-12-18 17:42:32 -08002231int SSL_get_error(const SSL *s, int i) {
2232 int reason;
2233 unsigned long l;
2234 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002235
Adam Langleyfcf25832014-12-18 17:42:32 -08002236 if (i > 0) {
2237 return SSL_ERROR_NONE;
2238 }
Adam Langley95c29f32014-06-20 12:00:00 -07002239
Adam Langleyfcf25832014-12-18 17:42:32 -08002240 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2241 * where we do encode the error */
2242 l = ERR_peek_error();
2243 if (l != 0) {
2244 if (ERR_GET_LIB(l) == ERR_LIB_SYS) {
2245 return SSL_ERROR_SYSCALL;
2246 }
2247 return SSL_ERROR_SSL;
2248 }
Adam Langley95c29f32014-06-20 12:00:00 -07002249
David Benjamin9a38e922015-01-22 16:06:11 -05002250 if (i == 0) {
2251 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2252 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2253 /* The socket was cleanly shut down with a close_notify. */
2254 return SSL_ERROR_ZERO_RETURN;
2255 }
2256 /* An EOF was observed which violates the protocol, and the underlying
2257 * transport does not participate in the error queue. Bubble up to the
2258 * caller. */
2259 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002260 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 if (SSL_want_session(s)) {
2263 return SSL_ERROR_PENDING_SESSION;
2264 }
Adam Langley95c29f32014-06-20 12:00:00 -07002265
Adam Langleyfcf25832014-12-18 17:42:32 -08002266 if (SSL_want_certificate(s)) {
2267 return SSL_ERROR_PENDING_CERTIFICATE;
2268 }
Adam Langley95c29f32014-06-20 12:00:00 -07002269
Adam Langleyfcf25832014-12-18 17:42:32 -08002270 if (SSL_want_read(s)) {
2271 bio = SSL_get_rbio(s);
2272 if (BIO_should_read(bio)) {
2273 return SSL_ERROR_WANT_READ;
2274 }
Adam Langley95c29f32014-06-20 12:00:00 -07002275
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 if (BIO_should_write(bio)) {
2277 /* This one doesn't make too much sense ... We never try to write to the
2278 * rbio, and an application program where rbio and wbio are separate
2279 * couldn't even know what it should wait for. However if we ever set
2280 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2281 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2282 * around that bug; so it might be safer to keep it. */
2283 return SSL_ERROR_WANT_WRITE;
2284 }
Adam Langley95c29f32014-06-20 12:00:00 -07002285
Adam Langleyfcf25832014-12-18 17:42:32 -08002286 if (BIO_should_io_special(bio)) {
2287 reason = BIO_get_retry_reason(bio);
2288 if (reason == BIO_RR_CONNECT) {
2289 return SSL_ERROR_WANT_CONNECT;
2290 }
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 if (reason == BIO_RR_ACCEPT) {
2293 return SSL_ERROR_WANT_ACCEPT;
2294 }
Adam Langley95c29f32014-06-20 12:00:00 -07002295
Adam Langleyfcf25832014-12-18 17:42:32 -08002296 return SSL_ERROR_SYSCALL; /* unknown */
2297 }
2298 }
Adam Langley95c29f32014-06-20 12:00:00 -07002299
Adam Langleyfcf25832014-12-18 17:42:32 -08002300 if (SSL_want_write(s)) {
2301 bio = SSL_get_wbio(s);
2302 if (BIO_should_write(bio)) {
2303 return SSL_ERROR_WANT_WRITE;
2304 }
Adam Langley95c29f32014-06-20 12:00:00 -07002305
Adam Langleyfcf25832014-12-18 17:42:32 -08002306 if (BIO_should_read(bio)) {
2307 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2308 return SSL_ERROR_WANT_READ;
2309 }
Adam Langley95c29f32014-06-20 12:00:00 -07002310
Adam Langleyfcf25832014-12-18 17:42:32 -08002311 if (BIO_should_io_special(bio)) {
2312 reason = BIO_get_retry_reason(bio);
2313 if (reason == BIO_RR_CONNECT) {
2314 return SSL_ERROR_WANT_CONNECT;
2315 }
Adam Langley95c29f32014-06-20 12:00:00 -07002316
Adam Langleyfcf25832014-12-18 17:42:32 -08002317 if (reason == BIO_RR_ACCEPT) {
2318 return SSL_ERROR_WANT_ACCEPT;
2319 }
Adam Langley95c29f32014-06-20 12:00:00 -07002320
Adam Langleyfcf25832014-12-18 17:42:32 -08002321 return SSL_ERROR_SYSCALL;
2322 }
2323 }
Adam Langley95c29f32014-06-20 12:00:00 -07002324
Adam Langleyfcf25832014-12-18 17:42:32 -08002325 if (SSL_want_x509_lookup(s)) {
2326 return SSL_ERROR_WANT_X509_LOOKUP;
2327 }
Adam Langley95c29f32014-06-20 12:00:00 -07002328
Adam Langleyfcf25832014-12-18 17:42:32 -08002329 if (SSL_want_channel_id_lookup(s)) {
2330 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2331 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002332
Adam Langleyfcf25832014-12-18 17:42:32 -08002333 return SSL_ERROR_SYSCALL;
2334}
Adam Langley0f4746e2014-08-13 12:26:32 -07002335
Adam Langleyfcf25832014-12-18 17:42:32 -08002336int SSL_do_handshake(SSL *s) {
2337 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002338
Adam Langleyfcf25832014-12-18 17:42:32 -08002339 if (s->handshake_func == NULL) {
2340 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2341 return -1;
2342 }
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langleyfcf25832014-12-18 17:42:32 -08002344 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002345
Adam Langleyfcf25832014-12-18 17:42:32 -08002346 if (SSL_in_init(s)) {
2347 ret = s->handshake_func(s);
2348 }
2349 return ret;
2350}
Adam Langley95c29f32014-06-20 12:00:00 -07002351
Adam Langleyfcf25832014-12-18 17:42:32 -08002352void SSL_set_accept_state(SSL *s) {
2353 s->server = 1;
2354 s->shutdown = 0;
2355 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2356 s->handshake_func = s->method->ssl_accept;
2357 /* clear the current cipher */
2358 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002359}
Adam Langley95c29f32014-06-20 12:00:00 -07002360
Adam Langleyfcf25832014-12-18 17:42:32 -08002361void SSL_set_connect_state(SSL *s) {
2362 s->server = 0;
2363 s->shutdown = 0;
2364 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2365 s->handshake_func = s->method->ssl_connect;
2366 /* clear the current cipher */
2367 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002368}
Adam Langley95c29f32014-06-20 12:00:00 -07002369
Adam Langleyfcf25832014-12-18 17:42:32 -08002370int ssl_undefined_function(SSL *s) {
2371 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2372 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2373 return 0;
2374}
Adam Langley95c29f32014-06-20 12:00:00 -07002375
Adam Langleyfcf25832014-12-18 17:42:32 -08002376int ssl_undefined_void_function(void) {
2377 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2378 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2379 return 0;
2380}
Adam Langley95c29f32014-06-20 12:00:00 -07002381
Adam Langleyfcf25832014-12-18 17:42:32 -08002382int ssl_undefined_const_function(const SSL *s) {
2383 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2384 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2385 return 0;
2386}
Adam Langley95c29f32014-06-20 12:00:00 -07002387
Adam Langleyfcf25832014-12-18 17:42:32 -08002388static const char *ssl_get_version(int version) {
2389 switch (version) {
2390 case TLS1_2_VERSION:
2391 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002392
Adam Langleyfcf25832014-12-18 17:42:32 -08002393 case TLS1_1_VERSION:
2394 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langleyfcf25832014-12-18 17:42:32 -08002396 case TLS1_VERSION:
2397 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002398
Adam Langleyfcf25832014-12-18 17:42:32 -08002399 case SSL3_VERSION:
2400 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002401
Adam Langleyfcf25832014-12-18 17:42:32 -08002402 default:
2403 return "unknown";
2404 }
2405}
Adam Langley95c29f32014-06-20 12:00:00 -07002406
Adam Langleyfcf25832014-12-18 17:42:32 -08002407const char *SSL_get_version(const SSL *s) {
2408 return ssl_get_version(s->version);
2409}
Adam Langley95c29f32014-06-20 12:00:00 -07002410
Adam Langleyfcf25832014-12-18 17:42:32 -08002411const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2412 return ssl_get_version(sess->ssl_version);
2413}
Adam Langley95c29f32014-06-20 12:00:00 -07002414
Adam Langleyfcf25832014-12-18 17:42:32 -08002415void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002416 if (s->aead_read_ctx != NULL) {
2417 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2418 OPENSSL_free(s->aead_read_ctx);
2419 s->aead_read_ctx = NULL;
2420 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422 if (s->aead_write_ctx != NULL) {
2423 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2424 OPENSSL_free(s->aead_write_ctx);
2425 s->aead_write_ctx = NULL;
2426 }
2427}
Adam Langley95c29f32014-06-20 12:00:00 -07002428
Adam Langleyfcf25832014-12-18 17:42:32 -08002429X509 *SSL_get_certificate(const SSL *s) {
2430 if (s->cert != NULL) {
2431 return s->cert->key->x509;
2432 }
2433
2434 return NULL;
2435}
2436
2437EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2438 if (s->cert != NULL) {
2439 return s->cert->key->privatekey;
2440 }
2441
2442 return NULL;
2443}
2444
2445X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2446 if (ctx->cert != NULL) {
2447 return ctx->cert->key->x509;
2448 }
2449
2450 return NULL;
2451}
2452
2453EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2454 if (ctx->cert != NULL) {
2455 return ctx->cert->key->privatekey;
2456 }
2457
2458 return NULL;
2459}
2460
2461const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2462 if (s->session != NULL && s->session->cipher != NULL) {
2463 return s->session->cipher;
2464 }
2465
2466 return NULL;
2467}
2468
2469const void *SSL_get_current_compression(SSL *s) { return NULL; }
2470
2471const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2472
2473int ssl_init_wbio_buffer(SSL *s, int push) {
2474 BIO *bbio;
2475
2476 if (s->bbio == NULL) {
2477 bbio = BIO_new(BIO_f_buffer());
2478 if (bbio == NULL) {
2479 return 0;
2480 }
2481 s->bbio = bbio;
2482 } else {
2483 bbio = s->bbio;
2484 if (s->bbio == s->wbio) {
2485 s->wbio = BIO_pop(s->wbio);
2486 }
2487 }
2488
2489 BIO_reset(bbio);
2490 if (!BIO_set_read_buffer_size(bbio, 1)) {
2491 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2492 return 0;
2493 }
2494
2495 if (push) {
2496 if (s->wbio != bbio) {
2497 s->wbio = BIO_push(bbio, s->wbio);
2498 }
2499 } else {
2500 if (s->wbio == bbio) {
2501 s->wbio = BIO_pop(bbio);
2502 }
2503 }
2504
2505 return 1;
2506}
2507
2508void ssl_free_wbio_buffer(SSL *s) {
2509 if (s->bbio == NULL) {
2510 return;
2511 }
2512
2513 if (s->bbio == s->wbio) {
2514 /* remove buffering */
2515 s->wbio = BIO_pop(s->wbio);
2516 }
2517
2518 BIO_free(s->bbio);
2519 s->bbio = NULL;
2520}
2521
2522void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2523 ctx->quiet_shutdown = mode;
2524}
2525
2526int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2527 return ctx->quiet_shutdown;
2528}
2529
2530void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2531
2532int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2533
2534void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2535
2536int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2537
2538int SSL_version(const SSL *s) { return s->version; }
2539
2540SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2541
2542SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2543 if (ssl->ctx == ctx) {
2544 return ssl->ctx;
2545 }
2546
2547 if (ctx == NULL) {
2548 ctx = ssl->initial_ctx;
2549 }
2550
2551 if (ssl->cert != NULL) {
2552 ssl_cert_free(ssl->cert);
2553 }
2554
2555 ssl->cert = ssl_cert_dup(ctx->cert);
2556 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2557 if (ssl->ctx != NULL) {
2558 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2559 }
2560 ssl->ctx = ctx;
2561
2562 ssl->sid_ctx_length = ctx->sid_ctx_length;
2563 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2564 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2565
2566 return ssl->ctx;
2567}
2568
2569int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2570 return X509_STORE_set_default_paths(ctx->cert_store);
2571}
Adam Langley95c29f32014-06-20 12:00:00 -07002572
2573int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002574 const char *CApath) {
2575 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2576}
Adam Langley95c29f32014-06-20 12:00:00 -07002577
2578void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002579 void (*cb)(const SSL *ssl, int type, int val)) {
2580 ssl->info_callback = cb;
2581}
Adam Langley95c29f32014-06-20 12:00:00 -07002582
Adam Langleyfcf25832014-12-18 17:42:32 -08002583void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2584 int /*val*/) {
2585 return ssl->info_callback;
2586}
Adam Langley95c29f32014-06-20 12:00:00 -07002587
Adam Langleyfcf25832014-12-18 17:42:32 -08002588int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002589
Adam Langleyfcf25832014-12-18 17:42:32 -08002590void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002591
Adam Langleyfcf25832014-12-18 17:42:32 -08002592void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002593
Adam Langleyfcf25832014-12-18 17:42:32 -08002594long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002595
Adam Langleyfcf25832014-12-18 17:42:32 -08002596int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2597 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2598 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2599 dup_func, free_func);
2600}
Adam Langley95c29f32014-06-20 12:00:00 -07002601
Adam Langleyfcf25832014-12-18 17:42:32 -08002602int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2603 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2604}
Adam Langley95c29f32014-06-20 12:00:00 -07002605
Adam Langleyfcf25832014-12-18 17:42:32 -08002606void *SSL_get_ex_data(const SSL *s, int idx) {
2607 return CRYPTO_get_ex_data(&s->ex_data, idx);
2608}
Adam Langley95c29f32014-06-20 12:00:00 -07002609
Adam Langleyfcf25832014-12-18 17:42:32 -08002610int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2611 CRYPTO_EX_dup *dup_func,
2612 CRYPTO_EX_free *free_func) {
2613 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2614 dup_func, free_func);
2615}
Adam Langley95c29f32014-06-20 12:00:00 -07002616
Adam Langleyfcf25832014-12-18 17:42:32 -08002617int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2618 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2619}
Adam Langley95c29f32014-06-20 12:00:00 -07002620
Adam Langleyfcf25832014-12-18 17:42:32 -08002621void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2622 return CRYPTO_get_ex_data(&s->ex_data, idx);
2623}
Adam Langley95c29f32014-06-20 12:00:00 -07002624
Adam Langleyfcf25832014-12-18 17:42:32 -08002625int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002626
Adam Langleyfcf25832014-12-18 17:42:32 -08002627X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2628 return ctx->cert_store;
2629}
Adam Langley95c29f32014-06-20 12:00:00 -07002630
Adam Langleyfcf25832014-12-18 17:42:32 -08002631void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2632 if (ctx->cert_store != NULL) {
2633 X509_STORE_free(ctx->cert_store);
2634 }
2635 ctx->cert_store = store;
2636}
Adam Langley95c29f32014-06-20 12:00:00 -07002637
Adam Langleyfcf25832014-12-18 17:42:32 -08002638int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002639
Adam Langleyfcf25832014-12-18 17:42:32 -08002640void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2641 RSA *(*cb)(SSL *ssl, int is_export,
2642 int keylength)) {
2643 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2644}
Adam Langley95c29f32014-06-20 12:00:00 -07002645
Adam Langleyfcf25832014-12-18 17:42:32 -08002646void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2647 int keylength)) {
2648 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2649}
2650
2651void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2652 DH *(*dh)(SSL *ssl, int is_export,
2653 int keylength)) {
2654 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2655}
2656
2657void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2658 int keylength)) {
2659 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2660}
2661
2662void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2663 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2664 int keylength)) {
2665 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2666}
2667
2668void SSL_set_tmp_ecdh_callback(SSL *ssl,
2669 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2670 int keylength)) {
2671 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2672}
2673
2674int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2675 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2676 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2677 SSL_R_DATA_LENGTH_TOO_LONG);
2678 return 0;
2679 }
2680
2681 if (ctx->psk_identity_hint != NULL) {
2682 OPENSSL_free(ctx->psk_identity_hint);
2683 }
2684
2685 if (identity_hint != NULL) {
2686 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2687 if (ctx->psk_identity_hint == NULL) {
2688 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002689 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002690 } else {
2691 ctx->psk_identity_hint = NULL;
2692 }
Adam Langley95c29f32014-06-20 12:00:00 -07002693
Adam Langleyfcf25832014-12-18 17:42:32 -08002694 return 1;
2695}
2696
2697int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2698 if (s == NULL) {
2699 return 0;
2700 }
2701
2702 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2703 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2704 SSL_R_DATA_LENGTH_TOO_LONG);
2705 return 0;
2706 }
2707
2708 /* Clear currently configured hint, if any. */
2709 if (s->psk_identity_hint != NULL) {
2710 OPENSSL_free(s->psk_identity_hint);
2711 s->psk_identity_hint = NULL;
2712 }
2713
2714 if (identity_hint != NULL) {
2715 s->psk_identity_hint = BUF_strdup(identity_hint);
2716 if (s->psk_identity_hint == NULL) {
2717 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002718 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002719 }
Adam Langley95c29f32014-06-20 12:00:00 -07002720
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 return 1;
2722}
Adam Langley95c29f32014-06-20 12:00:00 -07002723
Adam Langleyfcf25832014-12-18 17:42:32 -08002724const char *SSL_get_psk_identity_hint(const SSL *s) {
2725 if (s == NULL) {
2726 return NULL;
2727 }
2728 return s->psk_identity_hint;
2729}
Adam Langley95c29f32014-06-20 12:00:00 -07002730
Adam Langleyfcf25832014-12-18 17:42:32 -08002731const char *SSL_get_psk_identity(const SSL *s) {
2732 if (s == NULL || s->session == NULL) {
2733 return NULL;
2734 }
Adam Langley95c29f32014-06-20 12:00:00 -07002735
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 return s->session->psk_identity;
2737}
Adam Langley95c29f32014-06-20 12:00:00 -07002738
Adam Langleyfcf25832014-12-18 17:42:32 -08002739void SSL_set_psk_client_callback(
2740 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2741 unsigned int max_identity_len, uint8_t *psk,
2742 unsigned int max_psk_len)) {
2743 s->psk_client_callback = cb;
2744}
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Adam Langleyfcf25832014-12-18 17:42:32 -08002746void SSL_CTX_set_psk_client_callback(
2747 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2748 unsigned int max_identity_len,
2749 uint8_t *psk, unsigned int max_psk_len)) {
2750 ctx->psk_client_callback = cb;
2751}
Adam Langley95c29f32014-06-20 12:00:00 -07002752
Adam Langleyfcf25832014-12-18 17:42:32 -08002753void SSL_set_psk_server_callback(
2754 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2755 unsigned int max_psk_len)) {
2756 s->psk_server_callback = cb;
2757}
Adam Langley95c29f32014-06-20 12:00:00 -07002758
Adam Langleyfcf25832014-12-18 17:42:32 -08002759void SSL_CTX_set_psk_server_callback(
2760 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2761 uint8_t *psk, unsigned int max_psk_len)) {
2762 ctx->psk_server_callback = cb;
2763}
Adam Langley95c29f32014-06-20 12:00:00 -07002764
Adam Langleyfcf25832014-12-18 17:42:32 -08002765void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2766 ctx->min_version = version;
2767}
Adam Langley95c29f32014-06-20 12:00:00 -07002768
Adam Langleyfcf25832014-12-18 17:42:32 -08002769void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2770 ctx->max_version = version;
2771}
Adam Langley0289c732014-06-20 12:00:00 -07002772
Adam Langleyfcf25832014-12-18 17:42:32 -08002773void SSL_set_min_version(SSL *ssl, uint16_t version) {
2774 ssl->min_version = version;
2775}
Adam Langley0289c732014-06-20 12:00:00 -07002776
Adam Langleyfcf25832014-12-18 17:42:32 -08002777void SSL_set_max_version(SSL *ssl, uint16_t version) {
2778 ssl->max_version = version;
2779}
Adam Langley95c29f32014-06-20 12:00:00 -07002780
Adam Langleyfcf25832014-12-18 17:42:32 -08002781void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2782 void (*cb)(int write_p, int version,
2783 int content_type, const void *buf,
2784 size_t len, SSL *ssl, void *arg)) {
2785 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2786}
2787void SSL_set_msg_callback(SSL *ssl,
2788 void (*cb)(int write_p, int version, int content_type,
2789 const void *buf, size_t len, SSL *ssl,
2790 void *arg)) {
2791 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2792}
Adam Langley95c29f32014-06-20 12:00:00 -07002793
Adam Langleyfcf25832014-12-18 17:42:32 -08002794void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2795 if (ctx->keylog_bio != NULL) {
2796 BIO_free(ctx->keylog_bio);
2797 }
2798 ctx->keylog_bio = keylog_bio;
2799}
Adam Langley95c29f32014-06-20 12:00:00 -07002800
Adam Langleyfcf25832014-12-18 17:42:32 -08002801static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2802 static const char hextable[] = "0123456789abcdef";
2803 uint8_t *out;
2804 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002805
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2807 return 0;
2808 }
Adam Langley95c29f32014-06-20 12:00:00 -07002809
Adam Langleyfcf25832014-12-18 17:42:32 -08002810 for (i = 0; i < in_len; i++) {
2811 *(out++) = (uint8_t)hextable[in[i] >> 4];
2812 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2813 }
Adam Langley95c29f32014-06-20 12:00:00 -07002814
Adam Langleyfcf25832014-12-18 17:42:32 -08002815 return 1;
2816}
David Benjamin859ec3c2014-09-02 16:29:36 -04002817
2818int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 const uint8_t *encrypted_premaster,
2820 size_t encrypted_premaster_len,
2821 const uint8_t *premaster,
2822 size_t premaster_len) {
2823 BIO *bio = ctx->keylog_bio;
2824 CBB cbb;
2825 uint8_t *out;
2826 size_t out_len;
2827 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002828
Adam Langleyfcf25832014-12-18 17:42:32 -08002829 if (bio == NULL) {
2830 return 1;
2831 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002832
Adam Langleyfcf25832014-12-18 17:42:32 -08002833 if (encrypted_premaster_len < 8) {
2834 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2835 ERR_R_INTERNAL_ERROR);
2836 return 0;
2837 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002838
Adam Langleyfcf25832014-12-18 17:42:32 -08002839 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2840 return 0;
2841 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002842
Adam Langleyfcf25832014-12-18 17:42:32 -08002843 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2844 /* Only the first 8 bytes of the encrypted premaster secret are
2845 * logged. */
2846 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2847 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2848 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2849 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2850 !CBB_finish(&cbb, &out, &out_len)) {
2851 CBB_cleanup(&cbb);
2852 return 0;
2853 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002854
Adam Langleyfcf25832014-12-18 17:42:32 -08002855 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2856 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2857 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002858
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 OPENSSL_free(out);
2860 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002861}
2862
Adam Langleyfcf25832014-12-18 17:42:32 -08002863int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2864 size_t client_random_len, const uint8_t *master,
2865 size_t master_len) {
2866 BIO *bio = ctx->keylog_bio;
2867 CBB cbb;
2868 uint8_t *out;
2869 size_t out_len;
2870 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002871
Adam Langleyfcf25832014-12-18 17:42:32 -08002872 if (bio == NULL) {
2873 return 1;
2874 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002875
Adam Langleyfcf25832014-12-18 17:42:32 -08002876 if (client_random_len != 32) {
2877 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2878 return 0;
2879 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002880
Adam Langleyfcf25832014-12-18 17:42:32 -08002881 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2882 return 0;
2883 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002884
Adam Langleyfcf25832014-12-18 17:42:32 -08002885 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2886 !cbb_add_hex(&cbb, client_random, 32) ||
2887 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2888 !cbb_add_hex(&cbb, master, master_len) ||
2889 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2890 !CBB_finish(&cbb, &out, &out_len)) {
2891 CBB_cleanup(&cbb);
2892 return 0;
2893 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002894
Adam Langleyfcf25832014-12-18 17:42:32 -08002895 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2896 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2897 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002898
Adam Langleyfcf25832014-12-18 17:42:32 -08002899 OPENSSL_free(out);
2900 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002901}
2902
Adam Langleyfcf25832014-12-18 17:42:32 -08002903int SSL_cutthrough_complete(const SSL *s) {
David Benjaminccf74f82015-02-09 00:01:31 -05002904 return s->s3->tmp.cutthrough_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08002905}
Adam Langley95c29f32014-06-20 12:00:00 -07002906
Adam Langleyfcf25832014-12-18 17:42:32 -08002907void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2908 size_t *ssl_session_size) {
2909 *ssl_size = sizeof(SSL);
2910 *ssl_ctx_size = sizeof(SSL_CTX);
2911 *ssl_session_size = sizeof(SSL_SESSION);
2912}
Feng Lu41aa3252014-11-21 22:47:56 -08002913
Adam Langleyfcf25832014-12-18 17:42:32 -08002914int ssl3_can_cutthrough(const SSL *s) {
2915 const SSL_CIPHER *c;
2916
2917 /* require a strong enough cipher */
2918 if (SSL_get_cipher_bits(s, NULL) < 128) {
2919 return 0;
2920 }
2921
2922 /* require ALPN or NPN extension */
2923 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen) {
2924 return 0;
2925 }
2926
2927 /* require a forward-secret cipher */
2928 c = SSL_get_current_cipher(s);
2929 if (!c ||
2930 (c->algorithm_mkey != SSL_kEDH && c->algorithm_mkey != SSL_kEECDH)) {
2931 return 0;
2932 }
2933
2934 return 1;
2935}
2936
2937const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2938 switch (version) {
2939 case SSL3_VERSION:
2940 return &SSLv3_enc_data;
2941
2942 case TLS1_VERSION:
2943 return &TLSv1_enc_data;
2944
2945 case TLS1_1_VERSION:
2946 return &TLSv1_1_enc_data;
2947
2948 case TLS1_2_VERSION:
2949 return &TLSv1_2_enc_data;
2950
2951 case DTLS1_VERSION:
2952 return &DTLSv1_enc_data;
2953
2954 case DTLS1_2_VERSION:
2955 return &DTLSv1_2_enc_data;
2956
2957 default:
2958 return NULL;
2959 }
2960}
2961
2962uint16_t ssl3_get_max_server_version(const SSL *s) {
2963 uint16_t max_version;
2964
2965 if (SSL_IS_DTLS(s)) {
2966 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2967 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2968 return DTLS1_2_VERSION;
2969 }
2970 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2971 return DTLS1_VERSION;
2972 }
2973 return 0;
2974 }
2975
2976 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2977 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2978 return TLS1_2_VERSION;
2979 }
2980 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2981 return TLS1_1_VERSION;
2982 }
2983 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2984 return TLS1_VERSION;
2985 }
2986 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2987 return SSL3_VERSION;
2988 }
2989 return 0;
2990}
2991
2992uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2993 uint16_t version = 0;
2994
2995 if (SSL_IS_DTLS(s)) {
2996 /* Clamp client_version to max_version. */
2997 if (s->max_version != 0 && client_version < s->max_version) {
2998 client_version = s->max_version;
2999 }
3000
3001 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
3002 version = DTLS1_2_VERSION;
3003 } else if (client_version <= DTLS1_VERSION &&
3004 !(s->options & SSL_OP_NO_DTLSv1)) {
3005 version = DTLS1_VERSION;
3006 }
3007
3008 /* Check against min_version. */
3009 if (version != 0 && s->min_version != 0 && version > s->min_version) {
3010 return 0;
3011 }
3012 return version;
3013 } else {
3014 /* Clamp client_version to max_version. */
3015 if (s->max_version != 0 && client_version > s->max_version) {
3016 client_version = s->max_version;
3017 }
3018
3019 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
3020 version = TLS1_2_VERSION;
3021 } else if (client_version >= TLS1_1_VERSION &&
3022 !(s->options & SSL_OP_NO_TLSv1_1)) {
3023 version = TLS1_1_VERSION;
3024 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
3025 version = TLS1_VERSION;
3026 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
3027 version = SSL3_VERSION;
3028 }
3029
3030 /* Check against min_version. */
3031 if (version != 0 && s->min_version != 0 && version < s->min_version) {
3032 return 0;
3033 }
3034 return version;
3035 }
3036}
3037
3038uint16_t ssl3_get_max_client_version(SSL *s) {
3039 unsigned long options = s->options;
3040 uint16_t version = 0;
3041
3042 /* OpenSSL's API for controlling versions entails blacklisting individual
3043 * protocols. This has two problems. First, on the client, the protocol can
3044 * only express a contiguous range of versions. Second, a library consumer
3045 * trying to set a maximum version cannot disable protocol versions that get
3046 * added in a future version of the library.
3047 *
3048 * To account for both of these, OpenSSL interprets the client-side bitmask
3049 * as a min/max range by picking the lowest contiguous non-empty range of
3050 * enabled protocols. Note that this means it is impossible to set a maximum
3051 * version of TLS 1.2 in a future-proof way.
3052 *
3053 * By this scheme, the maximum version is the lowest version V such that V is
3054 * enabled and V+1 is disabled or unimplemented. */
3055 if (SSL_IS_DTLS(s)) {
3056 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3057 version = DTLS1_2_VERSION;
3058 }
3059 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3060 version = DTLS1_VERSION;
3061 }
3062 if (s->max_version != 0 && version < s->max_version) {
3063 version = s->max_version;
3064 }
3065 } else {
3066 if (!(options & SSL_OP_NO_TLSv1_2)) {
3067 version = TLS1_2_VERSION;
3068 }
3069 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3070 version = TLS1_1_VERSION;
3071 }
3072 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3073 version = TLS1_VERSION;
3074 }
3075 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3076 version = SSL3_VERSION;
3077 }
3078 if (s->max_version != 0 && version > s->max_version) {
3079 version = s->max_version;
3080 }
3081 }
3082
3083 return version;
3084}
3085
3086int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3087 if (SSL_IS_DTLS(s)) {
3088 if (s->max_version != 0 && version < s->max_version) {
3089 return 0;
3090 }
3091 if (s->min_version != 0 && version > s->min_version) {
3092 return 0;
3093 }
3094
3095 switch (version) {
3096 case DTLS1_VERSION:
3097 return !(s->options & SSL_OP_NO_DTLSv1);
3098
3099 case DTLS1_2_VERSION:
3100 return !(s->options & SSL_OP_NO_DTLSv1_2);
3101
3102 default:
3103 return 0;
3104 }
3105 } else {
3106 if (s->max_version != 0 && version > s->max_version) {
3107 return 0;
3108 }
3109 if (s->min_version != 0 && version < s->min_version) {
3110 return 0;
3111 }
3112
3113 switch (version) {
3114 case SSL3_VERSION:
3115 return !(s->options & SSL_OP_NO_SSLv3);
3116
3117 case TLS1_VERSION:
3118 return !(s->options & SSL_OP_NO_TLSv1);
3119
3120 case TLS1_1_VERSION:
3121 return !(s->options & SSL_OP_NO_TLSv1_1);
3122
3123 case TLS1_2_VERSION:
3124 return !(s->options & SSL_OP_NO_TLSv1_2);
3125
3126 default:
3127 return 0;
3128 }
3129 }
3130}
3131
David Benjaminea72bd02014-12-21 21:27:41 -05003132uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3133 if (!SSL_IS_DTLS(s)) {
3134 return wire_version;
3135 }
3136
3137 uint16_t tls_version = ~wire_version;
3138 uint16_t version = tls_version + 0x0201;
3139 /* If either component overflowed, clamp it so comparisons still work. */
3140 if ((version >> 8) < (tls_version >> 8)) {
3141 version = 0xff00 | (version & 0xff);
3142 }
3143 if ((version & 0xff) < (tls_version & 0xff)) {
3144 version = (version & 0xff00) | 0xff;
3145 }
3146 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3147 if (version == TLS1_VERSION) {
3148 version = TLS1_1_VERSION;
3149 }
3150 return version;
3151}
3152
Adam Langleyfcf25832014-12-18 17:42:32 -08003153int SSL_cache_hit(SSL *s) { return s->hit; }
3154
3155int SSL_is_server(SSL *s) { return s->server; }
3156
3157void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3158 s->fastradio_padding = on_off;
3159}