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-2006 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.
123 #include "ssl_locl.h"
124 #include "kssl_lcl.h"
125 #include <openssl/objects.h>
126 #include <openssl/lhash.h>
127 #include <openssl/x509v3.h>
128 #ifndef OPENSSL_NO_DH
129 #include <openssl/dh.h>
132 const char *SSL_version_str=OPENSSL_VERSION_TEXT;
134 SSL3_ENC_METHOD ssl3_undef_enc_method={
135 /* evil casts, but these functions are only called if there's a library bug */
136 (int (*)(SSL *,int))ssl_undefined_function,
137 (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
138 ssl_undefined_function,
139 (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
140 (int (*)(SSL*, int))ssl_undefined_function,
141 (int (*)(SSL *, EVP_MD_CTX *, EVP_MD_CTX *, const char*, int, unsigned char *))ssl_undefined_function,
142 0, /* finish_mac_length */
143 (int (*)(SSL *, EVP_MD_CTX *, unsigned char *))ssl_undefined_function,
144 NULL, /* client_finished_label */
145 0, /* client_finished_label_len */
146 NULL, /* server_finished_label */
147 0, /* server_finished_label_len */
148 (int (*)(int))ssl_undefined_function
151 int SSL_clear(SSL *s)
154 if (s->method == NULL)
156 SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
160 if (ssl_clear_bad_session(s))
162 SSL_SESSION_free(s->session);
170 #if 0 /* Disabled since version 1.10 of this file (early return not
171 * needed because SSL_clear is not called when doing renegotiation) */
172 /* This is set if we are doing dynamic renegotiation so keep
173 * the old cipher. It is sort of a SSL_clear_lite :-) */
174 if (s->new_session) return(1);
178 SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);
185 s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
187 s->version=s->method->version;
188 s->client_version=s->version;
189 s->rwstate=SSL_NOTHING;
190 s->rstate=SSL_ST_READ_HEADER;
192 s->read_ahead=s->ctx->read_ahead;
195 if (s->init_buf != NULL)
197 BUF_MEM_free(s->init_buf);
201 ssl_clear_cipher_ctx(s);
206 /* Check to see if we were changed into a different method, if
207 * so, revert back if we are not doing session-id reuse. */
208 if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
210 s->method->ssl_free(s);
211 s->method=s->ctx->method;
212 if (!s->method->ssl_new(s))
217 s->method->ssl_clear(s);
221 /** Used to change an SSL_CTXs default SSL method type */
222 int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
224 STACK_OF(SSL_CIPHER) *sk;
228 sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
229 &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
230 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
232 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
238 SSL *SSL_new(SSL_CTX *ctx)
244 SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
247 if (ctx->method == NULL)
249 SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
253 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
254 if (s == NULL) goto err;
255 memset(s,0,sizeof(SSL));
257 #ifndef OPENSSL_NO_KRB5
258 s->kssl_ctx = kssl_ctx_new();
259 #endif /* OPENSSL_NO_KRB5 */
261 s->options=ctx->options;
263 s->max_cert_list=ctx->max_cert_list;
265 if (ctx->cert != NULL)
267 /* Earlier library versions used to copy the pointer to
268 * the CERT, not its contents; only when setting new
269 * parameters for the per-SSL copy, ssl_cert_new would be
270 * called (and the direct reference to the per-SSL_CTX
271 * settings would be lost, but those still were indirectly
272 * accessed for various purposes, and for that reason they
273 * used to be known as s->ctx->default_cert).
274 * Now we don't look at the SSL_CTX's CERT after having
275 * duplicated it once. */
277 s->cert = ssl_cert_dup(ctx->cert);
282 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
284 s->read_ahead=ctx->read_ahead;
285 s->msg_callback=ctx->msg_callback;
286 s->msg_callback_arg=ctx->msg_callback_arg;
287 s->verify_mode=ctx->verify_mode;
289 s->verify_depth=ctx->verify_depth;
291 s->sid_ctx_length=ctx->sid_ctx_length;
292 OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
293 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
294 s->verify_callback=ctx->default_verify_callback;
295 s->generate_session_id=ctx->generate_session_id;
297 s->param = X509_VERIFY_PARAM_new();
300 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
302 s->purpose = ctx->purpose;
303 s->trust = ctx->trust;
305 s->quiet_shutdown=ctx->quiet_shutdown;
306 s->max_send_fragment = ctx->max_send_fragment;
308 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
310 #ifndef OPENSSL_NO_TLSEXT
311 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
315 s->verify_result=X509_V_OK;
317 s->method=ctx->method;
319 if (!s->method->ssl_new(s))
323 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
327 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
334 ssl_cert_free(s->cert);
336 SSL_CTX_free(s->ctx); /* decrement reference count */
339 SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
343 int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
344 unsigned int sid_ctx_len)
346 if(sid_ctx_len > sizeof ctx->sid_ctx)
348 SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
351 ctx->sid_ctx_length=sid_ctx_len;
352 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
357 int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
358 unsigned int sid_ctx_len)
360 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
362 SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
365 ssl->sid_ctx_length=sid_ctx_len;
366 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
371 int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
373 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
374 ctx->generate_session_id = cb;
375 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
379 int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
381 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
382 ssl->generate_session_id = cb;
383 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
387 int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
390 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
391 * we can "construct" a session to give us the desired check - ie. to
392 * find if there's a session in the hash table that would conflict with
393 * any new session built out of this id/id_len and the ssl_version in
394 * use by this SSL. */
397 if(id_len > sizeof r.session_id)
400 r.ssl_version = ssl->version;
401 r.session_id_length = id_len;
402 memcpy(r.session_id, id, id_len);
403 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
404 * callback is calling us to check the uniqueness of a shorter ID, it
405 * must be compared as a padded-out ID because that is what it will be
406 * converted to when the callback has finished choosing it. */
407 if((r.ssl_version == SSL2_VERSION) &&
408 (id_len < SSL2_SSL_SESSION_ID_LENGTH))
410 memset(r.session_id + id_len, 0,
411 SSL2_SSL_SESSION_ID_LENGTH - id_len);
412 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
415 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
416 p = (SSL_SESSION *)lh_retrieve(ssl->ctx->sessions, &r);
417 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
421 int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
423 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
426 int SSL_set_purpose(SSL *s, int purpose)
428 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
431 int SSL_CTX_set_trust(SSL_CTX *s, int trust)
433 return X509_VERIFY_PARAM_set_trust(s->param, trust);
436 int SSL_set_trust(SSL *s, int trust)
438 return X509_VERIFY_PARAM_set_trust(s->param, trust);
441 void SSL_free(SSL *s)
448 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
456 fprintf(stderr,"SSL_free, bad reference count\n");
462 X509_VERIFY_PARAM_free(s->param);
464 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
468 /* If the buffering BIO is in place, pop it off */
469 if (s->bbio == s->wbio)
471 s->wbio=BIO_pop(s->wbio);
477 BIO_free_all(s->rbio);
478 if ((s->wbio != NULL) && (s->wbio != s->rbio))
479 BIO_free_all(s->wbio);
481 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
483 /* add extra stuff */
484 if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);
485 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
487 /* Make the next call work :-) */
488 if (s->session != NULL)
490 ssl_clear_bad_session(s);
491 SSL_SESSION_free(s->session);
494 ssl_clear_cipher_ctx(s);
496 if (s->cert != NULL) ssl_cert_free(s->cert);
497 /* Free up if allocated */
499 if (s->ctx) SSL_CTX_free(s->ctx);
500 #ifndef OPENSSL_NO_TLSEXT
501 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
504 if (s->client_CA != NULL)
505 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
507 if (s->method != NULL) s->method->ssl_free(s);
509 #ifndef OPENSSL_NO_KRB5
510 if (s->kssl_ctx != NULL)
511 kssl_ctx_free(s->kssl_ctx);
512 #endif /* OPENSSL_NO_KRB5 */
517 void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
519 /* If the output buffering BIO is still in place, remove it
523 if (s->wbio == s->bbio)
525 s->wbio=s->wbio->next_bio;
526 s->bbio->next_bio=NULL;
529 if ((s->rbio != NULL) && (s->rbio != rbio))
530 BIO_free_all(s->rbio);
531 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
532 BIO_free_all(s->wbio);
537 BIO *SSL_get_rbio(const SSL *s)
540 BIO *SSL_get_wbio(const SSL *s)
543 int SSL_get_fd(const SSL *s)
545 return(SSL_get_rfd(s));
548 int SSL_get_rfd(const SSL *s)
554 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
560 int SSL_get_wfd(const SSL *s)
566 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
572 #ifndef OPENSSL_NO_SOCK
573 int SSL_set_fd(SSL *s,int fd)
578 bio=BIO_new(BIO_s_socket());
582 SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
585 BIO_set_fd(bio,fd,BIO_NOCLOSE);
586 SSL_set_bio(s,bio,bio);
592 int SSL_set_wfd(SSL *s,int fd)
597 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
598 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
600 bio=BIO_new(BIO_s_socket());
603 { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
604 BIO_set_fd(bio,fd,BIO_NOCLOSE);
605 SSL_set_bio(s,SSL_get_rbio(s),bio);
608 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
614 int SSL_set_rfd(SSL *s,int fd)
619 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
620 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
622 bio=BIO_new(BIO_s_socket());
626 SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
629 BIO_set_fd(bio,fd,BIO_NOCLOSE);
630 SSL_set_bio(s,bio,SSL_get_wbio(s));
633 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
641 /* return length of latest Finished message we sent, copy to 'buf' */
642 size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
648 ret = s->s3->tmp.finish_md_len;
651 memcpy(buf, s->s3->tmp.finish_md, count);
656 /* return length of latest Finished message we expected, copy to 'buf' */
657 size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
663 ret = s->s3->tmp.peer_finish_md_len;
666 memcpy(buf, s->s3->tmp.peer_finish_md, count);
672 int SSL_get_verify_mode(const SSL *s)
674 return(s->verify_mode);
677 int SSL_get_verify_depth(const SSL *s)
679 return X509_VERIFY_PARAM_get_depth(s->param);
682 int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
684 return(s->verify_callback);
687 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
689 return(ctx->verify_mode);
692 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
694 return X509_VERIFY_PARAM_get_depth(ctx->param);
697 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
699 return(ctx->default_verify_callback);
702 void SSL_set_verify(SSL *s,int mode,
703 int (*callback)(int ok,X509_STORE_CTX *ctx))
706 if (callback != NULL)
707 s->verify_callback=callback;
710 void SSL_set_verify_depth(SSL *s,int depth)
712 X509_VERIFY_PARAM_set_depth(s->param, depth);
715 void SSL_set_read_ahead(SSL *s,int yes)
720 int SSL_get_read_ahead(const SSL *s)
722 return(s->read_ahead);
725 int SSL_pending(const SSL *s)
727 /* SSL_pending cannot work properly if read-ahead is enabled
728 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
729 * and it is impossible to fix since SSL_pending cannot report
730 * errors that may be observed while scanning the new data.
731 * (Note that SSL_pending() is often used as a boolean value,
732 * so we'd better not return -1.)
734 return(s->method->ssl_pending(s));
737 X509 *SSL_get_peer_certificate(const SSL *s)
741 if ((s == NULL) || (s->session == NULL))
746 if (r == NULL) return(r);
748 CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
753 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
757 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
760 r=s->session->sess_cert->cert_chain;
762 /* If we are a client, cert_chain includes the peer's own
763 * certificate; if we are a server, it does not. */
768 /* Now in theory, since the calling process own 't' it should be safe to
769 * modify. We need to be able to read f without being hassled */
770 void SSL_copy_session_id(SSL *t,const SSL *f)
774 /* Do we need to to SSL locking? */
775 SSL_set_session(t,SSL_get_session(f));
777 /* what if we are setup as SSLv2 but want to talk SSLv3 or
779 if (t->method != f->method)
781 t->method->ssl_free(t); /* cleanup current */
782 t->method=f->method; /* change method */
783 t->method->ssl_new(t); /* setup new */
789 CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
794 if (tmp != NULL) ssl_cert_free(tmp);
795 SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length);
798 /* Fix this so it checks all the valid key/cert options */
799 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
801 if ( (ctx == NULL) ||
802 (ctx->cert == NULL) ||
803 (ctx->cert->key->x509 == NULL))
805 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
808 if (ctx->cert->key->privatekey == NULL)
810 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
813 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
816 /* Fix this function so that it takes an optional type parameter */
817 int SSL_check_private_key(const SSL *ssl)
821 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
824 if (ssl->cert == NULL)
826 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
829 if (ssl->cert->key->x509 == NULL)
831 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
834 if (ssl->cert->key->privatekey == NULL)
836 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
839 return(X509_check_private_key(ssl->cert->key->x509,
840 ssl->cert->key->privatekey));
843 int SSL_accept(SSL *s)
845 if (s->handshake_func == 0)
846 /* Not properly initialized yet */
847 SSL_set_accept_state(s);
849 return(s->method->ssl_accept(s));
852 int SSL_connect(SSL *s)
854 if (s->handshake_func == 0)
855 /* Not properly initialized yet */
856 SSL_set_connect_state(s);
858 return(s->method->ssl_connect(s));
861 long SSL_get_default_timeout(const SSL *s)
863 return(s->method->get_timeout());
866 int SSL_read(SSL *s,void *buf,int num)
868 if (s->handshake_func == 0)
870 SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
874 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
876 s->rwstate=SSL_NOTHING;
879 return(s->method->ssl_read(s,buf,num));
882 int SSL_peek(SSL *s,void *buf,int num)
884 if (s->handshake_func == 0)
886 SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
890 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
894 return(s->method->ssl_peek(s,buf,num));
897 int SSL_write(SSL *s,const void *buf,int num)
899 if (s->handshake_func == 0)
901 SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
905 if (s->shutdown & SSL_SENT_SHUTDOWN)
907 s->rwstate=SSL_NOTHING;
908 SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
911 return(s->method->ssl_write(s,buf,num));
914 int SSL_shutdown(SSL *s)
916 /* Note that this function behaves differently from what one might
917 * expect. Return values are 0 for no success (yet),
918 * 1 for success; but calling it once is usually not enough,
919 * even if blocking I/O is used (see ssl3_shutdown).
922 if (s->handshake_func == 0)
924 SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
928 if ((s != NULL) && !SSL_in_init(s))
929 return(s->method->ssl_shutdown(s));
934 int SSL_renegotiate(SSL *s)
936 if (s->new_session == 0)
940 return(s->method->ssl_renegotiate(s));
943 int SSL_renegotiate_pending(SSL *s)
945 /* becomes true when negotiation is requested;
946 * false again once a handshake has finished */
947 return (s->new_session != 0);
950 long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
956 case SSL_CTRL_GET_READ_AHEAD:
957 return(s->read_ahead);
958 case SSL_CTRL_SET_READ_AHEAD:
963 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
964 s->msg_callback_arg = parg;
967 case SSL_CTRL_OPTIONS:
968 return(s->options|=larg);
970 return(s->mode|=larg);
971 case SSL_CTRL_GET_MAX_CERT_LIST:
972 return(s->max_cert_list);
973 case SSL_CTRL_SET_MAX_CERT_LIST:
975 s->max_cert_list=larg;
977 case SSL_CTRL_SET_MTU:
978 if (SSL_version(s) == DTLS1_VERSION)
984 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
985 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
987 s->max_send_fragment = larg;
990 return(s->method->ssl_ctrl(s,cmd,larg,parg));
994 long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
998 case SSL_CTRL_SET_MSG_CALLBACK:
999 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1003 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1007 struct lhash_st *SSL_CTX_sessions(SSL_CTX *ctx)
1009 return ctx->sessions;
1012 long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1018 case SSL_CTRL_GET_READ_AHEAD:
1019 return(ctx->read_ahead);
1020 case SSL_CTRL_SET_READ_AHEAD:
1022 ctx->read_ahead=larg;
1025 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1026 ctx->msg_callback_arg = parg;
1029 case SSL_CTRL_GET_MAX_CERT_LIST:
1030 return(ctx->max_cert_list);
1031 case SSL_CTRL_SET_MAX_CERT_LIST:
1032 l=ctx->max_cert_list;
1033 ctx->max_cert_list=larg;
1036 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1037 l=ctx->session_cache_size;
1038 ctx->session_cache_size=larg;
1040 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1041 return(ctx->session_cache_size);
1042 case SSL_CTRL_SET_SESS_CACHE_MODE:
1043 l=ctx->session_cache_mode;
1044 ctx->session_cache_mode=larg;
1046 case SSL_CTRL_GET_SESS_CACHE_MODE:
1047 return(ctx->session_cache_mode);
1049 case SSL_CTRL_SESS_NUMBER:
1050 return(ctx->sessions->num_items);
1051 case SSL_CTRL_SESS_CONNECT:
1052 return(ctx->stats.sess_connect);
1053 case SSL_CTRL_SESS_CONNECT_GOOD:
1054 return(ctx->stats.sess_connect_good);
1055 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1056 return(ctx->stats.sess_connect_renegotiate);
1057 case SSL_CTRL_SESS_ACCEPT:
1058 return(ctx->stats.sess_accept);
1059 case SSL_CTRL_SESS_ACCEPT_GOOD:
1060 return(ctx->stats.sess_accept_good);
1061 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1062 return(ctx->stats.sess_accept_renegotiate);
1063 case SSL_CTRL_SESS_HIT:
1064 return(ctx->stats.sess_hit);
1065 case SSL_CTRL_SESS_CB_HIT:
1066 return(ctx->stats.sess_cb_hit);
1067 case SSL_CTRL_SESS_MISSES:
1068 return(ctx->stats.sess_miss);
1069 case SSL_CTRL_SESS_TIMEOUTS:
1070 return(ctx->stats.sess_timeout);
1071 case SSL_CTRL_SESS_CACHE_FULL:
1072 return(ctx->stats.sess_cache_full);
1073 case SSL_CTRL_OPTIONS:
1074 return(ctx->options|=larg);
1076 return(ctx->mode|=larg);
1077 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1078 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1080 ctx->max_send_fragment = larg;
1083 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1087 long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1091 case SSL_CTRL_SET_MSG_CALLBACK:
1092 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1096 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1100 int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
1108 return((l > 0)?1:-1);
1111 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap,
1112 const SSL_CIPHER * const *bp)
1116 l=(*ap)->id-(*bp)->id;
1120 return((l > 0)?1:-1);
1123 /** return a STACK of the ciphers available for the SSL and in order of
1125 STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1129 if (s->cipher_list != NULL)
1131 return(s->cipher_list);
1133 else if ((s->ctx != NULL) &&
1134 (s->ctx->cipher_list != NULL))
1136 return(s->ctx->cipher_list);
1142 /** return a STACK of the ciphers available for the SSL and in order of
1144 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1148 if (s->cipher_list_by_id != NULL)
1150 return(s->cipher_list_by_id);
1152 else if ((s->ctx != NULL) &&
1153 (s->ctx->cipher_list_by_id != NULL))
1155 return(s->ctx->cipher_list_by_id);
1161 /** The old interface to get the same thing as SSL_get_ciphers() */
1162 const char *SSL_get_cipher_list(const SSL *s,int n)
1165 STACK_OF(SSL_CIPHER) *sk;
1167 if (s == NULL) return(NULL);
1168 sk=SSL_get_ciphers(s);
1169 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1171 c=sk_SSL_CIPHER_value(sk,n);
1172 if (c == NULL) return(NULL);
1176 /** specify the ciphers to be used by default by the SSL_CTX */
1177 int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1179 STACK_OF(SSL_CIPHER) *sk;
1181 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1182 &ctx->cipher_list_by_id,str);
1183 /* ssl_create_cipher_list may return an empty stack if it
1184 * was unable to find a cipher matching the given rule string
1185 * (for example if the rule string specifies a cipher which
1186 * has been disabled). This is not an error as far as
1187 * ssl_create_cipher_list is concerned, and hence
1188 * ctx->cipher_list and ctx->cipher_list_by_id has been
1192 else if (sk_SSL_CIPHER_num(sk) == 0)
1194 SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1200 /** specify the ciphers to be used by the SSL */
1201 int SSL_set_cipher_list(SSL *s,const char *str)
1203 STACK_OF(SSL_CIPHER) *sk;
1205 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1206 &s->cipher_list_by_id,str);
1207 /* see comment in SSL_CTX_set_cipher_list */
1210 else if (sk_SSL_CIPHER_num(sk) == 0)
1212 SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1218 /* works well for SSLv2, not so good for SSLv3 */
1219 char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1223 STACK_OF(SSL_CIPHER) *sk;
1227 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1232 sk=s->session->ciphers;
1233 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1235 /* Decrement for either the ':' or a '\0' */
1237 c=sk_SSL_CIPHER_value(sk,i);
1238 for (cp=c->name; *cp; )
1254 int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
1255 int (*put_cb)(const SSL_CIPHER *, unsigned char *))
1260 #ifndef OPENSSL_NO_KRB5
1261 int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx);
1262 #endif /* OPENSSL_NO_KRB5 */
1264 if (sk == NULL) return(0);
1267 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1269 c=sk_SSL_CIPHER_value(sk,i);
1270 #ifndef OPENSSL_NO_KRB5
1271 if ((c->algorithms & SSL_KRB5) && nokrb5)
1273 #endif /* OPENSSL_NO_KRB5 */
1275 j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
1281 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
1282 STACK_OF(SSL_CIPHER) **skp)
1285 STACK_OF(SSL_CIPHER) *sk;
1288 n=ssl_put_cipher_by_char(s,NULL,NULL);
1291 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1294 if ((skp == NULL) || (*skp == NULL))
1295 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1299 sk_SSL_CIPHER_zero(sk);
1302 for (i=0; i<num; i+=n)
1304 c=ssl_get_cipher_by_char(s,p);
1308 if (!sk_SSL_CIPHER_push(sk,c))
1310 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
1320 if ((skp == NULL) || (*skp == NULL))
1321 sk_SSL_CIPHER_free(sk);
1326 #ifndef OPENSSL_TLSEXT
1327 /** return a servername extension value if provided in Client Hello, or NULL.
1328 * So far, only host_name types are defined (RFC 3546).
1331 const char *SSL_get_servername(const SSL *s, const int type)
1333 if (type != TLSEXT_NAMETYPE_host_name)
1335 /* XXX cf. SSL_CTRL_GET_TLSEXT_HOSTNAME case in ssl3_ctrl (s3_lib.c) */
1336 return s->session /*&&s->session->tlsext_hostname*/ ?
1337 s->session->tlsext_hostname :
1341 int SSL_get_servername_type(const SSL *s)
1343 if (s->session &&s->session->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)
1344 return TLSEXT_NAMETYPE_host_name;
1349 unsigned long SSL_SESSION_hash(const SSL_SESSION *a)
1354 ((unsigned int) a->session_id[0] )|
1355 ((unsigned int) a->session_id[1]<< 8L)|
1356 ((unsigned long)a->session_id[2]<<16L)|
1357 ((unsigned long)a->session_id[3]<<24L);
1361 /* NB: If this function (or indeed the hash function which uses a sort of
1362 * coarser function than this one) is changed, ensure
1363 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1364 * able to construct an SSL_SESSION that will collide with any existing session
1365 * with a matching session ID. */
1366 int SSL_SESSION_cmp(const SSL_SESSION *a,const SSL_SESSION *b)
1368 if (a->ssl_version != b->ssl_version)
1370 if (a->session_id_length != b->session_id_length)
1372 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1375 /* These wrapper functions should remain rather than redeclaring
1376 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1377 * variable. The reason is that the functions aren't static, they're exposed via
1379 static IMPLEMENT_LHASH_HASH_FN(SSL_SESSION_hash, SSL_SESSION *)
1380 static IMPLEMENT_LHASH_COMP_FN(SSL_SESSION_cmp, SSL_SESSION *)
1382 SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1388 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
1392 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1394 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1397 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1401 memset(ret,0,sizeof(SSL_CTX));
1405 ret->cert_store=NULL;
1406 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1407 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1408 ret->session_cache_head=NULL;
1409 ret->session_cache_tail=NULL;
1411 /* We take the system default */
1412 ret->session_timeout=meth->get_timeout();
1414 ret->new_session_cb=0;
1415 ret->remove_session_cb=0;
1416 ret->get_session_cb=0;
1417 ret->generate_session_id=0;
1419 memset((char *)&ret->stats,0,sizeof(ret->stats));
1422 ret->quiet_shutdown=0;
1424 /* ret->cipher=NULL;*/
1425 /* ret->s2->challenge=NULL;
1426 ret->master_key=NULL;
1428 ret->s2->conn_id=NULL; */
1430 ret->info_callback=NULL;
1432 ret->app_verify_callback=0;
1433 ret->app_verify_arg=NULL;
1435 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1437 ret->msg_callback=0;
1438 ret->msg_callback_arg=NULL;
1439 ret->verify_mode=SSL_VERIFY_NONE;
1441 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1443 ret->sid_ctx_length=0;
1444 ret->default_verify_callback=NULL;
1445 if ((ret->cert=ssl_cert_new()) == NULL)
1448 ret->default_passwd_callback=0;
1449 ret->default_passwd_callback_userdata=NULL;
1450 ret->client_cert_cb=0;
1451 ret->app_gen_cookie_cb=0;
1452 ret->app_verify_cookie_cb=0;
1454 ret->sessions=lh_new(LHASH_HASH_FN(SSL_SESSION_hash),
1455 LHASH_COMP_FN(SSL_SESSION_cmp));
1456 if (ret->sessions == NULL) goto err;
1457 ret->cert_store=X509_STORE_new();
1458 if (ret->cert_store == NULL) goto err;
1460 ssl_create_cipher_list(ret->method,
1461 &ret->cipher_list,&ret->cipher_list_by_id,
1462 SSL_DEFAULT_CIPHER_LIST);
1463 if (ret->cipher_list == NULL
1464 || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
1466 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
1470 ret->param = X509_VERIFY_PARAM_new();
1474 if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
1476 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
1479 if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
1481 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
1484 if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
1486 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
1490 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1493 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1495 ret->extra_certs=NULL;
1496 ret->comp_methods=SSL_COMP_get_compression_methods();
1498 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1500 #ifndef OPENSSL_NO_TLSEXT
1501 ret->tlsext_servername_callback = 0;
1502 ret->tlsext_servername_arg = NULL;
1506 SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
1508 if (ret != NULL) SSL_CTX_free(ret);
1513 static void SSL_COMP_free(SSL_COMP *comp)
1514 { OPENSSL_free(comp); }
1517 void SSL_CTX_free(SSL_CTX *a)
1521 if (a == NULL) return;
1523 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1525 REF_PRINT("SSL_CTX",a);
1531 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1537 X509_VERIFY_PARAM_free(a->param);
1540 * Free internal session cache. However: the remove_cb() may reference
1541 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1542 * after the sessions were flushed.
1543 * As the ex_data handling routines might also touch the session cache,
1544 * the most secure solution seems to be: empty (flush) the cache, then
1545 * free ex_data, then finally free the cache.
1546 * (See ticket [openssl.org #212].)
1548 if (a->sessions != NULL)
1549 SSL_CTX_flush_sessions(a,0);
1551 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
1553 if (a->sessions != NULL)
1554 lh_free(a->sessions);
1556 if (a->cert_store != NULL)
1557 X509_STORE_free(a->cert_store);
1558 if (a->cipher_list != NULL)
1559 sk_SSL_CIPHER_free(a->cipher_list);
1560 if (a->cipher_list_by_id != NULL)
1561 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1562 if (a->cert != NULL)
1563 ssl_cert_free(a->cert);
1564 if (a->client_CA != NULL)
1565 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
1566 if (a->extra_certs != NULL)
1567 sk_X509_pop_free(a->extra_certs,X509_free);
1568 #if 0 /* This should never be done, since it removes a global database */
1569 if (a->comp_methods != NULL)
1570 sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
1572 a->comp_methods = NULL;
1577 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
1579 ctx->default_passwd_callback=cb;
1582 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
1584 ctx->default_passwd_callback_userdata=u;
1587 void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
1589 ctx->app_verify_callback=cb;
1590 ctx->app_verify_arg=arg;
1593 void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
1595 ctx->verify_mode=mode;
1596 ctx->default_verify_callback=cb;
1599 void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
1601 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1604 void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
1607 int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1608 int rsa_enc_export,dh_rsa_export,dh_dsa_export;
1609 int rsa_tmp_export,dh_tmp_export,kl;
1610 unsigned long mask,emask;
1611 int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size;
1612 #ifndef OPENSSL_NO_ECDH
1616 EVP_PKEY *ecc_pkey = NULL;
1617 int signature_nid = 0;
1619 if (c == NULL) return;
1621 kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
1623 #ifndef OPENSSL_NO_RSA
1624 rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
1625 rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
1626 (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
1628 rsa_tmp=rsa_tmp_export=0;
1630 #ifndef OPENSSL_NO_DH
1631 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
1632 dh_tmp_export=(c->dh_tmp_cb != NULL ||
1633 (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
1635 dh_tmp=dh_tmp_export=0;
1638 #ifndef OPENSSL_NO_ECDH
1639 have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL);
1641 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
1642 rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
1643 rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1644 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
1645 rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1646 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
1647 dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1648 cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
1649 dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
1650 dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1651 cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1652 /* FIX THIS EAY EAY EAY */
1653 dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
1654 dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1655 cpk= &(c->pkeys[SSL_PKEY_ECC]);
1656 have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL);
1661 printf("rt=%d rte=%d dht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1662 rsa_tmp,rsa_tmp_export,dh_tmp,
1663 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1666 if (rsa_enc || (rsa_tmp && rsa_sign))
1668 if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
1672 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1673 if ( (dh_tmp || dh_rsa || dh_dsa) &&
1674 (rsa_enc || rsa_sign || dsa_sign))
1676 if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1677 (rsa_enc || rsa_sign || dsa_sign))
1687 if (dh_rsa) mask|=SSL_kDHr;
1688 if (dh_rsa_export) emask|=SSL_kDHr;
1690 if (dh_dsa) mask|=SSL_kDHd;
1691 if (dh_dsa_export) emask|=SSL_kDHd;
1693 if (rsa_enc || rsa_sign)
1708 #ifndef OPENSSL_NO_KRB5
1709 mask|=SSL_kKRB5|SSL_aKRB5;
1710 emask|=SSL_kKRB5|SSL_aKRB5;
1713 /* An ECC certificate may be usable for ECDH and/or
1714 * ECDSA cipher suites depending on the key usage extension.
1718 /* This call populates extension flags (ex_flags) */
1719 x = (c->pkeys[SSL_PKEY_ECC]).x509;
1720 X509_check_purpose(x, -1, 0);
1721 ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
1722 (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
1723 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
1724 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
1725 ecc_pkey = X509_get_pubkey(x);
1726 ecc_pkey_size = (ecc_pkey != NULL) ?
1727 EVP_PKEY_bits(ecc_pkey) : 0;
1728 EVP_PKEY_free(ecc_pkey);
1729 if ((x->sig_alg) && (x->sig_alg->algorithm))
1730 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1731 #ifndef OPENSSL_NO_ECDH
1734 if ((signature_nid == NID_md5WithRSAEncryption) ||
1735 (signature_nid == NID_md4WithRSAEncryption) ||
1736 (signature_nid == NID_md2WithRSAEncryption))
1738 mask|=SSL_kECDH|SSL_aRSA;
1739 if (ecc_pkey_size <= 163)
1740 emask|=SSL_kECDH|SSL_aRSA;
1742 if (signature_nid == NID_ecdsa_with_SHA1)
1744 mask|=SSL_kECDH|SSL_aECDSA;
1745 if (ecc_pkey_size <= 163)
1746 emask|=SSL_kECDH|SSL_aECDSA;
1750 #ifndef OPENSSL_NO_ECDSA
1759 #ifndef OPENSSL_NO_ECDH
1767 c->export_mask=emask;
1771 /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1772 #define ku_reject(x, usage) \
1773 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
1775 int check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs)
1777 unsigned long alg = cs->algorithms;
1778 EVP_PKEY *pkey = NULL;
1780 int signature_nid = 0;
1782 if (SSL_C_IS_EXPORT(cs))
1784 /* ECDH key length in export ciphers must be <= 163 bits */
1785 pkey = X509_get_pubkey(x);
1786 if (pkey == NULL) return 0;
1787 keysize = EVP_PKEY_bits(pkey);
1788 EVP_PKEY_free(pkey);
1789 if (keysize > 163) return 0;
1792 /* This call populates the ex_flags field correctly */
1793 X509_check_purpose(x, -1, 0);
1794 if ((x->sig_alg) && (x->sig_alg->algorithm))
1795 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1796 if (alg & SSL_kECDH)
1798 /* key usage, if present, must allow key agreement */
1799 if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
1803 if (alg & SSL_aECDSA)
1805 /* signature alg must be ECDSA */
1806 if (signature_nid != NID_ecdsa_with_SHA1)
1813 /* signature alg must be RSA */
1814 if ((signature_nid != NID_md5WithRSAEncryption) &&
1815 (signature_nid != NID_md4WithRSAEncryption) &&
1816 (signature_nid != NID_md2WithRSAEncryption))
1822 else if (alg & SSL_aECDSA)
1824 /* key usage, if present, must allow signing */
1825 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
1831 return 1; /* all checks are ok */
1834 /* THIS NEEDS CLEANING UP */
1835 X509 *ssl_get_server_send_cert(SSL *s)
1837 unsigned long alg,mask,kalg;
1842 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
1843 alg=s->s3->tmp.new_cipher->algorithms;
1844 is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
1845 mask=is_export?c->export_mask:c->mask;
1846 kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1848 if (kalg & SSL_kECDH)
1850 /* we don't need to look at SSL_kECDHE
1851 * since no certificate is needed for
1852 * anon ECDH and for authenticated
1853 * ECDHE, the check for the auth
1854 * algorithm will set i correctly
1855 * NOTE: For ECDH-RSA, we need an ECC
1856 * not an RSA cert but for ECDHE-RSA
1857 * we need an RSA cert. Placing the
1858 * checks for SSL_kECDH before RSA
1859 * checks ensures the correct cert is chosen.
1863 else if (kalg & SSL_aECDSA)
1867 else if (kalg & SSL_kDHr)
1869 else if (kalg & SSL_kDHd)
1871 else if (kalg & SSL_aDSS)
1872 i=SSL_PKEY_DSA_SIGN;
1873 else if (kalg & SSL_aRSA)
1875 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1876 i=SSL_PKEY_RSA_SIGN;
1880 else if (kalg & SSL_aKRB5)
1882 /* VRS something else here? */
1885 else /* if (kalg & SSL_aNULL) */
1887 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR);
1890 if (c->pkeys[i].x509 == NULL) return(NULL);
1892 return(c->pkeys[i].x509);
1895 EVP_PKEY *ssl_get_sign_pkey(SSL *s,SSL_CIPHER *cipher)
1900 alg=cipher->algorithms;
1903 if ((alg & SSL_aDSS) &&
1904 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
1905 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
1906 else if (alg & SSL_aRSA)
1908 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
1909 return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
1910 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
1911 return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
1915 else if ((alg & SSL_aECDSA) &&
1916 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
1917 return(c->pkeys[SSL_PKEY_ECC].privatekey);
1918 else /* if (alg & SSL_aNULL) */
1920 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR);
1925 void ssl_update_cache(SSL *s,int mode)
1929 /* If the session_id_length is 0, we are not supposed to cache it,
1930 * and it would be rather hard to do anyway :-) */
1931 if (s->session->session_id_length == 0) return;
1933 i=s->ctx->session_cache_mode;
1934 if ((i & mode) && (!s->hit)
1935 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
1936 || SSL_CTX_add_session(s->ctx,s->session))
1937 && (s->ctx->new_session_cb != NULL))
1939 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
1940 if (!s->ctx->new_session_cb(s,s->session))
1941 SSL_SESSION_free(s->session);
1944 /* auto flush every 255 connections */
1945 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
1946 ((i & mode) == mode))
1948 if ( (((mode & SSL_SESS_CACHE_CLIENT)
1949 ?s->ctx->stats.sess_connect_good
1950 :s->ctx->stats.sess_accept_good) & 0xff) == 0xff)
1952 SSL_CTX_flush_sessions(s->ctx,(unsigned long)time(NULL));
1957 const SSL_METHOD *SSL_get_ssl_method(SSL *s)
1962 int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
1967 if (s->method != meth)
1969 if (s->handshake_func != NULL)
1970 conn=(s->handshake_func == s->method->ssl_connect);
1972 if (s->method->version == meth->version)
1976 s->method->ssl_free(s);
1978 ret=s->method->ssl_new(s);
1982 s->handshake_func=meth->ssl_connect;
1984 s->handshake_func=meth->ssl_accept;
1989 int SSL_get_error(const SSL *s,int i)
1995 if (i > 0) return(SSL_ERROR_NONE);
1997 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
1998 * etc, where we do encode the error */
1999 if ((l=ERR_peek_error()) != 0)
2001 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2002 return(SSL_ERROR_SYSCALL);
2004 return(SSL_ERROR_SSL);
2007 if ((i < 0) && SSL_want_read(s))
2009 bio=SSL_get_rbio(s);
2010 if (BIO_should_read(bio))
2011 return(SSL_ERROR_WANT_READ);
2012 else if (BIO_should_write(bio))
2013 /* This one doesn't make too much sense ... We never try
2014 * to write to the rbio, and an application program where
2015 * rbio and wbio are separate couldn't even know what it
2017 * However if we ever set s->rwstate incorrectly
2018 * (so that we have SSL_want_read(s) instead of
2019 * SSL_want_write(s)) and rbio and wbio *are* the same,
2020 * this test works around that bug; so it might be safer
2022 return(SSL_ERROR_WANT_WRITE);
2023 else if (BIO_should_io_special(bio))
2025 reason=BIO_get_retry_reason(bio);
2026 if (reason == BIO_RR_CONNECT)
2027 return(SSL_ERROR_WANT_CONNECT);
2028 else if (reason == BIO_RR_ACCEPT)
2029 return(SSL_ERROR_WANT_ACCEPT);
2031 return(SSL_ERROR_SYSCALL); /* unknown */
2035 if ((i < 0) && SSL_want_write(s))
2037 bio=SSL_get_wbio(s);
2038 if (BIO_should_write(bio))
2039 return(SSL_ERROR_WANT_WRITE);
2040 else if (BIO_should_read(bio))
2041 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2042 return(SSL_ERROR_WANT_READ);
2043 else if (BIO_should_io_special(bio))
2045 reason=BIO_get_retry_reason(bio);
2046 if (reason == BIO_RR_CONNECT)
2047 return(SSL_ERROR_WANT_CONNECT);
2048 else if (reason == BIO_RR_ACCEPT)
2049 return(SSL_ERROR_WANT_ACCEPT);
2051 return(SSL_ERROR_SYSCALL);
2054 if ((i < 0) && SSL_want_x509_lookup(s))
2056 return(SSL_ERROR_WANT_X509_LOOKUP);
2061 if (s->version == SSL2_VERSION)
2063 /* assume it is the socket being closed */
2064 return(SSL_ERROR_ZERO_RETURN);
2068 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2069 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2070 return(SSL_ERROR_ZERO_RETURN);
2073 return(SSL_ERROR_SYSCALL);
2076 int SSL_do_handshake(SSL *s)
2080 if (s->handshake_func == NULL)
2082 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
2086 s->method->ssl_renegotiate_check(s);
2088 if (SSL_in_init(s) || SSL_in_before(s))
2090 ret=s->handshake_func(s);
2095 /* For the next 2 functions, SSL_clear() sets shutdown and so
2096 * one of these calls will reset it */
2097 void SSL_set_accept_state(SSL *s)
2101 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2102 s->handshake_func=s->method->ssl_accept;
2103 /* clear the current cipher */
2104 ssl_clear_cipher_ctx(s);
2107 void SSL_set_connect_state(SSL *s)
2111 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2112 s->handshake_func=s->method->ssl_connect;
2113 /* clear the current cipher */
2114 ssl_clear_cipher_ctx(s);
2117 int ssl_undefined_function(SSL *s)
2119 SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2123 int ssl_undefined_void_function(void)
2125 SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2129 int ssl_undefined_const_function(const SSL *s)
2131 SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2135 SSL_METHOD *ssl_bad_method(int ver)
2137 SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2141 const char *SSL_get_version(const SSL *s)
2143 if (s->version == TLS1_VERSION)
2145 else if (s->version == SSL3_VERSION)
2147 else if (s->version == SSL2_VERSION)
2153 SSL *SSL_dup(SSL *s)
2155 STACK_OF(X509_NAME) *sk;
2160 if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2163 ret->version = s->version;
2164 ret->type = s->type;
2165 ret->method = s->method;
2167 if (s->session != NULL)
2169 /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
2170 SSL_copy_session_id(ret,s);
2174 /* No session has been established yet, so we have to expect
2175 * that s->cert or ret->cert will be changed later --
2176 * they should not both point to the same object,
2177 * and thus we can't use SSL_copy_session_id. */
2179 ret->method->ssl_free(ret);
2180 ret->method = s->method;
2181 ret->method->ssl_new(ret);
2183 if (s->cert != NULL)
2185 if (ret->cert != NULL)
2187 ssl_cert_free(ret->cert);
2189 ret->cert = ssl_cert_dup(s->cert);
2190 if (ret->cert == NULL)
2194 SSL_set_session_id_context(ret,
2195 s->sid_ctx, s->sid_ctx_length);
2198 ret->options=s->options;
2200 SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s));
2201 SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
2202 ret->msg_callback = s->msg_callback;
2203 ret->msg_callback_arg = s->msg_callback_arg;
2204 SSL_set_verify(ret,SSL_get_verify_mode(s),
2205 SSL_get_verify_callback(s));
2206 SSL_set_verify_depth(ret,SSL_get_verify_depth(s));
2207 ret->generate_session_id = s->generate_session_id;
2209 SSL_set_info_callback(ret,SSL_get_info_callback(s));
2211 ret->debug=s->debug;
2213 /* copy app data, a little dangerous perhaps */
2214 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
2217 /* setup rbio, and wbio */
2218 if (s->rbio != NULL)
2220 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
2223 if (s->wbio != NULL)
2225 if (s->wbio != s->rbio)
2227 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
2231 ret->wbio=ret->rbio;
2233 ret->rwstate = s->rwstate;
2234 ret->in_handshake = s->in_handshake;
2235 ret->handshake_func = s->handshake_func;
2236 ret->server = s->server;
2237 ret->new_session = s->new_session;
2238 ret->quiet_shutdown = s->quiet_shutdown;
2239 ret->shutdown=s->shutdown;
2240 ret->state=s->state; /* SSL_dup does not really work at any state, though */
2241 ret->rstate=s->rstate;
2242 ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
2245 X509_VERIFY_PARAM_inherit(ret->param, s->param);
2247 /* dup the cipher_list and cipher_list_by_id stacks */
2248 if (s->cipher_list != NULL)
2250 if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2253 if (s->cipher_list_by_id != NULL)
2254 if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id))
2258 /* Dup the client_CA list */
2259 if (s->client_CA != NULL)
2261 if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
2263 for (i=0; i<sk_X509_NAME_num(sk); i++)
2265 xn=sk_X509_NAME_value(sk,i);
2266 if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL)
2277 if (ret != NULL) SSL_free(ret);
2283 void ssl_clear_cipher_ctx(SSL *s)
2285 if (s->enc_read_ctx != NULL)
2287 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2288 OPENSSL_free(s->enc_read_ctx);
2289 s->enc_read_ctx=NULL;
2291 if (s->enc_write_ctx != NULL)
2293 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2294 OPENSSL_free(s->enc_write_ctx);
2295 s->enc_write_ctx=NULL;
2297 #ifndef OPENSSL_NO_COMP
2298 if (s->expand != NULL)
2300 COMP_CTX_free(s->expand);
2303 if (s->compress != NULL)
2305 COMP_CTX_free(s->compress);
2311 /* Fix this function so that it takes an optional type parameter */
2312 X509 *SSL_get_certificate(const SSL *s)
2314 if (s->cert != NULL)
2315 return(s->cert->key->x509);
2320 /* Fix this function so that it takes an optional type parameter */
2321 EVP_PKEY *SSL_get_privatekey(SSL *s)
2323 if (s->cert != NULL)
2324 return(s->cert->key->privatekey);
2329 SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2331 if ((s->session != NULL) && (s->session->cipher != NULL))
2332 return(s->session->cipher);
2335 #ifdef OPENSSL_NO_COMP
2336 const void *SSL_get_current_compression(SSL *s)
2340 const void *SSL_get_current_expansion(SSL *s)
2346 const COMP_METHOD *SSL_get_current_compression(SSL *s)
2348 if (s->compress != NULL)
2349 return(s->compress->meth);
2353 const COMP_METHOD *SSL_get_current_expansion(SSL *s)
2355 if (s->expand != NULL)
2356 return(s->expand->meth);
2361 int ssl_init_wbio_buffer(SSL *s,int push)
2365 if (s->bbio == NULL)
2367 bbio=BIO_new(BIO_f_buffer());
2368 if (bbio == NULL) return(0);
2374 if (s->bbio == s->wbio)
2375 s->wbio=BIO_pop(s->wbio);
2377 (void)BIO_reset(bbio);
2378 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2379 if (!BIO_set_read_buffer_size(bbio,1))
2381 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
2386 if (s->wbio != bbio)
2387 s->wbio=BIO_push(bbio,s->wbio);
2391 if (s->wbio == bbio)
2392 s->wbio=BIO_pop(bbio);
2397 void ssl_free_wbio_buffer(SSL *s)
2399 if (s->bbio == NULL) return;
2401 if (s->bbio == s->wbio)
2403 /* remove buffering */
2404 s->wbio=BIO_pop(s->wbio);
2405 #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2406 assert(s->wbio != NULL);
2413 void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2415 ctx->quiet_shutdown=mode;
2418 int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2420 return(ctx->quiet_shutdown);
2423 void SSL_set_quiet_shutdown(SSL *s,int mode)
2425 s->quiet_shutdown=mode;
2428 int SSL_get_quiet_shutdown(const SSL *s)
2430 return(s->quiet_shutdown);
2433 void SSL_set_shutdown(SSL *s,int mode)
2438 int SSL_get_shutdown(const SSL *s)
2440 return(s->shutdown);
2443 int SSL_version(const SSL *s)
2448 SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2453 SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2455 if (ssl->cert != NULL)
2456 ssl_cert_free(ssl->cert);
2457 ssl->cert = ssl_cert_dup(ctx->cert);
2458 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2459 if (ssl->ctx != NULL)
2460 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2465 #ifndef OPENSSL_NO_STDIO
2466 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2468 return(X509_STORE_set_default_paths(ctx->cert_store));
2471 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2474 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2478 void SSL_set_info_callback(SSL *ssl,
2479 void (*cb)(const SSL *ssl,int type,int val))
2481 ssl->info_callback=cb;
2484 /* One compiler (Diab DCC) doesn't like argument names in returned
2485 function pointer. */
2486 void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2488 return ssl->info_callback;
2491 int SSL_state(const SSL *ssl)
2496 void SSL_set_verify_result(SSL *ssl,long arg)
2498 ssl->verify_result=arg;
2501 long SSL_get_verify_result(const SSL *ssl)
2503 return(ssl->verify_result);
2506 int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2507 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2509 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2510 new_func, dup_func, free_func);
2513 int SSL_set_ex_data(SSL *s,int idx,void *arg)
2515 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2518 void *SSL_get_ex_data(const SSL *s,int idx)
2520 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2523 int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2524 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2526 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2527 new_func, dup_func, free_func);
2530 int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2532 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2535 void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2537 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2545 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2547 return(ctx->cert_store);
2550 void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2552 if (ctx->cert_store != NULL)
2553 X509_STORE_free(ctx->cert_store);
2554 ctx->cert_store=store;
2557 int SSL_want(const SSL *s)
2563 * \brief Set the callback for generating temporary RSA keys.
2564 * \param ctx the SSL context.
2565 * \param cb the callback
2568 #ifndef OPENSSL_NO_RSA
2569 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2573 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2576 void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2580 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2586 * \brief The RSA temporary key callback function.
2587 * \param ssl the SSL session.
2588 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2589 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2590 * of the required key in bits.
2591 * \return the temporary RSA key.
2592 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2595 RSA *cb(SSL *ssl,int is_export,int keylength)
2600 * \brief Set the callback for generating temporary DH keys.
2601 * \param ctx the SSL context.
2602 * \param dh the callback
2605 #ifndef OPENSSL_NO_DH
2606 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2609 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2612 void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2615 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2619 #ifndef OPENSSL_NO_ECDH
2620 void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2623 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2626 void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2629 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2634 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))
2636 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2638 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))
2640 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2645 #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
2646 #include "../crypto/bio/bss_file.c"
2649 IMPLEMENT_STACK_OF(SSL_CIPHER)
2650 IMPLEMENT_STACK_OF(SSL_COMP)