This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / ssl / ssl_lib.c
1 /* ssl/ssl_lib.c */
2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
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.
8  * 
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).
15  * 
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.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
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)"
40  * 
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
51  * SUCH DAMAGE.
52  * 
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.]
57  */
58
59 #include <stdio.h>
60 #include "objects.h"
61 #include "lhash.h"
62 #include "ssl_locl.h"
63
64 #ifndef NOPROTO
65 static unsigned long conn_hash(SSL_SESSION *a);
66 #else
67 static unsigned long conn_hash();
68 #endif
69
70 char *SSL_version_str="SSLeay 0.8.1b 29-Jun-1998";
71
72 void SSL_clear(s)
73 SSL *s;
74         {
75         int state;
76
77         if (s->method == NULL) return;
78
79         s->error=0;
80         s->hit=0;
81
82         /* This is set if we are doing dynamic renegotiation so keep
83          * the old cipher.  It is sort of a SSL_clear_lite :-) */
84         if (s->new_session) return;
85
86         state=s->state; /* Keep to check if we throw away the session-id */
87         s->type=0;
88
89         s->version=s->method->version;
90         s->rwstate=SSL_NOTHING;
91         s->state=SSL_ST_BEFORE;
92         s->rstate=SSL_ST_READ_HEADER;
93         s->read_ahead=s->ctx->default_read_ahead;
94
95 /*      s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); */
96
97         if (s->init_buf != NULL)
98                 {
99                 BUF_MEM_free(s->init_buf);
100                 s->init_buf=NULL;
101                 }
102
103         ssl_clear_cipher_ctx(s);
104
105         if (ssl_clear_bad_session(s))
106                 {
107                 SSL_SESSION_free(s->session);
108                 s->session=NULL;
109                 }
110
111         s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
112         s->first_packet=0;
113
114         s->method->ssl_clear(s);
115         }
116
117 /* Used to change an SSL_CTXs default SSL method type */
118 int SSL_CTX_set_ssl_version(ctx,meth)
119 SSL_CTX *ctx;
120 SSL_METHOD *meth;
121         {
122         STACK *sk;
123
124         ctx->method=meth;
125
126         sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
127                 &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
128         if ((sk == NULL) || (sk_num(sk) <= 0))
129                 {
130                 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
131                 return(0);
132                 }
133         return(1);
134         }
135
136 SSL *SSL_new(ctx)
137 SSL_CTX *ctx;
138         {
139         SSL *s;
140
141         if (ctx == NULL)
142                 {
143                 SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
144                 return(NULL);
145                 }
146         if (ctx->method == NULL)
147                 {
148                 SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
149                 return(NULL);
150                 }
151
152         s=(SSL *)Malloc(sizeof(SSL));
153         if (s == NULL) goto err;
154         memset(s,0,sizeof(SSL));
155
156         if (ctx->default_cert != NULL)
157                 {
158                 CRYPTO_add(&ctx->default_cert->references,1,
159                         CRYPTO_LOCK_SSL_CERT);
160                 s->cert=ctx->default_cert;
161                 }
162         else
163                 s->cert=NULL;
164         s->verify_mode=ctx->default_verify_mode;
165         s->verify_callback=ctx->default_verify_callback;
166         CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
167         s->ctx=ctx;
168
169         s->verify_result=X509_V_OK;
170
171         s->method=ctx->method;
172
173         if (!s->method->ssl_new(s))
174                 {
175                 SSL_CTX_free(ctx);
176                 Free(s);
177                 goto err;
178                 }
179
180         s->quiet_shutdown=ctx->quiet_shutdown;
181         SSL_clear(s);
182         return(s);
183 err:
184         SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
185         return(NULL);
186         }
187
188 void SSL_free(s)
189 SSL *s;
190         {
191         if (s->bbio != NULL)
192                 {
193                 /* If the buffering BIO is in place, pop it off */
194                 if (s->bbio == s->wbio)
195                         {
196                         s->wbio=BIO_pop(s->wbio);
197                         }
198                 BIO_free(s->bbio);
199                 }
200         if (s->rbio != NULL)
201                 BIO_free_all(s->rbio);
202         if ((s->wbio != NULL) && (s->wbio != s->rbio))
203                 BIO_free_all(s->wbio);
204
205         if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
206
207         /* add extra stuff */
208         if (s->cipher_list != NULL) sk_free(s->cipher_list);
209         if (s->cipher_list_by_id != NULL) sk_free(s->cipher_list_by_id);
210
211         /* Make the next call work :-) */
212         if (s->session != NULL)
213                 {
214                 ssl_clear_bad_session(s);
215                 SSL_SESSION_free(s->session);
216                 }
217
218         ssl_clear_cipher_ctx(s);
219
220         if (s->cert != NULL) ssl_cert_free(s->cert);
221         /* Free up if allocated */
222
223         if (s->ctx) SSL_CTX_free(s->ctx);
224
225         if (s->client_CA != NULL)
226                 sk_pop_free(s->client_CA,X509_NAME_free);
227
228         if (s->method != NULL) s->method->ssl_free(s);
229
230         Free((char *)s);
231         }
232
233 void SSL_set_bio(s, rbio,wbio)
234 SSL *s;
235 BIO *rbio;
236 BIO *wbio;
237         {
238         /* If the output buffering BIO is still in place, remove it
239          */
240         if (s->bbio != NULL)
241                 {
242                 if (s->wbio == s->bbio)
243                         {
244                         s->wbio=s->wbio->next_bio;
245                         s->bbio->next_bio=NULL;
246                         }
247                 }
248         if ((s->rbio != NULL) && (s->rbio != rbio))
249                 BIO_free_all(s->rbio);
250         if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
251                 BIO_free_all(s->wbio);
252         s->rbio=rbio;
253         s->wbio=wbio;
254         }
255
256 BIO *SSL_get_rbio(s)
257 SSL *s;
258         { return(s->rbio); }
259
260 BIO *SSL_get_wbio(s)
261 SSL *s;
262         { return(s->wbio); }
263
264 int SSL_get_fd(s)
265 SSL *s;
266         {
267         int ret= -1;
268         BIO *b,*r;
269
270         b=SSL_get_rbio(s);
271         r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
272         if (r != NULL)
273                 BIO_get_fd(r,&ret);
274         return(ret);
275         }
276
277 #ifndef NO_SOCK
278 int SSL_set_fd(s, fd)
279 SSL *s;
280 int fd;
281         {
282         int ret=0;
283         BIO *bio=NULL;
284
285         bio=BIO_new(BIO_s_socket());
286
287         if (bio == NULL)
288                 {
289                 SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
290                 goto err;
291                 }
292         BIO_set_fd(bio,fd,BIO_NOCLOSE);
293         SSL_set_bio(s,bio,bio);
294         ret=1;
295 err:
296         return(ret);
297         }
298
299 int SSL_set_wfd(s, fd)
300 SSL *s;
301 int fd;
302         {
303         int ret=0;
304         BIO *bio=NULL;
305
306         bio=BIO_new(BIO_s_socket());
307
308         if (bio == NULL)
309                 { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
310         BIO_set_fd(bio,fd,BIO_NOCLOSE);
311         SSL_set_bio(s,SSL_get_rbio(s),bio);
312         ret=1;
313 err:
314         return(ret);
315         }
316
317 int SSL_set_rfd(s, fd)
318 SSL *s;
319 int fd;
320         {
321         int ret=0;
322         BIO *bio=NULL;
323
324         bio=BIO_new(BIO_s_socket());
325
326         if (bio == NULL)
327                 {
328                 SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
329                 goto err;
330                 }
331         BIO_set_fd(bio,fd,BIO_NOCLOSE);
332         SSL_set_bio(s,bio,SSL_get_wbio(s));
333         ret=1;
334 err:
335         return(ret);
336         }
337 #endif
338
339 int SSL_get_verify_mode(s)
340 SSL *s;
341         {
342         return(s->verify_mode);
343         }
344
345 int (*SSL_get_verify_callback(s))()
346 SSL *s;
347         {
348         return(s->verify_callback);
349         }
350
351 int SSL_CTX_get_verify_mode(ctx)
352 SSL_CTX *ctx;
353         {
354         return(ctx->default_verify_mode);
355         }
356
357 int (*SSL_CTX_get_verify_callback(ctx))()
358 SSL_CTX *ctx;
359         {
360         return(ctx->default_verify_callback);
361         }
362
363 void SSL_set_verify(s, mode, callback)
364 SSL *s;
365 int mode;
366 int (*callback)();
367         {
368         s->verify_mode=mode;
369         if (callback != NULL)
370                 s->verify_callback=callback;
371         }
372
373 void SSL_set_read_ahead(s, yes)
374 SSL *s;
375 int yes;
376         {
377         s->read_ahead=yes;
378         }
379
380 int SSL_get_read_ahead(s)
381 SSL *s;
382         {
383         return(s->read_ahead);
384         }
385
386 int SSL_pending(s)
387 SSL *s;
388         {
389         return(s->method->ssl_pending(s));
390         }
391
392 X509 *SSL_get_peer_certificate(s)
393 SSL *s;
394         {
395         X509 *r;
396         
397         if ((s == NULL) || (s->session == NULL))
398                 r=NULL;
399         else
400                 r=s->session->peer;
401
402         if (r == NULL) return(r);
403
404         CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
405
406         return(r);
407         }
408
409 STACK *SSL_get_peer_cert_chain(s)
410 SSL *s;
411         {
412         STACK *r;
413         
414         if ((s == NULL) || (s->session == NULL) || (s->session->cert == NULL))
415                 r=NULL;
416         else
417                 r=s->session->cert->cert_chain;
418
419         return(r);
420         }
421
422 /* Now in theory, since the calling process own 't' it should be safe to
423  * modify.  We need to be able to read f without being hassled */
424 void SSL_copy_session_id(t,f)
425 SSL *t,*f;
426         {
427         CERT *tmp;
428
429         /* Do we need to to SSL locking? */
430         SSL_set_session(t,SSL_get_session(f));
431
432         /* what if we are setup as SSLv2 but want to talk SSLv3 or
433          * vice-versa */
434         if (t->method != f->method)
435                 {
436                 t->method->ssl_free(t); /* cleanup current */
437                 t->method=f->method;    /* change method */
438                 t->method->ssl_new(t);  /* setup new */
439                 }
440
441         tmp=t->cert;
442         if (f->cert != NULL)
443                 {
444                 CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
445                 t->cert=f->cert;
446                 }
447         else
448                 t->cert=NULL;
449         if (tmp != NULL) ssl_cert_free(tmp);
450         }
451
452 int SSL_CTX_check_private_key(ctx)
453 SSL_CTX *ctx;
454         {
455         if (    (ctx == NULL) ||
456                 (ctx->default_cert == NULL) ||
457                 (ctx->default_cert->key->x509 == NULL))
458                 {
459                 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
460                 return(0);
461                 }
462         if      (ctx->default_cert->key->privatekey == NULL)
463                 {
464                 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
465                 return(0);
466                 }
467         return(X509_check_private_key(ctx->default_cert->key->x509, ctx->default_cert->key->privatekey));
468         }
469
470 int SSL_check_private_key(ssl)
471 SSL *ssl;
472         {
473         if (ssl == NULL)
474                 {
475                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
476                 return(0);
477                 }
478         if (ssl->cert == NULL)
479                 return(SSL_CTX_check_private_key(ssl->ctx));
480         if (ssl->cert->key->x509 == NULL)
481                 {
482                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
483                 return(0);
484                 }
485         if (ssl->cert->key->privatekey == NULL)
486                 {
487                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
488                 return(0);
489                 }
490         return(X509_check_private_key(ssl->cert->key->x509,
491                 ssl->cert->key->privatekey));
492         }
493
494 int SSL_accept(s)
495 SSL *s;
496         {
497         return(s->method->ssl_accept(s));
498         }
499
500 int SSL_connect(s)
501 SSL *s;
502         {
503         return(s->method->ssl_connect(s));
504         }
505
506 long SSL_get_default_timeout(s)
507 SSL *s;
508         {
509         return(s->method->get_timeout());
510         }
511
512 int SSL_read(s,buf,num)
513 SSL *s;
514 char *buf;
515 int num;
516         {
517         if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
518                 {
519                 s->rwstate=SSL_NOTHING;
520                 return(0);
521                 }
522         return(s->method->ssl_read(s,buf,num));
523         }
524
525 int SSL_peek(s,buf,num)
526 SSL *s;
527 char *buf;
528 int num;
529         {
530         if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
531                 {
532                 return(0);
533                 }
534         return(s->method->ssl_peek(s,buf,num));
535         }
536
537 int SSL_write(s,buf,num)
538 SSL *s;
539 char *buf;
540 int num;
541         {
542         if (s->shutdown & SSL_SENT_SHUTDOWN)
543                 {
544                 s->rwstate=SSL_NOTHING;
545                 SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
546                 return(-1);
547                 }
548         return(s->method->ssl_write(s,buf,num));
549         }
550
551 int SSL_shutdown(s)
552 SSL *s;
553         {
554         if ((s != NULL) && !SSL_in_init(s))
555                 return(s->method->ssl_shutdown(s));
556         else
557                 return(1);
558         }
559
560 int SSL_renegotiate(s)
561 SSL *s;
562         {
563         return(s->method->ssl_renegotiate(s));
564         }
565
566 long SSL_ctrl(s,cmd,larg,parg)
567 SSL *s;
568 int cmd;
569 long larg;
570 char *parg;
571         {
572         return(s->method->ssl_ctrl(s,cmd,larg,parg));
573         }
574
575 long SSL_CTX_ctrl(ctx,cmd,larg,parg)
576 SSL_CTX *ctx;
577 int cmd;
578 long larg;
579 char *parg;
580         {
581         return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
582         }
583
584 int ssl_cipher_id_cmp(a,b)
585 SSL_CIPHER *a,*b;
586         {
587         long l;
588
589         l=a->id-b->id;
590         if (l == 0L)
591                 return(0);
592         else
593                 return((l > 0)?1:-1);
594         }
595
596 int ssl_cipher_ptr_id_cmp(ap,bp)
597 SSL_CIPHER **ap,**bp;
598         {
599         long l;
600
601         l=(*ap)->id-(*bp)->id;
602         if (l == 0L)
603                 return(0);
604         else
605                 return((l > 0)?1:-1);
606         }
607
608 /* return a STACK of the ciphers available for the SSL and in order of
609  * preference */
610 STACK *SSL_get_ciphers(s)
611 SSL *s;
612         {
613         if ((s != NULL) && (s->cipher_list != NULL))
614                 {
615                 return(s->cipher_list);
616                 }
617         else if ((s != NULL) && (s->ctx != NULL) &&
618                 (s->ctx->cipher_list != NULL))
619                 {
620                 return(s->ctx->cipher_list);
621                 }
622         return(NULL);
623         }
624
625 /* return a STACK of the ciphers available for the SSL and in order of
626  * algorithm id */
627 STACK *ssl_get_ciphers_by_id(s)
628 SSL *s;
629         {
630         if ((s != NULL) && (s->cipher_list_by_id != NULL))
631                 {
632                 return(s->cipher_list_by_id);
633                 }
634         else if ((s != NULL) && (s->ctx != NULL) &&
635                 (s->ctx->cipher_list_by_id != NULL))
636                 {
637                 return(s->ctx->cipher_list_by_id);
638                 }
639         return(NULL);
640         }
641
642 /* The old interface to get the same thing as SSL_get_ciphers() */
643 char *SSL_get_cipher_list(s,n)
644 SSL *s;
645 int n;
646         {
647         SSL_CIPHER *c;
648         STACK *sk;
649
650         if (s == NULL) return(NULL);
651         sk=SSL_get_ciphers(s);
652         if ((sk == NULL) || (sk_num(sk) <= n))
653                 return(NULL);
654         c=(SSL_CIPHER *)sk_value(sk,n);
655         if (c == NULL) return(NULL);
656         return(c->name);
657         }
658
659 /* specify the ciphers to be used by defaut by the SSL_CTX */
660 int SSL_CTX_set_cipher_list(ctx,str)
661 SSL_CTX *ctx;
662 char *str;
663         {
664         STACK *sk;
665         
666         sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
667                 &ctx->cipher_list_by_id,str);
668 /* XXXX */
669         return((sk == NULL)?0:1);
670         }
671
672 /* specify the ciphers to be used by the SSL */
673 int SSL_set_cipher_list(s, str)
674 SSL *s;
675 char *str;
676         {
677         STACK *sk;
678         
679         sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
680                 &s->cipher_list_by_id,str);
681 /* XXXX */
682         return((sk == NULL)?0:1);
683         }
684
685 /* works well for SSLv2, not so good for SSLv3 */
686 char *SSL_get_shared_ciphers(s,buf,len)
687 SSL *s;
688 char *buf;
689 int len;
690         {
691         char *p,*cp;
692         STACK *sk;
693         SSL_CIPHER *c;
694         int i;
695
696         if ((s->session == NULL) || (s->session->ciphers == NULL) ||
697                 (len < 2))
698                 return(NULL);
699
700         p=buf;
701         sk=s->session->ciphers;
702         len--;
703         for (i=0; i<sk_num(sk); i++)
704                 {
705                 c=(SSL_CIPHER *)sk_value(sk,i);
706                 for (cp=c->name; *cp; )
707                         {
708                         if (--len == 0)
709                                 {
710                                 *p='\0';
711                                 return(buf);
712                                 }
713                         else
714                                 *(p++)= *(cp++);
715                         }
716                 *(p++)=':';
717                 }
718         p[-1]='\0';
719         return(buf);
720         }
721
722 int ssl_cipher_list_to_bytes(s,sk,p)
723 SSL *s;
724 STACK *sk;
725 unsigned char *p;
726         {
727         int i,j=0;
728         SSL_CIPHER *c;
729         unsigned char *q;
730
731         if (sk == NULL) return(0);
732         q=p;
733
734         for (i=0; i<sk_num(sk); i++)
735                 {
736                 c=(SSL_CIPHER *)sk_value(sk,i);
737                 j=ssl_put_cipher_by_char(s,c,p);
738                 p+=j;
739                 }
740         return(p-q);
741         }
742
743 STACK *ssl_bytes_to_cipher_list(s,p,num,skp)
744 SSL *s;
745 unsigned char *p;
746 int num;
747 STACK **skp;
748         {
749         SSL_CIPHER *c;
750         STACK *sk;
751         int i,n;
752
753         n=ssl_put_cipher_by_char(s,NULL,NULL);
754         if ((num%n) != 0)
755                 {
756                 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
757                 return(NULL);
758                 }
759         if ((skp == NULL) || (*skp == NULL))
760                 sk=sk_new(NULL); /* change perhaps later */
761         else
762                 {
763                 sk= *skp;
764                 sk_zero(sk);
765                 }
766
767         for (i=0; i<num; i+=n)
768                 {
769                 c=ssl_get_cipher_by_char(s,p);
770                 p+=n;
771                 if (c != NULL)
772                         {
773                         if (!sk_push(sk,(char *)c))
774                                 {
775                                 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
776                                 goto err;
777                                 }
778                         }
779                 }
780
781         if (skp != NULL)
782                 *skp=sk;
783         return(sk);
784 err:
785         if ((skp == NULL) || (*skp == NULL))
786                 sk_free(sk);
787         return(NULL);
788         }
789
790 static unsigned long conn_hash(a)
791 SSL_SESSION *a;
792         {
793         unsigned long l;
794
795         l=      (a->session_id[0]     )|(a->session_id[1]<< 8L)|
796                 (a->session_id[1]<<16L)|(a->session_id[2]<<24L);
797         return(l);
798         }
799
800 static int session_cmp(a, b)
801 SSL_SESSION *a;
802 SSL_SESSION *b;
803         {
804         int i;
805
806         i=a->session_id_length - b->session_id_length;
807         if (i == 0)
808                 return(memcmp(a->session_id,b->session_id,
809                         a->session_id_length));
810         else    return(1);
811         }
812
813 SSL_CTX *SSL_CTX_new(meth)
814 SSL_METHOD *meth;
815         {
816         SSL_CTX *ret;
817         
818         if (meth == NULL)
819                 {
820                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
821                 return(NULL);
822                 }
823         ret=(SSL_CTX *)Malloc(sizeof(SSL_CTX));
824         if (ret == NULL)
825                 goto err;
826
827         memset(ret,0,sizeof(SSL_CTX));
828
829         ret->method=meth;
830
831         ret->cert_store=NULL;
832         ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
833
834         /* We take the system default */
835         ret->session_timeout=meth->get_timeout();
836
837         ret->new_session_cb=NULL;
838         ret->remove_session_cb=NULL;
839         ret->get_session_cb=NULL;
840
841         ret->sess_connect=0;
842         ret->sess_connect_good=0;
843         ret->sess_accept=0;
844         ret->sess_accept_good=0;
845         ret->sess_miss=0;
846         ret->sess_timeout=0;
847         ret->sess_hit=0;
848         ret->sess_cb_hit=0;
849
850         ret->references=1;
851         ret->quiet_shutdown=0;
852
853 /*      ret->cipher=NULL;*/
854 /*      ret->s2->challenge=NULL;
855         ret->master_key=NULL;
856         ret->key_arg=NULL;
857         ret->s2->conn_id=NULL; */
858
859         ret->info_callback=NULL;
860
861         ret->app_verify_callback=NULL;
862         ret->app_verify_arg=NULL;
863
864         ret->default_read_ahead=0;
865         ret->default_verify_mode=SSL_VERIFY_NONE;
866         ret->default_verify_callback=NULL;
867         if ((ret->default_cert=ssl_cert_new()) == NULL)
868                 goto err;
869
870         ret->default_passwd_callback=NULL;
871         ret->client_cert_cb=NULL;
872
873         ret->sessions=lh_new(conn_hash,session_cmp);
874         if (ret->sessions == NULL) goto err;
875         ret->cert_store=X509_STORE_new();
876         if (ret->cert_store == NULL) goto err;
877
878         ssl_create_cipher_list(ret->method,
879                 &ret->cipher_list,&ret->cipher_list_by_id,
880                 SSL_DEFAULT_CIPHER_LIST);
881         if ((ret->cipher_list == NULL) || (sk_num(ret->cipher_list) <= 0))
882                 {
883                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
884                 goto err2;
885                 }
886
887         if ((ret->client_CA=sk_new_null()) == NULL)
888                 goto err;
889
890         return(ret);
891 err:
892         SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
893 err2:
894         if (ret != NULL) SSL_CTX_free(ret);
895         return(NULL);
896         }
897
898 void SSL_CTX_free(a)
899 SSL_CTX *a;
900         {
901         int i;
902
903         if (a == NULL) return;
904
905         i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
906         if (i > 0) return;
907 #ifdef REF_CHECK
908         if (i < 0)
909                 {
910                 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
911                 abort(); /* ok */
912                 }
913 #endif
914
915         if (a->sessions != NULL)
916                 {
917                 SSL_CTX_flush_sessions(a,0);
918                 lh_free(a->sessions);
919                 }
920         if (a->cert_store != NULL)
921                 X509_STORE_free(a->cert_store);
922         if (a->cipher_list != NULL)
923                 sk_free(a->cipher_list);
924         if (a->cipher_list_by_id != NULL)
925                 sk_free(a->cipher_list_by_id);
926         if (a->default_cert != NULL)
927                 ssl_cert_free(a->default_cert);
928         if (a->client_CA != NULL)
929                 sk_pop_free(a->client_CA,X509_NAME_free);
930         Free((char *)a);
931         }
932
933 void SSL_CTX_set_default_passwd_cb(ctx,cb)
934 SSL_CTX *ctx;
935 int (*cb)();
936         {
937         ctx->default_passwd_callback=cb;
938         }
939
940 void SSL_CTX_set_cert_verify_cb(ctx,cb,arg)
941 SSL_CTX *ctx;
942 int (*cb)();
943 char *arg;
944         {
945         ctx->app_verify_callback=cb;
946         ctx->app_verify_arg=arg;
947         }
948
949 void SSL_CTX_set_verify(ctx,mode,cb)
950 SSL_CTX *ctx;
951 int mode;
952 int (*cb)();
953         {
954         ctx->default_verify_mode=mode;
955         ctx->default_verify_callback=cb;
956         /* This needs cleaning up EAY EAY EAY */
957         X509_STORE_set_verify_cb_func(ctx->cert_store,cb);
958         }
959
960 void ssl_set_cert_masks(c)
961 CERT *c;
962         {
963         CERT_PKEY *cpk;
964         int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
965         int rsa_enc_export,dh_rsa_export,dh_dsa_export;
966         int rsa_tmp_export,dh_tmp_export;
967         unsigned long mask,emask;
968
969         if ((c == NULL) || (c->valid)) return;
970
971 #ifndef NO_RSA
972         rsa_tmp=((c->rsa_tmp != NULL) || (c->rsa_tmp_cb != NULL))?1:0;
973         rsa_tmp_export=((c->rsa_tmp_cb != NULL) ||
974                 (rsa_tmp && (RSA_size(c->rsa_tmp)*8 <= 512)))?1:0;
975 #else
976         rsa_tmp=rsa_tmp_export=0;
977 #endif
978 #ifndef NO_DH
979         dh_tmp=((c->dh_tmp != NULL) || (c->dh_tmp_cb != NULL))?1:0;
980         dh_tmp_export=((c->dh_tmp_cb != NULL) ||
981                 (dh_tmp && (DH_size(c->dh_tmp)*8 <= 512)))?1:0;
982 #else
983         dh_tmp=dh_tmp_export=0;
984 #endif
985
986         cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
987         rsa_enc= ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
988         rsa_enc_export=(rsa_enc && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
989         cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
990         rsa_sign=((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
991         cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
992         dsa_sign=((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
993         cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
994         dh_rsa=  ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
995         dh_rsa_export=(dh_rsa && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
996         cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
997 /* FIX THIS EAY EAY EAY */
998         dh_dsa=  ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
999         dh_dsa_export=(dh_dsa && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1000
1001         mask=0;
1002         emask=0;
1003
1004 #ifdef CIPHER_DEBUG
1005         printf("rt=%d dht=%d re=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1006                 rsa_tmp,dh_tmp,
1007                 rsa_enc,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1008 #endif
1009
1010         if (rsa_enc || (rsa_tmp && rsa_sign))
1011                 mask|=SSL_kRSA;
1012         if (rsa_enc_export || (rsa_tmp_export && rsa_sign))
1013                 emask|=SSL_kRSA;
1014
1015 #if 0
1016         /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1017         if (    (dh_tmp || dh_rsa || dh_dsa) && 
1018                 (rsa_enc || rsa_sign || dsa_sign))
1019                 mask|=SSL_kEDH;
1020         if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1021                 (rsa_enc || rsa_sign || dsa_sign))
1022                 emask|=SSL_kEDH;
1023 #endif
1024
1025         if (dh_tmp_export) 
1026                 emask|=SSL_kEDH;
1027
1028         if (dh_tmp)
1029                 mask|=SSL_kEDH;
1030
1031         if (dh_rsa) mask|=SSL_kDHr;
1032         if (dh_rsa_export) emask|=SSL_kDHr;
1033
1034         if (dh_dsa) mask|=SSL_kDHd;
1035         if (dh_dsa_export) emask|=SSL_kDHd;
1036
1037         if (rsa_enc || rsa_sign)
1038                 {
1039                 mask|=SSL_aRSA;
1040                 emask|=SSL_aRSA;
1041                 }
1042
1043         if (dsa_sign)
1044                 {
1045                 mask|=SSL_aDSS;
1046                 emask|=SSL_aDSS;
1047                 }
1048
1049 #ifdef SSL_ALLOW_ADH
1050         mask|=SSL_aNULL;
1051         emask|=SSL_aNULL;
1052 #endif
1053
1054         c->mask=mask;
1055         c->export_mask=emask;
1056         c->valid=1;
1057         }
1058
1059 /* THIS NEEDS CLEANING UP */
1060 X509 *ssl_get_server_send_cert(s)
1061 SSL *s;
1062         {
1063         unsigned long alg,mask,kalg;
1064         CERT *c;
1065         int i,export;
1066
1067         c=s->cert;
1068         ssl_set_cert_masks(c);
1069         alg=s->s3->tmp.new_cipher->algorithms;
1070         export=(alg & SSL_EXPORT)?1:0;
1071         mask=(export)?c->export_mask:c->mask;
1072         kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1073
1074         if      (kalg & SSL_kDHr)
1075                 i=SSL_PKEY_DH_RSA;
1076         else if (kalg & SSL_kDHd)
1077                 i=SSL_PKEY_DH_DSA;
1078         else if (kalg & SSL_aDSS)
1079                 i=SSL_PKEY_DSA_SIGN;
1080         else if (kalg & SSL_aRSA)
1081                 {
1082                 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1083                         i=SSL_PKEY_RSA_SIGN;
1084                 else
1085                         i=SSL_PKEY_RSA_ENC;
1086                 }
1087         else /* if (kalg & SSL_aNULL) */
1088                 {
1089                 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,SSL_R_INTERNAL_ERROR);
1090                 return(NULL);
1091                 }
1092         if (c->pkeys[i].x509 == NULL) return(NULL);
1093         return(c->pkeys[i].x509);
1094         }
1095
1096 EVP_PKEY *ssl_get_sign_pkey(s,cipher)
1097 SSL *s;
1098 SSL_CIPHER *cipher;
1099         {
1100         unsigned long alg;
1101         CERT *c;
1102
1103         alg=cipher->algorithms;
1104         c=s->cert;
1105
1106         if ((alg & SSL_aDSS) &&
1107                 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
1108                 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
1109         else if (alg & SSL_aRSA)
1110                 {
1111                 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
1112                         return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
1113                 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
1114                         return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
1115                 else
1116                         return(NULL);
1117                 }
1118         else /* if (alg & SSL_aNULL) */
1119                 {
1120                 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,SSL_R_INTERNAL_ERROR);
1121                 return(NULL);
1122                 }
1123         }
1124
1125 void ssl_update_cache(s,mode)
1126 SSL *s;
1127 int mode;
1128         {
1129         if ((s->ctx->session_cache_mode & mode)
1130                 && (!s->hit)
1131                 && SSL_CTX_add_session(s->ctx,s->session)
1132                 && (s->ctx->new_session_cb != NULL))
1133                 {
1134                 CRYPTO_add(&s->session->references,1,
1135                         CRYPTO_LOCK_SSL_SESSION);
1136                 if (!s->ctx->new_session_cb(s,s->session))
1137                         SSL_SESSION_free(s->session);
1138                 }
1139
1140         /* auto flush every 255 connections */
1141         if ((!(s->ctx->session_cache_mode &
1142                 SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
1143                 ((s->ctx->sess_connect_good & 0xff) == 0))
1144                 SSL_CTX_flush_sessions(s->ctx,time(NULL));
1145         }
1146
1147 SSL_METHOD *SSL_get_ssl_method(s)
1148 SSL *s;
1149         {
1150         return(s->method);
1151         }
1152
1153 int SSL_set_ssl_method(s,meth)
1154 SSL *s;
1155 SSL_METHOD *meth;
1156         {
1157         int conn= -1;
1158         int ret=1;
1159
1160         if (s->method != meth)
1161                 {
1162                 if (s->handshake_func != NULL)
1163                         conn=(s->handshake_func == s->method->ssl_connect);
1164
1165                 if (s->method->version == meth->version)
1166                         s->method=meth;
1167                 else
1168                         {
1169                         s->method->ssl_free(s);
1170                         s->method=meth;
1171                         ret=s->method->ssl_new(s);
1172                         }
1173
1174                 if (conn == 1)
1175                         s->handshake_func=meth->ssl_connect;
1176                 else if (conn == 0)
1177                         s->handshake_func=meth->ssl_accept;
1178                 }
1179         return(ret);
1180         }
1181
1182 int SSL_get_error(s,i)
1183 SSL *s;
1184 int i;
1185         {
1186         int reason;
1187         BIO *bio;
1188
1189         if (i > 0) return(SSL_ERROR_NONE);
1190
1191         if (ERR_peek_error() != 0)
1192                 return(SSL_ERROR_SSL);
1193
1194         if ((i < 0) && SSL_want_read(s))
1195                 {
1196                 bio=SSL_get_rbio(s);
1197                 if (BIO_should_read(bio))
1198                         return(SSL_ERROR_WANT_READ);
1199                 else if (BIO_should_write(bio))
1200                         return(SSL_ERROR_WANT_WRITE);
1201                 else if (BIO_should_io_special(bio))
1202                         {
1203                         reason=BIO_get_retry_reason(bio);
1204                         if (reason == BIO_RR_CONNECT)
1205                                 return(SSL_ERROR_WANT_CONNECT);
1206                         else
1207                                 return(SSL_ERROR_SYSCALL); /* unknown */
1208                         }
1209                 }
1210
1211         if ((i < 0) && SSL_want_write(s))
1212                 {
1213                 bio=SSL_get_wbio(s);
1214                 if (BIO_should_write(bio))
1215                         return(SSL_ERROR_WANT_WRITE);
1216                 else if (BIO_should_read(bio))
1217                         return(SSL_ERROR_WANT_READ);
1218                 else if (BIO_should_io_special(bio))
1219                         {
1220                         reason=BIO_get_retry_reason(bio);
1221                         if (reason == BIO_RR_CONNECT)
1222                                 return(SSL_ERROR_WANT_CONNECT);
1223                         else
1224                                 return(SSL_ERROR_SYSCALL);
1225                         }
1226                 }
1227         if ((i < 0) && SSL_want_x509_lookup(s))
1228                 {
1229                 return(SSL_ERROR_WANT_X509_LOOKUP);
1230                 }
1231
1232         if (i == 0)
1233                 {
1234                 if (s->version == 2)
1235                         {
1236                         /* assume it is the socket being closed */
1237                         return(SSL_ERROR_ZERO_RETURN);
1238                         }
1239                 else
1240                         {
1241                         if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1242                                 (s->s3->warn_alert == SSL3_AD_CLOSE_NOTIFY))
1243                                 return(SSL_ERROR_ZERO_RETURN);
1244                         }
1245                 }
1246         return(SSL_ERROR_SYSCALL);
1247         }
1248
1249 int SSL_do_handshake(s)
1250 SSL *s;
1251         {
1252         if (s->handshake_func == NULL)
1253                 {
1254                 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_INTERNAL_ERROR);
1255                 return(-1);
1256                 }
1257         if (SSL_in_init(s) || SSL_in_before(s))
1258                 return(s->handshake_func(s));
1259         else
1260                 return(1);
1261         }
1262
1263 /* For the next 2 functions, SSL_clear() sets shutdown and so
1264  * one of these calls will reset it */
1265 void SSL_set_accept_state(s)
1266 SSL *s;
1267         {
1268         s->shutdown=0;
1269         s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
1270         s->handshake_func=s->method->ssl_accept;
1271         /* clear the current cipher */
1272         ssl_clear_cipher_ctx(s);
1273         }
1274
1275 void SSL_set_connect_state(s)
1276 SSL *s;
1277         {
1278         s->shutdown=0;
1279         s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
1280         s->handshake_func=s->method->ssl_connect;
1281         /* clear the current cipher */
1282         ssl_clear_cipher_ctx(s);
1283         }
1284
1285 int ssl_undefined_function(s)
1286 SSL *s;
1287         {
1288         SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1289         return(0);
1290         }
1291
1292 SSL_METHOD *ssl_bad_method(ver)
1293 int ver;
1294         {
1295         SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1296         return(NULL);
1297         }
1298
1299 char *SSL_get_version(s)
1300 SSL *s;
1301         {
1302         if (s->version == 3)
1303                 return("SSLv3");
1304         else if (s->version == 2)
1305                 return("SSLv2");
1306         else
1307                 return("unknown");
1308         }
1309
1310 SSL *SSL_dup(s)
1311 SSL *s;
1312         {
1313         STACK *sk;
1314         X509_NAME *xn;
1315         SSL *ret;
1316         int i;
1317                  
1318         if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) return(NULL);
1319                           
1320         /* This copies version, session-id, SSL_METHOD and 'cert' */
1321         SSL_copy_session_id(ret,s);
1322
1323         SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
1324         SSL_set_verify(ret,SSL_get_verify_mode(s),
1325                 SSL_get_verify_callback(s));
1326
1327         SSL_set_info_callback(ret,SSL_get_info_callback(s));
1328         
1329         ret->debug=s->debug;
1330
1331         /* copy app data, a little dangerous perhaps */
1332         SSL_set_app_data(ret,SSL_get_app_data(s));
1333
1334         /* setup rbio, and wbio */
1335         if (s->rbio != NULL)
1336                 {
1337                 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
1338                         goto err;
1339                 }
1340         if (s->wbio != NULL)
1341                 {
1342                 if (s->wbio != s->rbio)
1343                         {
1344                         if (!BIO_dup_state(s->wbio,(char *)&ret->rbio))
1345                                 goto err;
1346                         }
1347                 else
1348                         ret->wbio=ret->rbio;
1349                 }
1350
1351         /* dup the cipher_list and cipher_list_by_id stacks */
1352         if (s->cipher_list != NULL)
1353                 {
1354                 if ((ret->cipher_list=sk_dup(s->cipher_list)) == NULL)
1355                         goto err;
1356                 }
1357         if (s->cipher_list_by_id != NULL)
1358                 if ((ret->cipher_list_by_id=sk_dup(s->cipher_list_by_id))
1359                         == NULL)
1360                         goto err;
1361
1362         /* Dup the client_CA list */
1363         if (s->client_CA != NULL)
1364                 {
1365                 if ((sk=sk_dup(s->client_CA)) == NULL) goto err;
1366                 ret->client_CA=sk;
1367                 for (i=0; i<sk_num(sk); i++)
1368                         {
1369                         xn=(X509_NAME *)sk_value(sk,i);
1370                         if ((sk_value(sk,i)=(char *)X509_NAME_dup(xn)) == NULL)
1371                                 {
1372                                 X509_NAME_free(xn);
1373                                 goto err;
1374                                 }
1375                         }
1376                 }
1377
1378         ret->shutdown=s->shutdown;
1379         ret->state=s->state;
1380         ret->handshake_func=s->handshake_func;
1381
1382         if (0)
1383                 {
1384 err:
1385                 if (ret != NULL) SSL_free(ret);
1386                 ret=NULL;
1387                 }
1388         return(ret);
1389         }
1390
1391 void ssl_clear_cipher_ctx(s)
1392 SSL *s;
1393         {
1394         if (s->enc_read_ctx != NULL)
1395                 {
1396                 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
1397                 Free(s->enc_read_ctx);
1398                 s->enc_read_ctx=NULL;
1399                 }
1400         if (s->enc_write_ctx != NULL)
1401                 {
1402                 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
1403                 Free(s->enc_write_ctx);
1404                 s->enc_write_ctx=NULL;
1405                 }
1406         }
1407
1408 X509 *SSL_get_certificate(s)
1409 SSL *s;
1410         {
1411         if (s->cert != NULL)
1412                 return(s->cert->key->x509);
1413         else
1414                 return(NULL);
1415         }
1416
1417 EVP_PKEY *SSL_get_privatekey(s)
1418 SSL *s;
1419         {
1420         if (s->cert != NULL)
1421                 return(s->cert->key->privatekey);
1422         else
1423                 return(NULL);
1424         }
1425
1426 SSL_CIPHER *SSL_get_current_cipher(s)
1427 SSL *s;
1428         {
1429         if ((s->session != NULL) && (s->session->cipher != NULL))
1430                 return(s->session->cipher);
1431         return(NULL);
1432         }
1433