36068780e7f5044f5d1074c0dbfb5ea833b3cb2c
[openssl.git] / ssl / s3_clnt.c
1 /* ssl/s3_clnt.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include <openssl/buffer.h>
61 #include <openssl/rand.h>
62 #include <openssl/objects.h>
63 #include <openssl/evp.h>
64 #include "ssl_locl.h"
65 #ifndef OPENSSL_NO_KRB5
66 #include "kssl_lcl.h"
67 #endif
68 #include <openssl/md5.h>
69
70 static SSL_METHOD *ssl3_get_client_method(int ver);
71 static int ssl3_client_hello(SSL *s);
72 static int ssl3_get_server_hello(SSL *s);
73 static int ssl3_get_certificate_request(SSL *s);
74 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
75 static int ssl3_get_server_done(SSL *s);
76 static int ssl3_send_client_verify(SSL *s);
77 static int ssl3_send_client_certificate(SSL *s);
78 static int ssl3_send_client_key_exchange(SSL *s);
79 static int ssl3_get_key_exchange(SSL *s);
80 static int ssl3_get_server_certificate(SSL *s);
81 static int ssl3_check_cert_and_algorithm(SSL *s);
82 static SSL_METHOD *ssl3_get_client_method(int ver)
83         {
84         if (ver == SSL3_VERSION)
85                 return(SSLv3_client_method());
86         else
87                 return(NULL);
88         }
89
90 SSL_METHOD *SSLv3_client_method(void)
91         {
92         static int init=1;
93         static SSL_METHOD SSLv3_client_data;
94
95         if (init)
96                 {
97                 init=0;
98                 memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
99                         sizeof(SSL_METHOD));
100                 SSLv3_client_data.ssl_connect=ssl3_connect;
101                 SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
102                 }
103         return(&SSLv3_client_data);
104         }
105
106 int ssl3_connect(SSL *s)
107         {
108         BUF_MEM *buf;
109         unsigned long Time=time(NULL),l;
110         long num1;
111         void (*cb)()=NULL;
112         int ret= -1;
113         int new_state,state,skip=0;;
114
115         RAND_add(&Time,sizeof(Time),0);
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         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 
125         s->in_handshake++;
126
127         for (;;)
128                 {
129                 state=s->state;
130
131                 switch(s->state)
132                         {
133                 case SSL_ST_RENEGOTIATE:
134                         s->new_session=1;
135                         s->state=SSL_ST_CONNECT;
136                         s->ctx->stats.sess_connect_renegotiate++;
137                         /* break */
138                 case SSL_ST_BEFORE:
139                 case SSL_ST_CONNECT:
140                 case SSL_ST_BEFORE|SSL_ST_CONNECT:
141                 case SSL_ST_OK|SSL_ST_CONNECT:
142
143                         s->server=0;
144                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
145
146                         if ((s->version & 0xff00 ) != 0x0300)
147                                 {
148                                 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
149                                 ret = -1;
150                                 goto end;
151                                 }
152                                 
153                         /* s->version=SSL3_VERSION; */
154                         s->type=SSL_ST_CONNECT;
155
156                         if (s->init_buf == NULL)
157                                 {
158                                 if ((buf=BUF_MEM_new()) == NULL)
159                                         {
160                                         ret= -1;
161                                         goto end;
162                                         }
163                                 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
164                                         {
165                                         ret= -1;
166                                         goto end;
167                                         }
168                                 s->init_buf=buf;
169                                 }
170
171                         if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
172
173                         /* setup buffing BIO */
174                         if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
175
176                         /* don't push the buffering BIO quite yet */
177
178                         ssl3_init_finished_mac(s);
179
180                         s->state=SSL3_ST_CW_CLNT_HELLO_A;
181                         s->ctx->stats.sess_connect++;
182                         s->init_num=0;
183                         break;
184
185                 case SSL3_ST_CW_CLNT_HELLO_A:
186                 case SSL3_ST_CW_CLNT_HELLO_B:
187
188                         s->shutdown=0;
189                         ret=ssl3_client_hello(s);
190                         if (ret <= 0) goto end;
191                         s->state=SSL3_ST_CR_SRVR_HELLO_A;
192                         s->init_num=0;
193
194                         /* turn on buffering for the next lot of output */
195                         if (s->bbio != s->wbio)
196                                 s->wbio=BIO_push(s->bbio,s->wbio);
197
198                         break;
199
200                 case SSL3_ST_CR_SRVR_HELLO_A:
201                 case SSL3_ST_CR_SRVR_HELLO_B:
202                         ret=ssl3_get_server_hello(s);
203                         if (ret <= 0) goto end;
204                         if (s->hit)
205                                 s->state=SSL3_ST_CR_FINISHED_A;
206                         else
207                                 s->state=SSL3_ST_CR_CERT_A;
208                         s->init_num=0;
209                         break;
210
211                 case SSL3_ST_CR_CERT_A:
212                 case SSL3_ST_CR_CERT_B:
213                         /* Check if it is anon DH */
214                         if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
215                                 {
216                                 ret=ssl3_get_server_certificate(s);
217                                 if (ret <= 0) goto end;
218                                 }
219                         else
220                                 skip=1;
221                         s->state=SSL3_ST_CR_KEY_EXCH_A;
222                         s->init_num=0;
223                         break;
224
225                 case SSL3_ST_CR_KEY_EXCH_A:
226                 case SSL3_ST_CR_KEY_EXCH_B:
227                         ret=ssl3_get_key_exchange(s);
228                         if (ret <= 0) goto end;
229                         s->state=SSL3_ST_CR_CERT_REQ_A;
230                         s->init_num=0;
231
232                         /* at this point we check that we have the
233                          * required stuff from the server */
234                         if (!ssl3_check_cert_and_algorithm(s))
235                                 {
236                                 ret= -1;
237                                 goto end;
238                                 }
239                         break;
240
241                 case SSL3_ST_CR_CERT_REQ_A:
242                 case SSL3_ST_CR_CERT_REQ_B:
243                         ret=ssl3_get_certificate_request(s);
244                         if (ret <= 0) goto end;
245                         s->state=SSL3_ST_CR_SRVR_DONE_A;
246                         s->init_num=0;
247                         break;
248
249                 case SSL3_ST_CR_SRVR_DONE_A:
250                 case SSL3_ST_CR_SRVR_DONE_B:
251                         ret=ssl3_get_server_done(s);
252                         if (ret <= 0) goto end;
253                         if (s->s3->tmp.cert_req)
254                                 s->state=SSL3_ST_CW_CERT_A;
255                         else
256                                 s->state=SSL3_ST_CW_KEY_EXCH_A;
257                         s->init_num=0;
258
259                         break;
260
261                 case SSL3_ST_CW_CERT_A:
262                 case SSL3_ST_CW_CERT_B:
263                 case SSL3_ST_CW_CERT_C:
264                 case SSL3_ST_CW_CERT_D:
265                         ret=ssl3_send_client_certificate(s);
266                         if (ret <= 0) goto end;
267                         s->state=SSL3_ST_CW_KEY_EXCH_A;
268                         s->init_num=0;
269                         break;
270
271                 case SSL3_ST_CW_KEY_EXCH_A:
272                 case SSL3_ST_CW_KEY_EXCH_B:
273                         ret=ssl3_send_client_key_exchange(s);
274                         if (ret <= 0) goto end;
275                         l=s->s3->tmp.new_cipher->algorithms;
276                         /* EAY EAY EAY need to check for DH fix cert
277                          * sent back */
278                         /* For TLS, cert_req is set to 2, so a cert chain
279                          * of nothing is sent, but no verify packet is sent */
280                         if (s->s3->tmp.cert_req == 1)
281                                 {
282                                 s->state=SSL3_ST_CW_CERT_VRFY_A;
283                                 }
284                         else
285                                 {
286                                 s->state=SSL3_ST_CW_CHANGE_A;
287                                 s->s3->change_cipher_spec=0;
288                                 }
289
290                         s->init_num=0;
291                         break;
292
293                 case SSL3_ST_CW_CERT_VRFY_A:
294                 case SSL3_ST_CW_CERT_VRFY_B:
295                         ret=ssl3_send_client_verify(s);
296                         if (ret <= 0) goto end;
297                         s->state=SSL3_ST_CW_CHANGE_A;
298                         s->init_num=0;
299                         s->s3->change_cipher_spec=0;
300                         break;
301
302                 case SSL3_ST_CW_CHANGE_A:
303                 case SSL3_ST_CW_CHANGE_B:
304                         ret=ssl3_send_change_cipher_spec(s,
305                                 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
306                         if (ret <= 0) goto end;
307                         s->state=SSL3_ST_CW_FINISHED_A;
308                         s->init_num=0;
309
310                         s->session->cipher=s->s3->tmp.new_cipher;
311                         if (s->s3->tmp.new_compression == NULL)
312                                 s->session->compress_meth=0;
313                         else
314                                 s->session->compress_meth=
315                                         s->s3->tmp.new_compression->id;
316                         if (!s->method->ssl3_enc->setup_key_block(s))
317                                 {
318                                 ret= -1;
319                                 goto end;
320                                 }
321
322                         if (!s->method->ssl3_enc->change_cipher_state(s,
323                                 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
324                                 {
325                                 ret= -1;
326                                 goto end;
327                                 }
328
329                         break;
330
331                 case SSL3_ST_CW_FINISHED_A:
332                 case SSL3_ST_CW_FINISHED_B:
333                         ret=ssl3_send_finished(s,
334                                 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
335                                 s->method->ssl3_enc->client_finished_label,
336                                 s->method->ssl3_enc->client_finished_label_len);
337                         if (ret <= 0) goto end;
338                         s->state=SSL3_ST_CW_FLUSH;
339
340                         /* clear flags */
341                         s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
342                         if (s->hit)
343                                 {
344                                 s->s3->tmp.next_state=SSL_ST_OK;
345                                 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
346                                         {
347                                         s->state=SSL_ST_OK;
348                                         s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
349                                         s->s3->delay_buf_pop_ret=0;
350                                         }
351                                 }
352                         else
353                                 {
354                                 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
355                                 }
356                         s->init_num=0;
357                         break;
358
359                 case SSL3_ST_CR_FINISHED_A:
360                 case SSL3_ST_CR_FINISHED_B:
361
362                         ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
363                                 SSL3_ST_CR_FINISHED_B);
364                         if (ret <= 0) goto end;
365
366                         if (s->hit)
367                                 s->state=SSL3_ST_CW_CHANGE_A;
368                         else
369                                 s->state=SSL_ST_OK;
370                         s->init_num=0;
371                         break;
372
373                 case SSL3_ST_CW_FLUSH:
374                         /* number of bytes to be flushed */
375                         num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
376                         if (num1 > 0)
377                                 {
378                                 s->rwstate=SSL_WRITING;
379                                 num1=BIO_flush(s->wbio);
380                                 if (num1 <= 0) { ret= -1; goto end; }
381                                 s->rwstate=SSL_NOTHING;
382                                 }
383
384                         s->state=s->s3->tmp.next_state;
385                         break;
386
387                 case SSL_ST_OK:
388                         /* clean a few things up */
389                         ssl3_cleanup_key_block(s);
390
391                         if (s->init_buf != NULL)
392                                 {
393                                 BUF_MEM_free(s->init_buf);
394                                 s->init_buf=NULL;
395                                 }
396
397                         /* If we are not 'joining' the last two packets,
398                          * remove the buffering now */
399                         if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
400                                 ssl_free_wbio_buffer(s);
401                         /* else do it later in ssl3_write */
402
403                         s->init_num=0;
404                         s->new_session=0;
405
406                         ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
407                         if (s->hit) s->ctx->stats.sess_hit++;
408
409                         ret=1;
410                         /* s->server=0; */
411                         s->handshake_func=ssl3_connect;
412                         s->ctx->stats.sess_connect_good++;
413
414                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
415
416                         goto end;
417                         /* break; */
418                         
419                 default:
420                         SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
421                         ret= -1;
422                         goto end;
423                         /* break; */
424                         }
425
426                 /* did we do anything */
427                 if (!s->s3->tmp.reuse_message && !skip)
428                         {
429                         if (s->debug)
430                                 {
431                                 if ((ret=BIO_flush(s->wbio)) <= 0)
432                                         goto end;
433                                 }
434
435                         if ((cb != NULL) && (s->state != state))
436                                 {
437                                 new_state=s->state;
438                                 s->state=state;
439                                 cb(s,SSL_CB_CONNECT_LOOP,1);
440                                 s->state=new_state;
441                                 }
442                         }
443                 skip=0;
444                 }
445 end:
446         if (cb != NULL)
447                 cb(s,SSL_CB_CONNECT_EXIT,ret);
448         s->in_handshake--;
449         return(ret);
450         }
451
452
453 static int ssl3_client_hello(SSL *s)
454         {
455         unsigned char *buf;
456         unsigned char *p,*d;
457         int i,j;
458         unsigned long Time,l;
459         SSL_COMP *comp;
460
461         buf=(unsigned char *)s->init_buf->data;
462         if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
463                 {
464                 if ((s->session == NULL) ||
465                         (s->session->ssl_version != s->version) ||
466                         (s->session->not_resumable))
467                         {
468                         if (!ssl_get_new_session(s,0))
469                                 goto err;
470                         }
471                 /* else use the pre-loaded session */
472
473                 p=s->s3->client_random;
474                 Time=time(NULL);                        /* Time */
475                 l2n(Time,p);
476                 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
477
478                 /* Do the message type and length last */
479                 d=p= &(buf[4]);
480
481                 *(p++)=s->version>>8;
482                 *(p++)=s->version&0xff;
483                 s->client_version=s->version;
484
485                 /* Random stuff */
486                 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
487                 p+=SSL3_RANDOM_SIZE;
488
489                 /* Session ID */
490                 if (s->new_session)
491                         i=0;
492                 else
493                         i=s->session->session_id_length;
494                 *(p++)=i;
495                 if (i != 0)
496                         {
497                         memcpy(p,s->session->session_id,i);
498                         p+=i;
499                         }
500                 
501                 /* Ciphers supported */
502                 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
503                 if (i == 0)
504                         {
505                         SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
506                         goto err;
507                         }
508                 s2n(i,p);
509                 p+=i;
510
511                 /* COMPRESSION */
512                 if (s->ctx->comp_methods == NULL)
513                         j=0;
514                 else
515                         j=sk_SSL_COMP_num(s->ctx->comp_methods);
516                 *(p++)=1+j;
517                 for (i=0; i<j; i++)
518                         {
519                         comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
520                         *(p++)=comp->id;
521                         }
522                 *(p++)=0; /* Add the NULL method */
523                 
524                 l=(p-d);
525                 d=buf;
526                 *(d++)=SSL3_MT_CLIENT_HELLO;
527                 l2n3(l,d);
528
529                 s->state=SSL3_ST_CW_CLNT_HELLO_B;
530                 /* number of bytes to write */
531                 s->init_num=p-buf;
532                 s->init_off=0;
533                 }
534
535         /* SSL3_ST_CW_CLNT_HELLO_B */
536         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
537 err:
538         return(-1);
539         }
540
541 static int ssl3_get_server_hello(SSL *s)
542         {
543         STACK_OF(SSL_CIPHER) *sk;
544         SSL_CIPHER *c;
545         unsigned char *p,*d;
546         int i,al,ok;
547         unsigned int j;
548         long n;
549         SSL_COMP *comp;
550
551         n=ssl3_get_message(s,
552                 SSL3_ST_CR_SRVR_HELLO_A,
553                 SSL3_ST_CR_SRVR_HELLO_B,
554                 SSL3_MT_SERVER_HELLO,
555                 300, /* ?? */
556                 &ok);
557
558         if (!ok) return((int)n);
559         d=p=(unsigned char *)s->init_buf->data;
560
561         if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
562                 {
563                 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
564                 s->version=(s->version&0xff00)|p[1];
565                 al=SSL_AD_PROTOCOL_VERSION;
566                 goto f_err;
567                 }
568         p+=2;
569
570         /* load the server hello data */
571         /* load the server random */
572         memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
573         p+=SSL3_RANDOM_SIZE;
574
575         /* get the session-id */
576         j= *(p++);
577
578         if ((j != 0) && (j != SSL3_SESSION_ID_SIZE))
579                 {
580                 /* SSLref returns 16 :-( */
581                 if (j < SSL2_SSL_SESSION_ID_LENGTH)
582                         {
583                         al=SSL_AD_ILLEGAL_PARAMETER;
584                         SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
585                         goto f_err;
586                         }
587                 }
588         if (j != 0 && j == s->session->session_id_length
589             && memcmp(p,s->session->session_id,j) == 0)
590             {
591             if(s->sid_ctx_length != s->session->sid_ctx_length
592                || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
593                 {
594                 al=SSL_AD_ILLEGAL_PARAMETER;
595                 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
596                 goto f_err;
597                 }
598             s->hit=1;
599             }
600         else    /* a miss or crap from the other end */
601                 {
602                 /* If we were trying for session-id reuse, make a new
603                  * SSL_SESSION so we don't stuff up other people */
604                 s->hit=0;
605                 if (s->session->session_id_length > 0)
606                         {
607                         if (!ssl_get_new_session(s,0))
608                                 {
609                                 al=SSL_AD_INTERNAL_ERROR;
610                                 goto f_err;
611                                 }
612                         }
613                 s->session->session_id_length=j;
614                 memcpy(s->session->session_id,p,j); /* j could be 0 */
615                 }
616         p+=j;
617         c=ssl_get_cipher_by_char(s,p);
618         if (c == NULL)
619                 {
620                 /* unknown cipher */
621                 al=SSL_AD_ILLEGAL_PARAMETER;
622                 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
623                 goto f_err;
624                 }
625         p+=ssl_put_cipher_by_char(s,NULL,NULL);
626
627         sk=ssl_get_ciphers_by_id(s);
628         i=sk_SSL_CIPHER_find(sk,c);
629         if (i < 0)
630                 {
631                 /* we did not say we would use this cipher */
632                 al=SSL_AD_ILLEGAL_PARAMETER;
633                 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
634                 goto f_err;
635                 }
636
637         if (s->hit && (s->session->cipher != c))
638                 {
639                 if (!(s->options &
640                         SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
641                         {
642                         al=SSL_AD_ILLEGAL_PARAMETER;
643                         SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
644                         goto f_err;
645                         }
646                 }
647         s->s3->tmp.new_cipher=c;
648
649         /* lets get the compression algorithm */
650         /* COMPRESSION */
651         j= *(p++);
652         if (j == 0)
653                 comp=NULL;
654         else
655                 comp=ssl3_comp_find(s->ctx->comp_methods,j);
656         
657         if ((j != 0) && (comp == NULL))
658                 {
659                 al=SSL_AD_ILLEGAL_PARAMETER;
660                 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
661                 goto f_err;
662                 }
663         else
664                 {
665                 s->s3->tmp.new_compression=comp;
666                 }
667
668         if (p != (d+n))
669                 {
670                 /* wrong packet length */
671                 al=SSL_AD_DECODE_ERROR;
672                 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
673                 goto err;
674                 }
675
676         return(1);
677 f_err:
678         ssl3_send_alert(s,SSL3_AL_FATAL,al);
679 err:
680         return(-1);
681         }
682
683 static int ssl3_get_server_certificate(SSL *s)
684         {
685         int al,i,ok,ret= -1;
686         unsigned long n,nc,llen,l;
687         X509 *x=NULL;
688         unsigned char *p,*d,*q;
689         STACK_OF(X509) *sk=NULL;
690         SESS_CERT *sc;
691         EVP_PKEY *pkey=NULL;
692         int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
693
694         n=ssl3_get_message(s,
695                 SSL3_ST_CR_CERT_A,
696                 SSL3_ST_CR_CERT_B,
697                 -1,
698                 s->max_cert_list,
699                 &ok);
700
701         if (!ok) return((int)n);
702
703         if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
704                 {
705                 s->s3->tmp.reuse_message=1;
706                 return(1);
707                 }
708
709         if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
710                 {
711                 al=SSL_AD_UNEXPECTED_MESSAGE;
712                 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
713                 goto f_err;
714                 }
715         d=p=(unsigned char *)s->init_buf->data;
716
717         if ((sk=sk_X509_new_null()) == NULL)
718                 {
719                 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
720                 goto err;
721                 }
722
723         n2l3(p,llen);
724         if (llen+3 != n)
725                 {
726                 al=SSL_AD_DECODE_ERROR;
727                 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
728                 goto f_err;
729                 }
730         for (nc=0; nc<llen; )
731                 {
732                 n2l3(p,l);
733                 if ((l+nc+3) > llen)
734                         {
735                         al=SSL_AD_DECODE_ERROR;
736                         SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
737                         goto f_err;
738                         }
739
740                 q=p;
741                 x=d2i_X509(NULL,&q,l);
742                 if (x == NULL)
743                         {
744                         al=SSL_AD_BAD_CERTIFICATE;
745                         SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
746                         goto f_err;
747                         }
748                 if (q != (p+l))
749                         {
750                         al=SSL_AD_DECODE_ERROR;
751                         SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
752                         goto f_err;
753                         }
754                 if (!sk_X509_push(sk,x))
755                         {
756                         SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
757                         goto err;
758                         }
759                 x=NULL;
760                 nc+=l+3;
761                 p=q;
762                 }
763
764         i=ssl_verify_cert_chain(s,sk);
765         if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
766 #ifndef OPENSSL_NO_KRB5
767                 && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
768                 != (SSL_aKRB5|SSL_kKRB5)
769 #endif /* OPENSSL_NO_KRB5 */
770                 )
771                 {
772                 al=ssl_verify_alarm_type(s->verify_result);
773                 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
774                 goto f_err; 
775                 }
776         ERR_clear_error(); /* but we keep s->verify_result */
777
778         sc=ssl_sess_cert_new();
779         if (sc == NULL) goto err;
780
781         if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
782         s->session->sess_cert=sc;
783
784         sc->cert_chain=sk;
785         /* Inconsistency alert: cert_chain does include the peer's
786          * certificate, which we don't include in s3_srvr.c */
787         x=sk_X509_value(sk,0);
788         sk=NULL;
789         /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
790
791         pkey=X509_get_pubkey(x);
792
793         /* VRS: allow null cert if auth == KRB5 */
794         need_cert =     ((s->s3->tmp.new_cipher->algorithms
795                         & (SSL_MKEY_MASK|SSL_AUTH_MASK))
796                         == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
797
798 #ifdef KSSL_DEBUG
799         printf("pkey,x = %p, %p\n", pkey,x);
800         printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
801         printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
802                 s->s3->tmp.new_cipher->algorithms, need_cert);
803 #endif    /* KSSL_DEBUG */
804
805         if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
806                 {
807                 x=NULL;
808                 al=SSL3_AL_FATAL;
809                 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
810                         SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
811                 goto f_err;
812                 }
813
814         i=ssl_cert_type(x,pkey);
815         if (need_cert && i < 0)
816                 {
817                 x=NULL;
818                 al=SSL3_AL_FATAL;
819                 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
820                         SSL_R_UNKNOWN_CERTIFICATE_TYPE);
821                 goto f_err;
822                 }
823
824         if (need_cert)
825                 {
826                 sc->peer_cert_type=i;
827                 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
828                 /* Why would the following ever happen?
829                  * We just created sc a couple of lines ago. */
830                 if (sc->peer_pkeys[i].x509 != NULL)
831                         X509_free(sc->peer_pkeys[i].x509);
832                 sc->peer_pkeys[i].x509=x;
833                 sc->peer_key= &(sc->peer_pkeys[i]);
834
835                 if (s->session->peer != NULL)
836                         X509_free(s->session->peer);
837                 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
838                 s->session->peer=x;
839                 }
840         else
841                 {
842                 sc->peer_cert_type=i;
843                 sc->peer_key= NULL;
844
845                 if (s->session->peer != NULL)
846                         X509_free(s->session->peer);
847                 s->session->peer=NULL;
848                 }
849         s->session->verify_result = s->verify_result;
850
851         x=NULL;
852         ret=1;
853
854         if (0)
855                 {
856 f_err:
857                 ssl3_send_alert(s,SSL3_AL_FATAL,al);
858                 }
859 err:
860         EVP_PKEY_free(pkey);
861         X509_free(x);
862         sk_X509_pop_free(sk,X509_free);
863         return(ret);
864         }
865
866 static int ssl3_get_key_exchange(SSL *s)
867         {
868 #ifndef OPENSSL_NO_RSA
869         unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
870 #endif
871         EVP_MD_CTX md_ctx;
872         unsigned char *param,*p;
873         int al,i,j,param_len,ok;
874         long n,alg;
875         EVP_PKEY *pkey=NULL;
876 #ifndef OPENSSL_NO_RSA
877         RSA *rsa=NULL;
878 #endif
879 #ifndef OPENSSL_NO_DH
880         DH *dh=NULL;
881 #endif
882
883         /* use same message size as in ssl3_get_certificate_request()
884          * as ServerKeyExchange message may be skipped */
885         n=ssl3_get_message(s,
886                 SSL3_ST_CR_KEY_EXCH_A,
887                 SSL3_ST_CR_KEY_EXCH_B,
888                 -1,
889                 s->max_cert_list,
890                 &ok);
891
892         if (!ok) return((int)n);
893
894         if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
895                 {
896                 s->s3->tmp.reuse_message=1;
897                 return(1);
898                 }
899
900         param=p=(unsigned char *)s->init_buf->data;
901
902         if (s->session->sess_cert != NULL)
903                 {
904 #ifndef OPENSSL_NO_RSA
905                 if (s->session->sess_cert->peer_rsa_tmp != NULL)
906                         {
907                         RSA_free(s->session->sess_cert->peer_rsa_tmp);
908                         s->session->sess_cert->peer_rsa_tmp=NULL;
909                         }
910 #endif
911 #ifndef OPENSSL_NO_DH
912                 if (s->session->sess_cert->peer_dh_tmp)
913                         {
914                         DH_free(s->session->sess_cert->peer_dh_tmp);
915                         s->session->sess_cert->peer_dh_tmp=NULL;
916                         }
917 #endif
918                 }
919         else
920                 {
921                 s->session->sess_cert=ssl_sess_cert_new();
922                 }
923
924         param_len=0;
925         alg=s->s3->tmp.new_cipher->algorithms;
926         EVP_MD_CTX_init(&md_ctx);
927
928 #ifndef OPENSSL_NO_RSA
929         if (alg & SSL_kRSA)
930                 {
931                 if ((rsa=RSA_new()) == NULL)
932                         {
933                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
934                         goto err;
935                         }
936                 n2s(p,i);
937                 param_len=i+2;
938                 if (param_len > n)
939                         {
940                         al=SSL_AD_DECODE_ERROR;
941                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
942                         goto f_err;
943                         }
944                 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
945                         {
946                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
947                         goto err;
948                         }
949                 p+=i;
950
951                 n2s(p,i);
952                 param_len+=i+2;
953                 if (param_len > n)
954                         {
955                         al=SSL_AD_DECODE_ERROR;
956                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
957                         goto f_err;
958                         }
959                 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
960                         {
961                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
962                         goto err;
963                         }
964                 p+=i;
965                 n-=param_len;
966
967                 /* this should be because we are using an export cipher */
968                 if (alg & SSL_aRSA)
969                         pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
970                 else
971                         {
972                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
973                         goto err;
974                         }
975                 s->session->sess_cert->peer_rsa_tmp=rsa;
976                 rsa=NULL;
977                 }
978 #else /* OPENSSL_NO_RSA */
979         if (0)
980                 ;
981 #endif
982 #ifndef OPENSSL_NO_DH
983         else if (alg & SSL_kEDH)
984                 {
985                 if ((dh=DH_new()) == NULL)
986                         {
987                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
988                         goto err;
989                         }
990                 n2s(p,i);
991                 param_len=i+2;
992                 if (param_len > n)
993                         {
994                         al=SSL_AD_DECODE_ERROR;
995                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
996                         goto f_err;
997                         }
998                 if (!(dh->p=BN_bin2bn(p,i,NULL)))
999                         {
1000                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1001                         goto err;
1002                         }
1003                 p+=i;
1004
1005                 n2s(p,i);
1006                 param_len+=i+2;
1007                 if (param_len > n)
1008                         {
1009                         al=SSL_AD_DECODE_ERROR;
1010                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
1011                         goto f_err;
1012                         }
1013                 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1014                         {
1015                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1016                         goto err;
1017                         }
1018                 p+=i;
1019
1020                 n2s(p,i);
1021                 param_len+=i+2;
1022                 if (param_len > n)
1023                         {
1024                         al=SSL_AD_DECODE_ERROR;
1025                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
1026                         goto f_err;
1027                         }
1028                 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1029                         {
1030                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1031                         goto err;
1032                         }
1033                 p+=i;
1034                 n-=param_len;
1035
1036 #ifndef OPENSSL_NO_RSA
1037                 if (alg & SSL_aRSA)
1038                         pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1039 #else
1040                 if (0)
1041                         ;
1042 #endif
1043 #ifndef OPENSSL_NO_DSA
1044                 else if (alg & SSL_aDSS)
1045                         pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1046 #endif
1047                 /* else anonymous DH, so no certificate or pkey. */
1048
1049                 s->session->sess_cert->peer_dh_tmp=dh;
1050                 dh=NULL;
1051                 }
1052         else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
1053                 {
1054                 al=SSL_AD_ILLEGAL_PARAMETER;
1055                 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1056                 goto f_err;
1057                 }
1058 #endif /* !OPENSSL_NO_DH */
1059         if (alg & SSL_aFZA)
1060                 {
1061                 al=SSL_AD_HANDSHAKE_FAILURE;
1062                 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1063                 goto f_err;
1064                 }
1065
1066
1067         /* p points to the next byte, there are 'n' bytes left */
1068
1069
1070         /* if it was signed, check the signature */
1071         if (pkey != NULL)
1072                 {
1073                 n2s(p,i);
1074                 n-=2;
1075                 j=EVP_PKEY_size(pkey);
1076
1077                 if ((i != n) || (n > j) || (n <= 0))
1078                         {
1079                         /* wrong packet length */
1080                         al=SSL_AD_DECODE_ERROR;
1081                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1082                         goto f_err;
1083                         }
1084
1085 #ifndef OPENSSL_NO_RSA
1086                 if (pkey->type == EVP_PKEY_RSA)
1087                         {
1088                         int num;
1089
1090                         j=0;
1091                         q=md_buf;
1092                         for (num=2; num > 0; num--)
1093                                 {
1094                                 EVP_DigestInit(&md_ctx,(num == 2)
1095                                         ?s->ctx->md5:s->ctx->sha1);
1096                                 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1097                                 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1098                                 EVP_DigestUpdate(&md_ctx,param,param_len);
1099                                 EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);
1100                                 q+=i;
1101                                 j+=i;
1102                                 }
1103                         i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1104                                                                 pkey->pkey.rsa);
1105                         if (i < 0)
1106                                 {
1107                                 al=SSL_AD_DECRYPT_ERROR;
1108                                 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1109                                 goto f_err;
1110                                 }
1111                         if (i == 0)
1112                                 {
1113                                 /* bad signature */
1114                                 al=SSL_AD_DECRYPT_ERROR;
1115                                 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1116                                 goto f_err;
1117                                 }
1118                         }
1119                 else
1120 #endif
1121 #ifndef OPENSSL_NO_DSA
1122                         if (pkey->type == EVP_PKEY_DSA)
1123                         {
1124                         /* lets do DSS */
1125                         EVP_VerifyInit(&md_ctx,EVP_dss1());
1126                         EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1127                         EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1128                         EVP_VerifyUpdate(&md_ctx,param,param_len);
1129                         if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1130                                 {
1131                                 /* bad signature */
1132                                 al=SSL_AD_DECRYPT_ERROR;
1133                                 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1134                                 goto f_err;
1135                                 }
1136                         }
1137                 else
1138 #endif
1139                         {
1140                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1141                         goto err;
1142                         }
1143                 }
1144         else
1145                 {
1146                 /* still data left over */
1147                 if (!(alg & SSL_aNULL))
1148                         {
1149                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1150                         goto err;
1151                         }
1152                 if (n != 0)
1153                         {
1154                         al=SSL_AD_DECODE_ERROR;
1155                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1156                         goto f_err;
1157                         }
1158                 }
1159         EVP_PKEY_free(pkey);
1160         EVP_MD_CTX_cleanup(&md_ctx);
1161         return(1);
1162 f_err:
1163         ssl3_send_alert(s,SSL3_AL_FATAL,al);
1164 err:
1165         EVP_PKEY_free(pkey);
1166 #ifndef OPENSSL_NO_RSA
1167         if (rsa != NULL)
1168                 RSA_free(rsa);
1169 #endif
1170 #ifndef OPENSSL_NO_DH
1171         if (dh != NULL)
1172                 DH_free(dh);
1173 #endif
1174         EVP_MD_CTX_cleanup(&md_ctx);
1175         return(-1);
1176         }
1177
1178 static int ssl3_get_certificate_request(SSL *s)
1179         {
1180         int ok,ret=0;
1181         unsigned long n,nc,l;
1182         unsigned int llen,ctype_num,i;
1183         X509_NAME *xn=NULL;
1184         unsigned char *p,*d,*q;
1185         STACK_OF(X509_NAME) *ca_sk=NULL;
1186
1187         n=ssl3_get_message(s,
1188                 SSL3_ST_CR_CERT_REQ_A,
1189                 SSL3_ST_CR_CERT_REQ_B,
1190                 -1,
1191                 s->max_cert_list,
1192                 &ok);
1193
1194         if (!ok) return((int)n);
1195
1196         s->s3->tmp.cert_req=0;
1197
1198         if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1199                 {
1200                 s->s3->tmp.reuse_message=1;
1201                 return(1);
1202                 }
1203
1204         if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1205                 {
1206                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1207                 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1208                 goto err;
1209                 }
1210
1211         /* TLS does not like anon-DH with client cert */
1212         if (s->version > SSL3_VERSION)
1213                 {
1214                 l=s->s3->tmp.new_cipher->algorithms;
1215                 if (l & SSL_aNULL)
1216                         {
1217                         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1218                         SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1219                         goto err;
1220                         }
1221                 }
1222
1223         d=p=(unsigned char *)s->init_buf->data;
1224
1225         if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1226                 {
1227                 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1228                 goto err;
1229                 }
1230
1231         /* get the certificate types */
1232         ctype_num= *(p++);
1233         if (ctype_num > SSL3_CT_NUMBER)
1234                 ctype_num=SSL3_CT_NUMBER;
1235         for (i=0; i<ctype_num; i++)
1236                 s->s3->tmp.ctype[i]= p[i];
1237         p+=ctype_num;
1238
1239         /* get the CA RDNs */
1240         n2s(p,llen);
1241 #if 0
1242 {
1243 FILE *out;
1244 out=fopen("/tmp/vsign.der","w");
1245 fwrite(p,1,llen,out);
1246 fclose(out);
1247 }
1248 #endif
1249
1250         if ((llen+ctype_num+2+1) != n)
1251                 {
1252                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1253                 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1254                 goto err;
1255                 }
1256
1257         for (nc=0; nc<llen; )
1258                 {
1259                 n2s(p,l);
1260                 if ((l+nc+2) > llen)
1261                         {
1262                         if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1263                                 goto cont; /* netscape bugs */
1264                         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1265                         SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1266                         goto err;
1267                         }
1268
1269                 q=p;
1270
1271                 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1272                         {
1273                         /* If netscape tolerance is on, ignore errors */
1274                         if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1275                                 goto cont;
1276                         else
1277                                 {
1278                                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1279                                 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1280                                 goto err;
1281                                 }
1282                         }
1283
1284                 if (q != (p+l))
1285                         {
1286                         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1287                         SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1288                         goto err;
1289                         }
1290                 if (!sk_X509_NAME_push(ca_sk,xn))
1291                         {
1292                         SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1293                         goto err;
1294                         }
1295
1296                 p+=l;
1297                 nc+=l+2;
1298                 }
1299
1300         if (0)
1301                 {
1302 cont:
1303                 ERR_clear_error();
1304                 }
1305
1306         /* we should setup a certificate to return.... */
1307         s->s3->tmp.cert_req=1;
1308         s->s3->tmp.ctype_num=ctype_num;
1309         if (s->s3->tmp.ca_names != NULL)
1310                 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1311         s->s3->tmp.ca_names=ca_sk;
1312         ca_sk=NULL;
1313
1314         ret=1;
1315 err:
1316         if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1317         return(ret);
1318         }
1319
1320 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1321         {
1322         return(X509_NAME_cmp(*a,*b));
1323         }
1324
1325 static int ssl3_get_server_done(SSL *s)
1326         {
1327         int ok,ret=0;
1328         long n;
1329
1330         n=ssl3_get_message(s,
1331                 SSL3_ST_CR_SRVR_DONE_A,
1332                 SSL3_ST_CR_SRVR_DONE_B,
1333                 SSL3_MT_SERVER_DONE,
1334                 30, /* should be very small, like 0 :-) */
1335                 &ok);
1336
1337         if (!ok) return((int)n);
1338         if (n > 0)
1339                 {
1340                 /* should contain no data */
1341                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1342                 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1343                 }
1344         ret=1;
1345         return(ret);
1346         }
1347
1348 static int ssl3_send_client_key_exchange(SSL *s)
1349         {
1350         unsigned char *p,*d;
1351         int n;
1352         unsigned long l;
1353 #ifndef OPENSSL_NO_RSA
1354         unsigned char *q;
1355         EVP_PKEY *pkey=NULL;
1356 #endif
1357 #ifndef OPENSSL_NO_KRB5
1358         KSSL_ERR kssl_err;
1359 #endif /* OPENSSL_NO_KRB5 */
1360
1361         if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1362                 {
1363                 d=(unsigned char *)s->init_buf->data;
1364                 p= &(d[4]);
1365
1366                 l=s->s3->tmp.new_cipher->algorithms;
1367
1368                 /* Fool emacs indentation */
1369                 if (0) {}
1370 #ifndef OPENSSL_NO_RSA
1371                 else if (l & SSL_kRSA)
1372                         {
1373                         RSA *rsa;
1374                         unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1375
1376                         if (s->session->sess_cert->peer_rsa_tmp != NULL)
1377                                 rsa=s->session->sess_cert->peer_rsa_tmp;
1378                         else
1379                                 {
1380                                 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1381                                 if ((pkey == NULL) ||
1382                                         (pkey->type != EVP_PKEY_RSA) ||
1383                                         (pkey->pkey.rsa == NULL))
1384                                         {
1385                                         SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1386                                         goto err;
1387                                         }
1388                                 rsa=pkey->pkey.rsa;
1389                                 EVP_PKEY_free(pkey);
1390                                 }
1391                                 
1392                         tmp_buf[0]=s->client_version>>8;
1393                         tmp_buf[1]=s->client_version&0xff;
1394                         if (RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2) <= 0)
1395                                         goto err;
1396
1397                         s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1398
1399                         q=p;
1400                         /* Fix buf for TLS and beyond */
1401                         if (s->version > SSL3_VERSION)
1402                                 p+=2;
1403                         n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1404                                 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1405 #ifdef PKCS1_CHECK
1406                         if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1407                         if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1408 #endif
1409                         if (n <= 0)
1410                                 {
1411                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1412                                 goto err;
1413                                 }
1414
1415                         /* Fix buf for TLS and beyond */
1416                         if (s->version > SSL3_VERSION)
1417                                 {
1418                                 s2n(n,q);
1419                                 n+=2;
1420                                 }
1421
1422                         s->session->master_key_length=
1423                                 s->method->ssl3_enc->generate_master_secret(s,
1424                                         s->session->master_key,
1425                                         tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1426                         memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
1427                         }
1428 #endif
1429 #ifndef OPENSSL_NO_KRB5
1430                 else if (l & SSL_kKRB5)
1431                         {
1432                         krb5_error_code krb5rc;
1433                         KSSL_CTX        *kssl_ctx = s->kssl_ctx;
1434                         /*  krb5_data   krb5_ap_req;  */
1435                         krb5_data       *enc_ticket;
1436                         krb5_data       authenticator, *authp = NULL;
1437                         EVP_CIPHER_CTX  ciph_ctx;
1438                         EVP_CIPHER      *enc = NULL;
1439                         unsigned char   iv[EVP_MAX_IV_LENGTH];
1440                         unsigned char   tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1441                         unsigned char   epms[SSL_MAX_MASTER_KEY_LENGTH 
1442                                                 + EVP_MAX_IV_LENGTH];
1443                         int             padl, outl = sizeof(epms);
1444
1445 #ifdef KSSL_DEBUG
1446                         printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
1447                                 l, SSL_kKRB5);
1448 #endif  /* KSSL_DEBUG */
1449
1450                         authp = NULL;
1451 #ifdef KRB5SENDAUTH
1452                         if (KRB5SENDAUTH)  authp = &authenticator;
1453 #endif  /* KRB5SENDAUTH */
1454
1455                         krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
1456                                 &kssl_err);
1457                         enc = kssl_map_enc(kssl_ctx->enctype);
1458                         if (enc == NULL)
1459                             goto err;
1460 #ifdef KSSL_DEBUG
1461                         {
1462                         printf("kssl_cget_tkt rtn %d\n", krb5rc);
1463                         if (krb5rc && kssl_err.text)
1464                           printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
1465                         }
1466 #endif  /* KSSL_DEBUG */
1467
1468                         if (krb5rc)
1469                                 {
1470                                 ssl3_send_alert(s,SSL3_AL_FATAL,
1471                                                 SSL_AD_HANDSHAKE_FAILURE);
1472                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1473                                                 kssl_err.reason);
1474                                 goto err;
1475                                 }
1476
1477                         /*  20010406 VRS - Earlier versions used KRB5 AP_REQ
1478                         **  in place of RFC 2712 KerberosWrapper, as in:
1479                         **
1480                         **  Send ticket (copy to *p, set n = length)
1481                         **  n = krb5_ap_req.length;
1482                         **  memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
1483                         **  if (krb5_ap_req.data)  
1484                         **    kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
1485                         **
1486                         **  Now using real RFC 2712 KerberosWrapper
1487                         **  (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
1488                         **  Note: 2712 "opaque" types are here replaced
1489                         **  with a 2-byte length followed by the value.
1490                         **  Example:
1491                         **  KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
1492                         **  Where "xx xx" = length bytes.  Shown here with
1493                         **  optional authenticator omitted.
1494                         */
1495
1496                         /*  KerberosWrapper.Ticket              */
1497                         s2n(enc_ticket->length,p);
1498                         memcpy(p, enc_ticket->data, enc_ticket->length);
1499                         p+= enc_ticket->length;
1500                         n = enc_ticket->length + 2;
1501
1502                         /*  KerberosWrapper.Authenticator       */
1503                         if (authp  &&  authp->length)  
1504                                 {
1505                                 s2n(authp->length,p);
1506                                 memcpy(p, authp->data, authp->length);
1507                                 p+= authp->length;
1508                                 n+= authp->length + 2;
1509                                 
1510                                 free(authp->data);
1511                                 authp->data = NULL;
1512                                 authp->length = 0;
1513                                 }
1514                         else
1515                                 {
1516                                 s2n(0,p);/*  null authenticator length  */
1517                                 n+=2;
1518                                 }
1519  
1520                         if (RAND_bytes(tmp_buf,SSL_MAX_MASTER_KEY_LENGTH) <= 0)
1521                             goto err;
1522
1523                         /*  20010420 VRS.  Tried it this way; failed.
1524                         **      EVP_EncryptInit(&ciph_ctx,enc, NULL,NULL);
1525                         **      EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
1526                         **                              kssl_ctx->length);
1527                         **      EVP_EncryptInit(&ciph_ctx,NULL, key,iv);
1528                         */
1529
1530                         memset(iv, 0, EVP_MAX_IV_LENGTH);  /* per RFC 1510 */
1531                         EVP_EncryptInit(&ciph_ctx,enc, kssl_ctx->key,iv);
1532                         EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
1533                                                 SSL_MAX_MASTER_KEY_LENGTH);
1534                         EVP_EncryptFinal(&ciph_ctx,&(epms[outl]),&padl);
1535                         outl += padl;
1536                         EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1537
1538                         /*  KerberosWrapper.EncryptedPreMasterSecret    */
1539                         s2n(outl,p);
1540                         memcpy(p, epms, outl);
1541                         p+=outl;
1542                         n+=outl + 2;
1543
1544                         s->session->master_key_length=
1545                                 s->method->ssl3_enc->generate_master_secret(s,
1546                                         s->session->master_key,
1547                                         tmp_buf, SSL_MAX_MASTER_KEY_LENGTH);
1548
1549                         memset(tmp_buf, 0, SSL_MAX_MASTER_KEY_LENGTH);
1550                         memset(epms, 0, outl);
1551                         }
1552 #endif
1553 #ifndef OPENSSL_NO_DH
1554                 else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1555                         {
1556                         DH *dh_srvr,*dh_clnt;
1557
1558                         if (s->session->sess_cert->peer_dh_tmp != NULL)
1559                                 dh_srvr=s->session->sess_cert->peer_dh_tmp;
1560                         else
1561                                 {
1562                                 /* we get them from the cert */
1563                                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1564                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1565                                 goto err;
1566                                 }
1567                         
1568                         /* generate a new random key */
1569                         if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1570                                 {
1571                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1572                                 goto err;
1573                                 }
1574                         if (!DH_generate_key(dh_clnt))
1575                                 {
1576                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1577                                 goto err;
1578                                 }
1579
1580                         /* use the 'p' output buffer for the DH key, but
1581                          * make sure to clear it out afterwards */
1582
1583                         n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1584
1585                         if (n <= 0)
1586                                 {
1587                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1588                                 goto err;
1589                                 }
1590
1591                         /* generate master key from the result */
1592                         s->session->master_key_length=
1593                                 s->method->ssl3_enc->generate_master_secret(s,
1594                                         s->session->master_key,p,n);
1595                         /* clean up */
1596                         memset(p,0,n);
1597
1598                         /* send off the data */
1599                         n=BN_num_bytes(dh_clnt->pub_key);
1600                         s2n(n,p);
1601                         BN_bn2bin(dh_clnt->pub_key,p);
1602                         n+=2;
1603
1604                         DH_free(dh_clnt);
1605
1606                         /* perhaps clean things up a bit EAY EAY EAY EAY*/
1607                         }
1608 #endif
1609                 else
1610                         {
1611                         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1612                         SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1613                         goto err;
1614                         }
1615                 
1616                 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1617                 l2n3(n,d);
1618
1619                 s->state=SSL3_ST_CW_KEY_EXCH_B;
1620                 /* number of bytes to write */
1621                 s->init_num=n+4;
1622                 s->init_off=0;
1623                 }
1624
1625         /* SSL3_ST_CW_KEY_EXCH_B */
1626         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1627 err:
1628         return(-1);
1629         }
1630
1631 static int ssl3_send_client_verify(SSL *s)
1632         {
1633         unsigned char *p,*d;
1634         unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1635         EVP_PKEY *pkey;
1636 #ifndef OPENSSL_NO_RSA
1637         unsigned u=0;
1638 #endif
1639         unsigned long n;
1640 #ifndef OPENSSL_NO_DSA
1641         int j;
1642 #endif
1643
1644         if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1645                 {
1646                 d=(unsigned char *)s->init_buf->data;
1647                 p= &(d[4]);
1648                 pkey=s->cert->key->privatekey;
1649
1650                 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1651                         &(data[MD5_DIGEST_LENGTH]));
1652
1653 #ifndef OPENSSL_NO_RSA
1654                 if (pkey->type == EVP_PKEY_RSA)
1655                         {
1656                         s->method->ssl3_enc->cert_verify_mac(s,
1657                                 &(s->s3->finish_dgst1),&(data[0]));
1658                         if (RSA_sign(NID_md5_sha1, data,
1659                                          MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1660                                         &(p[2]), &u, pkey->pkey.rsa) <= 0 )
1661                                 {
1662                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1663                                 goto err;
1664                                 }
1665                         s2n(u,p);
1666                         n=u+2;
1667                         }
1668                 else
1669 #endif
1670 #ifndef OPENSSL_NO_DSA
1671                         if (pkey->type == EVP_PKEY_DSA)
1672                         {
1673                         if (!DSA_sign(pkey->save_type,
1674                                 &(data[MD5_DIGEST_LENGTH]),
1675                                 SHA_DIGEST_LENGTH,&(p[2]),
1676                                 (unsigned int *)&j,pkey->pkey.dsa))
1677                                 {
1678                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1679                                 goto err;
1680                                 }
1681                         s2n(j,p);
1682                         n=j+2;
1683                         }
1684                 else
1685 #endif
1686                         {
1687                         SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
1688                         goto err;
1689                         }
1690                 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1691                 l2n3(n,d);
1692
1693                 s->init_num=(int)n+4;
1694                 s->init_off=0;
1695                 }
1696         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1697 err:
1698         return(-1);
1699         }
1700
1701 static int ssl3_send_client_certificate(SSL *s)
1702         {
1703         X509 *x509=NULL;
1704         EVP_PKEY *pkey=NULL;
1705         int i;
1706         unsigned long l;
1707
1708         if (s->state == SSL3_ST_CW_CERT_A)
1709                 {
1710                 if ((s->cert == NULL) ||
1711                         (s->cert->key->x509 == NULL) ||
1712                         (s->cert->key->privatekey == NULL))
1713                         s->state=SSL3_ST_CW_CERT_B;
1714                 else
1715                         s->state=SSL3_ST_CW_CERT_C;
1716                 }
1717
1718         /* We need to get a client cert */
1719         if (s->state == SSL3_ST_CW_CERT_B)
1720                 {
1721                 /* If we get an error, we need to
1722                  * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1723                  * We then get retied later */
1724                 i=0;
1725                 if (s->ctx->client_cert_cb != NULL)
1726                         i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1727                 if (i < 0)
1728                         {
1729                         s->rwstate=SSL_X509_LOOKUP;
1730                         return(-1);
1731                         }
1732                 s->rwstate=SSL_NOTHING;
1733                 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1734                         {
1735                         s->state=SSL3_ST_CW_CERT_B;
1736                         if (    !SSL_use_certificate(s,x509) ||
1737                                 !SSL_use_PrivateKey(s,pkey))
1738                                 i=0;
1739                         }
1740                 else if (i == 1)
1741                         {
1742                         i=0;
1743                         SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1744                         }
1745
1746                 if (x509 != NULL) X509_free(x509);
1747                 if (pkey != NULL) EVP_PKEY_free(pkey);
1748                 if (i == 0)
1749                         {
1750                         if (s->version == SSL3_VERSION)
1751                                 {
1752                                 s->s3->tmp.cert_req=0;
1753                                 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1754                                 return(1);
1755                                 }
1756                         else
1757                                 {
1758                                 s->s3->tmp.cert_req=2;
1759                                 }
1760                         }
1761
1762                 /* Ok, we have a cert */
1763                 s->state=SSL3_ST_CW_CERT_C;
1764                 }
1765
1766         if (s->state == SSL3_ST_CW_CERT_C)
1767                 {
1768                 s->state=SSL3_ST_CW_CERT_D;
1769                 l=ssl3_output_cert_chain(s,
1770                         (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1771                 s->init_num=(int)l;
1772                 s->init_off=0;
1773                 }
1774         /* SSL3_ST_CW_CERT_D */
1775         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1776         }
1777
1778 #define has_bits(i,m)   (((i)&(m)) == (m))
1779
1780 static int ssl3_check_cert_and_algorithm(SSL *s)
1781         {
1782         int i,idx;
1783         long algs;
1784         EVP_PKEY *pkey=NULL;
1785         SESS_CERT *sc;
1786 #ifndef OPENSSL_NO_RSA
1787         RSA *rsa;
1788 #endif
1789 #ifndef OPENSSL_NO_DH
1790         DH *dh;
1791 #endif
1792
1793         sc=s->session->sess_cert;
1794
1795         if (sc == NULL)
1796                 {
1797                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
1798                 goto err;
1799                 }
1800
1801         algs=s->s3->tmp.new_cipher->algorithms;
1802
1803         /* we don't have a certificate */
1804         if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
1805                 return(1);
1806
1807 #ifndef OPENSSL_NO_RSA
1808         rsa=s->session->sess_cert->peer_rsa_tmp;
1809 #endif
1810 #ifndef OPENSSL_NO_DH
1811         dh=s->session->sess_cert->peer_dh_tmp;
1812 #endif
1813
1814         /* This is the passed certificate */
1815
1816         idx=sc->peer_cert_type;
1817         pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
1818         i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
1819         EVP_PKEY_free(pkey);
1820
1821         
1822         /* Check that we have a certificate if we require one */
1823         if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1824                 {
1825                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1826                 goto f_err;
1827                 }
1828 #ifndef OPENSSL_NO_DSA
1829         else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1830                 {
1831                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1832                 goto f_err;
1833                 }
1834 #endif
1835 #ifndef OPENSSL_NO_RSA
1836         if ((algs & SSL_kRSA) &&
1837                 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1838                 {
1839                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1840                 goto f_err;
1841                 }
1842 #endif
1843 #ifndef OPENSSL_NO_DH
1844         if ((algs & SSL_kEDH) &&
1845                 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1846                 {
1847                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1848                 goto f_err;
1849                 }
1850         else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1851                 {
1852                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1853                 goto f_err;
1854                 }
1855 #ifndef OPENSSL_NO_DSA
1856         else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1857                 {
1858                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1859                 goto f_err;
1860                 }
1861 #endif
1862 #endif
1863
1864         if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
1865                 {
1866 #ifndef OPENSSL_NO_RSA
1867                 if (algs & SSL_kRSA)
1868                         {
1869                         if (rsa == NULL
1870                             || RSA_size(rsa) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1871                                 {
1872                                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1873                                 goto f_err;
1874                                 }
1875                         }
1876                 else
1877 #endif
1878 #ifndef OPENSSL_NO_DH
1879                         if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1880                             {
1881                             if (dh == NULL
1882                                 || DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1883                                 {
1884                                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1885                                 goto f_err;
1886                                 }
1887                         }
1888                 else
1889 #endif
1890                         {
1891                         SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1892                         goto f_err;
1893                         }
1894                 }
1895         return(1);
1896 f_err:
1897         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1898 err:
1899         return(0);
1900         }
1901