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 #ifndef OPENSSL_NO_DH
156 #include <openssl/dh.h>
159 const char *SSL_version_str=OPENSSL_VERSION_TEXT;
161 SSL3_ENC_METHOD ssl3_undef_enc_method={
162 /* evil casts, but these functions are only called if there's a library bug */
163 (int (*)(SSL *,int))ssl_undefined_function,
164 (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
165 ssl_undefined_function,
166 (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
167 (int (*)(SSL*, int))ssl_undefined_function,
168 (int (*)(SSL *, const char*, int, unsigned char *))ssl_undefined_function,
169 0, /* finish_mac_length */
170 (int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
171 NULL, /* client_finished_label */
172 0, /* client_finished_label_len */
173 NULL, /* server_finished_label */
174 0, /* server_finished_label_len */
175 (int (*)(int))ssl_undefined_function
178 int SSL_clear(SSL *s)
181 if (s->method == NULL)
183 SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
187 if (ssl_clear_bad_session(s))
189 SSL_SESSION_free(s->session);
197 #if 0 /* Disabled since version 1.10 of this file (early return not
198 * needed because SSL_clear is not called when doing renegotiation) */
199 /* This is set if we are doing dynamic renegotiation so keep
200 * the old cipher. It is sort of a SSL_clear_lite :-) */
201 if (s->new_session) return(1);
205 SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);
212 s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
214 s->version=s->method->version;
215 s->client_version=s->version;
216 s->rwstate=SSL_NOTHING;
217 s->rstate=SSL_ST_READ_HEADER;
219 s->read_ahead=s->ctx->read_ahead;
222 if (s->init_buf != NULL)
224 BUF_MEM_free(s->init_buf);
228 ssl_clear_cipher_ctx(s);
229 ssl_clear_hash_ctx(&s->read_hash);
230 ssl_clear_hash_ctx(&s->write_hash);
235 /* Check to see if we were changed into a different method, if
236 * so, revert back if we are not doing session-id reuse. */
237 if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
239 s->method->ssl_free(s);
240 s->method=s->ctx->method;
241 if (!s->method->ssl_new(s))
246 s->method->ssl_clear(s);
250 /** Used to change an SSL_CTXs default SSL method type */
251 int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
253 STACK_OF(SSL_CIPHER) *sk;
257 sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
258 &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
259 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
261 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
267 SSL *SSL_new(SSL_CTX *ctx)
273 SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
276 if (ctx->method == NULL)
278 SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
282 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
283 if (s == NULL) goto err;
284 memset(s,0,sizeof(SSL));
286 #ifndef OPENSSL_NO_KRB5
287 s->kssl_ctx = kssl_ctx_new();
288 #endif /* OPENSSL_NO_KRB5 */
290 s->options=ctx->options;
292 s->max_cert_list=ctx->max_cert_list;
294 if (ctx->cert != NULL)
296 /* Earlier library versions used to copy the pointer to
297 * the CERT, not its contents; only when setting new
298 * parameters for the per-SSL copy, ssl_cert_new would be
299 * called (and the direct reference to the per-SSL_CTX
300 * settings would be lost, but those still were indirectly
301 * accessed for various purposes, and for that reason they
302 * used to be known as s->ctx->default_cert).
303 * Now we don't look at the SSL_CTX's CERT after having
304 * duplicated it once. */
306 s->cert = ssl_cert_dup(ctx->cert);
311 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
313 s->read_ahead=ctx->read_ahead;
314 s->msg_callback=ctx->msg_callback;
315 s->msg_callback_arg=ctx->msg_callback_arg;
316 s->verify_mode=ctx->verify_mode;
318 s->verify_depth=ctx->verify_depth;
320 s->sid_ctx_length=ctx->sid_ctx_length;
321 OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
322 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
323 s->verify_callback=ctx->default_verify_callback;
324 s->generate_session_id=ctx->generate_session_id;
326 s->param = X509_VERIFY_PARAM_new();
329 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
331 s->purpose = ctx->purpose;
332 s->trust = ctx->trust;
334 s->quiet_shutdown=ctx->quiet_shutdown;
335 s->max_send_fragment = ctx->max_send_fragment;
337 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
339 #ifndef OPENSSL_NO_TLSEXT
340 s->tlsext_debug_cb = 0;
341 s->tlsext_debug_arg = NULL;
342 s->tlsext_ticket_expected = 0;
343 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
347 s->verify_result=X509_V_OK;
349 s->method=ctx->method;
351 if (!s->method->ssl_new(s))
355 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
359 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
361 #ifndef OPENSSL_NO_PSK
362 s->psk_client_callback=ctx->psk_client_callback;
363 s->psk_server_callback=ctx->psk_server_callback;
371 ssl_cert_free(s->cert);
373 SSL_CTX_free(s->ctx); /* decrement reference count */
376 SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
380 int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
381 unsigned int sid_ctx_len)
383 if(sid_ctx_len > sizeof ctx->sid_ctx)
385 SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
388 ctx->sid_ctx_length=sid_ctx_len;
389 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
394 int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
395 unsigned int sid_ctx_len)
397 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
399 SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
402 ssl->sid_ctx_length=sid_ctx_len;
403 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
408 int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
410 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
411 ctx->generate_session_id = cb;
412 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
416 int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
418 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
419 ssl->generate_session_id = cb;
420 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
424 int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
427 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
428 * we can "construct" a session to give us the desired check - ie. to
429 * find if there's a session in the hash table that would conflict with
430 * any new session built out of this id/id_len and the ssl_version in
431 * use by this SSL. */
434 if(id_len > sizeof r.session_id)
437 r.ssl_version = ssl->version;
438 r.session_id_length = id_len;
439 memcpy(r.session_id, id, id_len);
440 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
441 * callback is calling us to check the uniqueness of a shorter ID, it
442 * must be compared as a padded-out ID because that is what it will be
443 * converted to when the callback has finished choosing it. */
444 if((r.ssl_version == SSL2_VERSION) &&
445 (id_len < SSL2_SSL_SESSION_ID_LENGTH))
447 memset(r.session_id + id_len, 0,
448 SSL2_SSL_SESSION_ID_LENGTH - id_len);
449 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
452 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
453 p = (SSL_SESSION *)lh_retrieve(ssl->ctx->sessions, &r);
454 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
458 int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
460 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
463 int SSL_set_purpose(SSL *s, int purpose)
465 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
468 int SSL_CTX_set_trust(SSL_CTX *s, int trust)
470 return X509_VERIFY_PARAM_set_trust(s->param, trust);
473 int SSL_set_trust(SSL *s, int trust)
475 return X509_VERIFY_PARAM_set_trust(s->param, trust);
478 void SSL_free(SSL *s)
485 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
493 fprintf(stderr,"SSL_free, bad reference count\n");
499 X509_VERIFY_PARAM_free(s->param);
501 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
505 /* If the buffering BIO is in place, pop it off */
506 if (s->bbio == s->wbio)
508 s->wbio=BIO_pop(s->wbio);
514 BIO_free_all(s->rbio);
515 if ((s->wbio != NULL) && (s->wbio != s->rbio))
516 BIO_free_all(s->wbio);
518 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
520 /* add extra stuff */
521 if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);
522 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
524 /* Make the next call work :-) */
525 if (s->session != NULL)
527 ssl_clear_bad_session(s);
528 SSL_SESSION_free(s->session);
531 ssl_clear_cipher_ctx(s);
532 ssl_clear_hash_ctx(&s->read_hash);
533 ssl_clear_hash_ctx(&s->write_hash);
535 if (s->cert != NULL) ssl_cert_free(s->cert);
536 /* Free up if allocated */
538 if (s->ctx) SSL_CTX_free(s->ctx);
539 #ifndef OPENSSL_NO_TLSEXT
540 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
541 #ifndef OPENSSL_NO_EC
542 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
543 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
544 #endif /* OPENSSL_NO_EC */
545 if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input);
548 if (s->client_CA != NULL)
549 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
551 if (s->method != NULL) s->method->ssl_free(s);
553 #ifndef OPENSSL_NO_KRB5
554 if (s->kssl_ctx != NULL)
555 kssl_ctx_free(s->kssl_ctx);
556 #endif /* OPENSSL_NO_KRB5 */
561 void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
563 /* If the output buffering BIO is still in place, remove it
567 if (s->wbio == s->bbio)
569 s->wbio=s->wbio->next_bio;
570 s->bbio->next_bio=NULL;
573 if ((s->rbio != NULL) && (s->rbio != rbio))
574 BIO_free_all(s->rbio);
575 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
576 BIO_free_all(s->wbio);
581 BIO *SSL_get_rbio(const SSL *s)
584 BIO *SSL_get_wbio(const SSL *s)
587 int SSL_get_fd(const SSL *s)
589 return(SSL_get_rfd(s));
592 int SSL_get_rfd(const SSL *s)
598 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
604 int SSL_get_wfd(const SSL *s)
610 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
616 #ifndef OPENSSL_NO_SOCK
617 int SSL_set_fd(SSL *s,int fd)
622 bio=BIO_new(BIO_s_socket());
626 SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
629 BIO_set_fd(bio,fd,BIO_NOCLOSE);
630 SSL_set_bio(s,bio,bio);
636 int SSL_set_wfd(SSL *s,int fd)
641 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
642 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
644 bio=BIO_new(BIO_s_socket());
647 { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
648 BIO_set_fd(bio,fd,BIO_NOCLOSE);
649 SSL_set_bio(s,SSL_get_rbio(s),bio);
652 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
658 int SSL_set_rfd(SSL *s,int fd)
663 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
664 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
666 bio=BIO_new(BIO_s_socket());
670 SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
673 BIO_set_fd(bio,fd,BIO_NOCLOSE);
674 SSL_set_bio(s,bio,SSL_get_wbio(s));
677 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
685 /* return length of latest Finished message we sent, copy to 'buf' */
686 size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
692 ret = s->s3->tmp.finish_md_len;
695 memcpy(buf, s->s3->tmp.finish_md, count);
700 /* return length of latest Finished message we expected, copy to 'buf' */
701 size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
707 ret = s->s3->tmp.peer_finish_md_len;
710 memcpy(buf, s->s3->tmp.peer_finish_md, count);
716 int SSL_get_verify_mode(const SSL *s)
718 return(s->verify_mode);
721 int SSL_get_verify_depth(const SSL *s)
723 return X509_VERIFY_PARAM_get_depth(s->param);
726 int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
728 return(s->verify_callback);
731 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
733 return(ctx->verify_mode);
736 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
738 return X509_VERIFY_PARAM_get_depth(ctx->param);
741 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
743 return(ctx->default_verify_callback);
746 void SSL_set_verify(SSL *s,int mode,
747 int (*callback)(int ok,X509_STORE_CTX *ctx))
750 if (callback != NULL)
751 s->verify_callback=callback;
754 void SSL_set_verify_depth(SSL *s,int depth)
756 X509_VERIFY_PARAM_set_depth(s->param, depth);
759 void SSL_set_read_ahead(SSL *s,int yes)
764 int SSL_get_read_ahead(const SSL *s)
766 return(s->read_ahead);
769 int SSL_pending(const SSL *s)
771 /* SSL_pending cannot work properly if read-ahead is enabled
772 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
773 * and it is impossible to fix since SSL_pending cannot report
774 * errors that may be observed while scanning the new data.
775 * (Note that SSL_pending() is often used as a boolean value,
776 * so we'd better not return -1.)
778 return(s->method->ssl_pending(s));
781 X509 *SSL_get_peer_certificate(const SSL *s)
785 if ((s == NULL) || (s->session == NULL))
790 if (r == NULL) return(r);
792 CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
797 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
801 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
804 r=s->session->sess_cert->cert_chain;
806 /* If we are a client, cert_chain includes the peer's own
807 * certificate; if we are a server, it does not. */
812 /* Now in theory, since the calling process own 't' it should be safe to
813 * modify. We need to be able to read f without being hassled */
814 void SSL_copy_session_id(SSL *t,const SSL *f)
818 /* Do we need to to SSL locking? */
819 SSL_set_session(t,SSL_get_session(f));
821 /* what if we are setup as SSLv2 but want to talk SSLv3 or
823 if (t->method != f->method)
825 t->method->ssl_free(t); /* cleanup current */
826 t->method=f->method; /* change method */
827 t->method->ssl_new(t); /* setup new */
833 CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
838 if (tmp != NULL) ssl_cert_free(tmp);
839 SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length);
842 /* Fix this so it checks all the valid key/cert options */
843 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
845 if ( (ctx == NULL) ||
846 (ctx->cert == NULL) ||
847 (ctx->cert->key->x509 == NULL))
849 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
852 if (ctx->cert->key->privatekey == NULL)
854 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
857 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
860 /* Fix this function so that it takes an optional type parameter */
861 int SSL_check_private_key(const SSL *ssl)
865 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
868 if (ssl->cert == NULL)
870 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
873 if (ssl->cert->key->x509 == NULL)
875 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
878 if (ssl->cert->key->privatekey == NULL)
880 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
883 return(X509_check_private_key(ssl->cert->key->x509,
884 ssl->cert->key->privatekey));
887 int SSL_accept(SSL *s)
889 if (s->handshake_func == 0)
890 /* Not properly initialized yet */
891 SSL_set_accept_state(s);
893 return(s->method->ssl_accept(s));
896 int SSL_connect(SSL *s)
898 if (s->handshake_func == 0)
899 /* Not properly initialized yet */
900 SSL_set_connect_state(s);
902 return(s->method->ssl_connect(s));
905 long SSL_get_default_timeout(const SSL *s)
907 return(s->method->get_timeout());
910 int SSL_read(SSL *s,void *buf,int num)
912 if (s->handshake_func == 0)
914 SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
918 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
920 s->rwstate=SSL_NOTHING;
923 return(s->method->ssl_read(s,buf,num));
926 int SSL_peek(SSL *s,void *buf,int num)
928 if (s->handshake_func == 0)
930 SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
934 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
938 return(s->method->ssl_peek(s,buf,num));
941 int SSL_write(SSL *s,const void *buf,int num)
943 if (s->handshake_func == 0)
945 SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
949 if (s->shutdown & SSL_SENT_SHUTDOWN)
951 s->rwstate=SSL_NOTHING;
952 SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
955 return(s->method->ssl_write(s,buf,num));
958 int SSL_shutdown(SSL *s)
960 /* Note that this function behaves differently from what one might
961 * expect. Return values are 0 for no success (yet),
962 * 1 for success; but calling it once is usually not enough,
963 * even if blocking I/O is used (see ssl3_shutdown).
966 if (s->handshake_func == 0)
968 SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
972 if ((s != NULL) && !SSL_in_init(s))
973 return(s->method->ssl_shutdown(s));
978 int SSL_renegotiate(SSL *s)
980 if (s->new_session == 0)
984 return(s->method->ssl_renegotiate(s));
987 int SSL_renegotiate_pending(SSL *s)
989 /* becomes true when negotiation is requested;
990 * false again once a handshake has finished */
991 return (s->new_session != 0);
994 long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1000 case SSL_CTRL_GET_READ_AHEAD:
1001 return(s->read_ahead);
1002 case SSL_CTRL_SET_READ_AHEAD:
1007 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1008 s->msg_callback_arg = parg;
1011 case SSL_CTRL_OPTIONS:
1012 return(s->options|=larg);
1014 return(s->mode|=larg);
1015 case SSL_CTRL_GET_MAX_CERT_LIST:
1016 return(s->max_cert_list);
1017 case SSL_CTRL_SET_MAX_CERT_LIST:
1019 s->max_cert_list=larg;
1021 case SSL_CTRL_SET_MTU:
1022 if (SSL_version(s) == DTLS1_VERSION)
1028 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1029 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1031 s->max_send_fragment = larg;
1034 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1038 long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1042 case SSL_CTRL_SET_MSG_CALLBACK:
1043 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1047 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1051 struct lhash_st *SSL_CTX_sessions(SSL_CTX *ctx)
1053 return ctx->sessions;
1056 long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1062 case SSL_CTRL_GET_READ_AHEAD:
1063 return(ctx->read_ahead);
1064 case SSL_CTRL_SET_READ_AHEAD:
1066 ctx->read_ahead=larg;
1069 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1070 ctx->msg_callback_arg = parg;
1073 case SSL_CTRL_GET_MAX_CERT_LIST:
1074 return(ctx->max_cert_list);
1075 case SSL_CTRL_SET_MAX_CERT_LIST:
1076 l=ctx->max_cert_list;
1077 ctx->max_cert_list=larg;
1080 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1081 l=ctx->session_cache_size;
1082 ctx->session_cache_size=larg;
1084 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1085 return(ctx->session_cache_size);
1086 case SSL_CTRL_SET_SESS_CACHE_MODE:
1087 l=ctx->session_cache_mode;
1088 ctx->session_cache_mode=larg;
1090 case SSL_CTRL_GET_SESS_CACHE_MODE:
1091 return(ctx->session_cache_mode);
1093 case SSL_CTRL_SESS_NUMBER:
1094 return(ctx->sessions->num_items);
1095 case SSL_CTRL_SESS_CONNECT:
1096 return(ctx->stats.sess_connect);
1097 case SSL_CTRL_SESS_CONNECT_GOOD:
1098 return(ctx->stats.sess_connect_good);
1099 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1100 return(ctx->stats.sess_connect_renegotiate);
1101 case SSL_CTRL_SESS_ACCEPT:
1102 return(ctx->stats.sess_accept);
1103 case SSL_CTRL_SESS_ACCEPT_GOOD:
1104 return(ctx->stats.sess_accept_good);
1105 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1106 return(ctx->stats.sess_accept_renegotiate);
1107 case SSL_CTRL_SESS_HIT:
1108 return(ctx->stats.sess_hit);
1109 case SSL_CTRL_SESS_CB_HIT:
1110 return(ctx->stats.sess_cb_hit);
1111 case SSL_CTRL_SESS_MISSES:
1112 return(ctx->stats.sess_miss);
1113 case SSL_CTRL_SESS_TIMEOUTS:
1114 return(ctx->stats.sess_timeout);
1115 case SSL_CTRL_SESS_CACHE_FULL:
1116 return(ctx->stats.sess_cache_full);
1117 case SSL_CTRL_OPTIONS:
1118 return(ctx->options|=larg);
1120 return(ctx->mode|=larg);
1121 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1122 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1124 ctx->max_send_fragment = larg;
1127 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1131 long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1135 case SSL_CTRL_SET_MSG_CALLBACK:
1136 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1140 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1144 int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
1152 return((l > 0)?1:-1);
1155 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap,
1156 const SSL_CIPHER * const *bp)
1160 l=(*ap)->id-(*bp)->id;
1164 return((l > 0)?1:-1);
1167 /** return a STACK of the ciphers available for the SSL and in order of
1169 STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1173 if (s->cipher_list != NULL)
1175 return(s->cipher_list);
1177 else if ((s->ctx != NULL) &&
1178 (s->ctx->cipher_list != NULL))
1180 return(s->ctx->cipher_list);
1186 /** return a STACK of the ciphers available for the SSL and in order of
1188 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1192 if (s->cipher_list_by_id != NULL)
1194 return(s->cipher_list_by_id);
1196 else if ((s->ctx != NULL) &&
1197 (s->ctx->cipher_list_by_id != NULL))
1199 return(s->ctx->cipher_list_by_id);
1205 /** The old interface to get the same thing as SSL_get_ciphers() */
1206 const char *SSL_get_cipher_list(const SSL *s,int n)
1209 STACK_OF(SSL_CIPHER) *sk;
1211 if (s == NULL) return(NULL);
1212 sk=SSL_get_ciphers(s);
1213 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1215 c=sk_SSL_CIPHER_value(sk,n);
1216 if (c == NULL) return(NULL);
1220 /** specify the ciphers to be used by default by the SSL_CTX */
1221 int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1223 STACK_OF(SSL_CIPHER) *sk;
1225 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1226 &ctx->cipher_list_by_id,str);
1227 /* ssl_create_cipher_list may return an empty stack if it
1228 * was unable to find a cipher matching the given rule string
1229 * (for example if the rule string specifies a cipher which
1230 * has been disabled). This is not an error as far as
1231 * ssl_create_cipher_list is concerned, and hence
1232 * ctx->cipher_list and ctx->cipher_list_by_id has been
1236 else if (sk_SSL_CIPHER_num(sk) == 0)
1238 SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1244 /** specify the ciphers to be used by the SSL */
1245 int SSL_set_cipher_list(SSL *s,const char *str)
1247 STACK_OF(SSL_CIPHER) *sk;
1249 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1250 &s->cipher_list_by_id,str);
1251 /* see comment in SSL_CTX_set_cipher_list */
1254 else if (sk_SSL_CIPHER_num(sk) == 0)
1256 SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1262 /* works well for SSLv2, not so good for SSLv3 */
1263 char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1267 STACK_OF(SSL_CIPHER) *sk;
1271 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1276 sk=s->session->ciphers;
1277 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1279 /* Decrement for either the ':' or a '\0' */
1281 c=sk_SSL_CIPHER_value(sk,i);
1282 for (cp=c->name; *cp; )
1298 int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
1299 int (*put_cb)(const SSL_CIPHER *, unsigned char *))
1304 #ifndef OPENSSL_NO_KRB5
1305 int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx);
1306 #endif /* OPENSSL_NO_KRB5 */
1308 if (sk == NULL) return(0);
1311 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1313 c=sk_SSL_CIPHER_value(sk,i);
1314 #ifndef OPENSSL_NO_KRB5
1315 if (((c->algorithm_mkey & SSL_kKRB5) || (c->algorithm_auth & SSL_aKRB5)) &&
1318 #endif /* OPENSSL_NO_KRB5 */
1319 #ifndef OPENSSL_NO_PSK
1320 /* with PSK there must be client callback set */
1321 if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK)) &&
1322 s->psk_client_callback == NULL)
1324 #endif /* OPENSSL_NO_PSK */
1325 j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
1331 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
1332 STACK_OF(SSL_CIPHER) **skp)
1335 STACK_OF(SSL_CIPHER) *sk;
1338 n=ssl_put_cipher_by_char(s,NULL,NULL);
1341 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1344 if ((skp == NULL) || (*skp == NULL))
1345 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1349 sk_SSL_CIPHER_zero(sk);
1352 for (i=0; i<num; i+=n)
1354 c=ssl_get_cipher_by_char(s,p);
1358 if (!sk_SSL_CIPHER_push(sk,c))
1360 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
1370 if ((skp == NULL) || (*skp == NULL))
1371 sk_SSL_CIPHER_free(sk);
1376 #ifndef OPENSSL_NO_TLSEXT
1377 /** return a servername extension value if provided in Client Hello, or NULL.
1378 * So far, only host_name types are defined (RFC 3546).
1381 const char *SSL_get_servername(const SSL *s, const int type)
1383 if (type != TLSEXT_NAMETYPE_host_name)
1386 return s->session && !s->tlsext_hostname ?
1387 s->session->tlsext_hostname :
1391 int SSL_get_servername_type(const SSL *s)
1393 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1394 return TLSEXT_NAMETYPE_host_name;
1399 unsigned long SSL_SESSION_hash(const SSL_SESSION *a)
1404 ((unsigned int) a->session_id[0] )|
1405 ((unsigned int) a->session_id[1]<< 8L)|
1406 ((unsigned long)a->session_id[2]<<16L)|
1407 ((unsigned long)a->session_id[3]<<24L);
1411 /* NB: If this function (or indeed the hash function which uses a sort of
1412 * coarser function than this one) is changed, ensure
1413 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1414 * able to construct an SSL_SESSION that will collide with any existing session
1415 * with a matching session ID. */
1416 int SSL_SESSION_cmp(const SSL_SESSION *a,const SSL_SESSION *b)
1418 if (a->ssl_version != b->ssl_version)
1420 if (a->session_id_length != b->session_id_length)
1422 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1425 /* These wrapper functions should remain rather than redeclaring
1426 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1427 * variable. The reason is that the functions aren't static, they're exposed via
1429 static IMPLEMENT_LHASH_HASH_FN(SSL_SESSION_hash, SSL_SESSION *)
1430 static IMPLEMENT_LHASH_COMP_FN(SSL_SESSION_cmp, SSL_SESSION *)
1432 SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1438 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
1442 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1444 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1447 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1451 memset(ret,0,sizeof(SSL_CTX));
1455 ret->cert_store=NULL;
1456 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1457 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1458 ret->session_cache_head=NULL;
1459 ret->session_cache_tail=NULL;
1461 /* We take the system default */
1462 ret->session_timeout=meth->get_timeout();
1464 ret->new_session_cb=0;
1465 ret->remove_session_cb=0;
1466 ret->get_session_cb=0;
1467 ret->generate_session_id=0;
1469 memset((char *)&ret->stats,0,sizeof(ret->stats));
1472 ret->quiet_shutdown=0;
1474 /* ret->cipher=NULL;*/
1475 /* ret->s2->challenge=NULL;
1476 ret->master_key=NULL;
1478 ret->s2->conn_id=NULL; */
1480 ret->info_callback=NULL;
1482 ret->app_verify_callback=0;
1483 ret->app_verify_arg=NULL;
1485 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1487 ret->msg_callback=0;
1488 ret->msg_callback_arg=NULL;
1489 ret->verify_mode=SSL_VERIFY_NONE;
1491 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1493 ret->sid_ctx_length=0;
1494 ret->default_verify_callback=NULL;
1495 if ((ret->cert=ssl_cert_new()) == NULL)
1498 ret->default_passwd_callback=0;
1499 ret->default_passwd_callback_userdata=NULL;
1500 ret->client_cert_cb=0;
1501 ret->app_gen_cookie_cb=0;
1502 ret->app_verify_cookie_cb=0;
1504 ret->sessions=lh_new(LHASH_HASH_FN(SSL_SESSION_hash),
1505 LHASH_COMP_FN(SSL_SESSION_cmp));
1506 if (ret->sessions == NULL) goto err;
1507 ret->cert_store=X509_STORE_new();
1508 if (ret->cert_store == NULL) goto err;
1510 ssl_create_cipher_list(ret->method,
1511 &ret->cipher_list,&ret->cipher_list_by_id,
1512 SSL_DEFAULT_CIPHER_LIST);
1513 if (ret->cipher_list == NULL
1514 || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
1516 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
1520 ret->param = X509_VERIFY_PARAM_new();
1524 if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
1526 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
1529 if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
1531 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
1534 if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
1536 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
1540 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1543 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1545 ret->extra_certs=NULL;
1546 ret->comp_methods=SSL_COMP_get_compression_methods();
1548 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1550 #ifndef OPENSSL_NO_TLSEXT
1551 ret->tlsext_servername_callback = 0;
1552 ret->tlsext_servername_arg = NULL;
1553 /* Setup RFC4507 ticket keys */
1554 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1555 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1556 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1557 ret->options |= SSL_OP_NO_TICKET;
1560 #ifndef OPENSSL_NO_PSK
1561 ret->psk_identity_hint=NULL;
1562 ret->psk_client_callback=NULL;
1563 ret->psk_server_callback=NULL;
1567 SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
1569 if (ret != NULL) SSL_CTX_free(ret);
1574 static void SSL_COMP_free(SSL_COMP *comp)
1575 { OPENSSL_free(comp); }
1578 void SSL_CTX_free(SSL_CTX *a)
1582 if (a == NULL) return;
1584 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1586 REF_PRINT("SSL_CTX",a);
1592 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1598 X509_VERIFY_PARAM_free(a->param);
1601 * Free internal session cache. However: the remove_cb() may reference
1602 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1603 * after the sessions were flushed.
1604 * As the ex_data handling routines might also touch the session cache,
1605 * the most secure solution seems to be: empty (flush) the cache, then
1606 * free ex_data, then finally free the cache.
1607 * (See ticket [openssl.org #212].)
1609 if (a->sessions != NULL)
1610 SSL_CTX_flush_sessions(a,0);
1612 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
1614 if (a->sessions != NULL)
1615 lh_free(a->sessions);
1617 if (a->cert_store != NULL)
1618 X509_STORE_free(a->cert_store);
1619 if (a->cipher_list != NULL)
1620 sk_SSL_CIPHER_free(a->cipher_list);
1621 if (a->cipher_list_by_id != NULL)
1622 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1623 if (a->cert != NULL)
1624 ssl_cert_free(a->cert);
1625 if (a->client_CA != NULL)
1626 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
1627 if (a->extra_certs != NULL)
1628 sk_X509_pop_free(a->extra_certs,X509_free);
1629 #if 0 /* This should never be done, since it removes a global database */
1630 if (a->comp_methods != NULL)
1631 sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
1633 a->comp_methods = NULL;
1636 #ifndef OPENSSL_NO_PSK
1637 if (a->psk_identity_hint)
1638 OPENSSL_free(a->psk_identity_hint);
1643 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
1645 ctx->default_passwd_callback=cb;
1648 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
1650 ctx->default_passwd_callback_userdata=u;
1653 void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
1655 ctx->app_verify_callback=cb;
1656 ctx->app_verify_arg=arg;
1659 void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
1661 ctx->verify_mode=mode;
1662 ctx->default_verify_callback=cb;
1665 void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
1667 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1670 void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
1673 int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1674 int rsa_enc_export,dh_rsa_export,dh_dsa_export;
1675 int rsa_tmp_export,dh_tmp_export,kl;
1676 unsigned long mask_k,mask_a,emask_k,emask_a;
1677 int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size;
1678 #ifndef OPENSSL_NO_ECDH
1682 EVP_PKEY *ecc_pkey = NULL;
1683 int signature_nid = 0;
1685 if (c == NULL) return;
1687 kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
1689 #ifndef OPENSSL_NO_RSA
1690 rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
1691 rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
1692 (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
1694 rsa_tmp=rsa_tmp_export=0;
1696 #ifndef OPENSSL_NO_DH
1697 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
1698 dh_tmp_export=(c->dh_tmp_cb != NULL ||
1699 (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
1701 dh_tmp=dh_tmp_export=0;
1704 #ifndef OPENSSL_NO_ECDH
1705 have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL);
1707 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
1708 rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
1709 rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1710 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
1711 rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1712 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
1713 dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1714 cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
1715 dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
1716 dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1717 cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1718 /* FIX THIS EAY EAY EAY */
1719 dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
1720 dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1721 cpk= &(c->pkeys[SSL_PKEY_ECC]);
1722 have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL);
1729 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1730 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
1731 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1734 if (rsa_enc || (rsa_tmp && rsa_sign))
1736 if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
1740 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1741 if ( (dh_tmp || dh_rsa || dh_dsa) &&
1742 (rsa_enc || rsa_sign || dsa_sign))
1744 if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1745 (rsa_enc || rsa_sign || dsa_sign))
1755 if (dh_rsa) mask_k|=SSL_kDHr;
1756 if (dh_rsa_export) emask_k|=SSL_kDHr;
1758 if (dh_dsa) mask_k|=SSL_kDHd;
1759 if (dh_dsa_export) emask_k|=SSL_kDHd;
1761 if (rsa_enc || rsa_sign)
1776 #ifndef OPENSSL_NO_KRB5
1783 /* An ECC certificate may be usable for ECDH and/or
1784 * ECDSA cipher suites depending on the key usage extension.
1788 /* This call populates extension flags (ex_flags) */
1789 x = (c->pkeys[SSL_PKEY_ECC]).x509;
1790 X509_check_purpose(x, -1, 0);
1791 ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
1792 (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
1793 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
1794 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
1795 ecc_pkey = X509_get_pubkey(x);
1796 ecc_pkey_size = (ecc_pkey != NULL) ?
1797 EVP_PKEY_bits(ecc_pkey) : 0;
1798 EVP_PKEY_free(ecc_pkey);
1799 if ((x->sig_alg) && (x->sig_alg->algorithm))
1800 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1801 #ifndef OPENSSL_NO_ECDH
1804 const char *sig = OBJ_nid2ln(signature_nid);
1811 if (strstr(sig, "WithRSA"))
1815 if (ecc_pkey_size <= 163)
1817 emask_k|=SSL_kECDHr;
1822 if (signature_nid == NID_ecdsa_with_SHA1)
1826 if (ecc_pkey_size <= 163)
1828 emask_k|=SSL_kECDHe;
1834 #ifndef OPENSSL_NO_ECDSA
1838 emask_a|=SSL_aECDSA;
1843 #ifndef OPENSSL_NO_ECDH
1847 emask_k|=SSL_kEECDH;
1851 #ifndef OPENSSL_NO_PSK
1854 emask_k |= SSL_kPSK;
1855 emask_a |= SSL_aPSK;
1860 c->export_mask_k=emask_k;
1861 c->export_mask_a=emask_a;
1865 /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1866 #define ku_reject(x, usage) \
1867 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
1869 int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs)
1871 unsigned long alg_k, alg_a;
1872 EVP_PKEY *pkey = NULL;
1874 int signature_nid = 0;
1876 alg_k = cs->algorithm_mkey;
1877 alg_a = cs->algorithm_auth;
1879 if (SSL_C_IS_EXPORT(cs))
1881 /* ECDH key length in export ciphers must be <= 163 bits */
1882 pkey = X509_get_pubkey(x);
1883 if (pkey == NULL) return 0;
1884 keysize = EVP_PKEY_bits(pkey);
1885 EVP_PKEY_free(pkey);
1886 if (keysize > 163) return 0;
1889 /* This call populates the ex_flags field correctly */
1890 X509_check_purpose(x, -1, 0);
1891 if ((x->sig_alg) && (x->sig_alg->algorithm))
1892 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1893 if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr)
1895 /* key usage, if present, must allow key agreement */
1896 if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
1898 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
1901 if (alg_k & SSL_kECDHe)
1903 /* signature alg must be ECDSA */
1904 if (signature_nid != NID_ecdsa_with_SHA1)
1906 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
1910 if (alg_k & SSL_kECDHr)
1912 /* signature alg must be RSA */
1914 const char *sig = OBJ_nid2ln(signature_nid);
1920 if (strstr(sig, "WithRSA") == NULL)
1922 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
1927 if (alg_a & SSL_aECDSA)
1929 /* key usage, if present, must allow signing */
1930 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
1932 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
1937 return 1; /* all checks are ok */
1940 /* THIS NEEDS CLEANING UP */
1941 X509 *ssl_get_server_send_cert(SSL *s)
1943 unsigned long alg_k,alg_a,mask_k,mask_a;
1948 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
1949 is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
1952 mask_k = c->export_mask_k;
1953 mask_a = c->export_mask_a;
1961 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1962 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1964 if (alg_k & (SSL_kECDHr|SSL_kECDHe))
1966 /* we don't need to look at SSL_kEECDH
1967 * since no certificate is needed for
1968 * anon ECDH and for authenticated
1969 * EECDH, the check for the auth
1970 * algorithm will set i correctly
1971 * NOTE: For ECDH-RSA, we need an ECC
1972 * not an RSA cert but for EECDH-RSA
1973 * we need an RSA cert. Placing the
1974 * checks for SSL_kECDH before RSA
1975 * checks ensures the correct cert is chosen.
1979 else if (alg_a & SSL_aECDSA)
1983 else if (alg_k & SSL_kDHr)
1985 else if (alg_k & SSL_kDHd)
1987 else if (alg_a & SSL_aDSS)
1988 i=SSL_PKEY_DSA_SIGN;
1989 else if (alg_a & SSL_aRSA)
1991 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1992 i=SSL_PKEY_RSA_SIGN;
1996 else if (alg_a & SSL_aKRB5)
1998 /* VRS something else here? */
2001 else /* if (alg_a & SSL_aNULL) */
2003 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR);
2006 if (c->pkeys[i].x509 == NULL) return(NULL);
2008 return(c->pkeys[i].x509);
2011 EVP_PKEY *ssl_get_sign_pkey(SSL *s,SSL_CIPHER *cipher)
2013 unsigned long alg_a;
2016 alg_a = cipher->algorithm_auth;
2019 if ((alg_a & SSL_aDSS) &&
2020 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
2021 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
2022 else if (alg_a & SSL_aRSA)
2024 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2025 return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
2026 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2027 return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
2031 else if ((alg_a & SSL_aECDSA) &&
2032 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2033 return(c->pkeys[SSL_PKEY_ECC].privatekey);
2034 else /* if (alg_a & SSL_aNULL) */
2036 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR);
2041 void ssl_update_cache(SSL *s,int mode)
2045 /* If the session_id_length is 0, we are not supposed to cache it,
2046 * and it would be rather hard to do anyway :-) */
2047 if (s->session->session_id_length == 0) return;
2049 i=s->session_ctx->session_cache_mode;
2050 if ((i & mode) && (!s->hit)
2051 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2052 || SSL_CTX_add_session(s->session_ctx,s->session))
2053 && (s->session_ctx->new_session_cb != NULL))
2055 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
2056 if (!s->session_ctx->new_session_cb(s,s->session))
2057 SSL_SESSION_free(s->session);
2060 /* auto flush every 255 connections */
2061 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2062 ((i & mode) == mode))
2064 if ( (((mode & SSL_SESS_CACHE_CLIENT)
2065 ?s->session_ctx->stats.sess_connect_good
2066 :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
2068 SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
2073 const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2078 int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2083 if (s->method != meth)
2085 if (s->handshake_func != NULL)
2086 conn=(s->handshake_func == s->method->ssl_connect);
2088 if (s->method->version == meth->version)
2092 s->method->ssl_free(s);
2094 ret=s->method->ssl_new(s);
2098 s->handshake_func=meth->ssl_connect;
2100 s->handshake_func=meth->ssl_accept;
2105 int SSL_get_error(const SSL *s,int i)
2111 if (i > 0) return(SSL_ERROR_NONE);
2113 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2114 * etc, where we do encode the error */
2115 if ((l=ERR_peek_error()) != 0)
2117 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2118 return(SSL_ERROR_SYSCALL);
2120 return(SSL_ERROR_SSL);
2123 if ((i < 0) && SSL_want_read(s))
2125 bio=SSL_get_rbio(s);
2126 if (BIO_should_read(bio))
2127 return(SSL_ERROR_WANT_READ);
2128 else if (BIO_should_write(bio))
2129 /* This one doesn't make too much sense ... We never try
2130 * to write to the rbio, and an application program where
2131 * rbio and wbio are separate couldn't even know what it
2133 * However if we ever set s->rwstate incorrectly
2134 * (so that we have SSL_want_read(s) instead of
2135 * SSL_want_write(s)) and rbio and wbio *are* the same,
2136 * this test works around that bug; so it might be safer
2138 return(SSL_ERROR_WANT_WRITE);
2139 else if (BIO_should_io_special(bio))
2141 reason=BIO_get_retry_reason(bio);
2142 if (reason == BIO_RR_CONNECT)
2143 return(SSL_ERROR_WANT_CONNECT);
2144 else if (reason == BIO_RR_ACCEPT)
2145 return(SSL_ERROR_WANT_ACCEPT);
2147 return(SSL_ERROR_SYSCALL); /* unknown */
2151 if ((i < 0) && SSL_want_write(s))
2153 bio=SSL_get_wbio(s);
2154 if (BIO_should_write(bio))
2155 return(SSL_ERROR_WANT_WRITE);
2156 else if (BIO_should_read(bio))
2157 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2158 return(SSL_ERROR_WANT_READ);
2159 else if (BIO_should_io_special(bio))
2161 reason=BIO_get_retry_reason(bio);
2162 if (reason == BIO_RR_CONNECT)
2163 return(SSL_ERROR_WANT_CONNECT);
2164 else if (reason == BIO_RR_ACCEPT)
2165 return(SSL_ERROR_WANT_ACCEPT);
2167 return(SSL_ERROR_SYSCALL);
2170 if ((i < 0) && SSL_want_x509_lookup(s))
2172 return(SSL_ERROR_WANT_X509_LOOKUP);
2177 if (s->version == SSL2_VERSION)
2179 /* assume it is the socket being closed */
2180 return(SSL_ERROR_ZERO_RETURN);
2184 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2185 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2186 return(SSL_ERROR_ZERO_RETURN);
2189 return(SSL_ERROR_SYSCALL);
2192 int SSL_do_handshake(SSL *s)
2196 if (s->handshake_func == NULL)
2198 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
2202 s->method->ssl_renegotiate_check(s);
2204 if (SSL_in_init(s) || SSL_in_before(s))
2206 ret=s->handshake_func(s);
2211 /* For the next 2 functions, SSL_clear() sets shutdown and so
2212 * one of these calls will reset it */
2213 void SSL_set_accept_state(SSL *s)
2217 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2218 s->handshake_func=s->method->ssl_accept;
2219 /* clear the current cipher */
2220 ssl_clear_cipher_ctx(s);
2221 ssl_clear_hash_ctx(&s->read_hash);
2222 ssl_clear_hash_ctx(&s->write_hash);
2225 void SSL_set_connect_state(SSL *s)
2229 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2230 s->handshake_func=s->method->ssl_connect;
2231 /* clear the current cipher */
2232 ssl_clear_cipher_ctx(s);
2233 ssl_clear_hash_ctx(&s->read_hash);
2234 ssl_clear_hash_ctx(&s->write_hash);
2237 int ssl_undefined_function(SSL *s)
2239 SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2243 int ssl_undefined_void_function(void)
2245 SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2249 int ssl_undefined_const_function(const SSL *s)
2251 SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2255 SSL_METHOD *ssl_bad_method(int ver)
2257 SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2261 const char *SSL_get_version(const SSL *s)
2263 if (s->version == TLS1_VERSION)
2265 else if (s->version == SSL3_VERSION)
2267 else if (s->version == SSL2_VERSION)
2273 SSL *SSL_dup(SSL *s)
2275 STACK_OF(X509_NAME) *sk;
2280 if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2283 ret->version = s->version;
2284 ret->type = s->type;
2285 ret->method = s->method;
2287 if (s->session != NULL)
2289 /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
2290 SSL_copy_session_id(ret,s);
2294 /* No session has been established yet, so we have to expect
2295 * that s->cert or ret->cert will be changed later --
2296 * they should not both point to the same object,
2297 * and thus we can't use SSL_copy_session_id. */
2299 ret->method->ssl_free(ret);
2300 ret->method = s->method;
2301 ret->method->ssl_new(ret);
2303 if (s->cert != NULL)
2305 if (ret->cert != NULL)
2307 ssl_cert_free(ret->cert);
2309 ret->cert = ssl_cert_dup(s->cert);
2310 if (ret->cert == NULL)
2314 SSL_set_session_id_context(ret,
2315 s->sid_ctx, s->sid_ctx_length);
2318 ret->options=s->options;
2320 SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s));
2321 SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
2322 ret->msg_callback = s->msg_callback;
2323 ret->msg_callback_arg = s->msg_callback_arg;
2324 SSL_set_verify(ret,SSL_get_verify_mode(s),
2325 SSL_get_verify_callback(s));
2326 SSL_set_verify_depth(ret,SSL_get_verify_depth(s));
2327 ret->generate_session_id = s->generate_session_id;
2329 SSL_set_info_callback(ret,SSL_get_info_callback(s));
2331 ret->debug=s->debug;
2333 /* copy app data, a little dangerous perhaps */
2334 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
2337 /* setup rbio, and wbio */
2338 if (s->rbio != NULL)
2340 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
2343 if (s->wbio != NULL)
2345 if (s->wbio != s->rbio)
2347 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
2351 ret->wbio=ret->rbio;
2353 ret->rwstate = s->rwstate;
2354 ret->in_handshake = s->in_handshake;
2355 ret->handshake_func = s->handshake_func;
2356 ret->server = s->server;
2357 ret->new_session = s->new_session;
2358 ret->quiet_shutdown = s->quiet_shutdown;
2359 ret->shutdown=s->shutdown;
2360 ret->state=s->state; /* SSL_dup does not really work at any state, though */
2361 ret->rstate=s->rstate;
2362 ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
2365 X509_VERIFY_PARAM_inherit(ret->param, s->param);
2367 /* dup the cipher_list and cipher_list_by_id stacks */
2368 if (s->cipher_list != NULL)
2370 if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2373 if (s->cipher_list_by_id != NULL)
2374 if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id))
2378 /* Dup the client_CA list */
2379 if (s->client_CA != NULL)
2381 if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
2383 for (i=0; i<sk_X509_NAME_num(sk); i++)
2385 xn=sk_X509_NAME_value(sk,i);
2386 if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL)
2397 if (ret != NULL) SSL_free(ret);
2403 void ssl_clear_cipher_ctx(SSL *s)
2405 if (s->enc_read_ctx != NULL)
2407 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2408 OPENSSL_free(s->enc_read_ctx);
2409 s->enc_read_ctx=NULL;
2411 if (s->enc_write_ctx != NULL)
2413 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2414 OPENSSL_free(s->enc_write_ctx);
2415 s->enc_write_ctx=NULL;
2417 #ifndef OPENSSL_NO_COMP
2418 if (s->expand != NULL)
2420 COMP_CTX_free(s->expand);
2423 if (s->compress != NULL)
2425 COMP_CTX_free(s->compress);
2431 /* Fix this function so that it takes an optional type parameter */
2432 X509 *SSL_get_certificate(const SSL *s)
2434 if (s->cert != NULL)
2435 return(s->cert->key->x509);
2440 /* Fix this function so that it takes an optional type parameter */
2441 EVP_PKEY *SSL_get_privatekey(SSL *s)
2443 if (s->cert != NULL)
2444 return(s->cert->key->privatekey);
2449 SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2451 if ((s->session != NULL) && (s->session->cipher != NULL))
2452 return(s->session->cipher);
2455 #ifdef OPENSSL_NO_COMP
2456 const void *SSL_get_current_compression(SSL *s)
2460 const void *SSL_get_current_expansion(SSL *s)
2466 const COMP_METHOD *SSL_get_current_compression(SSL *s)
2468 if (s->compress != NULL)
2469 return(s->compress->meth);
2473 const COMP_METHOD *SSL_get_current_expansion(SSL *s)
2475 if (s->expand != NULL)
2476 return(s->expand->meth);
2481 int ssl_init_wbio_buffer(SSL *s,int push)
2485 if (s->bbio == NULL)
2487 bbio=BIO_new(BIO_f_buffer());
2488 if (bbio == NULL) return(0);
2494 if (s->bbio == s->wbio)
2495 s->wbio=BIO_pop(s->wbio);
2497 (void)BIO_reset(bbio);
2498 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2499 if (!BIO_set_read_buffer_size(bbio,1))
2501 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
2506 if (s->wbio != bbio)
2507 s->wbio=BIO_push(bbio,s->wbio);
2511 if (s->wbio == bbio)
2512 s->wbio=BIO_pop(bbio);
2517 void ssl_free_wbio_buffer(SSL *s)
2519 if (s->bbio == NULL) return;
2521 if (s->bbio == s->wbio)
2523 /* remove buffering */
2524 s->wbio=BIO_pop(s->wbio);
2525 #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2526 assert(s->wbio != NULL);
2533 void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2535 ctx->quiet_shutdown=mode;
2538 int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2540 return(ctx->quiet_shutdown);
2543 void SSL_set_quiet_shutdown(SSL *s,int mode)
2545 s->quiet_shutdown=mode;
2548 int SSL_get_quiet_shutdown(const SSL *s)
2550 return(s->quiet_shutdown);
2553 void SSL_set_shutdown(SSL *s,int mode)
2558 int SSL_get_shutdown(const SSL *s)
2560 return(s->shutdown);
2563 int SSL_version(const SSL *s)
2568 SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2573 SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2575 if (ssl->ctx == ctx)
2577 #ifndef OPENSSL_NO_TLSEXT
2579 ctx = ssl->initial_ctx;
2581 if (ssl->cert != NULL)
2582 ssl_cert_free(ssl->cert);
2583 ssl->cert = ssl_cert_dup(ctx->cert);
2584 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2585 if (ssl->ctx != NULL)
2586 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2591 #ifndef OPENSSL_NO_STDIO
2592 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2594 return(X509_STORE_set_default_paths(ctx->cert_store));
2597 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2600 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2604 void SSL_set_info_callback(SSL *ssl,
2605 void (*cb)(const SSL *ssl,int type,int val))
2607 ssl->info_callback=cb;
2610 /* One compiler (Diab DCC) doesn't like argument names in returned
2611 function pointer. */
2612 void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2614 return ssl->info_callback;
2617 int SSL_state(const SSL *ssl)
2622 void SSL_set_verify_result(SSL *ssl,long arg)
2624 ssl->verify_result=arg;
2627 long SSL_get_verify_result(const SSL *ssl)
2629 return(ssl->verify_result);
2632 int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2633 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2635 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2636 new_func, dup_func, free_func);
2639 int SSL_set_ex_data(SSL *s,int idx,void *arg)
2641 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2644 void *SSL_get_ex_data(const SSL *s,int idx)
2646 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2649 int SSL_CTX_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_CTX, argl, argp,
2653 new_func, dup_func, free_func);
2656 int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2658 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2661 void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2663 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2671 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2673 return(ctx->cert_store);
2676 void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2678 if (ctx->cert_store != NULL)
2679 X509_STORE_free(ctx->cert_store);
2680 ctx->cert_store=store;
2683 int SSL_want(const SSL *s)
2689 * \brief Set the callback for generating temporary RSA keys.
2690 * \param ctx the SSL context.
2691 * \param cb the callback
2694 #ifndef OPENSSL_NO_RSA
2695 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2699 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2702 void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2706 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2712 * \brief The RSA temporary key callback function.
2713 * \param ssl the SSL session.
2714 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2715 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2716 * of the required key in bits.
2717 * \return the temporary RSA key.
2718 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2721 RSA *cb(SSL *ssl,int is_export,int keylength)
2726 * \brief Set the callback for generating temporary DH keys.
2727 * \param ctx the SSL context.
2728 * \param dh the callback
2731 #ifndef OPENSSL_NO_DH
2732 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2735 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2738 void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2741 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2745 #ifndef OPENSSL_NO_ECDH
2746 void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2749 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2752 void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2755 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2759 #ifndef OPENSSL_NO_PSK
2760 int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2762 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2764 SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
2767 if (ctx->psk_identity_hint != NULL)
2768 OPENSSL_free(ctx->psk_identity_hint);
2769 if (identity_hint != NULL)
2771 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2772 if (ctx->psk_identity_hint == NULL)
2776 ctx->psk_identity_hint = NULL;
2780 int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2785 if (s->session == NULL)
2786 return 1; /* session not created yet, ignored */
2788 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2790 SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
2793 if (s->session->psk_identity_hint != NULL)
2794 OPENSSL_free(s->session->psk_identity_hint);
2795 if (identity_hint != NULL)
2797 s->session->psk_identity_hint = BUF_strdup(identity_hint);
2798 if (s->session->psk_identity_hint == NULL)
2802 s->session->psk_identity_hint = NULL;
2806 const char *SSL_get_psk_identity_hint(const SSL *s)
2808 if (s == NULL || s->session == NULL)
2810 return(s->session->psk_identity_hint);
2813 const char *SSL_get_psk_identity(const SSL *s)
2815 if (s == NULL || s->session == NULL)
2817 return(s->session->psk_identity);
2820 void SSL_set_psk_client_callback(SSL *s,
2821 unsigned int (*cb)(SSL *ssl, const char *hint,
2822 char *identity, unsigned int max_identity_len, unsigned char *psk,
2823 unsigned int max_psk_len))
2825 s->psk_client_callback = cb;
2828 void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2829 unsigned int (*cb)(SSL *ssl, const char *hint,
2830 char *identity, unsigned int max_identity_len, unsigned char *psk,
2831 unsigned int max_psk_len))
2833 ctx->psk_client_callback = cb;
2836 void SSL_set_psk_server_callback(SSL *s,
2837 unsigned int (*cb)(SSL *ssl, const char *identity,
2838 unsigned char *psk, unsigned int max_psk_len))
2840 s->psk_server_callback = cb;
2843 void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2844 unsigned int (*cb)(SSL *ssl, const char *identity,
2845 unsigned char *psk, unsigned int max_psk_len))
2847 ctx->psk_server_callback = cb;
2851 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))
2853 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2855 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))
2857 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2860 /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
2861 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
2862 * any. If EVP_MD pointer is passed, initializes ctx with this md
2863 * Returns newly allocated ctx;
2866 EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
2868 ssl_clear_hash_ctx(hash);
2869 *hash = EVP_MD_CTX_create();
2870 if (md) EVP_DigestInit_ex(*hash,md,NULL);
2873 void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
2876 if (*hash) EVP_MD_CTX_destroy(*hash);
2880 #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
2881 #include "../crypto/bio/bss_file.c"
2884 IMPLEMENT_STACK_OF(SSL_CIPHER)
2885 IMPLEMENT_STACK_OF(SSL_COMP)