c64b760a445c896eabc82aec2e9fe6268247e4a1
[openssl.git] / ssl / s3_lib.c
1 /* ssl/s3_lib.c */
2 /* Copyright (C) 1995-1998 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 "ssl_locl.h"
62
63 char *ssl3_version_str="SSLv3 part of OpenSSL 0.9.2 31-Dec-1998";
64
65 #define SSL3_NUM_CIPHERS        (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER))
66
67 #ifndef NOPROTO
68 static long ssl3_default_timeout(void );
69 #else
70 static long ssl3_default_timeout();
71 #endif
72
73 SSL_CIPHER ssl3_ciphers[]={
74 /* The RSA ciphers */
75 /* Cipher 01 */
76         {
77         1,
78         SSL3_TXT_RSA_NULL_MD5,
79         SSL3_CK_RSA_NULL_MD5,
80         SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_MD5|SSL_NOT_EXP|SSL_SSLV3,
81         0,
82         SSL_ALL_CIPHERS,
83         },
84 /* Cipher 02 */
85         {
86         1,
87         SSL3_TXT_RSA_NULL_SHA,
88         SSL3_CK_RSA_NULL_SHA,
89         SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
90         0,
91         SSL_ALL_CIPHERS,
92         },
93
94 /* anon DH */
95 /* Cipher 17 */
96         {
97         1,
98         SSL3_TXT_ADH_RC4_40_MD5,
99         SSL3_CK_ADH_RC4_40_MD5,
100         SSL_kEDH |SSL_aNULL|SSL_RC4  |SSL_MD5 |SSL_EXP|SSL_SSLV3,
101         0,
102         SSL_ALL_CIPHERS,
103         },
104 /* Cipher 18 */
105         {
106         1,
107         SSL3_TXT_ADH_RC4_128_MD5,
108         SSL3_CK_ADH_RC4_128_MD5,
109         SSL_kEDH |SSL_aNULL|SSL_RC4  |SSL_MD5|SSL_NOT_EXP|SSL_SSLV3,
110         0,
111         SSL_ALL_CIPHERS,
112         },
113 /* Cipher 19 */
114         {
115         1,
116         SSL3_TXT_ADH_DES_40_CBC_SHA,
117         SSL3_CK_ADH_DES_40_CBC_SHA,
118         SSL_kEDH |SSL_aNULL|SSL_DES|SSL_SHA1|SSL_EXP|SSL_SSLV3,
119         0,
120         SSL_ALL_CIPHERS,
121         },
122 /* Cipher 1A */
123         {
124         1,
125         SSL3_TXT_ADH_DES_64_CBC_SHA,
126         SSL3_CK_ADH_DES_64_CBC_SHA,
127         SSL_kEDH |SSL_aNULL|SSL_DES  |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
128         0,
129         SSL_ALL_CIPHERS,
130         },
131 /* Cipher 1B */
132         {
133         1,
134         SSL3_TXT_ADH_DES_192_CBC_SHA,
135         SSL3_CK_ADH_DES_192_CBC_SHA,
136         SSL_kEDH |SSL_aNULL|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
137         0,
138         SSL_ALL_CIPHERS,
139         },
140
141 /* RSA again */
142 /* Cipher 03 */
143         {
144         1,
145         SSL3_TXT_RSA_RC4_40_MD5,
146         SSL3_CK_RSA_RC4_40_MD5,
147         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_MD5 |SSL_EXP|SSL_SSLV3,
148         0,
149         SSL_ALL_CIPHERS,
150         },
151 /* Cipher 04 */
152         {
153         1,
154         SSL3_TXT_RSA_RC4_128_MD5,
155         SSL3_CK_RSA_RC4_128_MD5,
156         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_MD5|SSL_NOT_EXP|SSL_SSLV3|SSL_MEDIUM,
157         0,
158         SSL_ALL_CIPHERS,
159         },
160 /* Cipher 05 */
161         {
162         1,
163         SSL3_TXT_RSA_RC4_128_SHA,
164         SSL3_CK_RSA_RC4_128_SHA,
165         SSL_kRSA|SSL_aRSA|SSL_RC4  |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_MEDIUM,
166         0,
167         SSL_ALL_CIPHERS,
168         },
169 /* Cipher 06 */
170         {
171         1,
172         SSL3_TXT_RSA_RC2_40_MD5,
173         SSL3_CK_RSA_RC2_40_MD5,
174         SSL_kRSA|SSL_aRSA|SSL_RC2  |SSL_MD5 |SSL_EXP|SSL_SSLV3,
175         0,
176         SSL_ALL_CIPHERS,
177         },
178 /* Cipher 07 */
179         {
180         1,
181         SSL3_TXT_RSA_IDEA_128_SHA,
182         SSL3_CK_RSA_IDEA_128_SHA,
183         SSL_kRSA|SSL_aRSA|SSL_IDEA |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_MEDIUM,
184         0,
185         SSL_ALL_CIPHERS,
186         },
187 /* Cipher 08 */
188         {
189         1,
190         SSL3_TXT_RSA_DES_40_CBC_SHA,
191         SSL3_CK_RSA_DES_40_CBC_SHA,
192         SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_EXP|SSL_SSLV3,
193         0,
194         SSL_ALL_CIPHERS,
195         },
196 /* Cipher 09 */
197         {
198         1,
199         SSL3_TXT_RSA_DES_64_CBC_SHA,
200         SSL3_CK_RSA_DES_64_CBC_SHA,
201         SSL_kRSA|SSL_aRSA|SSL_DES  |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
202         0,
203         SSL_ALL_CIPHERS,
204         },
205 /* Cipher 0A */
206         {
207         1,
208         SSL3_TXT_RSA_DES_192_CBC3_SHA,
209         SSL3_CK_RSA_DES_192_CBC3_SHA,
210         SSL_kRSA|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
211         0,
212         SSL_ALL_CIPHERS,
213         },
214
215 /*  The DH ciphers */
216 /* Cipher 0B */
217         {
218         0,
219         SSL3_TXT_DH_DSS_DES_40_CBC_SHA,
220         SSL3_CK_DH_DSS_DES_40_CBC_SHA,
221         SSL_kDHd |SSL_aDH|SSL_DES|SSL_SHA1|SSL_EXP|SSL_SSLV3,
222         0,
223         SSL_ALL_CIPHERS,
224         },
225 /* Cipher 0C */
226         {
227         0,
228         SSL3_TXT_DH_DSS_DES_64_CBC_SHA,
229         SSL3_CK_DH_DSS_DES_64_CBC_SHA,
230         SSL_kDHd |SSL_aDH|SSL_DES  |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
231         0,
232         SSL_ALL_CIPHERS,
233         },
234 /* Cipher 0D */
235         {
236         0,
237         SSL3_TXT_DH_DSS_DES_192_CBC3_SHA,
238         SSL3_CK_DH_DSS_DES_192_CBC3_SHA,
239         SSL_kDHd |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
240         0,
241         SSL_ALL_CIPHERS,
242         },
243 /* Cipher 0E */
244         {
245         0,
246         SSL3_TXT_DH_RSA_DES_40_CBC_SHA,
247         SSL3_CK_DH_RSA_DES_40_CBC_SHA,
248         SSL_kDHr |SSL_aDH|SSL_DES|SSL_SHA1|SSL_EXP|SSL_SSLV3,
249         0,
250         SSL_ALL_CIPHERS,
251         },
252 /* Cipher 0F */
253         {
254         0,
255         SSL3_TXT_DH_RSA_DES_64_CBC_SHA,
256         SSL3_CK_DH_RSA_DES_64_CBC_SHA,
257         SSL_kDHr |SSL_aDH|SSL_DES  |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
258         0,
259         SSL_ALL_CIPHERS,
260         },
261 /* Cipher 10 */
262         {
263         0,
264         SSL3_TXT_DH_RSA_DES_192_CBC3_SHA,
265         SSL3_CK_DH_RSA_DES_192_CBC3_SHA,
266         SSL_kDHr |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
267         0,
268         SSL_ALL_CIPHERS,
269         },
270
271 /* The Ephemeral DH ciphers */
272 /* Cipher 11 */
273         {
274         1,
275         SSL3_TXT_EDH_DSS_DES_40_CBC_SHA,
276         SSL3_CK_EDH_DSS_DES_40_CBC_SHA,
277         SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA1|SSL_EXP|SSL_SSLV3,
278         0,
279         SSL_ALL_CIPHERS,
280         },
281 /* Cipher 12 */
282         {
283         1,
284         SSL3_TXT_EDH_DSS_DES_64_CBC_SHA,
285         SSL3_CK_EDH_DSS_DES_64_CBC_SHA,
286         SSL_kEDH|SSL_aDSS|SSL_DES  |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
287         0,
288         SSL_ALL_CIPHERS,
289         },
290 /* Cipher 13 */
291         {
292         1,
293         SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA,
294         SSL3_CK_EDH_DSS_DES_192_CBC3_SHA,
295         SSL_kEDH|SSL_aDSS|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
296         0,
297         SSL_ALL_CIPHERS,
298         },
299 /* Cipher 14 */
300         {
301         1,
302         SSL3_TXT_EDH_RSA_DES_40_CBC_SHA,
303         SSL3_CK_EDH_RSA_DES_40_CBC_SHA,
304         SSL_kEDH|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_EXP|SSL_SSLV3,
305         0,
306         SSL_ALL_CIPHERS,
307         },
308 /* Cipher 15 */
309         {
310         1,
311         SSL3_TXT_EDH_RSA_DES_64_CBC_SHA,
312         SSL3_CK_EDH_RSA_DES_64_CBC_SHA,
313         SSL_kEDH|SSL_aRSA|SSL_DES  |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_LOW,
314         0,
315         SSL_ALL_CIPHERS,
316         },
317 /* Cipher 16 */
318         {
319         1,
320         SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA,
321         SSL3_CK_EDH_RSA_DES_192_CBC3_SHA,
322         SSL_kEDH|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3|SSL_HIGH,
323         0,
324         SSL_ALL_CIPHERS,
325         },
326
327 /* Fortezza */
328 /* Cipher 1C */
329         {
330         0,
331         SSL3_TXT_FZA_DMS_NULL_SHA,
332         SSL3_CK_FZA_DMS_NULL_SHA,
333         SSL_kFZA|SSL_aFZA |SSL_eNULL |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
334         0,
335         SSL_ALL_CIPHERS,
336         },
337
338 /* Cipher 1D */
339         {
340         0,
341         SSL3_TXT_FZA_DMS_FZA_SHA,
342         SSL3_CK_FZA_DMS_FZA_SHA,
343         SSL_kFZA|SSL_aFZA |SSL_eFZA |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
344         0,
345         SSL_ALL_CIPHERS,
346         },
347
348 /* Cipher 1E */
349         {
350         0,
351         SSL3_TXT_FZA_DMS_RC4_SHA,
352         SSL3_CK_FZA_DMS_RC4_SHA,
353         SSL_kFZA|SSL_aFZA |SSL_RC4  |SSL_SHA1|SSL_NOT_EXP|SSL_SSLV3,
354         0,
355         SSL_ALL_CIPHERS,
356         },
357
358 /* end of list */
359         };
360
361 static SSL3_ENC_METHOD SSLv3_enc_data={
362         ssl3_enc,
363         ssl3_mac,
364         ssl3_setup_key_block,
365         ssl3_generate_master_secret,
366         ssl3_change_cipher_state,
367         ssl3_final_finish_mac,
368         MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
369         ssl3_cert_verify_mac,
370         SSL3_MD_CLIENT_FINISHED_CONST,4,
371         SSL3_MD_SERVER_FINISHED_CONST,4,
372         ssl3_alert_code,
373         };
374
375 static SSL_METHOD SSLv3_data= {
376         SSL3_VERSION,
377         ssl3_new,
378         ssl3_clear,
379         ssl3_free,
380         ssl_undefined_function,
381         ssl_undefined_function,
382         ssl3_read,
383         ssl3_peek,
384         ssl3_write,
385         ssl3_shutdown,
386         ssl3_renegotiate,
387         ssl3_renegotiate_check,
388         ssl3_ctrl,
389         ssl3_ctx_ctrl,
390         ssl3_get_cipher_by_char,
391         ssl3_put_cipher_by_char,
392         ssl3_pending,
393         ssl3_num_ciphers,
394         ssl3_get_cipher,
395         ssl_bad_method,
396         ssl3_default_timeout,
397         &SSLv3_enc_data,
398         };
399
400 static long ssl3_default_timeout()
401         {
402         /* 2 hours, the 24 hours mentioned in the SSLv3 spec
403          * is way too long for http, the cache would over fill */
404         return(60*60*2);
405         }
406
407 SSL_METHOD *sslv3_base_method()
408         {
409         return(&SSLv3_data);
410         }
411
412 int ssl3_num_ciphers()
413         {
414         return(SSL3_NUM_CIPHERS);
415         }
416
417 SSL_CIPHER *ssl3_get_cipher(u)
418 unsigned int u;
419         {
420         if (u < SSL3_NUM_CIPHERS)
421                 return(&(ssl3_ciphers[SSL3_NUM_CIPHERS-1-u]));
422         else
423                 return(NULL);
424         }
425
426 /* The problem is that it may not be the correct record type */
427 int ssl3_pending(s)
428 SSL *s;
429         {
430         return(s->s3->rrec.length);
431         }
432
433 int ssl3_new(s)
434 SSL *s;
435         {
436         SSL3_CTX *s3;
437
438         if ((s3=(SSL3_CTX *)Malloc(sizeof(SSL3_CTX))) == NULL) goto err;
439         memset(s3,0,sizeof(SSL3_CTX));
440
441         s->s3=s3;
442         /*
443         s->s3->tmp.ca_names=NULL;
444         s->s3->tmp.key_block=NULL;
445         s->s3->tmp.key_block_length=0;
446         s->s3->rbuf.buf=NULL;
447         s->s3->wbuf.buf=NULL;
448         */
449
450         s->method->ssl_clear(s);
451         return(1);
452 err:
453         return(0);
454         }
455
456 void ssl3_free(s)
457 SSL *s;
458         {
459         if(s == NULL)
460             return;
461
462         ssl3_cleanup_key_block(s);
463         if (s->s3->rbuf.buf != NULL)
464                 Free(s->s3->rbuf.buf);
465         if (s->s3->wbuf.buf != NULL)
466                 Free(s->s3->wbuf.buf);
467         if (s->s3->rrec.comp != NULL)
468                 Free(s->s3->rrec.comp);
469 #ifndef NO_DH
470         if (s->s3->tmp.dh != NULL)
471                 DH_free(s->s3->tmp.dh);
472 #endif
473         if (s->s3->tmp.ca_names != NULL)
474                 sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
475         memset(s->s3,0,sizeof(SSL3_CTX));
476         Free(s->s3);
477         s->s3=NULL;
478         }
479
480 void ssl3_clear(s)
481 SSL *s;
482         {
483         unsigned char *rp,*wp;
484
485         ssl3_cleanup_key_block(s);
486         if (s->s3->tmp.ca_names != NULL)
487                 sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
488
489         if (s->s3->rrec.comp != NULL)
490                 {
491                 Free(s->s3->rrec.comp);
492                 s->s3->rrec.comp=NULL;
493                 }
494
495         rp=s->s3->rbuf.buf;
496         wp=s->s3->wbuf.buf;
497
498         memset(s->s3,0,sizeof(SSL3_CTX));
499         if (rp != NULL) s->s3->rbuf.buf=rp;
500         if (wp != NULL) s->s3->wbuf.buf=wp;
501
502         ssl_free_wbio_buffer(s);
503
504         s->packet_length=0;
505         s->s3->renegotiate=0;
506         s->s3->total_renegotiations=0;
507         s->s3->num_renegotiations=0;
508         s->s3->in_read_app_data=0;
509         s->version=SSL3_VERSION;
510         }
511
512 long ssl3_ctrl(s,cmd,larg,parg)
513 SSL *s;
514 int cmd;
515 long larg;
516 char *parg;
517         {
518         int ret=0;
519
520         switch (cmd)
521                 {
522         case SSL_CTRL_GET_SESSION_REUSED:
523                 ret=s->hit;
524                 break;
525         case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
526                 break;
527         case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
528                 ret=s->s3->num_renegotiations;
529                 break;
530         case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
531                 ret=s->s3->num_renegotiations;
532                 s->s3->num_renegotiations=0;
533                 break;
534         case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
535                 ret=s->s3->total_renegotiations;
536                 break;
537         case SSL_CTRL_GET_FLAGS:
538                 ret=(int)(s->s3->flags);
539                 break;
540         default:
541                 break;
542                 }
543         return(ret);
544         }
545
546 long ssl3_ctx_ctrl(ctx,cmd,larg,parg)
547 SSL_CTX *ctx;
548 int cmd;
549 long larg;
550 char *parg;
551         {
552         CERT *cert;
553
554         cert=ctx->default_cert;
555
556         switch (cmd)
557                 {
558 #ifndef NO_RSA
559         case SSL_CTRL_NEED_TMP_RSA:
560                 if (    (cert->rsa_tmp == NULL) &&
561                         ((cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
562                          (EVP_PKEY_size(cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8)))
563                         )
564                         return(1);
565                 else
566                         return(0);
567                 /* break; */
568         case SSL_CTRL_SET_TMP_RSA:
569                 {
570                 RSA *rsa;
571                 int i;
572
573                 rsa=(RSA *)parg;
574                 i=1;
575                 if (rsa == NULL)
576                         i=0;
577                 else
578                         {
579                         if ((rsa=RSAPrivateKey_dup(rsa)) == NULL)
580                                 i=0;
581                         }
582                 if (!i)
583                         {
584                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_RSA_LIB);
585                         return(0);
586                         }
587                 else
588                         {
589                         if (cert->rsa_tmp != NULL)
590                                 RSA_free(cert->rsa_tmp);
591                         cert->rsa_tmp=rsa;
592                         return(1);
593                         }
594                 }
595                 /* break; */
596         case SSL_CTRL_SET_TMP_RSA_CB:
597                 cert->rsa_tmp_cb=(RSA *(*)())parg;
598                 break;
599 #endif
600 #ifndef NO_DH
601         case SSL_CTRL_SET_TMP_DH:
602                 {
603                 DH *new=NULL,*dh;
604                 int rret=0;
605
606                 dh=(DH *)parg;
607                 if (    ((new=DHparams_dup(dh)) == NULL) ||
608                         (!DH_generate_key(new)))
609                         {
610                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
611                         if (new != NULL) DH_free(new);
612                         }
613                 else
614                         {
615                         if (cert->dh_tmp != NULL)
616                                 DH_free(cert->dh_tmp);
617                         cert->dh_tmp=new;
618                         rret=1;
619                         }
620                 return(rret);
621                 }
622                 /*break; */
623         case SSL_CTRL_SET_TMP_DH_CB:
624                 cert->dh_tmp_cb=(DH *(*)())parg;
625                 break;
626 #endif
627         /* A Thawte special :-) */
628         case SSL_CTRL_EXTRA_CHAIN_CERT:
629                 if (ctx->extra_certs == NULL)
630                         {
631                         if ((ctx->extra_certs=sk_new_null()) == NULL)
632                                 return(0);
633                         }
634                 sk_push(ctx->extra_certs,(char *)parg);
635                 break;
636
637         default:
638                 return(0);
639                 }
640         return(1);
641         }
642
643 /* This function needs to check if the ciphers required are actually
644  * available */
645 SSL_CIPHER *ssl3_get_cipher_by_char(p)
646 const unsigned char *p;
647         {
648         static int init=1;
649         static SSL_CIPHER *sorted[SSL3_NUM_CIPHERS];
650         SSL_CIPHER c,*cp= &c,**cpp;
651         unsigned long id;
652         int i;
653
654         if (init)
655                 {
656                 init=0;
657
658                 for (i=0; i<SSL3_NUM_CIPHERS; i++)
659                         sorted[i]= &(ssl3_ciphers[i]);
660
661                 qsort(  (char *)sorted,
662                         SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
663                         FP_ICC ssl_cipher_ptr_id_cmp);
664                 }
665
666         id=0x03000000L|((unsigned long)p[0]<<8L)|(unsigned long)p[1];
667         c.id=id;
668         cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
669                 (char *)sorted,
670                 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
671                 (int (*)())ssl_cipher_ptr_id_cmp);
672         if ((cpp == NULL) || !(*cpp)->valid)
673                 return(NULL);
674         else
675                 return(*cpp);
676         }
677
678 int ssl3_put_cipher_by_char(c,p)
679 const SSL_CIPHER *c;
680 unsigned char *p;
681         {
682         long l;
683
684         if (p != NULL)
685                 {
686                 l=c->id;
687                 if ((l & 0xff000000) != 0x03000000) return(0);
688                 p[0]=((unsigned char)(l>> 8L))&0xFF;
689                 p[1]=((unsigned char)(l     ))&0xFF;
690                 }
691         return(2);
692         }
693
694 int ssl3_part_read(s,i)
695 SSL *s;
696 int i;
697         {
698         s->rwstate=SSL_READING;
699
700         if (i < 0)
701                 {
702                 return(i);
703                 }
704         else
705                 {
706                 s->init_num+=i;
707                 return(0);
708                 }
709         }
710
711 SSL_CIPHER *ssl3_choose_cipher(s,have,pref)
712 SSL *s;
713 STACK *have,*pref;
714         {
715         SSL_CIPHER *c,*ret=NULL;
716         int i,j,ok;
717         CERT *cert;
718         unsigned long alg,mask,emask;
719
720         /* Lets see which ciphers we can supported */
721         if (s->cert != NULL)
722                 cert=s->cert;
723         else
724                 cert=s->ctx->default_cert;
725
726         ssl_set_cert_masks(cert);
727         mask=cert->mask;
728         emask=cert->export_mask;
729                         
730         sk_set_cmp_func(pref,ssl_cipher_ptr_id_cmp);
731
732         for (i=0; i<sk_num(have); i++)
733                 {
734                 c=(SSL_CIPHER *)sk_value(have,i);
735                 alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);
736                 if (alg & SSL_EXPORT)
737                         {
738                         ok=((alg & emask) == alg)?1:0;
739 #ifdef CIPHER_DEBUG
740                         printf("%d:[%08lX:%08lX]%s\n",ok,alg,mask,c->name);
741 #endif
742                         }
743                 else
744                         {
745                         ok=((alg & mask) == alg)?1:0;
746 #ifdef CIPHER_DEBUG
747                         printf("%d:[%08lX:%08lX]%s\n",ok,alg,mask,c->name);
748 #endif
749                         }
750
751                 if (!ok) continue;
752         
753                 j=sk_find(pref,(char *)c);
754                 if (j >= 0)
755                         {
756                         ret=(SSL_CIPHER *)sk_value(pref,j);
757                         break;
758                         }
759                 }
760         return(ret);
761         }
762
763 int ssl3_get_req_cert_type(s,p)
764 SSL *s;
765 unsigned char *p;
766         {
767         int ret=0;
768         unsigned long alg;
769
770         alg=s->s3->tmp.new_cipher->algorithms;
771
772 #ifndef NO_DH
773         if (alg & (SSL_kDHr|SSL_kEDH))
774                 {
775 #  ifndef NO_RSA
776                 p[ret++]=SSL3_CT_RSA_FIXED_DH;
777 #  endif
778 #  ifndef NO_DSA
779                 p[ret++]=SSL3_CT_DSS_FIXED_DH;
780 #  endif
781                 }
782         if ((s->version == SSL3_VERSION) &&
783                 (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr)))
784                 {
785 #  ifndef NO_RSA
786                 p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
787 #  endif
788 #  ifndef NO_DSA
789                 p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
790 #  endif
791                 }
792 #endif /* !NO_DH */
793 #ifndef NO_RSA
794         p[ret++]=SSL3_CT_RSA_SIGN;
795 #endif
796 #ifndef NO_DSA
797         p[ret++]=SSL3_CT_DSS_SIGN;
798 #endif
799         return(ret);
800         }
801
802 int ssl3_shutdown(s)
803 SSL *s;
804         {
805
806         /* Don't do anything much if we have not done the handshake or
807          * we don't want to send messages :-) */
808         if ((s->quiet_shutdown) || (s->state == SSL_ST_BEFORE))
809                 {
810                 s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
811                 return(1);
812                 }
813
814         if (!(s->shutdown & SSL_SENT_SHUTDOWN))
815                 {
816                 s->shutdown|=SSL_SENT_SHUTDOWN;
817 #if 1
818                 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);
819 #endif
820                 /* our shutdown alert has been sent now, and if it still needs
821                  * to be written, s->s3->alert_dispatch will be true */
822                 }
823         else if (s->s3->alert_dispatch)
824                 {
825                 /* resend it if not sent */
826 #if 1
827                 ssl3_dispatch_alert(s);
828 #endif
829                 }
830         else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN))
831                 {
832                 /* If we are waiting for a close from our peer, we are closed */
833                 ssl3_read_bytes(s,0,NULL,0);
834                 }
835
836         if ((s->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) &&
837                 !s->s3->alert_dispatch)
838                 return(1);
839         else
840                 return(0);
841         }
842
843 int ssl3_write(s,buf,len)
844 SSL *s;
845 const char *buf;
846 int len;
847         {
848         int ret,n;
849
850 #if 0
851         if (s->shutdown & SSL_SEND_SHUTDOWN)
852                 {
853                 s->rwstate=SSL_NOTHING;
854                 return(0);
855                 }
856 #endif
857         clear_sys_error();
858         if (s->s3->renegotiate) ssl3_renegotiate_check(s);
859
860         /* This is an experimental flag that sends the
861          * last handshake message in the same packet as the first
862          * use data - used to see if it helps the TCP protocol during
863          * session-id reuse */
864         /* The second test is because the buffer may have been removed */
865         if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio))
866                 {
867                 /* First time through, we write into the buffer */
868                 if (s->s3->delay_buf_pop_ret == 0)
869                         {
870                         ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
871                                 (char *)buf,len);
872                         if (ret <= 0) return(ret);
873
874                         s->s3->delay_buf_pop_ret=ret;
875                         }
876
877                 s->rwstate=SSL_WRITING;
878                 n=BIO_flush(s->wbio);
879                 if (n <= 0) return(n);
880                 s->rwstate=SSL_NOTHING;
881
882                 /* We have flushed the buffer, so remove it */
883                 ssl_free_wbio_buffer(s);
884                 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
885
886                 ret=s->s3->delay_buf_pop_ret;
887                 s->s3->delay_buf_pop_ret=0;
888                 }
889         else
890                 {
891                 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
892                         (char *)buf,len);
893                 if (ret <= 0) return(ret);
894                 }
895
896         return(ret);
897         }
898
899 int ssl3_read(s,buf,len)
900 SSL *s;
901 char *buf;
902 int len;
903         {
904         int ret;
905         
906         clear_sys_error();
907         if (s->s3->renegotiate) ssl3_renegotiate_check(s);
908         s->s3->in_read_app_data=1;
909         ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
910         if ((ret == -1) && (s->s3->in_read_app_data == 0))
911                 {
912                 ERR_get_error(); /* clear the error */
913                 s->s3->in_read_app_data=0;
914                 s->in_handshake++;
915                 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
916                 s->in_handshake--;
917                 }
918         else
919                 s->s3->in_read_app_data=0;
920
921         return(ret);
922         }
923
924 int ssl3_peek(s,buf,len)
925 SSL *s;
926 char *buf;
927 int len;
928         {
929         SSL3_RECORD *rr;
930         int n;
931
932         rr= &(s->s3->rrec);
933         if ((rr->length == 0) || (rr->type != SSL3_RT_APPLICATION_DATA))
934                 {
935                 n=ssl3_read(s,buf,1);
936                 if (n <= 0) return(n);
937                 rr->length++;
938                 rr->off--;
939                 }
940
941         if ((unsigned int)len > rr->length)
942                 n=rr->length;
943         else
944                 n=len;
945         memcpy(buf,&(rr->data[rr->off]),(unsigned int)n);
946         return(n);
947         }
948
949 int ssl3_renegotiate(s)
950 SSL *s;
951         {
952         if (s->handshake_func == NULL)
953                 return(1);
954
955         if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
956                 return(0);
957
958         s->s3->renegotiate=1;
959         return(1);
960         }
961
962 int ssl3_renegotiate_check(s)
963 SSL *s;
964         {
965         int ret=0;
966
967         if (s->s3->renegotiate)
968                 {
969                 if (    (s->s3->rbuf.left == 0) &&
970                         (s->s3->wbuf.left == 0) &&
971                         !SSL_in_init(s))
972                         {
973 /*
974 if we are the server, and we have sent a 'RENEGOTIATE' message, we
975 need to go to SSL_ST_ACCEPT.\1e
976 */
977                         /* SSL_ST_ACCEPT */
978                         s->state=SSL_ST_RENEGOTIATE;
979                         s->s3->renegotiate=0;
980                         s->s3->num_renegotiations++;
981                         s->s3->total_renegotiations++;
982                         ret=1;
983                         }
984                 }
985         return(ret);
986         }
987