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