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