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