2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
58 /* ====================================================================
59 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
112 #define REUSE_CIPHER_BUG
113 #define NETSCAPE_HANG_BUG
117 #include <openssl/buffer.h>
118 #include <openssl/rand.h>
119 #include <openssl/objects.h>
120 #include <openssl/md5.h>
121 #include <openssl/sha.h>
122 #include <openssl/evp.h>
123 #include <openssl/x509.h>
124 #include "ssl_locl.h"
126 static SSL_METHOD *ssl3_get_server_method(int ver);
127 static int ssl3_get_client_hello(SSL *s);
128 static int ssl3_check_client_hello(SSL *s);
129 static int ssl3_send_server_hello(SSL *s);
130 static int ssl3_send_server_key_exchange(SSL *s);
131 static int ssl3_send_certificate_request(SSL *s);
132 static int ssl3_send_server_done(SSL *s);
133 static int ssl3_get_client_key_exchange(SSL *s);
134 static int ssl3_get_client_certificate(SSL *s);
135 static int ssl3_get_cert_verify(SSL *s);
136 static int ssl3_send_hello_request(SSL *s);
138 static SSL_METHOD *ssl3_get_server_method(int ver)
140 if (ver == SSL3_VERSION)
141 return(SSLv3_server_method());
146 SSL_METHOD *SSLv3_server_method(void)
149 static SSL_METHOD SSLv3_server_data;
153 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
155 SSLv3_server_data.ssl_accept=ssl3_accept;
156 SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
159 return(&SSLv3_server_data);
162 int ssl3_accept(SSL *s)
165 unsigned long l,Time=time(NULL);
169 int new_state,state,skip=0;
171 RAND_add(&Time,sizeof(Time),0);
175 if (s->info_callback != NULL)
177 else if (s->ctx->info_callback != NULL)
178 cb=s->ctx->info_callback;
180 /* init things to blank */
182 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
186 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
196 case SSL_ST_RENEGOTIATE:
198 /* s->state=SSL_ST_ACCEPT; */
202 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
203 case SSL_ST_OK|SSL_ST_ACCEPT:
206 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
208 if ((s->version>>8) != 3)
210 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_INTERNAL_ERROR);
213 s->type=SSL_ST_ACCEPT;
215 if (s->init_buf == NULL)
217 if ((buf=BUF_MEM_new()) == NULL)
222 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
230 if (!ssl3_setup_buffers(s))
238 if (s->state != SSL_ST_RENEGOTIATE)
240 /* Ok, we now need to push on a buffering BIO so that
241 * the output is sent in a way that TCP likes :-)
243 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
245 ssl3_init_finished_mac(s);
246 s->state=SSL3_ST_SR_CLNT_HELLO_A;
247 s->ctx->stats.sess_accept++;
251 /* s->state == SSL_ST_RENEGOTIATE,
252 * we will just send a HelloRequest */
253 s->ctx->stats.sess_accept_renegotiate++;
254 s->state=SSL3_ST_SW_HELLO_REQ_A;
258 case SSL3_ST_SW_HELLO_REQ_A:
259 case SSL3_ST_SW_HELLO_REQ_B:
262 ret=ssl3_send_hello_request(s);
263 if (ret <= 0) goto end;
264 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
265 s->state=SSL3_ST_SW_FLUSH;
268 ssl3_init_finished_mac(s);
271 case SSL3_ST_SW_HELLO_REQ_C:
275 case SSL3_ST_SR_CLNT_HELLO_A:
276 case SSL3_ST_SR_CLNT_HELLO_B:
277 case SSL3_ST_SR_CLNT_HELLO_C:
280 ret=ssl3_get_client_hello(s);
281 if (ret <= 0) goto end;
283 s->state=SSL3_ST_SW_SRVR_HELLO_A;
287 case SSL3_ST_SW_SRVR_HELLO_A:
288 case SSL3_ST_SW_SRVR_HELLO_B:
289 ret=ssl3_send_server_hello(s);
290 if (ret <= 0) goto end;
293 s->state=SSL3_ST_SW_CHANGE_A;
295 s->state=SSL3_ST_SW_CERT_A;
299 case SSL3_ST_SW_CERT_A:
300 case SSL3_ST_SW_CERT_B:
301 /* Check if it is anon DH */
302 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
304 ret=ssl3_send_server_certificate(s);
305 if (ret <= 0) goto end;
309 s->state=SSL3_ST_SW_KEY_EXCH_A;
313 case SSL3_ST_SW_KEY_EXCH_A:
314 case SSL3_ST_SW_KEY_EXCH_B:
315 l=s->s3->tmp.new_cipher->algorithms;
317 /* clear this, it may get reset by
318 * send_server_key_exchange */
319 if (s->options & SSL_OP_EPHEMERAL_RSA)
320 s->s3->tmp.use_rsa_tmp=1;
322 s->s3->tmp.use_rsa_tmp=0;
324 /* only send if a DH key exchange, fortezza or
325 * RSA but we have a sign only certificate */
326 if (s->s3->tmp.use_rsa_tmp
327 || (l & (SSL_DH|SSL_kFZA))
329 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
330 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
331 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
337 ret=ssl3_send_server_key_exchange(s);
338 if (ret <= 0) goto end;
343 s->state=SSL3_ST_SW_CERT_REQ_A;
347 case SSL3_ST_SW_CERT_REQ_A:
348 case SSL3_ST_SW_CERT_REQ_B:
349 if (/* don't request cert unless asked for it: */
350 !(s->verify_mode & SSL_VERIFY_PEER) ||
351 /* if SSL_VERIFY_CLIENT_ONCE is set,
352 * don't request cert during re-negotiation: */
353 ((s->session->peer != NULL) &&
354 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
355 /* never request cert in anonymous ciphersuites
356 * (see section "Certificate request" in SSL 3 drafts
357 * and in RFC 2246): */
358 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
359 /* ... except when the application insists on verification
360 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
361 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)))
363 /* no cert request */
365 s->s3->tmp.cert_request=0;
366 s->state=SSL3_ST_SW_SRVR_DONE_A;
370 s->s3->tmp.cert_request=1;
371 ret=ssl3_send_certificate_request(s);
372 if (ret <= 0) goto end;
373 #ifndef NETSCAPE_HANG_BUG
374 s->state=SSL3_ST_SW_SRVR_DONE_A;
376 s->state=SSL3_ST_SW_FLUSH;
377 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
383 case SSL3_ST_SW_SRVR_DONE_A:
384 case SSL3_ST_SW_SRVR_DONE_B:
385 ret=ssl3_send_server_done(s);
386 if (ret <= 0) goto end;
387 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
388 s->state=SSL3_ST_SW_FLUSH;
392 case SSL3_ST_SW_FLUSH:
393 /* number of bytes to be flushed */
394 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
397 s->rwstate=SSL_WRITING;
398 num1=BIO_flush(s->wbio);
399 if (num1 <= 0) { ret= -1; goto end; }
400 s->rwstate=SSL_NOTHING;
403 s->state=s->s3->tmp.next_state;
406 case SSL3_ST_SR_CERT_A:
407 case SSL3_ST_SR_CERT_B:
408 /* Check for second client hello (MS SGC) */
409 ret = ssl3_check_client_hello(s);
413 s->state = SSL3_ST_SR_CLNT_HELLO_C;
415 /* could be sent for a DH cert, even if we
416 * have not asked for it :-) */
417 ret=ssl3_get_client_certificate(s);
418 if (ret <= 0) goto end;
420 s->state=SSL3_ST_SR_KEY_EXCH_A;
424 case SSL3_ST_SR_KEY_EXCH_A:
425 case SSL3_ST_SR_KEY_EXCH_B:
426 ret=ssl3_get_client_key_exchange(s);
427 if (ret <= 0) goto end;
428 s->state=SSL3_ST_SR_CERT_VRFY_A;
431 /* We need to get hashes here so if there is
432 * a client cert, it can be verified */
433 s->method->ssl3_enc->cert_verify_mac(s,
434 &(s->s3->finish_dgst1),
435 &(s->s3->tmp.cert_verify_md[0]));
436 s->method->ssl3_enc->cert_verify_mac(s,
437 &(s->s3->finish_dgst2),
438 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
442 case SSL3_ST_SR_CERT_VRFY_A:
443 case SSL3_ST_SR_CERT_VRFY_B:
445 /* we should decide if we expected this one */
446 ret=ssl3_get_cert_verify(s);
447 if (ret <= 0) goto end;
449 s->state=SSL3_ST_SR_FINISHED_A;
453 case SSL3_ST_SR_FINISHED_A:
454 case SSL3_ST_SR_FINISHED_B:
455 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
456 SSL3_ST_SR_FINISHED_B);
457 if (ret <= 0) goto end;
461 s->state=SSL3_ST_SW_CHANGE_A;
465 case SSL3_ST_SW_CHANGE_A:
466 case SSL3_ST_SW_CHANGE_B:
468 s->session->cipher=s->s3->tmp.new_cipher;
469 if (!s->method->ssl3_enc->setup_key_block(s))
470 { ret= -1; goto end; }
472 ret=ssl3_send_change_cipher_spec(s,
473 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
475 if (ret <= 0) goto end;
476 s->state=SSL3_ST_SW_FINISHED_A;
479 if (!s->method->ssl3_enc->change_cipher_state(s,
480 SSL3_CHANGE_CIPHER_SERVER_WRITE))
488 case SSL3_ST_SW_FINISHED_A:
489 case SSL3_ST_SW_FINISHED_B:
490 ret=ssl3_send_finished(s,
491 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
492 s->method->ssl3_enc->server_finished_label,
493 s->method->ssl3_enc->server_finished_label_len);
494 if (ret <= 0) goto end;
495 s->state=SSL3_ST_SW_FLUSH;
497 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
499 s->s3->tmp.next_state=SSL_ST_OK;
504 /* clean a few things up */
505 ssl3_cleanup_key_block(s);
507 BUF_MEM_free(s->init_buf);
510 /* remove buffering on output */
511 ssl_free_wbio_buffer(s);
515 if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
517 /* actually not necessarily a 'new' session */
521 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
523 s->ctx->stats.sess_accept_good++;
525 s->handshake_func=ssl3_accept;
527 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
535 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
541 if (!s->s3->tmp.reuse_message && !skip)
545 if ((ret=BIO_flush(s->wbio)) <= 0)
550 if ((cb != NULL) && (s->state != state))
554 cb(s,SSL_CB_ACCEPT_LOOP,1);
561 /* BIO_flush(s->wbio); */
565 cb(s,SSL_CB_ACCEPT_EXIT,ret);
569 static int ssl3_send_hello_request(SSL *s)
573 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
575 p=(unsigned char *)s->init_buf->data;
576 *(p++)=SSL3_MT_HELLO_REQUEST;
581 s->state=SSL3_ST_SW_HELLO_REQ_B;
582 /* number of bytes to write */
587 /* SSL3_ST_SW_HELLO_REQ_B */
588 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
591 static int ssl3_check_client_hello(SSL *s)
596 /* this function is called when we really expect a Certificate message,
597 * so permit appropriate message length */
598 n=ssl3_get_message(s,
602 #if defined(MSDOS) && !defined(WIN32)
603 1024*30, /* 30k max cert list :-) */
605 1024*100, /* 100k max cert list :-) */
608 if (!ok) return((int)n);
609 s->s3->tmp.reuse_message = 1;
610 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
612 /* Throw away what we have done so far in the current handshake,
613 * which will now be aborted. (A full SSL_clear would be too much.)
614 * I hope that tmp.dh is the only thing that may need to be cleared
615 * when a handshake is not completed ... */
617 if (s->s3->tmp.dh != NULL)
619 DH_free(s->s3->tmp.dh);
620 s->s3->tmp.dh = NULL;
628 static int ssl3_get_client_hello(SSL *s)
630 int i,j,ok,al,ret= -1;
633 unsigned char *p,*d,*q;
636 STACK_OF(SSL_CIPHER) *ciphers=NULL;
638 /* We do this so that we will respond with our native type.
639 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
640 * This down switching should be handled by a different method.
641 * If we are SSLv3, we will respond with SSLv3, even if prompted with
644 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
647 s->state=SSL3_ST_SR_CLNT_HELLO_B;
649 n=ssl3_get_message(s,
650 SSL3_ST_SR_CLNT_HELLO_B,
651 SSL3_ST_SR_CLNT_HELLO_C,
652 SSL3_MT_CLIENT_HELLO,
653 SSL3_RT_MAX_PLAIN_LENGTH,
656 if (!ok) return((int)n);
657 d=p=(unsigned char *)s->init_buf->data;
659 /* use version from inside client hello, not from record header
660 * (may differ: see RFC 2246, Appendix E, second paragraph) */
661 s->client_version=(((int)p[0])<<8)|(int)p[1];
664 if (s->client_version < s->version)
666 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
667 if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
669 /* similar to ssl3_get_record, send alert using remote version number */
670 s->version = s->client_version;
672 al = SSL_AD_PROTOCOL_VERSION;
676 /* load the client random */
677 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
680 /* get the session-id */
686 if (!ssl_get_new_session(s,1))
691 i=ssl_get_prev_session(s,p,j);
693 { /* previous session */
700 if (!ssl_get_new_session(s,1))
707 if ((i == 0) && (j != 0))
709 /* we need a cipher if we are not resuming a session */
710 al=SSL_AD_ILLEGAL_PARAMETER;
711 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
716 /* not enough data */
717 al=SSL_AD_DECODE_ERROR;
718 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
721 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
728 /* If it is a hit, check that the cipher is in the list */
729 if ((s->hit) && (i > 0))
732 id=s->session->cipher->id;
735 printf("client sent %d ciphers\n",sk_num(ciphers));
737 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
739 c=sk_SSL_CIPHER_value(ciphers,i);
741 printf("client [%2d of %2d]:%s\n",
742 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
752 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
754 /* Very bad for multi-threading.... */
755 s->session->cipher=sk_SSL_CIPHER_value(ciphers,
760 /* we need to have the cipher in the cipher
761 * list if we are asked to reuse it */
762 al=SSL_AD_ILLEGAL_PARAMETER;
763 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
774 if (p[j] == 0) break;
781 al=SSL_AD_DECODE_ERROR;
782 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
786 /* Worst case, we will use the NULL compression, but if we have other
787 * options, we will now look for them. We have i-1 compression
788 * algorithms from the client, starting at q. */
789 s->s3->tmp.new_compression=NULL;
790 if (s->ctx->comp_methods != NULL)
791 { /* See if we have a match */
794 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
797 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
810 s->s3->tmp.new_compression=comp;
815 /* TLS does not mind if there is extra stuff */
816 if (s->version == SSL3_VERSION)
820 /* wrong number of bytes,
821 * there could be more to follow */
822 al=SSL_AD_DECODE_ERROR;
823 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
828 /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must
833 s->session->compress_meth=(comp == NULL)?0:comp->id;
834 if (s->session->ciphers != NULL)
835 sk_SSL_CIPHER_free(s->session->ciphers);
836 s->session->ciphers=ciphers;
839 al=SSL_AD_ILLEGAL_PARAMETER;
840 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
844 c=ssl3_choose_cipher(s,s->session->ciphers,
845 ssl_get_ciphers_by_id(s));
849 al=SSL_AD_HANDSHAKE_FAILURE;
850 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
853 s->s3->tmp.new_cipher=c;
857 /* Session-id reuse */
858 #ifdef REUSE_CIPHER_BUG
859 STACK_OF(SSL_CIPHER) *sk;
863 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
865 sk=s->session->ciphers;
866 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
868 c=sk_SSL_CIPHER_value(sk,i);
869 if (c->algorithms & SSL_eNULL)
871 if (SSL_C_IS_EXPORT(c))
875 s->s3->tmp.new_cipher=nc;
877 s->s3->tmp.new_cipher=ec;
879 s->s3->tmp.new_cipher=s->session->cipher;
883 s->s3->tmp.new_cipher=s->session->cipher;
886 /* we now have the following setup.
888 * cipher_list - our prefered list of ciphers
889 * ciphers - the clients prefered list of ciphers
890 * compression - basically ignored right now
891 * ssl version is set - sslv3
892 * s->session - The ssl session has been setup.
893 * s->hit - session reuse flag
894 * s->tmp.new_cipher - the new cipher to use.
901 ssl3_send_alert(s,SSL3_AL_FATAL,al);
904 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
908 static int ssl3_send_server_hello(SSL *s)
913 unsigned long l,Time;
915 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
917 buf=(unsigned char *)s->init_buf->data;
918 p=s->s3->server_random;
919 Time=time(NULL); /* Time */
921 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
922 /* Do the message type and length last */
925 *(p++)=s->version>>8;
926 *(p++)=s->version&0xff;
929 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
932 /* now in theory we have 3 options to sending back the
933 * session id. If it is a re-use, we send back the
934 * old session-id, if it is a new session, we send
935 * back the new session-id or we send back a 0 length
936 * session-id if we want it to be single use.
937 * Currently I will not implement the '0' length session-id
938 * 12-Jan-98 - I'll now support the '0' length stuff.
940 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
941 s->session->session_id_length=0;
943 sl=s->session->session_id_length;
945 memcpy(p,s->session->session_id,sl);
949 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
952 /* put the compression method */
953 if (s->s3->tmp.new_compression == NULL)
956 *(p++)=s->s3->tmp.new_compression->id;
961 *(d++)=SSL3_MT_SERVER_HELLO;
964 s->state=SSL3_ST_CW_CLNT_HELLO_B;
965 /* number of bytes to write */
970 /* SSL3_ST_CW_CLNT_HELLO_B */
971 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
974 static int ssl3_send_server_done(SSL *s)
978 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
980 p=(unsigned char *)s->init_buf->data;
983 *(p++)=SSL3_MT_SERVER_DONE;
988 s->state=SSL3_ST_SW_SRVR_DONE_B;
989 /* number of bytes to write */
994 /* SSL3_ST_CW_CLNT_HELLO_B */
995 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
998 static int ssl3_send_server_key_exchange(SSL *s)
1004 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1011 unsigned char *p,*d;
1021 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1023 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1028 r[0]=r[1]=r[2]=r[3]=NULL;
1031 if (type & SSL_kRSA)
1034 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1036 rsa=s->cert->rsa_tmp_cb(s,
1037 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1038 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1041 al=SSL_AD_HANDSHAKE_FAILURE;
1042 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1045 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
1050 al=SSL_AD_HANDSHAKE_FAILURE;
1051 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1056 s->s3->tmp.use_rsa_tmp=1;
1061 if (type & SSL_kEDH)
1064 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1065 dhp=s->cert->dh_tmp_cb(s,
1066 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1067 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1070 al=SSL_AD_HANDSHAKE_FAILURE;
1071 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1075 if (s->s3->tmp.dh != NULL)
1078 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_INTERNAL_ERROR);
1082 if ((dh=DHparams_dup(dhp)) == NULL)
1084 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1089 if ((dhp->pub_key == NULL ||
1090 dhp->priv_key == NULL ||
1091 (s->options & SSL_OP_SINGLE_DH_USE)))
1093 if(!DH_generate_key(dh))
1095 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1102 dh->pub_key=BN_dup(dhp->pub_key);
1103 dh->priv_key=BN_dup(dhp->priv_key);
1104 if ((dh->pub_key == NULL) ||
1105 (dh->priv_key == NULL))
1107 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1118 al=SSL_AD_HANDSHAKE_FAILURE;
1119 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1122 for (i=0; r[i] != NULL; i++)
1124 nr[i]=BN_num_bytes(r[i]);
1128 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1130 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1133 al=SSL_AD_DECODE_ERROR;
1136 kn=EVP_PKEY_size(pkey);
1144 if (!BUF_MEM_grow(buf,n+4+kn))
1146 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1149 d=(unsigned char *)s->init_buf->data;
1152 for (i=0; r[i] != NULL; i++)
1162 /* n is the length of the params, they start at &(d[4])
1163 * and p points to the space at the end. */
1165 if (pkey->type == EVP_PKEY_RSA)
1169 for (num=2; num > 0; num--)
1171 EVP_DigestInit(&md_ctx,(num == 2)
1172 ?s->ctx->md5:s->ctx->sha1);
1173 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1174 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1175 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1176 EVP_DigestFinal(&md_ctx,q,
1177 (unsigned int *)&i);
1181 if (RSA_sign(NID_md5_sha1, md_buf, j,
1182 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1184 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1192 #if !defined(NO_DSA)
1193 if (pkey->type == EVP_PKEY_DSA)
1196 EVP_SignInit(&md_ctx,EVP_dss1());
1197 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1198 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1199 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1200 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1201 (unsigned int *)&i,pkey))
1203 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1212 /* Is this error check actually needed? */
1213 al=SSL_AD_HANDSHAKE_FAILURE;
1214 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1219 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1222 /* we should now have things packed up, so lets send
1228 s->state = SSL3_ST_SW_KEY_EXCH_B;
1229 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1231 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1236 static int ssl3_send_certificate_request(SSL *s)
1238 unsigned char *p,*d;
1240 STACK_OF(X509_NAME) *sk=NULL;
1244 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1248 d=p=(unsigned char *)&(buf->data[4]);
1250 /* get the list of acceptable cert types */
1252 n=ssl3_get_req_cert_type(s,p);
1261 sk=SSL_get_client_CA_list(s);
1265 for (i=0; i<sk_X509_NAME_num(sk); i++)
1267 name=sk_X509_NAME_value(sk,i);
1268 j=i2d_X509_NAME(name,NULL);
1269 if (!BUF_MEM_grow(buf,4+n+j+2))
1271 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1274 p=(unsigned char *)&(buf->data[4+n]);
1275 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1278 i2d_X509_NAME(name,&p);
1285 i2d_X509_NAME(name,&p);
1286 j-=2; s2n(j,d); j+=2;
1292 /* else no CA names */
1293 p=(unsigned char *)&(buf->data[4+off]);
1296 d=(unsigned char *)buf->data;
1297 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1300 /* we should now have things packed up, so lets send
1305 #ifdef NETSCAPE_HANG_BUG
1306 p=(unsigned char *)s->init_buf->data + s->init_num;
1309 *(p++)=SSL3_MT_SERVER_DONE;
1318 /* SSL3_ST_SW_CERT_REQ_B */
1319 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1324 static int ssl3_get_client_key_exchange(SSL *s)
1332 EVP_PKEY *pkey=NULL;
1339 n=ssl3_get_message(s,
1340 SSL3_ST_SR_KEY_EXCH_A,
1341 SSL3_ST_SR_KEY_EXCH_B,
1342 SSL3_MT_CLIENT_KEY_EXCHANGE,
1346 if (!ok) return((int)n);
1347 p=(unsigned char *)s->init_buf->data;
1349 l=s->s3->tmp.new_cipher->algorithms;
1354 /* FIX THIS UP EAY EAY EAY EAY */
1355 if (s->s3->tmp.use_rsa_tmp)
1357 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1358 rsa=s->cert->rsa_tmp;
1359 /* Don't do a callback because rsa_tmp should
1360 * be sent already */
1363 al=SSL_AD_HANDSHAKE_FAILURE;
1364 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1371 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1372 if ( (pkey == NULL) ||
1373 (pkey->type != EVP_PKEY_RSA) ||
1374 (pkey->pkey.rsa == NULL))
1376 al=SSL_AD_HANDSHAKE_FAILURE;
1377 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1384 if (s->version > SSL3_VERSION)
1389 if (!(s->options & SSL_OP_TLS_D5_BUG))
1391 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1401 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1405 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1407 al=SSL_AD_DECODE_ERROR;
1408 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1411 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
1413 /* The premaster secret must contain the same version number as the
1414 * ClientHello to detect version rollback attacks (strangely, the
1415 * protocol does not offer such protection for DH ciphersuites).
1416 * However, buggy clients exist that send the negotiated protocol
1417 * version instead if the server does not support the requested
1419 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1420 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1421 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
1423 al=SSL_AD_DECODE_ERROR;
1424 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1434 /* Some decryption failure -- use random value instead as countermeasure
1435 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1436 * (see RFC 2246, section 7.4.7.1).
1437 * But note that due to length and protocol version checking, the
1438 * attack is impractical anyway (see section 5 in D. Bleichenbacher:
1439 * "Chosen Ciphertext Attacks Against Protocols Based on the RSA
1440 * Encryption Standard PKCS #1", CRYPTO '98, LNCS 1462, pp. 1-12).
1443 i = SSL_MAX_MASTER_KEY_LENGTH;
1444 p[0] = s->client_version >> 8;
1445 p[1] = s->client_version & 0xff;
1446 RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */
1450 s->session->master_key_length=
1451 s->method->ssl3_enc->generate_master_secret(s,
1452 s->session->master_key,
1459 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1464 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1466 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1476 if (n == 0L) /* the parameters are in the cert */
1478 al=SSL_AD_HANDSHAKE_FAILURE;
1479 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1484 if (s->s3->tmp.dh == NULL)
1486 al=SSL_AD_HANDSHAKE_FAILURE;
1487 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1491 dh_srvr=s->s3->tmp.dh;
1494 pub=BN_bin2bn(p,i,NULL);
1497 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1501 i=DH_compute_key(p,pub,dh_srvr);
1505 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1509 DH_free(s->s3->tmp.dh);
1514 s->session->master_key_length=
1515 s->method->ssl3_enc->generate_master_secret(s,
1516 s->session->master_key,p,i);
1522 al=SSL_AD_HANDSHAKE_FAILURE;
1523 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE);
1529 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1530 #if !defined(NO_DH) || !defined(NO_RSA)
1536 static int ssl3_get_cert_verify(SSL *s)
1538 EVP_PKEY *pkey=NULL;
1545 n=ssl3_get_message(s,
1546 SSL3_ST_SR_CERT_VRFY_A,
1547 SSL3_ST_SR_CERT_VRFY_B,
1552 if (!ok) return((int)n);
1554 if (s->session->peer != NULL)
1556 peer=s->session->peer;
1557 pkey=X509_get_pubkey(peer);
1558 type=X509_certificate_type(peer,pkey);
1566 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
1568 s->s3->tmp.reuse_message=1;
1569 if ((peer != NULL) && (type | EVP_PKT_SIGN))
1571 al=SSL_AD_UNEXPECTED_MESSAGE;
1572 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
1581 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
1582 al=SSL_AD_UNEXPECTED_MESSAGE;
1586 if (!(type & EVP_PKT_SIGN))
1588 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
1589 al=SSL_AD_ILLEGAL_PARAMETER;
1593 if (s->s3->change_cipher_spec)
1595 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
1596 al=SSL_AD_UNEXPECTED_MESSAGE;
1600 /* we now have a signature that we need to verify */
1601 p=(unsigned char *)s->init_buf->data;
1606 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
1607 al=SSL_AD_DECODE_ERROR;
1611 j=EVP_PKEY_size(pkey);
1612 if ((i > j) || (n > j) || (n <= 0))
1614 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
1615 al=SSL_AD_DECODE_ERROR;
1620 if (pkey->type == EVP_PKEY_RSA)
1622 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
1623 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
1627 al=SSL_AD_DECRYPT_ERROR;
1628 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
1633 al=SSL_AD_DECRYPT_ERROR;
1634 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
1641 if (pkey->type == EVP_PKEY_DSA)
1643 j=DSA_verify(pkey->save_type,
1644 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
1645 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
1649 al=SSL_AD_DECRYPT_ERROR;
1650 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
1657 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR);
1658 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
1667 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1670 EVP_PKEY_free(pkey);
1674 static int ssl3_get_client_certificate(SSL *s)
1676 int i,ok,al,ret= -1;
1678 unsigned long l,nc,llen,n;
1679 unsigned char *p,*d,*q;
1680 STACK_OF(X509) *sk=NULL;
1682 n=ssl3_get_message(s,
1686 #if defined(MSDOS) && !defined(WIN32)
1687 1024*30, /* 30k max cert list :-) */
1689 1024*100, /* 100k max cert list :-) */
1693 if (!ok) return((int)n);
1695 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
1697 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
1698 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1700 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1701 al=SSL_AD_HANDSHAKE_FAILURE;
1704 /* If tls asked for a client cert, the client must return a 0 list */
1705 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
1707 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
1708 al=SSL_AD_UNEXPECTED_MESSAGE;
1711 s->s3->tmp.reuse_message=1;
1715 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1717 al=SSL_AD_UNEXPECTED_MESSAGE;
1718 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
1721 d=p=(unsigned char *)s->init_buf->data;
1723 if ((sk=sk_X509_new_null()) == NULL)
1725 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1732 al=SSL_AD_DECODE_ERROR;
1733 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1736 for (nc=0; nc<llen; )
1739 if ((l+nc+3) > llen)
1741 al=SSL_AD_DECODE_ERROR;
1742 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1747 x=d2i_X509(NULL,&p,l);
1750 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
1755 al=SSL_AD_DECODE_ERROR;
1756 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1759 if (!sk_X509_push(sk,x))
1761 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1768 if (sk_X509_num(sk) <= 0)
1770 /* TLS does not mind 0 certs returned */
1771 if (s->version == SSL3_VERSION)
1773 al=SSL_AD_HANDSHAKE_FAILURE;
1774 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
1777 /* Fail for TLS only if we required a certificate */
1778 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
1779 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1781 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1782 al=SSL_AD_HANDSHAKE_FAILURE;
1788 i=ssl_verify_cert_chain(s,sk);
1791 al=ssl_verify_alarm_type(s->verify_result);
1792 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
1797 if (s->session->peer != NULL) /* This should not be needed */
1798 X509_free(s->session->peer);
1799 s->session->peer=sk_X509_shift(sk);
1800 s->session->verify_result = s->verify_result;
1802 /* With the current implementation, sess_cert will always be NULL
1803 * when we arrive here. */
1804 if (s->session->sess_cert == NULL)
1806 s->session->sess_cert = ssl_sess_cert_new();
1807 if (s->session->sess_cert == NULL)
1809 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1813 if (s->session->sess_cert->cert_chain != NULL)
1814 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
1815 s->session->sess_cert->cert_chain=sk;
1816 /* Inconsistency alert: cert_chain does *not* include the
1817 * peer's own certificate, while we do include it in s3_clnt.c */
1825 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1828 if (x != NULL) X509_free(x);
1829 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
1833 int ssl3_send_server_certificate(SSL *s)
1838 if (s->state == SSL3_ST_SW_CERT_A)
1840 x=ssl_get_server_send_cert(s);
1843 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR);
1847 l=ssl3_output_cert_chain(s,x);
1848 s->state=SSL3_ST_SW_CERT_B;
1853 /* SSL3_ST_SW_CERT_B */
1854 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));