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.1c 23-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) return;
90 /* This is set if we are doing dynamic renegotiation so keep
91 * the old cipher. It is sort of a SSL_clear_lite :-) */
92 if (s->new_session) return;
94 state=s->state; /* Keep to check if we throw away the session-id */
97 s->version=s->method->version;
98 s->rwstate=SSL_NOTHING;
99 s->state=SSL_ST_BEFORE;
100 s->rstate=SSL_ST_READ_HEADER;
101 s->read_ahead=s->ctx->default_read_ahead;
103 /* s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); */
105 if (s->init_buf != NULL)
107 BUF_MEM_free(s->init_buf);
111 ssl_clear_cipher_ctx(s);
113 if (ssl_clear_bad_session(s))
115 SSL_SESSION_free(s->session);
119 s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
122 s->method->ssl_clear(s);
125 /* Used to change an SSL_CTXs default SSL method type */
126 int SSL_CTX_set_ssl_version(ctx,meth)
134 sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
135 &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
136 if ((sk == NULL) || (sk_num(sk) <= 0))
138 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
151 SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
154 if (ctx->method == NULL)
156 SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
160 s=(SSL *)Malloc(sizeof(SSL));
161 if (s == NULL) goto err;
162 memset(s,0,sizeof(SSL));
164 if (ctx->default_cert != NULL)
166 CRYPTO_add(&ctx->default_cert->references,1,
167 CRYPTO_LOCK_SSL_CERT);
168 s->cert=ctx->default_cert;
172 s->verify_mode=ctx->default_verify_mode;
173 s->verify_callback=ctx->default_verify_callback;
174 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
177 s->verify_result=X509_V_OK;
179 s->method=ctx->method;
181 if (!s->method->ssl_new(s))
188 s->quiet_shutdown=ctx->quiet_shutdown;
190 s->options=ctx->options;
193 CRYPTO_new_ex_data(ssl_meth,(char *)s,&s->ex_data);
197 SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
206 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
214 fprintf(stderr,"SSL_free, bad reference count\n");
219 CRYPTO_free_ex_data(ssl_meth,(char *)s,&s->ex_data);
223 /* If the buffering BIO is in place, pop it off */
224 if (s->bbio == s->wbio)
226 s->wbio=BIO_pop(s->wbio);
232 BIO_free_all(s->rbio);
233 if ((s->wbio != NULL) && (s->wbio != s->rbio))
234 BIO_free_all(s->wbio);
236 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
238 /* add extra stuff */
239 if (s->cipher_list != NULL) sk_free(s->cipher_list);
240 if (s->cipher_list_by_id != NULL) sk_free(s->cipher_list_by_id);
242 /* Make the next call work :-) */
243 if (s->session != NULL)
245 ssl_clear_bad_session(s);
246 SSL_SESSION_free(s->session);
249 ssl_clear_cipher_ctx(s);
251 if (s->expand != NULL)
252 COMP_CTX_free(s->expand);
253 if (s->compress != NULL)
254 COMP_CTX_free(s->compress);
256 if (s->cert != NULL) ssl_cert_free(s->cert);
257 /* Free up if allocated */
259 if (s->ctx) SSL_CTX_free(s->ctx);
261 if (s->client_CA != NULL)
262 sk_pop_free(s->client_CA,X509_NAME_free);
264 if (s->method != NULL) s->method->ssl_free(s);
269 void SSL_set_bio(s, rbio,wbio)
274 /* If the output buffering BIO is still in place, remove it
278 if (s->wbio == s->bbio)
280 s->wbio=s->wbio->next_bio;
281 s->bbio->next_bio=NULL;
284 if ((s->rbio != NULL) && (s->rbio != rbio))
285 BIO_free_all(s->rbio);
286 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
287 BIO_free_all(s->wbio);
307 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
314 int SSL_set_fd(s, fd)
321 bio=BIO_new(BIO_s_socket());
325 SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
328 BIO_set_fd(bio,fd,BIO_NOCLOSE);
329 SSL_set_bio(s,bio,bio);
335 int SSL_set_wfd(s, fd)
342 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
343 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
345 bio=BIO_new(BIO_s_socket());
348 { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
349 BIO_set_fd(bio,fd,BIO_NOCLOSE);
350 SSL_set_bio(s,SSL_get_rbio(s),bio);
353 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
359 int SSL_set_rfd(s, fd)
366 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
367 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
369 bio=BIO_new(BIO_s_socket());
373 SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
376 BIO_set_fd(bio,fd,BIO_NOCLOSE);
377 SSL_set_bio(s,bio,SSL_get_wbio(s));
380 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
387 int SSL_get_verify_mode(s)
390 return(s->verify_mode);
393 int (*SSL_get_verify_callback(s))()
396 return(s->verify_callback);
399 int SSL_CTX_get_verify_mode(ctx)
402 return(ctx->default_verify_mode);
405 int (*SSL_CTX_get_verify_callback(ctx))()
408 return(ctx->default_verify_callback);
411 void SSL_set_verify(s, mode, callback)
417 if (callback != NULL)
418 s->verify_callback=callback;
421 void SSL_set_read_ahead(s, yes)
428 int SSL_get_read_ahead(s)
431 return(s->read_ahead);
437 return(s->method->ssl_pending(s));
440 X509 *SSL_get_peer_certificate(s)
445 if ((s == NULL) || (s->session == NULL))
450 if (r == NULL) return(r);
452 CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
457 STACK *SSL_get_peer_cert_chain(s)
462 if ((s == NULL) || (s->session == NULL) || (s->session->cert == NULL))
465 r=s->session->cert->cert_chain;
470 /* Now in theory, since the calling process own 't' it should be safe to
471 * modify. We need to be able to read f without being hassled */
472 void SSL_copy_session_id(t,f)
477 /* Do we need to to SSL locking? */
478 SSL_set_session(t,SSL_get_session(f));
480 /* what if we are setup as SSLv2 but want to talk SSLv3 or
482 if (t->method != f->method)
484 t->method->ssl_free(t); /* cleanup current */
485 t->method=f->method; /* change method */
486 t->method->ssl_new(t); /* setup new */
492 CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
497 if (tmp != NULL) ssl_cert_free(tmp);
500 /* Fix this so it checks all the valid key/cert options */
501 int SSL_CTX_check_private_key(ctx)
504 if ( (ctx == NULL) ||
505 (ctx->default_cert == NULL) ||
506 (ctx->default_cert->key->x509 == NULL))
508 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
511 if (ctx->default_cert->key->privatekey == NULL)
513 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
516 return(X509_check_private_key(ctx->default_cert->key->x509, ctx->default_cert->key->privatekey));
519 /* Fix this function so that it takes an optional type parameter */
520 int SSL_check_private_key(ssl)
525 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
528 if (ssl->cert == NULL)
529 return(SSL_CTX_check_private_key(ssl->ctx));
530 if (ssl->cert->key->x509 == NULL)
532 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
535 if (ssl->cert->key->privatekey == NULL)
537 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
540 return(X509_check_private_key(ssl->cert->key->x509,
541 ssl->cert->key->privatekey));
547 return(s->method->ssl_accept(s));
553 return(s->method->ssl_connect(s));
556 long SSL_get_default_timeout(s)
559 return(s->method->get_timeout());
562 int SSL_read(s,buf,num)
567 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
569 s->rwstate=SSL_NOTHING;
572 return(s->method->ssl_read(s,buf,num));
575 int SSL_peek(s,buf,num)
580 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
584 return(s->method->ssl_peek(s,buf,num));
587 int SSL_write(s,buf,num)
592 if (s->shutdown & SSL_SENT_SHUTDOWN)
594 s->rwstate=SSL_NOTHING;
595 SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
598 return(s->method->ssl_write(s,buf,num));
604 if ((s != NULL) && !SSL_in_init(s))
605 return(s->method->ssl_shutdown(s));
610 int SSL_renegotiate(s)
614 return(s->method->ssl_renegotiate(s));
617 long SSL_ctrl(s,cmd,larg,parg)
623 return(s->method->ssl_ctrl(s,cmd,larg,parg));
626 long SSL_CTX_ctrl(ctx,cmd,larg,parg)
632 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
635 int ssl_cipher_id_cmp(a,b)
644 return((l > 0)?1:-1);
647 int ssl_cipher_ptr_id_cmp(ap,bp)
648 SSL_CIPHER **ap,**bp;
652 l=(*ap)->id-(*bp)->id;
656 return((l > 0)?1:-1);
659 /* return a STACK of the ciphers available for the SSL and in order of
661 STACK *SSL_get_ciphers(s)
664 if ((s != NULL) && (s->cipher_list != NULL))
666 return(s->cipher_list);
668 else if ((s->ctx != NULL) &&
669 (s->ctx->cipher_list != NULL))
671 return(s->ctx->cipher_list);
676 /* return a STACK of the ciphers available for the SSL and in order of
678 STACK *ssl_get_ciphers_by_id(s)
681 if ((s != NULL) && (s->cipher_list_by_id != NULL))
683 return(s->cipher_list_by_id);
685 else if ((s != NULL) && (s->ctx != NULL) &&
686 (s->ctx->cipher_list_by_id != NULL))
688 return(s->ctx->cipher_list_by_id);
693 /* The old interface to get the same thing as SSL_get_ciphers() */
694 char *SSL_get_cipher_list(s,n)
701 if (s == NULL) return(NULL);
702 sk=SSL_get_ciphers(s);
703 if ((sk == NULL) || (sk_num(sk) <= n))
705 c=(SSL_CIPHER *)sk_value(sk,n);
706 if (c == NULL) return(NULL);
710 /* specify the ciphers to be used by defaut by the SSL_CTX */
711 int SSL_CTX_set_cipher_list(ctx,str)
717 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
718 &ctx->cipher_list_by_id,str);
720 return((sk == NULL)?0:1);
723 /* specify the ciphers to be used by the SSL */
724 int SSL_set_cipher_list(s, str)
730 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
731 &s->cipher_list_by_id,str);
733 return((sk == NULL)?0:1);
736 /* works well for SSLv2, not so good for SSLv3 */
737 char *SSL_get_shared_ciphers(s,buf,len)
747 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
752 sk=s->session->ciphers;
753 for (i=0; i<sk_num(sk); i++)
755 /* Decrement for either the ':' or a '\0' */
757 c=(SSL_CIPHER *)sk_value(sk,i);
758 for (cp=c->name; *cp; )
774 int ssl_cipher_list_to_bytes(s,sk,p)
783 if (sk == NULL) return(0);
786 for (i=0; i<sk_num(sk); i++)
788 c=(SSL_CIPHER *)sk_value(sk,i);
789 j=ssl_put_cipher_by_char(s,c,p);
795 STACK *ssl_bytes_to_cipher_list(s,p,num,skp)
805 n=ssl_put_cipher_by_char(s,NULL,NULL);
808 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
811 if ((skp == NULL) || (*skp == NULL))
812 sk=sk_new(NULL); /* change perhaps later */
819 for (i=0; i<num; i+=n)
821 c=ssl_get_cipher_by_char(s,p);
825 if (!sk_push(sk,(char *)c))
827 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
837 if ((skp == NULL) || (*skp == NULL))
842 unsigned long SSL_SESSION_hash(a)
848 ((unsigned int) a->session_id[0] )|
849 ((unsigned int) a->session_id[1]<< 8L)|
850 ((unsigned long)a->session_id[2]<<16L)|
851 ((unsigned long)a->session_id[3]<<24L);
855 int SSL_SESSION_cmp(a, b)
859 if (a->ssl_version != b->ssl_version)
861 if (a->session_id_length != b->session_id_length)
863 return(memcmp(a->session_id,b->session_id,a->session_id_length));
866 SSL_CTX *SSL_CTX_new(meth)
873 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
877 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
879 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
882 ret=(SSL_CTX *)Malloc(sizeof(SSL_CTX));
886 memset(ret,0,sizeof(SSL_CTX));
890 ret->cert_store=NULL;
891 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
892 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
893 ret->session_cache_head=NULL;
894 ret->session_cache_tail=NULL;
896 /* We take the system default */
897 ret->session_timeout=meth->get_timeout();
899 ret->new_session_cb=NULL;
900 ret->remove_session_cb=NULL;
901 ret->get_session_cb=NULL;
904 ret->sess_connect_good=0;
906 ret->sess_accept_renegotiate=0;
907 ret->sess_connect_renegotiate=0;
908 ret->sess_accept_good=0;
911 ret->sess_cache_full=0;
916 ret->quiet_shutdown=0;
918 /* ret->cipher=NULL;*/
919 /* ret->s2->challenge=NULL;
920 ret->master_key=NULL;
922 ret->s2->conn_id=NULL; */
924 ret->info_callback=NULL;
926 ret->app_verify_callback=NULL;
927 ret->app_verify_arg=NULL;
929 ret->default_read_ahead=0;
930 ret->default_verify_mode=SSL_VERIFY_NONE;
931 ret->default_verify_callback=NULL;
932 if ((ret->default_cert=ssl_cert_new()) == NULL)
935 ret->default_passwd_callback=NULL;
936 ret->client_cert_cb=NULL;
938 ret->sessions=lh_new(SSL_SESSION_hash,SSL_SESSION_cmp);
939 if (ret->sessions == NULL) goto err;
940 ret->cert_store=X509_STORE_new();
941 if (ret->cert_store == NULL) goto err;
943 ssl_create_cipher_list(ret->method,
944 &ret->cipher_list,&ret->cipher_list_by_id,
945 SSL_DEFAULT_CIPHER_LIST);
946 if ((ret->cipher_list == NULL) || (sk_num(ret->cipher_list) <= 0))
948 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
952 if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
954 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
957 if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
959 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
962 if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
964 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
968 if ((ret->client_CA=sk_new_null()) == NULL)
971 CRYPTO_new_ex_data(ssl_ctx_meth,(char *)ret,&ret->ex_data);
973 ret->extra_certs=NULL;
977 SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
979 if (ret != NULL) SSL_CTX_free(ret);
988 if (a == NULL) return;
990 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
992 REF_PRINT("SSL_CTX",a);
998 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1002 CRYPTO_free_ex_data(ssl_ctx_meth,(char *)a,&a->ex_data);
1004 if (a->sessions != NULL)
1006 SSL_CTX_flush_sessions(a,0);
1007 lh_free(a->sessions);
1009 if (a->cert_store != NULL)
1010 X509_STORE_free(a->cert_store);
1011 if (a->cipher_list != NULL)
1012 sk_free(a->cipher_list);
1013 if (a->cipher_list_by_id != NULL)
1014 sk_free(a->cipher_list_by_id);
1015 if (a->default_cert != NULL)
1016 ssl_cert_free(a->default_cert);
1017 if (a->client_CA != NULL)
1018 sk_pop_free(a->client_CA,X509_NAME_free);
1019 if (a->extra_certs != NULL)
1020 sk_pop_free(a->extra_certs,X509_free);
1024 void SSL_CTX_set_default_passwd_cb(ctx,cb)
1028 ctx->default_passwd_callback=cb;
1031 void SSL_CTX_set_cert_verify_cb(ctx,cb,arg)
1036 ctx->app_verify_callback=cb;
1037 ctx->app_verify_arg=arg;
1040 void SSL_CTX_set_verify(ctx,mode,cb)
1045 ctx->default_verify_mode=mode;
1046 ctx->default_verify_callback=cb;
1047 /* This needs cleaning up EAY EAY EAY */
1048 X509_STORE_set_verify_cb_func(ctx->cert_store,cb);
1051 void ssl_set_cert_masks(c)
1055 int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1056 int rsa_enc_export,dh_rsa_export,dh_dsa_export;
1057 int rsa_tmp_export,dh_tmp_export;
1058 unsigned long mask,emask;
1060 if ((c == NULL) || (c->valid)) return;
1063 rsa_tmp=((c->rsa_tmp != NULL) || (c->rsa_tmp_cb != NULL))?1:0;
1064 rsa_tmp_export=((c->rsa_tmp_cb != NULL) ||
1065 (rsa_tmp && (RSA_size(c->rsa_tmp)*8 <= 512)))?1:0;
1067 rsa_tmp=rsa_tmp_export=0;
1070 dh_tmp=((c->dh_tmp != NULL) || (c->dh_tmp_cb != NULL))?1:0;
1071 dh_tmp_export=((c->dh_tmp_cb != NULL) ||
1072 (dh_tmp && (DH_size(c->dh_tmp)*8 <= 512)))?1:0;
1074 dh_tmp=dh_tmp_export=0;
1077 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
1078 rsa_enc= ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1079 rsa_enc_export=(rsa_enc && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1080 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
1081 rsa_sign=((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1082 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
1083 dsa_sign=((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1084 cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
1085 dh_rsa= ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1086 dh_rsa_export=(dh_rsa && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1087 cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1088 /* FIX THIS EAY EAY EAY */
1089 dh_dsa= ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1090 dh_dsa_export=(dh_dsa && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1096 printf("rt=%d dht=%d re=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1098 rsa_enc,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1101 if (rsa_enc || (rsa_tmp && rsa_sign))
1103 if (rsa_enc_export || (rsa_tmp_export && rsa_sign))
1107 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1108 if ( (dh_tmp || dh_rsa || dh_dsa) &&
1109 (rsa_enc || rsa_sign || dsa_sign))
1111 if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1112 (rsa_enc || rsa_sign || dsa_sign))
1122 if (dh_rsa) mask|=SSL_kDHr;
1123 if (dh_rsa_export) emask|=SSL_kDHr;
1125 if (dh_dsa) mask|=SSL_kDHd;
1126 if (dh_dsa_export) emask|=SSL_kDHd;
1128 if (rsa_enc || rsa_sign)
1140 #ifdef SSL_ALLOW_ADH
1146 c->export_mask=emask;
1150 /* THIS NEEDS CLEANING UP */
1151 X509 *ssl_get_server_send_cert(s)
1154 unsigned long alg,mask,kalg;
1159 ssl_set_cert_masks(c);
1160 alg=s->s3->tmp.new_cipher->algorithms;
1161 export=(alg & SSL_EXPORT)?1:0;
1162 mask=(export)?c->export_mask:c->mask;
1163 kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1165 if (kalg & SSL_kDHr)
1167 else if (kalg & SSL_kDHd)
1169 else if (kalg & SSL_aDSS)
1170 i=SSL_PKEY_DSA_SIGN;
1171 else if (kalg & SSL_aRSA)
1173 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1174 i=SSL_PKEY_RSA_SIGN;
1178 else /* if (kalg & SSL_aNULL) */
1180 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,SSL_R_INTERNAL_ERROR);
1183 if (c->pkeys[i].x509 == NULL) return(NULL);
1184 return(c->pkeys[i].x509);
1187 EVP_PKEY *ssl_get_sign_pkey(s,cipher)
1194 alg=cipher->algorithms;
1197 if ((alg & SSL_aDSS) &&
1198 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
1199 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
1200 else if (alg & SSL_aRSA)
1202 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
1203 return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
1204 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
1205 return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
1209 else /* if (alg & SSL_aNULL) */
1211 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,SSL_R_INTERNAL_ERROR);
1216 void ssl_update_cache(s,mode)
1222 /* If the session_id_length is 0, we are not supposed to cache it,
1223 * and it would be rather hard to do anyway :-) */
1224 if (s->session->session_id_length == 0) return;
1226 if ((s->ctx->session_cache_mode & mode)
1228 && SSL_CTX_add_session(s->ctx,s->session)
1229 && (s->ctx->new_session_cb != NULL))
1231 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
1232 if (!s->ctx->new_session_cb(s,s->session))
1233 SSL_SESSION_free(s->session);
1236 /* auto flush every 255 connections */
1237 i=s->ctx->session_cache_mode;
1238 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
1239 ((i & mode) == mode))
1241 if ( (((mode & SSL_SESS_CACHE_CLIENT)
1242 ?s->ctx->sess_connect_good
1243 :s->ctx->sess_accept_good) & 0xff) == 0xff)
1245 SSL_CTX_flush_sessions(s->ctx,time(NULL));
1250 SSL_METHOD *SSL_get_ssl_method(s)
1256 int SSL_set_ssl_method(s,meth)
1263 if (s->method != meth)
1265 if (s->handshake_func != NULL)
1266 conn=(s->handshake_func == s->method->ssl_connect);
1268 if (s->method->version == meth->version)
1272 s->method->ssl_free(s);
1274 ret=s->method->ssl_new(s);
1278 s->handshake_func=meth->ssl_connect;
1280 s->handshake_func=meth->ssl_accept;
1285 int SSL_get_error(s,i)
1292 if (i > 0) return(SSL_ERROR_NONE);
1294 if (ERR_peek_error() != 0)
1295 return(SSL_ERROR_SSL);
1297 if ((i < 0) && SSL_want_read(s))
1299 bio=SSL_get_rbio(s);
1300 if (BIO_should_read(bio))
1301 return(SSL_ERROR_WANT_READ);
1302 else if (BIO_should_write(bio))
1303 return(SSL_ERROR_WANT_WRITE);
1304 else if (BIO_should_io_special(bio))
1306 reason=BIO_get_retry_reason(bio);
1307 if (reason == BIO_RR_CONNECT)
1308 return(SSL_ERROR_WANT_CONNECT);
1310 return(SSL_ERROR_SYSCALL); /* unknown */
1314 if ((i < 0) && SSL_want_write(s))
1316 bio=SSL_get_wbio(s);
1317 if (BIO_should_write(bio))
1318 return(SSL_ERROR_WANT_WRITE);
1319 else if (BIO_should_read(bio))
1320 return(SSL_ERROR_WANT_READ);
1321 else if (BIO_should_io_special(bio))
1323 reason=BIO_get_retry_reason(bio);
1324 if (reason == BIO_RR_CONNECT)
1325 return(SSL_ERROR_WANT_CONNECT);
1327 return(SSL_ERROR_SYSCALL);
1330 if ((i < 0) && SSL_want_x509_lookup(s))
1332 return(SSL_ERROR_WANT_X509_LOOKUP);
1337 if (s->version == SSL2_VERSION)
1339 /* assume it is the socket being closed */
1340 return(SSL_ERROR_ZERO_RETURN);
1344 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1345 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
1346 return(SSL_ERROR_ZERO_RETURN);
1349 return(SSL_ERROR_SYSCALL);
1352 int SSL_do_handshake(s)
1357 if (s->handshake_func == NULL)
1359 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
1363 s->method->ssl_renegotiate_check(s);
1365 if (SSL_in_init(s) || SSL_in_before(s))
1367 ret=s->handshake_func(s);
1372 /* For the next 2 functions, SSL_clear() sets shutdown and so
1373 * one of these calls will reset it */
1374 void SSL_set_accept_state(s)
1378 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
1379 s->handshake_func=s->method->ssl_accept;
1380 /* clear the current cipher */
1381 ssl_clear_cipher_ctx(s);
1384 void SSL_set_connect_state(s)
1388 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
1389 s->handshake_func=s->method->ssl_connect;
1390 /* clear the current cipher */
1391 ssl_clear_cipher_ctx(s);
1394 int ssl_undefined_function(s)
1397 SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1401 SSL_METHOD *ssl_bad_method(ver)
1404 SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1408 char *SSL_get_version(s)
1411 if (s->version == TLS1_VERSION)
1413 else if (s->version == SSL3_VERSION)
1415 else if (s->version == SSL2_VERSION)
1429 if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) return(NULL);
1431 /* This copies version, session-id, SSL_METHOD and 'cert' */
1432 SSL_copy_session_id(ret,s);
1434 SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
1435 SSL_set_verify(ret,SSL_get_verify_mode(s),
1436 SSL_get_verify_callback(s));
1438 SSL_set_info_callback(ret,SSL_get_info_callback(s));
1440 ret->debug=s->debug;
1441 ret->options=s->options;
1443 /* copy app data, a little dangerous perhaps */
1444 if (!CRYPTO_dup_ex_data(ssl_meth,&ret->ex_data,&s->ex_data))
1447 /* setup rbio, and wbio */
1448 if (s->rbio != NULL)
1450 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
1453 if (s->wbio != NULL)
1455 if (s->wbio != s->rbio)
1457 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
1461 ret->wbio=ret->rbio;
1464 /* dup the cipher_list and cipher_list_by_id stacks */
1465 if (s->cipher_list != NULL)
1467 if ((ret->cipher_list=sk_dup(s->cipher_list)) == NULL)
1470 if (s->cipher_list_by_id != NULL)
1471 if ((ret->cipher_list_by_id=sk_dup(s->cipher_list_by_id))
1475 /* Dup the client_CA list */
1476 if (s->client_CA != NULL)
1478 if ((sk=sk_dup(s->client_CA)) == NULL) goto err;
1480 for (i=0; i<sk_num(sk); i++)
1482 xn=(X509_NAME *)sk_value(sk,i);
1483 if ((sk_value(sk,i)=(char *)X509_NAME_dup(xn)) == NULL)
1491 ret->shutdown=s->shutdown;
1492 ret->state=s->state;
1493 ret->handshake_func=s->handshake_func;
1498 if (ret != NULL) SSL_free(ret);
1504 void ssl_clear_cipher_ctx(s)
1507 if (s->enc_read_ctx != NULL)
1509 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
1510 Free(s->enc_read_ctx);
1511 s->enc_read_ctx=NULL;
1513 if (s->enc_write_ctx != NULL)
1515 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
1516 Free(s->enc_write_ctx);
1517 s->enc_write_ctx=NULL;
1521 /* Fix this function so that it takes an optional type parameter */
1522 X509 *SSL_get_certificate(s)
1525 if (s->cert != NULL)
1526 return(s->cert->key->x509);
1531 /* Fix this function so that it takes an optional type parameter */
1532 EVP_PKEY *SSL_get_privatekey(s)
1535 if (s->cert != NULL)
1536 return(s->cert->key->privatekey);
1541 SSL_CIPHER *SSL_get_current_cipher(s)
1544 if ((s->session != NULL) && (s->session->cipher != NULL))
1545 return(s->session->cipher);
1549 int ssl_init_wbio_buffer(s,push)
1555 if (s->bbio == NULL)
1557 bbio=BIO_new(BIO_f_buffer());
1558 if (bbio == NULL) return(0);
1564 if (s->bbio == s->wbio)
1565 s->wbio=BIO_pop(s->wbio);
1568 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
1569 if (!BIO_set_read_buffer_size(bbio,1))
1571 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
1576 if (s->wbio != bbio)
1577 s->wbio=BIO_push(bbio,s->wbio);
1581 if (s->wbio == bbio)
1582 s->wbio=BIO_pop(bbio);
1587 void SSL_CTX_set_quiet_shutdown(ctx,mode)
1591 ctx->quiet_shutdown=mode;
1594 int SSL_CTX_get_quiet_shutdown(ctx)
1597 return(ctx->quiet_shutdown);
1600 void SSL_set_quiet_shutdown(s,mode)
1604 s->quiet_shutdown=mode;
1607 int SSL_get_quiet_shutdown(s)
1610 return(s->quiet_shutdown);
1613 void SSL_set_shutdown(s,mode)
1620 int SSL_get_shutdown(s)
1623 return(s->shutdown);
1632 SSL_CTX *SSL_get_SSL_CTX(ssl)
1639 int SSL_CTX_set_default_verify_paths(ctx)
1642 return(X509_STORE_set_default_paths(ctx->cert_store));
1645 int SSL_CTX_load_verify_locations(ctx,CAfile,CApath)
1650 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
1654 void SSL_set_info_callback(ssl,cb)
1658 ssl->info_callback=cb;
1661 void (*SSL_get_info_callback(ssl))()
1664 return((void (*)())ssl->info_callback);
1673 void SSL_set_verify_result(ssl,arg)
1677 ssl->verify_result=arg;
1680 long SSL_get_verify_result(ssl)
1683 return(ssl->verify_result);
1686 int SSL_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
1691 void (*free_func)();
1694 return(CRYPTO_get_ex_new_index(ssl_meth_num-1,
1695 &ssl_meth,argl,argp,new_func,dup_func,free_func));
1698 int SSL_set_ex_data(s,idx,arg)
1703 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1706 char *SSL_get_ex_data(s,idx)
1710 return(CRYPTO_get_ex_data(&s->ex_data,idx));
1713 int SSL_CTX_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
1718 void (*free_func)();
1721 return(CRYPTO_get_ex_new_index(ssl_ctx_meth_num-1,
1722 &ssl_ctx_meth,argl,argp,new_func,dup_func,free_func));
1725 int SSL_CTX_set_ex_data(s,idx,arg)
1730 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1733 char *SSL_CTX_get_ex_data(s,idx)
1737 return(CRYPTO_get_ex_data(&s->ex_data,idx));
1746 #if defined(_WINDLL) && defined(WIN16)
1747 #include "../crypto/bio/bss_file.c"