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