Make NO_RSA compile with pedantic.
[openssl.git] / ssl / s3_srvr.c
1 /* ssl/s3_srvr.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 #define REUSE_CIPHER_BUG
60
61 #include <stdio.h>
62 #include <openssl/buffer.h>
63 #include <openssl/rand.h>
64 #include <openssl/objects.h>
65 #include <openssl/md5.h>
66 #include <openssl/sha.h>
67 #include <openssl/evp.h>
68 #include <openssl/x509.h>
69 #include "ssl_locl.h"
70
71 static SSL_METHOD *ssl3_get_server_method(int ver);
72 static int ssl3_get_client_hello(SSL *s);
73 static int ssl3_check_client_hello(SSL *s);
74 static int ssl3_send_server_hello(SSL *s);
75 static int ssl3_send_server_key_exchange(SSL *s);
76 static int ssl3_send_certificate_request(SSL *s);
77 static int ssl3_send_server_done(SSL *s);
78 static int ssl3_get_cert_verify(SSL *s);
79 static int ssl3_get_client_key_exchange(SSL *s);
80 static int ssl3_get_client_certificate(SSL *s);
81 static int ssl3_send_hello_request(SSL *s);
82
83 static SSL_METHOD *ssl3_get_server_method(int ver)
84         {
85         if (ver == SSL3_VERSION)
86                 return(SSLv3_server_method());
87         else
88                 return(NULL);
89         }
90
91 SSL_METHOD *SSLv3_server_method(void)
92         {
93         static int init=1;
94         static SSL_METHOD SSLv3_server_data;
95
96         if (init)
97                 {
98                 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
99                         sizeof(SSL_METHOD));
100                 SSLv3_server_data.ssl_accept=ssl3_accept;
101                 SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
102                 init=0;
103                 }
104         return(&SSLv3_server_data);
105         }
106
107 int ssl3_accept(SSL *s)
108         {
109         BUF_MEM *buf;
110         unsigned long l,Time=time(NULL);
111         void (*cb)()=NULL;
112         long num1;
113         int ret= -1;
114         int new_state,state,skip=0;
115
116         RAND_seed(&Time,sizeof(Time));
117         ERR_clear_error();
118         clear_sys_error();
119
120         if (s->info_callback != NULL)
121                 cb=s->info_callback;
122         else if (s->ctx->info_callback != NULL)
123                 cb=s->ctx->info_callback;
124
125         /* init things to blank */
126         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
127         s->in_handshake++;
128
129         if (s->cert == NULL)
130                 {
131                 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
132                 return(-1);
133                 }
134
135         for (;;)
136                 {
137                 state=s->state;
138
139                 switch (s->state)
140                         {
141                 case SSL_ST_RENEGOTIATE:
142                         s->new_session=1;
143                         /* s->state=SSL_ST_ACCEPT; */
144
145                 case SSL3_ST_SR_MS_SGC:
146                 case SSL_ST_BEFORE:
147                 case SSL_ST_ACCEPT:
148                 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
149                 case SSL_ST_OK|SSL_ST_ACCEPT:
150
151                         s->server=1;
152                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
153
154                         if ((s->version>>8) != 3)
155                                 abort();
156                         /* s->version=SSL3_VERSION; */
157                         s->type=SSL_ST_ACCEPT;
158
159                         if (s->init_buf == NULL)
160                                 {
161                                 if ((buf=BUF_MEM_new()) == NULL)
162                                         {
163                                         ret= -1;
164                                         goto end;
165                                         }
166                                 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
167                                         {
168                                         ret= -1;
169                                         goto end;
170                                         }
171                                 s->init_buf=buf;
172                                 }
173
174                         if (!ssl3_setup_buffers(s))
175                                 {
176                                 ret= -1;
177                                 goto end;
178                                 }
179
180                         /* Ok, we now need to push on a buffering BIO so that
181                          * the output is sent in a way that TCP likes :-)
182                          */
183                         if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
184
185                         s->init_num=0;
186
187                         if (s->state != SSL_ST_RENEGOTIATE)
188                                 {
189                                 if(s->state != SSL3_ST_SR_MS_SGC) ssl3_init_finished_mac(s);
190                                 s->state=SSL3_ST_SR_CLNT_HELLO_A;
191                                 s->ctx->stats.sess_accept++;
192                                 }
193                         else
194                                 {
195                                 s->ctx->stats.sess_accept_renegotiate++;
196                                 s->state=SSL3_ST_SW_HELLO_REQ_A;
197                                 }
198                         break;
199
200                 case SSL3_ST_SW_HELLO_REQ_A:
201                 case SSL3_ST_SW_HELLO_REQ_B:
202
203                         s->shutdown=0;
204                         ret=ssl3_send_hello_request(s);
205                         if (ret <= 0) goto end;
206                         s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
207                         s->state=SSL3_ST_SW_FLUSH;
208                         s->init_num=0;
209
210                         ssl3_init_finished_mac(s);
211                         break;
212
213                 case SSL3_ST_SW_HELLO_REQ_C:
214                         s->state=SSL_ST_OK;
215                         ret=1;
216                         goto end;
217                         /* break; */
218
219                 case SSL3_ST_SR_CLNT_HELLO_A:
220                 case SSL3_ST_SR_CLNT_HELLO_B:
221                 case SSL3_ST_SR_CLNT_HELLO_C:
222
223                         s->shutdown=0;
224                         ret=ssl3_get_client_hello(s);
225                         if (ret <= 0) goto end;
226                         s->state=SSL3_ST_SW_SRVR_HELLO_A;
227                         s->init_num=0;
228                         break;
229
230                 case SSL3_ST_SW_SRVR_HELLO_A:
231                 case SSL3_ST_SW_SRVR_HELLO_B:
232                         ret=ssl3_send_server_hello(s);
233                         if (ret <= 0) goto end;
234
235                         if (s->hit)
236                                 s->state=SSL3_ST_SW_CHANGE_A;
237                         else
238                                 s->state=SSL3_ST_SW_CERT_A;
239                         s->init_num=0;
240                         break;
241
242                 case SSL3_ST_SW_CERT_A:
243                 case SSL3_ST_SW_CERT_B:
244                         /* Check if it is anon DH */
245                         if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
246                                 {
247                                 ret=ssl3_send_server_certificate(s);
248                                 if (ret <= 0) goto end;
249                                 }
250                         else
251                                 skip=1;
252                         s->state=SSL3_ST_SW_KEY_EXCH_A;
253                         s->init_num=0;
254                         break;
255
256                 case SSL3_ST_SW_KEY_EXCH_A:
257                 case SSL3_ST_SW_KEY_EXCH_B:
258                         l=s->s3->tmp.new_cipher->algorithms;
259
260                         /* clear this, it may get reset by
261                          * send_server_key_exchange */
262                         if (s->options & SSL_OP_EPHEMERAL_RSA)
263                                 s->s3->tmp.use_rsa_tmp=1;
264                         else
265                                 s->s3->tmp.use_rsa_tmp=0;
266
267                         /* only send if a DH key exchange, fortezza or
268                          * RSA but we have a sign only certificate */
269                         if (s->s3->tmp.use_rsa_tmp
270                             || (l & (SSL_DH|SSL_kFZA))
271                             || ((l & SSL_kRSA)
272                                 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
273                                     || (SSL_IS_EXPORT(l)
274                                         && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_EXPORT_PKEYLENGTH(l)
275                                         )
276                                     )
277                                 )
278                             )
279                                 {
280                                 ret=ssl3_send_server_key_exchange(s);
281                                 if (ret <= 0) goto end;
282                                 }
283                         else
284                                 skip=1;
285
286                         s->state=SSL3_ST_SW_CERT_REQ_A;
287                         s->init_num=0;
288                         break;
289
290                 case SSL3_ST_SW_CERT_REQ_A:
291                 case SSL3_ST_SW_CERT_REQ_B:
292                         if (/* don't request cert unless asked for it: */
293                                 !(s->verify_mode & SSL_VERIFY_PEER) ||
294                                 /* if SSL_VERIFY_CLIENT_ONCE is set,
295                                  * don't request cert during re-negotiation: */
296                                 ((s->session->peer != NULL) &&
297                                  (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
298                                 /* never request cert in anonymous ciphersuites
299                                  * (see section "Certificate request" in SSL 3 drafts
300                                  * and in RFC 2246): */
301                                 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
302                                  /* ... except when the application insists on verification
303                                   * (against the specs, but s3_clnt.c accepts this for SSL 3) */
304                                  !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)))
305                                 {
306                                 /* no cert request */
307                                 skip=1;
308                                 s->s3->tmp.cert_request=0;
309                                 s->state=SSL3_ST_SW_SRVR_DONE_A;
310                                 }
311                         else
312                                 {
313                                 s->s3->tmp.cert_request=1;
314                                 ret=ssl3_send_certificate_request(s);
315                                 if (ret <= 0) goto end;
316                                 s->state=SSL3_ST_SW_SRVR_DONE_A;
317                                 s->init_num=0;
318                                 }
319                         break;
320
321                 case SSL3_ST_SW_SRVR_DONE_A:
322                 case SSL3_ST_SW_SRVR_DONE_B:
323                         ret=ssl3_send_server_done(s);
324                         if (ret <= 0) goto end;
325                         s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
326                         s->state=SSL3_ST_SW_FLUSH;
327                         s->init_num=0;
328                         break;
329                 
330                 case SSL3_ST_SW_FLUSH:
331                         /* number of bytes to be flushed */
332                         num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
333                         if (num1 > 0)
334                                 {
335                                 s->rwstate=SSL_WRITING;
336                                 num1=BIO_flush(s->wbio);
337                                 if (num1 <= 0) { ret= -1; goto end; }
338                                 s->rwstate=SSL_NOTHING;
339                                 }
340
341                         s->state=s->s3->tmp.next_state;
342                         break;
343
344                 case SSL3_ST_SR_CERT_A:
345                 case SSL3_ST_SR_CERT_B:
346                         /* Check for second client hello if MS SGC */
347                         ret = ssl3_check_client_hello(s);
348                         if(ret <= 0) goto end;
349                         if(ret == 2) s->state = SSL3_ST_SR_MS_SGC;
350                         else {
351                                 /* could be sent for a DH cert, even if we
352                                  * have not asked for it :-) */
353                                 ret=ssl3_get_client_certificate(s);
354                                 if (ret <= 0) goto end;
355                                 s->init_num=0;
356                                 s->state=SSL3_ST_SR_KEY_EXCH_A;
357                         }
358                         break;
359
360                 case SSL3_ST_SR_KEY_EXCH_A:
361                 case SSL3_ST_SR_KEY_EXCH_B:
362                         ret=ssl3_get_client_key_exchange(s);
363                         if (ret <= 0) goto end;
364                         s->state=SSL3_ST_SR_CERT_VRFY_A;
365                         s->init_num=0;
366
367                         /* We need to get hashes here so if there is
368                          * a client cert, it can be verified */ 
369                         s->method->ssl3_enc->cert_verify_mac(s,
370                                 &(s->s3->finish_dgst1),
371                                 &(s->s3->tmp.cert_verify_md[0]));
372                         s->method->ssl3_enc->cert_verify_mac(s,
373                                 &(s->s3->finish_dgst2),
374                                 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
375
376                         break;
377
378                 case SSL3_ST_SR_CERT_VRFY_A:
379                 case SSL3_ST_SR_CERT_VRFY_B:
380
381                         /* we should decide if we expected this one */
382                         ret=ssl3_get_cert_verify(s);
383                         if (ret <= 0) goto end;
384
385                         s->state=SSL3_ST_SR_FINISHED_A;
386                         s->init_num=0;
387                         break;
388
389                 case SSL3_ST_SR_FINISHED_A:
390                 case SSL3_ST_SR_FINISHED_B:
391                         ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
392                                 SSL3_ST_SR_FINISHED_B);
393                         if (ret <= 0) goto end;
394                         if (s->hit)
395                                 s->state=SSL_ST_OK;
396                         else
397                                 s->state=SSL3_ST_SW_CHANGE_A;
398                         s->init_num=0;
399                         break;
400
401                 case SSL3_ST_SW_CHANGE_A:
402                 case SSL3_ST_SW_CHANGE_B:
403
404                         s->session->cipher=s->s3->tmp.new_cipher;
405                         if (!s->method->ssl3_enc->setup_key_block(s))
406                                 { ret= -1; goto end; }
407
408                         ret=ssl3_send_change_cipher_spec(s,
409                                 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
410
411                         if (ret <= 0) goto end;
412                         s->state=SSL3_ST_SW_FINISHED_A;
413                         s->init_num=0;
414
415                         if (!s->method->ssl3_enc->change_cipher_state(s,
416                                 SSL3_CHANGE_CIPHER_SERVER_WRITE))
417                                 {
418                                 ret= -1;
419                                 goto end;
420                                 }
421
422                         break;
423
424                 case SSL3_ST_SW_FINISHED_A:
425                 case SSL3_ST_SW_FINISHED_B:
426                         ret=ssl3_send_finished(s,
427                                 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
428                                 s->method->ssl3_enc->server_finished_label,
429                                 s->method->ssl3_enc->server_finished_label_len);
430                         if (ret <= 0) goto end;
431                         s->state=SSL3_ST_SW_FLUSH;
432                         if (s->hit)
433                                 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
434                         else
435                                 s->s3->tmp.next_state=SSL_ST_OK;
436                         s->init_num=0;
437                         break;
438
439                 case SSL_ST_OK:
440                         /* clean a few things up */
441                         ssl3_cleanup_key_block(s);
442
443                         BUF_MEM_free(s->init_buf);
444                         s->init_buf=NULL;
445
446                         /* remove buffering on output */
447                         ssl_free_wbio_buffer(s);
448
449                         s->new_session=0;
450                         s->init_num=0;
451
452                         ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
453
454                         s->ctx->stats.sess_accept_good++;
455                         /* s->server=1; */
456                         s->handshake_func=ssl3_accept;
457                         ret=1;
458
459                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
460
461                         goto end;
462                         /* break; */
463
464                 default:
465                         SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
466                         ret= -1;
467                         goto end;
468                         /* break; */
469                         }
470                 
471                 if (!s->s3->tmp.reuse_message && !skip)
472                         {
473                         if (s->debug)
474                                 {
475                                 if ((ret=BIO_flush(s->wbio)) <= 0)
476                                         goto end;
477                                 }
478
479
480                         if ((cb != NULL) && (s->state != state))
481                                 {
482                                 new_state=s->state;
483                                 s->state=state;
484                                 cb(s,SSL_CB_ACCEPT_LOOP,1);
485                                 s->state=new_state;
486                                 }
487                         }
488                 skip=0;
489                 }
490 end:
491         /* BIO_flush(s->wbio); */
492
493         if (cb != NULL)
494                 cb(s,SSL_CB_ACCEPT_EXIT,ret);
495         s->in_handshake--;
496         return(ret);
497         }
498
499 static int ssl3_send_hello_request(SSL *s)
500         {
501         unsigned char *p;
502
503         if (s->state == SSL3_ST_SW_HELLO_REQ_A)
504                 {
505                 p=(unsigned char *)s->init_buf->data;
506                 *(p++)=SSL3_MT_CLIENT_REQUEST;
507                 *(p++)=0;
508                 *(p++)=0;
509                 *(p++)=0;
510
511                 s->state=SSL3_ST_SW_HELLO_REQ_B;
512                 /* number of bytes to write */
513                 s->init_num=4;
514                 s->init_off=0;
515                 }
516
517         /* SSL3_ST_SW_HELLO_REQ_B */
518         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
519         }
520
521 static int ssl3_check_client_hello(SSL *s)
522         {
523         int ok;
524         long n;
525
526         n=ssl3_get_message(s,
527                 SSL3_ST_SR_CERT_A,
528                 SSL3_ST_SR_CERT_B,
529                 -1,
530                 SSL3_RT_MAX_PLAIN_LENGTH,
531                 &ok);
532         if (!ok) return((int)n);
533         s->s3->tmp.reuse_message = 1;
534         if(s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) return 2;
535         return 1;
536 }
537
538 static int ssl3_get_client_hello(SSL *s)
539         {
540         int i,j,ok,al,ret= -1;
541         long n;
542         unsigned long id;
543         unsigned char *p,*d,*q;
544         SSL_CIPHER *c;
545         SSL_COMP *comp=NULL;
546         STACK_OF(SSL_CIPHER) *ciphers=NULL;
547
548         /* We do this so that we will respond with our native type.
549          * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
550          * This down switching should be handled by a different method.
551          * If we are SSLv3, we will respond with SSLv3, even if prompted with
552          * TLSv1.
553          */
554         if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
555                 {
556                 s->first_packet=1;
557                 s->state=SSL3_ST_SR_CLNT_HELLO_B;
558                 }
559         n=ssl3_get_message(s,
560                 SSL3_ST_SR_CLNT_HELLO_B,
561                 SSL3_ST_SR_CLNT_HELLO_C,
562                 SSL3_MT_CLIENT_HELLO,
563                 SSL3_RT_MAX_PLAIN_LENGTH,
564                 &ok);
565
566         if (!ok) return((int)n);
567         d=p=(unsigned char *)s->init_buf->data;
568
569         /* use version from inside client hello, not from record header
570          * (may differ: see RFC 2246, Appendix E, second paragraph) */
571         s->client_version=(((int)p[0])<<8)|(int)p[1];
572         p+=2;
573
574         /* load the client random */
575         memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
576         p+=SSL3_RANDOM_SIZE;
577
578         /* get the session-id */
579         j= *(p++);
580
581         s->hit=0;
582         if (j == 0)
583                 {
584                 if (!ssl_get_new_session(s,1))
585                         goto err;
586                 }
587         else
588                 {
589                 i=ssl_get_prev_session(s,p,j);
590                 if (i == 1)
591                         { /* previous session */
592                         s->hit=1;
593                         }
594                 else if (i == -1)
595                         goto err;
596                 else /* i == 0 */
597                         {
598                         if (!ssl_get_new_session(s,1))
599                                 goto err;
600                         }
601                 }
602
603         p+=j;
604         n2s(p,i);
605         if ((i == 0) && (j != 0))
606                 {
607                 /* we need a cipher if we are not resuming a session */
608                 al=SSL_AD_ILLEGAL_PARAMETER;
609                 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
610                 goto f_err;
611                 }
612         if ((i+p) > (d+n))
613                 {
614                 /* not enough data */
615                 al=SSL_AD_DECODE_ERROR;
616                 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
617                 goto f_err;
618                 }
619         if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
620                 == NULL))
621                 {
622                 goto err;
623                 }
624         p+=i;
625
626         /* If it is a hit, check that the cipher is in the list */
627         if ((s->hit) && (i > 0))
628                 {
629                 j=0;
630                 id=s->session->cipher->id;
631
632 #ifdef CIPHER_DEBUG
633                 printf("client sent %d ciphers\n",sk_num(ciphers));
634 #endif
635                 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
636                         {
637                         c=sk_SSL_CIPHER_value(ciphers,i);
638 #ifdef CIPHER_DEBUG
639                         printf("client [%2d of %2d]:%s\n",
640                                 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
641 #endif
642                         if (c->id == id)
643                                 {
644                                 j=1;
645                                 break;
646                                 }
647                         }
648                 if (j == 0)
649                         {
650                         if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
651                                 {
652                                 /* Very bad for multi-threading.... */
653                                 s->session->cipher=sk_SSL_CIPHER_value(ciphers,
654                                                                        0);
655                                 }
656                         else
657                                 {
658                                 /* we need to have the cipher in the cipher
659                                  * list if we are asked to reuse it */
660                                 al=SSL_AD_ILLEGAL_PARAMETER;
661                                 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
662                                 goto f_err;
663                                 }
664                         }
665                 }
666
667         /* compression */
668         i= *(p++);
669         q=p;
670         for (j=0; j<i; j++)
671                 {
672                 if (p[j] == 0) break;
673                 }
674
675         p+=i;
676         if (j >= i)
677                 {
678                 /* no compress */
679                 al=SSL_AD_DECODE_ERROR;
680                 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
681                 goto f_err;
682                 }
683
684         /* Worst case, we will use the NULL compression, but if we have other
685          * options, we will now look for them.  We have i-1 compression
686          * algorithms from the client, starting at q. */
687         s->s3->tmp.new_compression=NULL;
688         if (s->ctx->comp_methods != NULL)
689                 { /* See if we have a match */
690                 int m,nn,o,v,done=0;
691
692                 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
693                 for (m=0; m<nn; m++)
694                         {
695                         comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
696                         v=comp->id;
697                         for (o=0; o<i; o++)
698                                 {
699                                 if (v == q[o])
700                                         {
701                                         done=1;
702                                         break;
703                                         }
704                                 }
705                         if (done) break;
706                         }
707                 if (done)
708                         s->s3->tmp.new_compression=comp;
709                 else
710                         comp=NULL;
711                 }
712
713         /* TLS does not mind if there is extra stuff */
714         if (s->version == SSL3_VERSION)
715                 {
716                 if (p > (d+n))
717                         {
718                         /* wrong number of bytes,
719                          * there could be more to follow */
720                         al=SSL_AD_DECODE_ERROR;
721                         SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
722                         goto f_err;
723                         }
724                 }
725
726         /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must
727          * pick a cipher */
728
729         if (!s->hit)
730                 {
731                 s->session->compress_meth=(comp == NULL)?0:comp->id;
732                 if (s->session->ciphers != NULL)
733                         sk_SSL_CIPHER_free(s->session->ciphers);
734                 s->session->ciphers=ciphers;
735                 if (ciphers == NULL)
736                         {
737                         al=SSL_AD_ILLEGAL_PARAMETER;
738                         SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
739                         goto f_err;
740                         }
741                 ciphers=NULL;
742                 c=ssl3_choose_cipher(s,s->session->ciphers,
743                                      ssl_get_ciphers_by_id(s));
744
745                 if (c == NULL)
746                         {
747                         al=SSL_AD_HANDSHAKE_FAILURE;
748                         SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
749                         goto f_err;
750                         }
751                 s->s3->tmp.new_cipher=c;
752                 }
753         else
754                 {
755                 /* Session-id reuse */
756 #ifdef REUSE_CIPHER_BUG
757                 STACK_OF(SSL_CIPHER) *sk;
758                 SSL_CIPHER *nc=NULL;
759                 SSL_CIPHER *ec=NULL;
760
761                 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
762                         {
763                         sk=s->session->ciphers;
764                         for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
765                                 {
766                                 c=sk_SSL_CIPHER_value(sk,i);
767                                 if (c->algorithms & SSL_eNULL)
768                                         nc=c;
769                                 if (SSL_C_IS_EXPORT(c))
770                                         ec=c;
771                                 }
772                         if (nc != NULL)
773                                 s->s3->tmp.new_cipher=nc;
774                         else if (ec != NULL)
775                                 s->s3->tmp.new_cipher=ec;
776                         else
777                                 s->s3->tmp.new_cipher=s->session->cipher;
778                         }
779                 else
780 #endif
781                 s->s3->tmp.new_cipher=s->session->cipher;
782                 }
783         
784         /* we now have the following setup. 
785          * client_random
786          * cipher_list          - our prefered list of ciphers
787          * ciphers              - the clients prefered list of ciphers
788          * compression          - basically ignored right now
789          * ssl version is set   - sslv3
790          * s->session           - The ssl session has been setup.
791          * s->hit               - sesson reuse flag
792          * s->tmp.new_cipher    - the new cipher to use.
793          */
794
795         ret=1;
796         if (0)
797                 {
798 f_err:
799                 ssl3_send_alert(s,SSL3_AL_FATAL,al);
800                 }
801 err:
802         if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
803         return(ret);
804         }
805
806 static int ssl3_send_server_hello(SSL *s)
807         {
808         unsigned char *buf;
809         unsigned char *p,*d;
810         int i,sl;
811         unsigned long l,Time;
812
813         if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
814                 {
815                 buf=(unsigned char *)s->init_buf->data;
816                 p=s->s3->server_random;
817                 Time=time(NULL);                        /* Time */
818                 l2n(Time,p);
819                 RAND_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
820                 /* Do the message type and length last */
821                 d=p= &(buf[4]);
822
823                 *(p++)=s->version>>8;
824                 *(p++)=s->version&0xff;
825
826                 /* Random stuff */
827                 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
828                 p+=SSL3_RANDOM_SIZE;
829
830                 /* now in theory we have 3 options to sending back the
831                  * session id.  If it is a re-use, we send back the
832                  * old session-id, if it is a new session, we send
833                  * back the new session-id or we send back a 0 length
834                  * session-id if we want it to be single use.
835                  * Currently I will not implement the '0' length session-id
836                  * 12-Jan-98 - I'll now support the '0' length stuff.
837                  */
838                 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
839                         s->session->session_id_length=0;
840
841                 sl=s->session->session_id_length;
842                 *(p++)=sl;
843                 memcpy(p,s->session->session_id,sl);
844                 p+=sl;
845
846                 /* put the cipher */
847                 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
848                 p+=i;
849
850                 /* put the compression method */
851                 if (s->s3->tmp.new_compression == NULL)
852                         *(p++)=0;
853                 else
854                         *(p++)=s->s3->tmp.new_compression->id;
855
856                 /* do the header */
857                 l=(p-d);
858                 d=buf;
859                 *(d++)=SSL3_MT_SERVER_HELLO;
860                 l2n3(l,d);
861
862                 s->state=SSL3_ST_CW_CLNT_HELLO_B;
863                 /* number of bytes to write */
864                 s->init_num=p-buf;
865                 s->init_off=0;
866                 }
867
868         /* SSL3_ST_CW_CLNT_HELLO_B */
869         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
870         }
871
872 static int ssl3_send_server_done(SSL *s)
873         {
874         unsigned char *p;
875
876         if (s->state == SSL3_ST_SW_SRVR_DONE_A)
877                 {
878                 p=(unsigned char *)s->init_buf->data;
879
880                 /* do the header */
881                 *(p++)=SSL3_MT_SERVER_DONE;
882                 *(p++)=0;
883                 *(p++)=0;
884                 *(p++)=0;
885
886                 s->state=SSL3_ST_SW_SRVR_DONE_B;
887                 /* number of bytes to write */
888                 s->init_num=4;
889                 s->init_off=0;
890                 }
891
892         /* SSL3_ST_CW_CLNT_HELLO_B */
893         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
894         }
895
896 static int ssl3_send_server_key_exchange(SSL *s)
897         {
898 #ifndef NO_RSA
899         unsigned char *q;
900         int j,num;
901         RSA *rsa;
902         unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
903         unsigned int u;
904 #endif
905 #ifndef NO_DH
906         DH *dh,*dhp;
907 #endif
908         EVP_PKEY *pkey;
909         unsigned char *p,*d;
910         int al,i;
911         unsigned long type;
912         int n;
913         CERT *cert;
914         BIGNUM *r[4];
915         int nr[4],kn;
916         BUF_MEM *buf;
917         EVP_MD_CTX md_ctx;
918
919         if (s->state == SSL3_ST_SW_KEY_EXCH_A)
920                 {
921                 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
922                 cert=s->cert;
923
924                 buf=s->init_buf;
925
926                 r[0]=r[1]=r[2]=r[3]=NULL;
927                 n=0;
928 #ifndef NO_RSA
929                 if (type & SSL_kRSA)
930                         {
931                         rsa=cert->rsa_tmp;
932                         if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
933                                 {
934                                 rsa=s->cert->rsa_tmp_cb(s,
935                                       SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
936                                       SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
937                                 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
938                                 cert->rsa_tmp=rsa;
939                                 }
940                         if (rsa == NULL)
941                                 {
942                                 al=SSL_AD_HANDSHAKE_FAILURE;
943                                 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
944                                 goto f_err;
945                                 }
946                         r[0]=rsa->n;
947                         r[1]=rsa->e;
948                         s->s3->tmp.use_rsa_tmp=1;
949                         }
950                 else
951 #endif
952 #ifndef NO_DH
953                         if (type & SSL_kEDH)
954                         {
955                         dhp=cert->dh_tmp;
956                         if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
957                                 dhp=s->cert->dh_tmp_cb(s,
958                                       !SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
959                                       SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
960                         if (dhp == NULL)
961                                 {
962                                 al=SSL_AD_HANDSHAKE_FAILURE;
963                                 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
964                                 goto f_err;
965                                 }
966                         if ((dh=DHparams_dup(dhp)) == NULL)
967                                 {
968                                 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
969                                 goto err;
970                                 }
971
972                         s->s3->tmp.dh=dh;
973                         if ((dhp->pub_key == NULL ||
974                              dhp->priv_key == NULL ||
975                              (s->options & SSL_OP_SINGLE_DH_USE)))
976                                 {
977                                 if(!DH_generate_key(dh))
978                                     {
979                                     SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
980                                            ERR_R_DH_LIB);
981                                     goto err;
982                                     }
983                                 }
984                         else
985                                 {
986                                 dh->pub_key=BN_dup(dhp->pub_key);
987                                 dh->priv_key=BN_dup(dhp->priv_key);
988                                 if ((dh->pub_key == NULL) ||
989                                         (dh->priv_key == NULL))
990                                         {
991                                         SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
992                                         goto err;
993                                         }
994                                 }
995                         r[0]=dh->p;
996                         r[1]=dh->g;
997                         r[2]=dh->pub_key;
998                         }
999                 else 
1000 #endif
1001                         {
1002                         al=SSL_AD_HANDSHAKE_FAILURE;
1003                         SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1004                         goto f_err;
1005                         }
1006                 for (i=0; r[i] != NULL; i++)
1007                         {
1008                         nr[i]=BN_num_bytes(r[i]);
1009                         n+=2+nr[i];
1010                         }
1011
1012                 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1013                         {
1014                         if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1015                                 == NULL)
1016                                 {
1017                                 al=SSL_AD_DECODE_ERROR;
1018                                 goto f_err;
1019                                 }
1020                         kn=EVP_PKEY_size(pkey);
1021                         }
1022                 else
1023                         {
1024                         pkey=NULL;
1025                         kn=0;
1026                         }
1027
1028                 if (!BUF_MEM_grow(buf,n+4+kn))
1029                         {
1030                         SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1031                         goto err;
1032                         }
1033                 d=(unsigned char *)s->init_buf->data;
1034                 p= &(d[4]);
1035
1036                 for (i=0; r[i] != NULL; i++)
1037                         {
1038                         s2n(nr[i],p);
1039                         BN_bn2bin(r[i],p);
1040                         p+=nr[i];
1041                         }
1042
1043                 /* not anonymous */
1044                 if (pkey != NULL)
1045                         {
1046                         /* n is the length of the params, they start at &(d[4])
1047                          * and p points to the space at the end. */
1048 #ifndef NO_RSA
1049                         if (pkey->type == EVP_PKEY_RSA)
1050                                 {
1051                                 q=md_buf;
1052                                 j=0;
1053                                 for (num=2; num > 0; num--)
1054                                         {
1055                                         EVP_DigestInit(&md_ctx,(num == 2)
1056                                                 ?s->ctx->md5:s->ctx->sha1);
1057                                         EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1058                                         EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1059                                         EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1060                                         EVP_DigestFinal(&md_ctx,q,
1061                                                 (unsigned int *)&i);
1062                                         q+=i;
1063                                         j+=i;
1064                                         }
1065                                 if (RSA_sign(NID_md5_sha1, md_buf, j,
1066                                         &(p[2]), &u, pkey->pkey.rsa) <= 0)
1067                                         {
1068                                         SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1069                                         goto err;
1070                                         }
1071                                 s2n(u,p);
1072                                 n+=u+2;
1073                                 }
1074                         else
1075 #endif
1076 #if !defined(NO_DSA)
1077                                 if (pkey->type == EVP_PKEY_DSA)
1078                                 {
1079                                 /* lets do DSS */
1080                                 EVP_SignInit(&md_ctx,EVP_dss1());
1081                                 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1082                                 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1083                                 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1084                                 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1085                                         (unsigned int *)&i,pkey))
1086                                         {
1087                                         SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1088                                         goto err;
1089                                         }
1090                                 s2n(i,p);
1091                                 n+=i+2;
1092                                 }
1093                         else
1094 #endif
1095                                 {
1096                                 /* Is this error check actually needed? */
1097                                 al=SSL_AD_HANDSHAKE_FAILURE;
1098                                 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1099                                 goto f_err;
1100                                 }
1101                         }
1102
1103                 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1104                 l2n3(n,d);
1105
1106                 /* we should now have things packed up, so lets send
1107                  * it off */
1108                 s->init_num=n+4;
1109                 s->init_off=0;
1110                 }
1111
1112         /* SSL3_ST_SW_KEY_EXCH_B */
1113         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1114 f_err:
1115         ssl3_send_alert(s,SSL3_AL_FATAL,al);
1116 err:
1117         return(-1);
1118         }
1119
1120 static int ssl3_send_certificate_request(SSL *s)
1121         {
1122         unsigned char *p,*d;
1123         int i,j,nl,off,n;
1124         STACK_OF(X509_NAME) *sk=NULL;
1125         X509_NAME *name;
1126         BUF_MEM *buf;
1127
1128         if (s->state == SSL3_ST_SW_CERT_REQ_A)
1129                 {
1130                 buf=s->init_buf;
1131
1132                 d=p=(unsigned char *)&(buf->data[4]);
1133
1134                 /* get the list of acceptable cert types */
1135                 p++;
1136                 n=ssl3_get_req_cert_type(s,p);
1137                 d[0]=n;
1138                 p+=n;
1139                 n++;
1140
1141                 off=n;
1142                 p+=2;
1143                 n+=2;
1144
1145                 sk=SSL_get_client_CA_list(s);
1146                 nl=0;
1147                 if (sk != NULL)
1148                         {
1149                         for (i=0; i<sk_X509_NAME_num(sk); i++)
1150                                 {
1151                                 name=sk_X509_NAME_value(sk,i);
1152                                 j=i2d_X509_NAME(name,NULL);
1153                                 if (!BUF_MEM_grow(buf,4+n+j+2))
1154                                         {
1155                                         SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1156                                         goto err;
1157                                         }
1158                                 p=(unsigned char *)&(buf->data[4+n]);
1159                                 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1160                                         {
1161                                         s2n(j,p);
1162                                         i2d_X509_NAME(name,&p);
1163                                         n+=2+j;
1164                                         nl+=2+j;
1165                                         }
1166                                 else
1167                                         {
1168                                         d=p;
1169                                         i2d_X509_NAME(name,&p);
1170                                         j-=2; s2n(j,d); j+=2;
1171                                         n+=j;
1172                                         nl+=j;
1173                                         }
1174                                 }
1175                         }
1176                 /* else no CA names */
1177                 p=(unsigned char *)&(buf->data[4+off]);
1178                 s2n(nl,p);
1179
1180                 d=(unsigned char *)buf->data;
1181                 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1182                 l2n3(n,d);
1183
1184                 /* we should now have things packed up, so lets send
1185                  * it off */
1186
1187                 s->init_num=n+4;
1188                 s->init_off=0;
1189                 }
1190
1191         /* SSL3_ST_SW_CERT_REQ_B */
1192         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1193 err:
1194         return(-1);
1195         }
1196
1197 static int ssl3_get_client_key_exchange(SSL *s)
1198         {
1199         int i,al,ok;
1200         long n;
1201         unsigned long l;
1202         unsigned char *p;
1203 #ifndef NO_RSA
1204         RSA *rsa=NULL;
1205         EVP_PKEY *pkey=NULL;
1206 #endif
1207 #ifndef NO_DH
1208         BIGNUM *pub=NULL;
1209         DH *dh_srvr;
1210 #endif
1211
1212         n=ssl3_get_message(s,
1213                 SSL3_ST_SR_KEY_EXCH_A,
1214                 SSL3_ST_SR_KEY_EXCH_B,
1215                 SSL3_MT_CLIENT_KEY_EXCHANGE,
1216                 400, /* ???? */
1217                 &ok);
1218
1219         if (!ok) return((int)n);
1220         p=(unsigned char *)s->init_buf->data;
1221
1222         l=s->s3->tmp.new_cipher->algorithms;
1223
1224 #ifndef NO_RSA
1225         if (l & SSL_kRSA)
1226                 {
1227                 /* FIX THIS UP EAY EAY EAY EAY */
1228                 if (s->s3->tmp.use_rsa_tmp)
1229                         {
1230                         if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1231                                 rsa=s->cert->rsa_tmp;
1232                         /* Don't do a callback because rsa_tmp should
1233                          * be sent already */
1234                         if (rsa == NULL)
1235                                 {
1236                                 al=SSL_AD_HANDSHAKE_FAILURE;
1237                                 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1238                                 goto f_err;
1239
1240                                 }
1241                         }
1242                 else
1243                         {
1244                         pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1245                         if (    (pkey == NULL) ||
1246                                 (pkey->type != EVP_PKEY_RSA) ||
1247                                 (pkey->pkey.rsa == NULL))
1248                                 {
1249                                 al=SSL_AD_HANDSHAKE_FAILURE;
1250                                 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1251                                 goto f_err;
1252                                 }
1253                         rsa=pkey->pkey.rsa;
1254                         }
1255
1256                 /* TLS */
1257                 if (s->version > SSL3_VERSION)
1258                         {
1259                         n2s(p,i);
1260                         if (n != i+2)
1261                                 {
1262                                 if (!(s->options & SSL_OP_TLS_D5_BUG))
1263                                         {
1264                                         SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1265                                         goto err;
1266                                         }
1267                                 else
1268                                         p-=2;
1269                                 }
1270                         else
1271                                 n=i;
1272                         }
1273
1274                 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1275
1276 #if 0
1277                 /* If a bad decrypt, use a random master key */
1278                 if ((i != SSL_MAX_MASTER_KEY_LENGTH) ||
1279                         ((p[0] != (s->client_version>>8)) ||
1280                          (p[1] != (s->client_version & 0xff))))
1281                         {
1282                         int bad=1;
1283
1284                         if ((i == SSL_MAX_MASTER_KEY_LENGTH) &&
1285                                 (p[0] == (s->version>>8)) &&
1286                                 (p[1] == 0))
1287                                 {
1288                                 if (s->options & SSL_OP_TLS_ROLLBACK_BUG)
1289                                         bad=0;
1290                                 }
1291                         if (bad)
1292                                 {
1293                                 p[0]=(s->version>>8);
1294                                 p[1]=(s->version & 0xff);
1295                                 RAND_bytes(&(p[2]),SSL_MAX_MASTER_KEY_LENGTH-2);
1296                                 i=SSL_MAX_MASTER_KEY_LENGTH;
1297                                 }
1298                         /* else, an SSLeay bug, ssl only server, tls client */
1299                         }
1300 #else
1301                 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1302                         {
1303                         al=SSL_AD_DECODE_ERROR;
1304                         SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1305                         goto f_err;
1306                         }
1307
1308                 if ((p[0] != (s->client_version>>8)) || (p[1] != (s->client_version & 0xff)))
1309                         {
1310                         al=SSL_AD_DECODE_ERROR;
1311                         SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1312                         goto f_err;
1313                         }
1314 #endif
1315
1316                 s->session->master_key_length=
1317                         s->method->ssl3_enc->generate_master_secret(s,
1318                                 s->session->master_key,
1319                                 p,i);
1320                 memset(p,0,i);
1321                 }
1322         else
1323 #endif
1324 #ifndef NO_DH
1325                 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1326                 {
1327                 n2s(p,i);
1328                 if (n != i+2)
1329                         {
1330                         if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1331                                 {
1332                                 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1333                                 goto err;
1334                                 }
1335                         else
1336                                 {
1337                                 p-=2;
1338                                 i=(int)n;
1339                                 }
1340                         }
1341
1342                 if (n == 0L) /* the parameters are in the cert */
1343                         {
1344                         al=SSL_AD_HANDSHAKE_FAILURE;
1345                         SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1346                         goto f_err;
1347                         }
1348                 else
1349                         {
1350                         if (s->s3->tmp.dh == NULL)
1351                                 {
1352                                 al=SSL_AD_HANDSHAKE_FAILURE;
1353                                 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1354                                 goto f_err;
1355                                 }
1356                         else
1357                                 dh_srvr=s->s3->tmp.dh;
1358                         }
1359
1360                 pub=BN_bin2bn(p,i,NULL);
1361                 if (pub == NULL)
1362                         {
1363                         SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1364                         goto err;
1365                         }
1366
1367                 i=DH_compute_key(p,pub,dh_srvr);
1368
1369                 if (i <= 0)
1370                         {
1371                         SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1372                         goto err;
1373                         }
1374
1375                 DH_free(s->s3->tmp.dh);
1376                 s->s3->tmp.dh=NULL;
1377
1378                 BN_clear_free(pub);
1379                 pub=NULL;
1380                 s->session->master_key_length=
1381                         s->method->ssl3_enc->generate_master_secret(s,
1382                                 s->session->master_key,p,i);
1383                 }
1384         else
1385 #endif
1386                 {
1387                 al=SSL_AD_HANDSHAKE_FAILURE;
1388                 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE);
1389                 goto f_err;
1390                 }
1391
1392         return(1);
1393 f_err:
1394         ssl3_send_alert(s,SSL3_AL_FATAL,al);
1395 #if !defined(NO_DH) || !defined(NO_RSA)
1396 err:
1397 #endif
1398         return(-1);
1399         }
1400
1401 static int ssl3_get_cert_verify(SSL *s)
1402         {
1403         EVP_PKEY *pkey=NULL;
1404         unsigned char *p;
1405         int al,ok,ret=0;
1406         long n;
1407         int type=0,i,j;
1408         X509 *peer;
1409
1410         n=ssl3_get_message(s,
1411                 SSL3_ST_SR_CERT_VRFY_A,
1412                 SSL3_ST_SR_CERT_VRFY_B,
1413                 -1,
1414                 512, /* 512? */
1415                 &ok);
1416
1417         if (!ok) return((int)n);
1418
1419         if (s->session->peer != NULL)
1420                 {
1421                 peer=s->session->peer;
1422                 pkey=X509_get_pubkey(peer);
1423                 type=X509_certificate_type(peer,pkey);
1424                 }
1425         else
1426                 {
1427                 peer=NULL;
1428                 pkey=NULL;
1429                 }
1430
1431         if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
1432                 {
1433                 s->s3->tmp.reuse_message=1;
1434                 if ((peer != NULL) && (type | EVP_PKT_SIGN))
1435                         {
1436                         al=SSL_AD_UNEXPECTED_MESSAGE;
1437                         SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
1438                         goto f_err;
1439                         }
1440                 ret=1;
1441                 goto end;
1442                 }
1443
1444         if (peer == NULL)
1445                 {
1446                 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
1447                 al=SSL_AD_UNEXPECTED_MESSAGE;
1448                 goto f_err;
1449                 }
1450
1451         if (!(type & EVP_PKT_SIGN))
1452                 {
1453                 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
1454                 al=SSL_AD_ILLEGAL_PARAMETER;
1455                 goto f_err;
1456                 }
1457
1458         if (s->s3->change_cipher_spec)
1459                 {
1460                 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
1461                 al=SSL_AD_UNEXPECTED_MESSAGE;
1462                 goto f_err;
1463                 }
1464
1465         /* we now have a signature that we need to verify */
1466         p=(unsigned char *)s->init_buf->data;
1467         n2s(p,i);
1468         n-=2;
1469         if (i > n)
1470                 {
1471                 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
1472                 al=SSL_AD_DECODE_ERROR;
1473                 goto f_err;
1474                 }
1475
1476         j=EVP_PKEY_size(pkey);
1477         if ((i > j) || (n > j) || (n <= 0))
1478                 {
1479                 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
1480                 al=SSL_AD_DECODE_ERROR;
1481                 goto f_err;
1482                 }
1483
1484 #ifndef NO_RSA 
1485         if (pkey->type == EVP_PKEY_RSA)
1486                 {
1487                 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
1488                         MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i, 
1489                                                         pkey->pkey.rsa);
1490                 if (i < 0)
1491                         {
1492                         al=SSL_AD_DECRYPT_ERROR;
1493                         SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
1494                         goto f_err;
1495                         }
1496                 if (i == 0)
1497                         {
1498                         al=SSL_AD_DECRYPT_ERROR;
1499                         SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
1500                         goto f_err;
1501                         }
1502                 }
1503         else
1504 #endif
1505 #ifndef NO_DSA
1506                 if (pkey->type == EVP_PKEY_DSA)
1507                 {
1508                 j=DSA_verify(pkey->save_type,
1509                         &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
1510                         SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
1511                 if (j <= 0)
1512                         {
1513                         /* bad signature */
1514                         al=SSL_AD_DECRYPT_ERROR;
1515                         SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
1516                         goto f_err;
1517                         }
1518                 }
1519         else
1520 #endif
1521                 {
1522                 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR);
1523                 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
1524                 goto f_err;
1525                 }
1526
1527
1528         ret=1;
1529         if (0)
1530                 {
1531 f_err:
1532                 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1533                 }
1534 end:
1535         EVP_PKEY_free(pkey);
1536         return(ret);
1537         }
1538
1539 static int ssl3_get_client_certificate(SSL *s)
1540         {
1541         int i,ok,al,ret= -1;
1542         X509 *x=NULL;
1543         unsigned long l,nc,llen,n;
1544         unsigned char *p,*d,*q;
1545         STACK_OF(X509) *sk=NULL;
1546
1547         n=ssl3_get_message(s,
1548                 SSL3_ST_SR_CERT_A,
1549                 SSL3_ST_SR_CERT_B,
1550                 -1,
1551 #if defined(MSDOS) && !defined(WIN32)
1552                 1024*30, /* 30k max cert list :-) */
1553 #else
1554                 1024*100, /* 100k max cert list :-) */
1555 #endif
1556                 &ok);
1557
1558         if (!ok) return((int)n);
1559
1560         if      (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
1561                 {
1562                 if (    (s->verify_mode & SSL_VERIFY_PEER) &&
1563                         (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1564                         {
1565                         SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1566                         al=SSL_AD_HANDSHAKE_FAILURE;
1567                         goto f_err;
1568                         }
1569                 /* If tls asked for a client cert, the client must return a 0 list */
1570                 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
1571                         {
1572                         SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
1573                         al=SSL_AD_UNEXPECTED_MESSAGE;
1574                         goto f_err;
1575                         }
1576                 s->s3->tmp.reuse_message=1;
1577                 return(1);
1578                 }
1579
1580         if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1581                 {
1582                 al=SSL_AD_UNEXPECTED_MESSAGE;
1583                 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
1584                 goto f_err;
1585                 }
1586         d=p=(unsigned char *)s->init_buf->data;
1587
1588         if ((sk=sk_X509_new_null()) == NULL)
1589                 {
1590                 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1591                 goto err;
1592                 }
1593
1594         n2l3(p,llen);
1595         if (llen+3 != n)
1596                 {
1597                 al=SSL_AD_DECODE_ERROR;
1598                 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1599                 goto f_err;
1600                 }
1601         for (nc=0; nc<llen; )
1602                 {
1603                 n2l3(p,l);
1604                 if ((l+nc+3) > llen)
1605                         {
1606                         al=SSL_AD_DECODE_ERROR;
1607                         SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1608                         goto f_err;
1609                         }
1610
1611                 q=p;
1612                 x=d2i_X509(NULL,&p,l);
1613                 if (x == NULL)
1614                         {
1615                         SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
1616                         goto err;
1617                         }
1618                 if (p != (q+l))
1619                         {
1620                         al=SSL_AD_DECODE_ERROR;
1621                         SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1622                         goto f_err;
1623                         }
1624                 if (!sk_X509_push(sk,x))
1625                         {
1626                         SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1627                         goto err;
1628                         }
1629                 x=NULL;
1630                 nc+=l+3;
1631                 }
1632
1633         if (sk_X509_num(sk) <= 0)
1634                 {
1635                 /* TLS does not mind 0 certs returned */
1636                 if (s->version == SSL3_VERSION)
1637                         {
1638                         al=SSL_AD_HANDSHAKE_FAILURE;
1639                         SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
1640                         goto f_err;
1641                         }
1642                 /* Fail for TLS only if we required a certificate */
1643                 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
1644                          (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1645                         {
1646                         SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1647                         al=SSL_AD_HANDSHAKE_FAILURE;
1648                         goto f_err;
1649                         }
1650                 }
1651         else
1652                 {
1653                 i=ssl_verify_cert_chain(s,sk);
1654                 if (!i)
1655                         {
1656                         al=ssl_verify_alarm_type(s->verify_result);
1657                         SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
1658                         goto f_err;
1659                         }
1660                 }
1661
1662         if (s->session->peer != NULL) /* This should not be needed */
1663                 X509_free(s->session->peer);
1664         s->session->peer=sk_X509_shift(sk);
1665         s->session->verify_result = s->verify_result;
1666
1667         /* With the current implementation, sess_cert will always be NULL
1668          * when we arrive here. */
1669         if (s->session->sess_cert == NULL)
1670                 {
1671                 s->session->sess_cert = ssl_sess_cert_new();
1672                 if (s->session->sess_cert == NULL)
1673                         {
1674                         SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1675                         goto err;
1676                         }
1677                 }
1678         if (s->session->sess_cert->cert_chain != NULL)
1679                 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
1680         s->session->sess_cert->cert_chain=sk;
1681
1682         sk=NULL;
1683
1684         ret=1;
1685         if (0)
1686                 {
1687 f_err:
1688                 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1689                 }
1690 err:
1691         if (x != NULL) X509_free(x);
1692         if (sk != NULL) sk_X509_pop_free(sk,X509_free);
1693         return(ret);
1694         }
1695
1696 int ssl3_send_server_certificate(SSL *s)
1697         {
1698         unsigned long l;
1699         X509 *x;
1700
1701         if (s->state == SSL3_ST_SW_CERT_A)
1702                 {
1703                 x=ssl_get_server_send_cert(s);
1704                 if (x == NULL)
1705                         {
1706                         SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR);
1707                         return(0);
1708                         }
1709
1710                 l=ssl3_output_cert_chain(s,x);
1711                 s->state=SSL3_ST_SW_CERT_B;
1712                 s->init_num=(int)l;
1713                 s->init_off=0;
1714                 }
1715
1716         /* SSL3_ST_SW_CERT_B */
1717         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1718         }