blob: 311c1e4dbb8be24b9f14aa7619cc0ea8b35a66e8 [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) {
1008 if (s->renegotiate == 0) {
1009 s->renegotiate = 1;
1010 }
Adam Langley95c29f32014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 s->new_session = 1;
1013 return s->method->ssl_renegotiate(s);
1014}
Adam Langley95c29f32014-06-20 12:00:00 -07001015
Adam Langleyfcf25832014-12-18 17:42:32 -08001016int SSL_renegotiate_abbreviated(SSL *s) {
1017 if (s->renegotiate == 0) {
1018 s->renegotiate = 1;
1019 }
Adam Langley95c29f32014-06-20 12:00:00 -07001020
Adam Langleyfcf25832014-12-18 17:42:32 -08001021 s->new_session = 0;
1022 return s->method->ssl_renegotiate(s);
1023}
Adam Langley95c29f32014-06-20 12:00:00 -07001024
Adam Langleyfcf25832014-12-18 17:42:32 -08001025int SSL_renegotiate_pending(SSL *s) {
1026 /* becomes true when negotiation is requested; false again once a handshake
1027 * has finished */
1028 return s->renegotiate != 0;
1029}
Adam Langley95c29f32014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1032 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 switch (cmd) {
1035 case SSL_CTRL_GET_READ_AHEAD:
1036 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -07001037
Adam Langleyfcf25832014-12-18 17:42:32 -08001038 case SSL_CTRL_SET_READ_AHEAD:
1039 l = s->read_ahead;
1040 s->read_ahead = larg;
1041 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001042
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1044 s->msg_callback_arg = parg;
1045 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 case SSL_CTRL_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_CLEAR_OPTIONS:
1051 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001052
Adam Langleyfcf25832014-12-18 17:42:32 -08001053 case SSL_CTRL_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_CLEAR_MODE:
1057 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001058
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 case SSL_CTRL_GET_MAX_CERT_LIST:
1060 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001061
Adam Langleyfcf25832014-12-18 17:42:32 -08001062 case SSL_CTRL_SET_MAX_CERT_LIST:
1063 l = s->max_cert_list;
1064 s->max_cert_list = larg;
1065 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001066
Adam Langleyfcf25832014-12-18 17:42:32 -08001067 case SSL_CTRL_SET_MTU:
1068 if (larg < (long)dtls1_min_mtu()) {
1069 return 0;
1070 }
1071 if (SSL_IS_DTLS(s)) {
1072 s->d1->mtu = larg;
1073 return larg;
1074 }
1075 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001076
Adam Langleyfcf25832014-12-18 17:42:32 -08001077 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1078 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1079 return 0;
1080 }
1081 s->max_send_fragment = larg;
1082 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001083
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 case SSL_CTRL_GET_RI_SUPPORT:
1085 if (s->s3) {
1086 return s->s3->send_connection_binding;
1087 }
1088 return 0;
1089
1090 case SSL_CTRL_CERT_FLAGS:
1091 return s->cert->cert_flags |= larg;
1092
1093 case SSL_CTRL_CLEAR_CERT_FLAGS:
1094 return s->cert->cert_flags &= ~larg;
1095
1096 case SSL_CTRL_GET_RAW_CIPHERLIST:
1097 if (parg) {
1098 if (s->cert->ciphers_raw == NULL) {
1099 return 0;
1100 }
1101 *(uint8_t **)parg = s->cert->ciphers_raw;
1102 return (int)s->cert->ciphers_rawlen;
1103 }
1104
1105 /* Passing a NULL |parg| returns the size of a single
1106 * cipher suite value. */
1107 return 2;
1108
1109 default:
1110 return s->method->ssl_ctrl(s, cmd, larg, parg);
1111 }
1112}
1113
1114long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1115 switch (cmd) {
1116 case SSL_CTRL_SET_MSG_CALLBACK:
1117 s->msg_callback =
1118 (void (*)(int write_p, int version, int content_type, const void *buf,
1119 size_t len, SSL *ssl, void *arg))(fp);
1120 return 1;
1121
1122 default:
1123 return s->method->ssl_callback_ctrl(s, cmd, fp);
1124 }
1125}
1126
1127LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1128
1129long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1130 long l;
1131
1132 switch (cmd) {
1133 case SSL_CTRL_GET_READ_AHEAD:
1134 return ctx->read_ahead;
1135
1136 case SSL_CTRL_SET_READ_AHEAD:
1137 l = ctx->read_ahead;
1138 ctx->read_ahead = larg;
1139 return l;
1140
1141 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1142 ctx->msg_callback_arg = parg;
1143 return 1;
1144
1145 case SSL_CTRL_GET_MAX_CERT_LIST:
1146 return ctx->max_cert_list;
1147
1148 case SSL_CTRL_SET_MAX_CERT_LIST:
1149 l = ctx->max_cert_list;
1150 ctx->max_cert_list = larg;
1151 return l;
1152
1153 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1154 l = ctx->session_cache_size;
1155 ctx->session_cache_size = larg;
1156 return l;
1157
1158 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1159 return ctx->session_cache_size;
1160
1161 case SSL_CTRL_SET_SESS_CACHE_MODE:
1162 l = ctx->session_cache_mode;
1163 ctx->session_cache_mode = larg;
1164 return l;
1165
1166 case SSL_CTRL_GET_SESS_CACHE_MODE:
1167 return ctx->session_cache_mode;
1168
1169 case SSL_CTRL_SESS_NUMBER:
1170 return lh_SSL_SESSION_num_items(ctx->sessions);
1171
1172 case SSL_CTRL_SESS_CONNECT:
1173 return ctx->stats.sess_connect;
1174
1175 case SSL_CTRL_SESS_CONNECT_GOOD:
1176 return ctx->stats.sess_connect_good;
1177
1178 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1179 return ctx->stats.sess_connect_renegotiate;
1180
1181 case SSL_CTRL_SESS_ACCEPT:
1182 return ctx->stats.sess_accept;
1183
1184 case SSL_CTRL_SESS_ACCEPT_GOOD:
1185 return ctx->stats.sess_accept_good;
1186
1187 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1188 return ctx->stats.sess_accept_renegotiate;
1189
1190 case SSL_CTRL_SESS_HIT:
1191 return ctx->stats.sess_hit;
1192
1193 case SSL_CTRL_SESS_CB_HIT:
1194 return ctx->stats.sess_cb_hit;
1195
1196 case SSL_CTRL_SESS_MISSES:
1197 return ctx->stats.sess_miss;
1198
1199 case SSL_CTRL_SESS_TIMEOUTS:
1200 return ctx->stats.sess_timeout;
1201
1202 case SSL_CTRL_SESS_CACHE_FULL:
1203 return ctx->stats.sess_cache_full;
1204
1205 case SSL_CTRL_OPTIONS:
1206 return ctx->options |= larg;
1207
1208 case SSL_CTRL_CLEAR_OPTIONS:
1209 return ctx->options &= ~larg;
1210
1211 case SSL_CTRL_MODE:
1212 return ctx->mode |= larg;
1213
1214 case SSL_CTRL_CLEAR_MODE:
1215 return ctx->mode &= ~larg;
1216
1217 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1218 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1219 return 0;
1220 }
1221 ctx->max_send_fragment = larg;
1222 return 1;
1223
1224 case SSL_CTRL_CERT_FLAGS:
1225 return ctx->cert->cert_flags |= larg;
1226
1227 case SSL_CTRL_CLEAR_CERT_FLAGS:
1228 return ctx->cert->cert_flags &= ~larg;
1229
1230 default:
1231 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1232 }
1233}
1234
1235long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1236 switch (cmd) {
1237 case SSL_CTRL_SET_MSG_CALLBACK:
1238 ctx->msg_callback =
1239 (void (*)(int write_p, int version, int content_type, const void *buf,
1240 size_t len, SSL *ssl, void *arg))(fp);
1241 return 1;
1242
1243 default:
1244 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1245 }
1246}
1247
1248int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1249 long l;
1250 const SSL_CIPHER *a = in_a;
1251 const SSL_CIPHER *b = in_b;
1252 const long a_id = a->id;
1253 const long b_id = b->id;
1254
1255 l = a_id - b_id;
1256 if (l == 0L) {
1257 return 0;
1258 } else {
1259 return (l > 0) ? 1 : -1;
1260 }
1261}
1262
1263int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1264 long l;
1265 const long a_id = (*ap)->id;
1266 const long b_id = (*bp)->id;
1267
1268 l = a_id - b_id;
1269 if (l == 0) {
1270 return 0;
1271 } else {
1272 return (l > 0) ? 1 : -1;
1273 }
1274}
1275
1276/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001277 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001278STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1279 if (s == NULL) {
1280 return NULL;
1281 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001282
Adam Langleyfcf25832014-12-18 17:42:32 -08001283 if (s->cipher_list != NULL) {
1284 return s->cipher_list->ciphers;
1285 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001286
Adam Langleyfcf25832014-12-18 17:42:32 -08001287 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1288 s->ctx->cipher_list_tls11 != NULL) {
1289 return s->ctx->cipher_list_tls11->ciphers;
1290 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001291
Adam Langleyfcf25832014-12-18 17:42:32 -08001292 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1293 return s->ctx->cipher_list->ciphers;
1294 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001295
Adam Langleyfcf25832014-12-18 17:42:32 -08001296 return NULL;
1297}
Adam Langley95c29f32014-06-20 12:00:00 -07001298
Adam Langleyfcf25832014-12-18 17:42:32 -08001299/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001300 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001301STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1302 if (s == NULL) {
1303 return NULL;
1304 }
Adam Langley95c29f32014-06-20 12:00:00 -07001305
Adam Langleyfcf25832014-12-18 17:42:32 -08001306 if (s->cipher_list_by_id != NULL) {
1307 return s->cipher_list_by_id;
1308 }
Adam Langley95c29f32014-06-20 12:00:00 -07001309
Adam Langleyfcf25832014-12-18 17:42:32 -08001310 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1311 return s->ctx->cipher_list_by_id;
1312 }
Adam Langley95c29f32014-06-20 12:00:00 -07001313
Adam Langleyfcf25832014-12-18 17:42:32 -08001314 return NULL;
1315}
Adam Langley95c29f32014-06-20 12:00:00 -07001316
Adam Langleyfcf25832014-12-18 17:42:32 -08001317/* The old interface to get the same thing as SSL_get_ciphers() */
1318const char *SSL_get_cipher_list(const SSL *s, int n) {
1319 const SSL_CIPHER *c;
1320 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001321
Adam Langleyfcf25832014-12-18 17:42:32 -08001322 if (s == NULL) {
1323 return NULL;
1324 }
Adam Langley95c29f32014-06-20 12:00:00 -07001325
Adam Langleyfcf25832014-12-18 17:42:32 -08001326 sk = SSL_get_ciphers(s);
1327 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1328 return NULL;
1329 }
Adam Langley95c29f32014-06-20 12:00:00 -07001330
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 c = sk_SSL_CIPHER_value(sk, n);
1332 if (c == NULL) {
1333 return NULL;
1334 }
Adam Langley95c29f32014-06-20 12:00:00 -07001335
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 return c->name;
1337}
David Benjamin5491e3f2014-09-29 19:33:09 -04001338
Adam Langleyfcf25832014-12-18 17:42:32 -08001339/* specify the ciphers to be used by default by the SSL_CTX */
1340int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1341 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001342
Adam Langleyfcf25832014-12-18 17:42:32 -08001343 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1344 &ctx->cipher_list_by_id, str, ctx->cert);
1345 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1346 * a cipher matching the given rule string (for example if the rule string
1347 * specifies a cipher which has been disabled). This is not an error as far
1348 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1349 * ctx->cipher_list_by_id has been updated. */
1350 if (sk == NULL) {
1351 return 0;
1352 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1353 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1354 return 0;
1355 }
Adam Langley95c29f32014-06-20 12:00:00 -07001356
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 return 1;
1358}
David Benjamin39482a12014-07-20 13:30:15 -04001359
Adam Langleyfcf25832014-12-18 17:42:32 -08001360int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1361 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str,
1364 ctx->cert);
1365 if (sk == NULL) {
1366 return 0;
1367 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1368 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1369 SSL_R_NO_CIPHER_MATCH);
1370 return 0;
1371 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001372
Adam Langleyfcf25832014-12-18 17:42:32 -08001373 return 1;
1374}
Adam Langley95c29f32014-06-20 12:00:00 -07001375
Adam Langleyfcf25832014-12-18 17:42:32 -08001376/* specify the ciphers to be used by the SSL */
1377int SSL_set_cipher_list(SSL *s, const char *str) {
1378 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langleyfcf25832014-12-18 17:42:32 -08001380 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1381 &s->cipher_list_by_id, str, s->cert);
David Benjamin39482a12014-07-20 13:30:15 -04001382
Adam Langleyfcf25832014-12-18 17:42:32 -08001383 /* see comment in SSL_CTX_set_cipher_list */
1384 if (sk == NULL) {
1385 return 0;
1386 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1387 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1388 return 0;
1389 }
David Benjamin39482a12014-07-20 13:30:15 -04001390
Adam Langleyfcf25832014-12-18 17:42:32 -08001391 return 1;
1392}
Adam Langley95c29f32014-06-20 12:00:00 -07001393
Adam Langleyfcf25832014-12-18 17:42:32 -08001394int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1395 size_t i;
1396 const SSL_CIPHER *c;
1397 CERT *ct = s->cert;
1398 uint8_t *q;
1399 /* Set disabled masks for this session */
1400 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001401
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 if (sk == NULL) {
1403 return 0;
1404 }
1405 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001406
Adam Langleyfcf25832014-12-18 17:42:32 -08001407 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1408 c = sk_SSL_CIPHER_value(sk, i);
1409 /* Skip disabled ciphers */
1410 if (c->algorithm_ssl & ct->mask_ssl ||
1411 c->algorithm_mkey & ct->mask_k ||
1412 c->algorithm_auth & ct->mask_a) {
1413 continue;
1414 }
1415 s2n(ssl3_get_cipher_value(c), p);
1416 }
1417
1418 /* If all ciphers were disabled, return the error to the caller. */
1419 if (p == q) {
1420 return 0;
1421 }
1422
1423 /* Add SCSVs. */
1424 if (!s->renegotiate) {
1425 s2n(SSL3_CK_SCSV & 0xffff, p);
1426 }
1427
1428 if (s->fallback_scsv) {
1429 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1430 }
1431
1432 return p - q;
1433}
1434
1435STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1436 CBS cipher_suites = *cbs;
1437 const SSL_CIPHER *c;
1438 STACK_OF(SSL_CIPHER) * sk;
1439
1440 if (s->s3) {
1441 s->s3->send_connection_binding = 0;
1442 }
1443
1444 if (CBS_len(&cipher_suites) % 2 != 0) {
1445 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1446 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1447 return NULL;
1448 }
1449
1450 sk = sk_SSL_CIPHER_new_null();
1451 if (sk == NULL) {
1452 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1453 goto err;
1454 }
1455
1456 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1457 &s->cert->ciphers_rawlen)) {
1458 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1459 goto err;
1460 }
1461
1462 while (CBS_len(&cipher_suites) > 0) {
1463 uint16_t cipher_suite;
1464
1465 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1466 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1467 goto err;
1468 }
1469
1470 /* Check for SCSV. */
1471 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1472 /* SCSV is fatal if renegotiating. */
1473 if (s->renegotiate) {
1474 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1475 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1476 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1477 goto err;
1478 }
1479 s->s3->send_connection_binding = 1;
1480 continue;
1481 }
1482
1483 /* Check for FALLBACK_SCSV. */
1484 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1485 uint16_t max_version = ssl3_get_max_server_version(s);
1486 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1487 : (uint16_t)s->version < max_version) {
1488 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1489 SSL_R_INAPPROPRIATE_FALLBACK);
1490 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1491 goto err;
1492 }
1493 continue;
1494 }
1495
1496 c = ssl3_get_cipher_by_value(cipher_suite);
1497 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1498 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1499 goto err;
1500 }
1501 }
1502
1503 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001504
Adam Langley95c29f32014-06-20 12:00:00 -07001505err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001506 if (sk != NULL) {
1507 sk_SSL_CIPHER_free(sk);
1508 }
1509 return NULL;
1510}
Adam Langley95c29f32014-06-20 12:00:00 -07001511
1512
Adam Langleyfcf25832014-12-18 17:42:32 -08001513/* return a servername extension value if provided in Client Hello, or NULL. So
1514 * far, only host_name types are defined (RFC 3546). */
1515const char *SSL_get_servername(const SSL *s, const int type) {
1516 if (type != TLSEXT_NAMETYPE_host_name) {
1517 return NULL;
1518 }
Adam Langley95c29f32014-06-20 12:00:00 -07001519
Adam Langleyfcf25832014-12-18 17:42:32 -08001520 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1521 : s->tlsext_hostname;
1522}
Adam Langley95c29f32014-06-20 12:00:00 -07001523
Adam Langleyfcf25832014-12-18 17:42:32 -08001524int SSL_get_servername_type(const SSL *s) {
1525 if (s->session &&
1526 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1527 return TLSEXT_NAMETYPE_host_name;
1528 }
Adam Langley95c29f32014-06-20 12:00:00 -07001529
Adam Langleyfcf25832014-12-18 17:42:32 -08001530 return -1;
1531}
Adam Langley95c29f32014-06-20 12:00:00 -07001532
Adam Langleyfcf25832014-12-18 17:42:32 -08001533void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1534 ctx->signed_cert_timestamps_enabled = 1;
1535}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001536
Adam Langleyfcf25832014-12-18 17:42:32 -08001537int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1538 ssl->signed_cert_timestamps_enabled = 1;
1539 return 1;
1540}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001541
Adam Langleyfcf25832014-12-18 17:42:32 -08001542void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1543 ctx->ocsp_stapling_enabled = 1;
1544}
David Benjamin6c7aed02014-08-27 16:42:38 -04001545
Adam Langleyfcf25832014-12-18 17:42:32 -08001546int SSL_enable_ocsp_stapling(SSL *ssl) {
1547 ssl->ocsp_stapling_enabled = 1;
1548 return 1;
1549}
David Benjamin6c7aed02014-08-27 16:42:38 -04001550
Adam Langleyfcf25832014-12-18 17:42:32 -08001551void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1552 size_t *out_len) {
1553 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001554
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 *out_len = 0;
1556 *out = NULL;
1557 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1558 return;
1559 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001560
Adam Langleyfcf25832014-12-18 17:42:32 -08001561 *out = session->tlsext_signed_cert_timestamp_list;
1562 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1563}
David Benjamin6c7aed02014-08-27 16:42:38 -04001564
Adam Langleyfcf25832014-12-18 17:42:32 -08001565void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1566 size_t *out_len) {
1567 SSL_SESSION *session = ssl->session;
1568
1569 *out_len = 0;
1570 *out = NULL;
1571 if (ssl->server || !session || !session->ocsp_response) {
1572 return;
1573 }
1574 *out = session->ocsp_response;
1575 *out_len = session->ocsp_response_length;
1576}
David Benjamin6c7aed02014-08-27 16:42:38 -04001577
Adam Langley95c29f32014-06-20 12:00:00 -07001578/* SSL_select_next_proto implements the standard protocol selection. It is
1579 * expected that this function is called from the callback set by
1580 * SSL_CTX_set_next_proto_select_cb.
1581 *
1582 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1583 * strings. The length byte itself is not included in the length. A byte
1584 * string of length 0 is invalid. No byte string may be truncated.
1585 *
1586 * The current, but experimental algorithm for selecting the protocol is:
1587 *
1588 * 1) If the server doesn't support NPN then this is indicated to the
1589 * callback. In this case, the client application has to abort the connection
1590 * or have a default application level protocol.
1591 *
1592 * 2) If the server supports NPN, but advertises an empty list then the
1593 * client selects the first protcol in its list, but indicates via the
1594 * API that this fallback case was enacted.
1595 *
1596 * 3) Otherwise, the client finds the first protocol in the server's list
1597 * that it supports and selects this protocol. This is because it's
1598 * assumed that the server has better information about which protocol
1599 * a client should use.
1600 *
1601 * 4) If the client doesn't support any of the server's advertised
1602 * protocols, then this is treated the same as case 2.
1603 *
1604 * It returns either
1605 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1606 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1607 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001608int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1609 unsigned int server_len, const uint8_t *client,
1610 unsigned int client_len) {
1611 unsigned int i, j;
1612 const uint8_t *result;
1613 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001614
Adam Langleyfcf25832014-12-18 17:42:32 -08001615 /* For each protocol in server preference order, see if we support it. */
1616 for (i = 0; i < server_len;) {
1617 for (j = 0; j < client_len;) {
1618 if (server[i] == client[j] &&
1619 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1620 /* We found a match */
1621 result = &server[i];
1622 status = OPENSSL_NPN_NEGOTIATED;
1623 goto found;
1624 }
1625 j += client[j];
1626 j++;
1627 }
1628 i += server[i];
1629 i++;
1630 }
Adam Langley95c29f32014-06-20 12:00:00 -07001631
Adam Langleyfcf25832014-12-18 17:42:32 -08001632 /* There's no overlap between our protocols and the server's list. */
1633 result = client;
1634 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001635
Adam Langleyfcf25832014-12-18 17:42:32 -08001636found:
1637 *out = (uint8_t *)result + 1;
1638 *outlen = result[0];
1639 return status;
1640}
Adam Langley95c29f32014-06-20 12:00:00 -07001641
Adam Langley95c29f32014-06-20 12:00:00 -07001642/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1643 * requested protocol for this connection and returns 0. If the client didn't
1644 * request any protocol, then *data is set to NULL.
1645 *
1646 * Note that the client can request any protocol it chooses. The value returned
1647 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001648 * provided by the callback. */
1649void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1650 unsigned *len) {
1651 *data = s->next_proto_negotiated;
1652 if (!*data) {
1653 *len = 0;
1654 } else {
1655 *len = s->next_proto_negotiated_len;
1656 }
Adam Langley95c29f32014-06-20 12:00:00 -07001657}
1658
1659/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1660 * TLS server needs a list of supported protocols for Next Protocol
1661 * Negotiation. The returned list must be in wire format. The list is returned
1662 * by setting |out| to point to it and |outlen| to its length. This memory will
1663 * not be modified, but one should assume that the SSL* keeps a reference to
1664 * it.
1665 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1667 * Otherwise, no such extension will be included in the ServerHello. */
1668void SSL_CTX_set_next_protos_advertised_cb(
1669 SSL_CTX *ctx,
1670 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1671 void *arg) {
1672 ctx->next_protos_advertised_cb = cb;
1673 ctx->next_protos_advertised_cb_arg = arg;
1674}
Adam Langley95c29f32014-06-20 12:00:00 -07001675
1676/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1677 * client needs to select a protocol from the server's provided list. |out|
1678 * must be set to point to the selected protocol (which may be within |in|).
1679 * The length of the protocol name must be written into |outlen|. The server's
1680 * advertised protocols are provided in |in| and |inlen|. The callback can
1681 * assume that |in| is syntactically valid.
1682 *
1683 * The client must select a protocol. It is fatal to the connection if this
1684 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1685 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001686void SSL_CTX_set_next_proto_select_cb(
1687 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1688 const uint8_t *in, unsigned int inlen, void *arg),
1689 void *arg) {
1690 ctx->next_proto_select_cb = cb;
1691 ctx->next_proto_select_cb_arg = arg;
1692}
Adam Langley95c29f32014-06-20 12:00:00 -07001693
Adam Langley95c29f32014-06-20 12:00:00 -07001694/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1695 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1696 * length-prefixed strings).
1697 *
1698 * Returns 0 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001699int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1700 unsigned protos_len) {
1701 if (ctx->alpn_client_proto_list) {
1702 OPENSSL_free(ctx->alpn_client_proto_list);
1703 }
Adam Langley95c29f32014-06-20 12:00:00 -07001704
Adam Langleyfcf25832014-12-18 17:42:32 -08001705 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1706 if (!ctx->alpn_client_proto_list) {
1707 return 1;
1708 }
1709 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001710
Adam Langleyfcf25832014-12-18 17:42:32 -08001711 return 0;
1712}
Adam Langley95c29f32014-06-20 12:00:00 -07001713
1714/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1715 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1716 * length-prefixed strings).
1717 *
1718 * Returns 0 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001719int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1720 if (ssl->alpn_client_proto_list) {
1721 OPENSSL_free(ssl->alpn_client_proto_list);
1722 }
Adam Langley95c29f32014-06-20 12:00:00 -07001723
Adam Langleyfcf25832014-12-18 17:42:32 -08001724 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1725 if (!ssl->alpn_client_proto_list) {
1726 return 1;
1727 }
1728 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001729
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 return 0;
1731}
Adam Langley95c29f32014-06-20 12:00:00 -07001732
1733/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1734 * during ClientHello processing in order to select an ALPN protocol from the
1735 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001736void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1737 int (*cb)(SSL *ssl, const uint8_t **out,
1738 uint8_t *outlen, const uint8_t *in,
1739 unsigned int inlen, void *arg),
1740 void *arg) {
1741 ctx->alpn_select_cb = cb;
1742 ctx->alpn_select_cb_arg = arg;
1743}
Adam Langley95c29f32014-06-20 12:00:00 -07001744
1745/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1746 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1747 * including the leading length-prefix byte). If the server didn't respond with
1748 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001749void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1750 unsigned *len) {
1751 *data = NULL;
1752 if (ssl->s3) {
1753 *data = ssl->s3->alpn_selected;
1754 }
1755 if (*data == NULL) {
1756 *len = 0;
1757 } else {
1758 *len = ssl->s3->alpn_selected_len;
1759 }
1760}
Adam Langley95c29f32014-06-20 12:00:00 -07001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762int SSL_export_keying_material(SSL *s, uint8_t *out, size_t olen,
1763 const char *label, size_t llen, const uint8_t *p,
1764 size_t plen, int use_context) {
1765 if (s->version < TLS1_VERSION) {
1766 return -1;
1767 }
Adam Langley95c29f32014-06-20 12:00:00 -07001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 return s->enc_method->export_keying_material(s, out, olen, label, llen, p,
1770 plen, use_context);
1771}
Adam Langley95c29f32014-06-20 12:00:00 -07001772
Adam Langleyfcf25832014-12-18 17:42:32 -08001773static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1774 uint32_t hash =
1775 ((uint32_t)a->session_id[0]) ||
1776 ((uint32_t)a->session_id[1] << 8) ||
1777 ((uint32_t)a->session_id[2] << 16) ||
1778 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001779
Adam Langleyfcf25832014-12-18 17:42:32 -08001780 return hash;
1781}
Adam Langley95c29f32014-06-20 12:00:00 -07001782
1783/* NB: If this function (or indeed the hash function which uses a sort of
1784 * coarser function than this one) is changed, ensure
1785 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1786 * able to construct an SSL_SESSION that will collide with any existing session
1787 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001788static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1789 if (a->ssl_version != b->ssl_version) {
1790 return 1;
1791 }
Adam Langley95c29f32014-06-20 12:00:00 -07001792
Adam Langleyfcf25832014-12-18 17:42:32 -08001793 if (a->session_id_length != b->session_id_length) {
1794 return 1;
1795 }
Adam Langley95c29f32014-06-20 12:00:00 -07001796
Adam Langleyfcf25832014-12-18 17:42:32 -08001797 return memcmp(a->session_id, b->session_id, a->session_id_length);
1798}
Adam Langley95c29f32014-06-20 12:00:00 -07001799
Adam Langleyfcf25832014-12-18 17:42:32 -08001800SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1801 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001802
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 if (meth == NULL) {
1804 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1805 return NULL;
1806 }
Adam Langley95c29f32014-06-20 12:00:00 -07001807
Adam Langleyfcf25832014-12-18 17:42:32 -08001808 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1809 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1810 goto err;
1811 }
Adam Langley95c29f32014-06-20 12:00:00 -07001812
Adam Langleyfcf25832014-12-18 17:42:32 -08001813 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1814 if (ret == NULL) {
1815 goto err;
1816 }
Adam Langley95c29f32014-06-20 12:00:00 -07001817
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001819
Adam Langleyfcf25832014-12-18 17:42:32 -08001820 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 ret->cert_store = NULL;
1823 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1824 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1825 ret->session_cache_head = NULL;
1826 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001827
Adam Langleyfcf25832014-12-18 17:42:32 -08001828 /* We take the system default */
1829 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 ret->new_session_cb = 0;
1832 ret->remove_session_cb = 0;
1833 ret->get_session_cb = 0;
1834 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001835
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 memset((char *)&ret->stats, 0, sizeof(ret->stats));
Adam Langley95c29f32014-06-20 12:00:00 -07001837
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 ret->references = 1;
1839 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001840
Adam Langleyfcf25832014-12-18 17:42:32 -08001841 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 ret->app_verify_callback = 0;
1844 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001845
Adam Langleyfcf25832014-12-18 17:42:32 -08001846 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1847 ret->read_ahead = 0;
1848 ret->msg_callback = 0;
1849 ret->msg_callback_arg = NULL;
1850 ret->verify_mode = SSL_VERIFY_NONE;
1851 ret->sid_ctx_length = 0;
1852 ret->default_verify_callback = NULL;
1853 ret->cert = ssl_cert_new();
1854 if (ret->cert == NULL) {
1855 goto err;
1856 }
Adam Langley95c29f32014-06-20 12:00:00 -07001857
Adam Langleyfcf25832014-12-18 17:42:32 -08001858 ret->default_passwd_callback = 0;
1859 ret->default_passwd_callback_userdata = NULL;
1860 ret->client_cert_cb = 0;
1861 ret->app_gen_cookie_cb = 0;
1862 ret->app_verify_cookie_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001863
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1865 if (ret->sessions == NULL) {
1866 goto err;
1867 }
1868 ret->cert_store = X509_STORE_new();
1869 if (ret->cert_store == NULL) {
1870 goto err;
1871 }
Adam Langley95c29f32014-06-20 12:00:00 -07001872
Adam Langleyfcf25832014-12-18 17:42:32 -08001873 ssl_create_cipher_list(ret->method, &ret->cipher_list,
1874 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST,
1875 ret->cert);
1876 if (ret->cipher_list == NULL ||
1877 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1878 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1879 goto err2;
1880 }
Adam Langley95c29f32014-06-20 12:00:00 -07001881
Adam Langleyfcf25832014-12-18 17:42:32 -08001882 ret->param = X509_VERIFY_PARAM_new();
1883 if (!ret->param) {
1884 goto err;
1885 }
Adam Langley95c29f32014-06-20 12:00:00 -07001886
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 ret->client_CA = sk_X509_NAME_new_null();
1888 if (ret->client_CA == NULL) {
1889 goto err;
1890 }
Adam Langley95c29f32014-06-20 12:00:00 -07001891
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001893
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001895
Adam Langleyfcf25832014-12-18 17:42:32 -08001896 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 ret->tlsext_servername_callback = 0;
1899 ret->tlsext_servername_arg = NULL;
1900 /* Setup RFC4507 ticket keys */
1901 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1902 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1903 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1904 ret->options |= SSL_OP_NO_TICKET;
1905 }
Adam Langley95c29f32014-06-20 12:00:00 -07001906
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 ret->tlsext_status_cb = 0;
1908 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001909
Adam Langleyfcf25832014-12-18 17:42:32 -08001910 ret->next_protos_advertised_cb = 0;
1911 ret->next_proto_select_cb = 0;
1912 ret->psk_identity_hint = NULL;
1913 ret->psk_client_callback = NULL;
1914 ret->psk_server_callback = NULL;
1915
1916 /* Default is to connect to non-RI servers. When RI is more widely deployed
1917 * might change this. */
1918 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1919
1920 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1921 * uses of SSL_METHOD. */
1922 if (meth->version != 0) {
1923 SSL_CTX_set_max_version(ret, meth->version);
1924 SSL_CTX_set_min_version(ret, meth->version);
1925 }
1926
1927 return ret;
1928
Adam Langley95c29f32014-06-20 12:00:00 -07001929err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001930 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001931err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001932 if (ret != NULL) {
1933 SSL_CTX_free(ret);
1934 }
1935 return NULL;
1936}
Adam Langley95c29f32014-06-20 12:00:00 -07001937
Adam Langleyfcf25832014-12-18 17:42:32 -08001938void SSL_CTX_free(SSL_CTX *a) {
1939 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001940
Adam Langleyfcf25832014-12-18 17:42:32 -08001941 if (a == NULL) {
1942 return;
1943 }
Adam Langley95c29f32014-06-20 12:00:00 -07001944
Adam Langleyfcf25832014-12-18 17:42:32 -08001945 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1946 if (i > 0) {
1947 return;
1948 }
Adam Langley95c29f32014-06-20 12:00:00 -07001949
Adam Langleyfcf25832014-12-18 17:42:32 -08001950 if (a->param) {
1951 X509_VERIFY_PARAM_free(a->param);
1952 }
Adam Langley95c29f32014-06-20 12:00:00 -07001953
Adam Langleyfcf25832014-12-18 17:42:32 -08001954 /* Free internal session cache. However: the remove_cb() may reference the
1955 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1956 * sessions were flushed. As the ex_data handling routines might also touch
1957 * the session cache, the most secure solution seems to be: empty (flush) the
1958 * cache, then free ex_data, then finally free the cache. (See ticket
1959 * [openssl.org #212].) */
1960 if (a->sessions != NULL) {
1961 SSL_CTX_flush_sessions(a, 0);
1962 }
Adam Langley95c29f32014-06-20 12:00:00 -07001963
Adam Langleyfcf25832014-12-18 17:42:32 -08001964 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966 if (a->sessions != NULL) {
1967 lh_SSL_SESSION_free(a->sessions);
1968 }
1969 if (a->cert_store != NULL) {
1970 X509_STORE_free(a->cert_store);
1971 }
1972 if (a->cipher_list != NULL) {
1973 ssl_cipher_preference_list_free(a->cipher_list);
1974 }
1975 if (a->cipher_list_by_id != NULL) {
1976 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1977 }
1978 if (a->cipher_list_tls11 != NULL) {
1979 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1980 }
1981 if (a->cert != NULL) {
1982 ssl_cert_free(a->cert);
1983 }
1984 if (a->client_CA != NULL) {
1985 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1986 }
1987 if (a->extra_certs != NULL) {
1988 sk_X509_pop_free(a->extra_certs, X509_free);
1989 }
1990 if (a->srtp_profiles) {
1991 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1992 }
1993 if (a->psk_identity_hint) {
1994 OPENSSL_free(a->psk_identity_hint);
1995 }
1996 if (a->tlsext_ecpointformatlist) {
1997 OPENSSL_free(a->tlsext_ecpointformatlist);
1998 }
1999 if (a->tlsext_ellipticcurvelist) {
2000 OPENSSL_free(a->tlsext_ellipticcurvelist);
2001 }
2002 if (a->alpn_client_proto_list != NULL) {
2003 OPENSSL_free(a->alpn_client_proto_list);
2004 }
2005 if (a->tlsext_channel_id_private) {
2006 EVP_PKEY_free(a->tlsext_channel_id_private);
2007 }
2008 if (a->keylog_bio) {
2009 BIO_free(a->keylog_bio);
2010 }
Adam Langley95c29f32014-06-20 12:00:00 -07002011
Adam Langleyfcf25832014-12-18 17:42:32 -08002012 OPENSSL_free(a);
2013}
Adam Langley95c29f32014-06-20 12:00:00 -07002014
Adam Langleyfcf25832014-12-18 17:42:32 -08002015void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
2016 ctx->default_passwd_callback = cb;
2017}
Adam Langley95c29f32014-06-20 12:00:00 -07002018
Adam Langleyfcf25832014-12-18 17:42:32 -08002019void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
2020 ctx->default_passwd_callback_userdata = u;
2021}
Adam Langley95c29f32014-06-20 12:00:00 -07002022
Adam Langleyfcf25832014-12-18 17:42:32 -08002023void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
2024 int (*cb)(X509_STORE_CTX *, void *),
2025 void *arg) {
2026 ctx->app_verify_callback = cb;
2027 ctx->app_verify_arg = arg;
2028}
Adam Langley95c29f32014-06-20 12:00:00 -07002029
Adam Langleyfcf25832014-12-18 17:42:32 -08002030void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
2031 int (*cb)(int, X509_STORE_CTX *)) {
2032 ctx->verify_mode = mode;
2033 ctx->default_verify_callback = cb;
2034}
Adam Langley95c29f32014-06-20 12:00:00 -07002035
Adam Langleyfcf25832014-12-18 17:42:32 -08002036void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
2037 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2038}
Adam Langley1258b6a2014-06-20 12:00:00 -07002039
Adam Langleyfcf25832014-12-18 17:42:32 -08002040void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
2041 void *arg) {
2042 ssl_cert_set_cert_cb(c->cert, cb, arg);
2043}
David Benjamin859ec3c2014-09-02 16:29:36 -04002044
Adam Langleyfcf25832014-12-18 17:42:32 -08002045void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
2046 ssl_cert_set_cert_cb(s->cert, cb, arg);
2047}
Adam Langley95c29f32014-06-20 12:00:00 -07002048
Adam Langleyfcf25832014-12-18 17:42:32 -08002049static int ssl_has_key(SSL *s, size_t idx) {
2050 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2051 return cpk->x509 && cpk->privatekey;
2052}
David Benjamin033e5f42014-11-13 18:47:41 -05002053
David Benjaminf31e6812014-11-13 18:05:55 -05002054void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 unsigned long *out_mask_a) {
2056 CERT *c = s->cert;
2057 int rsa_enc, rsa_sign, dh_tmp;
2058 unsigned long mask_k, mask_a;
2059 int have_ecc_cert, ecdsa_ok;
2060 int have_ecdh_tmp;
2061 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002062
Adam Langleyfcf25832014-12-18 17:42:32 -08002063 if (c == NULL) {
2064 /* TODO(davidben): Is this codepath possible? */
2065 *out_mask_k = 0;
2066 *out_mask_a = 0;
2067 return;
2068 }
Adam Langley95c29f32014-06-20 12:00:00 -07002069
Adam Langleyfcf25832014-12-18 17:42:32 -08002070 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002071
Adam Langleyfcf25832014-12-18 17:42:32 -08002072 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2073 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2074 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2075 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2076 mask_k = 0;
2077 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002078
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 if (rsa_enc) {
2080 mask_k |= SSL_kRSA;
2081 }
2082 if (dh_tmp) {
2083 mask_k |= SSL_kEDH;
2084 }
2085 if (rsa_enc || rsa_sign) {
2086 mask_a |= SSL_aRSA;
2087 }
Adam Langley95c29f32014-06-20 12:00:00 -07002088
Adam Langleyfcf25832014-12-18 17:42:32 -08002089 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002090
Adam Langleyfcf25832014-12-18 17:42:32 -08002091 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2092 * key usage extension and on the client's curve preferences. */
2093 if (have_ecc_cert) {
2094 x = c->pkeys[SSL_PKEY_ECC].x509;
2095 /* This call populates extension flags (ex_flags). */
2096 X509_check_purpose(x, -1, 0);
2097 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2098 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2099 : 1;
2100 if (!tls1_check_ec_cert(s, x)) {
2101 ecdsa_ok = 0;
2102 }
2103 if (ecdsa_ok) {
2104 mask_a |= SSL_aECDSA;
2105 }
2106 }
Adam Langley95c29f32014-06-20 12:00:00 -07002107
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2109 * key, check it. */
2110 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
2111 mask_k |= SSL_kEECDH;
2112 }
Adam Langley95c29f32014-06-20 12:00:00 -07002113
Adam Langleyfcf25832014-12-18 17:42:32 -08002114 /* PSK requires a server callback. */
2115 if (s->psk_server_callback != NULL) {
2116 mask_k |= SSL_kPSK;
2117 mask_a |= SSL_aPSK;
2118 }
Adam Langley95c29f32014-06-20 12:00:00 -07002119
Adam Langleyfcf25832014-12-18 17:42:32 -08002120 *out_mask_k = mask_k;
2121 *out_mask_a = mask_a;
2122}
Adam Langley95c29f32014-06-20 12:00:00 -07002123
2124/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2125#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002126 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002127
Adam Langleyfcf25832014-12-18 17:42:32 -08002128int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2129 unsigned long alg_a;
2130 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2131 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002132
Adam Langleyfcf25832014-12-18 17:42:32 -08002133 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002134
Adam Langleyfcf25832014-12-18 17:42:32 -08002135 /* This call populates the ex_flags field correctly */
2136 X509_check_purpose(x, -1, 0);
2137 if (x->sig_alg && x->sig_alg->algorithm) {
2138 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2139 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2140 }
2141 if (alg_a & SSL_aECDSA) {
2142 /* key usage, if present, must allow signing */
2143 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2144 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2145 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2146 return 0;
2147 }
2148 }
Adam Langley95c29f32014-06-20 12:00:00 -07002149
Adam Langleyfcf25832014-12-18 17:42:32 -08002150 return 1; /* all checks are ok */
2151}
Adam Langley95c29f32014-06-20 12:00:00 -07002152
Adam Langleyfcf25832014-12-18 17:42:32 -08002153static int ssl_get_server_cert_index(const SSL *s) {
2154 int idx;
2155 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2156 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2157 idx = SSL_PKEY_RSA_SIGN;
2158 }
2159 if (idx == -1) {
2160 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2161 }
2162 return idx;
2163}
Adam Langley95c29f32014-06-20 12:00:00 -07002164
Adam Langleyfcf25832014-12-18 17:42:32 -08002165CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2166 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002167
Adam Langleyfcf25832014-12-18 17:42:32 -08002168 /* This may or may not be an error. */
2169 if (i < 0) {
2170 return NULL;
2171 }
Adam Langley95c29f32014-06-20 12:00:00 -07002172
Adam Langleyfcf25832014-12-18 17:42:32 -08002173 /* May be NULL. */
2174 return &s->cert->pkeys[i];
2175}
Adam Langley95c29f32014-06-20 12:00:00 -07002176
Adam Langleyfcf25832014-12-18 17:42:32 -08002177EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2178 unsigned long alg_a;
2179 CERT *c;
2180 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002181
Adam Langleyfcf25832014-12-18 17:42:32 -08002182 alg_a = cipher->algorithm_auth;
2183 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002184
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 if (alg_a & SSL_aRSA) {
2186 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2187 idx = SSL_PKEY_RSA_SIGN;
2188 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2189 idx = SSL_PKEY_RSA_ENC;
2190 }
2191 } else if ((alg_a & SSL_aECDSA) &&
2192 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2193 idx = SSL_PKEY_ECC;
2194 }
Adam Langley95c29f32014-06-20 12:00:00 -07002195
Adam Langleyfcf25832014-12-18 17:42:32 -08002196 if (idx == -1) {
2197 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2198 return NULL;
2199 }
Adam Langley95c29f32014-06-20 12:00:00 -07002200
Adam Langleyfcf25832014-12-18 17:42:32 -08002201 return c->pkeys[idx].privatekey;
2202}
Adam Langley95c29f32014-06-20 12:00:00 -07002203
Adam Langleyfcf25832014-12-18 17:42:32 -08002204void ssl_update_cache(SSL *s, int mode) {
2205 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07002206
Adam Langleyfcf25832014-12-18 17:42:32 -08002207 /* If the session_id_length is 0, we are not supposed to cache it, and it
2208 * would be rather hard to do anyway :-) */
2209 if (s->session->session_id_length == 0) {
2210 return;
2211 }
Adam Langley95c29f32014-06-20 12:00:00 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 i = s->initial_ctx->session_cache_mode;
2214 if ((i & mode) && !s->hit &&
2215 ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2216 SSL_CTX_add_session(s->initial_ctx, s->session)) &&
2217 s->initial_ctx->new_session_cb != NULL) {
2218 CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2219 if (!s->initial_ctx->new_session_cb(s, s->session)) {
2220 SSL_SESSION_free(s->session);
2221 }
2222 }
Adam Langley95c29f32014-06-20 12:00:00 -07002223
Adam Langleyfcf25832014-12-18 17:42:32 -08002224 /* auto flush every 255 connections */
2225 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
2226 if ((((mode & SSL_SESS_CACHE_CLIENT)
2227 ? s->initial_ctx->stats.sess_connect_good
2228 : s->initial_ctx->stats.sess_accept_good) &
2229 0xff) == 0xff) {
2230 SSL_CTX_flush_sessions(s->initial_ctx, (unsigned long)time(NULL));
2231 }
2232 }
2233}
Adam Langley95c29f32014-06-20 12:00:00 -07002234
Adam Langleyfcf25832014-12-18 17:42:32 -08002235int SSL_get_error(const SSL *s, int i) {
2236 int reason;
2237 unsigned long l;
2238 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002239
Adam Langleyfcf25832014-12-18 17:42:32 -08002240 if (i > 0) {
2241 return SSL_ERROR_NONE;
2242 }
Adam Langley95c29f32014-06-20 12:00:00 -07002243
Adam Langleyfcf25832014-12-18 17:42:32 -08002244 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2245 * where we do encode the error */
2246 l = ERR_peek_error();
2247 if (l != 0) {
2248 if (ERR_GET_LIB(l) == ERR_LIB_SYS) {
2249 return SSL_ERROR_SYSCALL;
2250 }
2251 return SSL_ERROR_SSL;
2252 }
Adam Langley95c29f32014-06-20 12:00:00 -07002253
Adam Langleyfcf25832014-12-18 17:42:32 -08002254 if (i == 0 && (s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2255 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2256 return SSL_ERROR_ZERO_RETURN;
2257 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002258
Adam Langleyfcf25832014-12-18 17:42:32 -08002259 assert(i < 0);
Adam Langleydc9b1412014-06-20 12:00:00 -07002260
Adam Langleyfcf25832014-12-18 17:42:32 -08002261 if (SSL_want_session(s)) {
2262 return SSL_ERROR_PENDING_SESSION;
2263 }
Adam Langley95c29f32014-06-20 12:00:00 -07002264
Adam Langleyfcf25832014-12-18 17:42:32 -08002265 if (SSL_want_certificate(s)) {
2266 return SSL_ERROR_PENDING_CERTIFICATE;
2267 }
Adam Langley95c29f32014-06-20 12:00:00 -07002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269 if (SSL_want_read(s)) {
2270 bio = SSL_get_rbio(s);
2271 if (BIO_should_read(bio)) {
2272 return SSL_ERROR_WANT_READ;
2273 }
Adam Langley95c29f32014-06-20 12:00:00 -07002274
Adam Langleyfcf25832014-12-18 17:42:32 -08002275 if (BIO_should_write(bio)) {
2276 /* This one doesn't make too much sense ... We never try to write to the
2277 * rbio, and an application program where rbio and wbio are separate
2278 * couldn't even know what it should wait for. However if we ever set
2279 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2280 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2281 * around that bug; so it might be safer to keep it. */
2282 return SSL_ERROR_WANT_WRITE;
2283 }
Adam Langley95c29f32014-06-20 12:00:00 -07002284
Adam Langleyfcf25832014-12-18 17:42:32 -08002285 if (BIO_should_io_special(bio)) {
2286 reason = BIO_get_retry_reason(bio);
2287 if (reason == BIO_RR_CONNECT) {
2288 return SSL_ERROR_WANT_CONNECT;
2289 }
Adam Langley95c29f32014-06-20 12:00:00 -07002290
Adam Langleyfcf25832014-12-18 17:42:32 -08002291 if (reason == BIO_RR_ACCEPT) {
2292 return SSL_ERROR_WANT_ACCEPT;
2293 }
Adam Langley95c29f32014-06-20 12:00:00 -07002294
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 return SSL_ERROR_SYSCALL; /* unknown */
2296 }
2297 }
Adam Langley95c29f32014-06-20 12:00:00 -07002298
Adam Langleyfcf25832014-12-18 17:42:32 -08002299 if (SSL_want_write(s)) {
2300 bio = SSL_get_wbio(s);
2301 if (BIO_should_write(bio)) {
2302 return SSL_ERROR_WANT_WRITE;
2303 }
Adam Langley95c29f32014-06-20 12:00:00 -07002304
Adam Langleyfcf25832014-12-18 17:42:32 -08002305 if (BIO_should_read(bio)) {
2306 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2307 return SSL_ERROR_WANT_READ;
2308 }
Adam Langley95c29f32014-06-20 12:00:00 -07002309
Adam Langleyfcf25832014-12-18 17:42:32 -08002310 if (BIO_should_io_special(bio)) {
2311 reason = BIO_get_retry_reason(bio);
2312 if (reason == BIO_RR_CONNECT) {
2313 return SSL_ERROR_WANT_CONNECT;
2314 }
Adam Langley95c29f32014-06-20 12:00:00 -07002315
Adam Langleyfcf25832014-12-18 17:42:32 -08002316 if (reason == BIO_RR_ACCEPT) {
2317 return SSL_ERROR_WANT_ACCEPT;
2318 }
Adam Langley95c29f32014-06-20 12:00:00 -07002319
Adam Langleyfcf25832014-12-18 17:42:32 -08002320 return SSL_ERROR_SYSCALL;
2321 }
2322 }
Adam Langley95c29f32014-06-20 12:00:00 -07002323
Adam Langleyfcf25832014-12-18 17:42:32 -08002324 if (SSL_want_x509_lookup(s)) {
2325 return SSL_ERROR_WANT_X509_LOOKUP;
2326 }
Adam Langley95c29f32014-06-20 12:00:00 -07002327
Adam Langleyfcf25832014-12-18 17:42:32 -08002328 if (SSL_want_channel_id_lookup(s)) {
2329 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2330 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002331
Adam Langleyfcf25832014-12-18 17:42:32 -08002332 return SSL_ERROR_SYSCALL;
2333}
Adam Langley0f4746e2014-08-13 12:26:32 -07002334
Adam Langleyfcf25832014-12-18 17:42:32 -08002335int SSL_do_handshake(SSL *s) {
2336 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002337
Adam Langleyfcf25832014-12-18 17:42:32 -08002338 if (s->handshake_func == NULL) {
2339 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2340 return -1;
2341 }
Adam Langley95c29f32014-06-20 12:00:00 -07002342
Adam Langleyfcf25832014-12-18 17:42:32 -08002343 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002344
Adam Langleyfcf25832014-12-18 17:42:32 -08002345 if (SSL_in_init(s)) {
2346 ret = s->handshake_func(s);
2347 }
2348 return ret;
2349}
Adam Langley95c29f32014-06-20 12:00:00 -07002350
Adam Langleyfcf25832014-12-18 17:42:32 -08002351void SSL_set_accept_state(SSL *s) {
2352 s->server = 1;
2353 s->shutdown = 0;
2354 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2355 s->handshake_func = s->method->ssl_accept;
2356 /* clear the current cipher */
2357 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002358}
Adam Langley95c29f32014-06-20 12:00:00 -07002359
Adam Langleyfcf25832014-12-18 17:42:32 -08002360void SSL_set_connect_state(SSL *s) {
2361 s->server = 0;
2362 s->shutdown = 0;
2363 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2364 s->handshake_func = s->method->ssl_connect;
2365 /* clear the current cipher */
2366 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002367}
Adam Langley95c29f32014-06-20 12:00:00 -07002368
Adam Langleyfcf25832014-12-18 17:42:32 -08002369int ssl_undefined_function(SSL *s) {
2370 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
2371 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2372 return 0;
2373}
Adam Langley95c29f32014-06-20 12:00:00 -07002374
Adam Langleyfcf25832014-12-18 17:42:32 -08002375int ssl_undefined_void_function(void) {
2376 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
2377 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2378 return 0;
2379}
Adam Langley95c29f32014-06-20 12:00:00 -07002380
Adam Langleyfcf25832014-12-18 17:42:32 -08002381int ssl_undefined_const_function(const SSL *s) {
2382 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2383 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2384 return 0;
2385}
Adam Langley95c29f32014-06-20 12:00:00 -07002386
Adam Langleyfcf25832014-12-18 17:42:32 -08002387static const char *ssl_get_version(int version) {
2388 switch (version) {
2389 case TLS1_2_VERSION:
2390 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002391
Adam Langleyfcf25832014-12-18 17:42:32 -08002392 case TLS1_1_VERSION:
2393 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002394
Adam Langleyfcf25832014-12-18 17:42:32 -08002395 case TLS1_VERSION:
2396 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002397
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 case SSL3_VERSION:
2399 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002400
Adam Langleyfcf25832014-12-18 17:42:32 -08002401 default:
2402 return "unknown";
2403 }
2404}
Adam Langley95c29f32014-06-20 12:00:00 -07002405
Adam Langleyfcf25832014-12-18 17:42:32 -08002406const char *SSL_get_version(const SSL *s) {
2407 return ssl_get_version(s->version);
2408}
Adam Langley95c29f32014-06-20 12:00:00 -07002409
Adam Langleyfcf25832014-12-18 17:42:32 -08002410const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2411 return ssl_get_version(sess->ssl_version);
2412}
Adam Langley95c29f32014-06-20 12:00:00 -07002413
Adam Langleyfcf25832014-12-18 17:42:32 -08002414void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002415 if (s->aead_read_ctx != NULL) {
2416 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2417 OPENSSL_free(s->aead_read_ctx);
2418 s->aead_read_ctx = NULL;
2419 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002420
Adam Langleyfcf25832014-12-18 17:42:32 -08002421 if (s->aead_write_ctx != NULL) {
2422 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2423 OPENSSL_free(s->aead_write_ctx);
2424 s->aead_write_ctx = NULL;
2425 }
2426}
Adam Langley95c29f32014-06-20 12:00:00 -07002427
Adam Langleyfcf25832014-12-18 17:42:32 -08002428X509 *SSL_get_certificate(const SSL *s) {
2429 if (s->cert != NULL) {
2430 return s->cert->key->x509;
2431 }
2432
2433 return NULL;
2434}
2435
2436EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2437 if (s->cert != NULL) {
2438 return s->cert->key->privatekey;
2439 }
2440
2441 return NULL;
2442}
2443
2444X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2445 if (ctx->cert != NULL) {
2446 return ctx->cert->key->x509;
2447 }
2448
2449 return NULL;
2450}
2451
2452EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2453 if (ctx->cert != NULL) {
2454 return ctx->cert->key->privatekey;
2455 }
2456
2457 return NULL;
2458}
2459
2460const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2461 if (s->session != NULL && s->session->cipher != NULL) {
2462 return s->session->cipher;
2463 }
2464
2465 return NULL;
2466}
2467
2468const void *SSL_get_current_compression(SSL *s) { return NULL; }
2469
2470const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2471
2472int ssl_init_wbio_buffer(SSL *s, int push) {
2473 BIO *bbio;
2474
2475 if (s->bbio == NULL) {
2476 bbio = BIO_new(BIO_f_buffer());
2477 if (bbio == NULL) {
2478 return 0;
2479 }
2480 s->bbio = bbio;
2481 } else {
2482 bbio = s->bbio;
2483 if (s->bbio == s->wbio) {
2484 s->wbio = BIO_pop(s->wbio);
2485 }
2486 }
2487
2488 BIO_reset(bbio);
2489 if (!BIO_set_read_buffer_size(bbio, 1)) {
2490 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2491 return 0;
2492 }
2493
2494 if (push) {
2495 if (s->wbio != bbio) {
2496 s->wbio = BIO_push(bbio, s->wbio);
2497 }
2498 } else {
2499 if (s->wbio == bbio) {
2500 s->wbio = BIO_pop(bbio);
2501 }
2502 }
2503
2504 return 1;
2505}
2506
2507void ssl_free_wbio_buffer(SSL *s) {
2508 if (s->bbio == NULL) {
2509 return;
2510 }
2511
2512 if (s->bbio == s->wbio) {
2513 /* remove buffering */
2514 s->wbio = BIO_pop(s->wbio);
2515 }
2516
2517 BIO_free(s->bbio);
2518 s->bbio = NULL;
2519}
2520
2521void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2522 ctx->quiet_shutdown = mode;
2523}
2524
2525int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2526 return ctx->quiet_shutdown;
2527}
2528
2529void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2530
2531int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2532
2533void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2534
2535int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2536
2537int SSL_version(const SSL *s) { return s->version; }
2538
2539SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2540
2541SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2542 if (ssl->ctx == ctx) {
2543 return ssl->ctx;
2544 }
2545
2546 if (ctx == NULL) {
2547 ctx = ssl->initial_ctx;
2548 }
2549
2550 if (ssl->cert != NULL) {
2551 ssl_cert_free(ssl->cert);
2552 }
2553
2554 ssl->cert = ssl_cert_dup(ctx->cert);
2555 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2556 if (ssl->ctx != NULL) {
2557 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2558 }
2559 ssl->ctx = ctx;
2560
2561 ssl->sid_ctx_length = ctx->sid_ctx_length;
2562 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2563 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2564
2565 return ssl->ctx;
2566}
2567
2568int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2569 return X509_STORE_set_default_paths(ctx->cert_store);
2570}
Adam Langley95c29f32014-06-20 12:00:00 -07002571
2572int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002573 const char *CApath) {
2574 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2575}
Adam Langley95c29f32014-06-20 12:00:00 -07002576
2577void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002578 void (*cb)(const SSL *ssl, int type, int val)) {
2579 ssl->info_callback = cb;
2580}
Adam Langley95c29f32014-06-20 12:00:00 -07002581
Adam Langleyfcf25832014-12-18 17:42:32 -08002582void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2583 int /*val*/) {
2584 return ssl->info_callback;
2585}
Adam Langley95c29f32014-06-20 12:00:00 -07002586
Adam Langleyfcf25832014-12-18 17:42:32 -08002587int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002590
Adam Langleyfcf25832014-12-18 17:42:32 -08002591void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002592
Adam Langleyfcf25832014-12-18 17:42:32 -08002593long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002594
Adam Langleyfcf25832014-12-18 17:42:32 -08002595int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2596 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2597 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2598 dup_func, free_func);
2599}
Adam Langley95c29f32014-06-20 12:00:00 -07002600
Adam Langleyfcf25832014-12-18 17:42:32 -08002601int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2602 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2603}
Adam Langley95c29f32014-06-20 12:00:00 -07002604
Adam Langleyfcf25832014-12-18 17:42:32 -08002605void *SSL_get_ex_data(const SSL *s, int idx) {
2606 return CRYPTO_get_ex_data(&s->ex_data, idx);
2607}
Adam Langley95c29f32014-06-20 12:00:00 -07002608
Adam Langleyfcf25832014-12-18 17:42:32 -08002609int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2610 CRYPTO_EX_dup *dup_func,
2611 CRYPTO_EX_free *free_func) {
2612 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2613 dup_func, free_func);
2614}
Adam Langley95c29f32014-06-20 12:00:00 -07002615
Adam Langleyfcf25832014-12-18 17:42:32 -08002616int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2617 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2618}
Adam Langley95c29f32014-06-20 12:00:00 -07002619
Adam Langleyfcf25832014-12-18 17:42:32 -08002620void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2621 return CRYPTO_get_ex_data(&s->ex_data, idx);
2622}
Adam Langley95c29f32014-06-20 12:00:00 -07002623
Adam Langleyfcf25832014-12-18 17:42:32 -08002624int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002625
Adam Langleyfcf25832014-12-18 17:42:32 -08002626X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2627 return ctx->cert_store;
2628}
Adam Langley95c29f32014-06-20 12:00:00 -07002629
Adam Langleyfcf25832014-12-18 17:42:32 -08002630void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2631 if (ctx->cert_store != NULL) {
2632 X509_STORE_free(ctx->cert_store);
2633 }
2634 ctx->cert_store = store;
2635}
Adam Langley95c29f32014-06-20 12:00:00 -07002636
Adam Langleyfcf25832014-12-18 17:42:32 -08002637int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002638
Adam Langleyfcf25832014-12-18 17:42:32 -08002639void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2640 RSA *(*cb)(SSL *ssl, int is_export,
2641 int keylength)) {
2642 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2643}
Adam Langley95c29f32014-06-20 12:00:00 -07002644
Adam Langleyfcf25832014-12-18 17:42:32 -08002645void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2646 int keylength)) {
2647 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2648}
2649
2650void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2651 DH *(*dh)(SSL *ssl, int is_export,
2652 int keylength)) {
2653 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2654}
2655
2656void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2657 int keylength)) {
2658 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2659}
2660
2661void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2662 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2663 int keylength)) {
2664 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2665}
2666
2667void SSL_set_tmp_ecdh_callback(SSL *ssl,
2668 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2669 int keylength)) {
2670 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2671}
2672
2673int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2674 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2675 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2676 SSL_R_DATA_LENGTH_TOO_LONG);
2677 return 0;
2678 }
2679
2680 if (ctx->psk_identity_hint != NULL) {
2681 OPENSSL_free(ctx->psk_identity_hint);
2682 }
2683
2684 if (identity_hint != NULL) {
2685 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2686 if (ctx->psk_identity_hint == NULL) {
2687 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002688 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 } else {
2690 ctx->psk_identity_hint = NULL;
2691 }
Adam Langley95c29f32014-06-20 12:00:00 -07002692
Adam Langleyfcf25832014-12-18 17:42:32 -08002693 return 1;
2694}
2695
2696int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2697 if (s == NULL) {
2698 return 0;
2699 }
2700
2701 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2702 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2703 SSL_R_DATA_LENGTH_TOO_LONG);
2704 return 0;
2705 }
2706
2707 /* Clear currently configured hint, if any. */
2708 if (s->psk_identity_hint != NULL) {
2709 OPENSSL_free(s->psk_identity_hint);
2710 s->psk_identity_hint = NULL;
2711 }
2712
2713 if (identity_hint != NULL) {
2714 s->psk_identity_hint = BUF_strdup(identity_hint);
2715 if (s->psk_identity_hint == NULL) {
2716 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002717 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002718 }
Adam Langley95c29f32014-06-20 12:00:00 -07002719
Adam Langleyfcf25832014-12-18 17:42:32 -08002720 return 1;
2721}
Adam Langley95c29f32014-06-20 12:00:00 -07002722
Adam Langleyfcf25832014-12-18 17:42:32 -08002723const char *SSL_get_psk_identity_hint(const SSL *s) {
2724 if (s == NULL) {
2725 return NULL;
2726 }
2727 return s->psk_identity_hint;
2728}
Adam Langley95c29f32014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730const char *SSL_get_psk_identity(const SSL *s) {
2731 if (s == NULL || s->session == NULL) {
2732 return NULL;
2733 }
Adam Langley95c29f32014-06-20 12:00:00 -07002734
Adam Langleyfcf25832014-12-18 17:42:32 -08002735 return s->session->psk_identity;
2736}
Adam Langley95c29f32014-06-20 12:00:00 -07002737
Adam Langleyfcf25832014-12-18 17:42:32 -08002738void SSL_set_psk_client_callback(
2739 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2740 unsigned int max_identity_len, uint8_t *psk,
2741 unsigned int max_psk_len)) {
2742 s->psk_client_callback = cb;
2743}
Adam Langley95c29f32014-06-20 12:00:00 -07002744
Adam Langleyfcf25832014-12-18 17:42:32 -08002745void SSL_CTX_set_psk_client_callback(
2746 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2747 unsigned int max_identity_len,
2748 uint8_t *psk, unsigned int max_psk_len)) {
2749 ctx->psk_client_callback = cb;
2750}
Adam Langley95c29f32014-06-20 12:00:00 -07002751
Adam Langleyfcf25832014-12-18 17:42:32 -08002752void SSL_set_psk_server_callback(
2753 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2754 unsigned int max_psk_len)) {
2755 s->psk_server_callback = cb;
2756}
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langleyfcf25832014-12-18 17:42:32 -08002758void SSL_CTX_set_psk_server_callback(
2759 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2760 uint8_t *psk, unsigned int max_psk_len)) {
2761 ctx->psk_server_callback = cb;
2762}
Adam Langley95c29f32014-06-20 12:00:00 -07002763
Adam Langleyfcf25832014-12-18 17:42:32 -08002764void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2765 ctx->min_version = version;
2766}
Adam Langley95c29f32014-06-20 12:00:00 -07002767
Adam Langleyfcf25832014-12-18 17:42:32 -08002768void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2769 ctx->max_version = version;
2770}
Adam Langley0289c732014-06-20 12:00:00 -07002771
Adam Langleyfcf25832014-12-18 17:42:32 -08002772void SSL_set_min_version(SSL *ssl, uint16_t version) {
2773 ssl->min_version = version;
2774}
Adam Langley0289c732014-06-20 12:00:00 -07002775
Adam Langleyfcf25832014-12-18 17:42:32 -08002776void SSL_set_max_version(SSL *ssl, uint16_t version) {
2777 ssl->max_version = version;
2778}
Adam Langley95c29f32014-06-20 12:00:00 -07002779
Adam Langleyfcf25832014-12-18 17:42:32 -08002780void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2781 void (*cb)(int write_p, int version,
2782 int content_type, const void *buf,
2783 size_t len, SSL *ssl, void *arg)) {
2784 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2785}
2786void SSL_set_msg_callback(SSL *ssl,
2787 void (*cb)(int write_p, int version, int content_type,
2788 const void *buf, size_t len, SSL *ssl,
2789 void *arg)) {
2790 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2791}
Adam Langley95c29f32014-06-20 12:00:00 -07002792
Adam Langleyfcf25832014-12-18 17:42:32 -08002793void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2794 if (ctx->keylog_bio != NULL) {
2795 BIO_free(ctx->keylog_bio);
2796 }
2797 ctx->keylog_bio = keylog_bio;
2798}
Adam Langley95c29f32014-06-20 12:00:00 -07002799
Adam Langleyfcf25832014-12-18 17:42:32 -08002800static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2801 static const char hextable[] = "0123456789abcdef";
2802 uint8_t *out;
2803 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002804
Adam Langleyfcf25832014-12-18 17:42:32 -08002805 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2806 return 0;
2807 }
Adam Langley95c29f32014-06-20 12:00:00 -07002808
Adam Langleyfcf25832014-12-18 17:42:32 -08002809 for (i = 0; i < in_len; i++) {
2810 *(out++) = (uint8_t)hextable[in[i] >> 4];
2811 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2812 }
Adam Langley95c29f32014-06-20 12:00:00 -07002813
Adam Langleyfcf25832014-12-18 17:42:32 -08002814 return 1;
2815}
David Benjamin859ec3c2014-09-02 16:29:36 -04002816
2817int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002818 const uint8_t *encrypted_premaster,
2819 size_t encrypted_premaster_len,
2820 const uint8_t *premaster,
2821 size_t premaster_len) {
2822 BIO *bio = ctx->keylog_bio;
2823 CBB cbb;
2824 uint8_t *out;
2825 size_t out_len;
2826 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002827
Adam Langleyfcf25832014-12-18 17:42:32 -08002828 if (bio == NULL) {
2829 return 1;
2830 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002831
Adam Langleyfcf25832014-12-18 17:42:32 -08002832 if (encrypted_premaster_len < 8) {
2833 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2834 ERR_R_INTERNAL_ERROR);
2835 return 0;
2836 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002837
Adam Langleyfcf25832014-12-18 17:42:32 -08002838 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2839 return 0;
2840 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002841
Adam Langleyfcf25832014-12-18 17:42:32 -08002842 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2843 /* Only the first 8 bytes of the encrypted premaster secret are
2844 * logged. */
2845 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2846 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2847 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2848 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2849 !CBB_finish(&cbb, &out, &out_len)) {
2850 CBB_cleanup(&cbb);
2851 return 0;
2852 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002853
Adam Langleyfcf25832014-12-18 17:42:32 -08002854 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2855 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2856 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002857
Adam Langleyfcf25832014-12-18 17:42:32 -08002858 OPENSSL_free(out);
2859 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002860}
2861
Adam Langleyfcf25832014-12-18 17:42:32 -08002862int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2863 size_t client_random_len, const uint8_t *master,
2864 size_t master_len) {
2865 BIO *bio = ctx->keylog_bio;
2866 CBB cbb;
2867 uint8_t *out;
2868 size_t out_len;
2869 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002870
Adam Langleyfcf25832014-12-18 17:42:32 -08002871 if (bio == NULL) {
2872 return 1;
2873 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002874
Adam Langleyfcf25832014-12-18 17:42:32 -08002875 if (client_random_len != 32) {
2876 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2877 return 0;
2878 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002879
Adam Langleyfcf25832014-12-18 17:42:32 -08002880 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2881 return 0;
2882 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002883
Adam Langleyfcf25832014-12-18 17:42:32 -08002884 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2885 !cbb_add_hex(&cbb, client_random, 32) ||
2886 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2887 !cbb_add_hex(&cbb, master, master_len) ||
2888 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2889 !CBB_finish(&cbb, &out, &out_len)) {
2890 CBB_cleanup(&cbb);
2891 return 0;
2892 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002893
Adam Langleyfcf25832014-12-18 17:42:32 -08002894 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2895 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2896 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002897
Adam Langleyfcf25832014-12-18 17:42:32 -08002898 OPENSSL_free(out);
2899 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002900}
2901
Adam Langleyfcf25832014-12-18 17:42:32 -08002902int SSL_cutthrough_complete(const SSL *s) {
2903 return (
2904 !s->server && /* cutthrough only applies to clients */
2905 !s->hit && /* full-handshake */
2906 s->version >= SSL3_VERSION &&
2907 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
2908 (SSL_get_mode((SSL *)s) &
2909 SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
2910 ssl3_can_cutthrough(s) && /* cutthrough allowed */
2911 s->s3->previous_server_finished_len ==
2912 0 && /* not a renegotiation handshake */
2913 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
2914 s->state == SSL3_ST_CR_CHANGE || s->state == SSL3_ST_CR_FINISHED_A));
2915}
Adam Langley95c29f32014-06-20 12:00:00 -07002916
Adam Langleyfcf25832014-12-18 17:42:32 -08002917void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2918 size_t *ssl_session_size) {
2919 *ssl_size = sizeof(SSL);
2920 *ssl_ctx_size = sizeof(SSL_CTX);
2921 *ssl_session_size = sizeof(SSL_SESSION);
2922}
Feng Lu41aa3252014-11-21 22:47:56 -08002923
Adam Langleyfcf25832014-12-18 17:42:32 -08002924int ssl3_can_cutthrough(const SSL *s) {
2925 const SSL_CIPHER *c;
2926
2927 /* require a strong enough cipher */
2928 if (SSL_get_cipher_bits(s, NULL) < 128) {
2929 return 0;
2930 }
2931
2932 /* require ALPN or NPN extension */
2933 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen) {
2934 return 0;
2935 }
2936
2937 /* require a forward-secret cipher */
2938 c = SSL_get_current_cipher(s);
2939 if (!c ||
2940 (c->algorithm_mkey != SSL_kEDH && c->algorithm_mkey != SSL_kEECDH)) {
2941 return 0;
2942 }
2943
2944 return 1;
2945}
2946
2947const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2948 switch (version) {
2949 case SSL3_VERSION:
2950 return &SSLv3_enc_data;
2951
2952 case TLS1_VERSION:
2953 return &TLSv1_enc_data;
2954
2955 case TLS1_1_VERSION:
2956 return &TLSv1_1_enc_data;
2957
2958 case TLS1_2_VERSION:
2959 return &TLSv1_2_enc_data;
2960
2961 case DTLS1_VERSION:
2962 return &DTLSv1_enc_data;
2963
2964 case DTLS1_2_VERSION:
2965 return &DTLSv1_2_enc_data;
2966
2967 default:
2968 return NULL;
2969 }
2970}
2971
2972uint16_t ssl3_get_max_server_version(const SSL *s) {
2973 uint16_t max_version;
2974
2975 if (SSL_IS_DTLS(s)) {
2976 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2977 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2978 return DTLS1_2_VERSION;
2979 }
2980 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2981 return DTLS1_VERSION;
2982 }
2983 return 0;
2984 }
2985
2986 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2987 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2988 return TLS1_2_VERSION;
2989 }
2990 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2991 return TLS1_1_VERSION;
2992 }
2993 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2994 return TLS1_VERSION;
2995 }
2996 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2997 return SSL3_VERSION;
2998 }
2999 return 0;
3000}
3001
3002uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
3003 uint16_t version = 0;
3004
3005 if (SSL_IS_DTLS(s)) {
3006 /* Clamp client_version to max_version. */
3007 if (s->max_version != 0 && client_version < s->max_version) {
3008 client_version = s->max_version;
3009 }
3010
3011 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
3012 version = DTLS1_2_VERSION;
3013 } else if (client_version <= DTLS1_VERSION &&
3014 !(s->options & SSL_OP_NO_DTLSv1)) {
3015 version = DTLS1_VERSION;
3016 }
3017
3018 /* Check against min_version. */
3019 if (version != 0 && s->min_version != 0 && version > s->min_version) {
3020 return 0;
3021 }
3022 return version;
3023 } else {
3024 /* Clamp client_version to max_version. */
3025 if (s->max_version != 0 && client_version > s->max_version) {
3026 client_version = s->max_version;
3027 }
3028
3029 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
3030 version = TLS1_2_VERSION;
3031 } else if (client_version >= TLS1_1_VERSION &&
3032 !(s->options & SSL_OP_NO_TLSv1_1)) {
3033 version = TLS1_1_VERSION;
3034 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
3035 version = TLS1_VERSION;
3036 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
3037 version = SSL3_VERSION;
3038 }
3039
3040 /* Check against min_version. */
3041 if (version != 0 && s->min_version != 0 && version < s->min_version) {
3042 return 0;
3043 }
3044 return version;
3045 }
3046}
3047
3048uint16_t ssl3_get_max_client_version(SSL *s) {
3049 unsigned long options = s->options;
3050 uint16_t version = 0;
3051
3052 /* OpenSSL's API for controlling versions entails blacklisting individual
3053 * protocols. This has two problems. First, on the client, the protocol can
3054 * only express a contiguous range of versions. Second, a library consumer
3055 * trying to set a maximum version cannot disable protocol versions that get
3056 * added in a future version of the library.
3057 *
3058 * To account for both of these, OpenSSL interprets the client-side bitmask
3059 * as a min/max range by picking the lowest contiguous non-empty range of
3060 * enabled protocols. Note that this means it is impossible to set a maximum
3061 * version of TLS 1.2 in a future-proof way.
3062 *
3063 * By this scheme, the maximum version is the lowest version V such that V is
3064 * enabled and V+1 is disabled or unimplemented. */
3065 if (SSL_IS_DTLS(s)) {
3066 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3067 version = DTLS1_2_VERSION;
3068 }
3069 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3070 version = DTLS1_VERSION;
3071 }
3072 if (s->max_version != 0 && version < s->max_version) {
3073 version = s->max_version;
3074 }
3075 } else {
3076 if (!(options & SSL_OP_NO_TLSv1_2)) {
3077 version = TLS1_2_VERSION;
3078 }
3079 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3080 version = TLS1_1_VERSION;
3081 }
3082 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3083 version = TLS1_VERSION;
3084 }
3085 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3086 version = SSL3_VERSION;
3087 }
3088 if (s->max_version != 0 && version > s->max_version) {
3089 version = s->max_version;
3090 }
3091 }
3092
3093 return version;
3094}
3095
3096int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3097 if (SSL_IS_DTLS(s)) {
3098 if (s->max_version != 0 && version < s->max_version) {
3099 return 0;
3100 }
3101 if (s->min_version != 0 && version > s->min_version) {
3102 return 0;
3103 }
3104
3105 switch (version) {
3106 case DTLS1_VERSION:
3107 return !(s->options & SSL_OP_NO_DTLSv1);
3108
3109 case DTLS1_2_VERSION:
3110 return !(s->options & SSL_OP_NO_DTLSv1_2);
3111
3112 default:
3113 return 0;
3114 }
3115 } else {
3116 if (s->max_version != 0 && version > s->max_version) {
3117 return 0;
3118 }
3119 if (s->min_version != 0 && version < s->min_version) {
3120 return 0;
3121 }
3122
3123 switch (version) {
3124 case SSL3_VERSION:
3125 return !(s->options & SSL_OP_NO_SSLv3);
3126
3127 case TLS1_VERSION:
3128 return !(s->options & SSL_OP_NO_TLSv1);
3129
3130 case TLS1_1_VERSION:
3131 return !(s->options & SSL_OP_NO_TLSv1_1);
3132
3133 case TLS1_2_VERSION:
3134 return !(s->options & SSL_OP_NO_TLSv1_2);
3135
3136 default:
3137 return 0;
3138 }
3139 }
3140}
3141
David Benjaminea72bd02014-12-21 21:27:41 -05003142uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3143 if (!SSL_IS_DTLS(s)) {
3144 return wire_version;
3145 }
3146
3147 uint16_t tls_version = ~wire_version;
3148 uint16_t version = tls_version + 0x0201;
3149 /* If either component overflowed, clamp it so comparisons still work. */
3150 if ((version >> 8) < (tls_version >> 8)) {
3151 version = 0xff00 | (version & 0xff);
3152 }
3153 if ((version & 0xff) < (tls_version & 0xff)) {
3154 version = (version & 0xff00) | 0xff;
3155 }
3156 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3157 if (version == TLS1_VERSION) {
3158 version = TLS1_1_VERSION;
3159 }
3160 return version;
3161}
3162
Adam Langleyfcf25832014-12-18 17:42:32 -08003163int SSL_cache_hit(SSL *s) { return s->hit; }
3164
3165int SSL_is_server(SSL *s) { return s->server; }
3166
3167void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3168 s->fastradio_padding = on_off;
3169}