2 * ! \file ssl/ssl_lib.c \brief Version independent SSL functions.
4 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
7 * This package is an SSL implementation written
8 * by Eric Young (eay@cryptsoft.com).
9 * The implementation was written so as to conform with Netscapes SSL.
11 * This library is free for commercial and non-commercial use as long as
12 * the following conditions are aheared to. The following conditions
13 * apply to all code found in this distribution, be it the RC4, RSA,
14 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
15 * included with this distribution is covered by the same copyright terms
16 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
18 * Copyright remains Eric Young's, and as such any Copyright notices in
19 * the code are not to be removed.
20 * If this package is used in a product, Eric Young should be given attribution
21 * as the author of the parts of the library used.
22 * This can be in the form of a textual message at program startup or
23 * in documentation (online or textual) provided with the package.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 3. All advertising materials mentioning features or use of this software
34 * must display the following acknowledgement:
35 * "This product includes cryptographic software written by
36 * Eric Young (eay@cryptsoft.com)"
37 * The word 'cryptographic' can be left out if the rouines from the library
38 * being used are not cryptographic related :-).
39 * 4. If you include any Windows specific code (or a derivative thereof) from
40 * the apps directory (application code) you must include an acknowledgement:
41 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
43 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * The licence and distribution terms for any publically available version or
56 * derivative of this code cannot be changed. i.e. this code cannot simply be
57 * copied and put under another distribution licence
58 * [including the GNU Public Licence.]
60 /* ====================================================================
61 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in
72 * the documentation and/or other materials provided with the
75 * 3. All advertising materials mentioning features or use of this
76 * software must display the following acknowledgment:
77 * "This product includes software developed by the OpenSSL Project
78 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
80 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
81 * endorse or promote products derived from this software without
82 * prior written permission. For written permission, please contact
83 * openssl-core@openssl.org.
85 * 5. Products derived from this software may not be called "OpenSSL"
86 * nor may "OpenSSL" appear in their names without prior written
87 * permission of the OpenSSL Project.
89 * 6. Redistributions of any form whatsoever must retain the following
91 * "This product includes software developed by the OpenSSL Project
92 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
94 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
95 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
97 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
98 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
101 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105 * OF THE POSSIBILITY OF SUCH DAMAGE.
106 * ====================================================================
108 * This product includes cryptographic software written by Eric Young
109 * (eay@cryptsoft.com). This product includes software written by Tim
110 * Hudson (tjh@cryptsoft.com).
113 /* ====================================================================
114 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
115 * ECC cipher suite support in OpenSSL originally developed by
116 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
118 /* ====================================================================
119 * Copyright 2005 Nokia. All rights reserved.
121 * The portions of the attached software ("Contribution") is developed by
122 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
125 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
126 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
127 * support (see RFC 4279) to OpenSSL.
129 * No patent licenses or other rights except those expressly stated in
130 * the OpenSSL open source license shall be deemed granted or received
131 * expressly, by implication, estoppel, or otherwise.
133 * No assurances are provided by Nokia that the Contribution does not
134 * infringe the patent or other intellectual property rights of any third
135 * party or that the license provides you with all the necessary rights
136 * to make use of the Contribution.
138 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
139 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
140 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
141 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
149 #include "ssl_locl.h"
150 #include "kssl_lcl.h"
151 #include <openssl/objects.h>
152 #include <openssl/lhash.h>
153 #include <openssl/x509v3.h>
154 #include <openssl/rand.h>
155 #include <openssl/ocsp.h>
156 #ifndef OPENSSL_NO_DH
157 # include <openssl/dh.h>
159 #ifndef OPENSSL_NO_ENGINE
160 # include <openssl/engine.h>
163 const char *SSL_version_str = OPENSSL_VERSION_TEXT;
165 SSL3_ENC_METHOD ssl3_undef_enc_method = {
167 * evil casts, but these functions are only called if there's a library
170 (int (*)(SSL *, int))ssl_undefined_function,
171 (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
172 ssl_undefined_function,
173 (int (*)(SSL *, unsigned char *, unsigned char *, int))
174 ssl_undefined_function,
175 (int (*)(SSL *, int))ssl_undefined_function,
176 (int (*)(SSL *, const char *, int, unsigned char *))
177 ssl_undefined_function,
178 0, /* finish_mac_length */
179 (int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
180 NULL, /* client_finished_label */
181 0, /* client_finished_label_len */
182 NULL, /* server_finished_label */
183 0, /* server_finished_label_len */
184 (int (*)(int))ssl_undefined_function,
185 (int (*)(SSL *, unsigned char *, size_t, const char *,
186 size_t, const unsigned char *, size_t,
187 int use_context))ssl_undefined_function,
190 int SSL_clear(SSL *s)
192 unsigned char *rp, *wp;
196 if (s->method == NULL) {
197 SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED);
201 if (ssl_clear_bad_session(s)) {
202 SSL_SESSION_free(s->session);
210 if (s->renegotiate) {
211 SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR);
217 s->state = SSL_ST_BEFORE | ((s->server) ? SSL_ST_ACCEPT : SSL_ST_CONNECT);
219 s->version = s->method->version;
220 s->client_version = s->version;
221 s->rwstate = SSL_NOTHING;
222 s->rstate = SSL_ST_READ_HEADER;
224 if (s->init_buf != NULL) {
225 BUF_MEM_free(s->init_buf);
229 ssl_clear_cipher_ctx(s);
230 ssl_clear_hash_ctx(&s->read_hash);
231 ssl_clear_hash_ctx(&s->write_hash);
236 * Check to see if we were changed into a different method, if so, revert
237 * back if we are not doing session-id reuse.
239 if (!s->in_handshake && (s->session == NULL)
240 && (s->method != s->ctx->method)) {
241 s->method->ssl_free(s);
242 s->method = s->ctx->method;
243 if (!s->method->ssl_new(s))
246 s->method->ssl_clear(s);
248 read_ahead = RECORD_LAYER_get_read_ahead(&s->rlayer);
249 rp = SSL3_BUFFER_get_buf(RECORD_LAYER_get_rbuf(&s->rlayer));
250 rlen = SSL3_BUFFER_get_len(RECORD_LAYER_get_rbuf(&s->rlayer));
251 wp = SSL3_BUFFER_get_buf(RECORD_LAYER_get_wbuf(&s->rlayer));
252 wlen = SSL3_BUFFER_get_len(RECORD_LAYER_get_wbuf(&s->rlayer));
253 memset(&s->rlayer, 0, sizeof s->rlayer);
254 SSL3_BUFFER_set_buf(RECORD_LAYER_get_rbuf(&s->rlayer), rp);
255 SSL3_BUFFER_set_len(RECORD_LAYER_get_rbuf(&s->rlayer), rlen);
256 SSL3_BUFFER_set_buf(RECORD_LAYER_get_wbuf(&s->rlayer), wp);
257 SSL3_BUFFER_set_len(RECORD_LAYER_get_wbuf(&s->rlayer), wlen);
259 /* Do I need to do this? As far as I can tell read_ahead did not
260 * previously get reset by SSL_clear...so I'll keep it that way..but is
263 RECORD_LAYER_set_read_ahead(&s->rlayer, read_ahead);
264 RECORD_LAYER_set_ssl(&s->rlayer, s);
269 /** Used to change an SSL_CTXs default SSL method type */
270 int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
272 STACK_OF(SSL_CIPHER) *sk;
276 sk = ssl_create_cipher_list(ctx->method, &(ctx->cipher_list),
277 &(ctx->cipher_list_by_id),
278 SSL_DEFAULT_CIPHER_LIST, ctx->cert);
279 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
280 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,
281 SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
287 SSL *SSL_new(SSL_CTX *ctx)
292 SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);
295 if (ctx->method == NULL) {
296 SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
300 s = (SSL *)OPENSSL_malloc(sizeof(SSL));
303 memset(s, 0, sizeof(SSL));
305 RECORD_LAYER_set_ssl(&s->rlayer, s);
306 SSL3_RECORD_clear(RECORD_LAYER_get_rrec(&s->rlayer));
307 SSL3_RECORD_clear(RECORD_LAYER_get_wrec(&s->rlayer));
309 #ifndef OPENSSL_NO_KRB5
310 s->kssl_ctx = kssl_ctx_new();
311 #endif /* OPENSSL_NO_KRB5 */
313 s->options = ctx->options;
315 s->max_cert_list = ctx->max_cert_list;
318 * Earlier library versions used to copy the pointer to the CERT, not
319 * its contents; only when setting new parameters for the per-SSL
320 * copy, ssl_cert_new would be called (and the direct reference to
321 * the per-SSL_CTX settings would be lost, but those still were
322 * indirectly accessed for various purposes, and for that reason they
323 * used to be known as s->ctx->default_cert). Now we don't look at the
324 * SSL_CTX's CERT after having duplicated it once.
326 s->cert = ssl_cert_dup(ctx->cert);
330 RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
331 s->msg_callback = ctx->msg_callback;
332 s->msg_callback_arg = ctx->msg_callback_arg;
333 s->verify_mode = ctx->verify_mode;
334 s->not_resumable_session_cb = ctx->not_resumable_session_cb;
335 s->sid_ctx_length = ctx->sid_ctx_length;
336 OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
337 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
338 s->verify_callback = ctx->default_verify_callback;
339 s->generate_session_id = ctx->generate_session_id;
341 s->param = X509_VERIFY_PARAM_new();
344 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
345 s->quiet_shutdown = ctx->quiet_shutdown;
346 s->max_send_fragment = ctx->max_send_fragment;
348 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
350 #ifndef OPENSSL_NO_TLSEXT
351 s->tlsext_debug_cb = 0;
352 s->tlsext_debug_arg = NULL;
353 s->tlsext_ticket_expected = 0;
354 s->tlsext_status_type = -1;
355 s->tlsext_status_expected = 0;
356 s->tlsext_ocsp_ids = NULL;
357 s->tlsext_ocsp_exts = NULL;
358 s->tlsext_ocsp_resp = NULL;
359 s->tlsext_ocsp_resplen = -1;
360 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
361 s->initial_ctx = ctx;
362 # ifndef OPENSSL_NO_EC
363 if (ctx->tlsext_ecpointformatlist) {
364 s->tlsext_ecpointformatlist =
365 BUF_memdup(ctx->tlsext_ecpointformatlist,
366 ctx->tlsext_ecpointformatlist_length);
367 if (!s->tlsext_ecpointformatlist)
369 s->tlsext_ecpointformatlist_length =
370 ctx->tlsext_ecpointformatlist_length;
372 if (ctx->tlsext_ellipticcurvelist) {
373 s->tlsext_ellipticcurvelist =
374 BUF_memdup(ctx->tlsext_ellipticcurvelist,
375 ctx->tlsext_ellipticcurvelist_length);
376 if (!s->tlsext_ellipticcurvelist)
378 s->tlsext_ellipticcurvelist_length =
379 ctx->tlsext_ellipticcurvelist_length;
382 # ifndef OPENSSL_NO_NEXTPROTONEG
383 s->next_proto_negotiated = NULL;
386 if (s->ctx->alpn_client_proto_list) {
387 s->alpn_client_proto_list =
388 OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);
389 if (s->alpn_client_proto_list == NULL)
391 memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,
392 s->ctx->alpn_client_proto_list_len);
393 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
397 s->verify_result = X509_V_OK;
399 s->method = ctx->method;
401 if (!s->method->ssl_new(s))
405 s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
410 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
412 #ifndef OPENSSL_NO_PSK
413 s->psk_client_callback = ctx->psk_client_callback;
414 s->psk_server_callback = ctx->psk_server_callback;
421 SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
425 int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
426 unsigned int sid_ctx_len)
428 if (sid_ctx_len > sizeof ctx->sid_ctx) {
429 SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,
430 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
433 ctx->sid_ctx_length = sid_ctx_len;
434 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
439 int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
440 unsigned int sid_ctx_len)
442 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
443 SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,
444 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
447 ssl->sid_ctx_length = sid_ctx_len;
448 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
453 int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
455 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
456 ctx->generate_session_id = cb;
457 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
461 int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
463 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
464 ssl->generate_session_id = cb;
465 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
469 int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
473 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
474 * we can "construct" a session to give us the desired check - ie. to
475 * find if there's a session in the hash table that would conflict with
476 * any new session built out of this id/id_len and the ssl_version in use
481 if (id_len > sizeof r.session_id)
484 r.ssl_version = ssl->version;
485 r.session_id_length = id_len;
486 memcpy(r.session_id, id, id_len);
488 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
489 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
490 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
494 int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
496 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
499 int SSL_set_purpose(SSL *s, int purpose)
501 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
504 int SSL_CTX_set_trust(SSL_CTX *s, int trust)
506 return X509_VERIFY_PARAM_set_trust(s->param, trust);
509 int SSL_set_trust(SSL *s, int trust)
511 return X509_VERIFY_PARAM_set_trust(s->param, trust);
514 int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
516 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
519 int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
521 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
524 X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
529 X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
534 void SSL_certs_clear(SSL *s)
536 ssl_cert_clear_certs(s->cert);
539 void SSL_free(SSL *s)
546 i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);
554 fprintf(stderr, "SSL_free, bad reference count\n");
560 X509_VERIFY_PARAM_free(s->param);
562 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
564 if (s->bbio != NULL) {
565 /* If the buffering BIO is in place, pop it off */
566 if (s->bbio == s->wbio) {
567 s->wbio = BIO_pop(s->wbio);
572 BIO_free_all(s->rbio);
573 if (s->wbio != s->rbio)
574 BIO_free_all(s->wbio);
576 if (s->init_buf != NULL)
577 BUF_MEM_free(s->init_buf);
579 /* add extra stuff */
580 if (s->cipher_list != NULL)
581 sk_SSL_CIPHER_free(s->cipher_list);
582 if (s->cipher_list_by_id != NULL)
583 sk_SSL_CIPHER_free(s->cipher_list_by_id);
585 /* Make the next call work :-) */
586 if (s->session != NULL) {
587 ssl_clear_bad_session(s);
588 SSL_SESSION_free(s->session);
591 ssl_clear_cipher_ctx(s);
592 ssl_clear_hash_ctx(&s->read_hash);
593 ssl_clear_hash_ctx(&s->write_hash);
596 ssl_cert_free(s->cert);
597 /* Free up if allocated */
599 #ifndef OPENSSL_NO_TLSEXT
600 if (s->tlsext_hostname)
601 OPENSSL_free(s->tlsext_hostname);
603 SSL_CTX_free(s->initial_ctx);
604 # ifndef OPENSSL_NO_EC
605 if (s->tlsext_ecpointformatlist)
606 OPENSSL_free(s->tlsext_ecpointformatlist);
607 if (s->tlsext_ellipticcurvelist)
608 OPENSSL_free(s->tlsext_ellipticcurvelist);
609 # endif /* OPENSSL_NO_EC */
610 if (s->tlsext_ocsp_exts)
611 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free);
612 if (s->tlsext_ocsp_ids)
613 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
614 if (s->tlsext_ocsp_resp)
615 OPENSSL_free(s->tlsext_ocsp_resp);
616 if (s->alpn_client_proto_list)
617 OPENSSL_free(s->alpn_client_proto_list);
620 if (s->client_CA != NULL)
621 sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
623 if (s->method != NULL)
624 s->method->ssl_free(s);
626 if (SSL3_BUFFER_is_initialised(RECORD_LAYER_get_rbuf(&s->rlayer)))
627 ssl3_release_read_buffer(s);
628 if (SSL3_BUFFER_is_initialised(RECORD_LAYER_get_wbuf(&s->rlayer)))
629 ssl3_release_write_buffer(s);
630 SSL3_RECORD_release(RECORD_LAYER_get_rrec(&s->rlayer));
633 SSL_CTX_free(s->ctx);
635 #ifndef OPENSSL_NO_KRB5
636 if (s->kssl_ctx != NULL)
637 kssl_ctx_free(s->kssl_ctx);
638 #endif /* OPENSSL_NO_KRB5 */
640 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
641 if (s->next_proto_negotiated)
642 OPENSSL_free(s->next_proto_negotiated);
645 #ifndef OPENSSL_NO_SRTP
646 if (s->srtp_profiles)
647 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
653 void SSL_set_rbio(SSL *s, BIO *rbio)
656 BIO_free_all(s->rbio);
660 void SSL_set_wbio(SSL *s, BIO *wbio)
663 * If the output buffering BIO is still in place, remove it
665 if (s->bbio != NULL) {
666 if (s->wbio == s->bbio) {
667 s->wbio = s->wbio->next_bio;
668 s->bbio->next_bio = NULL;
671 if (s->wbio != wbio && s->rbio != s->wbio)
672 BIO_free_all(s->wbio);
676 void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
678 SSL_set_wbio(s, wbio);
679 SSL_set_rbio(s, rbio);
682 BIO *SSL_get_rbio(const SSL *s)
687 BIO *SSL_get_wbio(const SSL *s)
692 int SSL_get_fd(const SSL *s)
694 return (SSL_get_rfd(s));
697 int SSL_get_rfd(const SSL *s)
703 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
709 int SSL_get_wfd(const SSL *s)
715 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
721 #ifndef OPENSSL_NO_SOCK
722 int SSL_set_fd(SSL *s, int fd)
727 bio = BIO_new(BIO_s_socket());
730 SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
733 BIO_set_fd(bio, fd, BIO_NOCLOSE);
734 SSL_set_bio(s, bio, bio);
740 int SSL_set_wfd(SSL *s, int fd)
745 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
746 || ((int)BIO_get_fd(s->rbio, NULL) != fd)) {
747 bio = BIO_new(BIO_s_socket());
750 SSLerr(SSL_F_SSL_SET_WFD, ERR_R_BUF_LIB);
753 BIO_set_fd(bio, fd, BIO_NOCLOSE);
754 SSL_set_bio(s, SSL_get_rbio(s), bio);
756 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
762 int SSL_set_rfd(SSL *s, int fd)
767 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
768 || ((int)BIO_get_fd(s->wbio, NULL) != fd)) {
769 bio = BIO_new(BIO_s_socket());
772 SSLerr(SSL_F_SSL_SET_RFD, ERR_R_BUF_LIB);
775 BIO_set_fd(bio, fd, BIO_NOCLOSE);
776 SSL_set_bio(s, bio, SSL_get_wbio(s));
778 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
785 /* return length of latest Finished message we sent, copy to 'buf' */
786 size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
791 ret = s->s3->tmp.finish_md_len;
794 memcpy(buf, s->s3->tmp.finish_md, count);
799 /* return length of latest Finished message we expected, copy to 'buf' */
800 size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
805 ret = s->s3->tmp.peer_finish_md_len;
808 memcpy(buf, s->s3->tmp.peer_finish_md, count);
813 int SSL_get_verify_mode(const SSL *s)
815 return (s->verify_mode);
818 int SSL_get_verify_depth(const SSL *s)
820 return X509_VERIFY_PARAM_get_depth(s->param);
823 int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) {
824 return (s->verify_callback);
827 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
829 return (ctx->verify_mode);
832 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
834 return X509_VERIFY_PARAM_get_depth(ctx->param);
837 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) {
838 return (ctx->default_verify_callback);
841 void SSL_set_verify(SSL *s, int mode,
842 int (*callback) (int ok, X509_STORE_CTX *ctx))
844 s->verify_mode = mode;
845 if (callback != NULL)
846 s->verify_callback = callback;
849 void SSL_set_verify_depth(SSL *s, int depth)
851 X509_VERIFY_PARAM_set_depth(s->param, depth);
854 void SSL_set_read_ahead(SSL *s, int yes)
856 RECORD_LAYER_set_read_ahead(&s->rlayer, yes);
859 int SSL_get_read_ahead(const SSL *s)
861 return RECORD_LAYER_get_read_ahead(&s->rlayer);
864 int SSL_pending(const SSL *s)
867 * SSL_pending cannot work properly if read-ahead is enabled
868 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
869 * impossible to fix since SSL_pending cannot report errors that may be
870 * observed while scanning the new data. (Note that SSL_pending() is
871 * often used as a boolean value, so we'd better not return -1.)
873 return (s->method->ssl_pending(s));
876 X509 *SSL_get_peer_certificate(const SSL *s)
880 if ((s == NULL) || (s->session == NULL))
883 r = s->session->peer;
888 CRYPTO_add(&r->references, 1, CRYPTO_LOCK_X509);
893 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
897 if ((s == NULL) || (s->session == NULL)
898 || (s->session->sess_cert == NULL))
901 r = s->session->sess_cert->cert_chain;
904 * If we are a client, cert_chain includes the peer's own certificate; if
905 * we are a server, it does not.
912 * Now in theory, since the calling process own 't' it should be safe to
913 * modify. We need to be able to read f without being hassled
915 int SSL_copy_session_id(SSL *t, const SSL *f)
917 /* Do we need to to SSL locking? */
918 if(!SSL_set_session(t, SSL_get_session(f))) {
923 * what if we are setup as SSLv2 but want to talk SSLv3 or vice-versa
925 if (t->method != f->method) {
926 t->method->ssl_free(t); /* cleanup current */
927 t->method = f->method; /* change method */
928 t->method->ssl_new(t); /* setup new */
931 CRYPTO_add(&f->cert->references, 1, CRYPTO_LOCK_SSL_CERT);
932 ssl_cert_free(t->cert);
934 if(!SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length)) {
941 /* Fix this so it checks all the valid key/cert options */
942 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
945 (ctx->cert->key->x509 == NULL)) {
946 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,
947 SSL_R_NO_CERTIFICATE_ASSIGNED);
950 if (ctx->cert->key->privatekey == NULL) {
951 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,
952 SSL_R_NO_PRIVATE_KEY_ASSIGNED);
955 return (X509_check_private_key
956 (ctx->cert->key->x509, ctx->cert->key->privatekey));
959 /* Fix this function so that it takes an optional type parameter */
960 int SSL_check_private_key(const SSL *ssl)
963 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
966 if (ssl->cert->key->x509 == NULL) {
967 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED);
970 if (ssl->cert->key->privatekey == NULL) {
971 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
974 return (X509_check_private_key(ssl->cert->key->x509,
975 ssl->cert->key->privatekey));
978 int SSL_accept(SSL *s)
980 if (s->handshake_func == 0)
981 /* Not properly initialized yet */
982 SSL_set_accept_state(s);
984 return (s->method->ssl_accept(s));
987 int SSL_connect(SSL *s)
989 if (s->handshake_func == 0)
990 /* Not properly initialized yet */
991 SSL_set_connect_state(s);
993 return (s->method->ssl_connect(s));
996 long SSL_get_default_timeout(const SSL *s)
998 return (s->method->get_timeout());
1001 int SSL_read(SSL *s, void *buf, int num)
1003 if (s->handshake_func == 0) {
1004 SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
1008 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
1009 s->rwstate = SSL_NOTHING;
1012 return (s->method->ssl_read(s, buf, num));
1015 int SSL_peek(SSL *s, void *buf, int num)
1017 if (s->handshake_func == 0) {
1018 SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
1022 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
1025 return (s->method->ssl_peek(s, buf, num));
1028 int SSL_write(SSL *s, const void *buf, int num)
1030 if (s->handshake_func == 0) {
1031 SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
1035 if (s->shutdown & SSL_SENT_SHUTDOWN) {
1036 s->rwstate = SSL_NOTHING;
1037 SSLerr(SSL_F_SSL_WRITE, SSL_R_PROTOCOL_IS_SHUTDOWN);
1040 return (s->method->ssl_write(s, buf, num));
1043 int SSL_shutdown(SSL *s)
1046 * Note that this function behaves differently from what one might
1047 * expect. Return values are 0 for no success (yet), 1 for success; but
1048 * calling it once is usually not enough, even if blocking I/O is used
1049 * (see ssl3_shutdown).
1052 if (s->handshake_func == 0) {
1053 SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
1057 if ((s != NULL) && !SSL_in_init(s))
1058 return (s->method->ssl_shutdown(s));
1063 int SSL_renegotiate(SSL *s)
1065 if (s->renegotiate == 0)
1070 return (s->method->ssl_renegotiate(s));
1073 int SSL_renegotiate_abbreviated(SSL *s)
1075 if (s->renegotiate == 0)
1080 return (s->method->ssl_renegotiate(s));
1083 int SSL_renegotiate_pending(SSL *s)
1086 * becomes true when negotiation is requested; false again once a
1087 * handshake has finished
1089 return (s->renegotiate != 0);
1092 long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
1097 case SSL_CTRL_GET_READ_AHEAD:
1098 return (RECORD_LAYER_get_read_ahead(&s->rlayer));
1099 case SSL_CTRL_SET_READ_AHEAD:
1100 l = RECORD_LAYER_get_read_ahead(&s->rlayer);
1101 RECORD_LAYER_set_read_ahead(&s->rlayer, larg);
1104 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1105 s->msg_callback_arg = parg;
1108 case SSL_CTRL_OPTIONS:
1109 return (s->options |= larg);
1110 case SSL_CTRL_CLEAR_OPTIONS:
1111 return (s->options &= ~larg);
1113 return (s->mode |= larg);
1114 case SSL_CTRL_CLEAR_MODE:
1115 return (s->mode &= ~larg);
1116 case SSL_CTRL_GET_MAX_CERT_LIST:
1117 return (s->max_cert_list);
1118 case SSL_CTRL_SET_MAX_CERT_LIST:
1119 l = s->max_cert_list;
1120 s->max_cert_list = larg;
1122 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1123 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1125 s->max_send_fragment = larg;
1127 case SSL_CTRL_GET_RI_SUPPORT:
1129 return s->s3->send_connection_binding;
1132 case SSL_CTRL_CERT_FLAGS:
1133 return (s->cert->cert_flags |= larg);
1134 case SSL_CTRL_CLEAR_CERT_FLAGS:
1135 return (s->cert->cert_flags &= ~larg);
1137 case SSL_CTRL_GET_RAW_CIPHERLIST:
1139 if (s->cert->ciphers_raw == NULL)
1141 *(unsigned char **)parg = s->cert->ciphers_raw;
1142 return (int)s->cert->ciphers_rawlen;
1144 return ssl_put_cipher_by_char(s, NULL, NULL);
1145 case SSL_CTRL_GET_EXTMS_SUPPORT:
1146 if (!s->session || SSL_in_init(s) || s->in_handshake)
1148 if (s->session->flags & SSL_SESS_FLAG_EXTMS)
1153 return (s->method->ssl_ctrl(s, cmd, larg, parg));
1157 long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
1160 case SSL_CTRL_SET_MSG_CALLBACK:
1161 s->msg_callback = (void (*)
1162 (int write_p, int version, int content_type,
1163 const void *buf, size_t len, SSL *ssl,
1168 return (s->method->ssl_callback_ctrl(s, cmd, fp));
1172 LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1174 return ctx->sessions;
1177 long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1180 /* For some cases with ctx == NULL perform syntax checks */
1183 #ifndef OPENSSL_NO_EC
1184 case SSL_CTRL_SET_CURVES_LIST:
1185 return tls1_set_curves_list(NULL, NULL, parg);
1187 case SSL_CTRL_SET_SIGALGS_LIST:
1188 case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
1189 return tls1_set_sigalgs_list(NULL, parg, 0);
1196 case SSL_CTRL_GET_READ_AHEAD:
1197 return (ctx->read_ahead);
1198 case SSL_CTRL_SET_READ_AHEAD:
1199 l = ctx->read_ahead;
1200 ctx->read_ahead = larg;
1203 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1204 ctx->msg_callback_arg = parg;
1207 case SSL_CTRL_GET_MAX_CERT_LIST:
1208 return (ctx->max_cert_list);
1209 case SSL_CTRL_SET_MAX_CERT_LIST:
1210 l = ctx->max_cert_list;
1211 ctx->max_cert_list = larg;
1214 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1215 l = ctx->session_cache_size;
1216 ctx->session_cache_size = larg;
1218 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1219 return (ctx->session_cache_size);
1220 case SSL_CTRL_SET_SESS_CACHE_MODE:
1221 l = ctx->session_cache_mode;
1222 ctx->session_cache_mode = larg;
1224 case SSL_CTRL_GET_SESS_CACHE_MODE:
1225 return (ctx->session_cache_mode);
1227 case SSL_CTRL_SESS_NUMBER:
1228 return (lh_SSL_SESSION_num_items(ctx->sessions));
1229 case SSL_CTRL_SESS_CONNECT:
1230 return (ctx->stats.sess_connect);
1231 case SSL_CTRL_SESS_CONNECT_GOOD:
1232 return (ctx->stats.sess_connect_good);
1233 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1234 return (ctx->stats.sess_connect_renegotiate);
1235 case SSL_CTRL_SESS_ACCEPT:
1236 return (ctx->stats.sess_accept);
1237 case SSL_CTRL_SESS_ACCEPT_GOOD:
1238 return (ctx->stats.sess_accept_good);
1239 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1240 return (ctx->stats.sess_accept_renegotiate);
1241 case SSL_CTRL_SESS_HIT:
1242 return (ctx->stats.sess_hit);
1243 case SSL_CTRL_SESS_CB_HIT:
1244 return (ctx->stats.sess_cb_hit);
1245 case SSL_CTRL_SESS_MISSES:
1246 return (ctx->stats.sess_miss);
1247 case SSL_CTRL_SESS_TIMEOUTS:
1248 return (ctx->stats.sess_timeout);
1249 case SSL_CTRL_SESS_CACHE_FULL:
1250 return (ctx->stats.sess_cache_full);
1251 case SSL_CTRL_OPTIONS:
1252 return (ctx->options |= larg);
1253 case SSL_CTRL_CLEAR_OPTIONS:
1254 return (ctx->options &= ~larg);
1256 return (ctx->mode |= larg);
1257 case SSL_CTRL_CLEAR_MODE:
1258 return (ctx->mode &= ~larg);
1259 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1260 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1262 ctx->max_send_fragment = larg;
1264 case SSL_CTRL_CERT_FLAGS:
1265 return (ctx->cert->cert_flags |= larg);
1266 case SSL_CTRL_CLEAR_CERT_FLAGS:
1267 return (ctx->cert->cert_flags &= ~larg);
1269 return (ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg));
1273 long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
1276 case SSL_CTRL_SET_MSG_CALLBACK:
1277 ctx->msg_callback = (void (*)
1278 (int write_p, int version, int content_type,
1279 const void *buf, size_t len, SSL *ssl,
1284 return (ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp));
1288 int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
1296 return ((l > 0) ? 1 : -1);
1299 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
1300 const SSL_CIPHER *const *bp)
1304 l = (*ap)->id - (*bp)->id;
1308 return ((l > 0) ? 1 : -1);
1311 /** return a STACK of the ciphers available for the SSL and in order of
1313 STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1316 if (s->cipher_list != NULL) {
1317 return (s->cipher_list);
1318 } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
1319 return (s->ctx->cipher_list);
1325 STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
1327 STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
1329 ciphers = SSL_get_ciphers(s);
1332 ssl_set_client_disabled(s);
1333 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1334 const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
1335 if (!ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED)) {
1337 sk = sk_SSL_CIPHER_new_null();
1340 if (!sk_SSL_CIPHER_push(sk, c)) {
1341 sk_SSL_CIPHER_free(sk);
1349 /** return a STACK of the ciphers available for the SSL and in order of
1351 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1354 if (s->cipher_list_by_id != NULL) {
1355 return (s->cipher_list_by_id);
1356 } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) {
1357 return (s->ctx->cipher_list_by_id);
1363 /** The old interface to get the same thing as SSL_get_ciphers() */
1364 const char *SSL_get_cipher_list(const SSL *s, int n)
1367 STACK_OF(SSL_CIPHER) *sk;
1371 sk = SSL_get_ciphers(s);
1372 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1374 c = sk_SSL_CIPHER_value(sk, n);
1380 /** specify the ciphers to be used by default by the SSL_CTX */
1381 int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1383 STACK_OF(SSL_CIPHER) *sk;
1385 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1386 &ctx->cipher_list_by_id, str, ctx->cert);
1388 * ssl_create_cipher_list may return an empty stack if it was unable to
1389 * find a cipher matching the given rule string (for example if the rule
1390 * string specifies a cipher which has been disabled). This is not an
1391 * error as far as ssl_create_cipher_list is concerned, and hence
1392 * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
1396 else if (sk_SSL_CIPHER_num(sk) == 0) {
1397 SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1403 /** specify the ciphers to be used by the SSL */
1404 int SSL_set_cipher_list(SSL *s, const char *str)
1406 STACK_OF(SSL_CIPHER) *sk;
1408 sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1409 &s->cipher_list_by_id, str, s->cert);
1410 /* see comment in SSL_CTX_set_cipher_list */
1413 else if (sk_SSL_CIPHER_num(sk) == 0) {
1414 SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1420 /* works well for SSLv2, not so good for SSLv3 */
1421 char *SSL_get_shared_ciphers(const SSL *s, char *buf, int len)
1424 STACK_OF(SSL_CIPHER) *sk;
1428 if ((s->session == NULL) || (s->session->ciphers == NULL) || (len < 2))
1432 sk = s->session->ciphers;
1434 if (sk_SSL_CIPHER_num(sk) == 0)
1437 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1440 c = sk_SSL_CIPHER_value(sk, i);
1441 n = strlen(c->name);
1457 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
1459 int (*put_cb) (const SSL_CIPHER *,
1465 int empty_reneg_info_scsv = !s->renegotiate;
1466 /* Set disabled masks for this session */
1467 ssl_set_client_disabled(s);
1473 put_cb = s->method->put_cipher_by_char;
1475 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1476 c = sk_SSL_CIPHER_value(sk, i);
1477 /* Skip disabled ciphers */
1478 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
1480 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1481 if (c->id == SSL3_CK_SCSV) {
1482 if (!empty_reneg_info_scsv)
1485 empty_reneg_info_scsv = 0;
1492 * If p == q, no ciphers; caller indicates an error. Otherwise, add
1496 if (empty_reneg_info_scsv) {
1497 static SSL_CIPHER scsv = {
1498 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1500 j = put_cb(&scsv, p);
1502 #ifdef OPENSSL_RI_DEBUG
1504 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
1507 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
1508 static SSL_CIPHER scsv = {
1509 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1511 j = put_cb(&scsv, p);
1519 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, unsigned char *p,
1521 STACK_OF(SSL_CIPHER) **skp)
1523 const SSL_CIPHER *c;
1524 STACK_OF(SSL_CIPHER) *sk;
1528 s->s3->send_connection_binding = 0;
1530 n = ssl_put_cipher_by_char(s, NULL, NULL);
1531 if (n == 0 || (num % n) != 0) {
1532 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
1533 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1536 if ((skp == NULL) || (*skp == NULL))
1537 sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */
1540 sk_SSL_CIPHER_zero(sk);
1543 if (s->cert->ciphers_raw)
1544 OPENSSL_free(s->cert->ciphers_raw);
1545 s->cert->ciphers_raw = BUF_memdup(p, num);
1546 if (s->cert->ciphers_raw == NULL) {
1547 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
1550 s->cert->ciphers_rawlen = (size_t)num;
1552 for (i = 0; i < num; i += n) {
1553 /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
1554 if (s->s3 && (n != 3 || !p[0]) &&
1555 (p[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
1556 (p[n - 1] == (SSL3_CK_SCSV & 0xff))) {
1557 /* SCSV fatal if renegotiating */
1558 if (s->renegotiate) {
1559 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
1560 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1561 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1564 s->s3->send_connection_binding = 1;
1566 #ifdef OPENSSL_RI_DEBUG
1567 fprintf(stderr, "SCSV received by server\n");
1572 /* Check for TLS_FALLBACK_SCSV */
1573 if ((n != 3 || !p[0]) &&
1574 (p[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
1575 (p[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
1577 * The SCSV indicates that the client previously tried a higher
1578 * version. Fail if the current version is an unexpected
1581 if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL)) {
1582 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
1583 SSL_R_INAPPROPRIATE_FALLBACK);
1585 ssl3_send_alert(s, SSL3_AL_FATAL,
1586 SSL_AD_INAPPROPRIATE_FALLBACK);
1593 c = ssl_get_cipher_by_char(s, p);
1596 if (!sk_SSL_CIPHER_push(sk, c)) {
1597 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
1607 if ((skp == NULL) || (*skp == NULL))
1608 sk_SSL_CIPHER_free(sk);
1612 #ifndef OPENSSL_NO_TLSEXT
1613 /** return a servername extension value if provided in Client Hello, or NULL.
1614 * So far, only host_name types are defined (RFC 3546).
1617 const char *SSL_get_servername(const SSL *s, const int type)
1619 if (type != TLSEXT_NAMETYPE_host_name)
1622 return s->session && !s->tlsext_hostname ?
1623 s->session->tlsext_hostname : s->tlsext_hostname;
1626 int SSL_get_servername_type(const SSL *s)
1629 && (!s->tlsext_hostname ? s->session->
1630 tlsext_hostname : s->tlsext_hostname))
1631 return TLSEXT_NAMETYPE_host_name;
1636 * SSL_select_next_proto implements the standard protocol selection. It is
1637 * expected that this function is called from the callback set by
1638 * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
1639 * vector of 8-bit, length prefixed byte strings. The length byte itself is
1640 * not included in the length. A byte string of length 0 is invalid. No byte
1641 * string may be truncated. The current, but experimental algorithm for
1642 * selecting the protocol is: 1) If the server doesn't support NPN then this
1643 * is indicated to the callback. In this case, the client application has to
1644 * abort the connection or have a default application level protocol. 2) If
1645 * the server supports NPN, but advertises an empty list then the client
1646 * selects the first protcol in its list, but indicates via the API that this
1647 * fallback case was enacted. 3) Otherwise, the client finds the first
1648 * protocol in the server's list that it supports and selects this protocol.
1649 * This is because it's assumed that the server has better information about
1650 * which protocol a client should use. 4) If the client doesn't support any
1651 * of the server's advertised protocols, then this is treated the same as
1652 * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
1653 * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1655 int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
1656 const unsigned char *server,
1657 unsigned int server_len,
1658 const unsigned char *client,
1659 unsigned int client_len)
1662 const unsigned char *result;
1663 int status = OPENSSL_NPN_UNSUPPORTED;
1666 * For each protocol in server preference order, see if we support it.
1668 for (i = 0; i < server_len;) {
1669 for (j = 0; j < client_len;) {
1670 if (server[i] == client[j] &&
1671 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1672 /* We found a match */
1673 result = &server[i];
1674 status = OPENSSL_NPN_NEGOTIATED;
1684 /* There's no overlap between our protocols and the server's list. */
1686 status = OPENSSL_NPN_NO_OVERLAP;
1689 *out = (unsigned char *)result + 1;
1690 *outlen = result[0];
1694 # ifndef OPENSSL_NO_NEXTPROTONEG
1696 * SSL_get0_next_proto_negotiated sets *data and *len to point to the
1697 * client's requested protocol for this connection and returns 0. If the
1698 * client didn't request any protocol, then *data is set to NULL. Note that
1699 * the client can request any protocol it chooses. The value returned from
1700 * this function need not be a member of the list of supported protocols
1701 * provided by the callback.
1703 void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
1706 *data = s->next_proto_negotiated;
1710 *len = s->next_proto_negotiated_len;
1715 * SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when
1716 * a TLS server needs a list of supported protocols for Next Protocol
1717 * Negotiation. The returned list must be in wire format. The list is
1718 * returned by setting |out| to point to it and |outlen| to its length. This
1719 * memory will not be modified, but one should assume that the SSL* keeps a
1720 * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
1721 * wishes to advertise. Otherwise, no such extension will be included in the
1724 void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx,
1725 int (*cb) (SSL *ssl,
1728 unsigned int *outlen,
1729 void *arg), void *arg)
1731 ctx->next_protos_advertised_cb = cb;
1732 ctx->next_protos_advertised_cb_arg = arg;
1736 * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1737 * client needs to select a protocol from the server's provided list. |out|
1738 * must be set to point to the selected protocol (which may be within |in|).
1739 * The length of the protocol name must be written into |outlen|. The
1740 * server's advertised protocols are provided in |in| and |inlen|. The
1741 * callback can assume that |in| is syntactically valid. The client must
1742 * select a protocol. It is fatal to the connection if this callback returns
1743 * a value other than SSL_TLSEXT_ERR_OK.
1745 void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx,
1746 int (*cb) (SSL *s, unsigned char **out,
1747 unsigned char *outlen,
1748 const unsigned char *in,
1750 void *arg), void *arg)
1752 ctx->next_proto_select_cb = cb;
1753 ctx->next_proto_select_cb_arg = arg;
1758 * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1759 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1760 * length-prefixed strings). Returns 0 on success.
1762 int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
1763 unsigned protos_len)
1765 if (ctx->alpn_client_proto_list)
1766 OPENSSL_free(ctx->alpn_client_proto_list);
1768 ctx->alpn_client_proto_list = OPENSSL_malloc(protos_len);
1769 if (!ctx->alpn_client_proto_list)
1771 memcpy(ctx->alpn_client_proto_list, protos, protos_len);
1772 ctx->alpn_client_proto_list_len = protos_len;
1778 * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1779 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1780 * length-prefixed strings). Returns 0 on success.
1782 int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
1783 unsigned protos_len)
1785 if (ssl->alpn_client_proto_list)
1786 OPENSSL_free(ssl->alpn_client_proto_list);
1788 ssl->alpn_client_proto_list = OPENSSL_malloc(protos_len);
1789 if (!ssl->alpn_client_proto_list)
1791 memcpy(ssl->alpn_client_proto_list, protos, protos_len);
1792 ssl->alpn_client_proto_list_len = protos_len;
1798 * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
1799 * called during ClientHello processing in order to select an ALPN protocol
1800 * from the client's list of offered protocols.
1802 void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
1803 int (*cb) (SSL *ssl,
1804 const unsigned char **out,
1805 unsigned char *outlen,
1806 const unsigned char *in,
1808 void *arg), void *arg)
1810 ctx->alpn_select_cb = cb;
1811 ctx->alpn_select_cb_arg = arg;
1815 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from
1816 * |ssl|. On return it sets |*data| to point to |*len| bytes of protocol name
1817 * (not including the leading length-prefix byte). If the server didn't
1818 * respond with a negotiated protocol then |*len| will be zero.
1820 void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1825 *data = ssl->s3->alpn_selected;
1829 *len = ssl->s3->alpn_selected_len;
1832 #endif /* !OPENSSL_NO_TLSEXT */
1834 int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1835 const char *label, size_t llen,
1836 const unsigned char *p, size_t plen,
1839 if (s->version < TLS1_VERSION)
1842 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1847 static unsigned long ssl_session_hash(const SSL_SESSION *a)
1852 ((unsigned int)a->session_id[0]) |
1853 ((unsigned int)a->session_id[1] << 8L) |
1854 ((unsigned long)a->session_id[2] << 16L) |
1855 ((unsigned long)a->session_id[3] << 24L);
1860 * NB: If this function (or indeed the hash function which uses a sort of
1861 * coarser function than this one) is changed, ensure
1862 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
1863 * being able to construct an SSL_SESSION that will collide with any existing
1864 * session with a matching session ID.
1866 static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1868 if (a->ssl_version != b->ssl_version)
1870 if (a->session_id_length != b->session_id_length)
1872 return (memcmp(a->session_id, b->session_id, a->session_id_length));
1876 * These wrapper functions should remain rather than redeclaring
1877 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1878 * variable. The reason is that the functions aren't static, they're exposed
1881 static IMPLEMENT_LHASH_HASH_FN(ssl_session, SSL_SESSION)
1882 static IMPLEMENT_LHASH_COMP_FN(ssl_session, SSL_SESSION)
1884 SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1886 SSL_CTX *ret = NULL;
1889 SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED);
1893 if (FIPS_mode() && (meth->version < TLS1_VERSION)) {
1894 SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1898 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1899 SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1902 ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1906 memset(ret, 0, sizeof(SSL_CTX));
1910 ret->cert_store = NULL;
1911 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1912 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1913 ret->session_cache_head = NULL;
1914 ret->session_cache_tail = NULL;
1916 /* We take the system default */
1917 ret->session_timeout = meth->get_timeout();
1919 ret->new_session_cb = 0;
1920 ret->remove_session_cb = 0;
1921 ret->get_session_cb = 0;
1922 ret->generate_session_id = 0;
1924 memset((char *)&ret->stats, 0, sizeof(ret->stats));
1926 ret->references = 1;
1927 ret->quiet_shutdown = 0;
1928 ret->info_callback = NULL;
1929 ret->app_verify_callback = 0;
1930 ret->app_verify_arg = NULL;
1931 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1932 ret->read_ahead = 0;
1933 ret->msg_callback = 0;
1934 ret->msg_callback_arg = NULL;
1935 ret->verify_mode = SSL_VERIFY_NONE;
1936 ret->sid_ctx_length = 0;
1937 ret->default_verify_callback = NULL;
1938 if ((ret->cert = ssl_cert_new()) == NULL)
1941 ret->default_passwd_callback = 0;
1942 ret->default_passwd_callback_userdata = NULL;
1943 ret->client_cert_cb = 0;
1944 ret->app_gen_cookie_cb = 0;
1945 ret->app_verify_cookie_cb = 0;
1947 ret->sessions = lh_SSL_SESSION_new();
1948 if (ret->sessions == NULL)
1950 ret->cert_store = X509_STORE_new();
1951 if (ret->cert_store == NULL)
1954 if(!ssl_create_cipher_list(ret->method,
1955 &ret->cipher_list, &ret->cipher_list_by_id,
1956 SSL_DEFAULT_CIPHER_LIST, ret->cert)
1957 || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
1958 SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1962 ret->param = X509_VERIFY_PARAM_new();
1966 if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) {
1967 SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
1970 if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) {
1971 SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
1975 if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL)
1978 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1980 ret->extra_certs = NULL;
1981 /* No compression for DTLS */
1982 if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
1983 ret->comp_methods = SSL_COMP_get_compression_methods();
1985 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1987 #ifndef OPENSSL_NO_TLSEXT
1988 ret->tlsext_servername_callback = 0;
1989 ret->tlsext_servername_arg = NULL;
1990 /* Setup RFC4507 ticket keys */
1991 if ((RAND_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1992 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1993 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1994 ret->options |= SSL_OP_NO_TICKET;
1996 ret->tlsext_status_cb = 0;
1997 ret->tlsext_status_arg = NULL;
1999 # ifndef OPENSSL_NO_NEXTPROTONEG
2000 ret->next_protos_advertised_cb = 0;
2001 ret->next_proto_select_cb = 0;
2004 #ifndef OPENSSL_NO_PSK
2005 ret->psk_identity_hint = NULL;
2006 ret->psk_client_callback = NULL;
2007 ret->psk_server_callback = NULL;
2009 #ifndef OPENSSL_NO_SRP
2010 if(!SSL_CTX_SRP_CTX_init(ret))
2013 #ifndef OPENSSL_NO_ENGINE
2014 ret->client_cert_engine = NULL;
2015 # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
2016 # define eng_strx(x) #x
2017 # define eng_str(x) eng_strx(x)
2018 /* Use specific client engine automatically... ignore errors */
2021 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2024 ENGINE_load_builtin_engines();
2025 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2027 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
2033 * Default is to connect to non-RI servers. When RI is more widely
2034 * deployed might change this.
2036 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2040 SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
2047 void SSL_CTX_free(SSL_CTX *a)
2054 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
2056 REF_PRINT("SSL_CTX", a);
2062 fprintf(stderr, "SSL_CTX_free, bad reference count\n");
2068 X509_VERIFY_PARAM_free(a->param);
2071 * Free internal session cache. However: the remove_cb() may reference
2072 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2073 * after the sessions were flushed.
2074 * As the ex_data handling routines might also touch the session cache,
2075 * the most secure solution seems to be: empty (flush) the cache, then
2076 * free ex_data, then finally free the cache.
2077 * (See ticket [openssl.org #212].)
2079 if (a->sessions != NULL)
2080 SSL_CTX_flush_sessions(a, 0);
2082 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2084 if (a->sessions != NULL)
2085 lh_SSL_SESSION_free(a->sessions);
2087 if (a->cert_store != NULL)
2088 X509_STORE_free(a->cert_store);
2089 if (a->cipher_list != NULL)
2090 sk_SSL_CIPHER_free(a->cipher_list);
2091 if (a->cipher_list_by_id != NULL)
2092 sk_SSL_CIPHER_free(a->cipher_list_by_id);
2093 if (a->cert != NULL)
2094 ssl_cert_free(a->cert);
2095 if (a->client_CA != NULL)
2096 sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
2097 if (a->extra_certs != NULL)
2098 sk_X509_pop_free(a->extra_certs, X509_free);
2099 a->comp_methods = NULL;
2101 #ifndef OPENSSL_NO_SRTP
2102 if (a->srtp_profiles)
2103 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2106 #ifndef OPENSSL_NO_PSK
2107 if (a->psk_identity_hint)
2108 OPENSSL_free(a->psk_identity_hint);
2110 #ifndef OPENSSL_NO_SRP
2111 SSL_CTX_SRP_CTX_free(a);
2113 #ifndef OPENSSL_NO_ENGINE
2114 if (a->client_cert_engine)
2115 ENGINE_finish(a->client_cert_engine);
2118 #ifndef OPENSSL_NO_TLSEXT
2119 # ifndef OPENSSL_NO_EC
2120 if (a->tlsext_ecpointformatlist)
2121 OPENSSL_free(a->tlsext_ecpointformatlist);
2122 if (a->tlsext_ellipticcurvelist)
2123 OPENSSL_free(a->tlsext_ellipticcurvelist);
2124 # endif /* OPENSSL_NO_EC */
2125 if (a->alpn_client_proto_list != NULL)
2126 OPENSSL_free(a->alpn_client_proto_list);
2132 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2134 ctx->default_passwd_callback = cb;
2137 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
2139 ctx->default_passwd_callback_userdata = u;
2142 void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
2143 int (*cb) (X509_STORE_CTX *, void *),
2146 ctx->app_verify_callback = cb;
2147 ctx->app_verify_arg = arg;
2150 void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
2151 int (*cb) (int, X509_STORE_CTX *))
2153 ctx->verify_mode = mode;
2154 ctx->default_verify_callback = cb;
2157 void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
2159 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2162 void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg),
2165 ssl_cert_set_cert_cb(c->cert, cb, arg);
2168 void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg)
2170 ssl_cert_set_cert_cb(s->cert, cb, arg);
2173 void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2176 int rsa_enc, rsa_tmp, rsa_sign, dh_tmp, dh_rsa, dh_dsa, dsa_sign;
2177 int rsa_enc_export, dh_rsa_export, dh_dsa_export;
2178 int rsa_tmp_export, dh_tmp_export, kl;
2179 unsigned long mask_k, mask_a, emask_k, emask_a;
2180 #ifndef OPENSSL_NO_EC
2181 int have_ecc_cert, ecdsa_ok, ecc_pkey_size;
2182 int have_ecdh_tmp, ecdh_ok;
2184 EVP_PKEY *ecc_pkey = NULL;
2185 int signature_nid = 0, pk_nid = 0, md_nid = 0;
2190 kl = SSL_C_EXPORT_PKEYLENGTH(cipher);
2192 #ifndef OPENSSL_NO_RSA
2193 rsa_tmp = (c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
2194 rsa_tmp_export = (c->rsa_tmp_cb != NULL ||
2195 (rsa_tmp && RSA_size(c->rsa_tmp) * 8 <= kl));
2197 rsa_tmp = rsa_tmp_export = 0;
2199 #ifndef OPENSSL_NO_DH
2200 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL || c->dh_tmp_auto);
2201 dh_tmp_export = !c->dh_tmp_auto && (c->dh_tmp_cb != NULL ||
2203 && DH_size(c->dh_tmp) * 8 <= kl));
2205 dh_tmp = dh_tmp_export = 0;
2208 #ifndef OPENSSL_NO_EC
2209 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2211 cpk = &(c->pkeys[SSL_PKEY_RSA_ENC]);
2212 rsa_enc = cpk->valid_flags & CERT_PKEY_VALID;
2213 rsa_enc_export = (rsa_enc && EVP_PKEY_size(cpk->privatekey) * 8 <= kl);
2214 cpk = &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2215 rsa_sign = cpk->valid_flags & CERT_PKEY_SIGN;
2216 cpk = &(c->pkeys[SSL_PKEY_DSA_SIGN]);
2217 dsa_sign = cpk->valid_flags & CERT_PKEY_SIGN;
2218 cpk = &(c->pkeys[SSL_PKEY_DH_RSA]);
2219 dh_rsa = cpk->valid_flags & CERT_PKEY_VALID;
2220 dh_rsa_export = (dh_rsa && EVP_PKEY_size(cpk->privatekey) * 8 <= kl);
2221 cpk = &(c->pkeys[SSL_PKEY_DH_DSA]);
2222 /* FIX THIS EAY EAY EAY */
2223 dh_dsa = cpk->valid_flags & CERT_PKEY_VALID;
2224 dh_dsa_export = (dh_dsa && EVP_PKEY_size(cpk->privatekey) * 8 <= kl);
2225 cpk = &(c->pkeys[SSL_PKEY_ECC]);
2226 #ifndef OPENSSL_NO_EC
2227 have_ecc_cert = cpk->valid_flags & CERT_PKEY_VALID;
2236 "rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2237 rsa_tmp, rsa_tmp_export, dh_tmp, have_ecdh_tmp, rsa_enc,
2238 rsa_enc_export, rsa_sign, dsa_sign, dh_rsa, dh_dsa);
2241 cpk = &(c->pkeys[SSL_PKEY_GOST01]);
2242 if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2243 mask_k |= SSL_kGOST;
2244 mask_a |= SSL_aGOST01;
2246 cpk = &(c->pkeys[SSL_PKEY_GOST94]);
2247 if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2248 mask_k |= SSL_kGOST;
2249 mask_a |= SSL_aGOST94;
2252 if (rsa_enc || (rsa_tmp && rsa_sign))
2254 if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
2255 emask_k |= SSL_kRSA;
2258 emask_k |= SSL_kDHE;
2266 emask_k |= SSL_kDHr;
2271 emask_k |= SSL_kDHd;
2273 if (emask_k & (SSL_kDHr | SSL_kDHd))
2276 if (rsa_enc || rsa_sign) {
2278 emask_a |= SSL_aRSA;
2283 emask_a |= SSL_aDSS;
2286 mask_a |= SSL_aNULL;
2287 emask_a |= SSL_aNULL;
2289 #ifndef OPENSSL_NO_KRB5
2290 mask_k |= SSL_kKRB5;
2291 mask_a |= SSL_aKRB5;
2292 emask_k |= SSL_kKRB5;
2293 emask_a |= SSL_aKRB5;
2297 * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
2298 * depending on the key usage extension.
2300 #ifndef OPENSSL_NO_EC
2301 if (have_ecc_cert) {
2302 cpk = &c->pkeys[SSL_PKEY_ECC];
2304 /* This call populates extension flags (ex_flags) */
2305 X509_check_purpose(x, -1, 0);
2306 ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2307 (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
2308 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2309 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2310 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2312 ecc_pkey = X509_get_pubkey(x);
2313 ecc_pkey_size = (ecc_pkey != NULL) ? EVP_PKEY_bits(ecc_pkey) : 0;
2314 EVP_PKEY_free(ecc_pkey);
2315 if ((x->sig_alg) && (x->sig_alg->algorithm)) {
2316 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2317 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2321 if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa) {
2322 mask_k |= SSL_kECDHr;
2323 mask_a |= SSL_aECDH;
2324 if (ecc_pkey_size <= 163) {
2325 emask_k |= SSL_kECDHr;
2326 emask_a |= SSL_aECDH;
2330 if (pk_nid == NID_X9_62_id_ecPublicKey) {
2331 mask_k |= SSL_kECDHe;
2332 mask_a |= SSL_aECDH;
2333 if (ecc_pkey_size <= 163) {
2334 emask_k |= SSL_kECDHe;
2335 emask_a |= SSL_aECDH;
2340 mask_a |= SSL_aECDSA;
2341 emask_a |= SSL_aECDSA;
2346 #ifndef OPENSSL_NO_EC
2347 if (have_ecdh_tmp) {
2348 mask_k |= SSL_kECDHE;
2349 emask_k |= SSL_kECDHE;
2353 #ifndef OPENSSL_NO_PSK
2356 emask_k |= SSL_kPSK;
2357 emask_a |= SSL_aPSK;
2362 c->export_mask_k = emask_k;
2363 c->export_mask_a = emask_a;
2367 /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2368 #define ku_reject(x, usage) \
2369 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2371 #ifndef OPENSSL_NO_EC
2373 int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2375 unsigned long alg_k, alg_a;
2376 EVP_PKEY *pkey = NULL;
2378 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2379 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2381 alg_k = cs->algorithm_mkey;
2382 alg_a = cs->algorithm_auth;
2384 if (SSL_C_IS_EXPORT(cs)) {
2385 /* ECDH key length in export ciphers must be <= 163 bits */
2386 pkey = X509_get_pubkey(x);
2389 keysize = EVP_PKEY_bits(pkey);
2390 EVP_PKEY_free(pkey);
2395 /* This call populates the ex_flags field correctly */
2396 X509_check_purpose(x, -1, 0);
2397 if ((x->sig_alg) && (x->sig_alg->algorithm)) {
2398 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2399 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2401 if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) {
2402 /* key usage, if present, must allow key agreement */
2403 if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) {
2404 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
2405 SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
2408 if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION) {
2409 /* signature alg must be ECDSA */
2410 if (pk_nid != NID_X9_62_id_ecPublicKey) {
2411 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
2412 SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
2416 if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION) {
2417 /* signature alg must be RSA */
2419 if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa) {
2420 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
2421 SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
2426 if (alg_a & SSL_aECDSA) {
2427 /* key usage, if present, must allow signing */
2428 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2429 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
2430 SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2435 return 1; /* all checks are ok */
2440 static int ssl_get_server_cert_index(const SSL *s)
2443 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2444 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2445 idx = SSL_PKEY_RSA_SIGN;
2447 SSLerr(SSL_F_SSL_GET_SERVER_CERT_INDEX, ERR_R_INTERNAL_ERROR);
2451 CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2457 if (!s->s3 || !s->s3->tmp.new_cipher)
2459 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2461 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2463 * Broken protocol test: return last used certificate: which may mismatch
2466 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2470 i = ssl_get_server_cert_index(s);
2472 /* This may or may not be an error. */
2477 return &c->pkeys[i];
2480 EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher,
2483 unsigned long alg_a;
2487 alg_a = cipher->algorithm_auth;
2490 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2492 * Broken protocol test: use last key: which may mismatch the one
2495 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2496 idx = c->key - c->pkeys;
2500 if ((alg_a & SSL_aDSS) &&
2501 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
2502 idx = SSL_PKEY_DSA_SIGN;
2503 else if (alg_a & SSL_aRSA) {
2504 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2505 idx = SSL_PKEY_RSA_SIGN;
2506 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2507 idx = SSL_PKEY_RSA_ENC;
2508 } else if ((alg_a & SSL_aECDSA) &&
2509 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2512 SSLerr(SSL_F_SSL_GET_SIGN_PKEY, ERR_R_INTERNAL_ERROR);
2516 *pmd = c->pkeys[idx].digest;
2517 return c->pkeys[idx].privatekey;
2520 #ifndef OPENSSL_NO_TLSEXT
2521 int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo,
2522 size_t *serverinfo_length)
2526 *serverinfo_length = 0;
2529 i = ssl_get_server_cert_index(s);
2533 if (c->pkeys[i].serverinfo == NULL)
2536 *serverinfo = c->pkeys[i].serverinfo;
2537 *serverinfo_length = c->pkeys[i].serverinfo_length;
2542 void ssl_update_cache(SSL *s, int mode)
2547 * If the session_id_length is 0, we are not supposed to cache it, and it
2548 * would be rather hard to do anyway :-)
2550 if (s->session->session_id_length == 0)
2553 i = s->session_ctx->session_cache_mode;
2554 if ((i & mode) && (!s->hit)
2555 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2556 || SSL_CTX_add_session(s->session_ctx, s->session))
2557 && (s->session_ctx->new_session_cb != NULL)) {
2558 CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2559 if (!s->session_ctx->new_session_cb(s, s->session))
2560 SSL_SESSION_free(s->session);
2563 /* auto flush every 255 connections */
2564 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
2565 if ((((mode & SSL_SESS_CACHE_CLIENT)
2566 ? s->session_ctx->stats.sess_connect_good
2567 : s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) {
2568 SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
2573 const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2578 const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2583 int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2588 if (s->method != meth) {
2589 if (s->handshake_func != NULL)
2590 conn = (s->handshake_func == s->method->ssl_connect);
2592 if (s->method->version == meth->version)
2595 s->method->ssl_free(s);
2597 ret = s->method->ssl_new(s);
2601 s->handshake_func = meth->ssl_connect;
2603 s->handshake_func = meth->ssl_accept;
2608 int SSL_get_error(const SSL *s, int i)
2615 return (SSL_ERROR_NONE);
2618 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2619 * where we do encode the error
2621 if ((l = ERR_peek_error()) != 0) {
2622 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2623 return (SSL_ERROR_SYSCALL);
2625 return (SSL_ERROR_SSL);
2628 if ((i < 0) && SSL_want_read(s)) {
2629 bio = SSL_get_rbio(s);
2630 if (BIO_should_read(bio))
2631 return (SSL_ERROR_WANT_READ);
2632 else if (BIO_should_write(bio))
2634 * This one doesn't make too much sense ... We never try to write
2635 * to the rbio, and an application program where rbio and wbio
2636 * are separate couldn't even know what it should wait for.
2637 * However if we ever set s->rwstate incorrectly (so that we have
2638 * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
2639 * wbio *are* the same, this test works around that bug; so it
2640 * might be safer to keep it.
2642 return (SSL_ERROR_WANT_WRITE);
2643 else if (BIO_should_io_special(bio)) {
2644 reason = BIO_get_retry_reason(bio);
2645 if (reason == BIO_RR_CONNECT)
2646 return (SSL_ERROR_WANT_CONNECT);
2647 else if (reason == BIO_RR_ACCEPT)
2648 return (SSL_ERROR_WANT_ACCEPT);
2650 return (SSL_ERROR_SYSCALL); /* unknown */
2654 if ((i < 0) && SSL_want_write(s)) {
2655 bio = SSL_get_wbio(s);
2656 if (BIO_should_write(bio))
2657 return (SSL_ERROR_WANT_WRITE);
2658 else if (BIO_should_read(bio))
2660 * See above (SSL_want_read(s) with BIO_should_write(bio))
2662 return (SSL_ERROR_WANT_READ);
2663 else if (BIO_should_io_special(bio)) {
2664 reason = BIO_get_retry_reason(bio);
2665 if (reason == BIO_RR_CONNECT)
2666 return (SSL_ERROR_WANT_CONNECT);
2667 else if (reason == BIO_RR_ACCEPT)
2668 return (SSL_ERROR_WANT_ACCEPT);
2670 return (SSL_ERROR_SYSCALL);
2673 if ((i < 0) && SSL_want_x509_lookup(s)) {
2674 return (SSL_ERROR_WANT_X509_LOOKUP);
2678 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2679 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2680 return (SSL_ERROR_ZERO_RETURN);
2682 return (SSL_ERROR_SYSCALL);
2685 int SSL_do_handshake(SSL *s)
2689 if (s->handshake_func == NULL) {
2690 SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET);
2694 s->method->ssl_renegotiate_check(s);
2696 if (SSL_in_init(s) || SSL_in_before(s)) {
2697 ret = s->handshake_func(s);
2703 * For the next 2 functions, SSL_clear() sets shutdown and so one of these
2704 * calls will reset it
2706 void SSL_set_accept_state(SSL *s)
2710 s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2711 s->handshake_func = s->method->ssl_accept;
2712 /* clear the current cipher */
2713 ssl_clear_cipher_ctx(s);
2714 ssl_clear_hash_ctx(&s->read_hash);
2715 ssl_clear_hash_ctx(&s->write_hash);
2718 void SSL_set_connect_state(SSL *s)
2722 s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2723 s->handshake_func = s->method->ssl_connect;
2724 /* clear the current cipher */
2725 ssl_clear_cipher_ctx(s);
2726 ssl_clear_hash_ctx(&s->read_hash);
2727 ssl_clear_hash_ctx(&s->write_hash);
2730 int ssl_undefined_function(SSL *s)
2732 SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2736 int ssl_undefined_void_function(void)
2738 SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,
2739 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2743 int ssl_undefined_const_function(const SSL *s)
2745 SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,
2746 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2750 SSL_METHOD *ssl_bad_method(int ver)
2752 SSLerr(SSL_F_SSL_BAD_METHOD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2756 const char *SSL_get_version(const SSL *s)
2758 if (s->version == TLS1_2_VERSION)
2760 else if (s->version == TLS1_1_VERSION)
2762 else if (s->version == TLS1_VERSION)
2764 else if (s->version == SSL3_VERSION)
2766 else if (s->version == DTLS1_BAD_VER)
2767 return ("DTLSv0.9");
2768 else if (s->version == DTLS1_VERSION)
2770 else if (s->version == DTLS1_2_VERSION)
2771 return ("DTLSv1.2");
2776 SSL *SSL_dup(SSL *s)
2778 STACK_OF(X509_NAME) *sk;
2783 if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2786 ret->version = s->version;
2787 ret->type = s->type;
2788 ret->method = s->method;
2790 if (s->session != NULL) {
2791 /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
2792 if(!SSL_copy_session_id(ret, s))
2796 * No session has been established yet, so we have to expect that
2797 * s->cert or ret->cert will be changed later -- they should not both
2798 * point to the same object, and thus we can't use
2799 * SSL_copy_session_id.
2802 ret->method->ssl_free(ret);
2803 ret->method = s->method;
2804 ret->method->ssl_new(ret);
2806 if (s->cert != NULL) {
2807 if (ret->cert != NULL) {
2808 ssl_cert_free(ret->cert);
2810 ret->cert = ssl_cert_dup(s->cert);
2811 if (ret->cert == NULL)
2815 if(!SSL_set_session_id_context(ret, s->sid_ctx, s->sid_ctx_length))
2819 ret->options = s->options;
2820 ret->mode = s->mode;
2821 SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
2822 SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
2823 ret->msg_callback = s->msg_callback;
2824 ret->msg_callback_arg = s->msg_callback_arg;
2825 SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
2826 SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
2827 ret->generate_session_id = s->generate_session_id;
2829 SSL_set_info_callback(ret, SSL_get_info_callback(s));
2831 ret->debug = s->debug;
2833 /* copy app data, a little dangerous perhaps */
2834 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
2837 /* setup rbio, and wbio */
2838 if (s->rbio != NULL) {
2839 if (!BIO_dup_state(s->rbio, (char *)&ret->rbio))
2842 if (s->wbio != NULL) {
2843 if (s->wbio != s->rbio) {
2844 if (!BIO_dup_state(s->wbio, (char *)&ret->wbio))
2847 ret->wbio = ret->rbio;
2849 ret->rwstate = s->rwstate;
2850 ret->in_handshake = s->in_handshake;
2851 ret->handshake_func = s->handshake_func;
2852 ret->server = s->server;
2853 ret->renegotiate = s->renegotiate;
2854 ret->new_session = s->new_session;
2855 ret->quiet_shutdown = s->quiet_shutdown;
2856 ret->shutdown = s->shutdown;
2857 ret->state = s->state; /* SSL_dup does not really work at any state,
2859 ret->rstate = s->rstate;
2860 ret->init_num = 0; /* would have to copy ret->init_buf,
2861 * ret->init_msg, ret->init_num,
2865 X509_VERIFY_PARAM_inherit(ret->param, s->param);
2867 /* dup the cipher_list and cipher_list_by_id stacks */
2868 if (s->cipher_list != NULL) {
2869 if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2872 if (s->cipher_list_by_id != NULL)
2873 if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))
2877 /* Dup the client_CA list */
2878 if (s->client_CA != NULL) {
2879 if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL)
2881 ret->client_CA = sk;
2882 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2883 xn = sk_X509_NAME_value(sk, i);
2884 if (sk_X509_NAME_set(sk, i, X509_NAME_dup(xn)) == NULL) {
2900 void ssl_clear_cipher_ctx(SSL *s)
2902 if (s->enc_read_ctx != NULL) {
2903 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2904 OPENSSL_free(s->enc_read_ctx);
2905 s->enc_read_ctx = NULL;
2907 if (s->enc_write_ctx != NULL) {
2908 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2909 OPENSSL_free(s->enc_write_ctx);
2910 s->enc_write_ctx = NULL;
2912 #ifndef OPENSSL_NO_COMP
2913 if (s->expand != NULL) {
2914 COMP_CTX_free(s->expand);
2917 if (s->compress != NULL) {
2918 COMP_CTX_free(s->compress);
2924 X509 *SSL_get_certificate(const SSL *s)
2926 if (s->cert != NULL)
2927 return (s->cert->key->x509);
2932 EVP_PKEY *SSL_get_privatekey(const SSL *s)
2934 if (s->cert != NULL)
2935 return (s->cert->key->privatekey);
2940 X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2942 if (ctx->cert != NULL)
2943 return ctx->cert->key->x509;
2948 EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2950 if (ctx->cert != NULL)
2951 return ctx->cert->key->privatekey;
2956 const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2958 if ((s->session != NULL) && (s->session->cipher != NULL))
2959 return (s->session->cipher);
2963 #ifdef OPENSSL_NO_COMP
2964 const void *SSL_get_current_compression(SSL *s)
2969 const void *SSL_get_current_expansion(SSL *s)
2975 const COMP_METHOD *SSL_get_current_compression(SSL *s)
2977 if (s->compress != NULL)
2978 return (s->compress->meth);
2982 const COMP_METHOD *SSL_get_current_expansion(SSL *s)
2984 if (s->expand != NULL)
2985 return (s->expand->meth);
2990 int ssl_init_wbio_buffer(SSL *s, int push)
2994 if (s->bbio == NULL) {
2995 bbio = BIO_new(BIO_f_buffer());
3001 if (s->bbio == s->wbio)
3002 s->wbio = BIO_pop(s->wbio);
3004 (void)BIO_reset(bbio);
3005 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
3006 if (!BIO_set_read_buffer_size(bbio, 1)) {
3007 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER, ERR_R_BUF_LIB);
3011 if (s->wbio != bbio)
3012 s->wbio = BIO_push(bbio, s->wbio);
3014 if (s->wbio == bbio)
3015 s->wbio = BIO_pop(bbio);
3020 void ssl_free_wbio_buffer(SSL *s)
3022 if (s->bbio == NULL)
3025 if (s->bbio == s->wbio) {
3026 /* remove buffering */
3027 s->wbio = BIO_pop(s->wbio);
3028 #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids
3029 * adding one more preprocessor symbol */
3030 assert(s->wbio != NULL);
3037 void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
3039 ctx->quiet_shutdown = mode;
3042 int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
3044 return (ctx->quiet_shutdown);
3047 void SSL_set_quiet_shutdown(SSL *s, int mode)
3049 s->quiet_shutdown = mode;
3052 int SSL_get_quiet_shutdown(const SSL *s)
3054 return (s->quiet_shutdown);
3057 void SSL_set_shutdown(SSL *s, int mode)
3062 int SSL_get_shutdown(const SSL *s)
3064 return (s->shutdown);
3067 int SSL_version(const SSL *s)
3069 return (s->version);
3072 SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
3077 SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
3080 if (ssl->ctx == ctx)
3082 #ifndef OPENSSL_NO_TLSEXT
3084 ctx = ssl->initial_ctx;
3086 new_cert = ssl_cert_dup(ctx->cert);
3087 if (new_cert == NULL) {
3090 /* Preserve any already negotiated parameters */
3092 new_cert->peer_sigalgs = ssl->cert->peer_sigalgs;
3093 new_cert->peer_sigalgslen = ssl->cert->peer_sigalgslen;
3094 ssl->cert->peer_sigalgs = NULL;
3095 new_cert->ciphers_raw = ssl->cert->ciphers_raw;
3096 new_cert->ciphers_rawlen = ssl->cert->ciphers_rawlen;
3097 ssl->cert->ciphers_raw = NULL;
3099 ssl_cert_free(ssl->cert);
3100 ssl->cert = new_cert;
3103 * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
3104 * so setter APIs must prevent invalid lengths from entering the system.
3106 OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
3109 * If the session ID context matches that of the parent SSL_CTX,
3110 * inherit it from the new SSL_CTX as well. If however the context does
3111 * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
3112 * leave it unchanged.
3114 if ((ssl->ctx != NULL) &&
3115 (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
3116 (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {
3117 ssl->sid_ctx_length = ctx->sid_ctx_length;
3118 memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
3121 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
3122 if (ssl->ctx != NULL)
3123 SSL_CTX_free(ssl->ctx); /* decrement reference count */
3129 #ifndef OPENSSL_NO_STDIO
3130 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
3132 return (X509_STORE_set_default_paths(ctx->cert_store));
3135 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
3138 return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath));
3142 void SSL_set_info_callback(SSL *ssl,
3143 void (*cb) (const SSL *ssl, int type, int val))
3145 ssl->info_callback = cb;
3149 * One compiler (Diab DCC) doesn't like argument names in returned function
3152 void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
3155 return ssl->info_callback;
3158 int SSL_state(const SSL *ssl)
3160 return (ssl->state);
3163 void SSL_set_state(SSL *ssl, int state)
3168 void SSL_set_verify_result(SSL *ssl, long arg)
3170 ssl->verify_result = arg;
3173 long SSL_get_verify_result(const SSL *ssl)
3175 return (ssl->verify_result);
3178 int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3179 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3181 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3182 new_func, dup_func, free_func);
3185 int SSL_set_ex_data(SSL *s, int idx, void *arg)
3187 return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
3190 void *SSL_get_ex_data(const SSL *s, int idx)
3192 return (CRYPTO_get_ex_data(&s->ex_data, idx));
3195 int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3196 CRYPTO_EX_dup *dup_func,
3197 CRYPTO_EX_free *free_func)
3199 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3200 new_func, dup_func, free_func);
3203 int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
3205 return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
3208 void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
3210 return (CRYPTO_get_ex_data(&s->ex_data, idx));
3218 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
3220 return (ctx->cert_store);
3223 void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
3225 if (ctx->cert_store != NULL)
3226 X509_STORE_free(ctx->cert_store);
3227 ctx->cert_store = store;
3230 int SSL_want(const SSL *s)
3232 return (s->rwstate);
3236 * \brief Set the callback for generating temporary RSA keys.
3237 * \param ctx the SSL context.
3238 * \param cb the callback
3241 #ifndef OPENSSL_NO_RSA
3242 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb) (SSL *ssl,
3246 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
3249 void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb) (SSL *ssl,
3253 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
3259 * \brief The RSA temporary key callback function.
3260 * \param ssl the SSL session.
3261 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
3262 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
3263 * of the required key in bits.
3264 * \return the temporary RSA key.
3265 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
3268 RSA *cb(SSL *ssl, int is_export, int keylength)
3274 * \brief Set the callback for generating temporary DH keys.
3275 * \param ctx the SSL context.
3276 * \param dh the callback
3279 #ifndef OPENSSL_NO_DH
3280 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
3281 DH *(*dh) (SSL *ssl, int is_export,
3284 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
3287 void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh) (SSL *ssl, int is_export,
3290 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
3294 #ifndef OPENSSL_NO_EC
3295 void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
3296 EC_KEY *(*ecdh) (SSL *ssl, int is_export,
3299 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
3300 (void (*)(void))ecdh);
3303 void SSL_set_tmp_ecdh_callback(SSL *ssl,
3304 EC_KEY *(*ecdh) (SSL *ssl, int is_export,
3307 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
3311 #ifndef OPENSSL_NO_PSK
3312 int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
3314 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
3315 SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT,
3316 SSL_R_DATA_LENGTH_TOO_LONG);
3319 if (ctx->psk_identity_hint != NULL)
3320 OPENSSL_free(ctx->psk_identity_hint);
3321 if (identity_hint != NULL) {
3322 ctx->psk_identity_hint = BUF_strdup(identity_hint);
3323 if (ctx->psk_identity_hint == NULL)
3326 ctx->psk_identity_hint = NULL;
3330 int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
3335 if (s->session == NULL)
3336 return 1; /* session not created yet, ignored */
3338 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
3339 SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
3342 if (s->session->psk_identity_hint != NULL)
3343 OPENSSL_free(s->session->psk_identity_hint);
3344 if (identity_hint != NULL) {
3345 s->session->psk_identity_hint = BUF_strdup(identity_hint);
3346 if (s->session->psk_identity_hint == NULL)
3349 s->session->psk_identity_hint = NULL;
3353 const char *SSL_get_psk_identity_hint(const SSL *s)
3355 if (s == NULL || s->session == NULL)
3357 return (s->session->psk_identity_hint);
3360 const char *SSL_get_psk_identity(const SSL *s)
3362 if (s == NULL || s->session == NULL)
3364 return (s->session->psk_identity);
3367 void SSL_set_psk_client_callback(SSL *s,
3368 unsigned int (*cb) (SSL *ssl,
3377 s->psk_client_callback = cb;
3380 void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3381 unsigned int (*cb) (SSL *ssl,
3390 ctx->psk_client_callback = cb;
3393 void SSL_set_psk_server_callback(SSL *s,
3394 unsigned int (*cb) (SSL *ssl,
3395 const char *identity,
3400 s->psk_server_callback = cb;
3403 void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3404 unsigned int (*cb) (SSL *ssl,
3405 const char *identity,
3410 ctx->psk_server_callback = cb;
3414 void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
3415 void (*cb) (int write_p, int version,
3416 int content_type, const void *buf,
3417 size_t len, SSL *ssl, void *arg))
3419 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3422 void SSL_set_msg_callback(SSL *ssl,
3423 void (*cb) (int write_p, int version,
3424 int content_type, const void *buf,
3425 size_t len, SSL *ssl, void *arg))
3427 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3430 void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
3431 int (*cb) (SSL *ssl,
3435 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
3436 (void (*)(void))cb);
3439 void SSL_set_not_resumable_session_callback(SSL *ssl,
3440 int (*cb) (SSL *ssl,
3441 int is_forward_secure))
3443 SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
3444 (void (*)(void))cb);
3448 * Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3449 * vairable, freeing EVP_MD_CTX previously stored in that variable, if any.
3450 * If EVP_MD pointer is passed, initializes ctx with this md Returns newly
3454 EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
3456 ssl_clear_hash_ctx(hash);
3457 *hash = EVP_MD_CTX_create();
3459 EVP_DigestInit_ex(*hash, md, NULL);
3463 void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3467 EVP_MD_CTX_destroy(*hash);
3471 /* Retrieve handshake hashes */
3472 int ssl_handshake_hash(SSL *s, unsigned char *out, int outlen)
3474 unsigned char *p = out;
3479 EVP_MD_CTX_init(&ctx);
3480 for (idx = 0; ssl_get_handshake_digest(idx, &mask, &md); idx++) {
3481 if (mask & ssl_get_algorithm2(s)) {
3482 int hashsize = EVP_MD_size(md);
3483 EVP_MD_CTX *hdgst = s->s3->handshake_dgst[idx];
3484 if (!hdgst || hashsize < 0 || hashsize > outlen)
3486 if (!EVP_MD_CTX_copy_ex(&ctx, hdgst))
3488 if (!EVP_DigestFinal_ex(&ctx, p, NULL))
3496 EVP_MD_CTX_cleanup(&ctx);
3500 void SSL_set_debug(SSL *s, int debug)
3505 int SSL_cache_hit(SSL *s)
3510 int SSL_is_server(SSL *s)
3515 void SSL_set_security_level(SSL *s, int level)
3517 s->cert->sec_level = level;
3520 int SSL_get_security_level(const SSL *s)
3522 return s->cert->sec_level;
3525 void SSL_set_security_callback(SSL *s,
3526 int (*cb) (SSL *s, SSL_CTX *ctx, int op,
3527 int bits, int nid, void *other,
3530 s->cert->sec_cb = cb;
3533 int (*SSL_get_security_callback(const SSL *s)) (SSL *s, SSL_CTX *ctx, int op,
3535 void *other, void *ex) {
3536 return s->cert->sec_cb;
3539 void SSL_set0_security_ex_data(SSL *s, void *ex)
3541 s->cert->sec_ex = ex;
3544 void *SSL_get0_security_ex_data(const SSL *s)
3546 return s->cert->sec_ex;
3549 void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
3551 ctx->cert->sec_level = level;
3554 int SSL_CTX_get_security_level(const SSL_CTX *ctx)
3556 return ctx->cert->sec_level;
3559 void SSL_CTX_set_security_callback(SSL_CTX *ctx,
3560 int (*cb) (SSL *s, SSL_CTX *ctx, int op,
3561 int bits, int nid, void *other,
3564 ctx->cert->sec_cb = cb;
3567 int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (SSL *s,
3573 return ctx->cert->sec_cb;
3576 void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
3578 ctx->cert->sec_ex = ex;
3581 void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
3583 return ctx->cert->sec_ex;
3586 IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);