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