1 /*! \file ssl/ssl_lib.c
2 * \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>
160 const char *SSL_version_str=OPENSSL_VERSION_TEXT;
162 SSL3_ENC_METHOD ssl3_undef_enc_method={
163 /* evil casts, but these functions are only called if there's a library bug */
164 (int (*)(SSL *,int))ssl_undefined_function,
165 (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
166 ssl_undefined_function,
167 (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
168 (int (*)(SSL*, int))ssl_undefined_function,
169 (int (*)(SSL *, const char*, int, unsigned char *))ssl_undefined_function,
170 0, /* finish_mac_length */
171 (int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
172 NULL, /* client_finished_label */
173 0, /* client_finished_label_len */
174 NULL, /* server_finished_label */
175 0, /* server_finished_label_len */
176 (int (*)(int))ssl_undefined_function
179 int SSL_clear(SSL *s)
182 if (s->method == NULL)
184 SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
188 if (ssl_clear_bad_session(s))
190 SSL_SESSION_free(s->session);
198 #if 0 /* Disabled since version 1.10 of this file (early return not
199 * needed because SSL_clear is not called when doing renegotiation) */
200 /* This is set if we are doing dynamic renegotiation so keep
201 * the old cipher. It is sort of a SSL_clear_lite :-) */
202 if (s->new_session) return(1);
206 SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);
213 s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
215 s->version=s->method->version;
216 s->client_version=s->version;
217 s->rwstate=SSL_NOTHING;
218 s->rstate=SSL_ST_READ_HEADER;
220 s->read_ahead=s->ctx->read_ahead;
223 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
237 * so, revert back if we are not doing session-id reuse. */
238 if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
240 s->method->ssl_free(s);
241 s->method=s->ctx->method;
242 if (!s->method->ssl_new(s))
247 s->method->ssl_clear(s);
251 /** Used to change an SSL_CTXs default SSL method type */
252 int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
254 STACK_OF(SSL_CIPHER) *sk;
258 sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
259 &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
260 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
262 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
268 SSL *SSL_new(SSL_CTX *ctx)
274 SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
277 if (ctx->method == NULL)
279 SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
283 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
284 if (s == NULL) goto err;
285 memset(s,0,sizeof(SSL));
287 #ifndef OPENSSL_NO_KRB5
288 s->kssl_ctx = kssl_ctx_new();
289 #endif /* OPENSSL_NO_KRB5 */
291 s->options=ctx->options;
293 s->max_cert_list=ctx->max_cert_list;
295 if (ctx->cert != NULL)
297 /* Earlier library versions used to copy the pointer to
298 * the CERT, not its contents; only when setting new
299 * parameters for the per-SSL copy, ssl_cert_new would be
300 * called (and the direct reference to the per-SSL_CTX
301 * settings would be lost, but those still were indirectly
302 * accessed for various purposes, and for that reason they
303 * used to be known as s->ctx->default_cert).
304 * Now we don't look at the SSL_CTX's CERT after having
305 * duplicated it once. */
307 s->cert = ssl_cert_dup(ctx->cert);
312 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
314 s->read_ahead=ctx->read_ahead;
315 s->msg_callback=ctx->msg_callback;
316 s->msg_callback_arg=ctx->msg_callback_arg;
317 s->verify_mode=ctx->verify_mode;
319 s->verify_depth=ctx->verify_depth;
321 s->sid_ctx_length=ctx->sid_ctx_length;
322 OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
323 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
324 s->verify_callback=ctx->default_verify_callback;
325 s->generate_session_id=ctx->generate_session_id;
327 s->param = X509_VERIFY_PARAM_new();
330 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
332 s->purpose = ctx->purpose;
333 s->trust = ctx->trust;
335 s->quiet_shutdown=ctx->quiet_shutdown;
336 s->max_send_fragment = ctx->max_send_fragment;
338 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
340 #ifndef OPENSSL_NO_TLSEXT
341 s->tlsext_debug_cb = 0;
342 s->tlsext_debug_arg = NULL;
343 s->tlsext_ticket_expected = 0;
344 s->tlsext_status_type = -1;
345 s->tlsext_status_expected = 0;
346 s->tlsext_ocsp_ids = NULL;
347 s->tlsext_ocsp_exts = NULL;
348 s->tlsext_ocsp_resp = NULL;
349 s->tlsext_ocsp_resplen = -1;
350 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
354 s->verify_result=X509_V_OK;
356 s->method=ctx->method;
358 if (!s->method->ssl_new(s))
362 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
366 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
368 #ifndef OPENSSL_NO_PSK
369 s->psk_client_callback=ctx->psk_client_callback;
370 s->psk_server_callback=ctx->psk_server_callback;
378 ssl_cert_free(s->cert);
380 SSL_CTX_free(s->ctx); /* decrement reference count */
383 SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
387 int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
388 unsigned int sid_ctx_len)
390 if(sid_ctx_len > sizeof ctx->sid_ctx)
392 SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
395 ctx->sid_ctx_length=sid_ctx_len;
396 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
401 int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
402 unsigned int sid_ctx_len)
404 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
406 SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
409 ssl->sid_ctx_length=sid_ctx_len;
410 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
415 int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
417 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
418 ctx->generate_session_id = cb;
419 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
423 int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
425 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
426 ssl->generate_session_id = cb;
427 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
431 int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
434 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
435 * we can "construct" a session to give us the desired check - ie. to
436 * find if there's a session in the hash table that would conflict with
437 * any new session built out of this id/id_len and the ssl_version in
438 * use by this SSL. */
441 if(id_len > sizeof r.session_id)
444 r.ssl_version = ssl->version;
445 r.session_id_length = id_len;
446 memcpy(r.session_id, id, id_len);
447 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
448 * callback is calling us to check the uniqueness of a shorter ID, it
449 * must be compared as a padded-out ID because that is what it will be
450 * converted to when the callback has finished choosing it. */
451 if((r.ssl_version == SSL2_VERSION) &&
452 (id_len < SSL2_SSL_SESSION_ID_LENGTH))
454 memset(r.session_id + id_len, 0,
455 SSL2_SSL_SESSION_ID_LENGTH - id_len);
456 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
459 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
460 p = (SSL_SESSION *)lh_retrieve(ssl->ctx->sessions, &r);
461 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
465 int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
467 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
470 int SSL_set_purpose(SSL *s, int purpose)
472 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
475 int SSL_CTX_set_trust(SSL_CTX *s, int trust)
477 return X509_VERIFY_PARAM_set_trust(s->param, trust);
480 int SSL_set_trust(SSL *s, int trust)
482 return X509_VERIFY_PARAM_set_trust(s->param, trust);
485 void SSL_free(SSL *s)
492 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
500 fprintf(stderr,"SSL_free, bad reference count\n");
506 X509_VERIFY_PARAM_free(s->param);
508 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
512 /* If the buffering BIO is in place, pop it off */
513 if (s->bbio == s->wbio)
515 s->wbio=BIO_pop(s->wbio);
521 BIO_free_all(s->rbio);
522 if ((s->wbio != NULL) && (s->wbio != s->rbio))
523 BIO_free_all(s->wbio);
525 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
527 /* add extra stuff */
528 if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);
529 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
531 /* Make the next call work :-) */
532 if (s->session != NULL)
534 ssl_clear_bad_session(s);
535 SSL_SESSION_free(s->session);
538 ssl_clear_cipher_ctx(s);
539 ssl_clear_hash_ctx(&s->read_hash);
540 ssl_clear_hash_ctx(&s->write_hash);
542 if (s->cert != NULL) ssl_cert_free(s->cert);
543 /* Free up if allocated */
545 if (s->ctx) SSL_CTX_free(s->ctx);
546 #ifndef OPENSSL_NO_TLSEXT
547 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
548 #ifndef OPENSSL_NO_EC
549 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
550 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
551 #endif /* OPENSSL_NO_EC */
552 if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input);
553 if (s->tlsext_ocsp_exts)
554 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
555 X509_EXTENSION_free);
556 if (s->tlsext_ocsp_ids)
557 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
558 if (s->tlsext_ocsp_resp)
559 OPENSSL_free(s->tlsext_ocsp_resp);
562 if (s->client_CA != NULL)
563 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
565 if (s->method != NULL) s->method->ssl_free(s);
567 #ifndef OPENSSL_NO_KRB5
568 if (s->kssl_ctx != NULL)
569 kssl_ctx_free(s->kssl_ctx);
570 #endif /* OPENSSL_NO_KRB5 */
575 void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
577 /* If the output buffering BIO is still in place, remove it
581 if (s->wbio == s->bbio)
583 s->wbio=s->wbio->next_bio;
584 s->bbio->next_bio=NULL;
587 if ((s->rbio != NULL) && (s->rbio != rbio))
588 BIO_free_all(s->rbio);
589 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
590 BIO_free_all(s->wbio);
595 BIO *SSL_get_rbio(const SSL *s)
598 BIO *SSL_get_wbio(const SSL *s)
601 int SSL_get_fd(const SSL *s)
603 return(SSL_get_rfd(s));
606 int SSL_get_rfd(const SSL *s)
612 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
618 int SSL_get_wfd(const SSL *s)
624 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
630 #ifndef OPENSSL_NO_SOCK
631 int SSL_set_fd(SSL *s,int fd)
636 bio=BIO_new(BIO_s_socket());
640 SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
643 BIO_set_fd(bio,fd,BIO_NOCLOSE);
644 SSL_set_bio(s,bio,bio);
650 int SSL_set_wfd(SSL *s,int fd)
655 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
656 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
658 bio=BIO_new(BIO_s_socket());
661 { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
662 BIO_set_fd(bio,fd,BIO_NOCLOSE);
663 SSL_set_bio(s,SSL_get_rbio(s),bio);
666 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
672 int SSL_set_rfd(SSL *s,int fd)
677 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
678 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
680 bio=BIO_new(BIO_s_socket());
684 SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
687 BIO_set_fd(bio,fd,BIO_NOCLOSE);
688 SSL_set_bio(s,bio,SSL_get_wbio(s));
691 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
699 /* return length of latest Finished message we sent, copy to 'buf' */
700 size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
706 ret = s->s3->tmp.finish_md_len;
709 memcpy(buf, s->s3->tmp.finish_md, count);
714 /* return length of latest Finished message we expected, copy to 'buf' */
715 size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
721 ret = s->s3->tmp.peer_finish_md_len;
724 memcpy(buf, s->s3->tmp.peer_finish_md, count);
730 int SSL_get_verify_mode(const SSL *s)
732 return(s->verify_mode);
735 int SSL_get_verify_depth(const SSL *s)
737 return X509_VERIFY_PARAM_get_depth(s->param);
740 int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
742 return(s->verify_callback);
745 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
747 return(ctx->verify_mode);
750 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
752 return X509_VERIFY_PARAM_get_depth(ctx->param);
755 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
757 return(ctx->default_verify_callback);
760 void SSL_set_verify(SSL *s,int mode,
761 int (*callback)(int ok,X509_STORE_CTX *ctx))
764 if (callback != NULL)
765 s->verify_callback=callback;
768 void SSL_set_verify_depth(SSL *s,int depth)
770 X509_VERIFY_PARAM_set_depth(s->param, depth);
773 void SSL_set_read_ahead(SSL *s,int yes)
778 int SSL_get_read_ahead(const SSL *s)
780 return(s->read_ahead);
783 int SSL_pending(const SSL *s)
785 /* SSL_pending cannot work properly if read-ahead is enabled
786 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
787 * and it is impossible to fix since SSL_pending cannot report
788 * errors that may be observed while scanning the new data.
789 * (Note that SSL_pending() is often used as a boolean value,
790 * so we'd better not return -1.)
792 return(s->method->ssl_pending(s));
795 X509 *SSL_get_peer_certificate(const SSL *s)
799 if ((s == NULL) || (s->session == NULL))
804 if (r == NULL) return(r);
806 CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
811 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
815 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
818 r=s->session->sess_cert->cert_chain;
820 /* If we are a client, cert_chain includes the peer's own
821 * certificate; if we are a server, it does not. */
826 /* Now in theory, since the calling process own 't' it should be safe to
827 * modify. We need to be able to read f without being hassled */
828 void SSL_copy_session_id(SSL *t,const SSL *f)
832 /* Do we need to to SSL locking? */
833 SSL_set_session(t,SSL_get_session(f));
835 /* what if we are setup as SSLv2 but want to talk SSLv3 or
837 if (t->method != f->method)
839 t->method->ssl_free(t); /* cleanup current */
840 t->method=f->method; /* change method */
841 t->method->ssl_new(t); /* setup new */
847 CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
852 if (tmp != NULL) ssl_cert_free(tmp);
853 SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length);
856 /* Fix this so it checks all the valid key/cert options */
857 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
859 if ( (ctx == NULL) ||
860 (ctx->cert == NULL) ||
861 (ctx->cert->key->x509 == NULL))
863 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
866 if (ctx->cert->key->privatekey == NULL)
868 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
871 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
874 /* Fix this function so that it takes an optional type parameter */
875 int SSL_check_private_key(const SSL *ssl)
879 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
882 if (ssl->cert == NULL)
884 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
887 if (ssl->cert->key->x509 == NULL)
889 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
892 if (ssl->cert->key->privatekey == NULL)
894 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
897 return(X509_check_private_key(ssl->cert->key->x509,
898 ssl->cert->key->privatekey));
901 int SSL_accept(SSL *s)
903 if (s->handshake_func == 0)
904 /* Not properly initialized yet */
905 SSL_set_accept_state(s);
907 return(s->method->ssl_accept(s));
910 int SSL_connect(SSL *s)
912 if (s->handshake_func == 0)
913 /* Not properly initialized yet */
914 SSL_set_connect_state(s);
916 return(s->method->ssl_connect(s));
919 long SSL_get_default_timeout(const SSL *s)
921 return(s->method->get_timeout());
924 int SSL_read(SSL *s,void *buf,int num)
926 if (s->handshake_func == 0)
928 SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
932 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
934 s->rwstate=SSL_NOTHING;
937 return(s->method->ssl_read(s,buf,num));
940 int SSL_peek(SSL *s,void *buf,int num)
942 if (s->handshake_func == 0)
944 SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
948 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
952 return(s->method->ssl_peek(s,buf,num));
955 int SSL_write(SSL *s,const void *buf,int num)
957 if (s->handshake_func == 0)
959 SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
963 if (s->shutdown & SSL_SENT_SHUTDOWN)
965 s->rwstate=SSL_NOTHING;
966 SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
969 return(s->method->ssl_write(s,buf,num));
972 int SSL_shutdown(SSL *s)
974 /* Note that this function behaves differently from what one might
975 * expect. Return values are 0 for no success (yet),
976 * 1 for success; but calling it once is usually not enough,
977 * even if blocking I/O is used (see ssl3_shutdown).
980 if (s->handshake_func == 0)
982 SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
986 if ((s != NULL) && !SSL_in_init(s))
987 return(s->method->ssl_shutdown(s));
992 int SSL_renegotiate(SSL *s)
994 if (s->new_session == 0)
998 return(s->method->ssl_renegotiate(s));
1001 int SSL_renegotiate_pending(SSL *s)
1003 /* becomes true when negotiation is requested;
1004 * false again once a handshake has finished */
1005 return (s->new_session != 0);
1008 long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1014 case SSL_CTRL_GET_READ_AHEAD:
1015 return(s->read_ahead);
1016 case SSL_CTRL_SET_READ_AHEAD:
1021 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1022 s->msg_callback_arg = parg;
1025 case SSL_CTRL_OPTIONS:
1026 return(s->options|=larg);
1028 return(s->mode|=larg);
1029 case SSL_CTRL_GET_MAX_CERT_LIST:
1030 return(s->max_cert_list);
1031 case SSL_CTRL_SET_MAX_CERT_LIST:
1033 s->max_cert_list=larg;
1035 case SSL_CTRL_SET_MTU:
1036 if (SSL_version(s) == DTLS1_VERSION)
1042 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1043 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1045 s->max_send_fragment = larg;
1048 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1052 long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1056 case SSL_CTRL_SET_MSG_CALLBACK:
1057 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1061 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1065 struct lhash_st *SSL_CTX_sessions(SSL_CTX *ctx)
1067 return ctx->sessions;
1070 long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1076 case SSL_CTRL_GET_READ_AHEAD:
1077 return(ctx->read_ahead);
1078 case SSL_CTRL_SET_READ_AHEAD:
1080 ctx->read_ahead=larg;
1083 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1084 ctx->msg_callback_arg = parg;
1087 case SSL_CTRL_GET_MAX_CERT_LIST:
1088 return(ctx->max_cert_list);
1089 case SSL_CTRL_SET_MAX_CERT_LIST:
1090 l=ctx->max_cert_list;
1091 ctx->max_cert_list=larg;
1094 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1095 l=ctx->session_cache_size;
1096 ctx->session_cache_size=larg;
1098 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1099 return(ctx->session_cache_size);
1100 case SSL_CTRL_SET_SESS_CACHE_MODE:
1101 l=ctx->session_cache_mode;
1102 ctx->session_cache_mode=larg;
1104 case SSL_CTRL_GET_SESS_CACHE_MODE:
1105 return(ctx->session_cache_mode);
1107 case SSL_CTRL_SESS_NUMBER:
1108 return(ctx->sessions->num_items);
1109 case SSL_CTRL_SESS_CONNECT:
1110 return(ctx->stats.sess_connect);
1111 case SSL_CTRL_SESS_CONNECT_GOOD:
1112 return(ctx->stats.sess_connect_good);
1113 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1114 return(ctx->stats.sess_connect_renegotiate);
1115 case SSL_CTRL_SESS_ACCEPT:
1116 return(ctx->stats.sess_accept);
1117 case SSL_CTRL_SESS_ACCEPT_GOOD:
1118 return(ctx->stats.sess_accept_good);
1119 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1120 return(ctx->stats.sess_accept_renegotiate);
1121 case SSL_CTRL_SESS_HIT:
1122 return(ctx->stats.sess_hit);
1123 case SSL_CTRL_SESS_CB_HIT:
1124 return(ctx->stats.sess_cb_hit);
1125 case SSL_CTRL_SESS_MISSES:
1126 return(ctx->stats.sess_miss);
1127 case SSL_CTRL_SESS_TIMEOUTS:
1128 return(ctx->stats.sess_timeout);
1129 case SSL_CTRL_SESS_CACHE_FULL:
1130 return(ctx->stats.sess_cache_full);
1131 case SSL_CTRL_OPTIONS:
1132 return(ctx->options|=larg);
1134 return(ctx->mode|=larg);
1135 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1136 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1138 ctx->max_send_fragment = larg;
1141 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1145 long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1149 case SSL_CTRL_SET_MSG_CALLBACK:
1150 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1154 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1158 int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
1166 return((l > 0)?1:-1);
1169 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap,
1170 const SSL_CIPHER * const *bp)
1174 l=(*ap)->id-(*bp)->id;
1178 return((l > 0)?1:-1);
1181 /** return a STACK of the ciphers available for the SSL and in order of
1183 STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1187 if (s->cipher_list != NULL)
1189 return(s->cipher_list);
1191 else if ((s->ctx != NULL) &&
1192 (s->ctx->cipher_list != NULL))
1194 return(s->ctx->cipher_list);
1200 /** return a STACK of the ciphers available for the SSL and in order of
1202 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1206 if (s->cipher_list_by_id != NULL)
1208 return(s->cipher_list_by_id);
1210 else if ((s->ctx != NULL) &&
1211 (s->ctx->cipher_list_by_id != NULL))
1213 return(s->ctx->cipher_list_by_id);
1219 /** The old interface to get the same thing as SSL_get_ciphers() */
1220 const char *SSL_get_cipher_list(const SSL *s,int n)
1223 STACK_OF(SSL_CIPHER) *sk;
1225 if (s == NULL) return(NULL);
1226 sk=SSL_get_ciphers(s);
1227 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1229 c=sk_SSL_CIPHER_value(sk,n);
1230 if (c == NULL) return(NULL);
1234 /** specify the ciphers to be used by default by the SSL_CTX */
1235 int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1237 STACK_OF(SSL_CIPHER) *sk;
1239 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1240 &ctx->cipher_list_by_id,str);
1241 /* ssl_create_cipher_list may return an empty stack if it
1242 * was unable to find a cipher matching the given rule string
1243 * (for example if the rule string specifies a cipher which
1244 * has been disabled). This is not an error as far as
1245 * ssl_create_cipher_list is concerned, and hence
1246 * ctx->cipher_list and ctx->cipher_list_by_id has been
1250 else if (sk_SSL_CIPHER_num(sk) == 0)
1252 SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1258 /** specify the ciphers to be used by the SSL */
1259 int SSL_set_cipher_list(SSL *s,const char *str)
1261 STACK_OF(SSL_CIPHER) *sk;
1263 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1264 &s->cipher_list_by_id,str);
1265 /* see comment in SSL_CTX_set_cipher_list */
1268 else if (sk_SSL_CIPHER_num(sk) == 0)
1270 SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1276 /* works well for SSLv2, not so good for SSLv3 */
1277 char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1281 STACK_OF(SSL_CIPHER) *sk;
1285 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1290 sk=s->session->ciphers;
1291 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1293 /* Decrement for either the ':' or a '\0' */
1295 c=sk_SSL_CIPHER_value(sk,i);
1296 for (cp=c->name; *cp; )
1312 int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
1313 int (*put_cb)(const SSL_CIPHER *, unsigned char *))
1318 #ifndef OPENSSL_NO_KRB5
1319 int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx);
1320 #endif /* OPENSSL_NO_KRB5 */
1322 if (sk == NULL) return(0);
1325 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1327 c=sk_SSL_CIPHER_value(sk,i);
1328 #ifndef OPENSSL_NO_KRB5
1329 if (((c->algorithm_mkey & SSL_kKRB5) || (c->algorithm_auth & SSL_aKRB5)) &&
1332 #endif /* OPENSSL_NO_KRB5 */
1333 #ifndef OPENSSL_NO_PSK
1334 /* with PSK there must be client callback set */
1335 if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK)) &&
1336 s->psk_client_callback == NULL)
1338 #endif /* OPENSSL_NO_PSK */
1339 j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
1345 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
1346 STACK_OF(SSL_CIPHER) **skp)
1349 STACK_OF(SSL_CIPHER) *sk;
1352 n=ssl_put_cipher_by_char(s,NULL,NULL);
1355 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1358 if ((skp == NULL) || (*skp == NULL))
1359 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1363 sk_SSL_CIPHER_zero(sk);
1366 for (i=0; i<num; i+=n)
1368 c=ssl_get_cipher_by_char(s,p);
1372 if (!sk_SSL_CIPHER_push(sk,c))
1374 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
1384 if ((skp == NULL) || (*skp == NULL))
1385 sk_SSL_CIPHER_free(sk);
1390 #ifndef OPENSSL_NO_TLSEXT
1391 /** return a servername extension value if provided in Client Hello, or NULL.
1392 * So far, only host_name types are defined (RFC 3546).
1395 const char *SSL_get_servername(const SSL *s, const int type)
1397 if (type != TLSEXT_NAMETYPE_host_name)
1400 return s->session && !s->tlsext_hostname ?
1401 s->session->tlsext_hostname :
1405 int SSL_get_servername_type(const SSL *s)
1407 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1408 return TLSEXT_NAMETYPE_host_name;
1413 unsigned long SSL_SESSION_hash(const SSL_SESSION *a)
1418 ((unsigned int) a->session_id[0] )|
1419 ((unsigned int) a->session_id[1]<< 8L)|
1420 ((unsigned long)a->session_id[2]<<16L)|
1421 ((unsigned long)a->session_id[3]<<24L);
1425 /* NB: If this function (or indeed the hash function which uses a sort of
1426 * coarser function than this one) is changed, ensure
1427 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1428 * able to construct an SSL_SESSION that will collide with any existing session
1429 * with a matching session ID. */
1430 int SSL_SESSION_cmp(const SSL_SESSION *a,const SSL_SESSION *b)
1432 if (a->ssl_version != b->ssl_version)
1434 if (a->session_id_length != b->session_id_length)
1436 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1439 /* These wrapper functions should remain rather than redeclaring
1440 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1441 * variable. The reason is that the functions aren't static, they're exposed via
1443 static IMPLEMENT_LHASH_HASH_FN(SSL_SESSION_hash, SSL_SESSION *)
1444 static IMPLEMENT_LHASH_COMP_FN(SSL_SESSION_cmp, SSL_SESSION *)
1446 SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1452 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
1456 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1458 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1461 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1465 memset(ret,0,sizeof(SSL_CTX));
1469 ret->cert_store=NULL;
1470 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1471 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1472 ret->session_cache_head=NULL;
1473 ret->session_cache_tail=NULL;
1475 /* We take the system default */
1476 ret->session_timeout=meth->get_timeout();
1478 ret->new_session_cb=0;
1479 ret->remove_session_cb=0;
1480 ret->get_session_cb=0;
1481 ret->generate_session_id=0;
1483 memset((char *)&ret->stats,0,sizeof(ret->stats));
1486 ret->quiet_shutdown=0;
1488 /* ret->cipher=NULL;*/
1489 /* ret->s2->challenge=NULL;
1490 ret->master_key=NULL;
1492 ret->s2->conn_id=NULL; */
1494 ret->info_callback=NULL;
1496 ret->app_verify_callback=0;
1497 ret->app_verify_arg=NULL;
1499 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1501 ret->msg_callback=0;
1502 ret->msg_callback_arg=NULL;
1503 ret->verify_mode=SSL_VERIFY_NONE;
1505 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1507 ret->sid_ctx_length=0;
1508 ret->default_verify_callback=NULL;
1509 if ((ret->cert=ssl_cert_new()) == NULL)
1512 ret->default_passwd_callback=0;
1513 ret->default_passwd_callback_userdata=NULL;
1514 ret->client_cert_cb=0;
1515 ret->app_gen_cookie_cb=0;
1516 ret->app_verify_cookie_cb=0;
1518 ret->sessions=lh_new(LHASH_HASH_FN(SSL_SESSION_hash),
1519 LHASH_COMP_FN(SSL_SESSION_cmp));
1520 if (ret->sessions == NULL) goto err;
1521 ret->cert_store=X509_STORE_new();
1522 if (ret->cert_store == NULL) goto err;
1524 ssl_create_cipher_list(ret->method,
1525 &ret->cipher_list,&ret->cipher_list_by_id,
1526 SSL_DEFAULT_CIPHER_LIST);
1527 if (ret->cipher_list == NULL
1528 || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
1530 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
1534 ret->param = X509_VERIFY_PARAM_new();
1538 if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
1540 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
1543 if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
1545 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
1548 if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
1550 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
1554 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1557 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1559 ret->extra_certs=NULL;
1560 ret->comp_methods=SSL_COMP_get_compression_methods();
1562 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1564 #ifndef OPENSSL_NO_TLSEXT
1565 ret->tlsext_servername_callback = 0;
1566 ret->tlsext_servername_arg = NULL;
1567 /* Setup RFC4507 ticket keys */
1568 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1569 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1570 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1571 ret->options |= SSL_OP_NO_TICKET;
1573 ret->tlsext_status_cb = 0;
1574 ret->tlsext_status_arg = NULL;
1577 #ifndef OPENSSL_NO_PSK
1578 ret->psk_identity_hint=NULL;
1579 ret->psk_client_callback=NULL;
1580 ret->psk_server_callback=NULL;
1584 SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
1586 if (ret != NULL) SSL_CTX_free(ret);
1591 static void SSL_COMP_free(SSL_COMP *comp)
1592 { OPENSSL_free(comp); }
1595 void SSL_CTX_free(SSL_CTX *a)
1599 if (a == NULL) return;
1601 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1603 REF_PRINT("SSL_CTX",a);
1609 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1615 X509_VERIFY_PARAM_free(a->param);
1618 * Free internal session cache. However: the remove_cb() may reference
1619 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1620 * after the sessions were flushed.
1621 * As the ex_data handling routines might also touch the session cache,
1622 * the most secure solution seems to be: empty (flush) the cache, then
1623 * free ex_data, then finally free the cache.
1624 * (See ticket [openssl.org #212].)
1626 if (a->sessions != NULL)
1627 SSL_CTX_flush_sessions(a,0);
1629 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
1631 if (a->sessions != NULL)
1632 lh_free(a->sessions);
1634 if (a->cert_store != NULL)
1635 X509_STORE_free(a->cert_store);
1636 if (a->cipher_list != NULL)
1637 sk_SSL_CIPHER_free(a->cipher_list);
1638 if (a->cipher_list_by_id != NULL)
1639 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1640 if (a->cert != NULL)
1641 ssl_cert_free(a->cert);
1642 if (a->client_CA != NULL)
1643 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
1644 if (a->extra_certs != NULL)
1645 sk_X509_pop_free(a->extra_certs,X509_free);
1646 #if 0 /* This should never be done, since it removes a global database */
1647 if (a->comp_methods != NULL)
1648 sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
1650 a->comp_methods = NULL;
1653 #ifndef OPENSSL_NO_PSK
1654 if (a->psk_identity_hint)
1655 OPENSSL_free(a->psk_identity_hint);
1660 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
1662 ctx->default_passwd_callback=cb;
1665 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
1667 ctx->default_passwd_callback_userdata=u;
1670 void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
1672 ctx->app_verify_callback=cb;
1673 ctx->app_verify_arg=arg;
1676 void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
1678 ctx->verify_mode=mode;
1679 ctx->default_verify_callback=cb;
1682 void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
1684 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1687 void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
1690 int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1691 int rsa_enc_export,dh_rsa_export,dh_dsa_export;
1692 int rsa_tmp_export,dh_tmp_export,kl;
1693 unsigned long mask_k,mask_a,emask_k,emask_a;
1694 int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size;
1695 #ifndef OPENSSL_NO_ECDH
1699 EVP_PKEY *ecc_pkey = NULL;
1700 int signature_nid = 0;
1702 if (c == NULL) return;
1704 kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
1706 #ifndef OPENSSL_NO_RSA
1707 rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
1708 rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
1709 (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
1711 rsa_tmp=rsa_tmp_export=0;
1713 #ifndef OPENSSL_NO_DH
1714 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
1715 dh_tmp_export=(c->dh_tmp_cb != NULL ||
1716 (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
1718 dh_tmp=dh_tmp_export=0;
1721 #ifndef OPENSSL_NO_ECDH
1722 have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL);
1724 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
1725 rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
1726 rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1727 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
1728 rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1729 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
1730 dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1731 cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
1732 dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
1733 dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1734 cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1735 /* FIX THIS EAY EAY EAY */
1736 dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
1737 dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1738 cpk= &(c->pkeys[SSL_PKEY_ECC]);
1739 have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL);
1746 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1747 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
1748 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1751 if (rsa_enc || (rsa_tmp && rsa_sign))
1753 if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
1757 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1758 if ( (dh_tmp || dh_rsa || dh_dsa) &&
1759 (rsa_enc || rsa_sign || dsa_sign))
1761 if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1762 (rsa_enc || rsa_sign || dsa_sign))
1772 if (dh_rsa) mask_k|=SSL_kDHr;
1773 if (dh_rsa_export) emask_k|=SSL_kDHr;
1775 if (dh_dsa) mask_k|=SSL_kDHd;
1776 if (dh_dsa_export) emask_k|=SSL_kDHd;
1778 if (rsa_enc || rsa_sign)
1793 #ifndef OPENSSL_NO_KRB5
1800 /* An ECC certificate may be usable for ECDH and/or
1801 * ECDSA cipher suites depending on the key usage extension.
1805 /* This call populates extension flags (ex_flags) */
1806 x = (c->pkeys[SSL_PKEY_ECC]).x509;
1807 X509_check_purpose(x, -1, 0);
1808 ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
1809 (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
1810 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
1811 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
1812 ecc_pkey = X509_get_pubkey(x);
1813 ecc_pkey_size = (ecc_pkey != NULL) ?
1814 EVP_PKEY_bits(ecc_pkey) : 0;
1815 EVP_PKEY_free(ecc_pkey);
1816 if ((x->sig_alg) && (x->sig_alg->algorithm))
1817 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1818 #ifndef OPENSSL_NO_ECDH
1821 const char *sig = OBJ_nid2ln(signature_nid);
1828 if (strstr(sig, "WithRSA"))
1832 if (ecc_pkey_size <= 163)
1834 emask_k|=SSL_kECDHr;
1839 if (signature_nid == NID_ecdsa_with_SHA1)
1843 if (ecc_pkey_size <= 163)
1845 emask_k|=SSL_kECDHe;
1851 #ifndef OPENSSL_NO_ECDSA
1855 emask_a|=SSL_aECDSA;
1860 #ifndef OPENSSL_NO_ECDH
1864 emask_k|=SSL_kEECDH;
1868 #ifndef OPENSSL_NO_PSK
1871 emask_k |= SSL_kPSK;
1872 emask_a |= SSL_aPSK;
1877 c->export_mask_k=emask_k;
1878 c->export_mask_a=emask_a;
1882 /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1883 #define ku_reject(x, usage) \
1884 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
1886 int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs)
1888 unsigned long alg_k, alg_a;
1889 EVP_PKEY *pkey = NULL;
1891 int signature_nid = 0;
1893 alg_k = cs->algorithm_mkey;
1894 alg_a = cs->algorithm_auth;
1896 if (SSL_C_IS_EXPORT(cs))
1898 /* ECDH key length in export ciphers must be <= 163 bits */
1899 pkey = X509_get_pubkey(x);
1900 if (pkey == NULL) return 0;
1901 keysize = EVP_PKEY_bits(pkey);
1902 EVP_PKEY_free(pkey);
1903 if (keysize > 163) return 0;
1906 /* This call populates the ex_flags field correctly */
1907 X509_check_purpose(x, -1, 0);
1908 if ((x->sig_alg) && (x->sig_alg->algorithm))
1909 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1910 if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr)
1912 /* key usage, if present, must allow key agreement */
1913 if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
1915 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
1918 if (alg_k & SSL_kECDHe)
1920 /* signature alg must be ECDSA */
1921 if (signature_nid != NID_ecdsa_with_SHA1)
1923 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
1927 if (alg_k & SSL_kECDHr)
1929 /* signature alg must be RSA */
1931 const char *sig = OBJ_nid2ln(signature_nid);
1937 if (strstr(sig, "WithRSA") == NULL)
1939 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
1944 if (alg_a & SSL_aECDSA)
1946 /* key usage, if present, must allow signing */
1947 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
1949 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1954 return 1; /* all checks are ok */
1957 /* THIS NEEDS CLEANING UP */
1958 X509 *ssl_get_server_send_cert(SSL *s)
1960 unsigned long alg_k,alg_a,mask_k,mask_a;
1965 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
1966 is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
1969 mask_k = c->export_mask_k;
1970 mask_a = c->export_mask_a;
1978 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1979 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1981 if (alg_k & (SSL_kECDHr|SSL_kECDHe))
1983 /* we don't need to look at SSL_kEECDH
1984 * since no certificate is needed for
1985 * anon ECDH and for authenticated
1986 * EECDH, the check for the auth
1987 * algorithm will set i correctly
1988 * NOTE: For ECDH-RSA, we need an ECC
1989 * not an RSA cert but for EECDH-RSA
1990 * we need an RSA cert. Placing the
1991 * checks for SSL_kECDH before RSA
1992 * checks ensures the correct cert is chosen.
1996 else if (alg_a & SSL_aECDSA)
2000 else if (alg_k & SSL_kDHr)
2002 else if (alg_k & SSL_kDHd)
2004 else if (alg_a & SSL_aDSS)
2005 i=SSL_PKEY_DSA_SIGN;
2006 else if (alg_a & SSL_aRSA)
2008 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
2009 i=SSL_PKEY_RSA_SIGN;
2013 else if (alg_a & SSL_aKRB5)
2015 /* VRS something else here? */
2018 else /* if (alg_a & SSL_aNULL) */
2020 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR);
2023 if (c->pkeys[i].x509 == NULL) return(NULL);
2025 return(c->pkeys[i].x509);
2028 EVP_PKEY *ssl_get_sign_pkey(SSL *s,SSL_CIPHER *cipher)
2030 unsigned long alg_a;
2033 alg_a = cipher->algorithm_auth;
2036 if ((alg_a & SSL_aDSS) &&
2037 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
2038 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
2039 else if (alg_a & SSL_aRSA)
2041 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2042 return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
2043 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2044 return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
2048 else if ((alg_a & SSL_aECDSA) &&
2049 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2050 return(c->pkeys[SSL_PKEY_ECC].privatekey);
2051 else /* if (alg_a & SSL_aNULL) */
2053 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR);
2058 void ssl_update_cache(SSL *s,int mode)
2062 /* If the session_id_length is 0, we are not supposed to cache it,
2063 * and it would be rather hard to do anyway :-) */
2064 if (s->session->session_id_length == 0) return;
2066 i=s->session_ctx->session_cache_mode;
2067 if ((i & mode) && (!s->hit)
2068 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2069 || SSL_CTX_add_session(s->session_ctx,s->session))
2070 && (s->session_ctx->new_session_cb != NULL))
2072 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
2073 if (!s->session_ctx->new_session_cb(s,s->session))
2074 SSL_SESSION_free(s->session);
2077 /* auto flush every 255 connections */
2078 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2079 ((i & mode) == mode))
2081 if ( (((mode & SSL_SESS_CACHE_CLIENT)
2082 ?s->session_ctx->stats.sess_connect_good
2083 :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
2085 SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
2090 const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2095 int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2100 if (s->method != meth)
2102 if (s->handshake_func != NULL)
2103 conn=(s->handshake_func == s->method->ssl_connect);
2105 if (s->method->version == meth->version)
2109 s->method->ssl_free(s);
2111 ret=s->method->ssl_new(s);
2115 s->handshake_func=meth->ssl_connect;
2117 s->handshake_func=meth->ssl_accept;
2122 int SSL_get_error(const SSL *s,int i)
2128 if (i > 0) return(SSL_ERROR_NONE);
2130 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2131 * etc, where we do encode the error */
2132 if ((l=ERR_peek_error()) != 0)
2134 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2135 return(SSL_ERROR_SYSCALL);
2137 return(SSL_ERROR_SSL);
2140 if ((i < 0) && SSL_want_read(s))
2142 bio=SSL_get_rbio(s);
2143 if (BIO_should_read(bio))
2144 return(SSL_ERROR_WANT_READ);
2145 else if (BIO_should_write(bio))
2146 /* This one doesn't make too much sense ... We never try
2147 * to write to the rbio, and an application program where
2148 * rbio and wbio are separate couldn't even know what it
2150 * However if we ever set s->rwstate incorrectly
2151 * (so that we have SSL_want_read(s) instead of
2152 * SSL_want_write(s)) and rbio and wbio *are* the same,
2153 * this test works around that bug; so it might be safer
2155 return(SSL_ERROR_WANT_WRITE);
2156 else if (BIO_should_io_special(bio))
2158 reason=BIO_get_retry_reason(bio);
2159 if (reason == BIO_RR_CONNECT)
2160 return(SSL_ERROR_WANT_CONNECT);
2161 else if (reason == BIO_RR_ACCEPT)
2162 return(SSL_ERROR_WANT_ACCEPT);
2164 return(SSL_ERROR_SYSCALL); /* unknown */
2168 if ((i < 0) && SSL_want_write(s))
2170 bio=SSL_get_wbio(s);
2171 if (BIO_should_write(bio))
2172 return(SSL_ERROR_WANT_WRITE);
2173 else if (BIO_should_read(bio))
2174 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2175 return(SSL_ERROR_WANT_READ);
2176 else if (BIO_should_io_special(bio))
2178 reason=BIO_get_retry_reason(bio);
2179 if (reason == BIO_RR_CONNECT)
2180 return(SSL_ERROR_WANT_CONNECT);
2181 else if (reason == BIO_RR_ACCEPT)
2182 return(SSL_ERROR_WANT_ACCEPT);
2184 return(SSL_ERROR_SYSCALL);
2187 if ((i < 0) && SSL_want_x509_lookup(s))
2189 return(SSL_ERROR_WANT_X509_LOOKUP);
2194 if (s->version == SSL2_VERSION)
2196 /* assume it is the socket being closed */
2197 return(SSL_ERROR_ZERO_RETURN);
2201 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2202 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2203 return(SSL_ERROR_ZERO_RETURN);
2206 return(SSL_ERROR_SYSCALL);
2209 int SSL_do_handshake(SSL *s)
2213 if (s->handshake_func == NULL)
2215 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
2219 s->method->ssl_renegotiate_check(s);
2221 if (SSL_in_init(s) || SSL_in_before(s))
2223 ret=s->handshake_func(s);
2228 /* For the next 2 functions, SSL_clear() sets shutdown and so
2229 * one of these calls will reset it */
2230 void SSL_set_accept_state(SSL *s)
2234 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2235 s->handshake_func=s->method->ssl_accept;
2236 /* clear the current cipher */
2237 ssl_clear_cipher_ctx(s);
2238 ssl_clear_hash_ctx(&s->read_hash);
2239 ssl_clear_hash_ctx(&s->write_hash);
2242 void SSL_set_connect_state(SSL *s)
2246 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2247 s->handshake_func=s->method->ssl_connect;
2248 /* clear the current cipher */
2249 ssl_clear_cipher_ctx(s);
2250 ssl_clear_hash_ctx(&s->read_hash);
2251 ssl_clear_hash_ctx(&s->write_hash);
2254 int ssl_undefined_function(SSL *s)
2256 SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2260 int ssl_undefined_void_function(void)
2262 SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2266 int ssl_undefined_const_function(const SSL *s)
2268 SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2272 SSL_METHOD *ssl_bad_method(int ver)
2274 SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2278 const char *SSL_get_version(const SSL *s)
2280 if (s->version == TLS1_VERSION)
2282 else if (s->version == SSL3_VERSION)
2284 else if (s->version == SSL2_VERSION)
2290 SSL *SSL_dup(SSL *s)
2292 STACK_OF(X509_NAME) *sk;
2297 if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2300 ret->version = s->version;
2301 ret->type = s->type;
2302 ret->method = s->method;
2304 if (s->session != NULL)
2306 /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
2307 SSL_copy_session_id(ret,s);
2311 /* No session has been established yet, so we have to expect
2312 * that s->cert or ret->cert will be changed later --
2313 * they should not both point to the same object,
2314 * and thus we can't use SSL_copy_session_id. */
2316 ret->method->ssl_free(ret);
2317 ret->method = s->method;
2318 ret->method->ssl_new(ret);
2320 if (s->cert != NULL)
2322 if (ret->cert != NULL)
2324 ssl_cert_free(ret->cert);
2326 ret->cert = ssl_cert_dup(s->cert);
2327 if (ret->cert == NULL)
2331 SSL_set_session_id_context(ret,
2332 s->sid_ctx, s->sid_ctx_length);
2335 ret->options=s->options;
2337 SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s));
2338 SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
2339 ret->msg_callback = s->msg_callback;
2340 ret->msg_callback_arg = s->msg_callback_arg;
2341 SSL_set_verify(ret,SSL_get_verify_mode(s),
2342 SSL_get_verify_callback(s));
2343 SSL_set_verify_depth(ret,SSL_get_verify_depth(s));
2344 ret->generate_session_id = s->generate_session_id;
2346 SSL_set_info_callback(ret,SSL_get_info_callback(s));
2348 ret->debug=s->debug;
2350 /* copy app data, a little dangerous perhaps */
2351 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
2354 /* setup rbio, and wbio */
2355 if (s->rbio != NULL)
2357 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
2360 if (s->wbio != NULL)
2362 if (s->wbio != s->rbio)
2364 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
2368 ret->wbio=ret->rbio;
2370 ret->rwstate = s->rwstate;
2371 ret->in_handshake = s->in_handshake;
2372 ret->handshake_func = s->handshake_func;
2373 ret->server = s->server;
2374 ret->new_session = s->new_session;
2375 ret->quiet_shutdown = s->quiet_shutdown;
2376 ret->shutdown=s->shutdown;
2377 ret->state=s->state; /* SSL_dup does not really work at any state, though */
2378 ret->rstate=s->rstate;
2379 ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
2382 X509_VERIFY_PARAM_inherit(ret->param, s->param);
2384 /* dup the cipher_list and cipher_list_by_id stacks */
2385 if (s->cipher_list != NULL)
2387 if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2390 if (s->cipher_list_by_id != NULL)
2391 if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id))
2395 /* Dup the client_CA list */
2396 if (s->client_CA != NULL)
2398 if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
2400 for (i=0; i<sk_X509_NAME_num(sk); i++)
2402 xn=sk_X509_NAME_value(sk,i);
2403 if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL)
2414 if (ret != NULL) SSL_free(ret);
2420 void ssl_clear_cipher_ctx(SSL *s)
2422 if (s->enc_read_ctx != NULL)
2424 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2425 OPENSSL_free(s->enc_read_ctx);
2426 s->enc_read_ctx=NULL;
2428 if (s->enc_write_ctx != NULL)
2430 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2431 OPENSSL_free(s->enc_write_ctx);
2432 s->enc_write_ctx=NULL;
2434 #ifndef OPENSSL_NO_COMP
2435 if (s->expand != NULL)
2437 COMP_CTX_free(s->expand);
2440 if (s->compress != NULL)
2442 COMP_CTX_free(s->compress);
2448 /* Fix this function so that it takes an optional type parameter */
2449 X509 *SSL_get_certificate(const SSL *s)
2451 if (s->cert != NULL)
2452 return(s->cert->key->x509);
2457 /* Fix this function so that it takes an optional type parameter */
2458 EVP_PKEY *SSL_get_privatekey(SSL *s)
2460 if (s->cert != NULL)
2461 return(s->cert->key->privatekey);
2466 SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2468 if ((s->session != NULL) && (s->session->cipher != NULL))
2469 return(s->session->cipher);
2472 #ifdef OPENSSL_NO_COMP
2473 const void *SSL_get_current_compression(SSL *s)
2477 const void *SSL_get_current_expansion(SSL *s)
2483 const COMP_METHOD *SSL_get_current_compression(SSL *s)
2485 if (s->compress != NULL)
2486 return(s->compress->meth);
2490 const COMP_METHOD *SSL_get_current_expansion(SSL *s)
2492 if (s->expand != NULL)
2493 return(s->expand->meth);
2498 int ssl_init_wbio_buffer(SSL *s,int push)
2502 if (s->bbio == NULL)
2504 bbio=BIO_new(BIO_f_buffer());
2505 if (bbio == NULL) return(0);
2511 if (s->bbio == s->wbio)
2512 s->wbio=BIO_pop(s->wbio);
2514 (void)BIO_reset(bbio);
2515 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2516 if (!BIO_set_read_buffer_size(bbio,1))
2518 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
2523 if (s->wbio != bbio)
2524 s->wbio=BIO_push(bbio,s->wbio);
2528 if (s->wbio == bbio)
2529 s->wbio=BIO_pop(bbio);
2534 void ssl_free_wbio_buffer(SSL *s)
2536 if (s->bbio == NULL) return;
2538 if (s->bbio == s->wbio)
2540 /* remove buffering */
2541 s->wbio=BIO_pop(s->wbio);
2542 #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2543 assert(s->wbio != NULL);
2550 void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2552 ctx->quiet_shutdown=mode;
2555 int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2557 return(ctx->quiet_shutdown);
2560 void SSL_set_quiet_shutdown(SSL *s,int mode)
2562 s->quiet_shutdown=mode;
2565 int SSL_get_quiet_shutdown(const SSL *s)
2567 return(s->quiet_shutdown);
2570 void SSL_set_shutdown(SSL *s,int mode)
2575 int SSL_get_shutdown(const SSL *s)
2577 return(s->shutdown);
2580 int SSL_version(const SSL *s)
2585 SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2590 SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2592 if (ssl->ctx == ctx)
2594 #ifndef OPENSSL_NO_TLSEXT
2596 ctx = ssl->initial_ctx;
2598 if (ssl->cert != NULL)
2599 ssl_cert_free(ssl->cert);
2600 ssl->cert = ssl_cert_dup(ctx->cert);
2601 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2602 if (ssl->ctx != NULL)
2603 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2608 #ifndef OPENSSL_NO_STDIO
2609 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2611 return(X509_STORE_set_default_paths(ctx->cert_store));
2614 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2617 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2621 void SSL_set_info_callback(SSL *ssl,
2622 void (*cb)(const SSL *ssl,int type,int val))
2624 ssl->info_callback=cb;
2627 /* One compiler (Diab DCC) doesn't like argument names in returned
2628 function pointer. */
2629 void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2631 return ssl->info_callback;
2634 int SSL_state(const SSL *ssl)
2639 void SSL_set_verify_result(SSL *ssl,long arg)
2641 ssl->verify_result=arg;
2644 long SSL_get_verify_result(const SSL *ssl)
2646 return(ssl->verify_result);
2649 int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2650 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2652 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2653 new_func, dup_func, free_func);
2656 int SSL_set_ex_data(SSL *s,int idx,void *arg)
2658 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2661 void *SSL_get_ex_data(const SSL *s,int idx)
2663 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2666 int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2667 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2669 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2670 new_func, dup_func, free_func);
2673 int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2675 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2678 void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2680 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2688 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2690 return(ctx->cert_store);
2693 void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2695 if (ctx->cert_store != NULL)
2696 X509_STORE_free(ctx->cert_store);
2697 ctx->cert_store=store;
2700 int SSL_want(const SSL *s)
2706 * \brief Set the callback for generating temporary RSA keys.
2707 * \param ctx the SSL context.
2708 * \param cb the callback
2711 #ifndef OPENSSL_NO_RSA
2712 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2716 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2719 void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2723 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2729 * \brief The RSA temporary key callback function.
2730 * \param ssl the SSL session.
2731 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2732 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2733 * of the required key in bits.
2734 * \return the temporary RSA key.
2735 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2738 RSA *cb(SSL *ssl,int is_export,int keylength)
2743 * \brief Set the callback for generating temporary DH keys.
2744 * \param ctx the SSL context.
2745 * \param dh the callback
2748 #ifndef OPENSSL_NO_DH
2749 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2752 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2755 void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2758 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2762 #ifndef OPENSSL_NO_ECDH
2763 void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2766 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2769 void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2772 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2776 #ifndef OPENSSL_NO_PSK
2777 int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2779 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2781 SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
2784 if (ctx->psk_identity_hint != NULL)
2785 OPENSSL_free(ctx->psk_identity_hint);
2786 if (identity_hint != NULL)
2788 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2789 if (ctx->psk_identity_hint == NULL)
2793 ctx->psk_identity_hint = NULL;
2797 int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2802 if (s->session == NULL)
2803 return 1; /* session not created yet, ignored */
2805 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2807 SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
2810 if (s->session->psk_identity_hint != NULL)
2811 OPENSSL_free(s->session->psk_identity_hint);
2812 if (identity_hint != NULL)
2814 s->session->psk_identity_hint = BUF_strdup(identity_hint);
2815 if (s->session->psk_identity_hint == NULL)
2819 s->session->psk_identity_hint = NULL;
2823 const char *SSL_get_psk_identity_hint(const SSL *s)
2825 if (s == NULL || s->session == NULL)
2827 return(s->session->psk_identity_hint);
2830 const char *SSL_get_psk_identity(const SSL *s)
2832 if (s == NULL || s->session == NULL)
2834 return(s->session->psk_identity);
2837 void SSL_set_psk_client_callback(SSL *s,
2838 unsigned int (*cb)(SSL *ssl, const char *hint,
2839 char *identity, unsigned int max_identity_len, unsigned char *psk,
2840 unsigned int max_psk_len))
2842 s->psk_client_callback = cb;
2845 void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2846 unsigned int (*cb)(SSL *ssl, const char *hint,
2847 char *identity, unsigned int max_identity_len, unsigned char *psk,
2848 unsigned int max_psk_len))
2850 ctx->psk_client_callback = cb;
2853 void SSL_set_psk_server_callback(SSL *s,
2854 unsigned int (*cb)(SSL *ssl, const char *identity,
2855 unsigned char *psk, unsigned int max_psk_len))
2857 s->psk_server_callback = cb;
2860 void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2861 unsigned int (*cb)(SSL *ssl, const char *identity,
2862 unsigned char *psk, unsigned int max_psk_len))
2864 ctx->psk_server_callback = cb;
2868 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))
2870 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2872 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))
2874 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2877 /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
2878 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
2879 * any. If EVP_MD pointer is passed, initializes ctx with this md
2880 * Returns newly allocated ctx;
2883 EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
2885 ssl_clear_hash_ctx(hash);
2886 *hash = EVP_MD_CTX_create();
2887 if (md) EVP_DigestInit_ex(*hash,md,NULL);
2890 void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
2893 if (*hash) EVP_MD_CTX_destroy(*hash);
2897 #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
2898 #include "../crypto/bio/bss_file.c"
2901 IMPLEMENT_STACK_OF(SSL_CIPHER)
2902 IMPLEMENT_STACK_OF(SSL_COMP)