2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
64 char *SSL_version_str="OpenSSL 0.9.2 31-Dec-1998";
66 static STACK *ssl_meth=NULL;
67 static STACK *ssl_ctx_meth=NULL;
68 static int ssl_meth_num=0;
69 static int ssl_ctx_meth_num=0;
71 SSL3_ENC_METHOD ssl3_undef_enc_method={
72 ssl_undefined_function,
73 ssl_undefined_function,
74 ssl_undefined_function,
75 ssl_undefined_function,
76 ssl_undefined_function,
77 ssl_undefined_function,
85 if (s->method == NULL)
87 SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
96 /* This is set if we are doing dynamic renegotiation so keep
97 * the old cipher. It is sort of a SSL_clear_lite :-) */
98 if (s->new_session) return(1);
101 state=s->state; /* Keep to check if we throw away the session-id */
104 s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
106 s->version=s->method->version;
107 s->client_version=s->version;
108 s->rwstate=SSL_NOTHING;
109 s->rstate=SSL_ST_READ_HEADER;
110 s->read_ahead=s->ctx->read_ahead;
112 if (s->init_buf != NULL)
114 BUF_MEM_free(s->init_buf);
118 ssl_clear_cipher_ctx(s);
120 if (ssl_clear_bad_session(s))
122 SSL_SESSION_free(s->session);
129 /* Check to see if we were changed into a different method, if
130 * so, revert back if we are not doing session-id reuse. */
131 if ((s->session == NULL) && (s->method != s->ctx->method))
133 s->method->ssl_free(s);
134 s->method=s->ctx->method;
135 if (!s->method->ssl_new(s))
140 s->method->ssl_clear(s);
144 /* Used to change an SSL_CTXs default SSL method type */
145 int SSL_CTX_set_ssl_version(ctx,meth)
153 sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
154 &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
155 if ((sk == NULL) || (sk_num(sk) <= 0))
157 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
170 SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
173 if (ctx->method == NULL)
175 SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
179 s=(SSL *)Malloc(sizeof(SSL));
180 if (s == NULL) goto err;
181 memset(s,0,sizeof(SSL));
183 if (ctx->default_cert != NULL)
185 CRYPTO_add(&ctx->default_cert->references,1,
186 CRYPTO_LOCK_SSL_CERT);
187 s->cert=ctx->default_cert;
191 s->verify_mode=ctx->verify_mode;
192 s->verify_callback=ctx->default_verify_callback;
193 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
196 s->verify_result=X509_V_OK;
198 s->method=ctx->method;
200 if (!s->method->ssl_new(s))
207 s->quiet_shutdown=ctx->quiet_shutdown;
209 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
210 s->options=ctx->options;
213 CRYPTO_new_ex_data(ssl_meth,(char *)s,&s->ex_data);
217 SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
229 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
237 fprintf(stderr,"SSL_free, bad reference count\n");
242 CRYPTO_free_ex_data(ssl_meth,(char *)s,&s->ex_data);
246 /* If the buffering BIO is in place, pop it off */
247 if (s->bbio == s->wbio)
249 s->wbio=BIO_pop(s->wbio);
255 BIO_free_all(s->rbio);
256 if ((s->wbio != NULL) && (s->wbio != s->rbio))
257 BIO_free_all(s->wbio);
259 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
261 /* add extra stuff */
262 if (s->cipher_list != NULL) sk_free(s->cipher_list);
263 if (s->cipher_list_by_id != NULL) sk_free(s->cipher_list_by_id);
265 /* Make the next call work :-) */
266 if (s->session != NULL)
268 ssl_clear_bad_session(s);
269 SSL_SESSION_free(s->session);
272 ssl_clear_cipher_ctx(s);
274 if (s->cert != NULL) ssl_cert_free(s->cert);
275 /* Free up if allocated */
277 if (s->ctx) SSL_CTX_free(s->ctx);
279 if (s->client_CA != NULL)
280 sk_pop_free(s->client_CA,X509_NAME_free);
282 if (s->method != NULL) s->method->ssl_free(s);
287 void SSL_set_bio(s, rbio,wbio)
292 /* If the output buffering BIO is still in place, remove it
296 if (s->wbio == s->bbio)
298 s->wbio=s->wbio->next_bio;
299 s->bbio->next_bio=NULL;
302 if ((s->rbio != NULL) && (s->rbio != rbio))
303 BIO_free_all(s->rbio);
304 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
305 BIO_free_all(s->wbio);
325 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
332 int SSL_set_fd(s, fd)
339 bio=BIO_new(BIO_s_socket());
343 SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
346 BIO_set_fd(bio,fd,BIO_NOCLOSE);
347 SSL_set_bio(s,bio,bio);
353 int SSL_set_wfd(s, fd)
360 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
361 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
363 bio=BIO_new(BIO_s_socket());
366 { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
367 BIO_set_fd(bio,fd,BIO_NOCLOSE);
368 SSL_set_bio(s,SSL_get_rbio(s),bio);
371 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
377 int SSL_set_rfd(s, fd)
384 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
385 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
387 bio=BIO_new(BIO_s_socket());
391 SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
394 BIO_set_fd(bio,fd,BIO_NOCLOSE);
395 SSL_set_bio(s,bio,SSL_get_wbio(s));
398 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
405 int SSL_get_verify_mode(s)
408 return(s->verify_mode);
411 int (*SSL_get_verify_callback(s))()
414 return(s->verify_callback);
417 int SSL_CTX_get_verify_mode(ctx)
420 return(ctx->verify_mode);
423 int (*SSL_CTX_get_verify_callback(ctx))()
426 return(ctx->default_verify_callback);
429 void SSL_set_verify(s, mode, callback)
435 if (callback != NULL)
436 s->verify_callback=callback;
439 void SSL_set_read_ahead(s, yes)
446 int SSL_get_read_ahead(s)
449 return(s->read_ahead);
455 return(s->method->ssl_pending(s));
458 X509 *SSL_get_peer_certificate(s)
463 if ((s == NULL) || (s->session == NULL))
468 if (r == NULL) return(r);
470 CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
475 STACK *SSL_get_peer_cert_chain(s)
480 if ((s == NULL) || (s->session == NULL) || (s->session->cert == NULL))
483 r=s->session->cert->cert_chain;
488 /* Now in theory, since the calling process own 't' it should be safe to
489 * modify. We need to be able to read f without being hassled */
490 void SSL_copy_session_id(t,f)
495 /* Do we need to to SSL locking? */
496 SSL_set_session(t,SSL_get_session(f));
498 /* what if we are setup as SSLv2 but want to talk SSLv3 or
500 if (t->method != f->method)
502 t->method->ssl_free(t); /* cleanup current */
503 t->method=f->method; /* change method */
504 t->method->ssl_new(t); /* setup new */
510 CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
515 if (tmp != NULL) ssl_cert_free(tmp);
518 /* Fix this so it checks all the valid key/cert options */
519 int SSL_CTX_check_private_key(ctx)
522 if ( (ctx == NULL) ||
523 (ctx->default_cert == NULL) ||
524 (ctx->default_cert->key->x509 == NULL))
526 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
529 if (ctx->default_cert->key->privatekey == NULL)
531 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
534 return(X509_check_private_key(ctx->default_cert->key->x509, ctx->default_cert->key->privatekey));
537 /* Fix this function so that it takes an optional type parameter */
538 int SSL_check_private_key(ssl)
543 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
546 if (ssl->cert == NULL)
547 return(SSL_CTX_check_private_key(ssl->ctx));
548 if (ssl->cert->key->x509 == NULL)
550 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
553 if (ssl->cert->key->privatekey == NULL)
555 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
558 return(X509_check_private_key(ssl->cert->key->x509,
559 ssl->cert->key->privatekey));
565 return(s->method->ssl_accept(s));
571 return(s->method->ssl_connect(s));
574 long SSL_get_default_timeout(s)
577 return(s->method->get_timeout());
580 int SSL_read(s,buf,num)
585 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
587 s->rwstate=SSL_NOTHING;
590 return(s->method->ssl_read(s,buf,num));
593 int SSL_peek(s,buf,num)
598 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
602 return(s->method->ssl_peek(s,buf,num));
605 int SSL_write(s,buf,num)
610 if (s->shutdown & SSL_SENT_SHUTDOWN)
612 s->rwstate=SSL_NOTHING;
613 SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
616 return(s->method->ssl_write(s,buf,num));
622 if ((s != NULL) && !SSL_in_init(s))
623 return(s->method->ssl_shutdown(s));
628 int SSL_renegotiate(s)
632 return(s->method->ssl_renegotiate(s));
635 long SSL_ctrl(s,cmd,larg,parg)
645 case SSL_CTRL_GET_READ_AHEAD:
646 return(s->read_ahead);
647 case SSL_CTRL_SET_READ_AHEAD:
651 case SSL_CTRL_OPTIONS:
652 return(s->options|=larg);
654 return(s->method->ssl_ctrl(s,cmd,larg,parg));
659 long SSL_CTX_ctrl(ctx,cmd,larg,parg)
669 case SSL_CTRL_GET_READ_AHEAD:
670 return(ctx->read_ahead);
671 case SSL_CTRL_SET_READ_AHEAD:
673 ctx->read_ahead=larg;
676 case SSL_CTRL_SET_SESS_CACHE_SIZE:
677 l=ctx->session_cache_size;
678 ctx->session_cache_size=larg;
680 case SSL_CTRL_GET_SESS_CACHE_SIZE:
681 return(ctx->session_cache_size);
682 case SSL_CTRL_SET_SESS_CACHE_MODE:
683 l=ctx->session_cache_mode;
684 ctx->session_cache_mode=larg;
686 case SSL_CTRL_GET_SESS_CACHE_MODE:
687 return(ctx->session_cache_mode);
689 case SSL_CTRL_SESS_NUMBER:
690 return(ctx->sessions->num_items);
691 case SSL_CTRL_SESS_CONNECT:
692 return(ctx->stats.sess_connect);
693 case SSL_CTRL_SESS_CONNECT_GOOD:
694 return(ctx->stats.sess_connect_good);
695 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
696 return(ctx->stats.sess_connect_renegotiate);
697 case SSL_CTRL_SESS_ACCEPT:
698 return(ctx->stats.sess_accept);
699 case SSL_CTRL_SESS_ACCEPT_GOOD:
700 return(ctx->stats.sess_accept_good);
701 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
702 return(ctx->stats.sess_accept_renegotiate);
703 case SSL_CTRL_SESS_HIT:
704 return(ctx->stats.sess_hit);
705 case SSL_CTRL_SESS_CB_HIT:
706 return(ctx->stats.sess_cb_hit);
707 case SSL_CTRL_SESS_MISSES:
708 return(ctx->stats.sess_miss);
709 case SSL_CTRL_SESS_TIMEOUTS:
710 return(ctx->stats.sess_timeout);
711 case SSL_CTRL_SESS_CACHE_FULL:
712 return(ctx->stats.sess_cache_full);
713 case SSL_CTRL_OPTIONS:
714 return(ctx->options|=larg);
716 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
721 int ssl_cipher_id_cmp(a,b)
730 return((l > 0)?1:-1);
733 int ssl_cipher_ptr_id_cmp(ap,bp)
734 SSL_CIPHER **ap,**bp;
738 l=(*ap)->id-(*bp)->id;
742 return((l > 0)?1:-1);
745 /* return a STACK of the ciphers available for the SSL and in order of
747 STACK *SSL_get_ciphers(s)
750 if ((s != NULL) && (s->cipher_list != NULL))
752 return(s->cipher_list);
754 else if ((s->ctx != NULL) &&
755 (s->ctx->cipher_list != NULL))
757 return(s->ctx->cipher_list);
762 /* return a STACK of the ciphers available for the SSL and in order of
764 STACK *ssl_get_ciphers_by_id(s)
767 if ((s != NULL) && (s->cipher_list_by_id != NULL))
769 return(s->cipher_list_by_id);
771 else if ((s != NULL) && (s->ctx != NULL) &&
772 (s->ctx->cipher_list_by_id != NULL))
774 return(s->ctx->cipher_list_by_id);
779 /* The old interface to get the same thing as SSL_get_ciphers() */
780 char *SSL_get_cipher_list(s,n)
787 if (s == NULL) return(NULL);
788 sk=SSL_get_ciphers(s);
789 if ((sk == NULL) || (sk_num(sk) <= n))
791 c=(SSL_CIPHER *)sk_value(sk,n);
792 if (c == NULL) return(NULL);
796 /* specify the ciphers to be used by defaut by the SSL_CTX */
797 int SSL_CTX_set_cipher_list(ctx,str)
803 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
804 &ctx->cipher_list_by_id,str);
806 return((sk == NULL)?0:1);
809 /* specify the ciphers to be used by the SSL */
810 int SSL_set_cipher_list(s, str)
816 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
817 &s->cipher_list_by_id,str);
819 return((sk == NULL)?0:1);
822 /* works well for SSLv2, not so good for SSLv3 */
823 char *SSL_get_shared_ciphers(s,buf,len)
833 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
838 sk=s->session->ciphers;
839 for (i=0; i<sk_num(sk); i++)
841 /* Decrement for either the ':' or a '\0' */
843 c=(SSL_CIPHER *)sk_value(sk,i);
844 for (cp=c->name; *cp; )
860 int ssl_cipher_list_to_bytes(s,sk,p)
869 if (sk == NULL) return(0);
872 for (i=0; i<sk_num(sk); i++)
874 c=(SSL_CIPHER *)sk_value(sk,i);
875 j=ssl_put_cipher_by_char(s,c,p);
881 STACK *ssl_bytes_to_cipher_list(s,p,num,skp)
891 n=ssl_put_cipher_by_char(s,NULL,NULL);
894 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
897 if ((skp == NULL) || (*skp == NULL))
898 sk=sk_new(NULL); /* change perhaps later */
905 for (i=0; i<num; i+=n)
907 c=ssl_get_cipher_by_char(s,p);
911 if (!sk_push(sk,(char *)c))
913 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
923 if ((skp == NULL) || (*skp == NULL))
928 unsigned long SSL_SESSION_hash(a)
934 ((unsigned int) a->session_id[0] )|
935 ((unsigned int) a->session_id[1]<< 8L)|
936 ((unsigned long)a->session_id[2]<<16L)|
937 ((unsigned long)a->session_id[3]<<24L);
941 int SSL_SESSION_cmp(a, b)
945 if (a->ssl_version != b->ssl_version)
947 if (a->session_id_length != b->session_id_length)
949 return(memcmp(a->session_id,b->session_id,a->session_id_length));
952 SSL_CTX *SSL_CTX_new(meth)
959 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
963 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
965 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
968 ret=(SSL_CTX *)Malloc(sizeof(SSL_CTX));
972 memset(ret,0,sizeof(SSL_CTX));
976 ret->cert_store=NULL;
977 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
978 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
979 ret->session_cache_head=NULL;
980 ret->session_cache_tail=NULL;
982 /* We take the system default */
983 ret->session_timeout=meth->get_timeout();
985 ret->new_session_cb=NULL;
986 ret->remove_session_cb=NULL;
987 ret->get_session_cb=NULL;
989 memset((char *)&ret->stats,0,sizeof(ret->stats));
992 ret->quiet_shutdown=0;
994 /* ret->cipher=NULL;*/
995 /* ret->s2->challenge=NULL;
996 ret->master_key=NULL;
998 ret->s2->conn_id=NULL; */
1000 ret->info_callback=NULL;
1002 ret->app_verify_callback=NULL;
1003 ret->app_verify_arg=NULL;
1006 ret->verify_mode=SSL_VERIFY_NONE;
1007 ret->default_verify_callback=NULL;
1008 if ((ret->default_cert=ssl_cert_new()) == NULL)
1011 ret->default_passwd_callback=NULL;
1012 ret->client_cert_cb=NULL;
1014 ret->sessions=lh_new(SSL_SESSION_hash,SSL_SESSION_cmp);
1015 if (ret->sessions == NULL) goto err;
1016 ret->cert_store=X509_STORE_new();
1017 if (ret->cert_store == NULL) goto err;
1019 ssl_create_cipher_list(ret->method,
1020 &ret->cipher_list,&ret->cipher_list_by_id,
1021 SSL_DEFAULT_CIPHER_LIST);
1022 if ((ret->cipher_list == NULL) || (sk_num(ret->cipher_list) <= 0))
1024 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
1028 if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
1030 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
1033 if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
1035 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
1038 if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
1040 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
1044 if ((ret->client_CA=sk_new_null()) == NULL)
1047 CRYPTO_new_ex_data(ssl_ctx_meth,(char *)ret,&ret->ex_data);
1049 ret->extra_certs=NULL;
1050 ret->comp_methods=SSL_COMP_get_compression_methods();
1054 SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
1056 if (ret != NULL) SSL_CTX_free(ret);
1060 void SSL_CTX_free(a)
1065 if (a == NULL) return;
1067 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1069 REF_PRINT("SSL_CTX",a);
1075 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1079 CRYPTO_free_ex_data(ssl_ctx_meth,(char *)a,&a->ex_data);
1081 if (a->sessions != NULL)
1083 SSL_CTX_flush_sessions(a,0);
1084 lh_free(a->sessions);
1086 if (a->cert_store != NULL)
1087 X509_STORE_free(a->cert_store);
1088 if (a->cipher_list != NULL)
1089 sk_free(a->cipher_list);
1090 if (a->cipher_list_by_id != NULL)
1091 sk_free(a->cipher_list_by_id);
1092 if (a->default_cert != NULL)
1093 ssl_cert_free(a->default_cert);
1094 if (a->client_CA != NULL)
1095 sk_pop_free(a->client_CA,X509_NAME_free);
1096 if (a->extra_certs != NULL)
1097 sk_pop_free(a->extra_certs,X509_free);
1098 if (a->comp_methods != NULL)
1099 sk_pop_free(a->comp_methods,free);
1103 void SSL_CTX_set_default_passwd_cb(ctx,cb)
1107 ctx->default_passwd_callback=cb;
1110 void SSL_CTX_set_cert_verify_cb(ctx,cb,arg)
1115 ctx->app_verify_callback=cb;
1116 ctx->app_verify_arg=arg;
1119 void SSL_CTX_set_verify(ctx,mode,cb)
1123 int (*cb)(int, X509_STORE_CTX *);
1128 ctx->verify_mode=mode;
1129 ctx->default_verify_callback=cb;
1130 /* This needs cleaning up EAY EAY EAY */
1131 X509_STORE_set_verify_cb_func(ctx->cert_store,cb);
1134 void ssl_set_cert_masks(c)
1138 int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1139 int rsa_enc_export,dh_rsa_export,dh_dsa_export;
1140 int rsa_tmp_export,dh_tmp_export;
1141 unsigned long mask,emask;
1143 if ((c == NULL) || (c->valid)) return;
1146 rsa_tmp=((c->rsa_tmp != NULL) || (c->rsa_tmp_cb != NULL))?1:0;
1147 rsa_tmp_export=((c->rsa_tmp_cb != NULL) ||
1148 (rsa_tmp && (RSA_size(c->rsa_tmp)*8 <= 512)))?1:0;
1150 rsa_tmp=rsa_tmp_export=0;
1153 dh_tmp=((c->dh_tmp != NULL) || (c->dh_tmp_cb != NULL))?1:0;
1154 dh_tmp_export=((c->dh_tmp_cb != NULL) ||
1155 (dh_tmp && (DH_size(c->dh_tmp)*8 <= 512)))?1:0;
1157 dh_tmp=dh_tmp_export=0;
1160 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
1161 rsa_enc= ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1162 rsa_enc_export=(rsa_enc && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1163 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
1164 rsa_sign=((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1165 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
1166 dsa_sign=((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1167 cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
1168 dh_rsa= ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1169 dh_rsa_export=(dh_rsa && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1170 cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1171 /* FIX THIS EAY EAY EAY */
1172 dh_dsa= ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1173 dh_dsa_export=(dh_dsa && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1179 printf("rt=%d dht=%d re=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1181 rsa_enc,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1184 if (rsa_enc || (rsa_tmp && rsa_sign))
1186 if (rsa_enc_export || (rsa_tmp_export && rsa_sign))
1190 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1191 if ( (dh_tmp || dh_rsa || dh_dsa) &&
1192 (rsa_enc || rsa_sign || dsa_sign))
1194 if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1195 (rsa_enc || rsa_sign || dsa_sign))
1205 if (dh_rsa) mask|=SSL_kDHr;
1206 if (dh_rsa_export) emask|=SSL_kDHr;
1208 if (dh_dsa) mask|=SSL_kDHd;
1209 if (dh_dsa_export) emask|=SSL_kDHd;
1211 if (rsa_enc || rsa_sign)
1223 #ifdef SSL_ALLOW_ADH
1229 c->export_mask=emask;
1233 /* THIS NEEDS CLEANING UP */
1234 X509 *ssl_get_server_send_cert(s)
1237 unsigned long alg,mask,kalg;
1242 ssl_set_cert_masks(c);
1243 alg=s->s3->tmp.new_cipher->algorithms;
1244 _export=SSL_IS_EXPORT(alg);
1245 mask=_export?c->export_mask:c->mask;
1246 kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1248 if (kalg & SSL_kDHr)
1250 else if (kalg & SSL_kDHd)
1252 else if (kalg & SSL_aDSS)
1253 i=SSL_PKEY_DSA_SIGN;
1254 else if (kalg & SSL_aRSA)
1256 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1257 i=SSL_PKEY_RSA_SIGN;
1261 else /* if (kalg & SSL_aNULL) */
1263 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,SSL_R_INTERNAL_ERROR);
1266 if (c->pkeys[i].x509 == NULL) return(NULL);
1267 return(c->pkeys[i].x509);
1270 EVP_PKEY *ssl_get_sign_pkey(s,cipher)
1277 alg=cipher->algorithms;
1280 if ((alg & SSL_aDSS) &&
1281 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
1282 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
1283 else if (alg & SSL_aRSA)
1285 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
1286 return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
1287 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
1288 return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
1292 else /* if (alg & SSL_aNULL) */
1294 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,SSL_R_INTERNAL_ERROR);
1299 void ssl_update_cache(s,mode)
1305 /* If the session_id_length is 0, we are not supposed to cache it,
1306 * and it would be rather hard to do anyway :-) */
1307 if (s->session->session_id_length == 0) return;
1309 if ((s->ctx->session_cache_mode & mode)
1311 && SSL_CTX_add_session(s->ctx,s->session)
1312 && (s->ctx->new_session_cb != NULL))
1314 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
1315 if (!s->ctx->new_session_cb(s,s->session))
1316 SSL_SESSION_free(s->session);
1319 /* auto flush every 255 connections */
1320 i=s->ctx->session_cache_mode;
1321 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
1322 ((i & mode) == mode))
1324 if ( (((mode & SSL_SESS_CACHE_CLIENT)
1325 ?s->ctx->stats.sess_connect_good
1326 :s->ctx->stats.sess_accept_good) & 0xff) == 0xff)
1328 SSL_CTX_flush_sessions(s->ctx,time(NULL));
1333 SSL_METHOD *SSL_get_ssl_method(s)
1339 int SSL_set_ssl_method(s,meth)
1346 if (s->method != meth)
1348 if (s->handshake_func != NULL)
1349 conn=(s->handshake_func == s->method->ssl_connect);
1351 if (s->method->version == meth->version)
1355 s->method->ssl_free(s);
1357 ret=s->method->ssl_new(s);
1361 s->handshake_func=meth->ssl_connect;
1363 s->handshake_func=meth->ssl_accept;
1368 int SSL_get_error(s,i)
1376 if (i > 0) return(SSL_ERROR_NONE);
1378 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
1379 * etc, where we do encode the error */
1380 if ((l=ERR_peek_error()) != 0)
1382 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
1383 return(SSL_ERROR_SYSCALL);
1385 return(SSL_ERROR_SSL);
1388 if ((i < 0) && SSL_want_read(s))
1390 bio=SSL_get_rbio(s);
1391 if (BIO_should_read(bio))
1392 return(SSL_ERROR_WANT_READ);
1393 else if (BIO_should_write(bio))
1394 return(SSL_ERROR_WANT_WRITE);
1395 else if (BIO_should_io_special(bio))
1397 reason=BIO_get_retry_reason(bio);
1398 if (reason == BIO_RR_CONNECT)
1399 return(SSL_ERROR_WANT_CONNECT);
1401 return(SSL_ERROR_SYSCALL); /* unknown */
1405 if ((i < 0) && SSL_want_write(s))
1407 bio=SSL_get_wbio(s);
1408 if (BIO_should_write(bio))
1409 return(SSL_ERROR_WANT_WRITE);
1410 else if (BIO_should_read(bio))
1411 return(SSL_ERROR_WANT_READ);
1412 else if (BIO_should_io_special(bio))
1414 reason=BIO_get_retry_reason(bio);
1415 if (reason == BIO_RR_CONNECT)
1416 return(SSL_ERROR_WANT_CONNECT);
1418 return(SSL_ERROR_SYSCALL);
1421 if ((i < 0) && SSL_want_x509_lookup(s))
1423 return(SSL_ERROR_WANT_X509_LOOKUP);
1428 if (s->version == SSL2_VERSION)
1430 /* assume it is the socket being closed */
1431 return(SSL_ERROR_ZERO_RETURN);
1435 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1436 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
1437 return(SSL_ERROR_ZERO_RETURN);
1440 return(SSL_ERROR_SYSCALL);
1443 int SSL_do_handshake(s)
1448 if (s->handshake_func == NULL)
1450 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
1454 s->method->ssl_renegotiate_check(s);
1456 if (SSL_in_init(s) || SSL_in_before(s))
1458 ret=s->handshake_func(s);
1463 /* For the next 2 functions, SSL_clear() sets shutdown and so
1464 * one of these calls will reset it */
1465 void SSL_set_accept_state(s)
1470 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
1471 s->handshake_func=s->method->ssl_accept;
1472 /* clear the current cipher */
1473 ssl_clear_cipher_ctx(s);
1476 void SSL_set_connect_state(s)
1481 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
1482 s->handshake_func=s->method->ssl_connect;
1483 /* clear the current cipher */
1484 ssl_clear_cipher_ctx(s);
1487 int ssl_undefined_function(s)
1490 SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1494 SSL_METHOD *ssl_bad_method(ver)
1497 SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1501 char *SSL_get_version(s)
1504 if (s->version == TLS1_VERSION)
1506 else if (s->version == SSL3_VERSION)
1508 else if (s->version == SSL2_VERSION)
1522 if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) return(NULL);
1524 /* This copies version, session-id, SSL_METHOD and 'cert' */
1525 SSL_copy_session_id(ret,s);
1527 SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
1528 SSL_set_verify(ret,SSL_get_verify_mode(s),
1529 SSL_get_verify_callback(s));
1531 SSL_set_info_callback(ret,SSL_get_info_callback(s));
1533 ret->debug=s->debug;
1534 ret->options=s->options;
1536 /* copy app data, a little dangerous perhaps */
1537 if (!CRYPTO_dup_ex_data(ssl_meth,&ret->ex_data,&s->ex_data))
1540 /* setup rbio, and wbio */
1541 if (s->rbio != NULL)
1543 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
1546 if (s->wbio != NULL)
1548 if (s->wbio != s->rbio)
1550 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
1554 ret->wbio=ret->rbio;
1557 /* dup the cipher_list and cipher_list_by_id stacks */
1558 if (s->cipher_list != NULL)
1560 if ((ret->cipher_list=sk_dup(s->cipher_list)) == NULL)
1563 if (s->cipher_list_by_id != NULL)
1564 if ((ret->cipher_list_by_id=sk_dup(s->cipher_list_by_id))
1568 /* Dup the client_CA list */
1569 if (s->client_CA != NULL)
1571 if ((sk=sk_dup(s->client_CA)) == NULL) goto err;
1573 for (i=0; i<sk_num(sk); i++)
1575 xn=(X509_NAME *)sk_value(sk,i);
1576 if ((sk_value(sk,i)=(char *)X509_NAME_dup(xn)) == NULL)
1584 ret->shutdown=s->shutdown;
1585 ret->state=s->state;
1586 ret->handshake_func=s->handshake_func;
1587 ret->server=s->server;
1592 if (ret != NULL) SSL_free(ret);
1598 void ssl_clear_cipher_ctx(s)
1601 if (s->enc_read_ctx != NULL)
1603 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
1604 Free(s->enc_read_ctx);
1605 s->enc_read_ctx=NULL;
1607 if (s->enc_write_ctx != NULL)
1609 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
1610 Free(s->enc_write_ctx);
1611 s->enc_write_ctx=NULL;
1613 if (s->expand != NULL)
1615 COMP_CTX_free(s->expand);
1618 if (s->compress != NULL)
1620 COMP_CTX_free(s->compress);
1625 /* Fix this function so that it takes an optional type parameter */
1626 X509 *SSL_get_certificate(s)
1629 if (s->cert != NULL)
1630 return(s->cert->key->x509);
1635 /* Fix this function so that it takes an optional type parameter */
1636 EVP_PKEY *SSL_get_privatekey(s)
1639 if (s->cert != NULL)
1640 return(s->cert->key->privatekey);
1645 SSL_CIPHER *SSL_get_current_cipher(s)
1648 if ((s->session != NULL) && (s->session->cipher != NULL))
1649 return(s->session->cipher);
1653 int ssl_init_wbio_buffer(s,push)
1659 if (s->bbio == NULL)
1661 bbio=BIO_new(BIO_f_buffer());
1662 if (bbio == NULL) return(0);
1668 if (s->bbio == s->wbio)
1669 s->wbio=BIO_pop(s->wbio);
1672 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
1673 if (!BIO_set_read_buffer_size(bbio,1))
1675 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
1680 if (s->wbio != bbio)
1681 s->wbio=BIO_push(bbio,s->wbio);
1685 if (s->wbio == bbio)
1686 s->wbio=BIO_pop(bbio);
1691 void ssl_free_wbio_buffer(s)
1696 if (s->bbio == NULL) return;
1698 if (s->bbio == s->wbio)
1700 /* remove buffering */
1701 under=BIO_pop(s->wbio);
1711 void SSL_CTX_set_quiet_shutdown(ctx,mode)
1715 ctx->quiet_shutdown=mode;
1718 int SSL_CTX_get_quiet_shutdown(ctx)
1721 return(ctx->quiet_shutdown);
1724 void SSL_set_quiet_shutdown(s,mode)
1728 s->quiet_shutdown=mode;
1731 int SSL_get_quiet_shutdown(s)
1734 return(s->quiet_shutdown);
1737 void SSL_set_shutdown(s,mode)
1744 int SSL_get_shutdown(s)
1747 return(s->shutdown);
1756 SSL_CTX *SSL_get_SSL_CTX(ssl)
1763 int SSL_CTX_set_default_verify_paths(ctx)
1766 return(X509_STORE_set_default_paths(ctx->cert_store));
1769 int SSL_CTX_load_verify_locations(ctx,CAfile,CApath)
1774 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
1778 void SSL_set_info_callback(ssl,cb)
1782 ssl->info_callback=cb;
1785 void (*SSL_get_info_callback(ssl))()
1788 return((void (*)())ssl->info_callback);
1797 void SSL_set_verify_result(ssl,arg)
1801 ssl->verify_result=arg;
1804 long SSL_get_verify_result(ssl)
1807 return(ssl->verify_result);
1810 int SSL_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
1815 void (*free_func)();
1818 return(CRYPTO_get_ex_new_index(ssl_meth_num-1,
1819 &ssl_meth,argl,argp,new_func,dup_func,free_func));
1822 int SSL_set_ex_data(s,idx,arg)
1827 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1830 void *SSL_get_ex_data(s,idx)
1834 return(CRYPTO_get_ex_data(&s->ex_data,idx));
1837 int SSL_CTX_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
1842 void (*free_func)();
1845 return(CRYPTO_get_ex_new_index(ssl_ctx_meth_num-1,
1846 &ssl_ctx_meth,argl,argp,new_func,dup_func,free_func));
1849 int SSL_CTX_set_ex_data(s,idx,arg)
1854 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1857 void *SSL_CTX_get_ex_data(s,idx)
1861 return(CRYPTO_get_ex_data(&s->ex_data,idx));
1870 X509_STORE *SSL_CTX_get_cert_store(ctx)
1873 return(ctx->cert_store);
1876 void SSL_CTX_set_cert_store(ctx,store)
1880 if (ctx->cert_store != NULL)
1881 X509_STORE_free(ctx->cert_store);
1882 ctx->cert_store=store;
1891 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,int export))
1892 { SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,0,(char *)cb); }
1894 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int export))
1895 { SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,0,(char *)dh); }
1897 #if defined(_WINDLL) && defined(WIN16)
1898 #include "../crypto/bio/bss_file.c"