1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
57 /* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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.
115 /* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
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.
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.
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.
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
144 #include <openssl/bytestring.h>
145 #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>
153 #include "ssl_locl.h"
155 SSL3_ENC_METHOD ssl3_undef_enc_method={
156 /* evil casts, but these functions are only called if there's a library bug */
157 (int (*)(SSL *,int))ssl_undefined_function,
158 (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
159 ssl_undefined_function,
160 (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
161 (int (*)(SSL*, int))ssl_undefined_function,
162 (int (*)(SSL *, const char*, int, unsigned char *))ssl_undefined_function,
163 0, /* finish_mac_length */
164 (int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
165 NULL, /* client_finished_label */
166 0, /* client_finished_label_len */
167 NULL, /* server_finished_label */
168 0, /* server_finished_label_len */
169 (int (*)(int))ssl_undefined_function,
170 (int (*)(SSL *, unsigned char *, size_t, const char *,
171 size_t, const unsigned char *, size_t,
172 int use_context)) ssl_undefined_function,
175 /* Some error codes are special. Ensure the make_errors.go script
176 * never regresses this. */
177 OPENSSL_COMPILE_ASSERT(
178 SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
179 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
180 ssl_alert_reason_code_mismatch);
182 int SSL_clear(SSL *s)
185 if (s->method == NULL)
187 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
191 if (ssl_clear_bad_session(s))
193 SSL_SESSION_free(s->session);
200 #if 0 /* Disabled since version 1.10 of this file (early return not
201 * needed because SSL_clear is not called when doing renegotiation) */
202 /* This is set if we are doing dynamic renegotiation so keep
203 * the old cipher. It is sort of a SSL_clear_lite :-) */
204 if (s->renegotiate) return(1);
208 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
215 s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
217 s->version=s->method->version;
218 s->client_version=s->version;
219 s->rwstate=SSL_NOTHING;
220 s->rstate=SSL_ST_READ_HEADER;
222 s->read_ahead=s->ctx->read_ahead;
225 if (s->init_buf != NULL)
227 BUF_MEM_free(s->init_buf);
231 ssl_clear_cipher_ctx(s);
232 ssl_clear_hash_ctx(&s->read_hash);
233 ssl_clear_hash_ctx(&s->write_hash);
238 /* Check to see if we were changed into a different method, if
239 * so, revert back if we are not doing session-id reuse. */
240 if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
242 s->method->ssl_free(s);
243 s->method=s->ctx->method;
244 if (!s->method->ssl_new(s))
249 s->method->ssl_clear(s);
253 /** Used to change an SSL_CTXs default SSL method type */
254 int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
256 STACK_OF(SSL_CIPHER) *sk;
260 sk=ssl_create_cipher_list(
261 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id,
262 meth->version == SSL2_VERSION ?
264 SSL_DEFAULT_CIPHER_LIST,
267 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
269 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_ssl_version, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
275 SSL *SSL_new(SSL_CTX *ctx)
281 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
284 if (ctx->method == NULL)
286 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
290 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
291 if (s == NULL) goto err;
292 memset(s,0,sizeof(SSL));
294 s->options=ctx->options;
296 s->max_cert_list=ctx->max_cert_list;
298 if (ctx->cert != NULL)
300 /* Earlier library versions used to copy the pointer to
301 * the CERT, not its contents; only when setting new
302 * parameters for the per-SSL copy, ssl_cert_new would be
303 * called (and the direct reference to the per-SSL_CTX
304 * settings would be lost, but those still were indirectly
305 * accessed for various purposes, and for that reason they
306 * used to be known as s->ctx->default_cert).
307 * Now we don't look at the SSL_CTX's CERT after having
308 * duplicated it once. */
310 s->cert = ssl_cert_dup(ctx->cert);
315 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
317 s->read_ahead=ctx->read_ahead;
318 s->msg_callback=ctx->msg_callback;
319 s->msg_callback_arg=ctx->msg_callback_arg;
320 s->verify_mode=ctx->verify_mode;
322 s->verify_depth=ctx->verify_depth;
324 s->sid_ctx_length=ctx->sid_ctx_length;
325 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
326 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
327 s->verify_callback=ctx->default_verify_callback;
328 s->generate_session_id=ctx->generate_session_id;
330 s->param = X509_VERIFY_PARAM_new();
333 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
335 s->purpose = ctx->purpose;
336 s->trust = ctx->trust;
338 s->quiet_shutdown=ctx->quiet_shutdown;
339 s->max_send_fragment = ctx->max_send_fragment;
341 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
343 s->tlsext_debug_cb = 0;
344 s->tlsext_debug_arg = NULL;
345 s->tlsext_ticket_expected = 0;
346 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
348 if (ctx->tlsext_ecpointformatlist)
350 s->tlsext_ecpointformatlist =
351 BUF_memdup(ctx->tlsext_ecpointformatlist,
352 ctx->tlsext_ecpointformatlist_length);
353 if (!s->tlsext_ecpointformatlist)
355 s->tlsext_ecpointformatlist_length =
356 ctx->tlsext_ecpointformatlist_length;
358 if (ctx->tlsext_ellipticcurvelist)
360 s->tlsext_ellipticcurvelist =
361 BUF_memdup(ctx->tlsext_ellipticcurvelist,
362 ctx->tlsext_ellipticcurvelist_length * 2);
363 if (!s->tlsext_ellipticcurvelist)
365 s->tlsext_ellipticcurvelist_length =
366 ctx->tlsext_ellipticcurvelist_length;
368 s->next_proto_negotiated = NULL;
370 if (s->ctx->alpn_client_proto_list)
372 s->alpn_client_proto_list = BUF_memdup(
373 s->ctx->alpn_client_proto_list,
374 s->ctx->alpn_client_proto_list_len);
375 if (s->alpn_client_proto_list == NULL)
377 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
380 s->verify_result=X509_V_OK;
382 s->method=ctx->method;
384 if (!s->method->ssl_new(s))
388 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
392 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
394 s->psk_identity_hint = NULL;
395 if (ctx->psk_identity_hint)
397 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
398 if (s->psk_identity_hint == NULL)
401 s->psk_client_callback=ctx->psk_client_callback;
402 s->psk_server_callback=ctx->psk_server_callback;
406 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
407 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
415 ssl_cert_free(s->cert);
417 SSL_CTX_free(s->ctx); /* decrement reference count */
420 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
424 int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
425 unsigned int sid_ctx_len)
427 if(sid_ctx_len > sizeof ctx->sid_ctx)
429 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
432 ctx->sid_ctx_length=sid_ctx_len;
433 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
438 int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
439 unsigned int sid_ctx_len)
441 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
443 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
446 ssl->sid_ctx_length=sid_ctx_len;
447 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
452 int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
454 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
455 ctx->generate_session_id = cb;
456 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
460 int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
462 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
463 ssl->generate_session_id = cb;
464 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
468 int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
471 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
472 * we can "construct" a session to give us the desired check - ie. to
473 * find if there's a session in the hash table that would conflict with
474 * any new session built out of this id/id_len and the ssl_version in
475 * use by this SSL. */
478 if(id_len > sizeof r.session_id)
481 r.ssl_version = ssl->version;
482 r.session_id_length = id_len;
483 memcpy(r.session_id, id, id_len);
484 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
485 * callback is calling us to check the uniqueness of a shorter ID, it
486 * must be compared as a padded-out ID because that is what it will be
487 * converted to when the callback has finished choosing it. */
488 if((r.ssl_version == SSL2_VERSION) &&
489 (id_len < SSL2_SSL_SESSION_ID_LENGTH))
491 memset(r.session_id + id_len, 0,
492 SSL2_SSL_SESSION_ID_LENGTH - id_len);
493 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
496 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
497 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
498 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
502 int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
504 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
507 int SSL_set_purpose(SSL *s, int purpose)
509 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
512 int SSL_CTX_set_trust(SSL_CTX *s, int trust)
514 return X509_VERIFY_PARAM_set_trust(s->param, trust);
517 int SSL_set_trust(SSL *s, int trust)
519 return X509_VERIFY_PARAM_set_trust(s->param, trust);
522 int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
524 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
527 int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
529 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
532 void ssl_cipher_preference_list_free(
533 struct ssl_cipher_preference_list_st *cipher_list)
535 sk_SSL_CIPHER_free(cipher_list->ciphers);
536 OPENSSL_free(cipher_list->in_group_flags);
537 OPENSSL_free(cipher_list);
540 struct ssl_cipher_preference_list_st*
541 ssl_cipher_preference_list_dup(
542 struct ssl_cipher_preference_list_st *cipher_list)
544 struct ssl_cipher_preference_list_st* ret = NULL;
545 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
547 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
551 ret->in_group_flags = NULL;
552 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
555 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
556 if (!ret->in_group_flags)
561 if (ret && ret->ciphers)
562 sk_SSL_CIPHER_free(ret->ciphers);
568 struct ssl_cipher_preference_list_st*
569 ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
571 struct ssl_cipher_preference_list_st* ret = NULL;
572 size_t n = sk_SSL_CIPHER_num(ciphers);
574 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
578 ret->in_group_flags = NULL;
579 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
582 ret->in_group_flags = OPENSSL_malloc(n);
583 if (!ret->in_group_flags)
585 memset(ret->in_group_flags, 0, n);
589 if (ret && ret->ciphers)
590 sk_SSL_CIPHER_free(ret->ciphers);
596 X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
601 X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
606 void SSL_certs_clear(SSL *s)
608 ssl_cert_clear_certs(s->cert);
611 void SSL_free(SSL *s)
618 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
626 fprintf(stderr,"SSL_free, bad reference count\n");
632 X509_VERIFY_PARAM_free(s->param);
634 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
638 /* If the buffering BIO is in place, pop it off */
639 if (s->bbio == s->wbio)
641 s->wbio=BIO_pop(s->wbio);
647 BIO_free_all(s->rbio);
648 if ((s->wbio != NULL) && (s->wbio != s->rbio))
649 BIO_free_all(s->wbio);
651 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
653 /* add extra stuff */
654 if (s->cipher_list != NULL)
655 ssl_cipher_preference_list_free(s->cipher_list);
656 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
658 /* Make the next call work :-) */
659 if (s->session != NULL)
661 ssl_clear_bad_session(s);
662 SSL_SESSION_free(s->session);
665 ssl_clear_cipher_ctx(s);
666 ssl_clear_hash_ctx(&s->read_hash);
667 ssl_clear_hash_ctx(&s->write_hash);
669 if (s->cert != NULL) ssl_cert_free(s->cert);
670 /* Free up if allocated */
672 if (s->tlsext_hostname)
673 OPENSSL_free(s->tlsext_hostname);
674 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
675 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
676 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
677 if (s->alpn_client_proto_list)
678 OPENSSL_free(s->alpn_client_proto_list);
679 if (s->tlsext_channel_id_private)
680 EVP_PKEY_free(s->tlsext_channel_id_private);
682 if (s->psk_identity_hint)
683 OPENSSL_free(s->psk_identity_hint);
685 if (s->client_CA != NULL)
686 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
688 if (s->method != NULL) s->method->ssl_free(s);
690 if (s->ctx) SSL_CTX_free(s->ctx);
692 if (s->next_proto_negotiated)
693 OPENSSL_free(s->next_proto_negotiated);
695 if (s->srtp_profiles)
696 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
698 if (s->tlsext_session_ticket)
700 OPENSSL_free(s->tlsext_session_ticket);
706 void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
708 /* If the output buffering BIO is still in place, remove it
712 if (s->wbio == s->bbio)
714 s->wbio=s->wbio->next_bio;
715 s->bbio->next_bio=NULL;
718 if ((s->rbio != NULL) && (s->rbio != rbio))
719 BIO_free_all(s->rbio);
720 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
721 BIO_free_all(s->wbio);
726 BIO *SSL_get_rbio(const SSL *s)
729 BIO *SSL_get_wbio(const SSL *s)
732 int SSL_get_fd(const SSL *s)
734 return(SSL_get_rfd(s));
737 int SSL_get_rfd(const SSL *s)
743 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
749 int SSL_get_wfd(const SSL *s)
755 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
761 #ifndef OPENSSL_NO_SOCK
762 int SSL_set_fd(SSL *s,int fd)
767 bio=BIO_new(BIO_s_fd());
771 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
774 BIO_set_fd(bio,fd,BIO_NOCLOSE);
775 SSL_set_bio(s,bio,bio);
781 int SSL_set_wfd(SSL *s,int fd)
786 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
787 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
789 bio=BIO_new(BIO_s_fd());
793 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
796 BIO_set_fd(bio,fd,BIO_NOCLOSE);
797 SSL_set_bio(s,SSL_get_rbio(s),bio);
800 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
806 int SSL_set_rfd(SSL *s,int fd)
811 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
812 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
814 bio=BIO_new(BIO_s_fd());
818 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
821 BIO_set_fd(bio,fd,BIO_NOCLOSE);
822 SSL_set_bio(s,bio,SSL_get_wbio(s));
825 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
833 /* return length of latest Finished message we sent, copy to 'buf' */
834 size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
840 ret = s->s3->tmp.finish_md_len;
843 memcpy(buf, s->s3->tmp.finish_md, count);
848 /* return length of latest Finished message we expected, copy to 'buf' */
849 size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
855 ret = s->s3->tmp.peer_finish_md_len;
858 memcpy(buf, s->s3->tmp.peer_finish_md, count);
864 int SSL_get_verify_mode(const SSL *s)
866 return(s->verify_mode);
869 int SSL_get_verify_depth(const SSL *s)
871 return X509_VERIFY_PARAM_get_depth(s->param);
874 int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
876 return(s->verify_callback);
879 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
881 return(ctx->verify_mode);
884 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
886 return X509_VERIFY_PARAM_get_depth(ctx->param);
889 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
891 return(ctx->default_verify_callback);
894 void SSL_set_verify(SSL *s,int mode,
895 int (*callback)(int ok,X509_STORE_CTX *ctx))
898 if (callback != NULL)
899 s->verify_callback=callback;
902 void SSL_set_verify_depth(SSL *s,int depth)
904 X509_VERIFY_PARAM_set_depth(s->param, depth);
907 void SSL_set_read_ahead(SSL *s,int yes)
912 int SSL_get_read_ahead(const SSL *s)
914 return(s->read_ahead);
917 int SSL_pending(const SSL *s)
919 /* SSL_pending cannot work properly if read-ahead is enabled
920 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
921 * and it is impossible to fix since SSL_pending cannot report
922 * errors that may be observed while scanning the new data.
923 * (Note that SSL_pending() is often used as a boolean value,
924 * so we'd better not return -1.)
926 return(s->method->ssl_pending(s));
929 X509 *SSL_get_peer_certificate(const SSL *s)
933 if ((s == NULL) || (s->session == NULL))
941 return X509_up_ref(r);
944 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
948 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
951 r=s->session->sess_cert->cert_chain;
953 /* If we are a client, cert_chain includes the peer's own
954 * certificate; if we are a server, it does not. */
959 /* Fix this so it checks all the valid key/cert options */
960 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
962 if ( (ctx == NULL) ||
963 (ctx->cert == NULL) ||
964 (ctx->cert->key->x509 == NULL))
966 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
969 if (ctx->cert->key->privatekey == NULL)
971 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
974 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
977 /* Fix this function so that it takes an optional type parameter */
978 int SSL_check_private_key(const SSL *ssl)
982 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
985 if (ssl->cert == NULL)
987 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
990 if (ssl->cert->key->x509 == NULL)
992 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
995 if (ssl->cert->key->privatekey == NULL)
997 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
1000 return(X509_check_private_key(ssl->cert->key->x509,
1001 ssl->cert->key->privatekey));
1004 int SSL_accept(SSL *s)
1006 if (s->handshake_func == 0)
1007 /* Not properly initialized yet */
1008 SSL_set_accept_state(s);
1010 return(s->method->ssl_accept(s));
1013 int SSL_connect(SSL *s)
1015 if (s->handshake_func == 0)
1016 /* Not properly initialized yet */
1017 SSL_set_connect_state(s);
1019 return(s->method->ssl_connect(s));
1022 long SSL_get_default_timeout(const SSL *s)
1024 return SSL_DEFAULT_SESSION_TIMEOUT;
1027 int SSL_read(SSL *s,void *buf,int num)
1029 if (s->handshake_func == 0)
1031 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1035 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1037 s->rwstate=SSL_NOTHING;
1040 return(s->method->ssl_read(s,buf,num));
1043 int SSL_peek(SSL *s,void *buf,int num)
1045 if (s->handshake_func == 0)
1047 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1051 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1055 return(s->method->ssl_peek(s,buf,num));
1058 int SSL_write(SSL *s,const void *buf,int num)
1060 if (s->handshake_func == 0)
1062 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1066 if (s->shutdown & SSL_SENT_SHUTDOWN)
1068 s->rwstate=SSL_NOTHING;
1069 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1072 return(s->method->ssl_write(s,buf,num));
1075 int SSL_shutdown(SSL *s)
1077 /* Note that this function behaves differently from what one might
1078 * expect. Return values are 0 for no success (yet),
1079 * 1 for success; but calling it once is usually not enough,
1080 * even if blocking I/O is used (see ssl3_shutdown).
1083 if (s->handshake_func == 0)
1085 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1089 if (!SSL_in_init(s))
1090 return(s->method->ssl_shutdown(s));
1095 int SSL_renegotiate(SSL *s)
1097 if (s->renegotiate == 0)
1102 return(s->method->ssl_renegotiate(s));
1105 int SSL_renegotiate_abbreviated(SSL *s)
1107 if (s->renegotiate == 0)
1112 return(s->method->ssl_renegotiate(s));
1115 int SSL_renegotiate_pending(SSL *s)
1117 /* becomes true when negotiation is requested;
1118 * false again once a handshake has finished */
1119 return (s->renegotiate != 0);
1122 long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1128 case SSL_CTRL_GET_READ_AHEAD:
1129 return(s->read_ahead);
1130 case SSL_CTRL_SET_READ_AHEAD:
1135 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1136 s->msg_callback_arg = parg;
1139 case SSL_CTRL_OPTIONS:
1140 return(s->options|=larg);
1141 case SSL_CTRL_CLEAR_OPTIONS:
1142 return(s->options&=~larg);
1144 return(s->mode|=larg);
1145 case SSL_CTRL_CLEAR_MODE:
1146 return(s->mode &=~larg);
1147 case SSL_CTRL_GET_MAX_CERT_LIST:
1148 return(s->max_cert_list);
1149 case SSL_CTRL_SET_MAX_CERT_LIST:
1151 s->max_cert_list=larg;
1153 case SSL_CTRL_SET_MTU:
1154 if (larg < (long)dtls1_min_mtu())
1162 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1163 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1165 s->max_send_fragment = larg;
1167 case SSL_CTRL_GET_RI_SUPPORT:
1169 return s->s3->send_connection_binding;
1171 case SSL_CTRL_CERT_FLAGS:
1172 return(s->cert->cert_flags|=larg);
1173 case SSL_CTRL_CLEAR_CERT_FLAGS:
1174 return(s->cert->cert_flags &=~larg);
1176 case SSL_CTRL_GET_RAW_CIPHERLIST:
1179 if (s->cert->ciphers_raw == NULL)
1181 *(unsigned char **)parg = s->cert->ciphers_raw;
1182 return (int)s->cert->ciphers_rawlen;
1186 /* Passing a NULL |parg| returns the size of a single
1187 * cipher suite value. */
1191 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1195 long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1199 case SSL_CTRL_SET_MSG_CALLBACK:
1200 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1204 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1208 LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1210 return ctx->sessions;
1213 long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1219 case SSL_CTRL_GET_READ_AHEAD:
1220 return(ctx->read_ahead);
1221 case SSL_CTRL_SET_READ_AHEAD:
1223 ctx->read_ahead=larg;
1226 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1227 ctx->msg_callback_arg = parg;
1230 case SSL_CTRL_GET_MAX_CERT_LIST:
1231 return(ctx->max_cert_list);
1232 case SSL_CTRL_SET_MAX_CERT_LIST:
1233 l=ctx->max_cert_list;
1234 ctx->max_cert_list=larg;
1237 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1238 l=ctx->session_cache_size;
1239 ctx->session_cache_size=larg;
1241 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1242 return(ctx->session_cache_size);
1243 case SSL_CTRL_SET_SESS_CACHE_MODE:
1244 l=ctx->session_cache_mode;
1245 ctx->session_cache_mode=larg;
1247 case SSL_CTRL_GET_SESS_CACHE_MODE:
1248 return(ctx->session_cache_mode);
1250 case SSL_CTRL_SESS_NUMBER:
1251 return(lh_SSL_SESSION_num_items(ctx->sessions));
1252 case SSL_CTRL_SESS_CONNECT:
1253 return(ctx->stats.sess_connect);
1254 case SSL_CTRL_SESS_CONNECT_GOOD:
1255 return(ctx->stats.sess_connect_good);
1256 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1257 return(ctx->stats.sess_connect_renegotiate);
1258 case SSL_CTRL_SESS_ACCEPT:
1259 return(ctx->stats.sess_accept);
1260 case SSL_CTRL_SESS_ACCEPT_GOOD:
1261 return(ctx->stats.sess_accept_good);
1262 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1263 return(ctx->stats.sess_accept_renegotiate);
1264 case SSL_CTRL_SESS_HIT:
1265 return(ctx->stats.sess_hit);
1266 case SSL_CTRL_SESS_CB_HIT:
1267 return(ctx->stats.sess_cb_hit);
1268 case SSL_CTRL_SESS_MISSES:
1269 return(ctx->stats.sess_miss);
1270 case SSL_CTRL_SESS_TIMEOUTS:
1271 return(ctx->stats.sess_timeout);
1272 case SSL_CTRL_SESS_CACHE_FULL:
1273 return(ctx->stats.sess_cache_full);
1274 case SSL_CTRL_OPTIONS:
1275 return(ctx->options|=larg);
1276 case SSL_CTRL_CLEAR_OPTIONS:
1277 return(ctx->options&=~larg);
1279 return(ctx->mode|=larg);
1280 case SSL_CTRL_CLEAR_MODE:
1281 return(ctx->mode&=~larg);
1282 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1283 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1285 ctx->max_send_fragment = larg;
1287 case SSL_CTRL_CERT_FLAGS:
1288 return(ctx->cert->cert_flags|=larg);
1289 case SSL_CTRL_CLEAR_CERT_FLAGS:
1290 return(ctx->cert->cert_flags &=~larg);
1292 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1296 long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1300 case SSL_CTRL_SET_MSG_CALLBACK:
1301 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1305 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1309 int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1312 const SSL_CIPHER *a = in_a;
1313 const SSL_CIPHER *b = in_b;
1314 const long a_id = a->id;
1315 const long b_id = b->id;
1321 return((l > 0)?1:-1);
1324 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1327 const long a_id = (*ap)->id;
1328 const long b_id = (*bp)->id;
1334 return((l > 0)?1:-1);
1337 /** return a STACK of the ciphers available for the SSL and in order of
1339 STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1344 if (s->cipher_list != NULL)
1346 return(s->cipher_list->ciphers);
1349 if (s->version >= TLS1_1_VERSION)
1351 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
1352 return s->ctx->cipher_list_tls11->ciphers;
1355 if ((s->ctx != NULL) &&
1356 (s->ctx->cipher_list != NULL))
1358 return(s->ctx->cipher_list->ciphers);
1364 /** return a STACK of the ciphers available for the SSL and in order of
1366 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1370 if (s->cipher_list_by_id != NULL)
1372 return(s->cipher_list_by_id);
1374 else if ((s->ctx != NULL) &&
1375 (s->ctx->cipher_list_by_id != NULL))
1377 return(s->ctx->cipher_list_by_id);
1383 /** The old interface to get the same thing as SSL_get_ciphers() */
1384 const char *SSL_get_cipher_list(const SSL *s,int n)
1386 const SSL_CIPHER *c;
1387 STACK_OF(SSL_CIPHER) *sk;
1389 if (s == NULL) return(NULL);
1390 sk=SSL_get_ciphers(s);
1391 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1393 c=sk_SSL_CIPHER_value(sk,n);
1394 if (c == NULL) return(NULL);
1398 /** specify the ciphers to be used by default by the SSL_CTX */
1399 int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1401 STACK_OF(SSL_CIPHER) *sk;
1403 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1404 &ctx->cipher_list_by_id,str, ctx->cert);
1405 /* ssl_create_cipher_list may return an empty stack if it
1406 * was unable to find a cipher matching the given rule string
1407 * (for example if the rule string specifies a cipher which
1408 * has been disabled). This is not an error as far as
1409 * ssl_create_cipher_list is concerned, and hence
1410 * ctx->cipher_list and ctx->cipher_list_by_id has been
1414 else if (sk_SSL_CIPHER_num(sk) == 0)
1416 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1422 int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1424 STACK_OF(SSL_CIPHER) *sk;
1426 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1429 else if (sk_SSL_CIPHER_num(sk) == 0)
1431 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1437 /** specify the ciphers to be used by the SSL */
1438 int SSL_set_cipher_list(SSL *s,const char *str)
1440 STACK_OF(SSL_CIPHER) *sk;
1442 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1443 &s->cipher_list_by_id,str, s->cert);
1444 /* see comment in SSL_CTX_set_cipher_list */
1447 else if (sk_SSL_CIPHER_num(sk) == 0)
1449 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1455 /* works well for SSLv2, not so good for SSLv3 */
1456 char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1459 STACK_OF(SSL_CIPHER) *sk;
1460 const SSL_CIPHER *c;
1463 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1468 sk=s->session->ciphers;
1470 if (sk_SSL_CIPHER_num(sk) == 0)
1473 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1477 c=sk_SSL_CIPHER_value(sk,i);
1495 int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p)
1498 const SSL_CIPHER *c;
1501 int no_scsv = s->renegotiate;
1502 /* Set disabled masks for this session */
1503 ssl_set_client_disabled(s);
1505 if (sk == NULL) return(0);
1508 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1510 c=sk_SSL_CIPHER_value(sk,i);
1511 /* Skip disabled ciphers */
1512 if (c->algorithm_ssl & ct->mask_ssl ||
1513 c->algorithm_mkey & ct->mask_k ||
1514 c->algorithm_auth & ct->mask_a)
1516 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1517 if (c->id == SSL3_CK_SCSV)
1525 s2n(ssl3_get_cipher_value(c), p);
1527 /* If p == q, no ciphers and caller indicates an error. Otherwise
1528 * add SCSV if not renegotiating.
1534 static const SSL_CIPHER scsv =
1536 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1538 s2n(ssl3_get_cipher_value(&scsv), p);
1539 #ifdef OPENSSL_RI_DEBUG
1540 fprintf(stderr, "SCSV sent by client\n");
1543 if (s->fallback_scsv)
1545 static const SSL_CIPHER fallback_scsv =
1547 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1549 s2n(ssl3_get_cipher_value(&fallback_scsv), p);
1556 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs,
1557 STACK_OF(SSL_CIPHER) **skp)
1559 CBS cipher_suites = *cbs;
1560 const SSL_CIPHER *c;
1561 STACK_OF(SSL_CIPHER) *sk;
1564 s->s3->send_connection_binding = 0;
1566 if (CBS_len(&cipher_suites) % 2 != 0)
1568 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1571 if ((skp == NULL) || (*skp == NULL))
1572 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1576 sk_SSL_CIPHER_zero(sk);
1579 if (!CBS_stow(&cipher_suites,
1580 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
1582 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1586 while (CBS_len(&cipher_suites) > 0)
1588 uint16_t cipher_suite;
1590 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1592 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1596 /* Check for SCSV */
1597 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
1599 /* SCSV fatal if renegotiating */
1602 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1603 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1606 s->s3->send_connection_binding = 1;
1607 #ifdef OPENSSL_RI_DEBUG
1608 fprintf(stderr, "SCSV received by server\n");
1613 /* Check for FALLBACK_SCSV */
1614 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff) &&
1615 s->version < ssl_get_max_version(s))
1617 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1618 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_INAPPROPRIATE_FALLBACK);
1622 c = ssl3_get_cipher_by_value(cipher_suite);
1625 if (!sk_SSL_CIPHER_push(sk,c))
1627 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1637 if ((skp == NULL) || (*skp == NULL))
1638 sk_SSL_CIPHER_free(sk);
1643 /** return a servername extension value if provided in Client Hello, or NULL.
1644 * So far, only host_name types are defined (RFC 3546).
1647 const char *SSL_get_servername(const SSL *s, const int type)
1649 if (type != TLSEXT_NAMETYPE_host_name)
1652 return s->session && !s->tlsext_hostname ?
1653 s->session->tlsext_hostname :
1657 int SSL_get_servername_type(const SSL *s)
1659 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1660 return TLSEXT_NAMETYPE_host_name;
1664 void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1666 ctx->signed_cert_timestamps_enabled = 1;
1669 int SSL_enable_signed_cert_timestamps(SSL *ssl)
1671 /* Currently not implemented server-side. */
1675 ssl->signed_cert_timestamps_enabled = 1;
1679 void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1681 ctx->ocsp_stapling_enabled = 1;
1684 int SSL_enable_ocsp_stapling(SSL *ssl)
1686 /* Currently not implemented server-side. */
1689 ssl->ocsp_stapling_enabled = 1;
1693 void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, uint8_t **out, size_t *out_len)
1695 SSL_SESSION *session = ssl->session;
1701 if (!session || !session->tlsext_signed_cert_timestamp_list)
1703 *out = session->tlsext_signed_cert_timestamp_list;
1704 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1707 void SSL_get0_ocsp_response(const SSL *ssl, uint8_t **out, size_t *out_len)
1709 SSL_SESSION *session = ssl->session;
1715 if (!session || !session->ocsp_response)
1717 *out = session->ocsp_response;
1718 *out_len = session->ocsp_response_length;
1721 /* SSL_select_next_proto implements the standard protocol selection. It is
1722 * expected that this function is called from the callback set by
1723 * SSL_CTX_set_next_proto_select_cb.
1725 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1726 * strings. The length byte itself is not included in the length. A byte
1727 * string of length 0 is invalid. No byte string may be truncated.
1729 * The current, but experimental algorithm for selecting the protocol is:
1731 * 1) If the server doesn't support NPN then this is indicated to the
1732 * callback. In this case, the client application has to abort the connection
1733 * or have a default application level protocol.
1735 * 2) If the server supports NPN, but advertises an empty list then the
1736 * client selects the first protcol in its list, but indicates via the
1737 * API that this fallback case was enacted.
1739 * 3) Otherwise, the client finds the first protocol in the server's list
1740 * that it supports and selects this protocol. This is because it's
1741 * assumed that the server has better information about which protocol
1742 * a client should use.
1744 * 4) If the client doesn't support any of the server's advertised
1745 * protocols, then this is treated the same as case 2.
1748 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1749 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1751 int 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)
1754 const unsigned char *result;
1755 int status = OPENSSL_NPN_UNSUPPORTED;
1757 /* For each protocol in server preference order, see if we support it. */
1758 for (i = 0; i < server_len; )
1760 for (j = 0; j < client_len; )
1762 if (server[i] == client[j] &&
1763 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1765 /* We found a match */
1766 result = &server[i];
1767 status = OPENSSL_NPN_NEGOTIATED;
1777 /* There's no overlap between our protocols and the server's list. */
1779 status = OPENSSL_NPN_NO_OVERLAP;
1782 *out = (unsigned char *) result + 1;
1783 *outlen = result[0];
1787 /* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1788 * requested protocol for this connection and returns 0. If the client didn't
1789 * request any protocol, then *data is set to NULL.
1791 * Note that the client can request any protocol it chooses. The value returned
1792 * from this function need not be a member of the list of supported protocols
1793 * provided by the callback.
1795 void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
1797 *data = s->next_proto_negotiated;
1801 *len = s->next_proto_negotiated_len;
1805 /* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1806 * TLS server needs a list of supported protocols for Next Protocol
1807 * Negotiation. The returned list must be in wire format. The list is returned
1808 * by setting |out| to point to it and |outlen| to its length. This memory will
1809 * not be modified, but one should assume that the SSL* keeps a reference to
1812 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1813 * such extension will be included in the ServerHello. */
1814 void 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)
1816 ctx->next_protos_advertised_cb = cb;
1817 ctx->next_protos_advertised_cb_arg = arg;
1820 /* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1821 * client needs to select a protocol from the server's provided list. |out|
1822 * must be set to point to the selected protocol (which may be within |in|).
1823 * The length of the protocol name must be written into |outlen|. The server's
1824 * advertised protocols are provided in |in| and |inlen|. The callback can
1825 * assume that |in| is syntactically valid.
1827 * The client must select a protocol. It is fatal to the connection if this
1828 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1830 void 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)
1832 ctx->next_proto_select_cb = cb;
1833 ctx->next_proto_select_cb_arg = arg;
1836 /* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1837 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1838 * length-prefixed strings).
1840 * Returns 0 on success. */
1841 int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1842 unsigned protos_len)
1844 if (ctx->alpn_client_proto_list)
1845 OPENSSL_free(ctx->alpn_client_proto_list);
1847 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1848 if (!ctx->alpn_client_proto_list)
1850 ctx->alpn_client_proto_list_len = protos_len;
1855 /* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1856 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1857 * length-prefixed strings).
1859 * Returns 0 on success. */
1860 int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1861 unsigned protos_len)
1863 if (ssl->alpn_client_proto_list)
1864 OPENSSL_free(ssl->alpn_client_proto_list);
1866 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
1867 if (!ssl->alpn_client_proto_list)
1869 ssl->alpn_client_proto_list_len = protos_len;
1874 /* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1875 * during ClientHello processing in order to select an ALPN protocol from the
1876 * client's list of offered protocols. */
1877 void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1878 int (*cb) (SSL *ssl,
1879 const unsigned char **out,
1880 unsigned char *outlen,
1881 const unsigned char *in,
1886 ctx->alpn_select_cb = cb;
1887 ctx->alpn_select_cb_arg = arg;
1890 /* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1891 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1892 * including the leading length-prefix byte). If the server didn't respond with
1893 * a negotiated protocol then |*len| will be zero. */
1894 void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1899 *data = ssl->s3->alpn_selected;
1903 *len = ssl->s3->alpn_selected_len;
1906 int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1907 const char *label, size_t llen, const unsigned char *p, size_t plen,
1910 if (s->version < TLS1_VERSION)
1913 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1918 static uint32_t ssl_session_hash(const SSL_SESSION *a)
1920 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1921 ((uint32_t) a->session_id[1] << 8) ||
1922 ((uint32_t) a->session_id[2] << 16) ||
1923 ((uint32_t) a->session_id[3] << 24);
1928 /* NB: If this function (or indeed the hash function which uses a sort of
1929 * coarser function than this one) is changed, ensure
1930 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1931 * able to construct an SSL_SESSION that will collide with any existing session
1932 * with a matching session ID. */
1933 static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1935 if (a->ssl_version != b->ssl_version)
1937 if (a->session_id_length != b->session_id_length)
1939 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1942 SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1948 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1952 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1954 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1957 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1961 memset(ret,0,sizeof(SSL_CTX));
1965 ret->cert_store=NULL;
1966 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1967 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1968 ret->session_cache_head=NULL;
1969 ret->session_cache_tail=NULL;
1971 /* We take the system default */
1972 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
1974 ret->new_session_cb=0;
1975 ret->remove_session_cb=0;
1976 ret->get_session_cb=0;
1977 ret->generate_session_id=0;
1979 memset((char *)&ret->stats,0,sizeof(ret->stats));
1982 ret->quiet_shutdown=0;
1984 ret->info_callback=NULL;
1986 ret->app_verify_callback=0;
1987 ret->app_verify_arg=NULL;
1989 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1991 ret->msg_callback=0;
1992 ret->msg_callback_arg=NULL;
1993 ret->verify_mode=SSL_VERIFY_NONE;
1995 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1997 ret->sid_ctx_length=0;
1998 ret->default_verify_callback=NULL;
1999 if ((ret->cert=ssl_cert_new()) == NULL)
2002 ret->default_passwd_callback=0;
2003 ret->default_passwd_callback_userdata=NULL;
2004 ret->client_cert_cb=0;
2005 ret->app_gen_cookie_cb=0;
2006 ret->app_verify_cookie_cb=0;
2008 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
2009 if (ret->sessions == NULL) goto err;
2010 ret->cert_store=X509_STORE_new();
2011 if (ret->cert_store == NULL) goto err;
2013 ssl_create_cipher_list(ret->method,
2014 &ret->cipher_list,&ret->cipher_list_by_id,
2015 meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ret->cert);
2016 if (ret->cipher_list == NULL
2017 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
2019 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
2023 ret->param = X509_VERIFY_PARAM_new();
2027 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
2030 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
2032 ret->extra_certs=NULL;
2034 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
2036 ret->tlsext_servername_callback = 0;
2037 ret->tlsext_servername_arg = NULL;
2038 /* Setup RFC4507 ticket keys */
2039 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
2040 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
2041 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
2042 ret->options |= SSL_OP_NO_TICKET;
2044 ret->tlsext_status_cb = 0;
2045 ret->tlsext_status_arg = NULL;
2047 ret->next_protos_advertised_cb = 0;
2048 ret->next_proto_select_cb = 0;
2049 ret->psk_identity_hint=NULL;
2050 ret->psk_client_callback=NULL;
2051 ret->psk_server_callback=NULL;
2053 /* Default is to connect to non-RI servers. When RI is more widely
2054 * deployed might change this.
2056 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2060 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
2062 if (ret != NULL) SSL_CTX_free(ret);
2067 void SSL_CTX_free(SSL_CTX *a)
2071 if (a == NULL) return;
2073 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
2075 REF_PRINT("SSL_CTX",a);
2081 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2087 X509_VERIFY_PARAM_free(a->param);
2090 * Free internal session cache. However: the remove_cb() may reference
2091 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2092 * after the sessions were flushed.
2093 * As the ex_data handling routines might also touch the session cache,
2094 * the most secure solution seems to be: empty (flush) the cache, then
2095 * free ex_data, then finally free the cache.
2096 * (See ticket [openssl.org #212].)
2098 if (a->sessions != NULL)
2099 SSL_CTX_flush_sessions(a,0);
2101 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2103 if (a->sessions != NULL)
2104 lh_SSL_SESSION_free(a->sessions);
2106 if (a->cert_store != NULL)
2107 X509_STORE_free(a->cert_store);
2108 if (a->cipher_list != NULL)
2109 ssl_cipher_preference_list_free(a->cipher_list);
2110 if (a->cipher_list_by_id != NULL)
2111 sk_SSL_CIPHER_free(a->cipher_list_by_id);
2112 if (a->cipher_list_tls11 != NULL)
2113 ssl_cipher_preference_list_free(a->cipher_list_tls11);
2114 if (a->cert != NULL)
2115 ssl_cert_free(a->cert);
2116 if (a->client_CA != NULL)
2117 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2118 if (a->extra_certs != NULL)
2119 sk_X509_pop_free(a->extra_certs,X509_free);
2121 if (a->srtp_profiles)
2122 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2124 if (a->psk_identity_hint)
2125 OPENSSL_free(a->psk_identity_hint);
2127 if (a->tlsext_ecpointformatlist)
2128 OPENSSL_free(a->tlsext_ecpointformatlist);
2129 if (a->tlsext_ellipticcurvelist)
2130 OPENSSL_free(a->tlsext_ellipticcurvelist);
2131 if (a->alpn_client_proto_list != NULL)
2132 OPENSSL_free(a->alpn_client_proto_list);
2134 if (a->tlsext_channel_id_private)
2135 EVP_PKEY_free(a->tlsext_channel_id_private);
2138 BIO_free(a->keylog_bio);
2143 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2145 ctx->default_passwd_callback=cb;
2148 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2150 ctx->default_passwd_callback_userdata=u;
2153 void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2155 ctx->app_verify_callback=cb;
2156 ctx->app_verify_arg=arg;
2159 void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2161 ctx->verify_mode=mode;
2162 ctx->default_verify_callback=cb;
2165 void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2167 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2170 void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2172 ssl_cert_set_cert_cb(c->cert, cb, arg);
2175 void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2177 ssl_cert_set_cert_cb(s->cert, cb, arg);
2180 void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2183 int rsa_enc,rsa_sign,dh_tmp;
2184 unsigned long mask_k,mask_a;
2185 int have_ecc_cert, ecdsa_ok;
2188 if (c == NULL) return;
2190 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
2192 have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2193 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
2194 rsa_enc= cpk->valid_flags & CERT_PKEY_VALID;
2195 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2196 rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
2197 cpk= &(c->pkeys[SSL_PKEY_ECC]);
2198 have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID;
2203 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2204 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
2205 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
2214 if (rsa_enc || rsa_sign)
2221 /* An ECC certificate may be usable for ECDSA cipher suites depending on
2222 * the key usage extension. */
2225 cpk = &c->pkeys[SSL_PKEY_ECC];
2227 /* This call populates extension flags (ex_flags) */
2228 X509_check_purpose(x, -1, 0);
2229 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2230 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2231 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2252 /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2253 #define ku_reject(x, usage) \
2254 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2257 int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2259 unsigned long alg_a;
2260 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2261 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2263 alg_a = cs->algorithm_auth;
2265 /* This call populates the ex_flags field correctly */
2266 X509_check_purpose(x, -1, 0);
2267 if ((x->sig_alg) && (x->sig_alg->algorithm))
2269 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2270 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2272 if (alg_a & SSL_aECDSA)
2274 /* key usage, if present, must allow signing */
2275 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2277 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2282 return 1; /* all checks are ok */
2286 static int ssl_get_server_cert_index(const SSL *s)
2289 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2290 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2291 idx = SSL_PKEY_RSA_SIGN;
2293 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2297 CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2303 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2305 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2306 /* Broken protocol test: return last used certificate: which may
2307 * mismatch the one expected.
2309 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2313 i = ssl_get_server_cert_index(s);
2315 /* This may or may not be an error. */
2320 return &c->pkeys[i];
2323 EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2325 unsigned long alg_a;
2329 alg_a = cipher->algorithm_auth;
2332 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2333 /* Broken protocol test: use last key: which may
2334 * mismatch the one expected.
2336 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2337 idx = c->key - c->pkeys;
2341 if (alg_a & SSL_aRSA)
2343 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2344 idx = SSL_PKEY_RSA_SIGN;
2345 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2346 idx = SSL_PKEY_RSA_ENC;
2348 else if ((alg_a & SSL_aECDSA) &&
2349 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2353 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2357 *pmd = c->pkeys[idx].digest;
2358 return c->pkeys[idx].privatekey;
2361 void ssl_update_cache(SSL *s,int mode)
2365 /* If the session_id_length is 0, we are not supposed to cache it,
2366 * and it would be rather hard to do anyway :-) */
2367 if (s->session->session_id_length == 0) return;
2369 i=s->initial_ctx->session_cache_mode;
2370 if ((i & mode) && (!s->hit)
2371 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2372 || SSL_CTX_add_session(s->initial_ctx,s->session))
2373 && (s->initial_ctx->new_session_cb != NULL))
2375 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
2376 if (!s->initial_ctx->new_session_cb(s,s->session))
2377 SSL_SESSION_free(s->session);
2380 /* auto flush every 255 connections */
2381 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2382 ((i & mode) == mode))
2384 if ( (((mode & SSL_SESS_CACHE_CLIENT)
2385 ?s->initial_ctx->stats.sess_connect_good
2386 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
2388 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
2393 const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2398 const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2403 int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2408 if (s->method != meth)
2410 if (s->handshake_func != NULL)
2411 conn=(s->handshake_func == s->method->ssl_connect);
2413 if (s->method->version == meth->version)
2417 s->method->ssl_free(s);
2419 ret=s->method->ssl_new(s);
2423 s->handshake_func=meth->ssl_connect;
2425 s->handshake_func=meth->ssl_accept;
2430 int SSL_get_error(const SSL *s,int i)
2436 if (i > 0) return(SSL_ERROR_NONE);
2438 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2439 * etc, where we do encode the error */
2440 if ((l=ERR_peek_error()) != 0)
2442 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2443 return(SSL_ERROR_SYSCALL);
2445 return(SSL_ERROR_SSL);
2448 if ((i < 0) && SSL_want_session(s))
2449 return(SSL_ERROR_PENDING_SESSION);
2451 if ((i < 0) && SSL_want_certificate(s))
2452 return(SSL_ERROR_PENDING_CERTIFICATE);
2454 if ((i < 0) && SSL_want_read(s))
2456 bio=SSL_get_rbio(s);
2457 if (BIO_should_read(bio))
2458 return(SSL_ERROR_WANT_READ);
2459 else if (BIO_should_write(bio))
2460 /* This one doesn't make too much sense ... We never try
2461 * to write to the rbio, and an application program where
2462 * rbio and wbio are separate couldn't even know what it
2464 * However if we ever set s->rwstate incorrectly
2465 * (so that we have SSL_want_read(s) instead of
2466 * SSL_want_write(s)) and rbio and wbio *are* the same,
2467 * this test works around that bug; so it might be safer
2469 return(SSL_ERROR_WANT_WRITE);
2470 else if (BIO_should_io_special(bio))
2472 reason=BIO_get_retry_reason(bio);
2473 if (reason == BIO_RR_CONNECT)
2474 return(SSL_ERROR_WANT_CONNECT);
2475 else if (reason == BIO_RR_ACCEPT)
2476 return(SSL_ERROR_WANT_ACCEPT);
2478 return(SSL_ERROR_SYSCALL); /* unknown */
2482 if ((i < 0) && SSL_want_write(s))
2484 bio=SSL_get_wbio(s);
2485 if (BIO_should_write(bio))
2486 return(SSL_ERROR_WANT_WRITE);
2487 else if (BIO_should_read(bio))
2488 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2489 return(SSL_ERROR_WANT_READ);
2490 else if (BIO_should_io_special(bio))
2492 reason=BIO_get_retry_reason(bio);
2493 if (reason == BIO_RR_CONNECT)
2494 return(SSL_ERROR_WANT_CONNECT);
2495 else if (reason == BIO_RR_ACCEPT)
2496 return(SSL_ERROR_WANT_ACCEPT);
2498 return(SSL_ERROR_SYSCALL);
2501 if ((i < 0) && SSL_want_x509_lookup(s))
2503 return(SSL_ERROR_WANT_X509_LOOKUP);
2505 if ((i < 0) && SSL_want_channel_id_lookup(s))
2507 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2512 if (s->version == SSL2_VERSION)
2514 /* assume it is the socket being closed */
2515 return(SSL_ERROR_ZERO_RETURN);
2519 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2520 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2521 return(SSL_ERROR_ZERO_RETURN);
2524 return(SSL_ERROR_SYSCALL);
2527 int SSL_do_handshake(SSL *s)
2531 if (s->handshake_func == NULL)
2533 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2537 s->method->ssl_renegotiate_check(s);
2539 if (SSL_in_init(s) || SSL_in_before(s))
2541 ret=s->handshake_func(s);
2546 /* For the next 2 functions, SSL_clear() sets shutdown and so
2547 * one of these calls will reset it */
2548 void SSL_set_accept_state(SSL *s)
2552 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2553 s->handshake_func=s->method->ssl_accept;
2554 /* clear the current cipher */
2555 ssl_clear_cipher_ctx(s);
2556 ssl_clear_hash_ctx(&s->read_hash);
2557 ssl_clear_hash_ctx(&s->write_hash);
2560 void SSL_set_connect_state(SSL *s)
2564 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2565 s->handshake_func=s->method->ssl_connect;
2566 /* clear the current cipher */
2567 ssl_clear_cipher_ctx(s);
2568 ssl_clear_hash_ctx(&s->read_hash);
2569 ssl_clear_hash_ctx(&s->write_hash);
2572 int ssl_undefined_function(SSL *s)
2574 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2578 int ssl_undefined_void_function(void)
2580 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2584 int ssl_undefined_const_function(const SSL *s)
2586 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2590 SSL_METHOD *ssl_bad_method(int ver)
2592 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2596 static const char *ssl_get_version(int version)
2598 if (version == TLS1_2_VERSION)
2600 else if (version == TLS1_1_VERSION)
2602 else if (version == TLS1_VERSION)
2604 else if (version == SSL3_VERSION)
2606 else if (version == SSL2_VERSION)
2612 const char *SSL_get_version(const SSL *s)
2614 return ssl_get_version(s->version);
2617 const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2619 return ssl_get_version(sess->ssl_version);
2622 void ssl_clear_cipher_ctx(SSL *s)
2624 if (s->enc_read_ctx != NULL)
2626 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2627 OPENSSL_free(s->enc_read_ctx);
2628 s->enc_read_ctx=NULL;
2630 if (s->enc_write_ctx != NULL)
2632 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2633 OPENSSL_free(s->enc_write_ctx);
2634 s->enc_write_ctx=NULL;
2636 if (s->aead_read_ctx != NULL)
2638 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2639 OPENSSL_free(s->aead_read_ctx);
2640 s->aead_read_ctx = NULL;
2642 if (s->aead_write_ctx != NULL)
2644 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2645 OPENSSL_free(s->aead_write_ctx);
2646 s->aead_write_ctx = NULL;
2650 X509 *SSL_get_certificate(const SSL *s)
2652 if (s->cert != NULL)
2653 return(s->cert->key->x509);
2658 EVP_PKEY *SSL_get_privatekey(const SSL *s)
2660 if (s->cert != NULL)
2661 return(s->cert->key->privatekey);
2666 X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2668 if (ctx->cert != NULL)
2669 return ctx->cert->key->x509;
2674 EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2676 if (ctx->cert != NULL)
2677 return ctx->cert->key->privatekey;
2682 const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2684 if ((s->session != NULL) && (s->session->cipher != NULL))
2685 return(s->session->cipher);
2688 const void *SSL_get_current_compression(SSL *s)
2692 const void *SSL_get_current_expansion(SSL *s)
2697 int ssl_init_wbio_buffer(SSL *s,int push)
2701 if (s->bbio == NULL)
2703 bbio=BIO_new(BIO_f_buffer());
2704 if (bbio == NULL) return(0);
2710 if (s->bbio == s->wbio)
2711 s->wbio=BIO_pop(s->wbio);
2713 (void)BIO_reset(bbio);
2714 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2715 if (!BIO_set_read_buffer_size(bbio,1))
2717 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2722 if (s->wbio != bbio)
2723 s->wbio=BIO_push(bbio,s->wbio);
2727 if (s->wbio == bbio)
2728 s->wbio=BIO_pop(bbio);
2733 void ssl_free_wbio_buffer(SSL *s)
2735 if (s->bbio == NULL) return;
2737 if (s->bbio == s->wbio)
2739 /* remove buffering */
2740 s->wbio=BIO_pop(s->wbio);
2741 #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2742 assert(s->wbio != NULL);
2749 void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2751 ctx->quiet_shutdown=mode;
2754 int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2756 return(ctx->quiet_shutdown);
2759 void SSL_set_quiet_shutdown(SSL *s,int mode)
2761 s->quiet_shutdown=mode;
2764 int SSL_get_quiet_shutdown(const SSL *s)
2766 return(s->quiet_shutdown);
2769 void SSL_set_shutdown(SSL *s,int mode)
2774 int SSL_get_shutdown(const SSL *s)
2776 return(s->shutdown);
2779 int SSL_version(const SSL *s)
2784 SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2789 SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2791 if (ssl->ctx == ctx)
2794 ctx = ssl->initial_ctx;
2795 if (ssl->cert != NULL)
2796 ssl_cert_free(ssl->cert);
2797 ssl->cert = ssl_cert_dup(ctx->cert);
2798 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2799 if (ssl->ctx != NULL)
2800 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2803 ssl->sid_ctx_length = ctx->sid_ctx_length;
2804 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2805 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2810 #ifndef OPENSSL_NO_STDIO
2811 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2813 return(X509_STORE_set_default_paths(ctx->cert_store));
2816 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2819 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2823 void SSL_set_info_callback(SSL *ssl,
2824 void (*cb)(const SSL *ssl,int type,int val))
2826 ssl->info_callback=cb;
2829 /* One compiler (Diab DCC) doesn't like argument names in returned
2830 function pointer. */
2831 void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2833 return ssl->info_callback;
2836 int SSL_state(const SSL *ssl)
2841 void SSL_set_state(SSL *ssl, int state)
2846 void SSL_set_verify_result(SSL *ssl,long arg)
2848 ssl->verify_result=arg;
2851 long SSL_get_verify_result(const SSL *ssl)
2853 return(ssl->verify_result);
2856 int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2857 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2859 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2860 new_func, dup_func, free_func);
2863 int SSL_set_ex_data(SSL *s,int idx,void *arg)
2865 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2868 void *SSL_get_ex_data(const SSL *s,int idx)
2870 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2873 int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2874 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2876 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2877 new_func, dup_func, free_func);
2880 int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2882 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2885 void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2887 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2895 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2897 return(ctx->cert_store);
2900 void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2902 if (ctx->cert_store != NULL)
2903 X509_STORE_free(ctx->cert_store);
2904 ctx->cert_store=store;
2907 int SSL_want(const SSL *s)
2913 * \brief Set the callback for generating temporary RSA keys.
2914 * \param ctx the SSL context.
2915 * \param cb the callback
2918 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2922 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2925 void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2929 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2934 * \brief The RSA temporary key callback function.
2935 * \param ssl the SSL session.
2936 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2937 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2938 * of the required key in bits.
2939 * \return the temporary RSA key.
2940 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2943 RSA *cb(SSL *ssl,int is_export,int keylength)
2948 * \brief Set the callback for generating temporary DH keys.
2949 * \param ctx the SSL context.
2950 * \param dh the callback
2953 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2956 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2959 void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2962 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2965 void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2968 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2971 void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2974 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2977 int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2979 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2981 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2984 if (ctx->psk_identity_hint != NULL)
2985 OPENSSL_free(ctx->psk_identity_hint);
2986 if (identity_hint != NULL)
2988 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2989 if (ctx->psk_identity_hint == NULL)
2993 ctx->psk_identity_hint = NULL;
2997 int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
3002 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3004 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3008 /* Clear hint in SSL and associated SSL_SESSION (if any). */
3009 if (s->psk_identity_hint != NULL)
3011 OPENSSL_free(s->psk_identity_hint);
3012 s->psk_identity_hint = NULL;
3014 if (s->session != NULL && s->session->psk_identity_hint != NULL)
3016 OPENSSL_free(s->session->psk_identity_hint);
3017 s->session->psk_identity_hint = NULL;
3020 if (identity_hint != NULL)
3022 /* The hint is stored in SSL and SSL_SESSION with the one in
3023 * SSL_SESSION taking precedence. Thus, if SSL_SESSION is avaiable,
3024 * we store the hint there, otherwise we store it in SSL. */
3025 if (s->session != NULL)
3027 s->session->psk_identity_hint = BUF_strdup(identity_hint);
3028 if (s->session->psk_identity_hint == NULL)
3033 s->psk_identity_hint = BUF_strdup(identity_hint);
3034 if (s->psk_identity_hint == NULL)
3041 const char *SSL_get_psk_identity_hint(const SSL *s)
3045 /* The hint is stored in SSL and SSL_SESSION with the one in SSL_SESSION
3046 * taking precedence. */
3047 if (s->session != NULL)
3048 return(s->session->psk_identity_hint);
3049 return(s->psk_identity_hint);
3052 const char *SSL_get_psk_identity(const SSL *s)
3054 if (s == NULL || s->session == NULL)
3056 return(s->session->psk_identity);
3059 void SSL_set_psk_client_callback(SSL *s,
3060 unsigned int (*cb)(SSL *ssl, const char *hint,
3061 char *identity, unsigned int max_identity_len, unsigned char *psk,
3062 unsigned int max_psk_len))
3064 s->psk_client_callback = cb;
3067 void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3068 unsigned int (*cb)(SSL *ssl, const char *hint,
3069 char *identity, unsigned int max_identity_len, unsigned char *psk,
3070 unsigned int max_psk_len))
3072 ctx->psk_client_callback = cb;
3075 void SSL_set_psk_server_callback(SSL *s,
3076 unsigned int (*cb)(SSL *ssl, const char *identity,
3077 unsigned char *psk, unsigned int max_psk_len))
3079 s->psk_server_callback = cb;
3082 void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3083 unsigned int (*cb)(SSL *ssl, const char *identity,
3084 unsigned char *psk, unsigned int max_psk_len))
3086 ctx->psk_server_callback = cb;
3089 void 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))
3091 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3093 void 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))
3095 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3098 void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
3100 if (ctx->keylog_bio != NULL)
3101 BIO_free(ctx->keylog_bio);
3102 ctx->keylog_bio = keylog_bio;
3105 static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
3107 static const char hextable[] = "0123456789abcdef";
3111 if (!CBB_add_space(cbb, &out, in_len * 2))
3116 for (i = 0; i < in_len; i++)
3118 *(out++) = (uint8_t)hextable[in[i] >> 4];
3119 *(out++) = (uint8_t)hextable[in[i] & 0xf];
3124 int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
3125 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
3126 const uint8_t *premaster, size_t premaster_len)
3128 BIO *bio = ctx->keylog_bio;
3139 if (encrypted_premaster_len < 8)
3141 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
3145 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
3149 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
3150 /* Only the first 8 bytes of the encrypted premaster secret are
3152 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
3153 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3154 !cbb_add_hex(&cbb, premaster, premaster_len) ||
3155 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3156 !CBB_finish(&cbb, &out, &out_len))
3162 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3163 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3164 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3170 int ssl_ctx_log_master_secret(SSL_CTX *ctx,
3171 const uint8_t *client_random, size_t client_random_len,
3172 const uint8_t *master, size_t master_len)
3174 BIO *bio = ctx->keylog_bio;
3185 if (client_random_len != 32)
3187 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3191 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3195 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3196 !cbb_add_hex(&cbb, client_random, 32) ||
3197 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3198 !cbb_add_hex(&cbb, master, master_len) ||
3199 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3200 !CBB_finish(&cbb, &out, &out_len))
3206 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3207 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3208 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3214 int SSL_cutthrough_complete(const SSL *s)
3216 return (!s->server && /* cutthrough only applies to clients */
3217 !s->hit && /* full-handshake */
3218 s->version >= SSL3_VERSION &&
3219 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3220 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3221 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3222 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3223 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
3224 s->state == SSL3_ST_CR_CHANGE ||
3225 s->state == SSL3_ST_CR_FINISHED_A));
3228 void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3229 size_t* ssl_session_size)
3231 *ssl_size = sizeof(SSL);
3232 *ssl_ctx_size = sizeof(SSL_CTX);
3233 *ssl_session_size = sizeof(SSL_SESSION);
3236 int ssl3_can_cutthrough(const SSL *s)
3238 const SSL_CIPHER *c;
3240 /* require a strong enough cipher */
3241 if (SSL_get_cipher_bits(s, NULL) < 128)
3244 /* require ALPN or NPN extension */
3245 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
3250 /* require a forward-secret cipher */
3251 c = SSL_get_current_cipher(s);
3252 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3253 c->algorithm_mkey != SSL_kEECDH))
3261 /* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3262 * |s|, or zero if all versions are disabled. */
3263 int ssl_get_max_version(const SSL *s)
3265 /* Only one version supported for DTLS. */
3266 if (s->version == DTLS1_VERSION)
3267 return DTLS1_VERSION;
3269 if (!(s->options & SSL_OP_NO_TLSv1_2))
3270 return TLS1_2_VERSION;
3271 if (!(s->options & SSL_OP_NO_TLSv1_1))
3272 return TLS1_1_VERSION;
3273 if (!(s->options & SSL_OP_NO_TLSv1))
3274 return TLS1_VERSION;
3275 if (!(s->options & SSL_OP_NO_SSLv3))
3276 return SSL3_VERSION;
3277 if (!(s->options & SSL_OP_NO_SSLv2))
3278 return SSL2_VERSION;
3282 /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3283 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3284 * any. If EVP_MD pointer is passed, initializes ctx with this md
3285 * Returns newly allocated ctx;
3288 EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3290 ssl_clear_hash_ctx(hash);
3291 *hash = EVP_MD_CTX_create();
3292 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3295 void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3298 if (*hash) EVP_MD_CTX_destroy(*hash);
3302 void SSL_set_debug(SSL *s, int debug)
3307 int SSL_cache_hit(SSL *s)
3312 int SSL_is_server(SSL *s)