blob: 574b85f8caa86a27ab4558cfbb500b9b3bf045cc [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
David Benjamine1136082014-09-20 12:28:58 -0400155/* Some error codes are special. Ensure the make_errors.go script
156 * never regresses this. */
157OPENSSL_COMPILE_ASSERT(
158 SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
159 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
160 ssl_alert_reason_code_mismatch);
161
Adam Langley95c29f32014-06-20 12:00:00 -0700162int SSL_clear(SSL *s)
163 {
164
165 if (s->method == NULL)
166 {
167 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
168 return(0);
169 }
170
171 if (ssl_clear_bad_session(s))
172 {
173 SSL_SESSION_free(s->session);
174 s->session=NULL;
175 }
176
Adam Langley95c29f32014-06-20 12:00:00 -0700177 s->hit=0;
178 s->shutdown=0;
179
180#if 0 /* Disabled since version 1.10 of this file (early return not
181 * needed because SSL_clear is not called when doing renegotiation) */
182 /* This is set if we are doing dynamic renegotiation so keep
183 * the old cipher. It is sort of a SSL_clear_lite :-) */
184 if (s->renegotiate) return(1);
185#else
186 if (s->renegotiate)
187 {
188 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
189 return 0;
190 }
191#endif
192
David Benjaminf34a0092014-11-30 02:01:26 -0500193 /* SSL_clear may be called before or after the |s| is initialized in
194 * either accept or connect state. In the latter case, SSL_clear should
195 * preserve the half and reset |s->state| accordingly. */
196 if (s->handshake_func != NULL)
197 {
198 if (s->server)
199 SSL_set_accept_state(s);
200 else
201 SSL_set_connect_state(s);
202 }
203 else
204 {
205 assert(s->state == 0);
206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
208 s->version=s->method->version;
209 s->client_version=s->version;
210 s->rwstate=SSL_NOTHING;
211 s->rstate=SSL_ST_READ_HEADER;
212#if 0
213 s->read_ahead=s->ctx->read_ahead;
214#endif
215
216 if (s->init_buf != NULL)
217 {
218 BUF_MEM_free(s->init_buf);
219 s->init_buf=NULL;
220 }
221
222 ssl_clear_cipher_ctx(s);
223 ssl_clear_hash_ctx(&s->read_hash);
224 ssl_clear_hash_ctx(&s->write_hash);
225
David Benjaminbb15e3d2014-11-29 16:03:41 -0500226 s->method->ssl_clear(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700227 return(1);
228 }
229
Adam Langley95c29f32014-06-20 12:00:00 -0700230SSL *SSL_new(SSL_CTX *ctx)
231 {
232 SSL *s;
233
234 if (ctx == NULL)
235 {
236 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
237 return(NULL);
238 }
239 if (ctx->method == NULL)
240 {
241 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
242 return(NULL);
243 }
244
245 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
246 if (s == NULL) goto err;
247 memset(s,0,sizeof(SSL));
248
David Benjamin1eb367c2014-12-12 18:17:51 -0500249 s->min_version = ctx->min_version;
250 s->max_version = ctx->max_version;
251
Adam Langley95c29f32014-06-20 12:00:00 -0700252 s->options=ctx->options;
253 s->mode=ctx->mode;
254 s->max_cert_list=ctx->max_cert_list;
255
256 if (ctx->cert != NULL)
257 {
258 /* Earlier library versions used to copy the pointer to
259 * the CERT, not its contents; only when setting new
260 * parameters for the per-SSL copy, ssl_cert_new would be
261 * called (and the direct reference to the per-SSL_CTX
262 * settings would be lost, but those still were indirectly
263 * accessed for various purposes, and for that reason they
264 * used to be known as s->ctx->default_cert).
265 * Now we don't look at the SSL_CTX's CERT after having
266 * duplicated it once. */
267
268 s->cert = ssl_cert_dup(ctx->cert);
269 if (s->cert == NULL)
270 goto err;
271 }
272 else
273 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
274
275 s->read_ahead=ctx->read_ahead;
276 s->msg_callback=ctx->msg_callback;
277 s->msg_callback_arg=ctx->msg_callback_arg;
278 s->verify_mode=ctx->verify_mode;
279#if 0
280 s->verify_depth=ctx->verify_depth;
281#endif
282 s->sid_ctx_length=ctx->sid_ctx_length;
283 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
284 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
285 s->verify_callback=ctx->default_verify_callback;
286 s->generate_session_id=ctx->generate_session_id;
287
288 s->param = X509_VERIFY_PARAM_new();
289 if (!s->param)
290 goto err;
291 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
292#if 0
293 s->purpose = ctx->purpose;
294 s->trust = ctx->trust;
295#endif
296 s->quiet_shutdown=ctx->quiet_shutdown;
297 s->max_send_fragment = ctx->max_send_fragment;
298
299 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
300 s->ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700301 s->tlsext_debug_cb = 0;
302 s->tlsext_debug_arg = NULL;
303 s->tlsext_ticket_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700304 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
305 s->initial_ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700306 if (ctx->tlsext_ecpointformatlist)
307 {
308 s->tlsext_ecpointformatlist =
309 BUF_memdup(ctx->tlsext_ecpointformatlist,
310 ctx->tlsext_ecpointformatlist_length);
311 if (!s->tlsext_ecpointformatlist)
312 goto err;
313 s->tlsext_ecpointformatlist_length =
314 ctx->tlsext_ecpointformatlist_length;
315 }
316 if (ctx->tlsext_ellipticcurvelist)
317 {
318 s->tlsext_ellipticcurvelist =
319 BUF_memdup(ctx->tlsext_ellipticcurvelist,
David Benjamin072334d2014-07-13 16:24:27 -0400320 ctx->tlsext_ellipticcurvelist_length * 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700321 if (!s->tlsext_ellipticcurvelist)
322 goto err;
323 s->tlsext_ellipticcurvelist_length =
324 ctx->tlsext_ellipticcurvelist_length;
325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700327
328 if (s->ctx->alpn_client_proto_list)
329 {
David Benjamin072c9532014-07-26 11:44:25 -0400330 s->alpn_client_proto_list = BUF_memdup(
331 s->ctx->alpn_client_proto_list,
332 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700333 if (s->alpn_client_proto_list == NULL)
334 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700335 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
336 }
Adam Langley95c29f32014-06-20 12:00:00 -0700337
338 s->verify_result=X509_V_OK;
339
340 s->method=ctx->method;
341
342 if (!s->method->ssl_new(s))
343 goto err;
David Benjamine99e9122014-12-11 01:46:01 -0500344 s->enc_method = ssl3_get_enc_method(s->version);
345 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700346
347 s->references=1;
Adam Langley95c29f32014-06-20 12:00:00 -0700348
349 SSL_clear(s);
350
351 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
352
Adam Langley0289c732014-06-20 12:00:00 -0700353 s->psk_identity_hint = NULL;
354 if (ctx->psk_identity_hint)
355 {
356 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
357 if (s->psk_identity_hint == NULL)
358 goto err;
359 }
Adam Langley95c29f32014-06-20 12:00:00 -0700360 s->psk_client_callback=ctx->psk_client_callback;
361 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700362
David Benjaminff42cc12014-11-26 00:16:14 -0500363 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
364 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200365
Adam Langley95c29f32014-06-20 12:00:00 -0700366 return(s);
367err:
368 if (s != NULL)
369 {
370 if (s->cert != NULL)
371 ssl_cert_free(s->cert);
372 if (s->ctx != NULL)
373 SSL_CTX_free(s->ctx); /* decrement reference count */
374 OPENSSL_free(s);
375 }
376 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
377 return(NULL);
378 }
379
380int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
381 unsigned int sid_ctx_len)
382 {
383 if(sid_ctx_len > sizeof ctx->sid_ctx)
384 {
385 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
386 return 0;
387 }
388 ctx->sid_ctx_length=sid_ctx_len;
389 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
390
391 return 1;
392 }
393
394int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
395 unsigned int sid_ctx_len)
396 {
397 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
398 {
399 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
400 return 0;
401 }
402 ssl->sid_ctx_length=sid_ctx_len;
403 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
404
405 return 1;
406 }
407
408int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
409 {
410 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
411 ctx->generate_session_id = cb;
412 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
413 return 1;
414 }
415
416int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
417 {
418 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
419 ssl->generate_session_id = cb;
420 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
421 return 1;
422 }
423
424int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
425 unsigned int id_len)
426 {
427 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
428 * we can "construct" a session to give us the desired check - ie. to
429 * find if there's a session in the hash table that would conflict with
430 * any new session built out of this id/id_len and the ssl_version in
431 * use by this SSL. */
432 SSL_SESSION r, *p;
433
434 if(id_len > sizeof r.session_id)
435 return 0;
436
437 r.ssl_version = ssl->version;
438 r.session_id_length = id_len;
439 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700440
441 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
442 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
443 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
444 return (p != NULL);
445 }
446
447int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
448 {
449 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
450 }
451
452int SSL_set_purpose(SSL *s, int purpose)
453 {
454 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
455 }
456
457int SSL_CTX_set_trust(SSL_CTX *s, int trust)
458 {
459 return X509_VERIFY_PARAM_set_trust(s->param, trust);
460 }
461
462int SSL_set_trust(SSL *s, int trust)
463 {
464 return X509_VERIFY_PARAM_set_trust(s->param, trust);
465 }
466
467int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
468 {
469 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
470 }
471
472int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
473 {
474 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
475 }
476
Adam Langley858a88d2014-06-20 12:00:00 -0700477void ssl_cipher_preference_list_free(
478 struct ssl_cipher_preference_list_st *cipher_list)
479 {
480 sk_SSL_CIPHER_free(cipher_list->ciphers);
481 OPENSSL_free(cipher_list->in_group_flags);
482 OPENSSL_free(cipher_list);
483 }
484
485struct ssl_cipher_preference_list_st*
486ssl_cipher_preference_list_dup(
487 struct ssl_cipher_preference_list_st *cipher_list)
488 {
489 struct ssl_cipher_preference_list_st* ret = NULL;
490 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
491
492 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
493 if (!ret)
494 goto err;
495 ret->ciphers = NULL;
496 ret->in_group_flags = NULL;
497 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
498 if (!ret->ciphers)
499 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400500 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700501 if (!ret->in_group_flags)
502 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700503 return ret;
504
505err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400506 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700507 sk_SSL_CIPHER_free(ret->ciphers);
508 if (ret)
509 OPENSSL_free(ret);
510 return NULL;
511 }
512
513struct ssl_cipher_preference_list_st*
514ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
515 {
516 struct ssl_cipher_preference_list_st* ret = NULL;
517 size_t n = sk_SSL_CIPHER_num(ciphers);
518
519 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
520 if (!ret)
521 goto err;
522 ret->ciphers = NULL;
523 ret->in_group_flags = NULL;
524 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
525 if (!ret->ciphers)
526 goto err;
527 ret->in_group_flags = OPENSSL_malloc(n);
528 if (!ret->in_group_flags)
529 goto err;
530 memset(ret->in_group_flags, 0, n);
531 return ret;
532
533err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400534 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700535 sk_SSL_CIPHER_free(ret->ciphers);
536 if (ret)
537 OPENSSL_free(ret);
538 return NULL;
539 }
540
Adam Langley95c29f32014-06-20 12:00:00 -0700541X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
542 {
543 return ctx->param;
544 }
545
546X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
547 {
548 return ssl->param;
549 }
550
551void SSL_certs_clear(SSL *s)
552 {
553 ssl_cert_clear_certs(s->cert);
554 }
555
556void SSL_free(SSL *s)
557 {
558 int i;
559
560 if(s == NULL)
561 return;
562
563 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
564#ifdef REF_PRINT
565 REF_PRINT("SSL",s);
566#endif
567 if (i > 0) return;
568#ifdef REF_CHECK
569 if (i < 0)
570 {
571 fprintf(stderr,"SSL_free, bad reference count\n");
572 abort(); /* ok */
573 }
574#endif
575
576 if (s->param)
577 X509_VERIFY_PARAM_free(s->param);
578
579 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
580
581 if (s->bbio != NULL)
582 {
583 /* If the buffering BIO is in place, pop it off */
584 if (s->bbio == s->wbio)
585 {
586 s->wbio=BIO_pop(s->wbio);
587 }
588 BIO_free(s->bbio);
589 s->bbio=NULL;
590 }
591 if (s->rbio != NULL)
592 BIO_free_all(s->rbio);
593 if ((s->wbio != NULL) && (s->wbio != s->rbio))
594 BIO_free_all(s->wbio);
595
596 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
597
598 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700599 if (s->cipher_list != NULL)
600 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700601 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
602
603 /* Make the next call work :-) */
604 if (s->session != NULL)
605 {
606 ssl_clear_bad_session(s);
607 SSL_SESSION_free(s->session);
608 }
609
610 ssl_clear_cipher_ctx(s);
611 ssl_clear_hash_ctx(&s->read_hash);
612 ssl_clear_hash_ctx(&s->write_hash);
613
614 if (s->cert != NULL) ssl_cert_free(s->cert);
615 /* Free up if allocated */
616
Adam Langley95c29f32014-06-20 12:00:00 -0700617 if (s->tlsext_hostname)
618 OPENSSL_free(s->tlsext_hostname);
619 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700620 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
621 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -0700622 if (s->alpn_client_proto_list)
623 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700624 if (s->tlsext_channel_id_private)
625 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langley0289c732014-06-20 12:00:00 -0700627 if (s->psk_identity_hint)
628 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700629
Adam Langley95c29f32014-06-20 12:00:00 -0700630 if (s->client_CA != NULL)
631 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
632
633 if (s->method != NULL) s->method->ssl_free(s);
634
635 if (s->ctx) SSL_CTX_free(s->ctx);
636
Adam Langley95c29f32014-06-20 12:00:00 -0700637 if (s->next_proto_negotiated)
638 OPENSSL_free(s->next_proto_negotiated);
Adam Langley95c29f32014-06-20 12:00:00 -0700639
640 if (s->srtp_profiles)
641 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
642
Adam Langley95c29f32014-06-20 12:00:00 -0700643 OPENSSL_free(s);
644 }
645
646void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
647 {
648 /* If the output buffering BIO is still in place, remove it
649 */
650 if (s->bbio != NULL)
651 {
652 if (s->wbio == s->bbio)
653 {
654 s->wbio=s->wbio->next_bio;
655 s->bbio->next_bio=NULL;
656 }
657 }
658 if ((s->rbio != NULL) && (s->rbio != rbio))
659 BIO_free_all(s->rbio);
660 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
661 BIO_free_all(s->wbio);
662 s->rbio=rbio;
663 s->wbio=wbio;
664 }
665
666BIO *SSL_get_rbio(const SSL *s)
667 { return(s->rbio); }
668
669BIO *SSL_get_wbio(const SSL *s)
670 { return(s->wbio); }
671
672int SSL_get_fd(const SSL *s)
673 {
674 return(SSL_get_rfd(s));
675 }
676
677int SSL_get_rfd(const SSL *s)
678 {
679 int ret= -1;
680 BIO *b,*r;
681
682 b=SSL_get_rbio(s);
683 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
684 if (r != NULL)
685 BIO_get_fd(r,&ret);
686 return(ret);
687 }
688
689int SSL_get_wfd(const SSL *s)
690 {
691 int ret= -1;
692 BIO *b,*r;
693
694 b=SSL_get_wbio(s);
695 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
696 if (r != NULL)
697 BIO_get_fd(r,&ret);
698 return(ret);
699 }
700
701#ifndef OPENSSL_NO_SOCK
702int SSL_set_fd(SSL *s,int fd)
703 {
704 int ret=0;
705 BIO *bio=NULL;
706
707 bio=BIO_new(BIO_s_fd());
708
709 if (bio == NULL)
710 {
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;
717err:
718 return(ret);
719 }
720
721int SSL_set_wfd(SSL *s,int fd)
722 {
723 int ret=0;
724 BIO *bio=NULL;
725
726 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
727 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
728 {
729 bio=BIO_new(BIO_s_fd());
730
731 if (bio == NULL)
732 {
733 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
734 goto err;
735 }
736 BIO_set_fd(bio,fd,BIO_NOCLOSE);
737 SSL_set_bio(s,SSL_get_rbio(s),bio);
738 }
739 else
740 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
741 ret=1;
742err:
743 return(ret);
744 }
745
746int SSL_set_rfd(SSL *s,int fd)
747 {
748 int ret=0;
749 BIO *bio=NULL;
750
751 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
752 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
753 {
754 bio=BIO_new(BIO_s_fd());
755
756 if (bio == NULL)
757 {
758 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
759 goto err;
760 }
761 BIO_set_fd(bio,fd,BIO_NOCLOSE);
762 SSL_set_bio(s,bio,SSL_get_wbio(s));
763 }
764 else
765 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
766 ret=1;
767err:
768 return(ret);
769 }
770#endif
771
772
773/* return length of latest Finished message we sent, copy to 'buf' */
774size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
775 {
776 size_t ret = 0;
777
778 if (s->s3 != NULL)
779 {
780 ret = s->s3->tmp.finish_md_len;
781 if (count > ret)
782 count = ret;
783 memcpy(buf, s->s3->tmp.finish_md, count);
784 }
785 return ret;
786 }
787
788/* return length of latest Finished message we expected, copy to 'buf' */
789size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
790 {
791 size_t ret = 0;
792
793 if (s->s3 != NULL)
794 {
795 ret = s->s3->tmp.peer_finish_md_len;
796 if (count > ret)
797 count = ret;
798 memcpy(buf, s->s3->tmp.peer_finish_md, count);
799 }
800 return ret;
801 }
802
803
804int SSL_get_verify_mode(const SSL *s)
805 {
806 return(s->verify_mode);
807 }
808
809int SSL_get_verify_depth(const SSL *s)
810 {
811 return X509_VERIFY_PARAM_get_depth(s->param);
812 }
813
814int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
815 {
816 return(s->verify_callback);
817 }
818
819int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
820 {
821 return(ctx->verify_mode);
822 }
823
824int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
825 {
826 return X509_VERIFY_PARAM_get_depth(ctx->param);
827 }
828
829int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
830 {
831 return(ctx->default_verify_callback);
832 }
833
834void SSL_set_verify(SSL *s,int mode,
835 int (*callback)(int ok,X509_STORE_CTX *ctx))
836 {
837 s->verify_mode=mode;
838 if (callback != NULL)
839 s->verify_callback=callback;
840 }
841
842void SSL_set_verify_depth(SSL *s,int depth)
843 {
844 X509_VERIFY_PARAM_set_depth(s->param, depth);
845 }
846
847void SSL_set_read_ahead(SSL *s,int yes)
848 {
849 s->read_ahead=yes;
850 }
851
852int SSL_get_read_ahead(const SSL *s)
853 {
854 return(s->read_ahead);
855 }
856
857int SSL_pending(const SSL *s)
858 {
859 /* SSL_pending cannot work properly if read-ahead is enabled
860 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
861 * and it is impossible to fix since SSL_pending cannot report
862 * errors that may be observed while scanning the new data.
863 * (Note that SSL_pending() is often used as a boolean value,
864 * so we'd better not return -1.)
865 */
866 return(s->method->ssl_pending(s));
867 }
868
869X509 *SSL_get_peer_certificate(const SSL *s)
870 {
871 X509 *r;
872
873 if ((s == NULL) || (s->session == NULL))
874 r=NULL;
875 else
876 r=s->session->peer;
877
David Benjamin150c6172014-08-05 22:22:49 -0400878 if (r == NULL)
879 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700880
David Benjamin150c6172014-08-05 22:22:49 -0400881 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700882 }
883
884STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
885 {
886 STACK_OF(X509) *r;
887
888 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
889 r=NULL;
890 else
891 r=s->session->sess_cert->cert_chain;
892
893 /* If we are a client, cert_chain includes the peer's own
894 * certificate; if we are a server, it does not. */
895
896 return(r);
897 }
898
Adam Langley95c29f32014-06-20 12:00:00 -0700899/* Fix this so it checks all the valid key/cert options */
900int SSL_CTX_check_private_key(const SSL_CTX *ctx)
901 {
902 if ( (ctx == NULL) ||
903 (ctx->cert == NULL) ||
904 (ctx->cert->key->x509 == NULL))
905 {
906 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
907 return(0);
908 }
909 if (ctx->cert->key->privatekey == NULL)
910 {
911 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
912 return(0);
913 }
914 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
915 }
916
917/* Fix this function so that it takes an optional type parameter */
918int SSL_check_private_key(const SSL *ssl)
919 {
920 if (ssl == NULL)
921 {
922 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
923 return(0);
924 }
925 if (ssl->cert == NULL)
926 {
927 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
928 return 0;
929 }
930 if (ssl->cert->key->x509 == NULL)
931 {
932 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
933 return(0);
934 }
935 if (ssl->cert->key->privatekey == NULL)
936 {
937 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
938 return(0);
939 }
940 return(X509_check_private_key(ssl->cert->key->x509,
941 ssl->cert->key->privatekey));
942 }
943
944int SSL_accept(SSL *s)
945 {
946 if (s->handshake_func == 0)
947 /* Not properly initialized yet */
948 SSL_set_accept_state(s);
949
David Benjamin0b145c22014-11-26 20:10:09 -0500950 if (s->handshake_func != s->method->ssl_accept)
951 {
952 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
953 return -1;
954 }
955
956 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700957 }
958
959int SSL_connect(SSL *s)
960 {
961 if (s->handshake_func == 0)
962 /* Not properly initialized yet */
963 SSL_set_connect_state(s);
964
David Benjamin0b145c22014-11-26 20:10:09 -0500965 if (s->handshake_func != s->method->ssl_connect)
966 {
967 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
968 return -1;
969 }
970
971 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700972 }
973
974long SSL_get_default_timeout(const SSL *s)
975 {
David Benjaminf4501342014-08-14 17:24:37 -0400976 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -0700977 }
978
979int SSL_read(SSL *s,void *buf,int num)
980 {
981 if (s->handshake_func == 0)
982 {
983 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
984 return -1;
985 }
986
987 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
988 {
989 s->rwstate=SSL_NOTHING;
990 return(0);
991 }
992 return(s->method->ssl_read(s,buf,num));
993 }
994
995int SSL_peek(SSL *s,void *buf,int num)
996 {
997 if (s->handshake_func == 0)
998 {
999 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1000 return -1;
1001 }
1002
1003 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1004 {
1005 return(0);
1006 }
1007 return(s->method->ssl_peek(s,buf,num));
1008 }
1009
1010int SSL_write(SSL *s,const void *buf,int num)
1011 {
1012 if (s->handshake_func == 0)
1013 {
1014 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1015 return -1;
1016 }
1017
1018 if (s->shutdown & SSL_SENT_SHUTDOWN)
1019 {
1020 s->rwstate=SSL_NOTHING;
1021 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1022 return(-1);
1023 }
1024 return(s->method->ssl_write(s,buf,num));
1025 }
1026
1027int SSL_shutdown(SSL *s)
1028 {
1029 /* Note that this function behaves differently from what one might
1030 * expect. Return values are 0 for no success (yet),
1031 * 1 for success; but calling it once is usually not enough,
1032 * even if blocking I/O is used (see ssl3_shutdown).
1033 */
1034
1035 if (s->handshake_func == 0)
1036 {
1037 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1038 return -1;
1039 }
1040
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001041 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001042 return(s->method->ssl_shutdown(s));
1043 else
1044 return(1);
1045 }
1046
1047int SSL_renegotiate(SSL *s)
1048 {
1049 if (s->renegotiate == 0)
1050 s->renegotiate=1;
1051
1052 s->new_session=1;
1053
1054 return(s->method->ssl_renegotiate(s));
1055 }
1056
1057int SSL_renegotiate_abbreviated(SSL *s)
1058 {
1059 if (s->renegotiate == 0)
1060 s->renegotiate=1;
1061
1062 s->new_session=0;
1063
1064 return(s->method->ssl_renegotiate(s));
1065 }
1066
1067int SSL_renegotiate_pending(SSL *s)
1068 {
1069 /* becomes true when negotiation is requested;
1070 * false again once a handshake has finished */
1071 return (s->renegotiate != 0);
1072 }
1073
1074long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1075 {
1076 long l;
1077
1078 switch (cmd)
1079 {
1080 case SSL_CTRL_GET_READ_AHEAD:
1081 return(s->read_ahead);
1082 case SSL_CTRL_SET_READ_AHEAD:
1083 l=s->read_ahead;
1084 s->read_ahead=larg;
1085 return(l);
1086
1087 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1088 s->msg_callback_arg = parg;
1089 return 1;
1090
1091 case SSL_CTRL_OPTIONS:
1092 return(s->options|=larg);
1093 case SSL_CTRL_CLEAR_OPTIONS:
1094 return(s->options&=~larg);
1095 case SSL_CTRL_MODE:
1096 return(s->mode|=larg);
1097 case SSL_CTRL_CLEAR_MODE:
1098 return(s->mode &=~larg);
1099 case SSL_CTRL_GET_MAX_CERT_LIST:
1100 return(s->max_cert_list);
1101 case SSL_CTRL_SET_MAX_CERT_LIST:
1102 l=s->max_cert_list;
1103 s->max_cert_list=larg;
1104 return(l);
1105 case SSL_CTRL_SET_MTU:
Adam Langley95c29f32014-06-20 12:00:00 -07001106 if (larg < (long)dtls1_min_mtu())
1107 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001108 if (SSL_IS_DTLS(s))
1109 {
1110 s->d1->mtu = larg;
1111 return larg;
1112 }
1113 return 0;
1114 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1115 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1116 return 0;
1117 s->max_send_fragment = larg;
1118 return 1;
1119 case SSL_CTRL_GET_RI_SUPPORT:
1120 if (s->s3)
1121 return s->s3->send_connection_binding;
1122 else return 0;
1123 case SSL_CTRL_CERT_FLAGS:
1124 return(s->cert->cert_flags|=larg);
1125 case SSL_CTRL_CLEAR_CERT_FLAGS:
1126 return(s->cert->cert_flags &=~larg);
1127
1128 case SSL_CTRL_GET_RAW_CIPHERLIST:
1129 if (parg)
1130 {
1131 if (s->cert->ciphers_raw == NULL)
1132 return 0;
1133 *(unsigned char **)parg = s->cert->ciphers_raw;
1134 return (int)s->cert->ciphers_rawlen;
1135 }
1136 else
David Benjamin39482a12014-07-20 13:30:15 -04001137 {
1138 /* Passing a NULL |parg| returns the size of a single
1139 * cipher suite value. */
1140 return 2;
1141 }
Adam Langley95c29f32014-06-20 12:00:00 -07001142 default:
1143 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1144 }
1145 }
1146
1147long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1148 {
1149 switch(cmd)
1150 {
1151 case SSL_CTRL_SET_MSG_CALLBACK:
1152 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1153 return 1;
1154
1155 default:
1156 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1157 }
1158 }
1159
1160LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1161 {
1162 return ctx->sessions;
1163 }
1164
1165long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1166 {
1167 long l;
1168
1169 switch (cmd)
1170 {
1171 case SSL_CTRL_GET_READ_AHEAD:
1172 return(ctx->read_ahead);
1173 case SSL_CTRL_SET_READ_AHEAD:
1174 l=ctx->read_ahead;
1175 ctx->read_ahead=larg;
1176 return(l);
1177
1178 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1179 ctx->msg_callback_arg = parg;
1180 return 1;
1181
1182 case SSL_CTRL_GET_MAX_CERT_LIST:
1183 return(ctx->max_cert_list);
1184 case SSL_CTRL_SET_MAX_CERT_LIST:
1185 l=ctx->max_cert_list;
1186 ctx->max_cert_list=larg;
1187 return(l);
1188
1189 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1190 l=ctx->session_cache_size;
1191 ctx->session_cache_size=larg;
1192 return(l);
1193 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1194 return(ctx->session_cache_size);
1195 case SSL_CTRL_SET_SESS_CACHE_MODE:
1196 l=ctx->session_cache_mode;
1197 ctx->session_cache_mode=larg;
1198 return(l);
1199 case SSL_CTRL_GET_SESS_CACHE_MODE:
1200 return(ctx->session_cache_mode);
1201
1202 case SSL_CTRL_SESS_NUMBER:
1203 return(lh_SSL_SESSION_num_items(ctx->sessions));
1204 case SSL_CTRL_SESS_CONNECT:
1205 return(ctx->stats.sess_connect);
1206 case SSL_CTRL_SESS_CONNECT_GOOD:
1207 return(ctx->stats.sess_connect_good);
1208 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1209 return(ctx->stats.sess_connect_renegotiate);
1210 case SSL_CTRL_SESS_ACCEPT:
1211 return(ctx->stats.sess_accept);
1212 case SSL_CTRL_SESS_ACCEPT_GOOD:
1213 return(ctx->stats.sess_accept_good);
1214 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1215 return(ctx->stats.sess_accept_renegotiate);
1216 case SSL_CTRL_SESS_HIT:
1217 return(ctx->stats.sess_hit);
1218 case SSL_CTRL_SESS_CB_HIT:
1219 return(ctx->stats.sess_cb_hit);
1220 case SSL_CTRL_SESS_MISSES:
1221 return(ctx->stats.sess_miss);
1222 case SSL_CTRL_SESS_TIMEOUTS:
1223 return(ctx->stats.sess_timeout);
1224 case SSL_CTRL_SESS_CACHE_FULL:
1225 return(ctx->stats.sess_cache_full);
1226 case SSL_CTRL_OPTIONS:
1227 return(ctx->options|=larg);
1228 case SSL_CTRL_CLEAR_OPTIONS:
1229 return(ctx->options&=~larg);
1230 case SSL_CTRL_MODE:
1231 return(ctx->mode|=larg);
1232 case SSL_CTRL_CLEAR_MODE:
1233 return(ctx->mode&=~larg);
1234 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1235 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1236 return 0;
1237 ctx->max_send_fragment = larg;
1238 return 1;
1239 case SSL_CTRL_CERT_FLAGS:
1240 return(ctx->cert->cert_flags|=larg);
1241 case SSL_CTRL_CLEAR_CERT_FLAGS:
1242 return(ctx->cert->cert_flags &=~larg);
1243 default:
1244 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1245 }
1246 }
1247
1248long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1249 {
1250 switch(cmd)
1251 {
1252 case SSL_CTRL_SET_MSG_CALLBACK:
1253 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1254 return 1;
1255
1256 default:
1257 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1258 }
1259 }
1260
1261int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1262 {
1263 long l;
1264 const SSL_CIPHER *a = in_a;
1265 const SSL_CIPHER *b = in_b;
1266 const long a_id = a->id;
1267 const long b_id = b->id;
1268
1269 l = a_id - b_id;
1270 if (l == 0L)
1271 return(0);
1272 else
1273 return((l > 0)?1:-1);
1274 }
1275
1276int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1277 {
1278 long l;
1279 const long a_id = (*ap)->id;
1280 const long b_id = (*bp)->id;
1281
1282 l = a_id - b_id;
1283 if (l == 0)
1284 return(0);
1285 else
1286 return((l > 0)?1:-1);
1287 }
1288
1289/** return a STACK of the ciphers available for the SSL and in order of
1290 * preference */
1291STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1292 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001293 if (s == NULL)
1294 return NULL;
1295
1296 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001297 {
Adam Langley858a88d2014-06-20 12:00:00 -07001298 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001299 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001300
1301 if (s->version >= TLS1_1_VERSION)
1302 {
1303 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001304 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001305 }
1306
1307 if ((s->ctx != NULL) &&
1308 (s->ctx->cipher_list != NULL))
1309 {
Adam Langley858a88d2014-06-20 12:00:00 -07001310 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001311 }
1312
Adam Langley95c29f32014-06-20 12:00:00 -07001313 return(NULL);
1314 }
1315
1316/** return a STACK of the ciphers available for the SSL and in order of
1317 * algorithm id */
1318STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1319 {
1320 if (s != NULL)
1321 {
1322 if (s->cipher_list_by_id != NULL)
1323 {
1324 return(s->cipher_list_by_id);
1325 }
1326 else if ((s->ctx != NULL) &&
1327 (s->ctx->cipher_list_by_id != NULL))
1328 {
1329 return(s->ctx->cipher_list_by_id);
1330 }
1331 }
1332 return(NULL);
1333 }
1334
1335/** The old interface to get the same thing as SSL_get_ciphers() */
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001336const char *SSL_get_cipher_list(const SSL *s, int n)
Adam Langley95c29f32014-06-20 12:00:00 -07001337 {
David Benjamin6f260012014-08-15 13:49:12 -04001338 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001339 STACK_OF(SSL_CIPHER) *sk;
1340
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001341 if (s == NULL)
1342 return NULL;
1343 sk = SSL_get_ciphers(s);
1344 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk))
1345 return NULL;
1346 c = sk_SSL_CIPHER_value(sk, n);
1347 if (c == NULL)
1348 return NULL;
1349 return c->name;
Adam Langley95c29f32014-06-20 12:00:00 -07001350 }
1351
1352/** specify the ciphers to be used by default by the SSL_CTX */
1353int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1354 {
1355 STACK_OF(SSL_CIPHER) *sk;
1356
1357 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1358 &ctx->cipher_list_by_id,str, ctx->cert);
1359 /* ssl_create_cipher_list may return an empty stack if it
1360 * was unable to find a cipher matching the given rule string
1361 * (for example if the rule string specifies a cipher which
1362 * has been disabled). This is not an error as far as
1363 * ssl_create_cipher_list is concerned, and hence
1364 * ctx->cipher_list and ctx->cipher_list_by_id has been
1365 * updated. */
1366 if (sk == NULL)
1367 return 0;
1368 else if (sk_SSL_CIPHER_num(sk) == 0)
1369 {
1370 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1371 return 0;
1372 }
1373 return 1;
1374 }
1375
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001376int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1377 {
1378 STACK_OF(SSL_CIPHER) *sk;
1379
1380 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1381 if (sk == NULL)
1382 return 0;
1383 else if (sk_SSL_CIPHER_num(sk) == 0)
1384 {
1385 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1386 return 0;
1387 }
1388 return 1;
1389 }
1390
Adam Langley95c29f32014-06-20 12:00:00 -07001391/** specify the ciphers to be used by the SSL */
1392int SSL_set_cipher_list(SSL *s,const char *str)
1393 {
1394 STACK_OF(SSL_CIPHER) *sk;
1395
1396 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1397 &s->cipher_list_by_id,str, s->cert);
1398 /* see comment in SSL_CTX_set_cipher_list */
1399 if (sk == NULL)
1400 return 0;
1401 else if (sk_SSL_CIPHER_num(sk) == 0)
1402 {
1403 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1404 return 0;
1405 }
1406 return 1;
1407 }
1408
David Benjamin5491e3f2014-09-29 19:33:09 -04001409int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001410 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001411 size_t i;
David Benjamin6f260012014-08-15 13:49:12 -04001412 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001413 CERT *ct = s->cert;
David Benjamin5491e3f2014-09-29 19:33:09 -04001414 uint8_t *q;
Adam Langley95c29f32014-06-20 12:00:00 -07001415 /* Set disabled masks for this session */
1416 ssl_set_client_disabled(s);
1417
1418 if (sk == NULL) return(0);
1419 q=p;
1420
1421 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1422 {
1423 c=sk_SSL_CIPHER_value(sk,i);
1424 /* Skip disabled ciphers */
1425 if (c->algorithm_ssl & ct->mask_ssl ||
1426 c->algorithm_mkey & ct->mask_k ||
1427 c->algorithm_auth & ct->mask_a)
1428 continue;
David Benjamin39482a12014-07-20 13:30:15 -04001429 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001430 }
David Benjamin5491e3f2014-09-29 19:33:09 -04001431 /* If all ciphers were disabled, return the error to the caller. */
1432 if (p == q)
Adam Langley95c29f32014-06-20 12:00:00 -07001433 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001434 return 0;
1435 }
1436
1437 /* Add SCSVs. */
1438 if (!s->renegotiate)
1439 {
1440 s2n(SSL3_CK_SCSV & 0xffff, p);
1441 }
1442 if (s->fallback_scsv)
1443 {
1444 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001445 }
1446
1447 return(p-q);
1448 }
1449
David Benjamin9f2c0d72014-10-21 22:00:19 -04001450STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001451 {
David Benjamin39482a12014-07-20 13:30:15 -04001452 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001453 const SSL_CIPHER *c;
1454 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001455
Adam Langley95c29f32014-06-20 12:00:00 -07001456 if (s->s3)
1457 s->s3->send_connection_binding = 0;
1458
David Benjamin39482a12014-07-20 13:30:15 -04001459 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001460 {
1461 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001462 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001463 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001464
1465 sk = sk_SSL_CIPHER_new_null();
1466 if (sk == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001467 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001468 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1469 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001470 }
1471
David Benjamin39482a12014-07-20 13:30:15 -04001472 if (!CBS_stow(&cipher_suites,
1473 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001474 {
1475 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1476 goto err;
1477 }
Adam Langley95c29f32014-06-20 12:00:00 -07001478
David Benjamin39482a12014-07-20 13:30:15 -04001479 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001480 {
David Benjamin39482a12014-07-20 13:30:15 -04001481 uint16_t cipher_suite;
1482
1483 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1484 {
1485 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1486 goto err;
1487 }
1488
David Benjamin9f2c0d72014-10-21 22:00:19 -04001489 /* Check for SCSV. */
David Benjamin39482a12014-07-20 13:30:15 -04001490 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001491 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001492 /* SCSV is fatal if renegotiating. */
Adam Langley95c29f32014-06-20 12:00:00 -07001493 if (s->renegotiate)
1494 {
1495 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1496 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1497 goto err;
1498 }
1499 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001500 continue;
1501 }
1502
David Benjamin9f2c0d72014-10-21 22:00:19 -04001503 /* Check for FALLBACK_SCSV. */
1504 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff))
Adam Langley29707792014-06-20 12:00:00 -07001505 {
David Benjaminceb6f282014-12-07 23:56:19 -05001506 uint16_t max_version = ssl3_get_max_server_version(s);
1507 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1508 : (uint16_t)s->version < max_version)
David Benjamin9f2c0d72014-10-21 22:00:19 -04001509 {
1510 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1511 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1512 goto err;
1513 }
1514 continue;
Adam Langley29707792014-06-20 12:00:00 -07001515 }
1516
David Benjamin39482a12014-07-20 13:30:15 -04001517 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001518 if (c != NULL)
1519 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001520 if (!sk_SSL_CIPHER_push(sk, c))
Adam Langley95c29f32014-06-20 12:00:00 -07001521 {
1522 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1523 goto err;
1524 }
1525 }
1526 }
1527
David Benjamin9f2c0d72014-10-21 22:00:19 -04001528 return sk;
1529
Adam Langley95c29f32014-06-20 12:00:00 -07001530err:
David Benjamin9f2c0d72014-10-21 22:00:19 -04001531 if (sk != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001532 sk_SSL_CIPHER_free(sk);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001533 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001534 }
1535
1536
Adam Langley95c29f32014-06-20 12:00:00 -07001537/** return a servername extension value if provided in Client Hello, or NULL.
1538 * So far, only host_name types are defined (RFC 3546).
1539 */
1540
1541const char *SSL_get_servername(const SSL *s, const int type)
1542 {
1543 if (type != TLSEXT_NAMETYPE_host_name)
1544 return NULL;
1545
1546 return s->session && !s->tlsext_hostname ?
1547 s->session->tlsext_hostname :
1548 s->tlsext_hostname;
1549 }
1550
1551int SSL_get_servername_type(const SSL *s)
1552 {
1553 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1554 return TLSEXT_NAMETYPE_host_name;
1555 return -1;
1556 }
1557
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001558void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1559 {
1560 ctx->signed_cert_timestamps_enabled = 1;
1561 }
1562
1563int SSL_enable_signed_cert_timestamps(SSL *ssl)
1564 {
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001565 ssl->signed_cert_timestamps_enabled = 1;
1566 return 1;
1567 }
1568
David Benjamin6c7aed02014-08-27 16:42:38 -04001569void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1570 {
1571 ctx->ocsp_stapling_enabled = 1;
1572 }
1573
1574int SSL_enable_ocsp_stapling(SSL *ssl)
1575 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001576 ssl->ocsp_stapling_enabled = 1;
1577 return 1;
1578 }
1579
David Benjamin52d699f2014-11-25 01:35:18 -05001580void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out, size_t *out_len)
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001581 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001582 SSL_SESSION *session = ssl->session;
1583
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001584 *out_len = 0;
1585 *out = NULL;
1586 if (ssl->server)
1587 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001588 if (!session || !session->tlsext_signed_cert_timestamp_list)
1589 return;
1590 *out = session->tlsext_signed_cert_timestamp_list;
1591 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1592 }
1593
David Benjamin52d699f2014-11-25 01:35:18 -05001594void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, size_t *out_len)
David Benjamin6c7aed02014-08-27 16:42:38 -04001595 {
1596 SSL_SESSION *session = ssl->session;
1597
1598 *out_len = 0;
1599 *out = NULL;
1600 if (ssl->server)
1601 return;
1602 if (!session || !session->ocsp_response)
1603 return;
1604 *out = session->ocsp_response;
1605 *out_len = session->ocsp_response_length;
1606 }
1607
Adam Langley95c29f32014-06-20 12:00:00 -07001608/* SSL_select_next_proto implements the standard protocol selection. It is
1609 * expected that this function is called from the callback set by
1610 * SSL_CTX_set_next_proto_select_cb.
1611 *
1612 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1613 * strings. The length byte itself is not included in the length. A byte
1614 * string of length 0 is invalid. No byte string may be truncated.
1615 *
1616 * The current, but experimental algorithm for selecting the protocol is:
1617 *
1618 * 1) If the server doesn't support NPN then this is indicated to the
1619 * callback. In this case, the client application has to abort the connection
1620 * or have a default application level protocol.
1621 *
1622 * 2) If the server supports NPN, but advertises an empty list then the
1623 * client selects the first protcol in its list, but indicates via the
1624 * API that this fallback case was enacted.
1625 *
1626 * 3) Otherwise, the client finds the first protocol in the server's list
1627 * that it supports and selects this protocol. This is because it's
1628 * assumed that the server has better information about which protocol
1629 * a client should use.
1630 *
1631 * 4) If the client doesn't support any of the server's advertised
1632 * protocols, then this is treated the same as case 2.
1633 *
1634 * It returns either
1635 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1636 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1637 */
1638int SSL_select_next_proto(unsigned char **out, unsigned char *outlen, const unsigned char *server, unsigned int server_len, const unsigned char *client, unsigned int client_len)
1639 {
1640 unsigned int i, j;
1641 const unsigned char *result;
1642 int status = OPENSSL_NPN_UNSUPPORTED;
1643
1644 /* For each protocol in server preference order, see if we support it. */
1645 for (i = 0; i < server_len; )
1646 {
1647 for (j = 0; j < client_len; )
1648 {
1649 if (server[i] == client[j] &&
1650 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1651 {
1652 /* We found a match */
1653 result = &server[i];
1654 status = OPENSSL_NPN_NEGOTIATED;
1655 goto found;
1656 }
1657 j += client[j];
1658 j++;
1659 }
1660 i += server[i];
1661 i++;
1662 }
1663
1664 /* There's no overlap between our protocols and the server's list. */
1665 result = client;
1666 status = OPENSSL_NPN_NO_OVERLAP;
1667
1668 found:
1669 *out = (unsigned char *) result + 1;
1670 *outlen = result[0];
1671 return status;
1672 }
1673
Adam Langley95c29f32014-06-20 12:00:00 -07001674/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1675 * requested protocol for this connection and returns 0. If the client didn't
1676 * request any protocol, then *data is set to NULL.
1677 *
1678 * Note that the client can request any protocol it chooses. The value returned
1679 * from this function need not be a member of the list of supported protocols
1680 * provided by the callback.
1681 */
David Benjaminced551f2014-07-11 23:16:16 -04001682void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001683 {
1684 *data = s->next_proto_negotiated;
1685 if (!*data) {
1686 *len = 0;
1687 } else {
1688 *len = s->next_proto_negotiated_len;
1689 }
1690}
1691
1692/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1693 * TLS server needs a list of supported protocols for Next Protocol
1694 * Negotiation. The returned list must be in wire format. The list is returned
1695 * by setting |out| to point to it and |outlen| to its length. This memory will
1696 * not be modified, but one should assume that the SSL* keeps a reference to
1697 * it.
1698 *
1699 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1700 * such extension will be included in the ServerHello. */
1701void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg), void *arg)
1702 {
1703 ctx->next_protos_advertised_cb = cb;
1704 ctx->next_protos_advertised_cb_arg = arg;
1705 }
1706
1707/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1708 * client needs to select a protocol from the server's provided list. |out|
1709 * must be set to point to the selected protocol (which may be within |in|).
1710 * The length of the protocol name must be written into |outlen|. The server's
1711 * advertised protocols are provided in |in| and |inlen|. The callback can
1712 * assume that |in| is syntactically valid.
1713 *
1714 * The client must select a protocol. It is fatal to the connection if this
1715 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1716 */
1717void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg), void *arg)
1718 {
1719 ctx->next_proto_select_cb = cb;
1720 ctx->next_proto_select_cb_arg = arg;
1721 }
Adam Langley95c29f32014-06-20 12:00:00 -07001722
Adam Langley95c29f32014-06-20 12:00:00 -07001723/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1724 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1725 * length-prefixed strings).
1726 *
1727 * Returns 0 on success. */
1728int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1729 unsigned protos_len)
1730 {
1731 if (ctx->alpn_client_proto_list)
1732 OPENSSL_free(ctx->alpn_client_proto_list);
1733
David Benjamin072c9532014-07-26 11:44:25 -04001734 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001735 if (!ctx->alpn_client_proto_list)
1736 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001737 ctx->alpn_client_proto_list_len = protos_len;
1738
1739 return 0;
1740 }
1741
1742/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1743 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1744 * length-prefixed strings).
1745 *
1746 * Returns 0 on success. */
1747int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1748 unsigned protos_len)
1749 {
1750 if (ssl->alpn_client_proto_list)
1751 OPENSSL_free(ssl->alpn_client_proto_list);
1752
David Benjamin072c9532014-07-26 11:44:25 -04001753 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001754 if (!ssl->alpn_client_proto_list)
1755 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001756 ssl->alpn_client_proto_list_len = protos_len;
1757
1758 return 0;
1759 }
1760
1761/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1762 * during ClientHello processing in order to select an ALPN protocol from the
1763 * client's list of offered protocols. */
1764void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1765 int (*cb) (SSL *ssl,
1766 const unsigned char **out,
1767 unsigned char *outlen,
1768 const unsigned char *in,
1769 unsigned int inlen,
1770 void *arg),
1771 void *arg)
1772 {
1773 ctx->alpn_select_cb = cb;
1774 ctx->alpn_select_cb_arg = arg;
1775 }
1776
1777/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1778 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1779 * including the leading length-prefix byte). If the server didn't respond with
1780 * a negotiated protocol then |*len| will be zero. */
1781void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1782 unsigned *len)
1783 {
1784 *data = NULL;
1785 if (ssl->s3)
1786 *data = ssl->s3->alpn_selected;
1787 if (*data == NULL)
1788 *len = 0;
1789 else
1790 *len = ssl->s3->alpn_selected_len;
1791 }
Adam Langley95c29f32014-06-20 12:00:00 -07001792
1793int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1794 const char *label, size_t llen, const unsigned char *p, size_t plen,
1795 int use_context)
1796 {
1797 if (s->version < TLS1_VERSION)
1798 return -1;
1799
David Benjamine99e9122014-12-11 01:46:01 -05001800 return s->enc_method->export_keying_material(s, out, olen, label,
Adam Langley95c29f32014-06-20 12:00:00 -07001801 llen, p, plen,
1802 use_context);
1803 }
1804
1805static uint32_t ssl_session_hash(const SSL_SESSION *a)
1806 {
1807 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1808 ((uint32_t) a->session_id[1] << 8) ||
1809 ((uint32_t) a->session_id[2] << 16) ||
1810 ((uint32_t) a->session_id[3] << 24);
1811
1812 return hash;
1813 }
1814
1815/* NB: If this function (or indeed the hash function which uses a sort of
1816 * coarser function than this one) is changed, ensure
1817 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1818 * able to construct an SSL_SESSION that will collide with any existing session
1819 * with a matching session ID. */
1820static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1821 {
1822 if (a->ssl_version != b->ssl_version)
1823 return(1);
1824 if (a->session_id_length != b->session_id_length)
1825 return(1);
1826 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1827 }
1828
1829SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1830 {
1831 SSL_CTX *ret=NULL;
1832
1833 if (meth == NULL)
1834 {
1835 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1836 return(NULL);
1837 }
1838
Adam Langley95c29f32014-06-20 12:00:00 -07001839 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1840 {
1841 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1842 goto err;
1843 }
1844 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1845 if (ret == NULL)
1846 goto err;
1847
1848 memset(ret,0,sizeof(SSL_CTX));
1849
1850 ret->method=meth;
1851
1852 ret->cert_store=NULL;
1853 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1854 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1855 ret->session_cache_head=NULL;
1856 ret->session_cache_tail=NULL;
1857
1858 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001859 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001860
1861 ret->new_session_cb=0;
1862 ret->remove_session_cb=0;
1863 ret->get_session_cb=0;
1864 ret->generate_session_id=0;
1865
1866 memset((char *)&ret->stats,0,sizeof(ret->stats));
1867
1868 ret->references=1;
1869 ret->quiet_shutdown=0;
1870
Adam Langley95c29f32014-06-20 12:00:00 -07001871 ret->info_callback=NULL;
1872
1873 ret->app_verify_callback=0;
1874 ret->app_verify_arg=NULL;
1875
1876 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1877 ret->read_ahead=0;
1878 ret->msg_callback=0;
1879 ret->msg_callback_arg=NULL;
1880 ret->verify_mode=SSL_VERIFY_NONE;
1881#if 0
1882 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1883#endif
1884 ret->sid_ctx_length=0;
1885 ret->default_verify_callback=NULL;
1886 if ((ret->cert=ssl_cert_new()) == NULL)
1887 goto err;
1888
1889 ret->default_passwd_callback=0;
1890 ret->default_passwd_callback_userdata=NULL;
1891 ret->client_cert_cb=0;
1892 ret->app_gen_cookie_cb=0;
1893 ret->app_verify_cookie_cb=0;
1894
1895 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1896 if (ret->sessions == NULL) goto err;
1897 ret->cert_store=X509_STORE_new();
1898 if (ret->cert_store == NULL) goto err;
1899
1900 ssl_create_cipher_list(ret->method,
1901 &ret->cipher_list,&ret->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -05001902 SSL_DEFAULT_CIPHER_LIST, ret->cert);
Adam Langley95c29f32014-06-20 12:00:00 -07001903 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001904 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001905 {
1906 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1907 goto err2;
1908 }
1909
1910 ret->param = X509_VERIFY_PARAM_new();
1911 if (!ret->param)
1912 goto err;
1913
Adam Langley95c29f32014-06-20 12:00:00 -07001914 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1915 goto err;
1916
1917 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1918
1919 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001920
1921 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1922
Adam Langley95c29f32014-06-20 12:00:00 -07001923 ret->tlsext_servername_callback = 0;
1924 ret->tlsext_servername_arg = NULL;
1925 /* Setup RFC4507 ticket keys */
1926 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1927 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1928 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1929 ret->options |= SSL_OP_NO_TICKET;
1930
1931 ret->tlsext_status_cb = 0;
1932 ret->tlsext_status_arg = NULL;
1933
Adam Langley95c29f32014-06-20 12:00:00 -07001934 ret->next_protos_advertised_cb = 0;
1935 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001936 ret->psk_identity_hint=NULL;
1937 ret->psk_client_callback=NULL;
1938 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04001939
Adam Langley95c29f32014-06-20 12:00:00 -07001940 /* Default is to connect to non-RI servers. When RI is more widely
1941 * deployed might change this.
1942 */
1943 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1944
1945 return(ret);
1946err:
1947 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
1948err2:
1949 if (ret != NULL) SSL_CTX_free(ret);
1950 return(NULL);
1951 }
1952
Adam Langley95c29f32014-06-20 12:00:00 -07001953
1954void SSL_CTX_free(SSL_CTX *a)
1955 {
1956 int i;
1957
1958 if (a == NULL) return;
1959
1960 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1961#ifdef REF_PRINT
1962 REF_PRINT("SSL_CTX",a);
1963#endif
1964 if (i > 0) return;
1965#ifdef REF_CHECK
1966 if (i < 0)
1967 {
1968 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1969 abort(); /* ok */
1970 }
1971#endif
1972
1973 if (a->param)
1974 X509_VERIFY_PARAM_free(a->param);
1975
1976 /*
1977 * Free internal session cache. However: the remove_cb() may reference
1978 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1979 * after the sessions were flushed.
1980 * As the ex_data handling routines might also touch the session cache,
1981 * the most secure solution seems to be: empty (flush) the cache, then
1982 * free ex_data, then finally free the cache.
1983 * (See ticket [openssl.org #212].)
1984 */
1985 if (a->sessions != NULL)
1986 SSL_CTX_flush_sessions(a,0);
1987
1988 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
1989
1990 if (a->sessions != NULL)
1991 lh_SSL_SESSION_free(a->sessions);
1992
1993 if (a->cert_store != NULL)
1994 X509_STORE_free(a->cert_store);
1995 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001996 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07001997 if (a->cipher_list_by_id != NULL)
1998 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001999 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002000 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002001 if (a->cert != NULL)
2002 ssl_cert_free(a->cert);
2003 if (a->client_CA != NULL)
2004 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2005 if (a->extra_certs != NULL)
2006 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002007
2008 if (a->srtp_profiles)
2009 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2010
Adam Langley95c29f32014-06-20 12:00:00 -07002011 if (a->psk_identity_hint)
2012 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002013
Adam Langley95c29f32014-06-20 12:00:00 -07002014 if (a->tlsext_ecpointformatlist)
2015 OPENSSL_free(a->tlsext_ecpointformatlist);
2016 if (a->tlsext_ellipticcurvelist)
2017 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002018 if (a->alpn_client_proto_list != NULL)
2019 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002020
Adam Langley1258b6a2014-06-20 12:00:00 -07002021 if (a->tlsext_channel_id_private)
2022 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002023
David Benjamin859ec3c2014-09-02 16:29:36 -04002024 if (a->keylog_bio)
2025 BIO_free(a->keylog_bio);
2026
Adam Langley95c29f32014-06-20 12:00:00 -07002027 OPENSSL_free(a);
2028 }
2029
2030void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2031 {
2032 ctx->default_passwd_callback=cb;
2033 }
2034
2035void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2036 {
2037 ctx->default_passwd_callback_userdata=u;
2038 }
2039
2040void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2041 {
2042 ctx->app_verify_callback=cb;
2043 ctx->app_verify_arg=arg;
2044 }
2045
2046void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2047 {
2048 ctx->verify_mode=mode;
2049 ctx->default_verify_callback=cb;
2050 }
2051
2052void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2053 {
2054 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2055 }
2056
2057void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2058 {
2059 ssl_cert_set_cert_cb(c->cert, cb, arg);
2060 }
2061
2062void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2063 {
2064 ssl_cert_set_cert_cb(s->cert, cb, arg);
2065 }
2066
David Benjamin033e5f42014-11-13 18:47:41 -05002067static int ssl_has_key(SSL *s, size_t idx)
2068 {
2069 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2070 return cpk->x509 && cpk->privatekey;
2071 }
2072
David Benjaminf31e6812014-11-13 18:05:55 -05002073void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
2074 unsigned long *out_mask_a)
Adam Langley95c29f32014-06-20 12:00:00 -07002075 {
David Benjaminf31e6812014-11-13 18:05:55 -05002076 CERT *c = s->cert;
David Benjaminf31e6812014-11-13 18:05:55 -05002077 int rsa_enc, rsa_sign, dh_tmp;
2078 unsigned long mask_k, mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002079 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002080 int have_ecdh_tmp;
David Benjaminf31e6812014-11-13 18:05:55 -05002081 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002082
David Benjaminf31e6812014-11-13 18:05:55 -05002083 if (c == NULL)
2084 {
2085 /* TODO(davidben): Is this codepath possible? */
2086 *out_mask_k = 0;
2087 *out_mask_a = 0;
2088 return;
2089 }
Adam Langley95c29f32014-06-20 12:00:00 -07002090
David Benjaminf31e6812014-11-13 18:05:55 -05002091 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002092
David Benjaminf31e6812014-11-13 18:05:55 -05002093 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
David Benjamin033e5f42014-11-13 18:47:41 -05002094 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2095 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2096 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
David Benjaminf31e6812014-11-13 18:05:55 -05002097 mask_k = 0;
2098 mask_a = 0;
2099
David Benjamin77a942b2014-07-15 01:22:50 -04002100 if (rsa_enc)
David Benjaminf31e6812014-11-13 18:05:55 -05002101 mask_k |= SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002102
Adam Langley95c29f32014-06-20 12:00:00 -07002103 if (dh_tmp)
David Benjaminf31e6812014-11-13 18:05:55 -05002104 mask_k |= SSL_kEDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002105
Adam Langley95c29f32014-06-20 12:00:00 -07002106 if (rsa_enc || rsa_sign)
2107 {
David Benjaminf31e6812014-11-13 18:05:55 -05002108 mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002109 }
2110
David Benjaminf31e6812014-11-13 18:05:55 -05002111 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002112
David Benjamin0da0e182014-08-19 16:20:28 -04002113 /* An ECC certificate may be usable for ECDSA cipher suites depending on
David Benjamin033e5f42014-11-13 18:47:41 -05002114 * the key usage extension and on the client's curve preferences. */
Adam Langley95c29f32014-06-20 12:00:00 -07002115 if (have_ecc_cert)
2116 {
David Benjamin033e5f42014-11-13 18:47:41 -05002117 x = c->pkeys[SSL_PKEY_ECC].x509;
Adam Langley95c29f32014-06-20 12:00:00 -07002118 /* This call populates extension flags (ex_flags) */
2119 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002120 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2121 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
David Benjamin033e5f42014-11-13 18:47:41 -05002122 if (!tls1_check_ec_cert(s, x))
Adam Langley95c29f32014-06-20 12:00:00 -07002123 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002124 if (ecdsa_ok)
2125 {
David Benjaminf31e6812014-11-13 18:05:55 -05002126 mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002127 }
Adam Langley95c29f32014-06-20 12:00:00 -07002128 }
Adam Langley95c29f32014-06-20 12:00:00 -07002129
David Benjaminf31e6812014-11-13 18:05:55 -05002130 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2131 * key, check it. */
2132 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002133 {
David Benjaminf31e6812014-11-13 18:05:55 -05002134 mask_k |= SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002135 }
Adam Langley95c29f32014-06-20 12:00:00 -07002136
David Benjaminf31e6812014-11-13 18:05:55 -05002137 /* PSK requires a server callback. */
2138 if (s->psk_server_callback != NULL)
2139 {
2140 mask_k |= SSL_kPSK;
2141 mask_a |= SSL_aPSK;
2142 }
Adam Langley95c29f32014-06-20 12:00:00 -07002143
David Benjaminf31e6812014-11-13 18:05:55 -05002144 *out_mask_k = mask_k;
2145 *out_mask_a = mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002146 }
2147
2148/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2149#define ku_reject(x, usage) \
2150 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2151
Adam Langley95c29f32014-06-20 12:00:00 -07002152
2153int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2154 {
David Benjamin0da0e182014-08-19 16:20:28 -04002155 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002156 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2157 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2158
Adam Langley95c29f32014-06-20 12:00:00 -07002159 alg_a = cs->algorithm_auth;
2160
Adam Langley95c29f32014-06-20 12:00:00 -07002161 /* This call populates the ex_flags field correctly */
2162 X509_check_purpose(x, -1, 0);
2163 if ((x->sig_alg) && (x->sig_alg->algorithm))
2164 {
2165 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2166 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2167 }
Adam Langley95c29f32014-06-20 12:00:00 -07002168 if (alg_a & SSL_aECDSA)
2169 {
2170 /* key usage, if present, must allow signing */
2171 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2172 {
2173 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2174 return 0;
2175 }
2176 }
2177
2178 return 1; /* all checks are ok */
2179 }
2180
Adam Langley95c29f32014-06-20 12:00:00 -07002181
2182static int ssl_get_server_cert_index(const SSL *s)
2183 {
2184 int idx;
2185 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2186 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2187 idx = SSL_PKEY_RSA_SIGN;
2188 if (idx == -1)
2189 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2190 return idx;
2191 }
2192
2193CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2194 {
David Benjaminf31e6812014-11-13 18:05:55 -05002195 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002196
2197 /* This may or may not be an error. */
2198 if (i < 0)
2199 return NULL;
2200
2201 /* May be NULL. */
David Benjaminf31e6812014-11-13 18:05:55 -05002202 return &s->cert->pkeys[i];
Adam Langley95c29f32014-06-20 12:00:00 -07002203 }
2204
David Benjaminec2f27d2014-11-13 19:17:25 -05002205EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher)
Adam Langley95c29f32014-06-20 12:00:00 -07002206 {
2207 unsigned long alg_a;
2208 CERT *c;
2209 int idx = -1;
2210
2211 alg_a = cipher->algorithm_auth;
2212 c=s->cert;
2213
David Benjaminef2116d2014-08-19 20:21:56 -04002214 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002215 {
2216 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2217 idx = SSL_PKEY_RSA_SIGN;
2218 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2219 idx = SSL_PKEY_RSA_ENC;
2220 }
2221 else if ((alg_a & SSL_aECDSA) &&
2222 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2223 idx = SSL_PKEY_ECC;
2224 if (idx == -1)
2225 {
2226 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2227 return(NULL);
2228 }
Adam Langley95c29f32014-06-20 12:00:00 -07002229 return c->pkeys[idx].privatekey;
2230 }
2231
Adam Langley95c29f32014-06-20 12:00:00 -07002232void ssl_update_cache(SSL *s,int mode)
2233 {
2234 int i;
2235
2236 /* If the session_id_length is 0, we are not supposed to cache it,
2237 * and it would be rather hard to do anyway :-) */
2238 if (s->session->session_id_length == 0) return;
2239
David Benjamin041b58a2014-08-27 15:25:21 -04002240 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002241 if ((i & mode) && (!s->hit)
2242 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002243 || SSL_CTX_add_session(s->initial_ctx,s->session))
2244 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002245 {
2246 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002247 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002248 SSL_SESSION_free(s->session);
2249 }
2250
2251 /* auto flush every 255 connections */
2252 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2253 ((i & mode) == mode))
2254 {
2255 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002256 ?s->initial_ctx->stats.sess_connect_good
2257 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002258 {
David Benjamin041b58a2014-08-27 15:25:21 -04002259 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002260 }
2261 }
2262 }
2263
Adam Langley95c29f32014-06-20 12:00:00 -07002264int SSL_get_error(const SSL *s,int i)
2265 {
2266 int reason;
2267 unsigned long l;
2268 BIO *bio;
2269
2270 if (i > 0) return(SSL_ERROR_NONE);
2271
2272 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2273 * etc, where we do encode the error */
2274 if ((l=ERR_peek_error()) != 0)
2275 {
2276 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2277 return(SSL_ERROR_SYSCALL);
2278 else
2279 return(SSL_ERROR_SSL);
2280 }
2281
Adam Langleyb2ce0582014-06-20 12:00:00 -07002282 if ((i < 0) && SSL_want_session(s))
2283 return(SSL_ERROR_PENDING_SESSION);
2284
Adam Langleydc9b1412014-06-20 12:00:00 -07002285 if ((i < 0) && SSL_want_certificate(s))
2286 return(SSL_ERROR_PENDING_CERTIFICATE);
2287
Adam Langley95c29f32014-06-20 12:00:00 -07002288 if ((i < 0) && SSL_want_read(s))
2289 {
2290 bio=SSL_get_rbio(s);
2291 if (BIO_should_read(bio))
2292 return(SSL_ERROR_WANT_READ);
2293 else if (BIO_should_write(bio))
2294 /* This one doesn't make too much sense ... We never try
2295 * to write to the rbio, and an application program where
2296 * rbio and wbio are separate couldn't even know what it
2297 * should wait for.
2298 * However if we ever set s->rwstate incorrectly
2299 * (so that we have SSL_want_read(s) instead of
2300 * SSL_want_write(s)) and rbio and wbio *are* the same,
2301 * this test works around that bug; so it might be safer
2302 * to keep it. */
2303 return(SSL_ERROR_WANT_WRITE);
2304 else if (BIO_should_io_special(bio))
2305 {
2306 reason=BIO_get_retry_reason(bio);
2307 if (reason == BIO_RR_CONNECT)
2308 return(SSL_ERROR_WANT_CONNECT);
2309 else if (reason == BIO_RR_ACCEPT)
2310 return(SSL_ERROR_WANT_ACCEPT);
2311 else
2312 return(SSL_ERROR_SYSCALL); /* unknown */
2313 }
2314 }
2315
2316 if ((i < 0) && SSL_want_write(s))
2317 {
2318 bio=SSL_get_wbio(s);
2319 if (BIO_should_write(bio))
2320 return(SSL_ERROR_WANT_WRITE);
2321 else if (BIO_should_read(bio))
2322 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2323 return(SSL_ERROR_WANT_READ);
2324 else if (BIO_should_io_special(bio))
2325 {
2326 reason=BIO_get_retry_reason(bio);
2327 if (reason == BIO_RR_CONNECT)
2328 return(SSL_ERROR_WANT_CONNECT);
2329 else if (reason == BIO_RR_ACCEPT)
2330 return(SSL_ERROR_WANT_ACCEPT);
2331 else
2332 return(SSL_ERROR_SYSCALL);
2333 }
2334 }
2335 if ((i < 0) && SSL_want_x509_lookup(s))
2336 {
2337 return(SSL_ERROR_WANT_X509_LOOKUP);
2338 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002339 if ((i < 0) && SSL_want_channel_id_lookup(s))
2340 {
2341 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2342 }
Adam Langley95c29f32014-06-20 12:00:00 -07002343
2344 if (i == 0)
2345 {
David Benjamin5e4f6e92014-11-17 03:23:24 -05002346 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2347 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
Adam Langley95c29f32014-06-20 12:00:00 -07002348 return(SSL_ERROR_ZERO_RETURN);
Adam Langley95c29f32014-06-20 12:00:00 -07002349 }
2350 return(SSL_ERROR_SYSCALL);
2351 }
2352
2353int SSL_do_handshake(SSL *s)
2354 {
2355 int ret=1;
2356
2357 if (s->handshake_func == NULL)
2358 {
2359 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2360 return(-1);
2361 }
2362
2363 s->method->ssl_renegotiate_check(s);
2364
David Benjamin7e237462014-12-11 18:13:28 -05002365 if (SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002366 {
2367 ret=s->handshake_func(s);
2368 }
2369 return(ret);
2370 }
2371
Adam Langley95c29f32014-06-20 12:00:00 -07002372void SSL_set_accept_state(SSL *s)
2373 {
2374 s->server=1;
2375 s->shutdown=0;
2376 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2377 s->handshake_func=s->method->ssl_accept;
2378 /* clear the current cipher */
2379 ssl_clear_cipher_ctx(s);
2380 ssl_clear_hash_ctx(&s->read_hash);
2381 ssl_clear_hash_ctx(&s->write_hash);
2382 }
2383
2384void SSL_set_connect_state(SSL *s)
2385 {
2386 s->server=0;
2387 s->shutdown=0;
2388 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2389 s->handshake_func=s->method->ssl_connect;
2390 /* clear the current cipher */
2391 ssl_clear_cipher_ctx(s);
2392 ssl_clear_hash_ctx(&s->read_hash);
2393 ssl_clear_hash_ctx(&s->write_hash);
2394 }
2395
2396int ssl_undefined_function(SSL *s)
2397 {
2398 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2399 return(0);
2400 }
2401
2402int ssl_undefined_void_function(void)
2403 {
2404 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2405 return(0);
2406 }
2407
2408int ssl_undefined_const_function(const SSL *s)
2409 {
2410 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2411 return(0);
2412 }
2413
Adam Langley0f4746e2014-08-13 12:26:32 -07002414static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002415 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002416 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002417 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002418 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002419 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002420 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002421 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002422 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002423 return("SSLv3");
Adam Langley95c29f32014-06-20 12:00:00 -07002424 else
2425 return("unknown");
2426 }
2427
Adam Langley0f4746e2014-08-13 12:26:32 -07002428const char *SSL_get_version(const SSL *s)
2429 {
2430 return ssl_get_version(s->version);
2431 }
2432
2433const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2434 {
2435 return ssl_get_version(sess->ssl_version);
2436 }
2437
Adam Langley95c29f32014-06-20 12:00:00 -07002438void ssl_clear_cipher_ctx(SSL *s)
2439 {
2440 if (s->enc_read_ctx != NULL)
2441 {
2442 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2443 OPENSSL_free(s->enc_read_ctx);
2444 s->enc_read_ctx=NULL;
2445 }
2446 if (s->enc_write_ctx != NULL)
2447 {
2448 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2449 OPENSSL_free(s->enc_write_ctx);
2450 s->enc_write_ctx=NULL;
2451 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002452 if (s->aead_read_ctx != NULL)
2453 {
2454 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2455 OPENSSL_free(s->aead_read_ctx);
2456 s->aead_read_ctx = NULL;
2457 }
2458 if (s->aead_write_ctx != NULL)
2459 {
2460 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2461 OPENSSL_free(s->aead_write_ctx);
2462 s->aead_write_ctx = NULL;
2463 }
Adam Langley95c29f32014-06-20 12:00:00 -07002464 }
2465
2466X509 *SSL_get_certificate(const SSL *s)
2467 {
2468 if (s->cert != NULL)
2469 return(s->cert->key->x509);
2470 else
2471 return(NULL);
2472 }
2473
2474EVP_PKEY *SSL_get_privatekey(const SSL *s)
2475 {
2476 if (s->cert != NULL)
2477 return(s->cert->key->privatekey);
2478 else
2479 return(NULL);
2480 }
2481
2482X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2483 {
2484 if (ctx->cert != NULL)
2485 return ctx->cert->key->x509;
2486 else
2487 return NULL;
2488 }
2489
2490EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2491 {
2492 if (ctx->cert != NULL)
2493 return ctx->cert->key->privatekey;
2494 else
2495 return NULL ;
2496 }
2497
2498const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2499 {
2500 if ((s->session != NULL) && (s->session->cipher != NULL))
2501 return(s->session->cipher);
2502 return(NULL);
2503 }
2504const void *SSL_get_current_compression(SSL *s)
2505 {
2506 return NULL;
2507 }
2508const void *SSL_get_current_expansion(SSL *s)
2509 {
2510 return NULL;
2511 }
2512
2513int ssl_init_wbio_buffer(SSL *s,int push)
2514 {
2515 BIO *bbio;
2516
2517 if (s->bbio == NULL)
2518 {
2519 bbio=BIO_new(BIO_f_buffer());
2520 if (bbio == NULL) return(0);
2521 s->bbio=bbio;
2522 }
2523 else
2524 {
2525 bbio=s->bbio;
2526 if (s->bbio == s->wbio)
2527 s->wbio=BIO_pop(s->wbio);
2528 }
2529 (void)BIO_reset(bbio);
2530/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2531 if (!BIO_set_read_buffer_size(bbio,1))
2532 {
2533 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2534 return(0);
2535 }
2536 if (push)
2537 {
2538 if (s->wbio != bbio)
2539 s->wbio=BIO_push(bbio,s->wbio);
2540 }
2541 else
2542 {
2543 if (s->wbio == bbio)
2544 s->wbio=BIO_pop(bbio);
2545 }
2546 return(1);
2547 }
2548
2549void ssl_free_wbio_buffer(SSL *s)
2550 {
2551 if (s->bbio == NULL) return;
2552
2553 if (s->bbio == s->wbio)
2554 {
2555 /* remove buffering */
2556 s->wbio=BIO_pop(s->wbio);
2557#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2558 assert(s->wbio != NULL);
2559#endif
2560 }
2561 BIO_free(s->bbio);
2562 s->bbio=NULL;
2563 }
2564
2565void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2566 {
2567 ctx->quiet_shutdown=mode;
2568 }
2569
2570int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2571 {
2572 return(ctx->quiet_shutdown);
2573 }
2574
2575void SSL_set_quiet_shutdown(SSL *s,int mode)
2576 {
2577 s->quiet_shutdown=mode;
2578 }
2579
2580int SSL_get_quiet_shutdown(const SSL *s)
2581 {
2582 return(s->quiet_shutdown);
2583 }
2584
2585void SSL_set_shutdown(SSL *s,int mode)
2586 {
2587 s->shutdown=mode;
2588 }
2589
2590int SSL_get_shutdown(const SSL *s)
2591 {
2592 return(s->shutdown);
2593 }
2594
2595int SSL_version(const SSL *s)
2596 {
2597 return(s->version);
2598 }
2599
2600SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2601 {
2602 return(ssl->ctx);
2603 }
2604
2605SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2606 {
2607 if (ssl->ctx == ctx)
2608 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002609 if (ctx == NULL)
2610 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002611 if (ssl->cert != NULL)
2612 ssl_cert_free(ssl->cert);
2613 ssl->cert = ssl_cert_dup(ctx->cert);
2614 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2615 if (ssl->ctx != NULL)
2616 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2617 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002618
2619 ssl->sid_ctx_length = ctx->sid_ctx_length;
2620 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2621 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2622
Adam Langley95c29f32014-06-20 12:00:00 -07002623 return(ssl->ctx);
2624 }
2625
2626#ifndef OPENSSL_NO_STDIO
2627int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2628 {
2629 return(X509_STORE_set_default_paths(ctx->cert_store));
2630 }
2631
2632int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2633 const char *CApath)
2634 {
2635 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2636 }
2637#endif
2638
2639void SSL_set_info_callback(SSL *ssl,
2640 void (*cb)(const SSL *ssl,int type,int val))
2641 {
2642 ssl->info_callback=cb;
2643 }
2644
2645/* One compiler (Diab DCC) doesn't like argument names in returned
2646 function pointer. */
2647void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2648 {
2649 return ssl->info_callback;
2650 }
2651
2652int SSL_state(const SSL *ssl)
2653 {
2654 return(ssl->state);
2655 }
2656
2657void SSL_set_state(SSL *ssl, int state)
2658 {
2659 ssl->state = state;
2660 }
2661
2662void SSL_set_verify_result(SSL *ssl,long arg)
2663 {
2664 ssl->verify_result=arg;
2665 }
2666
2667long SSL_get_verify_result(const SSL *ssl)
2668 {
2669 return(ssl->verify_result);
2670 }
2671
2672int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2673 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2674 {
2675 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2676 new_func, dup_func, free_func);
2677 }
2678
2679int SSL_set_ex_data(SSL *s,int idx,void *arg)
2680 {
2681 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2682 }
2683
2684void *SSL_get_ex_data(const SSL *s,int idx)
2685 {
2686 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2687 }
2688
2689int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2690 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2691 {
2692 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2693 new_func, dup_func, free_func);
2694 }
2695
2696int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2697 {
2698 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2699 }
2700
2701void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2702 {
2703 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2704 }
2705
2706int ssl_ok(SSL *s)
2707 {
2708 return(1);
2709 }
2710
2711X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2712 {
2713 return(ctx->cert_store);
2714 }
2715
2716void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2717 {
2718 if (ctx->cert_store != NULL)
2719 X509_STORE_free(ctx->cert_store);
2720 ctx->cert_store=store;
2721 }
2722
2723int SSL_want(const SSL *s)
2724 {
2725 return(s->rwstate);
2726 }
2727
2728/*!
2729 * \brief Set the callback for generating temporary RSA keys.
2730 * \param ctx the SSL context.
2731 * \param cb the callback
2732 */
2733
Adam Langley95c29f32014-06-20 12:00:00 -07002734void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2735 int is_export,
2736 int keylength))
2737 {
2738 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2739 }
2740
2741void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2742 int is_export,
2743 int keylength))
2744 {
2745 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2746 }
Adam Langley95c29f32014-06-20 12:00:00 -07002747
2748#ifdef DOXYGEN
2749/*!
2750 * \brief The RSA temporary key callback function.
2751 * \param ssl the SSL session.
2752 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2753 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2754 * of the required key in bits.
2755 * \return the temporary RSA key.
2756 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2757 */
2758
2759RSA *cb(SSL *ssl,int is_export,int keylength)
2760 {}
2761#endif
2762
2763/*!
2764 * \brief Set the callback for generating temporary DH keys.
2765 * \param ctx the SSL context.
2766 * \param dh the callback
2767 */
2768
Adam Langley95c29f32014-06-20 12:00:00 -07002769void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2770 int keylength))
2771 {
2772 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2773 }
2774
2775void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2776 int keylength))
2777 {
2778 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2779 }
Adam Langley95c29f32014-06-20 12:00:00 -07002780
Adam Langley95c29f32014-06-20 12:00:00 -07002781void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2782 int keylength))
2783 {
2784 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2785 }
2786
2787void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2788 int keylength))
2789 {
2790 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2791 }
Adam Langley95c29f32014-06-20 12:00:00 -07002792
Adam Langley95c29f32014-06-20 12:00:00 -07002793int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2794 {
2795 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2796 {
2797 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2798 return 0;
2799 }
2800 if (ctx->psk_identity_hint != NULL)
2801 OPENSSL_free(ctx->psk_identity_hint);
2802 if (identity_hint != NULL)
2803 {
2804 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2805 if (ctx->psk_identity_hint == NULL)
2806 return 0;
2807 }
2808 else
2809 ctx->psk_identity_hint = NULL;
2810 return 1;
2811 }
2812
2813int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2814 {
2815 if (s == NULL)
2816 return 0;
2817
Adam Langley95c29f32014-06-20 12:00:00 -07002818 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2819 {
2820 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2821 return 0;
2822 }
Adam Langley0289c732014-06-20 12:00:00 -07002823
David Benjamin688d8df2014-11-02 23:06:42 -05002824 /* Clear currently configured hint, if any. */
Adam Langley0289c732014-06-20 12:00:00 -07002825 if (s->psk_identity_hint != NULL)
2826 {
2827 OPENSSL_free(s->psk_identity_hint);
2828 s->psk_identity_hint = NULL;
2829 }
Adam Langley0289c732014-06-20 12:00:00 -07002830
Adam Langley95c29f32014-06-20 12:00:00 -07002831 if (identity_hint != NULL)
2832 {
David Benjamin688d8df2014-11-02 23:06:42 -05002833 s->psk_identity_hint = BUF_strdup(identity_hint);
2834 if (s->psk_identity_hint == NULL)
2835 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002836 }
Adam Langley95c29f32014-06-20 12:00:00 -07002837 return 1;
2838 }
2839
2840const char *SSL_get_psk_identity_hint(const SSL *s)
2841 {
Adam Langley0289c732014-06-20 12:00:00 -07002842 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002843 return NULL;
David Benjamin688d8df2014-11-02 23:06:42 -05002844 return s->psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07002845 }
2846
2847const char *SSL_get_psk_identity(const SSL *s)
2848 {
2849 if (s == NULL || s->session == NULL)
2850 return NULL;
2851 return(s->session->psk_identity);
2852 }
2853
2854void SSL_set_psk_client_callback(SSL *s,
2855 unsigned int (*cb)(SSL *ssl, const char *hint,
2856 char *identity, unsigned int max_identity_len, unsigned char *psk,
2857 unsigned int max_psk_len))
2858 {
2859 s->psk_client_callback = cb;
2860 }
2861
2862void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2863 unsigned int (*cb)(SSL *ssl, const char *hint,
2864 char *identity, unsigned int max_identity_len, unsigned char *psk,
2865 unsigned int max_psk_len))
2866 {
2867 ctx->psk_client_callback = cb;
2868 }
2869
2870void SSL_set_psk_server_callback(SSL *s,
2871 unsigned int (*cb)(SSL *ssl, const char *identity,
2872 unsigned char *psk, unsigned int max_psk_len))
2873 {
2874 s->psk_server_callback = cb;
2875 }
2876
2877void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2878 unsigned int (*cb)(SSL *ssl, const char *identity,
2879 unsigned char *psk, unsigned int max_psk_len))
2880 {
2881 ctx->psk_server_callback = cb;
2882 }
Adam Langley95c29f32014-06-20 12:00:00 -07002883
David Benjamin1eb367c2014-12-12 18:17:51 -05002884void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version)
2885 {
2886 ctx->min_version = version;
2887 }
2888
2889void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version)
2890 {
2891 ctx->max_version = version;
2892 }
2893
2894void SSL_set_min_version(SSL *ssl, uint16_t version)
2895 {
2896 ssl->min_version = version;
2897 }
2898
2899void SSL_set_max_version(SSL *ssl, uint16_t version)
2900 {
2901 ssl->max_version = version;
2902 }
2903
Adam Langley95c29f32014-06-20 12:00:00 -07002904void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
2905 {
2906 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2907 }
2908void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
2909 {
2910 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2911 }
2912
David Benjamin859ec3c2014-09-02 16:29:36 -04002913void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
2914 {
2915 if (ctx->keylog_bio != NULL)
2916 BIO_free(ctx->keylog_bio);
2917 ctx->keylog_bio = keylog_bio;
2918 }
2919
2920static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
2921 {
2922 static const char hextable[] = "0123456789abcdef";
2923 uint8_t *out;
2924 size_t i;
2925
2926 if (!CBB_add_space(cbb, &out, in_len * 2))
2927 {
2928 return 0;
2929 }
2930
2931 for (i = 0; i < in_len; i++)
2932 {
2933 *(out++) = (uint8_t)hextable[in[i] >> 4];
2934 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2935 }
2936 return 1;
2937 }
2938
2939int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
2940 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
2941 const uint8_t *premaster, size_t premaster_len)
2942 {
2943 BIO *bio = ctx->keylog_bio;
2944 CBB cbb;
2945 uint8_t *out;
2946 size_t out_len;
2947 int ret;
2948
2949 if (bio == NULL)
2950 {
2951 return 1;
2952 }
2953
2954 if (encrypted_premaster_len < 8)
2955 {
2956 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
2957 return 0;
2958 }
2959
2960 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
2961 {
2962 return 0;
2963 }
2964 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
2965 /* Only the first 8 bytes of the encrypted premaster secret are
2966 * logged. */
2967 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2968 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
2969 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2970 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
2971 !CBB_finish(&cbb, &out, &out_len))
2972 {
2973 CBB_cleanup(&cbb);
2974 return 0;
2975 }
2976
2977 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2978 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2979 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2980
2981 OPENSSL_free(out);
2982 return ret;
2983 }
2984
2985int ssl_ctx_log_master_secret(SSL_CTX *ctx,
2986 const uint8_t *client_random, size_t client_random_len,
2987 const uint8_t *master, size_t master_len)
2988 {
2989 BIO *bio = ctx->keylog_bio;
2990 CBB cbb;
2991 uint8_t *out;
2992 size_t out_len;
2993 int ret;
2994
2995 if (bio == NULL)
2996 {
2997 return 1;
2998 }
2999
3000 if (client_random_len != 32)
3001 {
3002 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3003 return 0;
3004 }
3005
3006 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3007 {
3008 return 0;
3009 }
3010 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3011 !cbb_add_hex(&cbb, client_random, 32) ||
3012 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3013 !cbb_add_hex(&cbb, master, master_len) ||
3014 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3015 !CBB_finish(&cbb, &out, &out_len))
3016 {
3017 CBB_cleanup(&cbb);
3018 return 0;
3019 }
3020
3021 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3022 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3023 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3024
3025 OPENSSL_free(out);
3026 return ret;
3027 }
3028
Adam Langleyadb739e2014-06-20 12:00:00 -07003029int SSL_cutthrough_complete(const SSL *s)
3030 {
3031 return (!s->server && /* cutthrough only applies to clients */
3032 !s->hit && /* full-handshake */
3033 s->version >= SSL3_VERSION &&
3034 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3035 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3036 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3037 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3038 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003039 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003040 s->state == SSL3_ST_CR_FINISHED_A));
3041 }
3042
Adam Langley95f22882014-06-20 12:00:00 -07003043void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3044 size_t* ssl_session_size)
3045{
3046 *ssl_size = sizeof(SSL);
3047 *ssl_ctx_size = sizeof(SSL_CTX);
3048 *ssl_session_size = sizeof(SSL_SESSION);
3049}
3050
Adam Langleyadb739e2014-06-20 12:00:00 -07003051int ssl3_can_cutthrough(const SSL *s)
3052 {
3053 const SSL_CIPHER *c;
3054
3055 /* require a strong enough cipher */
3056 if (SSL_get_cipher_bits(s, NULL) < 128)
3057 return 0;
3058
3059 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003060 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003061 {
3062 return 0;
3063 }
3064
3065 /* require a forward-secret cipher */
3066 c = SSL_get_current_cipher(s);
3067 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3068 c->algorithm_mkey != SSL_kEECDH))
3069 {
3070 return 0;
3071 }
3072
3073 return 1;
3074 }
3075
David Benjaminceb6f282014-12-07 23:56:19 -05003076const SSL_METHOD *ssl3_get_method(uint16_t version)
Adam Langley29707792014-06-20 12:00:00 -07003077 {
David Benjaminceb6f282014-12-07 23:56:19 -05003078 switch (version)
3079 {
3080 case SSL3_VERSION:
3081 return SSLv3_method();
3082 case TLS1_VERSION:
3083 return TLSv1_method();
3084 case TLS1_1_VERSION:
3085 return TLSv1_1_method();
3086 case TLS1_2_VERSION:
3087 return TLSv1_2_method();
3088 case DTLS1_VERSION:
3089 return DTLSv1_method();
3090 case DTLS1_2_VERSION:
3091 return DTLSv1_2_method();
3092 default:
3093 return NULL;
3094 }
3095 }
3096
David Benjamine99e9122014-12-11 01:46:01 -05003097const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version)
3098 {
3099 switch (version)
3100 {
3101 case SSL3_VERSION:
3102 return &SSLv3_enc_data;
3103 case TLS1_VERSION:
3104 return &TLSv1_enc_data;
3105 case TLS1_1_VERSION:
3106 return &TLSv1_1_enc_data;
3107 case TLS1_2_VERSION:
3108 return &TLSv1_2_enc_data;
3109 case DTLS1_VERSION:
3110 return &DTLSv1_enc_data;
3111 case DTLS1_2_VERSION:
3112 return &DTLSv1_2_enc_data;
3113 default:
3114 return NULL;
3115 }
3116 }
3117
David Benjaminceb6f282014-12-07 23:56:19 -05003118uint16_t ssl3_get_max_server_version(const SSL *s)
3119 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003120 uint16_t max_version;
3121
David Benjaminceb6f282014-12-07 23:56:19 -05003122 if (SSL_IS_DTLS(s))
3123 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003124 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
3125 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version)
David Benjaminceb6f282014-12-07 23:56:19 -05003126 return DTLS1_2_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003127 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version)
David Benjaminceb6f282014-12-07 23:56:19 -05003128 return DTLS1_VERSION;
3129 return 0;
3130 }
Adam Langley29707792014-06-20 12:00:00 -07003131
David Benjamin1eb367c2014-12-12 18:17:51 -05003132 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
3133 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version)
Adam Langley29707792014-06-20 12:00:00 -07003134 return TLS1_2_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003135 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version)
Adam Langley29707792014-06-20 12:00:00 -07003136 return TLS1_1_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003137 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version)
Adam Langley29707792014-06-20 12:00:00 -07003138 return TLS1_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003139 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version)
Adam Langley29707792014-06-20 12:00:00 -07003140 return SSL3_VERSION;
Adam Langley29707792014-06-20 12:00:00 -07003141 return 0;
3142 }
3143
David Benjaminceb6f282014-12-07 23:56:19 -05003144uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version)
3145 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003146 uint16_t version = 0;
3147
David Benjaminceb6f282014-12-07 23:56:19 -05003148 if (SSL_IS_DTLS(s))
3149 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003150 /* Clamp client_version to max_version. */
3151 if (s->max_version != 0 && client_version < s->max_version)
3152 client_version = s->max_version;
3153
David Benjaminceb6f282014-12-07 23:56:19 -05003154 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2))
David Benjamin1eb367c2014-12-12 18:17:51 -05003155 version = DTLS1_2_VERSION;
3156 else if (client_version <= DTLS1_VERSION && !(s->options & SSL_OP_NO_DTLSv1))
3157 version = DTLS1_VERSION;
3158
3159 /* Check against min_version. */
3160 if (version != 0 && s->min_version != 0 && version > s->min_version)
3161 return 0;
3162 return version;
David Benjaminceb6f282014-12-07 23:56:19 -05003163 }
3164 else
3165 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003166 /* Clamp client_version to max_version. */
3167 if (s->max_version != 0 && client_version > s->max_version)
3168 client_version = s->max_version;
3169
David Benjaminceb6f282014-12-07 23:56:19 -05003170 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2))
David Benjamin1eb367c2014-12-12 18:17:51 -05003171 version = TLS1_2_VERSION;
3172 else if (client_version >= TLS1_1_VERSION && !(s->options & SSL_OP_NO_TLSv1_1))
3173 version = TLS1_1_VERSION;
3174 else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1))
3175 version = TLS1_VERSION;
3176 else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3))
3177 version = SSL3_VERSION;
3178
3179 /* Check against min_version. */
3180 if (version != 0 && s->min_version != 0 && version < s->min_version)
3181 return 0;
3182 return version;
David Benjaminceb6f282014-12-07 23:56:19 -05003183 }
3184 }
3185
3186uint16_t ssl3_get_max_client_version(SSL *s)
3187 {
3188 unsigned long options = s->options;
3189 uint16_t version = 0;
3190
3191 /* OpenSSL's API for controlling versions entails blacklisting
3192 * individual protocols. This has two problems. First, on the client,
3193 * the protocol can only express a contiguous range of versions. Second,
3194 * a library consumer trying to set a maximum version cannot disable
3195 * protocol versions that get added in a future version of the library.
3196 *
3197 * To account for both of these, OpenSSL interprets the client-side
3198 * bitmask as a min/max range by picking the lowest contiguous non-empty
3199 * range of enabled protocols. Note that this means it is impossible to
3200 * set a maximum version of TLS 1.2 in a future-proof way.
3201 *
3202 * By this scheme, the maximum version is the lowest version V such that
David Benjamin1eb367c2014-12-12 18:17:51 -05003203 * V is enabled and V+1 is disabled or unimplemented. */
David Benjaminceb6f282014-12-07 23:56:19 -05003204 if (SSL_IS_DTLS(s))
3205 {
3206 if (!(options & SSL_OP_NO_DTLSv1_2))
3207 version = DTLS1_2_VERSION;
3208 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2))
3209 version = DTLS1_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003210 if (s->max_version != 0 && version < s->max_version)
3211 version = s->max_version;
David Benjaminceb6f282014-12-07 23:56:19 -05003212 }
3213 else
3214 {
3215 if (!(options & SSL_OP_NO_TLSv1_2))
3216 version = TLS1_2_VERSION;
3217 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2))
3218 version = TLS1_1_VERSION;
3219 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1))
3220 version = TLS1_VERSION;
3221 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1))
3222 version = SSL3_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003223 if (s->max_version != 0 && version > s->max_version)
3224 version = s->max_version;
David Benjaminceb6f282014-12-07 23:56:19 -05003225 }
3226
3227 return version;
3228 }
3229
3230int ssl3_is_version_enabled(SSL *s, uint16_t version)
3231 {
3232 if (SSL_IS_DTLS(s))
3233 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003234 if (s->max_version != 0 && version < s->max_version)
3235 return 0;
3236 if (s->min_version != 0 && version > s->min_version)
3237 return 0;
David Benjaminceb6f282014-12-07 23:56:19 -05003238 switch (version)
3239 {
3240 case DTLS1_VERSION:
3241 return !(s->options & SSL_OP_NO_DTLSv1);
3242 case DTLS1_2_VERSION:
3243 return !(s->options & SSL_OP_NO_DTLSv1_2);
3244 default:
3245 return 0;
3246 }
3247 }
3248 else
3249 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003250 if (s->max_version != 0 && version > s->max_version)
3251 return 0;
3252 if (s->min_version != 0 && version < s->min_version)
3253 return 0;
David Benjaminceb6f282014-12-07 23:56:19 -05003254 switch (version)
3255 {
3256 case SSL3_VERSION:
3257 return !(s->options & SSL_OP_NO_SSLv3);
3258 case TLS1_VERSION:
3259 return !(s->options & SSL_OP_NO_TLSv1);
3260 case TLS1_1_VERSION:
3261 return !(s->options & SSL_OP_NO_TLSv1_1);
3262 case TLS1_2_VERSION:
3263 return !(s->options & SSL_OP_NO_TLSv1_2);
3264 default:
3265 return 0;
3266 }
3267 }
3268 }
3269
Adam Langley95c29f32014-06-20 12:00:00 -07003270/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3271 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3272 * any. If EVP_MD pointer is passed, initializes ctx with this md
3273 * Returns newly allocated ctx;
3274 */
3275
3276EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3277{
3278 ssl_clear_hash_ctx(hash);
3279 *hash = EVP_MD_CTX_create();
David Benjaminae3e4872014-11-18 21:52:26 -05003280 if (md != NULL && *hash != NULL &&
3281 !EVP_DigestInit_ex(*hash, md, NULL))
Adam Langley69a01602014-11-17 17:26:55 -08003282 {
David Benjaminae3e4872014-11-18 21:52:26 -05003283 EVP_MD_CTX_destroy(*hash);
3284 *hash = NULL;
Adam Langley69a01602014-11-17 17:26:55 -08003285 }
Adam Langley95c29f32014-06-20 12:00:00 -07003286 return *hash;
3287}
3288void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3289{
3290
3291 if (*hash) EVP_MD_CTX_destroy(*hash);
3292 *hash=NULL;
3293}
3294
Adam Langley95c29f32014-06-20 12:00:00 -07003295int SSL_cache_hit(SSL *s)
3296 {
3297 return s->hit;
3298 }
3299
3300int SSL_is_server(SSL *s)
3301 {
3302 return s->server;
3303 }
Feng Lu41aa3252014-11-21 22:47:56 -08003304
3305void SSL_enable_fastradio_padding(SSL *s, char on_off)
3306 {
3307 s->fastradio_padding = on_off;
3308 }