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