blob: 62ddaeb04ba0ee4397425c2cc0b56fd61e4007d4 [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) {
2215 CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2216 if (!s->initial_ctx->new_session_cb(s, s->session)) {
2217 SSL_SESSION_free(s->session);
2218 }
2219 }
Adam Langley95c29f32014-06-20 12:00:00 -07002220
Adam Langleyfcf25832014-12-18 17:42:32 -08002221 /* auto flush every 255 connections */
2222 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
2223 if ((((mode & SSL_SESS_CACHE_CLIENT)
2224 ? s->initial_ctx->stats.sess_connect_good
2225 : s->initial_ctx->stats.sess_accept_good) &
2226 0xff) == 0xff) {
2227 SSL_CTX_flush_sessions(s->initial_ctx, (unsigned long)time(NULL));
2228 }
2229 }
2230}
Adam Langley95c29f32014-06-20 12:00:00 -07002231
Adam Langleyfcf25832014-12-18 17:42:32 -08002232int SSL_get_error(const SSL *s, int i) {
2233 int reason;
2234 unsigned long l;
2235 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002236
Adam Langleyfcf25832014-12-18 17:42:32 -08002237 if (i > 0) {
2238 return SSL_ERROR_NONE;
2239 }
Adam Langley95c29f32014-06-20 12:00:00 -07002240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2242 * where we do encode the error */
2243 l = ERR_peek_error();
2244 if (l != 0) {
2245 if (ERR_GET_LIB(l) == ERR_LIB_SYS) {
2246 return SSL_ERROR_SYSCALL;
2247 }
2248 return SSL_ERROR_SSL;
2249 }
Adam Langley95c29f32014-06-20 12:00:00 -07002250
David Benjamin9a38e922015-01-22 16:06:11 -05002251 if (i == 0) {
2252 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2253 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2254 /* The socket was cleanly shut down with a close_notify. */
2255 return SSL_ERROR_ZERO_RETURN;
2256 }
2257 /* An EOF was observed which violates the protocol, and the underlying
2258 * transport does not participate in the error queue. Bubble up to the
2259 * caller. */
2260 return SSL_ERROR_SYSCALL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002261 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002262
Adam Langleyfcf25832014-12-18 17:42:32 -08002263 if (SSL_want_session(s)) {
2264 return SSL_ERROR_PENDING_SESSION;
2265 }
Adam Langley95c29f32014-06-20 12:00:00 -07002266
Adam Langleyfcf25832014-12-18 17:42:32 -08002267 if (SSL_want_certificate(s)) {
2268 return SSL_ERROR_PENDING_CERTIFICATE;
2269 }
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Adam Langleyfcf25832014-12-18 17:42:32 -08002271 if (SSL_want_read(s)) {
2272 bio = SSL_get_rbio(s);
2273 if (BIO_should_read(bio)) {
2274 return SSL_ERROR_WANT_READ;
2275 }
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 if (BIO_should_write(bio)) {
2278 /* This one doesn't make too much sense ... We never try to write to the
2279 * rbio, and an application program where rbio and wbio are separate
2280 * couldn't even know what it should wait for. However if we ever set
2281 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2282 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2283 * around that bug; so it might be safer to keep it. */
2284 return SSL_ERROR_WANT_WRITE;
2285 }
Adam Langley95c29f32014-06-20 12:00:00 -07002286
Adam Langleyfcf25832014-12-18 17:42:32 -08002287 if (BIO_should_io_special(bio)) {
2288 reason = BIO_get_retry_reason(bio);
2289 if (reason == BIO_RR_CONNECT) {
2290 return SSL_ERROR_WANT_CONNECT;
2291 }
Adam Langley95c29f32014-06-20 12:00:00 -07002292
Adam Langleyfcf25832014-12-18 17:42:32 -08002293 if (reason == BIO_RR_ACCEPT) {
2294 return SSL_ERROR_WANT_ACCEPT;
2295 }
Adam Langley95c29f32014-06-20 12:00:00 -07002296
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 return SSL_ERROR_SYSCALL; /* unknown */
2298 }
2299 }
Adam Langley95c29f32014-06-20 12:00:00 -07002300
Adam Langleyfcf25832014-12-18 17:42:32 -08002301 if (SSL_want_write(s)) {
2302 bio = SSL_get_wbio(s);
2303 if (BIO_should_write(bio)) {
2304 return SSL_ERROR_WANT_WRITE;
2305 }
Adam Langley95c29f32014-06-20 12:00:00 -07002306
Adam Langleyfcf25832014-12-18 17:42:32 -08002307 if (BIO_should_read(bio)) {
2308 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2309 return SSL_ERROR_WANT_READ;
2310 }
Adam Langley95c29f32014-06-20 12:00:00 -07002311
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 if (BIO_should_io_special(bio)) {
2313 reason = BIO_get_retry_reason(bio);
2314 if (reason == BIO_RR_CONNECT) {
2315 return SSL_ERROR_WANT_CONNECT;
2316 }
Adam Langley95c29f32014-06-20 12:00:00 -07002317
Adam Langleyfcf25832014-12-18 17:42:32 -08002318 if (reason == BIO_RR_ACCEPT) {
2319 return SSL_ERROR_WANT_ACCEPT;
2320 }
Adam Langley95c29f32014-06-20 12:00:00 -07002321
Adam Langleyfcf25832014-12-18 17:42:32 -08002322 return SSL_ERROR_SYSCALL;
2323 }
2324 }
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 if (SSL_want_x509_lookup(s)) {
2327 return SSL_ERROR_WANT_X509_LOOKUP;
2328 }
Adam Langley95c29f32014-06-20 12:00:00 -07002329
Adam Langleyfcf25832014-12-18 17:42:32 -08002330 if (SSL_want_channel_id_lookup(s)) {
2331 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2332 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002333
Adam Langleyfcf25832014-12-18 17:42:32 -08002334 return SSL_ERROR_SYSCALL;
2335}
Adam Langley0f4746e2014-08-13 12:26:32 -07002336
Adam Langleyfcf25832014-12-18 17:42:32 -08002337int SSL_do_handshake(SSL *s) {
2338 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002339
Adam Langleyfcf25832014-12-18 17:42:32 -08002340 if (s->handshake_func == NULL) {
2341 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2342 return -1;
2343 }
Adam Langley95c29f32014-06-20 12:00:00 -07002344
Adam Langleyfcf25832014-12-18 17:42:32 -08002345 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langleyfcf25832014-12-18 17:42:32 -08002347 if (SSL_in_init(s)) {
2348 ret = s->handshake_func(s);
2349 }
2350 return ret;
2351}
Adam Langley95c29f32014-06-20 12:00:00 -07002352
Adam Langleyfcf25832014-12-18 17:42:32 -08002353void SSL_set_accept_state(SSL *s) {
2354 s->server = 1;
2355 s->shutdown = 0;
2356 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2357 s->handshake_func = s->method->ssl_accept;
2358 /* clear the current cipher */
2359 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002360}
Adam Langley95c29f32014-06-20 12:00:00 -07002361
Adam Langleyfcf25832014-12-18 17:42:32 -08002362void SSL_set_connect_state(SSL *s) {
2363 s->server = 0;
2364 s->shutdown = 0;
2365 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2366 s->handshake_func = s->method->ssl_connect;
2367 /* clear the current cipher */
2368 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002369}
Adam Langley95c29f32014-06-20 12:00:00 -07002370
Adam Langleyfcf25832014-12-18 17:42:32 -08002371int ssl_undefined_function(SSL *s) {
2372 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2373 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2374 return 0;
2375}
Adam Langley95c29f32014-06-20 12:00:00 -07002376
Adam Langleyfcf25832014-12-18 17:42:32 -08002377int ssl_undefined_void_function(void) {
2378 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2379 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2380 return 0;
2381}
Adam Langley95c29f32014-06-20 12:00:00 -07002382
Adam Langleyfcf25832014-12-18 17:42:32 -08002383int ssl_undefined_const_function(const SSL *s) {
2384 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2385 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2386 return 0;
2387}
Adam Langley95c29f32014-06-20 12:00:00 -07002388
Adam Langleyfcf25832014-12-18 17:42:32 -08002389static const char *ssl_get_version(int version) {
2390 switch (version) {
2391 case TLS1_2_VERSION:
2392 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002393
Adam Langleyfcf25832014-12-18 17:42:32 -08002394 case TLS1_1_VERSION:
2395 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002396
Adam Langleyfcf25832014-12-18 17:42:32 -08002397 case TLS1_VERSION:
2398 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400 case SSL3_VERSION:
2401 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002402
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 default:
2404 return "unknown";
2405 }
2406}
Adam Langley95c29f32014-06-20 12:00:00 -07002407
Adam Langleyfcf25832014-12-18 17:42:32 -08002408const char *SSL_get_version(const SSL *s) {
2409 return ssl_get_version(s->version);
2410}
Adam Langley95c29f32014-06-20 12:00:00 -07002411
Adam Langleyfcf25832014-12-18 17:42:32 -08002412const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2413 return ssl_get_version(sess->ssl_version);
2414}
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002417 if (s->aead_read_ctx != NULL) {
2418 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2419 OPENSSL_free(s->aead_read_ctx);
2420 s->aead_read_ctx = NULL;
2421 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002422
Adam Langleyfcf25832014-12-18 17:42:32 -08002423 if (s->aead_write_ctx != NULL) {
2424 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2425 OPENSSL_free(s->aead_write_ctx);
2426 s->aead_write_ctx = NULL;
2427 }
2428}
Adam Langley95c29f32014-06-20 12:00:00 -07002429
Adam Langleyfcf25832014-12-18 17:42:32 -08002430X509 *SSL_get_certificate(const SSL *s) {
2431 if (s->cert != NULL) {
2432 return s->cert->key->x509;
2433 }
2434
2435 return NULL;
2436}
2437
2438EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2439 if (s->cert != NULL) {
2440 return s->cert->key->privatekey;
2441 }
2442
2443 return NULL;
2444}
2445
2446X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2447 if (ctx->cert != NULL) {
2448 return ctx->cert->key->x509;
2449 }
2450
2451 return NULL;
2452}
2453
2454EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2455 if (ctx->cert != NULL) {
2456 return ctx->cert->key->privatekey;
2457 }
2458
2459 return NULL;
2460}
2461
2462const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2463 if (s->session != NULL && s->session->cipher != NULL) {
2464 return s->session->cipher;
2465 }
2466
2467 return NULL;
2468}
2469
2470const void *SSL_get_current_compression(SSL *s) { return NULL; }
2471
2472const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2473
2474int ssl_init_wbio_buffer(SSL *s, int push) {
2475 BIO *bbio;
2476
2477 if (s->bbio == NULL) {
2478 bbio = BIO_new(BIO_f_buffer());
2479 if (bbio == NULL) {
2480 return 0;
2481 }
2482 s->bbio = bbio;
2483 } else {
2484 bbio = s->bbio;
2485 if (s->bbio == s->wbio) {
2486 s->wbio = BIO_pop(s->wbio);
2487 }
2488 }
2489
2490 BIO_reset(bbio);
2491 if (!BIO_set_read_buffer_size(bbio, 1)) {
2492 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2493 return 0;
2494 }
2495
2496 if (push) {
2497 if (s->wbio != bbio) {
2498 s->wbio = BIO_push(bbio, s->wbio);
2499 }
2500 } else {
2501 if (s->wbio == bbio) {
2502 s->wbio = BIO_pop(bbio);
2503 }
2504 }
2505
2506 return 1;
2507}
2508
2509void ssl_free_wbio_buffer(SSL *s) {
2510 if (s->bbio == NULL) {
2511 return;
2512 }
2513
2514 if (s->bbio == s->wbio) {
2515 /* remove buffering */
2516 s->wbio = BIO_pop(s->wbio);
2517 }
2518
2519 BIO_free(s->bbio);
2520 s->bbio = NULL;
2521}
2522
2523void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2524 ctx->quiet_shutdown = mode;
2525}
2526
2527int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2528 return ctx->quiet_shutdown;
2529}
2530
2531void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2532
2533int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2534
2535void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2536
2537int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2538
2539int SSL_version(const SSL *s) { return s->version; }
2540
2541SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2542
2543SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2544 if (ssl->ctx == ctx) {
2545 return ssl->ctx;
2546 }
2547
2548 if (ctx == NULL) {
2549 ctx = ssl->initial_ctx;
2550 }
2551
2552 if (ssl->cert != NULL) {
2553 ssl_cert_free(ssl->cert);
2554 }
2555
2556 ssl->cert = ssl_cert_dup(ctx->cert);
2557 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2558 if (ssl->ctx != NULL) {
2559 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2560 }
2561 ssl->ctx = ctx;
2562
2563 ssl->sid_ctx_length = ctx->sid_ctx_length;
2564 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2565 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2566
2567 return ssl->ctx;
2568}
2569
2570int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2571 return X509_STORE_set_default_paths(ctx->cert_store);
2572}
Adam Langley95c29f32014-06-20 12:00:00 -07002573
2574int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002575 const char *CApath) {
2576 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2577}
Adam Langley95c29f32014-06-20 12:00:00 -07002578
2579void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002580 void (*cb)(const SSL *ssl, int type, int val)) {
2581 ssl->info_callback = cb;
2582}
Adam Langley95c29f32014-06-20 12:00:00 -07002583
Adam Langleyfcf25832014-12-18 17:42:32 -08002584void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2585 int /*val*/) {
2586 return ssl->info_callback;
2587}
Adam Langley95c29f32014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002590
Adam Langleyfcf25832014-12-18 17:42:32 -08002591void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002592
Adam Langleyfcf25832014-12-18 17:42:32 -08002593void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002594
Adam Langleyfcf25832014-12-18 17:42:32 -08002595long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002596
Adam Langleyfcf25832014-12-18 17:42:32 -08002597int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2598 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2599 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2600 dup_func, free_func);
2601}
Adam Langley95c29f32014-06-20 12:00:00 -07002602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2604 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2605}
Adam Langley95c29f32014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607void *SSL_get_ex_data(const SSL *s, int idx) {
2608 return CRYPTO_get_ex_data(&s->ex_data, idx);
2609}
Adam Langley95c29f32014-06-20 12:00:00 -07002610
Adam Langleyfcf25832014-12-18 17:42:32 -08002611int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2612 CRYPTO_EX_dup *dup_func,
2613 CRYPTO_EX_free *free_func) {
2614 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2615 dup_func, free_func);
2616}
Adam Langley95c29f32014-06-20 12:00:00 -07002617
Adam Langleyfcf25832014-12-18 17:42:32 -08002618int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2619 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2620}
Adam Langley95c29f32014-06-20 12:00:00 -07002621
Adam Langleyfcf25832014-12-18 17:42:32 -08002622void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2623 return CRYPTO_get_ex_data(&s->ex_data, idx);
2624}
Adam Langley95c29f32014-06-20 12:00:00 -07002625
Adam Langleyfcf25832014-12-18 17:42:32 -08002626int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002627
Adam Langleyfcf25832014-12-18 17:42:32 -08002628X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2629 return ctx->cert_store;
2630}
Adam Langley95c29f32014-06-20 12:00:00 -07002631
Adam Langleyfcf25832014-12-18 17:42:32 -08002632void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2633 if (ctx->cert_store != NULL) {
2634 X509_STORE_free(ctx->cert_store);
2635 }
2636 ctx->cert_store = store;
2637}
Adam Langley95c29f32014-06-20 12:00:00 -07002638
Adam Langleyfcf25832014-12-18 17:42:32 -08002639int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002640
Adam Langleyfcf25832014-12-18 17:42:32 -08002641void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2642 RSA *(*cb)(SSL *ssl, int is_export,
2643 int keylength)) {
2644 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2645}
Adam Langley95c29f32014-06-20 12:00:00 -07002646
Adam Langleyfcf25832014-12-18 17:42:32 -08002647void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2648 int keylength)) {
2649 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2650}
2651
2652void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2653 DH *(*dh)(SSL *ssl, int is_export,
2654 int keylength)) {
2655 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2656}
2657
2658void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2659 int keylength)) {
2660 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2661}
2662
2663void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2664 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2665 int keylength)) {
2666 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2667}
2668
2669void SSL_set_tmp_ecdh_callback(SSL *ssl,
2670 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2671 int keylength)) {
2672 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2673}
2674
2675int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2676 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2677 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2678 SSL_R_DATA_LENGTH_TOO_LONG);
2679 return 0;
2680 }
2681
2682 if (ctx->psk_identity_hint != NULL) {
2683 OPENSSL_free(ctx->psk_identity_hint);
2684 }
2685
2686 if (identity_hint != NULL) {
2687 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2688 if (ctx->psk_identity_hint == NULL) {
2689 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002690 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002691 } else {
2692 ctx->psk_identity_hint = NULL;
2693 }
Adam Langley95c29f32014-06-20 12:00:00 -07002694
Adam Langleyfcf25832014-12-18 17:42:32 -08002695 return 1;
2696}
2697
2698int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2699 if (s == NULL) {
2700 return 0;
2701 }
2702
2703 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2704 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2705 SSL_R_DATA_LENGTH_TOO_LONG);
2706 return 0;
2707 }
2708
2709 /* Clear currently configured hint, if any. */
2710 if (s->psk_identity_hint != NULL) {
2711 OPENSSL_free(s->psk_identity_hint);
2712 s->psk_identity_hint = NULL;
2713 }
2714
2715 if (identity_hint != NULL) {
2716 s->psk_identity_hint = BUF_strdup(identity_hint);
2717 if (s->psk_identity_hint == NULL) {
2718 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002719 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002720 }
Adam Langley95c29f32014-06-20 12:00:00 -07002721
Adam Langleyfcf25832014-12-18 17:42:32 -08002722 return 1;
2723}
Adam Langley95c29f32014-06-20 12:00:00 -07002724
Adam Langleyfcf25832014-12-18 17:42:32 -08002725const char *SSL_get_psk_identity_hint(const SSL *s) {
2726 if (s == NULL) {
2727 return NULL;
2728 }
2729 return s->psk_identity_hint;
2730}
Adam Langley95c29f32014-06-20 12:00:00 -07002731
Adam Langleyfcf25832014-12-18 17:42:32 -08002732const char *SSL_get_psk_identity(const SSL *s) {
2733 if (s == NULL || s->session == NULL) {
2734 return NULL;
2735 }
Adam Langley95c29f32014-06-20 12:00:00 -07002736
Adam Langleyfcf25832014-12-18 17:42:32 -08002737 return s->session->psk_identity;
2738}
Adam Langley95c29f32014-06-20 12:00:00 -07002739
Adam Langleyfcf25832014-12-18 17:42:32 -08002740void SSL_set_psk_client_callback(
2741 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2742 unsigned int max_identity_len, uint8_t *psk,
2743 unsigned int max_psk_len)) {
2744 s->psk_client_callback = cb;
2745}
Adam Langley95c29f32014-06-20 12:00:00 -07002746
Adam Langleyfcf25832014-12-18 17:42:32 -08002747void SSL_CTX_set_psk_client_callback(
2748 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2749 unsigned int max_identity_len,
2750 uint8_t *psk, unsigned int max_psk_len)) {
2751 ctx->psk_client_callback = cb;
2752}
Adam Langley95c29f32014-06-20 12:00:00 -07002753
Adam Langleyfcf25832014-12-18 17:42:32 -08002754void SSL_set_psk_server_callback(
2755 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2756 unsigned int max_psk_len)) {
2757 s->psk_server_callback = cb;
2758}
Adam Langley95c29f32014-06-20 12:00:00 -07002759
Adam Langleyfcf25832014-12-18 17:42:32 -08002760void SSL_CTX_set_psk_server_callback(
2761 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2762 uint8_t *psk, unsigned int max_psk_len)) {
2763 ctx->psk_server_callback = cb;
2764}
Adam Langley95c29f32014-06-20 12:00:00 -07002765
Adam Langleyfcf25832014-12-18 17:42:32 -08002766void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2767 ctx->min_version = version;
2768}
Adam Langley95c29f32014-06-20 12:00:00 -07002769
Adam Langleyfcf25832014-12-18 17:42:32 -08002770void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2771 ctx->max_version = version;
2772}
Adam Langley0289c732014-06-20 12:00:00 -07002773
Adam Langleyfcf25832014-12-18 17:42:32 -08002774void SSL_set_min_version(SSL *ssl, uint16_t version) {
2775 ssl->min_version = version;
2776}
Adam Langley0289c732014-06-20 12:00:00 -07002777
Adam Langleyfcf25832014-12-18 17:42:32 -08002778void SSL_set_max_version(SSL *ssl, uint16_t version) {
2779 ssl->max_version = version;
2780}
Adam Langley95c29f32014-06-20 12:00:00 -07002781
Adam Langleyfcf25832014-12-18 17:42:32 -08002782void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2783 void (*cb)(int write_p, int version,
2784 int content_type, const void *buf,
2785 size_t len, SSL *ssl, void *arg)) {
2786 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2787}
2788void SSL_set_msg_callback(SSL *ssl,
2789 void (*cb)(int write_p, int version, int content_type,
2790 const void *buf, size_t len, SSL *ssl,
2791 void *arg)) {
2792 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2793}
Adam Langley95c29f32014-06-20 12:00:00 -07002794
Adam Langleyfcf25832014-12-18 17:42:32 -08002795void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2796 if (ctx->keylog_bio != NULL) {
2797 BIO_free(ctx->keylog_bio);
2798 }
2799 ctx->keylog_bio = keylog_bio;
2800}
Adam Langley95c29f32014-06-20 12:00:00 -07002801
Adam Langleyfcf25832014-12-18 17:42:32 -08002802static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2803 static const char hextable[] = "0123456789abcdef";
2804 uint8_t *out;
2805 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002806
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2808 return 0;
2809 }
Adam Langley95c29f32014-06-20 12:00:00 -07002810
Adam Langleyfcf25832014-12-18 17:42:32 -08002811 for (i = 0; i < in_len; i++) {
2812 *(out++) = (uint8_t)hextable[in[i] >> 4];
2813 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2814 }
Adam Langley95c29f32014-06-20 12:00:00 -07002815
Adam Langleyfcf25832014-12-18 17:42:32 -08002816 return 1;
2817}
David Benjamin859ec3c2014-09-02 16:29:36 -04002818
2819int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002820 const uint8_t *encrypted_premaster,
2821 size_t encrypted_premaster_len,
2822 const uint8_t *premaster,
2823 size_t premaster_len) {
2824 BIO *bio = ctx->keylog_bio;
2825 CBB cbb;
2826 uint8_t *out;
2827 size_t out_len;
2828 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002829
Adam Langleyfcf25832014-12-18 17:42:32 -08002830 if (bio == NULL) {
2831 return 1;
2832 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002833
Adam Langleyfcf25832014-12-18 17:42:32 -08002834 if (encrypted_premaster_len < 8) {
2835 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2836 ERR_R_INTERNAL_ERROR);
2837 return 0;
2838 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002839
Adam Langleyfcf25832014-12-18 17:42:32 -08002840 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2841 return 0;
2842 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002843
Adam Langleyfcf25832014-12-18 17:42:32 -08002844 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2845 /* Only the first 8 bytes of the encrypted premaster secret are
2846 * logged. */
2847 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2848 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2849 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2850 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2851 !CBB_finish(&cbb, &out, &out_len)) {
2852 CBB_cleanup(&cbb);
2853 return 0;
2854 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002855
Adam Langleyfcf25832014-12-18 17:42:32 -08002856 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2857 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2858 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002859
Adam Langleyfcf25832014-12-18 17:42:32 -08002860 OPENSSL_free(out);
2861 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002862}
2863
Adam Langleyfcf25832014-12-18 17:42:32 -08002864int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2865 size_t client_random_len, const uint8_t *master,
2866 size_t master_len) {
2867 BIO *bio = ctx->keylog_bio;
2868 CBB cbb;
2869 uint8_t *out;
2870 size_t out_len;
2871 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002872
Adam Langleyfcf25832014-12-18 17:42:32 -08002873 if (bio == NULL) {
2874 return 1;
2875 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002876
Adam Langleyfcf25832014-12-18 17:42:32 -08002877 if (client_random_len != 32) {
2878 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2879 return 0;
2880 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002881
Adam Langleyfcf25832014-12-18 17:42:32 -08002882 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2883 return 0;
2884 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002885
Adam Langleyfcf25832014-12-18 17:42:32 -08002886 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2887 !cbb_add_hex(&cbb, client_random, 32) ||
2888 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2889 !cbb_add_hex(&cbb, master, master_len) ||
2890 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2891 !CBB_finish(&cbb, &out, &out_len)) {
2892 CBB_cleanup(&cbb);
2893 return 0;
2894 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002895
Adam Langleyfcf25832014-12-18 17:42:32 -08002896 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2897 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2898 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002899
Adam Langleyfcf25832014-12-18 17:42:32 -08002900 OPENSSL_free(out);
2901 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002902}
2903
Adam Langleyfcf25832014-12-18 17:42:32 -08002904int SSL_cutthrough_complete(const SSL *s) {
David Benjaminccf74f82015-02-09 00:01:31 -05002905 return s->s3->tmp.cutthrough_complete;
Adam Langleyfcf25832014-12-18 17:42:32 -08002906}
Adam Langley95c29f32014-06-20 12:00:00 -07002907
Adam Langleyfcf25832014-12-18 17:42:32 -08002908void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2909 size_t *ssl_session_size) {
2910 *ssl_size = sizeof(SSL);
2911 *ssl_ctx_size = sizeof(SSL_CTX);
2912 *ssl_session_size = sizeof(SSL_SESSION);
2913}
Feng Lu41aa3252014-11-21 22:47:56 -08002914
Adam Langleyfcf25832014-12-18 17:42:32 -08002915int ssl3_can_cutthrough(const SSL *s) {
2916 const SSL_CIPHER *c;
2917
2918 /* require a strong enough cipher */
2919 if (SSL_get_cipher_bits(s, NULL) < 128) {
2920 return 0;
2921 }
2922
2923 /* require ALPN or NPN extension */
2924 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen) {
2925 return 0;
2926 }
2927
2928 /* require a forward-secret cipher */
2929 c = SSL_get_current_cipher(s);
2930 if (!c ||
2931 (c->algorithm_mkey != SSL_kEDH && c->algorithm_mkey != SSL_kEECDH)) {
2932 return 0;
2933 }
2934
2935 return 1;
2936}
2937
2938const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2939 switch (version) {
2940 case SSL3_VERSION:
2941 return &SSLv3_enc_data;
2942
2943 case TLS1_VERSION:
2944 return &TLSv1_enc_data;
2945
2946 case TLS1_1_VERSION:
2947 return &TLSv1_1_enc_data;
2948
2949 case TLS1_2_VERSION:
2950 return &TLSv1_2_enc_data;
2951
2952 case DTLS1_VERSION:
2953 return &DTLSv1_enc_data;
2954
2955 case DTLS1_2_VERSION:
2956 return &DTLSv1_2_enc_data;
2957
2958 default:
2959 return NULL;
2960 }
2961}
2962
2963uint16_t ssl3_get_max_server_version(const SSL *s) {
2964 uint16_t max_version;
2965
2966 if (SSL_IS_DTLS(s)) {
2967 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2968 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2969 return DTLS1_2_VERSION;
2970 }
2971 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2972 return DTLS1_VERSION;
2973 }
2974 return 0;
2975 }
2976
2977 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2978 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2979 return TLS1_2_VERSION;
2980 }
2981 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2982 return TLS1_1_VERSION;
2983 }
2984 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2985 return TLS1_VERSION;
2986 }
2987 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2988 return SSL3_VERSION;
2989 }
2990 return 0;
2991}
2992
2993uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
2994 uint16_t version = 0;
2995
2996 if (SSL_IS_DTLS(s)) {
2997 /* Clamp client_version to max_version. */
2998 if (s->max_version != 0 && client_version < s->max_version) {
2999 client_version = s->max_version;
3000 }
3001
3002 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
3003 version = DTLS1_2_VERSION;
3004 } else if (client_version <= DTLS1_VERSION &&
3005 !(s->options & SSL_OP_NO_DTLSv1)) {
3006 version = DTLS1_VERSION;
3007 }
3008
3009 /* Check against min_version. */
3010 if (version != 0 && s->min_version != 0 && version > s->min_version) {
3011 return 0;
3012 }
3013 return version;
3014 } else {
3015 /* Clamp client_version to max_version. */
3016 if (s->max_version != 0 && client_version > s->max_version) {
3017 client_version = s->max_version;
3018 }
3019
3020 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
3021 version = TLS1_2_VERSION;
3022 } else if (client_version >= TLS1_1_VERSION &&
3023 !(s->options & SSL_OP_NO_TLSv1_1)) {
3024 version = TLS1_1_VERSION;
3025 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
3026 version = TLS1_VERSION;
3027 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
3028 version = SSL3_VERSION;
3029 }
3030
3031 /* Check against min_version. */
3032 if (version != 0 && s->min_version != 0 && version < s->min_version) {
3033 return 0;
3034 }
3035 return version;
3036 }
3037}
3038
3039uint16_t ssl3_get_max_client_version(SSL *s) {
3040 unsigned long options = s->options;
3041 uint16_t version = 0;
3042
3043 /* OpenSSL's API for controlling versions entails blacklisting individual
3044 * protocols. This has two problems. First, on the client, the protocol can
3045 * only express a contiguous range of versions. Second, a library consumer
3046 * trying to set a maximum version cannot disable protocol versions that get
3047 * added in a future version of the library.
3048 *
3049 * To account for both of these, OpenSSL interprets the client-side bitmask
3050 * as a min/max range by picking the lowest contiguous non-empty range of
3051 * enabled protocols. Note that this means it is impossible to set a maximum
3052 * version of TLS 1.2 in a future-proof way.
3053 *
3054 * By this scheme, the maximum version is the lowest version V such that V is
3055 * enabled and V+1 is disabled or unimplemented. */
3056 if (SSL_IS_DTLS(s)) {
3057 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3058 version = DTLS1_2_VERSION;
3059 }
3060 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3061 version = DTLS1_VERSION;
3062 }
3063 if (s->max_version != 0 && version < s->max_version) {
3064 version = s->max_version;
3065 }
3066 } else {
3067 if (!(options & SSL_OP_NO_TLSv1_2)) {
3068 version = TLS1_2_VERSION;
3069 }
3070 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3071 version = TLS1_1_VERSION;
3072 }
3073 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3074 version = TLS1_VERSION;
3075 }
3076 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3077 version = SSL3_VERSION;
3078 }
3079 if (s->max_version != 0 && version > s->max_version) {
3080 version = s->max_version;
3081 }
3082 }
3083
3084 return version;
3085}
3086
3087int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3088 if (SSL_IS_DTLS(s)) {
3089 if (s->max_version != 0 && version < s->max_version) {
3090 return 0;
3091 }
3092 if (s->min_version != 0 && version > s->min_version) {
3093 return 0;
3094 }
3095
3096 switch (version) {
3097 case DTLS1_VERSION:
3098 return !(s->options & SSL_OP_NO_DTLSv1);
3099
3100 case DTLS1_2_VERSION:
3101 return !(s->options & SSL_OP_NO_DTLSv1_2);
3102
3103 default:
3104 return 0;
3105 }
3106 } else {
3107 if (s->max_version != 0 && version > s->max_version) {
3108 return 0;
3109 }
3110 if (s->min_version != 0 && version < s->min_version) {
3111 return 0;
3112 }
3113
3114 switch (version) {
3115 case SSL3_VERSION:
3116 return !(s->options & SSL_OP_NO_SSLv3);
3117
3118 case TLS1_VERSION:
3119 return !(s->options & SSL_OP_NO_TLSv1);
3120
3121 case TLS1_1_VERSION:
3122 return !(s->options & SSL_OP_NO_TLSv1_1);
3123
3124 case TLS1_2_VERSION:
3125 return !(s->options & SSL_OP_NO_TLSv1_2);
3126
3127 default:
3128 return 0;
3129 }
3130 }
3131}
3132
David Benjaminea72bd02014-12-21 21:27:41 -05003133uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3134 if (!SSL_IS_DTLS(s)) {
3135 return wire_version;
3136 }
3137
3138 uint16_t tls_version = ~wire_version;
3139 uint16_t version = tls_version + 0x0201;
3140 /* If either component overflowed, clamp it so comparisons still work. */
3141 if ((version >> 8) < (tls_version >> 8)) {
3142 version = 0xff00 | (version & 0xff);
3143 }
3144 if ((version & 0xff) < (tls_version & 0xff)) {
3145 version = (version & 0xff00) | 0xff;
3146 }
3147 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3148 if (version == TLS1_VERSION) {
3149 version = TLS1_1_VERSION;
3150 }
3151 return version;
3152}
3153
Adam Langleyfcf25832014-12-18 17:42:32 -08003154int SSL_cache_hit(SSL *s) { return s->hit; }
3155
3156int SSL_is_server(SSL *s) { return s->server; }
3157
3158void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3159 s->fastradio_padding = on_off;
3160}