blob: d179dc85d76d5fd2158d868fca57224ec96837c5 [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) {
377 if (s->cert != NULL) {
378 ssl_cert_free(s->cert);
Adam Langley95c29f32014-06-20 12:00:00 -0700379 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 if (s->ctx != NULL) {
381 SSL_CTX_free(s->ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700382 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 OPENSSL_free(s);
384 }
385 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 return NULL;
388}
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langleyfcf25832014-12-18 17:42:32 -0800390int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
391 unsigned int sid_ctx_len) {
392 if (sid_ctx_len > sizeof ctx->sid_ctx) {
393 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
394 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
395 return 0;
396 }
397 ctx->sid_ctx_length = sid_ctx_len;
398 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700399
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 return 1;
401}
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
404 unsigned int sid_ctx_len) {
405 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
406 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
407 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
408 return 0;
409 }
410 ssl->sid_ctx_length = sid_ctx_len;
411 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 return 1;
414}
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langleyfcf25832014-12-18 17:42:32 -0800416int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
417 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
418 ctx->generate_session_id = cb;
419 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
420 return 1;
421}
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
424 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
425 ssl->generate_session_id = cb;
426 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
427 return 1;
428}
Adam Langley95c29f32014-06-20 12:00:00 -0700429
Adam Langleyfcf25832014-12-18 17:42:32 -0800430int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
431 unsigned int id_len) {
432 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
433 * can "construct" a session to give us the desired check - ie. to find if
434 * there's a session in the hash table that would conflict with any new
435 * session built out of this id/id_len and the ssl_version in use by this
436 * SSL. */
437 SSL_SESSION r, *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700438
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 if (id_len > sizeof r.session_id) {
440 return 0;
441 }
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 r.ssl_version = ssl->version;
444 r.session_id_length = id_len;
445 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
448 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
449 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
450 return p != NULL;
451}
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
454 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
455}
456
457int SSL_set_purpose(SSL *s, int purpose) {
458 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
459}
460
461int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
462 return X509_VERIFY_PARAM_set_trust(s->param, trust);
463}
464
465int SSL_set_trust(SSL *s, int trust) {
466 return X509_VERIFY_PARAM_set_trust(s->param, trust);
467}
468
469int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
470 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
471}
472
473int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
474 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
475}
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Adam Langley858a88d2014-06-20 12:00:00 -0700477void ssl_cipher_preference_list_free(
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 struct ssl_cipher_preference_list_st *cipher_list) {
479 sk_SSL_CIPHER_free(cipher_list->ciphers);
480 OPENSSL_free(cipher_list->in_group_flags);
481 OPENSSL_free(cipher_list);
482}
Adam Langley858a88d2014-06-20 12:00:00 -0700483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
485 struct ssl_cipher_preference_list_st *cipher_list) {
486 struct ssl_cipher_preference_list_st *ret = NULL;
487 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700488
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
490 if (!ret) {
491 goto err;
492 }
493
494 ret->ciphers = NULL;
495 ret->in_group_flags = NULL;
496 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
497 if (!ret->ciphers) {
498 goto err;
499 }
500 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
501 if (!ret->in_group_flags) {
502 goto err;
503 }
504
505 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700506
507err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 if (ret && ret->ciphers) {
509 sk_SSL_CIPHER_free(ret->ciphers);
510 }
511 if (ret) {
512 OPENSSL_free(ret);
513 }
514 return NULL;
515}
Adam Langley858a88d2014-06-20 12:00:00 -0700516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
518 STACK_OF(SSL_CIPHER) * ciphers) {
519 struct ssl_cipher_preference_list_st *ret = NULL;
520 size_t n = sk_SSL_CIPHER_num(ciphers);
Adam Langley858a88d2014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
523 if (!ret) {
524 goto err;
525 }
526 ret->ciphers = NULL;
527 ret->in_group_flags = NULL;
528 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
529 if (!ret->ciphers) {
530 goto err;
531 }
532 ret->in_group_flags = OPENSSL_malloc(n);
533 if (!ret->in_group_flags) {
534 goto err;
535 }
536 memset(ret->in_group_flags, 0, n);
537 return ret;
Adam Langley858a88d2014-06-20 12:00:00 -0700538
539err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 if (ret && ret->ciphers) {
541 sk_SSL_CIPHER_free(ret->ciphers);
542 }
543 if (ret) {
544 OPENSSL_free(ret);
545 }
546 return NULL;
547}
Adam Langley858a88d2014-06-20 12:00:00 -0700548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700550
Adam Langleyfcf25832014-12-18 17:42:32 -0800551X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langleyfcf25832014-12-18 17:42:32 -0800553void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
Adam Langley95c29f32014-06-20 12:00:00 -0700554
Adam Langleyfcf25832014-12-18 17:42:32 -0800555void SSL_free(SSL *s) {
556 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700557
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 if (s == NULL) {
559 return;
560 }
Adam Langley95c29f32014-06-20 12:00:00 -0700561
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);
563 if (i > 0) {
564 return;
565 }
Adam Langley95c29f32014-06-20 12:00:00 -0700566
Adam Langleyfcf25832014-12-18 17:42:32 -0800567 if (s->param) {
568 X509_VERIFY_PARAM_free(s->param);
569 }
Adam Langley95c29f32014-06-20 12:00:00 -0700570
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 if (s->bbio != NULL) {
574 /* If the buffering BIO is in place, pop it off */
575 if (s->bbio == s->wbio) {
576 s->wbio = BIO_pop(s->wbio);
577 }
578 BIO_free(s->bbio);
579 s->bbio = NULL;
580 }
Adam Langley95c29f32014-06-20 12:00:00 -0700581
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 if (s->rbio != NULL) {
583 BIO_free_all(s->rbio);
584 }
Adam Langley95c29f32014-06-20 12:00:00 -0700585
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 if (s->wbio != NULL && s->wbio != s->rbio) {
587 BIO_free_all(s->wbio);
588 }
Adam Langley95c29f32014-06-20 12:00:00 -0700589
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 if (s->init_buf != NULL) {
591 BUF_MEM_free(s->init_buf);
592 }
Adam Langley95c29f32014-06-20 12:00:00 -0700593
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 /* add extra stuff */
595 if (s->cipher_list != NULL) {
596 ssl_cipher_preference_list_free(s->cipher_list);
597 }
598 if (s->cipher_list_by_id != NULL) {
599 sk_SSL_CIPHER_free(s->cipher_list_by_id);
600 }
Adam Langley95c29f32014-06-20 12:00:00 -0700601
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 if (s->session != NULL) {
603 ssl_clear_bad_session(s);
604 SSL_SESSION_free(s->session);
605 }
Adam Langley95c29f32014-06-20 12:00:00 -0700606
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 ssl_clear_cipher_ctx(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 if (s->cert != NULL) {
610 ssl_cert_free(s->cert);
611 }
Adam Langley0289c732014-06-20 12:00:00 -0700612
Adam Langleyfcf25832014-12-18 17:42:32 -0800613 if (s->tlsext_hostname) {
614 OPENSSL_free(s->tlsext_hostname);
615 }
616 if (s->initial_ctx) {
617 SSL_CTX_free(s->initial_ctx);
618 }
619 if (s->tlsext_ecpointformatlist) {
620 OPENSSL_free(s->tlsext_ecpointformatlist);
621 }
622 if (s->tlsext_ellipticcurvelist) {
623 OPENSSL_free(s->tlsext_ellipticcurvelist);
624 }
625 if (s->alpn_client_proto_list) {
626 OPENSSL_free(s->alpn_client_proto_list);
627 }
628 if (s->tlsext_channel_id_private) {
629 EVP_PKEY_free(s->tlsext_channel_id_private);
630 }
631 if (s->psk_identity_hint) {
632 OPENSSL_free(s->psk_identity_hint);
633 }
634 if (s->client_CA != NULL) {
635 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
636 }
637 if (s->next_proto_negotiated) {
638 OPENSSL_free(s->next_proto_negotiated);
639 }
640 if (s->srtp_profiles) {
641 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
642 }
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 if (s->method != NULL) {
645 s->method->ssl_free(s);
646 }
647 if (s->ctx) {
648 SSL_CTX_free(s->ctx);
649 }
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Adam Langleyfcf25832014-12-18 17:42:32 -0800651 OPENSSL_free(s);
652}
Adam Langley95c29f32014-06-20 12:00:00 -0700653
Adam Langleyfcf25832014-12-18 17:42:32 -0800654void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
655 /* If the output buffering BIO is still in place, remove it. */
656 if (s->bbio != NULL) {
657 if (s->wbio == s->bbio) {
658 s->wbio = s->wbio->next_bio;
659 s->bbio->next_bio = NULL;
660 }
661 }
Adam Langley95c29f32014-06-20 12:00:00 -0700662
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 if (s->rbio != NULL && s->rbio != rbio) {
664 BIO_free_all(s->rbio);
665 }
666 if (s->wbio != NULL && s->wbio != wbio && s->rbio != s->wbio) {
667 BIO_free_all(s->wbio);
668 }
669 s->rbio = rbio;
670 s->wbio = wbio;
671}
Adam Langley95c29f32014-06-20 12:00:00 -0700672
Adam Langleyfcf25832014-12-18 17:42:32 -0800673BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langleyfcf25832014-12-18 17:42:32 -0800675BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
Adam Langley95c29f32014-06-20 12:00:00 -0700676
Adam Langleyfcf25832014-12-18 17:42:32 -0800677int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
Adam Langley95c29f32014-06-20 12:00:00 -0700678
Adam Langleyfcf25832014-12-18 17:42:32 -0800679int SSL_get_rfd(const SSL *s) {
680 int ret = -1;
681 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700682
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 b = SSL_get_rbio(s);
684 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
685 if (r != NULL) {
686 BIO_get_fd(r, &ret);
687 }
688 return ret;
689}
Adam Langley95c29f32014-06-20 12:00:00 -0700690
Adam Langleyfcf25832014-12-18 17:42:32 -0800691int SSL_get_wfd(const SSL *s) {
692 int ret = -1;
693 BIO *b, *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Adam Langleyfcf25832014-12-18 17:42:32 -0800695 b = SSL_get_wbio(s);
696 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
697 if (r != NULL) {
698 BIO_get_fd(r, &ret);
699 }
Adam Langley95c29f32014-06-20 12:00:00 -0700700
Adam Langleyfcf25832014-12-18 17:42:32 -0800701 return ret;
702}
Adam Langley95c29f32014-06-20 12:00:00 -0700703
Adam Langleyfcf25832014-12-18 17:42:32 -0800704int SSL_set_fd(SSL *s, int fd) {
705 int ret = 0;
706 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700707
Adam Langleyfcf25832014-12-18 17:42:32 -0800708 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langleyfcf25832014-12-18 17:42:32 -0800710 if (bio == NULL) {
711 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
712 goto err;
713 }
714 BIO_set_fd(bio, fd, BIO_NOCLOSE);
715 SSL_set_bio(s, bio, bio);
716 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Adam Langley95c29f32014-06-20 12:00:00 -0700718err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800719 return ret;
720}
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722int SSL_set_wfd(SSL *s, int fd) {
723 int ret = 0;
724 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700725
Adam Langleyfcf25832014-12-18 17:42:32 -0800726 if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
727 (int)BIO_get_fd(s->rbio, NULL) != fd) {
728 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700729
Adam Langleyfcf25832014-12-18 17:42:32 -0800730 if (bio == NULL) {
731 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
732 goto err;
733 }
734 BIO_set_fd(bio, fd, BIO_NOCLOSE);
735 SSL_set_bio(s, SSL_get_rbio(s), bio);
736 } else {
737 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
738 }
739
740 ret = 1;
741
Adam Langley95c29f32014-06-20 12:00:00 -0700742err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800743 return ret;
744}
Adam Langley95c29f32014-06-20 12:00:00 -0700745
Adam Langleyfcf25832014-12-18 17:42:32 -0800746int SSL_set_rfd(SSL *s, int fd) {
747 int ret = 0;
748 BIO *bio = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700749
Adam Langleyfcf25832014-12-18 17:42:32 -0800750 if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
751 (int)BIO_get_fd(s->wbio, NULL) != fd) {
752 bio = BIO_new(BIO_s_fd());
Adam Langley95c29f32014-06-20 12:00:00 -0700753
Adam Langleyfcf25832014-12-18 17:42:32 -0800754 if (bio == NULL) {
755 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
756 goto err;
757 }
758 BIO_set_fd(bio, fd, BIO_NOCLOSE);
759 SSL_set_bio(s, bio, SSL_get_wbio(s));
760 } else {
761 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
762 }
763 ret = 1;
764
Adam Langley95c29f32014-06-20 12:00:00 -0700765err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800766 return ret;
767}
Adam Langley95c29f32014-06-20 12:00:00 -0700768
769/* return length of latest Finished message we sent, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800770size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
771 size_t ret = 0;
772
773 if (s->s3 != NULL) {
774 ret = s->s3->tmp.finish_md_len;
775 if (count > ret) {
776 count = ret;
777 }
778 memcpy(buf, s->s3->tmp.finish_md, count);
779 }
780
781 return ret;
782}
Adam Langley95c29f32014-06-20 12:00:00 -0700783
784/* return length of latest Finished message we expected, copy to 'buf' */
Adam Langleyfcf25832014-12-18 17:42:32 -0800785size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
786 size_t ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700787
Adam Langleyfcf25832014-12-18 17:42:32 -0800788 if (s->s3 != NULL) {
789 ret = s->s3->tmp.peer_finish_md_len;
790 if (count > ret) {
791 count = ret;
792 }
793 memcpy(buf, s->s3->tmp.peer_finish_md, count);
794 }
Adam Langley95c29f32014-06-20 12:00:00 -0700795
Adam Langleyfcf25832014-12-18 17:42:32 -0800796 return ret;
797}
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Adam Langleyfcf25832014-12-18 17:42:32 -0800799int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700800
Adam Langleyfcf25832014-12-18 17:42:32 -0800801int SSL_get_verify_depth(const SSL *s) {
802 return X509_VERIFY_PARAM_get_depth(s->param);
803}
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langleyfcf25832014-12-18 17:42:32 -0800805int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
806 return s->verify_callback;
807}
Adam Langley95c29f32014-06-20 12:00:00 -0700808
Adam Langleyfcf25832014-12-18 17:42:32 -0800809int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langleyfcf25832014-12-18 17:42:32 -0800811int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
812 return X509_VERIFY_PARAM_get_depth(ctx->param);
813}
Adam Langley95c29f32014-06-20 12:00:00 -0700814
Adam Langleyfcf25832014-12-18 17:42:32 -0800815int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
816 return ctx->default_verify_callback;
817}
Adam Langley95c29f32014-06-20 12:00:00 -0700818
Adam Langleyfcf25832014-12-18 17:42:32 -0800819void SSL_set_verify(SSL *s, int mode,
820 int (*callback)(int ok, X509_STORE_CTX *ctx)) {
821 s->verify_mode = mode;
822 if (callback != NULL) {
823 s->verify_callback = callback;
824 }
825}
Adam Langley95c29f32014-06-20 12:00:00 -0700826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827void SSL_set_verify_depth(SSL *s, int depth) {
828 X509_VERIFY_PARAM_set_depth(s->param, depth);
829}
Adam Langley95c29f32014-06-20 12:00:00 -0700830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
Adam Langley95c29f32014-06-20 12:00:00 -0700832
Adam Langleyfcf25832014-12-18 17:42:32 -0800833int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
Adam Langleyfcf25832014-12-18 17:42:32 -0800835int SSL_pending(const SSL *s) {
836 /* SSL_pending cannot work properly if read-ahead is enabled
837 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
838 * impossible to fix since SSL_pending cannot report errors that may be
839 * observed while scanning the new data. (Note that SSL_pending() is often
840 * used as a boolean value, so we'd better not return -1.). */
841 return s->method->ssl_pending(s);
842}
Adam Langley95c29f32014-06-20 12:00:00 -0700843
Adam Langleyfcf25832014-12-18 17:42:32 -0800844X509 *SSL_get_peer_certificate(const SSL *s) {
845 X509 *r;
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 if (s == NULL || s->session == NULL) {
848 r = NULL;
849 } else {
850 r = s->session->peer;
851 }
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 if (r == NULL) {
854 return NULL;
855 }
Adam Langley95c29f32014-06-20 12:00:00 -0700856
Adam Langleyfcf25832014-12-18 17:42:32 -0800857 return X509_up_ref(r);
858}
859
860STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
861 STACK_OF(X509) * r;
862
863 if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
864 r = NULL;
865 } else {
866 r = s->session->sess_cert->cert_chain;
867 }
868
869 /* If we are a client, cert_chain includes the peer's own certificate; if we
870 * are a server, it does not. */
871 return r;
872}
Adam Langley95c29f32014-06-20 12:00:00 -0700873
Adam Langley95c29f32014-06-20 12:00:00 -0700874/* Fix this so it checks all the valid key/cert options */
Adam Langleyfcf25832014-12-18 17:42:32 -0800875int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
876 if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
877 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
878 SSL_R_NO_CERTIFICATE_ASSIGNED);
879 return 0;
880 }
881
882 if (ctx->cert->key->privatekey == NULL) {
883 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
884 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
885 return 0;
886 }
887
888 return X509_check_private_key(ctx->cert->key->x509,
889 ctx->cert->key->privatekey);
890}
Adam Langley95c29f32014-06-20 12:00:00 -0700891
892/* Fix this function so that it takes an optional type parameter */
Adam Langleyfcf25832014-12-18 17:42:32 -0800893int SSL_check_private_key(const SSL *ssl) {
894 if (ssl == NULL) {
895 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
896 return 0;
897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 if (ssl->cert == NULL) {
900 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
901 SSL_R_NO_CERTIFICATE_ASSIGNED);
902 return 0;
903 }
Adam Langley95c29f32014-06-20 12:00:00 -0700904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 if (ssl->cert->key->x509 == NULL) {
906 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
907 SSL_R_NO_CERTIFICATE_ASSIGNED);
908 return 0;
909 }
David Benjamin0b145c22014-11-26 20:10:09 -0500910
Adam Langleyfcf25832014-12-18 17:42:32 -0800911 if (ssl->cert->key->privatekey == NULL) {
912 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
913 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
914 return 0;
915 }
Adam Langley95c29f32014-06-20 12:00:00 -0700916
Adam Langleyfcf25832014-12-18 17:42:32 -0800917 return X509_check_private_key(ssl->cert->key->x509,
918 ssl->cert->key->privatekey);
919}
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921int SSL_accept(SSL *s) {
922 if (s->handshake_func == 0) {
923 /* Not properly initialized yet */
924 SSL_set_accept_state(s);
925 }
David Benjamin0b145c22014-11-26 20:10:09 -0500926
Adam Langleyfcf25832014-12-18 17:42:32 -0800927 if (s->handshake_func != s->method->ssl_accept) {
928 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
929 return -1;
930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 return s->handshake_func(s);
933}
Adam Langley95c29f32014-06-20 12:00:00 -0700934
Adam Langleyfcf25832014-12-18 17:42:32 -0800935int SSL_connect(SSL *s) {
936 if (s->handshake_func == 0) {
937 /* Not properly initialized yet */
938 SSL_set_connect_state(s);
939 }
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 if (s->handshake_func != s->method->ssl_connect) {
942 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
943 return -1;
944 }
Adam Langley95c29f32014-06-20 12:00:00 -0700945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 return s->handshake_func(s);
947}
Adam Langley95c29f32014-06-20 12:00:00 -0700948
Adam Langleyfcf25832014-12-18 17:42:32 -0800949long SSL_get_default_timeout(const SSL *s) {
950 return SSL_DEFAULT_SESSION_TIMEOUT;
951}
Adam Langley95c29f32014-06-20 12:00:00 -0700952
Adam Langleyfcf25832014-12-18 17:42:32 -0800953int SSL_read(SSL *s, void *buf, int num) {
954 if (s->handshake_func == 0) {
955 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
956 return -1;
957 }
Adam Langley95c29f32014-06-20 12:00:00 -0700958
Adam Langleyfcf25832014-12-18 17:42:32 -0800959 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
960 s->rwstate = SSL_NOTHING;
961 return 0;
962 }
Adam Langley95c29f32014-06-20 12:00:00 -0700963
Adam Langleyfcf25832014-12-18 17:42:32 -0800964 return s->method->ssl_read(s, buf, num);
965}
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967int SSL_peek(SSL *s, void *buf, int num) {
968 if (s->handshake_func == 0) {
969 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
970 return -1;
971 }
Adam Langley95c29f32014-06-20 12:00:00 -0700972
Adam Langleyfcf25832014-12-18 17:42:32 -0800973 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
974 return 0;
975 }
Adam Langley95c29f32014-06-20 12:00:00 -0700976
Adam Langleyfcf25832014-12-18 17:42:32 -0800977 return s->method->ssl_peek(s, buf, num);
978}
Adam Langley95c29f32014-06-20 12:00:00 -0700979
Adam Langleyfcf25832014-12-18 17:42:32 -0800980int SSL_write(SSL *s, const void *buf, int num) {
981 if (s->handshake_func == 0) {
982 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
983 return -1;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langleyfcf25832014-12-18 17:42:32 -0800986 if (s->shutdown & SSL_SENT_SHUTDOWN) {
987 s->rwstate = SSL_NOTHING;
988 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
989 return -1;
990 }
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 return s->method->ssl_write(s, buf, num);
993}
Adam Langley95c29f32014-06-20 12:00:00 -0700994
Adam Langleyfcf25832014-12-18 17:42:32 -0800995int SSL_shutdown(SSL *s) {
996 /* Note that this function behaves differently from what one might expect.
997 * Return values are 0 for no success (yet), 1 for success; but calling it
998 * once is usually not enough, even if blocking I/O is used (see
999 * ssl3_shutdown). */
Adam Langley95c29f32014-06-20 12:00:00 -07001000
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 if (s->handshake_func == 0) {
1002 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1003 return -1;
1004 }
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 if (!SSL_in_init(s)) {
1007 return s->method->ssl_shutdown(s);
1008 }
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 return 1;
1011}
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013int SSL_renegotiate(SSL *s) {
1014 if (s->renegotiate == 0) {
1015 s->renegotiate = 1;
1016 }
Adam Langley95c29f32014-06-20 12:00:00 -07001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 s->new_session = 1;
1019 return s->method->ssl_renegotiate(s);
1020}
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langleyfcf25832014-12-18 17:42:32 -08001022int SSL_renegotiate_abbreviated(SSL *s) {
1023 if (s->renegotiate == 0) {
1024 s->renegotiate = 1;
1025 }
Adam Langley95c29f32014-06-20 12:00:00 -07001026
Adam Langleyfcf25832014-12-18 17:42:32 -08001027 s->new_session = 0;
1028 return s->method->ssl_renegotiate(s);
1029}
Adam Langley95c29f32014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031int SSL_renegotiate_pending(SSL *s) {
1032 /* becomes true when negotiation is requested; false again once a handshake
1033 * has finished */
1034 return s->renegotiate != 0;
1035}
Adam Langley95c29f32014-06-20 12:00:00 -07001036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
1038 long l;
Adam Langley95c29f32014-06-20 12:00:00 -07001039
Adam Langleyfcf25832014-12-18 17:42:32 -08001040 switch (cmd) {
1041 case SSL_CTRL_GET_READ_AHEAD:
1042 return s->read_ahead;
Adam Langley95c29f32014-06-20 12:00:00 -07001043
Adam Langleyfcf25832014-12-18 17:42:32 -08001044 case SSL_CTRL_SET_READ_AHEAD:
1045 l = s->read_ahead;
1046 s->read_ahead = larg;
1047 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001048
Adam Langleyfcf25832014-12-18 17:42:32 -08001049 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1050 s->msg_callback_arg = parg;
1051 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001052
Adam Langleyfcf25832014-12-18 17:42:32 -08001053 case SSL_CTRL_OPTIONS:
1054 return s->options |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001055
Adam Langleyfcf25832014-12-18 17:42:32 -08001056 case SSL_CTRL_CLEAR_OPTIONS:
1057 return s->options &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001058
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 case SSL_CTRL_MODE:
1060 return s->mode |= larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001061
Adam Langleyfcf25832014-12-18 17:42:32 -08001062 case SSL_CTRL_CLEAR_MODE:
1063 return s->mode &= ~larg;
Adam Langley95c29f32014-06-20 12:00:00 -07001064
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 case SSL_CTRL_GET_MAX_CERT_LIST:
1066 return s->max_cert_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001067
Adam Langleyfcf25832014-12-18 17:42:32 -08001068 case SSL_CTRL_SET_MAX_CERT_LIST:
1069 l = s->max_cert_list;
1070 s->max_cert_list = larg;
1071 return l;
Adam Langley95c29f32014-06-20 12:00:00 -07001072
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 case SSL_CTRL_SET_MTU:
1074 if (larg < (long)dtls1_min_mtu()) {
1075 return 0;
1076 }
1077 if (SSL_IS_DTLS(s)) {
1078 s->d1->mtu = larg;
1079 return larg;
1080 }
1081 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001082
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1084 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1085 return 0;
1086 }
1087 s->max_send_fragment = larg;
1088 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001089
Adam Langleyfcf25832014-12-18 17:42:32 -08001090 case SSL_CTRL_GET_RI_SUPPORT:
1091 if (s->s3) {
1092 return s->s3->send_connection_binding;
1093 }
1094 return 0;
1095
1096 case SSL_CTRL_CERT_FLAGS:
1097 return s->cert->cert_flags |= larg;
1098
1099 case SSL_CTRL_CLEAR_CERT_FLAGS:
1100 return s->cert->cert_flags &= ~larg;
1101
1102 case SSL_CTRL_GET_RAW_CIPHERLIST:
1103 if (parg) {
1104 if (s->cert->ciphers_raw == NULL) {
1105 return 0;
1106 }
1107 *(uint8_t **)parg = s->cert->ciphers_raw;
1108 return (int)s->cert->ciphers_rawlen;
1109 }
1110
1111 /* Passing a NULL |parg| returns the size of a single
1112 * cipher suite value. */
1113 return 2;
1114
1115 default:
1116 return s->method->ssl_ctrl(s, cmd, larg, parg);
1117 }
1118}
1119
1120long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
1121 switch (cmd) {
1122 case SSL_CTRL_SET_MSG_CALLBACK:
1123 s->msg_callback =
1124 (void (*)(int write_p, int version, int content_type, const void *buf,
1125 size_t len, SSL *ssl, void *arg))(fp);
1126 return 1;
1127
1128 default:
1129 return s->method->ssl_callback_ctrl(s, cmd, fp);
1130 }
1131}
1132
1133LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
1134
1135long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
1136 long l;
1137
1138 switch (cmd) {
1139 case SSL_CTRL_GET_READ_AHEAD:
1140 return ctx->read_ahead;
1141
1142 case SSL_CTRL_SET_READ_AHEAD:
1143 l = ctx->read_ahead;
1144 ctx->read_ahead = larg;
1145 return l;
1146
1147 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1148 ctx->msg_callback_arg = parg;
1149 return 1;
1150
1151 case SSL_CTRL_GET_MAX_CERT_LIST:
1152 return ctx->max_cert_list;
1153
1154 case SSL_CTRL_SET_MAX_CERT_LIST:
1155 l = ctx->max_cert_list;
1156 ctx->max_cert_list = larg;
1157 return l;
1158
1159 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1160 l = ctx->session_cache_size;
1161 ctx->session_cache_size = larg;
1162 return l;
1163
1164 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1165 return ctx->session_cache_size;
1166
1167 case SSL_CTRL_SET_SESS_CACHE_MODE:
1168 l = ctx->session_cache_mode;
1169 ctx->session_cache_mode = larg;
1170 return l;
1171
1172 case SSL_CTRL_GET_SESS_CACHE_MODE:
1173 return ctx->session_cache_mode;
1174
1175 case SSL_CTRL_SESS_NUMBER:
1176 return lh_SSL_SESSION_num_items(ctx->sessions);
1177
1178 case SSL_CTRL_SESS_CONNECT:
1179 return ctx->stats.sess_connect;
1180
1181 case SSL_CTRL_SESS_CONNECT_GOOD:
1182 return ctx->stats.sess_connect_good;
1183
1184 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1185 return ctx->stats.sess_connect_renegotiate;
1186
1187 case SSL_CTRL_SESS_ACCEPT:
1188 return ctx->stats.sess_accept;
1189
1190 case SSL_CTRL_SESS_ACCEPT_GOOD:
1191 return ctx->stats.sess_accept_good;
1192
1193 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1194 return ctx->stats.sess_accept_renegotiate;
1195
1196 case SSL_CTRL_SESS_HIT:
1197 return ctx->stats.sess_hit;
1198
1199 case SSL_CTRL_SESS_CB_HIT:
1200 return ctx->stats.sess_cb_hit;
1201
1202 case SSL_CTRL_SESS_MISSES:
1203 return ctx->stats.sess_miss;
1204
1205 case SSL_CTRL_SESS_TIMEOUTS:
1206 return ctx->stats.sess_timeout;
1207
1208 case SSL_CTRL_SESS_CACHE_FULL:
1209 return ctx->stats.sess_cache_full;
1210
1211 case SSL_CTRL_OPTIONS:
1212 return ctx->options |= larg;
1213
1214 case SSL_CTRL_CLEAR_OPTIONS:
1215 return ctx->options &= ~larg;
1216
1217 case SSL_CTRL_MODE:
1218 return ctx->mode |= larg;
1219
1220 case SSL_CTRL_CLEAR_MODE:
1221 return ctx->mode &= ~larg;
1222
1223 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1224 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
1225 return 0;
1226 }
1227 ctx->max_send_fragment = larg;
1228 return 1;
1229
1230 case SSL_CTRL_CERT_FLAGS:
1231 return ctx->cert->cert_flags |= larg;
1232
1233 case SSL_CTRL_CLEAR_CERT_FLAGS:
1234 return ctx->cert->cert_flags &= ~larg;
1235
1236 default:
1237 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
1238 }
1239}
1240
1241long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
1242 switch (cmd) {
1243 case SSL_CTRL_SET_MSG_CALLBACK:
1244 ctx->msg_callback =
1245 (void (*)(int write_p, int version, int content_type, const void *buf,
1246 size_t len, SSL *ssl, void *arg))(fp);
1247 return 1;
1248
1249 default:
1250 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
1251 }
1252}
1253
1254int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1255 long l;
1256 const SSL_CIPHER *a = in_a;
1257 const SSL_CIPHER *b = in_b;
1258 const long a_id = a->id;
1259 const long b_id = b->id;
1260
1261 l = a_id - b_id;
1262 if (l == 0L) {
1263 return 0;
1264 } else {
1265 return (l > 0) ? 1 : -1;
1266 }
1267}
1268
1269int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
1270 long l;
1271 const long a_id = (*ap)->id;
1272 const long b_id = (*bp)->id;
1273
1274 l = a_id - b_id;
1275 if (l == 0) {
1276 return 0;
1277 } else {
1278 return (l > 0) ? 1 : -1;
1279 }
1280}
1281
1282/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001283 * preference */
Adam Langleyfcf25832014-12-18 17:42:32 -08001284STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
1285 if (s == NULL) {
1286 return NULL;
1287 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001288
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 if (s->cipher_list != NULL) {
1290 return s->cipher_list->ciphers;
1291 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001292
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
1294 s->ctx->cipher_list_tls11 != NULL) {
1295 return s->ctx->cipher_list_tls11->ciphers;
1296 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001297
Adam Langleyfcf25832014-12-18 17:42:32 -08001298 if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
1299 return s->ctx->cipher_list->ciphers;
1300 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001301
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 return NULL;
1303}
Adam Langley95c29f32014-06-20 12:00:00 -07001304
Adam Langleyfcf25832014-12-18 17:42:32 -08001305/* return a STACK of the ciphers available for the SSL and in order of
Adam Langley95c29f32014-06-20 12:00:00 -07001306 * algorithm id */
Adam Langleyfcf25832014-12-18 17:42:32 -08001307STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
1308 if (s == NULL) {
1309 return NULL;
1310 }
Adam Langley95c29f32014-06-20 12:00:00 -07001311
Adam Langleyfcf25832014-12-18 17:42:32 -08001312 if (s->cipher_list_by_id != NULL) {
1313 return s->cipher_list_by_id;
1314 }
Adam Langley95c29f32014-06-20 12:00:00 -07001315
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
1317 return s->ctx->cipher_list_by_id;
1318 }
Adam Langley95c29f32014-06-20 12:00:00 -07001319
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 return NULL;
1321}
Adam Langley95c29f32014-06-20 12:00:00 -07001322
Adam Langleyfcf25832014-12-18 17:42:32 -08001323/* The old interface to get the same thing as SSL_get_ciphers() */
1324const char *SSL_get_cipher_list(const SSL *s, int n) {
1325 const SSL_CIPHER *c;
1326 STACK_OF(SSL_CIPHER) * sk;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001327
Adam Langleyfcf25832014-12-18 17:42:32 -08001328 if (s == NULL) {
1329 return NULL;
1330 }
Adam Langley95c29f32014-06-20 12:00:00 -07001331
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 sk = SSL_get_ciphers(s);
1333 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
1334 return NULL;
1335 }
Adam Langley95c29f32014-06-20 12:00:00 -07001336
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 c = sk_SSL_CIPHER_value(sk, n);
1338 if (c == NULL) {
1339 return NULL;
1340 }
Adam Langley95c29f32014-06-20 12:00:00 -07001341
Adam Langleyfcf25832014-12-18 17:42:32 -08001342 return c->name;
1343}
David Benjamin5491e3f2014-09-29 19:33:09 -04001344
Adam Langleyfcf25832014-12-18 17:42:32 -08001345/* specify the ciphers to be used by default by the SSL_CTX */
1346int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
1347 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001348
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1350 &ctx->cipher_list_by_id, str, ctx->cert);
1351 /* ssl_create_cipher_list may return an empty stack if it was unable to find
1352 * a cipher matching the given rule string (for example if the rule string
1353 * specifies a cipher which has been disabled). This is not an error as far
1354 * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
1355 * ctx->cipher_list_by_id has been updated. */
1356 if (sk == NULL) {
1357 return 0;
1358 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1359 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1360 return 0;
1361 }
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 return 1;
1364}
David Benjamin39482a12014-07-20 13:30:15 -04001365
Adam Langleyfcf25832014-12-18 17:42:32 -08001366int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
1367 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001368
Adam Langleyfcf25832014-12-18 17:42:32 -08001369 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str,
1370 ctx->cert);
1371 if (sk == NULL) {
1372 return 0;
1373 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1374 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
1375 SSL_R_NO_CIPHER_MATCH);
1376 return 0;
1377 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001378
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 return 1;
1380}
Adam Langley95c29f32014-06-20 12:00:00 -07001381
Adam Langleyfcf25832014-12-18 17:42:32 -08001382/* specify the ciphers to be used by the SSL */
1383int SSL_set_cipher_list(SSL *s, const char *str) {
1384 STACK_OF(SSL_CIPHER) *sk;
Adam Langley95c29f32014-06-20 12:00:00 -07001385
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1387 &s->cipher_list_by_id, str, s->cert);
David Benjamin39482a12014-07-20 13:30:15 -04001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 /* see comment in SSL_CTX_set_cipher_list */
1390 if (sk == NULL) {
1391 return 0;
1392 } else if (sk_SSL_CIPHER_num(sk) == 0) {
1393 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1394 return 0;
1395 }
David Benjamin39482a12014-07-20 13:30:15 -04001396
Adam Langleyfcf25832014-12-18 17:42:32 -08001397 return 1;
1398}
Adam Langley95c29f32014-06-20 12:00:00 -07001399
Adam Langleyfcf25832014-12-18 17:42:32 -08001400int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
1401 size_t i;
1402 const SSL_CIPHER *c;
1403 CERT *ct = s->cert;
1404 uint8_t *q;
1405 /* Set disabled masks for this session */
1406 ssl_set_client_disabled(s);
Adam Langley29707792014-06-20 12:00:00 -07001407
Adam Langleyfcf25832014-12-18 17:42:32 -08001408 if (sk == NULL) {
1409 return 0;
1410 }
1411 q = p;
Adam Langley95c29f32014-06-20 12:00:00 -07001412
Adam Langleyfcf25832014-12-18 17:42:32 -08001413 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1414 c = sk_SSL_CIPHER_value(sk, i);
1415 /* Skip disabled ciphers */
1416 if (c->algorithm_ssl & ct->mask_ssl ||
1417 c->algorithm_mkey & ct->mask_k ||
1418 c->algorithm_auth & ct->mask_a) {
1419 continue;
1420 }
1421 s2n(ssl3_get_cipher_value(c), p);
1422 }
1423
1424 /* If all ciphers were disabled, return the error to the caller. */
1425 if (p == q) {
1426 return 0;
1427 }
1428
1429 /* Add SCSVs. */
1430 if (!s->renegotiate) {
1431 s2n(SSL3_CK_SCSV & 0xffff, p);
1432 }
1433
1434 if (s->fallback_scsv) {
1435 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
1436 }
1437
1438 return p - q;
1439}
1440
1441STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
1442 CBS cipher_suites = *cbs;
1443 const SSL_CIPHER *c;
1444 STACK_OF(SSL_CIPHER) * sk;
1445
1446 if (s->s3) {
1447 s->s3->send_connection_binding = 0;
1448 }
1449
1450 if (CBS_len(&cipher_suites) % 2 != 0) {
1451 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1452 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1453 return NULL;
1454 }
1455
1456 sk = sk_SSL_CIPHER_new_null();
1457 if (sk == NULL) {
1458 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1459 goto err;
1460 }
1461
1462 if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
1463 &s->cert->ciphers_rawlen)) {
1464 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1465 goto err;
1466 }
1467
1468 while (CBS_len(&cipher_suites) > 0) {
1469 uint16_t cipher_suite;
1470
1471 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1472 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1473 goto err;
1474 }
1475
1476 /* Check for SCSV. */
1477 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1478 /* SCSV is fatal if renegotiating. */
1479 if (s->renegotiate) {
1480 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1481 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1482 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1483 goto err;
1484 }
1485 s->s3->send_connection_binding = 1;
1486 continue;
1487 }
1488
1489 /* Check for FALLBACK_SCSV. */
1490 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1491 uint16_t max_version = ssl3_get_max_server_version(s);
1492 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1493 : (uint16_t)s->version < max_version) {
1494 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
1495 SSL_R_INAPPROPRIATE_FALLBACK);
1496 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1497 goto err;
1498 }
1499 continue;
1500 }
1501
1502 c = ssl3_get_cipher_by_value(cipher_suite);
1503 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1504 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1505 goto err;
1506 }
1507 }
1508
1509 return sk;
David Benjamin9f2c0d72014-10-21 22:00:19 -04001510
Adam Langley95c29f32014-06-20 12:00:00 -07001511err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001512 if (sk != NULL) {
1513 sk_SSL_CIPHER_free(sk);
1514 }
1515 return NULL;
1516}
Adam Langley95c29f32014-06-20 12:00:00 -07001517
1518
Adam Langleyfcf25832014-12-18 17:42:32 -08001519/* return a servername extension value if provided in Client Hello, or NULL. So
1520 * far, only host_name types are defined (RFC 3546). */
1521const char *SSL_get_servername(const SSL *s, const int type) {
1522 if (type != TLSEXT_NAMETYPE_host_name) {
1523 return NULL;
1524 }
Adam Langley95c29f32014-06-20 12:00:00 -07001525
Adam Langleyfcf25832014-12-18 17:42:32 -08001526 return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
1527 : s->tlsext_hostname;
1528}
Adam Langley95c29f32014-06-20 12:00:00 -07001529
Adam Langleyfcf25832014-12-18 17:42:32 -08001530int SSL_get_servername_type(const SSL *s) {
1531 if (s->session &&
1532 (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
1533 return TLSEXT_NAMETYPE_host_name;
1534 }
Adam Langley95c29f32014-06-20 12:00:00 -07001535
Adam Langleyfcf25832014-12-18 17:42:32 -08001536 return -1;
1537}
Adam Langley95c29f32014-06-20 12:00:00 -07001538
Adam Langleyfcf25832014-12-18 17:42:32 -08001539void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
1540 ctx->signed_cert_timestamps_enabled = 1;
1541}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001542
Adam Langleyfcf25832014-12-18 17:42:32 -08001543int SSL_enable_signed_cert_timestamps(SSL *ssl) {
1544 ssl->signed_cert_timestamps_enabled = 1;
1545 return 1;
1546}
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001547
Adam Langleyfcf25832014-12-18 17:42:32 -08001548void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
1549 ctx->ocsp_stapling_enabled = 1;
1550}
David Benjamin6c7aed02014-08-27 16:42:38 -04001551
Adam Langleyfcf25832014-12-18 17:42:32 -08001552int SSL_enable_ocsp_stapling(SSL *ssl) {
1553 ssl->ocsp_stapling_enabled = 1;
1554 return 1;
1555}
David Benjamin6c7aed02014-08-27 16:42:38 -04001556
Adam Langleyfcf25832014-12-18 17:42:32 -08001557void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1558 size_t *out_len) {
1559 SSL_SESSION *session = ssl->session;
Adam Langley3cb50e02014-08-26 14:00:31 -07001560
Adam Langleyfcf25832014-12-18 17:42:32 -08001561 *out_len = 0;
1562 *out = NULL;
1563 if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
1564 return;
1565 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001566
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 *out = session->tlsext_signed_cert_timestamp_list;
1568 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1569}
David Benjamin6c7aed02014-08-27 16:42:38 -04001570
Adam Langleyfcf25832014-12-18 17:42:32 -08001571void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1572 size_t *out_len) {
1573 SSL_SESSION *session = ssl->session;
1574
1575 *out_len = 0;
1576 *out = NULL;
1577 if (ssl->server || !session || !session->ocsp_response) {
1578 return;
1579 }
1580 *out = session->ocsp_response;
1581 *out_len = session->ocsp_response_length;
1582}
David Benjamin6c7aed02014-08-27 16:42:38 -04001583
Adam Langley95c29f32014-06-20 12:00:00 -07001584/* SSL_select_next_proto implements the standard protocol selection. It is
1585 * expected that this function is called from the callback set by
1586 * SSL_CTX_set_next_proto_select_cb.
1587 *
1588 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1589 * strings. The length byte itself is not included in the length. A byte
1590 * string of length 0 is invalid. No byte string may be truncated.
1591 *
1592 * The current, but experimental algorithm for selecting the protocol is:
1593 *
1594 * 1) If the server doesn't support NPN then this is indicated to the
1595 * callback. In this case, the client application has to abort the connection
1596 * or have a default application level protocol.
1597 *
1598 * 2) If the server supports NPN, but advertises an empty list then the
1599 * client selects the first protcol in its list, but indicates via the
1600 * API that this fallback case was enacted.
1601 *
1602 * 3) Otherwise, the client finds the first protocol in the server's list
1603 * that it supports and selects this protocol. This is because it's
1604 * assumed that the server has better information about which protocol
1605 * a client should use.
1606 *
1607 * 4) If the client doesn't support any of the server's advertised
1608 * protocols, then this is treated the same as case 2.
1609 *
1610 * It returns either
1611 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1612 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1613 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001614int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
1615 unsigned int server_len, const uint8_t *client,
1616 unsigned int client_len) {
1617 unsigned int i, j;
1618 const uint8_t *result;
1619 int status = OPENSSL_NPN_UNSUPPORTED;
Adam Langley95c29f32014-06-20 12:00:00 -07001620
Adam Langleyfcf25832014-12-18 17:42:32 -08001621 /* For each protocol in server preference order, see if we support it. */
1622 for (i = 0; i < server_len;) {
1623 for (j = 0; j < client_len;) {
1624 if (server[i] == client[j] &&
1625 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1626 /* We found a match */
1627 result = &server[i];
1628 status = OPENSSL_NPN_NEGOTIATED;
1629 goto found;
1630 }
1631 j += client[j];
1632 j++;
1633 }
1634 i += server[i];
1635 i++;
1636 }
Adam Langley95c29f32014-06-20 12:00:00 -07001637
Adam Langleyfcf25832014-12-18 17:42:32 -08001638 /* There's no overlap between our protocols and the server's list. */
1639 result = client;
1640 status = OPENSSL_NPN_NO_OVERLAP;
Adam Langley95c29f32014-06-20 12:00:00 -07001641
Adam Langleyfcf25832014-12-18 17:42:32 -08001642found:
1643 *out = (uint8_t *)result + 1;
1644 *outlen = result[0];
1645 return status;
1646}
Adam Langley95c29f32014-06-20 12:00:00 -07001647
Adam Langley95c29f32014-06-20 12:00:00 -07001648/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1649 * requested protocol for this connection and returns 0. If the client didn't
1650 * request any protocol, then *data is set to NULL.
1651 *
1652 * Note that the client can request any protocol it chooses. The value returned
1653 * from this function need not be a member of the list of supported protocols
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 * provided by the callback. */
1655void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
1656 unsigned *len) {
1657 *data = s->next_proto_negotiated;
1658 if (!*data) {
1659 *len = 0;
1660 } else {
1661 *len = s->next_proto_negotiated_len;
1662 }
Adam Langley95c29f32014-06-20 12:00:00 -07001663}
1664
1665/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1666 * TLS server needs a list of supported protocols for Next Protocol
1667 * Negotiation. The returned list must be in wire format. The list is returned
1668 * by setting |out| to point to it and |outlen| to its length. This memory will
1669 * not be modified, but one should assume that the SSL* keeps a reference to
1670 * it.
1671 *
Adam Langleyfcf25832014-12-18 17:42:32 -08001672 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
1673 * Otherwise, no such extension will be included in the ServerHello. */
1674void SSL_CTX_set_next_protos_advertised_cb(
1675 SSL_CTX *ctx,
1676 int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
1677 void *arg) {
1678 ctx->next_protos_advertised_cb = cb;
1679 ctx->next_protos_advertised_cb_arg = arg;
1680}
Adam Langley95c29f32014-06-20 12:00:00 -07001681
1682/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1683 * client needs to select a protocol from the server's provided list. |out|
1684 * must be set to point to the selected protocol (which may be within |in|).
1685 * The length of the protocol name must be written into |outlen|. The server's
1686 * advertised protocols are provided in |in| and |inlen|. The callback can
1687 * assume that |in| is syntactically valid.
1688 *
1689 * The client must select a protocol. It is fatal to the connection if this
1690 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1691 */
Adam Langleyfcf25832014-12-18 17:42:32 -08001692void SSL_CTX_set_next_proto_select_cb(
1693 SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
1694 const uint8_t *in, unsigned int inlen, void *arg),
1695 void *arg) {
1696 ctx->next_proto_select_cb = cb;
1697 ctx->next_proto_select_cb_arg = arg;
1698}
Adam Langley95c29f32014-06-20 12:00:00 -07001699
Adam Langley95c29f32014-06-20 12:00:00 -07001700/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1701 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1702 * length-prefixed strings).
1703 *
1704 * Returns 0 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001705int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
1706 unsigned protos_len) {
1707 if (ctx->alpn_client_proto_list) {
1708 OPENSSL_free(ctx->alpn_client_proto_list);
1709 }
Adam Langley95c29f32014-06-20 12:00:00 -07001710
Adam Langleyfcf25832014-12-18 17:42:32 -08001711 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1712 if (!ctx->alpn_client_proto_list) {
1713 return 1;
1714 }
1715 ctx->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001716
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 return 0;
1718}
Adam Langley95c29f32014-06-20 12:00:00 -07001719
1720/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1721 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1722 * length-prefixed strings).
1723 *
1724 * Returns 0 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001725int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
1726 if (ssl->alpn_client_proto_list) {
1727 OPENSSL_free(ssl->alpn_client_proto_list);
1728 }
Adam Langley95c29f32014-06-20 12:00:00 -07001729
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1731 if (!ssl->alpn_client_proto_list) {
1732 return 1;
1733 }
1734 ssl->alpn_client_proto_list_len = protos_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001735
Adam Langleyfcf25832014-12-18 17:42:32 -08001736 return 0;
1737}
Adam Langley95c29f32014-06-20 12:00:00 -07001738
1739/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1740 * during ClientHello processing in order to select an ALPN protocol from the
1741 * client's list of offered protocols. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001742void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1743 int (*cb)(SSL *ssl, const uint8_t **out,
1744 uint8_t *outlen, const uint8_t *in,
1745 unsigned int inlen, void *arg),
1746 void *arg) {
1747 ctx->alpn_select_cb = cb;
1748 ctx->alpn_select_cb_arg = arg;
1749}
Adam Langley95c29f32014-06-20 12:00:00 -07001750
1751/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1752 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1753 * including the leading length-prefix byte). If the server didn't respond with
1754 * a negotiated protocol then |*len| will be zero. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001755void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
1756 unsigned *len) {
1757 *data = NULL;
1758 if (ssl->s3) {
1759 *data = ssl->s3->alpn_selected;
1760 }
1761 if (*data == NULL) {
1762 *len = 0;
1763 } else {
1764 *len = ssl->s3->alpn_selected_len;
1765 }
1766}
Adam Langley95c29f32014-06-20 12:00:00 -07001767
Adam Langleyfcf25832014-12-18 17:42:32 -08001768int SSL_export_keying_material(SSL *s, uint8_t *out, size_t olen,
1769 const char *label, size_t llen, const uint8_t *p,
1770 size_t plen, int use_context) {
1771 if (s->version < TLS1_VERSION) {
1772 return -1;
1773 }
Adam Langley95c29f32014-06-20 12:00:00 -07001774
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 return s->enc_method->export_keying_material(s, out, olen, label, llen, p,
1776 plen, use_context);
1777}
Adam Langley95c29f32014-06-20 12:00:00 -07001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779static uint32_t ssl_session_hash(const SSL_SESSION *a) {
1780 uint32_t hash =
1781 ((uint32_t)a->session_id[0]) ||
1782 ((uint32_t)a->session_id[1] << 8) ||
1783 ((uint32_t)a->session_id[2] << 16) ||
1784 ((uint32_t)a->session_id[3] << 24);
Adam Langley95c29f32014-06-20 12:00:00 -07001785
Adam Langleyfcf25832014-12-18 17:42:32 -08001786 return hash;
1787}
Adam Langley95c29f32014-06-20 12:00:00 -07001788
1789/* NB: If this function (or indeed the hash function which uses a sort of
1790 * coarser function than this one) is changed, ensure
1791 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1792 * able to construct an SSL_SESSION that will collide with any existing session
1793 * with a matching session ID. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001794static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
1795 if (a->ssl_version != b->ssl_version) {
1796 return 1;
1797 }
Adam Langley95c29f32014-06-20 12:00:00 -07001798
Adam Langleyfcf25832014-12-18 17:42:32 -08001799 if (a->session_id_length != b->session_id_length) {
1800 return 1;
1801 }
Adam Langley95c29f32014-06-20 12:00:00 -07001802
Adam Langleyfcf25832014-12-18 17:42:32 -08001803 return memcmp(a->session_id, b->session_id, a->session_id_length);
1804}
Adam Langley95c29f32014-06-20 12:00:00 -07001805
Adam Langleyfcf25832014-12-18 17:42:32 -08001806SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
1807 SSL_CTX *ret = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001808
Adam Langleyfcf25832014-12-18 17:42:32 -08001809 if (meth == NULL) {
1810 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1811 return NULL;
1812 }
Adam Langley95c29f32014-06-20 12:00:00 -07001813
Adam Langleyfcf25832014-12-18 17:42:32 -08001814 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1815 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1816 goto err;
1817 }
Adam Langley95c29f32014-06-20 12:00:00 -07001818
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1820 if (ret == NULL) {
1821 goto err;
1822 }
Adam Langley95c29f32014-06-20 12:00:00 -07001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824 memset(ret, 0, sizeof(SSL_CTX));
Adam Langley95c29f32014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001827
Adam Langleyfcf25832014-12-18 17:42:32 -08001828 ret->cert_store = NULL;
1829 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1830 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1831 ret->session_cache_head = NULL;
1832 ret->session_cache_tail = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 /* We take the system default */
1835 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001836
Adam Langleyfcf25832014-12-18 17:42:32 -08001837 ret->new_session_cb = 0;
1838 ret->remove_session_cb = 0;
1839 ret->get_session_cb = 0;
1840 ret->generate_session_id = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001841
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 memset((char *)&ret->stats, 0, sizeof(ret->stats));
Adam Langley95c29f32014-06-20 12:00:00 -07001843
Adam Langleyfcf25832014-12-18 17:42:32 -08001844 ret->references = 1;
1845 ret->quiet_shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001846
Adam Langleyfcf25832014-12-18 17:42:32 -08001847 ret->info_callback = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001848
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 ret->app_verify_callback = 0;
1850 ret->app_verify_arg = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1853 ret->read_ahead = 0;
1854 ret->msg_callback = 0;
1855 ret->msg_callback_arg = NULL;
1856 ret->verify_mode = SSL_VERIFY_NONE;
1857 ret->sid_ctx_length = 0;
1858 ret->default_verify_callback = NULL;
1859 ret->cert = ssl_cert_new();
1860 if (ret->cert == NULL) {
1861 goto err;
1862 }
Adam Langley95c29f32014-06-20 12:00:00 -07001863
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 ret->default_passwd_callback = 0;
1865 ret->default_passwd_callback_userdata = NULL;
1866 ret->client_cert_cb = 0;
1867 ret->app_gen_cookie_cb = 0;
1868 ret->app_verify_cookie_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001869
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1871 if (ret->sessions == NULL) {
1872 goto err;
1873 }
1874 ret->cert_store = X509_STORE_new();
1875 if (ret->cert_store == NULL) {
1876 goto err;
1877 }
Adam Langley95c29f32014-06-20 12:00:00 -07001878
Adam Langleyfcf25832014-12-18 17:42:32 -08001879 ssl_create_cipher_list(ret->method, &ret->cipher_list,
1880 &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST,
1881 ret->cert);
1882 if (ret->cipher_list == NULL ||
1883 sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
1884 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1885 goto err2;
1886 }
Adam Langley95c29f32014-06-20 12:00:00 -07001887
Adam Langleyfcf25832014-12-18 17:42:32 -08001888 ret->param = X509_VERIFY_PARAM_new();
1889 if (!ret->param) {
1890 goto err;
1891 }
Adam Langley95c29f32014-06-20 12:00:00 -07001892
Adam Langleyfcf25832014-12-18 17:42:32 -08001893 ret->client_CA = sk_X509_NAME_new_null();
1894 if (ret->client_CA == NULL) {
1895 goto err;
1896 }
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001899
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 ret->extra_certs = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001901
Adam Langleyfcf25832014-12-18 17:42:32 -08001902 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
David Benjamin422d3a42014-08-20 11:09:03 -04001903
Adam Langleyfcf25832014-12-18 17:42:32 -08001904 ret->tlsext_servername_callback = 0;
1905 ret->tlsext_servername_arg = NULL;
1906 /* Setup RFC4507 ticket keys */
1907 if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
1908 !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
1909 !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
1910 ret->options |= SSL_OP_NO_TICKET;
1911 }
Adam Langley95c29f32014-06-20 12:00:00 -07001912
Adam Langleyfcf25832014-12-18 17:42:32 -08001913 ret->tlsext_status_cb = 0;
1914 ret->tlsext_status_arg = NULL;
David Benjamin82c9e902014-12-12 15:55:27 -05001915
Adam Langleyfcf25832014-12-18 17:42:32 -08001916 ret->next_protos_advertised_cb = 0;
1917 ret->next_proto_select_cb = 0;
1918 ret->psk_identity_hint = NULL;
1919 ret->psk_client_callback = NULL;
1920 ret->psk_server_callback = NULL;
1921
1922 /* Default is to connect to non-RI servers. When RI is more widely deployed
1923 * might change this. */
1924 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1925
1926 /* Lock the SSL_CTX to the specified version, for compatibility with legacy
1927 * uses of SSL_METHOD. */
1928 if (meth->version != 0) {
1929 SSL_CTX_set_max_version(ret, meth->version);
1930 SSL_CTX_set_min_version(ret, meth->version);
1931 }
1932
1933 return ret;
1934
Adam Langley95c29f32014-06-20 12:00:00 -07001935err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001936 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -07001937err2:
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 if (ret != NULL) {
1939 SSL_CTX_free(ret);
1940 }
1941 return NULL;
1942}
Adam Langley95c29f32014-06-20 12:00:00 -07001943
Adam Langleyfcf25832014-12-18 17:42:32 -08001944void SSL_CTX_free(SSL_CTX *a) {
1945 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001946
Adam Langleyfcf25832014-12-18 17:42:32 -08001947 if (a == NULL) {
1948 return;
1949 }
Adam Langley95c29f32014-06-20 12:00:00 -07001950
Adam Langleyfcf25832014-12-18 17:42:32 -08001951 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1952 if (i > 0) {
1953 return;
1954 }
Adam Langley95c29f32014-06-20 12:00:00 -07001955
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 if (a->param) {
1957 X509_VERIFY_PARAM_free(a->param);
1958 }
Adam Langley95c29f32014-06-20 12:00:00 -07001959
Adam Langleyfcf25832014-12-18 17:42:32 -08001960 /* Free internal session cache. However: the remove_cb() may reference the
1961 * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
1962 * sessions were flushed. As the ex_data handling routines might also touch
1963 * the session cache, the most secure solution seems to be: empty (flush) the
1964 * cache, then free ex_data, then finally free the cache. (See ticket
1965 * [openssl.org #212].) */
1966 if (a->sessions != NULL) {
1967 SSL_CTX_flush_sessions(a, 0);
1968 }
Adam Langley95c29f32014-06-20 12:00:00 -07001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -07001971
Adam Langleyfcf25832014-12-18 17:42:32 -08001972 if (a->sessions != NULL) {
1973 lh_SSL_SESSION_free(a->sessions);
1974 }
1975 if (a->cert_store != NULL) {
1976 X509_STORE_free(a->cert_store);
1977 }
1978 if (a->cipher_list != NULL) {
1979 ssl_cipher_preference_list_free(a->cipher_list);
1980 }
1981 if (a->cipher_list_by_id != NULL) {
1982 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1983 }
1984 if (a->cipher_list_tls11 != NULL) {
1985 ssl_cipher_preference_list_free(a->cipher_list_tls11);
1986 }
1987 if (a->cert != NULL) {
1988 ssl_cert_free(a->cert);
1989 }
1990 if (a->client_CA != NULL) {
1991 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1992 }
1993 if (a->extra_certs != NULL) {
1994 sk_X509_pop_free(a->extra_certs, X509_free);
1995 }
1996 if (a->srtp_profiles) {
1997 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1998 }
1999 if (a->psk_identity_hint) {
2000 OPENSSL_free(a->psk_identity_hint);
2001 }
2002 if (a->tlsext_ecpointformatlist) {
2003 OPENSSL_free(a->tlsext_ecpointformatlist);
2004 }
2005 if (a->tlsext_ellipticcurvelist) {
2006 OPENSSL_free(a->tlsext_ellipticcurvelist);
2007 }
2008 if (a->alpn_client_proto_list != NULL) {
2009 OPENSSL_free(a->alpn_client_proto_list);
2010 }
2011 if (a->tlsext_channel_id_private) {
2012 EVP_PKEY_free(a->tlsext_channel_id_private);
2013 }
2014 if (a->keylog_bio) {
2015 BIO_free(a->keylog_bio);
2016 }
Adam Langley95c29f32014-06-20 12:00:00 -07002017
Adam Langleyfcf25832014-12-18 17:42:32 -08002018 OPENSSL_free(a);
2019}
Adam Langley95c29f32014-06-20 12:00:00 -07002020
Adam Langleyfcf25832014-12-18 17:42:32 -08002021void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
2022 ctx->default_passwd_callback = cb;
2023}
Adam Langley95c29f32014-06-20 12:00:00 -07002024
Adam Langleyfcf25832014-12-18 17:42:32 -08002025void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
2026 ctx->default_passwd_callback_userdata = u;
2027}
Adam Langley95c29f32014-06-20 12:00:00 -07002028
Adam Langleyfcf25832014-12-18 17:42:32 -08002029void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
2030 int (*cb)(X509_STORE_CTX *, void *),
2031 void *arg) {
2032 ctx->app_verify_callback = cb;
2033 ctx->app_verify_arg = arg;
2034}
Adam Langley95c29f32014-06-20 12:00:00 -07002035
Adam Langleyfcf25832014-12-18 17:42:32 -08002036void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
2037 int (*cb)(int, X509_STORE_CTX *)) {
2038 ctx->verify_mode = mode;
2039 ctx->default_verify_callback = cb;
2040}
Adam Langley95c29f32014-06-20 12:00:00 -07002041
Adam Langleyfcf25832014-12-18 17:42:32 -08002042void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
2043 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2044}
Adam Langley1258b6a2014-06-20 12:00:00 -07002045
Adam Langleyfcf25832014-12-18 17:42:32 -08002046void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
2047 void *arg) {
2048 ssl_cert_set_cert_cb(c->cert, cb, arg);
2049}
David Benjamin859ec3c2014-09-02 16:29:36 -04002050
Adam Langleyfcf25832014-12-18 17:42:32 -08002051void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
2052 ssl_cert_set_cert_cb(s->cert, cb, arg);
2053}
Adam Langley95c29f32014-06-20 12:00:00 -07002054
Adam Langleyfcf25832014-12-18 17:42:32 -08002055static int ssl_has_key(SSL *s, size_t idx) {
2056 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2057 return cpk->x509 && cpk->privatekey;
2058}
David Benjamin033e5f42014-11-13 18:47:41 -05002059
David Benjaminf31e6812014-11-13 18:05:55 -05002060void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
Adam Langleyfcf25832014-12-18 17:42:32 -08002061 unsigned long *out_mask_a) {
2062 CERT *c = s->cert;
2063 int rsa_enc, rsa_sign, dh_tmp;
2064 unsigned long mask_k, mask_a;
2065 int have_ecc_cert, ecdsa_ok;
2066 int have_ecdh_tmp;
2067 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002068
Adam Langleyfcf25832014-12-18 17:42:32 -08002069 if (c == NULL) {
2070 /* TODO(davidben): Is this codepath possible? */
2071 *out_mask_k = 0;
2072 *out_mask_a = 0;
2073 return;
2074 }
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langleyfcf25832014-12-18 17:42:32 -08002076 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002077
Adam Langleyfcf25832014-12-18 17:42:32 -08002078 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2079 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2080 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2081 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
2082 mask_k = 0;
2083 mask_a = 0;
David Benjaminf31e6812014-11-13 18:05:55 -05002084
Adam Langleyfcf25832014-12-18 17:42:32 -08002085 if (rsa_enc) {
2086 mask_k |= SSL_kRSA;
2087 }
2088 if (dh_tmp) {
2089 mask_k |= SSL_kEDH;
2090 }
2091 if (rsa_enc || rsa_sign) {
2092 mask_a |= SSL_aRSA;
2093 }
Adam Langley95c29f32014-06-20 12:00:00 -07002094
Adam Langleyfcf25832014-12-18 17:42:32 -08002095 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002096
Adam Langleyfcf25832014-12-18 17:42:32 -08002097 /* An ECC certificate may be usable for ECDSA cipher suites depending on the
2098 * key usage extension and on the client's curve preferences. */
2099 if (have_ecc_cert) {
2100 x = c->pkeys[SSL_PKEY_ECC].x509;
2101 /* This call populates extension flags (ex_flags). */
2102 X509_check_purpose(x, -1, 0);
2103 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2104 ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2105 : 1;
2106 if (!tls1_check_ec_cert(s, x)) {
2107 ecdsa_ok = 0;
2108 }
2109 if (ecdsa_ok) {
2110 mask_a |= SSL_aECDSA;
2111 }
2112 }
Adam Langley95c29f32014-06-20 12:00:00 -07002113
Adam Langleyfcf25832014-12-18 17:42:32 -08002114 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2115 * key, check it. */
2116 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) {
2117 mask_k |= SSL_kEECDH;
2118 }
Adam Langley95c29f32014-06-20 12:00:00 -07002119
Adam Langleyfcf25832014-12-18 17:42:32 -08002120 /* PSK requires a server callback. */
2121 if (s->psk_server_callback != NULL) {
2122 mask_k |= SSL_kPSK;
2123 mask_a |= SSL_aPSK;
2124 }
Adam Langley95c29f32014-06-20 12:00:00 -07002125
Adam Langleyfcf25832014-12-18 17:42:32 -08002126 *out_mask_k = mask_k;
2127 *out_mask_a = mask_a;
2128}
Adam Langley95c29f32014-06-20 12:00:00 -07002129
2130/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2131#define ku_reject(x, usage) \
Adam Langleyfcf25832014-12-18 17:42:32 -08002132 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
Adam Langley95c29f32014-06-20 12:00:00 -07002133
Adam Langleyfcf25832014-12-18 17:42:32 -08002134int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
2135 unsigned long alg_a;
2136 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2137 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langleyfcf25832014-12-18 17:42:32 -08002139 alg_a = cs->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07002140
Adam Langleyfcf25832014-12-18 17:42:32 -08002141 /* This call populates the ex_flags field correctly */
2142 X509_check_purpose(x, -1, 0);
2143 if (x->sig_alg && x->sig_alg->algorithm) {
2144 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2145 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2146 }
2147 if (alg_a & SSL_aECDSA) {
2148 /* key usage, if present, must allow signing */
2149 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2150 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
2151 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2152 return 0;
2153 }
2154 }
Adam Langley95c29f32014-06-20 12:00:00 -07002155
Adam Langleyfcf25832014-12-18 17:42:32 -08002156 return 1; /* all checks are ok */
2157}
Adam Langley95c29f32014-06-20 12:00:00 -07002158
Adam Langleyfcf25832014-12-18 17:42:32 -08002159static int ssl_get_server_cert_index(const SSL *s) {
2160 int idx;
2161 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2162 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
2163 idx = SSL_PKEY_RSA_SIGN;
2164 }
2165 if (idx == -1) {
2166 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2167 }
2168 return idx;
2169}
Adam Langley95c29f32014-06-20 12:00:00 -07002170
Adam Langleyfcf25832014-12-18 17:42:32 -08002171CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
2172 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002173
Adam Langleyfcf25832014-12-18 17:42:32 -08002174 /* This may or may not be an error. */
2175 if (i < 0) {
2176 return NULL;
2177 }
Adam Langley95c29f32014-06-20 12:00:00 -07002178
Adam Langleyfcf25832014-12-18 17:42:32 -08002179 /* May be NULL. */
2180 return &s->cert->pkeys[i];
2181}
Adam Langley95c29f32014-06-20 12:00:00 -07002182
Adam Langleyfcf25832014-12-18 17:42:32 -08002183EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
2184 unsigned long alg_a;
2185 CERT *c;
2186 int idx = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002187
Adam Langleyfcf25832014-12-18 17:42:32 -08002188 alg_a = cipher->algorithm_auth;
2189 c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002190
Adam Langleyfcf25832014-12-18 17:42:32 -08002191 if (alg_a & SSL_aRSA) {
2192 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
2193 idx = SSL_PKEY_RSA_SIGN;
2194 } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
2195 idx = SSL_PKEY_RSA_ENC;
2196 }
2197 } else if ((alg_a & SSL_aECDSA) &&
2198 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
2199 idx = SSL_PKEY_ECC;
2200 }
Adam Langley95c29f32014-06-20 12:00:00 -07002201
Adam Langleyfcf25832014-12-18 17:42:32 -08002202 if (idx == -1) {
2203 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2204 return NULL;
2205 }
Adam Langley95c29f32014-06-20 12:00:00 -07002206
Adam Langleyfcf25832014-12-18 17:42:32 -08002207 return c->pkeys[idx].privatekey;
2208}
Adam Langley95c29f32014-06-20 12:00:00 -07002209
Adam Langleyfcf25832014-12-18 17:42:32 -08002210void ssl_update_cache(SSL *s, int mode) {
2211 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 /* If the session_id_length is 0, we are not supposed to cache it, and it
2214 * would be rather hard to do anyway :-) */
2215 if (s->session->session_id_length == 0) {
2216 return;
2217 }
Adam Langley95c29f32014-06-20 12:00:00 -07002218
Adam Langleyfcf25832014-12-18 17:42:32 -08002219 i = s->initial_ctx->session_cache_mode;
2220 if ((i & mode) && !s->hit &&
2221 ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
2222 SSL_CTX_add_session(s->initial_ctx, s->session)) &&
2223 s->initial_ctx->new_session_cb != NULL) {
2224 CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2225 if (!s->initial_ctx->new_session_cb(s, s->session)) {
2226 SSL_SESSION_free(s->session);
2227 }
2228 }
Adam Langley95c29f32014-06-20 12:00:00 -07002229
Adam Langleyfcf25832014-12-18 17:42:32 -08002230 /* auto flush every 255 connections */
2231 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
2232 if ((((mode & SSL_SESS_CACHE_CLIENT)
2233 ? s->initial_ctx->stats.sess_connect_good
2234 : s->initial_ctx->stats.sess_accept_good) &
2235 0xff) == 0xff) {
2236 SSL_CTX_flush_sessions(s->initial_ctx, (unsigned long)time(NULL));
2237 }
2238 }
2239}
Adam Langley95c29f32014-06-20 12:00:00 -07002240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241int SSL_get_error(const SSL *s, int i) {
2242 int reason;
2243 unsigned long l;
2244 BIO *bio;
Adam Langley95c29f32014-06-20 12:00:00 -07002245
Adam Langleyfcf25832014-12-18 17:42:32 -08002246 if (i > 0) {
2247 return SSL_ERROR_NONE;
2248 }
Adam Langley95c29f32014-06-20 12:00:00 -07002249
Adam Langleyfcf25832014-12-18 17:42:32 -08002250 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2251 * where we do encode the error */
2252 l = ERR_peek_error();
2253 if (l != 0) {
2254 if (ERR_GET_LIB(l) == ERR_LIB_SYS) {
2255 return SSL_ERROR_SYSCALL;
2256 }
2257 return SSL_ERROR_SSL;
2258 }
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langleyfcf25832014-12-18 17:42:32 -08002260 if (i == 0 && (s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2261 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
2262 return SSL_ERROR_ZERO_RETURN;
2263 }
Adam Langleyb2ce0582014-06-20 12:00:00 -07002264
Adam Langleyfcf25832014-12-18 17:42:32 -08002265 assert(i < 0);
Adam Langleydc9b1412014-06-20 12:00:00 -07002266
Adam Langleyfcf25832014-12-18 17:42:32 -08002267 if (SSL_want_session(s)) {
2268 return SSL_ERROR_PENDING_SESSION;
2269 }
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Adam Langleyfcf25832014-12-18 17:42:32 -08002271 if (SSL_want_certificate(s)) {
2272 return SSL_ERROR_PENDING_CERTIFICATE;
2273 }
Adam Langley95c29f32014-06-20 12:00:00 -07002274
Adam Langleyfcf25832014-12-18 17:42:32 -08002275 if (SSL_want_read(s)) {
2276 bio = SSL_get_rbio(s);
2277 if (BIO_should_read(bio)) {
2278 return SSL_ERROR_WANT_READ;
2279 }
Adam Langley95c29f32014-06-20 12:00:00 -07002280
Adam Langleyfcf25832014-12-18 17:42:32 -08002281 if (BIO_should_write(bio)) {
2282 /* This one doesn't make too much sense ... We never try to write to the
2283 * rbio, and an application program where rbio and wbio are separate
2284 * couldn't even know what it should wait for. However if we ever set
2285 * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
2286 * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
2287 * around that bug; so it might be safer to keep it. */
2288 return SSL_ERROR_WANT_WRITE;
2289 }
Adam Langley95c29f32014-06-20 12:00:00 -07002290
Adam Langleyfcf25832014-12-18 17:42:32 -08002291 if (BIO_should_io_special(bio)) {
2292 reason = BIO_get_retry_reason(bio);
2293 if (reason == BIO_RR_CONNECT) {
2294 return SSL_ERROR_WANT_CONNECT;
2295 }
Adam Langley95c29f32014-06-20 12:00:00 -07002296
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 if (reason == BIO_RR_ACCEPT) {
2298 return SSL_ERROR_WANT_ACCEPT;
2299 }
Adam Langley95c29f32014-06-20 12:00:00 -07002300
Adam Langleyfcf25832014-12-18 17:42:32 -08002301 return SSL_ERROR_SYSCALL; /* unknown */
2302 }
2303 }
Adam Langley95c29f32014-06-20 12:00:00 -07002304
Adam Langleyfcf25832014-12-18 17:42:32 -08002305 if (SSL_want_write(s)) {
2306 bio = SSL_get_wbio(s);
2307 if (BIO_should_write(bio)) {
2308 return SSL_ERROR_WANT_WRITE;
2309 }
Adam Langley95c29f32014-06-20 12:00:00 -07002310
Adam Langleyfcf25832014-12-18 17:42:32 -08002311 if (BIO_should_read(bio)) {
2312 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2313 return SSL_ERROR_WANT_READ;
2314 }
Adam Langley95c29f32014-06-20 12:00:00 -07002315
Adam Langleyfcf25832014-12-18 17:42:32 -08002316 if (BIO_should_io_special(bio)) {
2317 reason = BIO_get_retry_reason(bio);
2318 if (reason == BIO_RR_CONNECT) {
2319 return SSL_ERROR_WANT_CONNECT;
2320 }
Adam Langley95c29f32014-06-20 12:00:00 -07002321
Adam Langleyfcf25832014-12-18 17:42:32 -08002322 if (reason == BIO_RR_ACCEPT) {
2323 return SSL_ERROR_WANT_ACCEPT;
2324 }
Adam Langley95c29f32014-06-20 12:00:00 -07002325
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 return SSL_ERROR_SYSCALL;
2327 }
2328 }
Adam Langley95c29f32014-06-20 12:00:00 -07002329
Adam Langleyfcf25832014-12-18 17:42:32 -08002330 if (SSL_want_x509_lookup(s)) {
2331 return SSL_ERROR_WANT_X509_LOOKUP;
2332 }
Adam Langley95c29f32014-06-20 12:00:00 -07002333
Adam Langleyfcf25832014-12-18 17:42:32 -08002334 if (SSL_want_channel_id_lookup(s)) {
2335 return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
2336 }
Adam Langley0f4746e2014-08-13 12:26:32 -07002337
Adam Langleyfcf25832014-12-18 17:42:32 -08002338 return SSL_ERROR_SYSCALL;
2339}
Adam Langley0f4746e2014-08-13 12:26:32 -07002340
Adam Langleyfcf25832014-12-18 17:42:32 -08002341int SSL_do_handshake(SSL *s) {
2342 int ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langleyfcf25832014-12-18 17:42:32 -08002344 if (s->handshake_func == NULL) {
2345 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2346 return -1;
2347 }
Adam Langley95c29f32014-06-20 12:00:00 -07002348
Adam Langleyfcf25832014-12-18 17:42:32 -08002349 s->method->ssl_renegotiate_check(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002350
Adam Langleyfcf25832014-12-18 17:42:32 -08002351 if (SSL_in_init(s)) {
2352 ret = s->handshake_func(s);
2353 }
2354 return ret;
2355}
Adam Langley95c29f32014-06-20 12:00:00 -07002356
Adam Langleyfcf25832014-12-18 17:42:32 -08002357void SSL_set_accept_state(SSL *s) {
2358 s->server = 1;
2359 s->shutdown = 0;
2360 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2361 s->handshake_func = s->method->ssl_accept;
2362 /* clear the current cipher */
2363 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002364}
Adam Langley95c29f32014-06-20 12:00:00 -07002365
Adam Langleyfcf25832014-12-18 17:42:32 -08002366void SSL_set_connect_state(SSL *s) {
2367 s->server = 0;
2368 s->shutdown = 0;
2369 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2370 s->handshake_func = s->method->ssl_connect;
2371 /* clear the current cipher */
2372 ssl_clear_cipher_ctx(s);
Adam Langleyfcf25832014-12-18 17:42:32 -08002373}
Adam Langley95c29f32014-06-20 12:00:00 -07002374
Adam Langleyfcf25832014-12-18 17:42:32 -08002375int ssl_undefined_function(SSL *s) {
2376 OPENSSL_PUT_ERROR(SSL, ssl_undefined_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_void_function(void) {
2382 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_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 -08002387int ssl_undefined_const_function(const SSL *s) {
2388 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
2389 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2390 return 0;
2391}
Adam Langley95c29f32014-06-20 12:00:00 -07002392
Adam Langleyfcf25832014-12-18 17:42:32 -08002393static const char *ssl_get_version(int version) {
2394 switch (version) {
2395 case TLS1_2_VERSION:
2396 return "TLSv1.2";
Adam Langley95c29f32014-06-20 12:00:00 -07002397
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 case TLS1_1_VERSION:
2399 return "TLSv1.1";
Adam Langley95c29f32014-06-20 12:00:00 -07002400
Adam Langleyfcf25832014-12-18 17:42:32 -08002401 case TLS1_VERSION:
2402 return "TLSv1";
Adam Langley95c29f32014-06-20 12:00:00 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404 case SSL3_VERSION:
2405 return "SSLv3";
Adam Langley95c29f32014-06-20 12:00:00 -07002406
Adam Langleyfcf25832014-12-18 17:42:32 -08002407 default:
2408 return "unknown";
2409 }
2410}
Adam Langley95c29f32014-06-20 12:00:00 -07002411
Adam Langleyfcf25832014-12-18 17:42:32 -08002412const char *SSL_get_version(const SSL *s) {
2413 return ssl_get_version(s->version);
2414}
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
2417 return ssl_get_version(sess->ssl_version);
2418}
Adam Langley95c29f32014-06-20 12:00:00 -07002419
Adam Langleyfcf25832014-12-18 17:42:32 -08002420void ssl_clear_cipher_ctx(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002421 if (s->aead_read_ctx != NULL) {
2422 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2423 OPENSSL_free(s->aead_read_ctx);
2424 s->aead_read_ctx = NULL;
2425 }
Adam Langleya5dc5452014-06-20 12:00:00 -07002426
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 if (s->aead_write_ctx != NULL) {
2428 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2429 OPENSSL_free(s->aead_write_ctx);
2430 s->aead_write_ctx = NULL;
2431 }
2432}
Adam Langley95c29f32014-06-20 12:00:00 -07002433
Adam Langleyfcf25832014-12-18 17:42:32 -08002434X509 *SSL_get_certificate(const SSL *s) {
2435 if (s->cert != NULL) {
2436 return s->cert->key->x509;
2437 }
2438
2439 return NULL;
2440}
2441
2442EVP_PKEY *SSL_get_privatekey(const SSL *s) {
2443 if (s->cert != NULL) {
2444 return s->cert->key->privatekey;
2445 }
2446
2447 return NULL;
2448}
2449
2450X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
2451 if (ctx->cert != NULL) {
2452 return ctx->cert->key->x509;
2453 }
2454
2455 return NULL;
2456}
2457
2458EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
2459 if (ctx->cert != NULL) {
2460 return ctx->cert->key->privatekey;
2461 }
2462
2463 return NULL;
2464}
2465
2466const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
2467 if (s->session != NULL && s->session->cipher != NULL) {
2468 return s->session->cipher;
2469 }
2470
2471 return NULL;
2472}
2473
2474const void *SSL_get_current_compression(SSL *s) { return NULL; }
2475
2476const void *SSL_get_current_expansion(SSL *s) { return NULL; }
2477
2478int ssl_init_wbio_buffer(SSL *s, int push) {
2479 BIO *bbio;
2480
2481 if (s->bbio == NULL) {
2482 bbio = BIO_new(BIO_f_buffer());
2483 if (bbio == NULL) {
2484 return 0;
2485 }
2486 s->bbio = bbio;
2487 } else {
2488 bbio = s->bbio;
2489 if (s->bbio == s->wbio) {
2490 s->wbio = BIO_pop(s->wbio);
2491 }
2492 }
2493
2494 BIO_reset(bbio);
2495 if (!BIO_set_read_buffer_size(bbio, 1)) {
2496 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2497 return 0;
2498 }
2499
2500 if (push) {
2501 if (s->wbio != bbio) {
2502 s->wbio = BIO_push(bbio, s->wbio);
2503 }
2504 } else {
2505 if (s->wbio == bbio) {
2506 s->wbio = BIO_pop(bbio);
2507 }
2508 }
2509
2510 return 1;
2511}
2512
2513void ssl_free_wbio_buffer(SSL *s) {
2514 if (s->bbio == NULL) {
2515 return;
2516 }
2517
2518 if (s->bbio == s->wbio) {
2519 /* remove buffering */
2520 s->wbio = BIO_pop(s->wbio);
2521 }
2522
2523 BIO_free(s->bbio);
2524 s->bbio = NULL;
2525}
2526
2527void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
2528 ctx->quiet_shutdown = mode;
2529}
2530
2531int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
2532 return ctx->quiet_shutdown;
2533}
2534
2535void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
2536
2537int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
2538
2539void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
2540
2541int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
2542
2543int SSL_version(const SSL *s) { return s->version; }
2544
2545SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
2546
2547SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
2548 if (ssl->ctx == ctx) {
2549 return ssl->ctx;
2550 }
2551
2552 if (ctx == NULL) {
2553 ctx = ssl->initial_ctx;
2554 }
2555
2556 if (ssl->cert != NULL) {
2557 ssl_cert_free(ssl->cert);
2558 }
2559
2560 ssl->cert = ssl_cert_dup(ctx->cert);
2561 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2562 if (ssl->ctx != NULL) {
2563 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2564 }
2565 ssl->ctx = ctx;
2566
2567 ssl->sid_ctx_length = ctx->sid_ctx_length;
2568 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2569 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2570
2571 return ssl->ctx;
2572}
2573
2574int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
2575 return X509_STORE_set_default_paths(ctx->cert_store);
2576}
Adam Langley95c29f32014-06-20 12:00:00 -07002577
2578int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
Adam Langleyfcf25832014-12-18 17:42:32 -08002579 const char *CApath) {
2580 return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
2581}
Adam Langley95c29f32014-06-20 12:00:00 -07002582
2583void SSL_set_info_callback(SSL *ssl,
Adam Langleyfcf25832014-12-18 17:42:32 -08002584 void (*cb)(const SSL *ssl, int type, int val)) {
2585 ssl->info_callback = cb;
2586}
Adam Langley95c29f32014-06-20 12:00:00 -07002587
Adam Langleyfcf25832014-12-18 17:42:32 -08002588void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
2589 int /*val*/) {
2590 return ssl->info_callback;
2591}
Adam Langley95c29f32014-06-20 12:00:00 -07002592
Adam Langleyfcf25832014-12-18 17:42:32 -08002593int SSL_state(const SSL *ssl) { return ssl->state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002594
Adam Langleyfcf25832014-12-18 17:42:32 -08002595void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
Adam Langley95c29f32014-06-20 12:00:00 -07002596
Adam Langleyfcf25832014-12-18 17:42:32 -08002597void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
Adam Langley95c29f32014-06-20 12:00:00 -07002598
Adam Langleyfcf25832014-12-18 17:42:32 -08002599long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
Adam Langley95c29f32014-06-20 12:00:00 -07002600
Adam Langleyfcf25832014-12-18 17:42:32 -08002601int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2602 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
2603 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func,
2604 dup_func, free_func);
2605}
Adam Langley95c29f32014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607int SSL_set_ex_data(SSL *s, int idx, void *arg) {
2608 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2609}
Adam Langley95c29f32014-06-20 12:00:00 -07002610
Adam Langleyfcf25832014-12-18 17:42:32 -08002611void *SSL_get_ex_data(const SSL *s, int idx) {
2612 return CRYPTO_get_ex_data(&s->ex_data, idx);
2613}
Adam Langley95c29f32014-06-20 12:00:00 -07002614
Adam Langleyfcf25832014-12-18 17:42:32 -08002615int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2616 CRYPTO_EX_dup *dup_func,
2617 CRYPTO_EX_free *free_func) {
2618 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func,
2619 dup_func, free_func);
2620}
Adam Langley95c29f32014-06-20 12:00:00 -07002621
Adam Langleyfcf25832014-12-18 17:42:32 -08002622int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
2623 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
2624}
Adam Langley95c29f32014-06-20 12:00:00 -07002625
Adam Langleyfcf25832014-12-18 17:42:32 -08002626void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
2627 return CRYPTO_get_ex_data(&s->ex_data, idx);
2628}
Adam Langley95c29f32014-06-20 12:00:00 -07002629
Adam Langleyfcf25832014-12-18 17:42:32 -08002630int ssl_ok(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07002631
Adam Langleyfcf25832014-12-18 17:42:32 -08002632X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
2633 return ctx->cert_store;
2634}
Adam Langley95c29f32014-06-20 12:00:00 -07002635
Adam Langleyfcf25832014-12-18 17:42:32 -08002636void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
2637 if (ctx->cert_store != NULL) {
2638 X509_STORE_free(ctx->cert_store);
2639 }
2640 ctx->cert_store = store;
2641}
Adam Langley95c29f32014-06-20 12:00:00 -07002642
Adam Langleyfcf25832014-12-18 17:42:32 -08002643int SSL_want(const SSL *s) { return s->rwstate; }
Adam Langley95c29f32014-06-20 12:00:00 -07002644
Adam Langleyfcf25832014-12-18 17:42:32 -08002645void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
2646 RSA *(*cb)(SSL *ssl, int is_export,
2647 int keylength)) {
2648 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2649}
Adam Langley95c29f32014-06-20 12:00:00 -07002650
Adam Langleyfcf25832014-12-18 17:42:32 -08002651void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
2652 int keylength)) {
2653 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
2654}
2655
2656void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
2657 DH *(*dh)(SSL *ssl, int is_export,
2658 int keylength)) {
2659 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2660}
2661
2662void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
2663 int keylength)) {
2664 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
2665}
2666
2667void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
2668 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2669 int keylength)) {
2670 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2671}
2672
2673void SSL_set_tmp_ecdh_callback(SSL *ssl,
2674 EC_KEY *(*ecdh)(SSL *ssl, int is_export,
2675 int keylength)) {
2676 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
2677}
2678
2679int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2680 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2681 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
2682 SSL_R_DATA_LENGTH_TOO_LONG);
2683 return 0;
2684 }
2685
2686 if (ctx->psk_identity_hint != NULL) {
2687 OPENSSL_free(ctx->psk_identity_hint);
2688 }
2689
2690 if (identity_hint != NULL) {
2691 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2692 if (ctx->psk_identity_hint == NULL) {
2693 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002694 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002695 } else {
2696 ctx->psk_identity_hint = NULL;
2697 }
Adam Langley95c29f32014-06-20 12:00:00 -07002698
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 return 1;
2700}
2701
2702int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
2703 if (s == NULL) {
2704 return 0;
2705 }
2706
2707 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2708 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
2709 SSL_R_DATA_LENGTH_TOO_LONG);
2710 return 0;
2711 }
2712
2713 /* Clear currently configured hint, if any. */
2714 if (s->psk_identity_hint != NULL) {
2715 OPENSSL_free(s->psk_identity_hint);
2716 s->psk_identity_hint = NULL;
2717 }
2718
2719 if (identity_hint != NULL) {
2720 s->psk_identity_hint = BUF_strdup(identity_hint);
2721 if (s->psk_identity_hint == NULL) {
2722 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002723 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 }
Adam Langley95c29f32014-06-20 12:00:00 -07002725
Adam Langleyfcf25832014-12-18 17:42:32 -08002726 return 1;
2727}
Adam Langley95c29f32014-06-20 12:00:00 -07002728
Adam Langleyfcf25832014-12-18 17:42:32 -08002729const char *SSL_get_psk_identity_hint(const SSL *s) {
2730 if (s == NULL) {
2731 return NULL;
2732 }
2733 return s->psk_identity_hint;
2734}
Adam Langley95c29f32014-06-20 12:00:00 -07002735
Adam Langleyfcf25832014-12-18 17:42:32 -08002736const char *SSL_get_psk_identity(const SSL *s) {
2737 if (s == NULL || s->session == NULL) {
2738 return NULL;
2739 }
Adam Langley95c29f32014-06-20 12:00:00 -07002740
Adam Langleyfcf25832014-12-18 17:42:32 -08002741 return s->session->psk_identity;
2742}
Adam Langley95c29f32014-06-20 12:00:00 -07002743
Adam Langleyfcf25832014-12-18 17:42:32 -08002744void SSL_set_psk_client_callback(
2745 SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2746 unsigned int max_identity_len, uint8_t *psk,
2747 unsigned int max_psk_len)) {
2748 s->psk_client_callback = cb;
2749}
Adam Langley95c29f32014-06-20 12:00:00 -07002750
Adam Langleyfcf25832014-12-18 17:42:32 -08002751void SSL_CTX_set_psk_client_callback(
2752 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
2753 unsigned int max_identity_len,
2754 uint8_t *psk, unsigned int max_psk_len)) {
2755 ctx->psk_client_callback = cb;
2756}
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langleyfcf25832014-12-18 17:42:32 -08002758void SSL_set_psk_server_callback(
2759 SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
2760 unsigned int max_psk_len)) {
2761 s->psk_server_callback = cb;
2762}
Adam Langley95c29f32014-06-20 12:00:00 -07002763
Adam Langleyfcf25832014-12-18 17:42:32 -08002764void SSL_CTX_set_psk_server_callback(
2765 SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
2766 uint8_t *psk, unsigned int max_psk_len)) {
2767 ctx->psk_server_callback = cb;
2768}
Adam Langley95c29f32014-06-20 12:00:00 -07002769
Adam Langleyfcf25832014-12-18 17:42:32 -08002770void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
2771 ctx->min_version = version;
2772}
Adam Langley95c29f32014-06-20 12:00:00 -07002773
Adam Langleyfcf25832014-12-18 17:42:32 -08002774void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
2775 ctx->max_version = version;
2776}
Adam Langley0289c732014-06-20 12:00:00 -07002777
Adam Langleyfcf25832014-12-18 17:42:32 -08002778void SSL_set_min_version(SSL *ssl, uint16_t version) {
2779 ssl->min_version = version;
2780}
Adam Langley0289c732014-06-20 12:00:00 -07002781
Adam Langleyfcf25832014-12-18 17:42:32 -08002782void SSL_set_max_version(SSL *ssl, uint16_t version) {
2783 ssl->max_version = version;
2784}
Adam Langley95c29f32014-06-20 12:00:00 -07002785
Adam Langleyfcf25832014-12-18 17:42:32 -08002786void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
2787 void (*cb)(int write_p, int version,
2788 int content_type, const void *buf,
2789 size_t len, SSL *ssl, void *arg)) {
2790 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2791}
2792void SSL_set_msg_callback(SSL *ssl,
2793 void (*cb)(int write_p, int version, int content_type,
2794 const void *buf, size_t len, SSL *ssl,
2795 void *arg)) {
2796 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2797}
Adam Langley95c29f32014-06-20 12:00:00 -07002798
Adam Langleyfcf25832014-12-18 17:42:32 -08002799void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
2800 if (ctx->keylog_bio != NULL) {
2801 BIO_free(ctx->keylog_bio);
2802 }
2803 ctx->keylog_bio = keylog_bio;
2804}
Adam Langley95c29f32014-06-20 12:00:00 -07002805
Adam Langleyfcf25832014-12-18 17:42:32 -08002806static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2807 static const char hextable[] = "0123456789abcdef";
2808 uint8_t *out;
2809 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002810
Adam Langleyfcf25832014-12-18 17:42:32 -08002811 if (!CBB_add_space(cbb, &out, in_len * 2)) {
2812 return 0;
2813 }
Adam Langley95c29f32014-06-20 12:00:00 -07002814
Adam Langleyfcf25832014-12-18 17:42:32 -08002815 for (i = 0; i < in_len; i++) {
2816 *(out++) = (uint8_t)hextable[in[i] >> 4];
2817 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2818 }
Adam Langley95c29f32014-06-20 12:00:00 -07002819
Adam Langleyfcf25832014-12-18 17:42:32 -08002820 return 1;
2821}
David Benjamin859ec3c2014-09-02 16:29:36 -04002822
2823int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 const uint8_t *encrypted_premaster,
2825 size_t encrypted_premaster_len,
2826 const uint8_t *premaster,
2827 size_t premaster_len) {
2828 BIO *bio = ctx->keylog_bio;
2829 CBB cbb;
2830 uint8_t *out;
2831 size_t out_len;
2832 int ret;
David Benjamin859ec3c2014-09-02 16:29:36 -04002833
Adam Langleyfcf25832014-12-18 17:42:32 -08002834 if (bio == NULL) {
2835 return 1;
2836 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002837
Adam Langleyfcf25832014-12-18 17:42:32 -08002838 if (encrypted_premaster_len < 8) {
2839 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
2840 ERR_R_INTERNAL_ERROR);
2841 return 0;
2842 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002843
Adam Langleyfcf25832014-12-18 17:42:32 -08002844 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
2845 return 0;
2846 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002847
Adam Langleyfcf25832014-12-18 17:42:32 -08002848 if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
2849 /* Only the first 8 bytes of the encrypted premaster secret are
2850 * logged. */
2851 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2852 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2853 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2854 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2855 !CBB_finish(&cbb, &out, &out_len)) {
2856 CBB_cleanup(&cbb);
2857 return 0;
2858 }
David Benjamin859ec3c2014-09-02 16:29:36 -04002859
Adam Langleyfcf25832014-12-18 17:42:32 -08002860 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2861 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2862 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamin859ec3c2014-09-02 16:29:36 -04002863
Adam Langleyfcf25832014-12-18 17:42:32 -08002864 OPENSSL_free(out);
2865 return ret;
Adam Langley95f22882014-06-20 12:00:00 -07002866}
2867
Adam Langleyfcf25832014-12-18 17:42:32 -08002868int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
2869 size_t client_random_len, const uint8_t *master,
2870 size_t master_len) {
2871 BIO *bio = ctx->keylog_bio;
2872 CBB cbb;
2873 uint8_t *out;
2874 size_t out_len;
2875 int ret;
Adam Langleyadb739e2014-06-20 12:00:00 -07002876
Adam Langleyfcf25832014-12-18 17:42:32 -08002877 if (bio == NULL) {
2878 return 1;
2879 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002880
Adam Langleyfcf25832014-12-18 17:42:32 -08002881 if (client_random_len != 32) {
2882 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2883 return 0;
2884 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002885
Adam Langleyfcf25832014-12-18 17:42:32 -08002886 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
2887 return 0;
2888 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002889
Adam Langleyfcf25832014-12-18 17:42:32 -08002890 if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2891 !cbb_add_hex(&cbb, client_random, 32) ||
2892 !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2893 !cbb_add_hex(&cbb, master, master_len) ||
2894 !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
2895 !CBB_finish(&cbb, &out, &out_len)) {
2896 CBB_cleanup(&cbb);
2897 return 0;
2898 }
Adam Langleyadb739e2014-06-20 12:00:00 -07002899
Adam Langleyfcf25832014-12-18 17:42:32 -08002900 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2901 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2902 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
David Benjamine99e9122014-12-11 01:46:01 -05002903
Adam Langleyfcf25832014-12-18 17:42:32 -08002904 OPENSSL_free(out);
2905 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -07002906}
2907
Adam Langleyfcf25832014-12-18 17:42:32 -08002908int SSL_cutthrough_complete(const SSL *s) {
2909 return (
2910 !s->server && /* cutthrough only applies to clients */
2911 !s->hit && /* full-handshake */
2912 s->version >= SSL3_VERSION &&
2913 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
2914 (SSL_get_mode((SSL *)s) &
2915 SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
2916 ssl3_can_cutthrough(s) && /* cutthrough allowed */
2917 s->s3->previous_server_finished_len ==
2918 0 && /* not a renegotiation handshake */
2919 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
2920 s->state == SSL3_ST_CR_CHANGE || s->state == SSL3_ST_CR_FINISHED_A));
2921}
Adam Langley95c29f32014-06-20 12:00:00 -07002922
Adam Langleyfcf25832014-12-18 17:42:32 -08002923void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
2924 size_t *ssl_session_size) {
2925 *ssl_size = sizeof(SSL);
2926 *ssl_ctx_size = sizeof(SSL_CTX);
2927 *ssl_session_size = sizeof(SSL_SESSION);
2928}
Feng Lu41aa3252014-11-21 22:47:56 -08002929
Adam Langleyfcf25832014-12-18 17:42:32 -08002930int ssl3_can_cutthrough(const SSL *s) {
2931 const SSL_CIPHER *c;
2932
2933 /* require a strong enough cipher */
2934 if (SSL_get_cipher_bits(s, NULL) < 128) {
2935 return 0;
2936 }
2937
2938 /* require ALPN or NPN extension */
2939 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen) {
2940 return 0;
2941 }
2942
2943 /* require a forward-secret cipher */
2944 c = SSL_get_current_cipher(s);
2945 if (!c ||
2946 (c->algorithm_mkey != SSL_kEDH && c->algorithm_mkey != SSL_kEECDH)) {
2947 return 0;
2948 }
2949
2950 return 1;
2951}
2952
2953const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2954 switch (version) {
2955 case SSL3_VERSION:
2956 return &SSLv3_enc_data;
2957
2958 case TLS1_VERSION:
2959 return &TLSv1_enc_data;
2960
2961 case TLS1_1_VERSION:
2962 return &TLSv1_1_enc_data;
2963
2964 case TLS1_2_VERSION:
2965 return &TLSv1_2_enc_data;
2966
2967 case DTLS1_VERSION:
2968 return &DTLSv1_enc_data;
2969
2970 case DTLS1_2_VERSION:
2971 return &DTLSv1_2_enc_data;
2972
2973 default:
2974 return NULL;
2975 }
2976}
2977
2978uint16_t ssl3_get_max_server_version(const SSL *s) {
2979 uint16_t max_version;
2980
2981 if (SSL_IS_DTLS(s)) {
2982 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
2983 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
2984 return DTLS1_2_VERSION;
2985 }
2986 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2987 return DTLS1_VERSION;
2988 }
2989 return 0;
2990 }
2991
2992 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
2993 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2994 return TLS1_2_VERSION;
2995 }
2996 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2997 return TLS1_1_VERSION;
2998 }
2999 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
3000 return TLS1_VERSION;
3001 }
3002 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
3003 return SSL3_VERSION;
3004 }
3005 return 0;
3006}
3007
3008uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
3009 uint16_t version = 0;
3010
3011 if (SSL_IS_DTLS(s)) {
3012 /* Clamp client_version to max_version. */
3013 if (s->max_version != 0 && client_version < s->max_version) {
3014 client_version = s->max_version;
3015 }
3016
3017 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
3018 version = DTLS1_2_VERSION;
3019 } else if (client_version <= DTLS1_VERSION &&
3020 !(s->options & SSL_OP_NO_DTLSv1)) {
3021 version = DTLS1_VERSION;
3022 }
3023
3024 /* Check against min_version. */
3025 if (version != 0 && s->min_version != 0 && version > s->min_version) {
3026 return 0;
3027 }
3028 return version;
3029 } else {
3030 /* Clamp client_version to max_version. */
3031 if (s->max_version != 0 && client_version > s->max_version) {
3032 client_version = s->max_version;
3033 }
3034
3035 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
3036 version = TLS1_2_VERSION;
3037 } else if (client_version >= TLS1_1_VERSION &&
3038 !(s->options & SSL_OP_NO_TLSv1_1)) {
3039 version = TLS1_1_VERSION;
3040 } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
3041 version = TLS1_VERSION;
3042 } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
3043 version = SSL3_VERSION;
3044 }
3045
3046 /* Check against min_version. */
3047 if (version != 0 && s->min_version != 0 && version < s->min_version) {
3048 return 0;
3049 }
3050 return version;
3051 }
3052}
3053
3054uint16_t ssl3_get_max_client_version(SSL *s) {
3055 unsigned long options = s->options;
3056 uint16_t version = 0;
3057
3058 /* OpenSSL's API for controlling versions entails blacklisting individual
3059 * protocols. This has two problems. First, on the client, the protocol can
3060 * only express a contiguous range of versions. Second, a library consumer
3061 * trying to set a maximum version cannot disable protocol versions that get
3062 * added in a future version of the library.
3063 *
3064 * To account for both of these, OpenSSL interprets the client-side bitmask
3065 * as a min/max range by picking the lowest contiguous non-empty range of
3066 * enabled protocols. Note that this means it is impossible to set a maximum
3067 * version of TLS 1.2 in a future-proof way.
3068 *
3069 * By this scheme, the maximum version is the lowest version V such that V is
3070 * enabled and V+1 is disabled or unimplemented. */
3071 if (SSL_IS_DTLS(s)) {
3072 if (!(options & SSL_OP_NO_DTLSv1_2)) {
3073 version = DTLS1_2_VERSION;
3074 }
3075 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
3076 version = DTLS1_VERSION;
3077 }
3078 if (s->max_version != 0 && version < s->max_version) {
3079 version = s->max_version;
3080 }
3081 } else {
3082 if (!(options & SSL_OP_NO_TLSv1_2)) {
3083 version = TLS1_2_VERSION;
3084 }
3085 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
3086 version = TLS1_1_VERSION;
3087 }
3088 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
3089 version = TLS1_VERSION;
3090 }
3091 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
3092 version = SSL3_VERSION;
3093 }
3094 if (s->max_version != 0 && version > s->max_version) {
3095 version = s->max_version;
3096 }
3097 }
3098
3099 return version;
3100}
3101
3102int ssl3_is_version_enabled(SSL *s, uint16_t version) {
3103 if (SSL_IS_DTLS(s)) {
3104 if (s->max_version != 0 && version < s->max_version) {
3105 return 0;
3106 }
3107 if (s->min_version != 0 && version > s->min_version) {
3108 return 0;
3109 }
3110
3111 switch (version) {
3112 case DTLS1_VERSION:
3113 return !(s->options & SSL_OP_NO_DTLSv1);
3114
3115 case DTLS1_2_VERSION:
3116 return !(s->options & SSL_OP_NO_DTLSv1_2);
3117
3118 default:
3119 return 0;
3120 }
3121 } else {
3122 if (s->max_version != 0 && version > s->max_version) {
3123 return 0;
3124 }
3125 if (s->min_version != 0 && version < s->min_version) {
3126 return 0;
3127 }
3128
3129 switch (version) {
3130 case SSL3_VERSION:
3131 return !(s->options & SSL_OP_NO_SSLv3);
3132
3133 case TLS1_VERSION:
3134 return !(s->options & SSL_OP_NO_TLSv1);
3135
3136 case TLS1_1_VERSION:
3137 return !(s->options & SSL_OP_NO_TLSv1_1);
3138
3139 case TLS1_2_VERSION:
3140 return !(s->options & SSL_OP_NO_TLSv1_2);
3141
3142 default:
3143 return 0;
3144 }
3145 }
3146}
3147
David Benjaminea72bd02014-12-21 21:27:41 -05003148uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
3149 if (!SSL_IS_DTLS(s)) {
3150 return wire_version;
3151 }
3152
3153 uint16_t tls_version = ~wire_version;
3154 uint16_t version = tls_version + 0x0201;
3155 /* If either component overflowed, clamp it so comparisons still work. */
3156 if ((version >> 8) < (tls_version >> 8)) {
3157 version = 0xff00 | (version & 0xff);
3158 }
3159 if ((version & 0xff) < (tls_version & 0xff)) {
3160 version = (version & 0xff00) | 0xff;
3161 }
3162 /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
3163 if (version == TLS1_VERSION) {
3164 version = TLS1_1_VERSION;
3165 }
3166 return version;
3167}
3168
Adam Langleyfcf25832014-12-18 17:42:32 -08003169int SSL_cache_hit(SSL *s) { return s->hit; }
3170
3171int SSL_is_server(SSL *s) { return s->server; }
3172
3173void SSL_enable_fastradio_padding(SSL *s, char on_off) {
3174 s->fastradio_padding = on_off;
3175}