Put the dependencies back.
[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_EXP40|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_EXP40|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_EXP40|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_EXP40|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_EXP40|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_EXP40|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_EXP40|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_EXP40|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_EXP40|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         /* New TLS Export CipherSuites */
359         /* Cipher 60 */
360             {
361             1,
362             TLS1_TXT_RSA_EXPORT56_WITH_RC4_56_MD5,
363             TLS1_CK_RSA_EXPORT56_WITH_RC4_56_MD5,
364             SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_EXP56|SSL_TLSV1,
365             0,
366             SSL_ALL_CIPHERS
367             },
368         /* Cipher 61 */
369             {
370             1,
371             TLS1_TXT_RSA_EXPORT56_WITH_RC2_CBC_56_MD5,
372             TLS1_CK_RSA_EXPORT56_WITH_RC2_CBC_56_MD5,
373             SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_EXP56|SSL_TLSV1,
374             0,
375             SSL_ALL_CIPHERS
376             },
377         /* Cipher 62 */
378             {
379             1,
380             TLS1_TXT_RSA_EXPORT56_WITH_DES_CBC_SHA,
381             TLS1_CK_RSA_EXPORT56_WITH_DES_CBC_SHA,
382             SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA|SSL_EXP56|SSL_TLSV1,
383             0,
384             SSL_ALL_CIPHERS
385             },
386
387 /* end of list */
388         };
389
390 static SSL3_ENC_METHOD SSLv3_enc_data={
391         ssl3_enc,
392         ssl3_mac,
393         ssl3_setup_key_block,
394         ssl3_generate_master_secret,
395         ssl3_change_cipher_state,
396         ssl3_final_finish_mac,
397         MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
398         ssl3_cert_verify_mac,
399         SSL3_MD_CLIENT_FINISHED_CONST,4,
400         SSL3_MD_SERVER_FINISHED_CONST,4,
401         ssl3_alert_code,
402         };
403
404 static SSL_METHOD SSLv3_data= {
405         SSL3_VERSION,
406         ssl3_new,
407         ssl3_clear,
408         ssl3_free,
409         ssl_undefined_function,
410         ssl_undefined_function,
411         ssl3_read,
412         ssl3_peek,
413         ssl3_write,
414         ssl3_shutdown,
415         ssl3_renegotiate,
416         ssl3_renegotiate_check,
417         ssl3_ctrl,
418         ssl3_ctx_ctrl,
419         ssl3_get_cipher_by_char,
420         ssl3_put_cipher_by_char,
421         ssl3_pending,
422         ssl3_num_ciphers,
423         ssl3_get_cipher,
424         ssl_bad_method,
425         ssl3_default_timeout,
426         &SSLv3_enc_data,
427         };
428
429 static long ssl3_default_timeout()
430         {
431         /* 2 hours, the 24 hours mentioned in the SSLv3 spec
432          * is way too long for http, the cache would over fill */
433         return(60*60*2);
434         }
435
436 SSL_METHOD *sslv3_base_method()
437         {
438         return(&SSLv3_data);
439         }
440
441 int ssl3_num_ciphers()
442         {
443         return(SSL3_NUM_CIPHERS);
444         }
445
446 SSL_CIPHER *ssl3_get_cipher(u)
447 unsigned int u;
448         {
449         if (u < SSL3_NUM_CIPHERS)
450                 return(&(ssl3_ciphers[SSL3_NUM_CIPHERS-1-u]));
451         else
452                 return(NULL);
453         }
454
455 /* The problem is that it may not be the correct record type */
456 int ssl3_pending(s)
457 SSL *s;
458         {
459         return(s->s3->rrec.length);
460         }
461
462 int ssl3_new(s)
463 SSL *s;
464         {
465         SSL3_CTX *s3;
466
467         if ((s3=(SSL3_CTX *)Malloc(sizeof(SSL3_CTX))) == NULL) goto err;
468         memset(s3,0,sizeof(SSL3_CTX));
469
470         s->s3=s3;
471         /*
472         s->s3->tmp.ca_names=NULL;
473         s->s3->tmp.key_block=NULL;
474         s->s3->tmp.key_block_length=0;
475         s->s3->rbuf.buf=NULL;
476         s->s3->wbuf.buf=NULL;
477         */
478
479         s->method->ssl_clear(s);
480         return(1);
481 err:
482         return(0);
483         }
484
485 void ssl3_free(s)
486 SSL *s;
487         {
488         if(s == NULL)
489             return;
490
491         ssl3_cleanup_key_block(s);
492         if (s->s3->rbuf.buf != NULL)
493                 Free(s->s3->rbuf.buf);
494         if (s->s3->wbuf.buf != NULL)
495                 Free(s->s3->wbuf.buf);
496         if (s->s3->rrec.comp != NULL)
497                 Free(s->s3->rrec.comp);
498 #ifndef NO_DH
499         if (s->s3->tmp.dh != NULL)
500                 DH_free(s->s3->tmp.dh);
501 #endif
502         if (s->s3->tmp.ca_names != NULL)
503                 sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
504         memset(s->s3,0,sizeof(SSL3_CTX));
505         Free(s->s3);
506         s->s3=NULL;
507         }
508
509 void ssl3_clear(s)
510 SSL *s;
511         {
512         unsigned char *rp,*wp;
513
514         ssl3_cleanup_key_block(s);
515         if (s->s3->tmp.ca_names != NULL)
516                 sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
517
518         if (s->s3->rrec.comp != NULL)
519                 {
520                 Free(s->s3->rrec.comp);
521                 s->s3->rrec.comp=NULL;
522                 }
523
524         rp=s->s3->rbuf.buf;
525         wp=s->s3->wbuf.buf;
526
527         memset(s->s3,0,sizeof(SSL3_CTX));
528         if (rp != NULL) s->s3->rbuf.buf=rp;
529         if (wp != NULL) s->s3->wbuf.buf=wp;
530
531         ssl_free_wbio_buffer(s);
532
533         s->packet_length=0;
534         s->s3->renegotiate=0;
535         s->s3->total_renegotiations=0;
536         s->s3->num_renegotiations=0;
537         s->s3->in_read_app_data=0;
538         s->version=SSL3_VERSION;
539         }
540
541 long ssl3_ctrl(s,cmd,larg,parg)
542 SSL *s;
543 int cmd;
544 long larg;
545 char *parg;
546         {
547         int ret=0;
548
549 #if !defined(NO_DSA) || !defined(NO_RSA)
550         if (
551 #ifndef NO_RSA
552             cmd == SSL_CTRL_SET_TMP_RSA ||
553             cmd == SSL_CTRL_SET_TMP_RSA_CB ||
554 #endif
555 #ifndef NO_DSA
556             cmd == SSL_CTRL_SET_TMP_DH ||
557             cmd == SSL_CTRL_SET_TMP_DH_CB ||
558 #endif
559                 0)
560                 {
561                 if (!ssl_cert_instantiate(&s->cert, s->ctx->default_cert)) 
562                         {
563                         SSLerr(SSL_F_SSL3_CTRL, ERR_R_MALLOC_FAILURE);
564                         return(0);
565                         }
566                 }
567 #endif
568
569         switch (cmd)
570                 {
571         case SSL_CTRL_GET_SESSION_REUSED:
572                 ret=s->hit;
573                 break;
574         case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
575                 break;
576         case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
577                 ret=s->s3->num_renegotiations;
578                 break;
579         case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
580                 ret=s->s3->num_renegotiations;
581                 s->s3->num_renegotiations=0;
582                 break;
583         case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
584                 ret=s->s3->total_renegotiations;
585                 break;
586         case SSL_CTRL_GET_FLAGS:
587                 ret=(int)(s->s3->flags);
588                 break;
589 #ifndef NO_RSA
590         case SSL_CTRL_NEED_TMP_RSA:
591                 if ((s->cert != NULL) && (s->cert->rsa_tmp == NULL) &&
592                     ((s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
593                      (EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8))))
594                         ret = 1;
595                 break;
596         case SSL_CTRL_SET_TMP_RSA:
597                 {
598                         RSA *rsa = (RSA *)parg;
599                         if (rsa == NULL) {
600                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
601                                 return(ret);
602                         }
603                         if ((rsa = RSAPrivateKey_dup(rsa)) == NULL) {
604                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
605                                 return(ret);
606                         }
607                         if (s->cert->rsa_tmp != NULL)
608                                 RSA_free(s->cert->rsa_tmp);
609                         s->cert->rsa_tmp = rsa;
610                         ret = 1;
611                 }
612                 break;
613         case SSL_CTRL_SET_TMP_RSA_CB:
614 #ifndef NOPROTO
615                 s->cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))parg;
616 #else
617                 s->cert->rsa_tmp_cb = (RSA *(*)())parg;
618 #endif
619                 break;
620 #endif
621 #ifndef NO_DH
622         case SSL_CTRL_SET_TMP_DH:
623                 {
624                         DH *dh = (DH *)parg;
625                         if (dh == NULL) {
626                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
627                                 return(ret);
628                         }
629                         if ((dh = DHparams_dup(dh)) == NULL) {
630                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
631                                 return(ret);
632                         }
633                         if (!DH_generate_key(dh)) {
634                                 DH_free(dh);
635                                 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
636                                 return(ret);
637                         }
638                         if (s->cert->dh_tmp != NULL)
639                                 DH_free(s->cert->dh_tmp);
640                         s->cert->dh_tmp = dh;
641                         ret = 1;
642                 }
643                 break;
644         case SSL_CTRL_SET_TMP_DH_CB:
645 #ifndef NOPROTO
646                 s->cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))parg;
647 #else
648                 s->cert->dh_tmp_cb = (DH *(*)())parg;
649 #endif
650                 break;
651 #endif
652         default:
653                 break;
654                 }
655         return(ret);
656         }
657
658 long ssl3_ctx_ctrl(ctx,cmd,larg,parg)
659 SSL_CTX *ctx;
660 int cmd;
661 long larg;
662 char *parg;
663         {
664         CERT *cert;
665
666         cert=ctx->default_cert;
667
668         switch (cmd)
669                 {
670 #ifndef NO_RSA
671         case SSL_CTRL_NEED_TMP_RSA:
672                 if (    (cert->rsa_tmp == NULL) &&
673                         ((cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
674                          (EVP_PKEY_size(cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8)))
675                         )
676                         return(1);
677                 else
678                         return(0);
679                 /* break; */
680         case SSL_CTRL_SET_TMP_RSA:
681                 {
682                 RSA *rsa;
683                 int i;
684
685                 rsa=(RSA *)parg;
686                 i=1;
687                 if (rsa == NULL)
688                         i=0;
689                 else
690                         {
691                         if ((rsa=RSAPrivateKey_dup(rsa)) == NULL)
692                                 i=0;
693                         }
694                 if (!i)
695                         {
696                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_RSA_LIB);
697                         return(0);
698                         }
699                 else
700                         {
701                         if (cert->rsa_tmp != NULL)
702                                 RSA_free(cert->rsa_tmp);
703                         cert->rsa_tmp=rsa;
704                         return(1);
705                         }
706                 }
707                 /* break; */
708         case SSL_CTRL_SET_TMP_RSA_CB:
709 #ifndef NOPROTO
710                 cert->rsa_tmp_cb=(RSA *(*)(SSL *, int, int))parg;
711 #else
712                 cert->rsa_tmp_cb=(RSA *(*)())parg;
713 #endif
714                 break;
715 #endif
716 #ifndef NO_DH
717         case SSL_CTRL_SET_TMP_DH:
718                 {
719                 DH *new=NULL,*dh;
720                 int rret=0;
721
722                 dh=(DH *)parg;
723                 if (    ((new=DHparams_dup(dh)) == NULL) ||
724                         (!DH_generate_key(new)))
725                         {
726                         SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
727                         if (new != NULL) DH_free(new);
728                         }
729                 else
730                         {
731                         if (cert->dh_tmp != NULL)
732                                 DH_free(cert->dh_tmp);
733                         cert->dh_tmp=new;
734                         rret=1;
735                         }
736                 return(rret);
737                 }
738                 /*break; */
739         case SSL_CTRL_SET_TMP_DH_CB:
740 #ifndef NOPROTO
741                 cert->dh_tmp_cb=(DH *(*)(SSL *, int, int))parg;
742 #else
743                 cert->dh_tmp_cb=(DH *(*)())parg;
744 #endif
745                 break;
746 #endif
747         /* A Thawte special :-) */
748         case SSL_CTRL_EXTRA_CHAIN_CERT:
749                 if (ctx->extra_certs == NULL)
750                         {
751                         if ((ctx->extra_certs=sk_new_null()) == NULL)
752                                 return(0);
753                         }
754                 sk_push(ctx->extra_certs,(char *)parg);
755                 break;
756
757         default:
758                 return(0);
759                 }
760         return(1);
761         }
762
763 /* This function needs to check if the ciphers required are actually
764  * available */
765 SSL_CIPHER *ssl3_get_cipher_by_char(p)
766 const unsigned char *p;
767         {
768         static int init=1;
769         static SSL_CIPHER *sorted[SSL3_NUM_CIPHERS];
770         SSL_CIPHER c,*cp= &c,**cpp;
771         unsigned long id;
772         int i;
773
774         if (init)
775                 {
776                 init=0;
777
778                 for (i=0; i<SSL3_NUM_CIPHERS; i++)
779                         sorted[i]= &(ssl3_ciphers[i]);
780
781                 qsort(  (char *)sorted,
782                         SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
783                         FP_ICC ssl_cipher_ptr_id_cmp);
784                 }
785
786         id=0x03000000L|((unsigned long)p[0]<<8L)|(unsigned long)p[1];
787         c.id=id;
788         cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
789                 (char *)sorted,
790                 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
791                 (int (*)())ssl_cipher_ptr_id_cmp);
792         if ((cpp == NULL) || !(*cpp)->valid)
793                 return(NULL);
794         else
795                 return(*cpp);
796         }
797
798 int ssl3_put_cipher_by_char(c,p)
799 const SSL_CIPHER *c;
800 unsigned char *p;
801         {
802         long l;
803
804         if (p != NULL)
805                 {
806                 l=c->id;
807                 if ((l & 0xff000000) != 0x03000000) return(0);
808                 p[0]=((unsigned char)(l>> 8L))&0xFF;
809                 p[1]=((unsigned char)(l     ))&0xFF;
810                 }
811         return(2);
812         }
813
814 int ssl3_part_read(s,i)
815 SSL *s;
816 int i;
817         {
818         s->rwstate=SSL_READING;
819
820         if (i < 0)
821                 {
822                 return(i);
823                 }
824         else
825                 {
826                 s->init_num+=i;
827                 return(0);
828                 }
829         }
830
831 SSL_CIPHER *ssl3_choose_cipher(s,have,pref)
832 SSL *s;
833 STACK *have,*pref;
834         {
835         SSL_CIPHER *c,*ret=NULL;
836         int i,j,ok;
837         CERT *cert;
838         unsigned long alg,mask,emask;
839
840         /* Lets see which ciphers we can supported */
841         if (s->cert != NULL)
842                 cert=s->cert;
843         else
844                 cert=s->ctx->default_cert;
845
846         sk_set_cmp_func(pref,ssl_cipher_ptr_id_cmp);
847
848 #ifdef CIPHER_DEBUG
849         printf("Have:\n");
850         for(i=0 ; i < sk_num(pref) ; ++i)
851             {
852             c=(SSL_CIPHER *)sk_value(pref,i);
853             printf("%p:%s\n",c,c->name);
854             }
855 #endif
856
857         for (i=0; i<sk_num(have); i++)
858                 {
859                 c=(SSL_CIPHER *)sk_value(have,i);
860
861                 ssl_set_cert_masks(cert,s->ctx->default_cert,c);
862                 mask=cert->mask;
863                 emask=cert->export_mask;
864                         
865                 alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);
866                 if (SSL_IS_EXPORT(c->algorithms))
867                         {
868                         ok=((alg & emask) == alg)?1:0;
869 #ifdef CIPHER_DEBUG
870                         printf("%d:[%08lX:%08lX]%p:%s (export)\n",ok,alg,emask,
871                                c,c->name);
872 #endif
873                         }
874                 else
875                         {
876                         ok=((alg & mask) == alg)?1:0;
877 #ifdef CIPHER_DEBUG
878                         printf("%d:[%08lX:%08lX]%p:%s\n",ok,alg,mask,c,
879                                c->name);
880 #endif
881                         }
882
883                 if (!ok) continue;
884         
885                 j=sk_find(pref,(char *)c);
886                 if (j >= 0)
887                         {
888                         ret=(SSL_CIPHER *)sk_value(pref,j);
889                         break;
890                         }
891                 }
892         return(ret);
893         }
894
895 int ssl3_get_req_cert_type(s,p)
896 SSL *s;
897 unsigned char *p;
898         {
899         int ret=0;
900         unsigned long alg;
901
902         alg=s->s3->tmp.new_cipher->algorithms;
903
904 #ifndef NO_DH
905         if (alg & (SSL_kDHr|SSL_kEDH))
906                 {
907 #  ifndef NO_RSA
908                 p[ret++]=SSL3_CT_RSA_FIXED_DH;
909 #  endif
910 #  ifndef NO_DSA
911                 p[ret++]=SSL3_CT_DSS_FIXED_DH;
912 #  endif
913                 }
914         if ((s->version == SSL3_VERSION) &&
915                 (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr)))
916                 {
917 #  ifndef NO_RSA
918                 p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
919 #  endif
920 #  ifndef NO_DSA
921                 p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
922 #  endif
923                 }
924 #endif /* !NO_DH */
925 #ifndef NO_RSA
926         p[ret++]=SSL3_CT_RSA_SIGN;
927 #endif
928 #ifndef NO_DSA
929         p[ret++]=SSL3_CT_DSS_SIGN;
930 #endif
931         return(ret);
932         }
933
934 int ssl3_shutdown(s)
935 SSL *s;
936         {
937
938         /* Don't do anything much if we have not done the handshake or
939          * we don't want to send messages :-) */
940         if ((s->quiet_shutdown) || (s->state == SSL_ST_BEFORE))
941                 {
942                 s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
943                 return(1);
944                 }
945
946         if (!(s->shutdown & SSL_SENT_SHUTDOWN))
947                 {
948                 s->shutdown|=SSL_SENT_SHUTDOWN;
949 #if 1
950                 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);
951 #endif
952                 /* our shutdown alert has been sent now, and if it still needs
953                  * to be written, s->s3->alert_dispatch will be true */
954                 }
955         else if (s->s3->alert_dispatch)
956                 {
957                 /* resend it if not sent */
958 #if 1
959                 ssl3_dispatch_alert(s);
960 #endif
961                 }
962         else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN))
963                 {
964                 /* If we are waiting for a close from our peer, we are closed */
965                 ssl3_read_bytes(s,0,NULL,0);
966                 }
967
968         if ((s->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) &&
969                 !s->s3->alert_dispatch)
970                 return(1);
971         else
972                 return(0);
973         }
974
975 int ssl3_write(s,buf,len)
976 SSL *s;
977 const char *buf;
978 int len;
979         {
980         int ret,n;
981
982 #if 0
983         if (s->shutdown & SSL_SEND_SHUTDOWN)
984                 {
985                 s->rwstate=SSL_NOTHING;
986                 return(0);
987                 }
988 #endif
989         clear_sys_error();
990         if (s->s3->renegotiate) ssl3_renegotiate_check(s);
991
992         /* This is an experimental flag that sends the
993          * last handshake message in the same packet as the first
994          * use data - used to see if it helps the TCP protocol during
995          * session-id reuse */
996         /* The second test is because the buffer may have been removed */
997         if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio))
998                 {
999                 /* First time through, we write into the buffer */
1000                 if (s->s3->delay_buf_pop_ret == 0)
1001                         {
1002                         ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1003                                 (char *)buf,len);
1004                         if (ret <= 0) return(ret);
1005
1006                         s->s3->delay_buf_pop_ret=ret;
1007                         }
1008
1009                 s->rwstate=SSL_WRITING;
1010                 n=BIO_flush(s->wbio);
1011                 if (n <= 0) return(n);
1012                 s->rwstate=SSL_NOTHING;
1013
1014                 /* We have flushed the buffer, so remove it */
1015                 ssl_free_wbio_buffer(s);
1016                 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
1017
1018                 ret=s->s3->delay_buf_pop_ret;
1019                 s->s3->delay_buf_pop_ret=0;
1020                 }
1021         else
1022                 {
1023                 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1024                         (char *)buf,len);
1025                 if (ret <= 0) return(ret);
1026                 }
1027
1028         return(ret);
1029         }
1030
1031 int ssl3_read(s,buf,len)
1032 SSL *s;
1033 char *buf;
1034 int len;
1035         {
1036         int ret;
1037         
1038         clear_sys_error();
1039         if (s->s3->renegotiate) ssl3_renegotiate_check(s);
1040         s->s3->in_read_app_data=1;
1041         ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
1042         if ((ret == -1) && (s->s3->in_read_app_data == 0))
1043                 {
1044                 ERR_get_error(); /* clear the error */
1045                 s->s3->in_read_app_data=0;
1046                 s->in_handshake++;
1047                 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
1048                 s->in_handshake--;
1049                 }
1050         else
1051                 s->s3->in_read_app_data=0;
1052
1053         return(ret);
1054         }
1055
1056 int ssl3_peek(s,buf,len)
1057 SSL *s;
1058 char *buf;
1059 int len;
1060         {
1061         SSL3_RECORD *rr;
1062         int n;
1063
1064         rr= &(s->s3->rrec);
1065         if ((rr->length == 0) || (rr->type != SSL3_RT_APPLICATION_DATA))
1066                 {
1067                 n=ssl3_read(s,buf,1);
1068                 if (n <= 0) return(n);
1069                 rr->length++;
1070                 rr->off--;
1071                 }
1072
1073         if ((unsigned int)len > rr->length)
1074                 n=rr->length;
1075         else
1076                 n=len;
1077         memcpy(buf,&(rr->data[rr->off]),(unsigned int)n);
1078         return(n);
1079         }
1080
1081 int ssl3_renegotiate(s)
1082 SSL *s;
1083         {
1084         if (s->handshake_func == NULL)
1085                 return(1);
1086
1087         if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
1088                 return(0);
1089
1090         s->s3->renegotiate=1;
1091         return(1);
1092         }
1093
1094 int ssl3_renegotiate_check(s)
1095 SSL *s;
1096         {
1097         int ret=0;
1098
1099         if (s->s3->renegotiate)
1100                 {
1101                 if (    (s->s3->rbuf.left == 0) &&
1102                         (s->s3->wbuf.left == 0) &&
1103                         !SSL_in_init(s))
1104                         {
1105 /*
1106 if we are the server, and we have sent a 'RENEGOTIATE' message, we
1107 need to go to SSL_ST_ACCEPT.\1e
1108 */
1109                         /* SSL_ST_ACCEPT */
1110                         s->state=SSL_ST_RENEGOTIATE;
1111                         s->s3->renegotiate=0;
1112                         s->s3->num_renegotiations++;
1113                         s->s3->total_renegotiations++;
1114                         ret=1;
1115                         }
1116                 }
1117         return(ret);
1118         }
1119