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