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-2001 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/evp.h>
121 #include <openssl/x509.h>
122 #include <openssl/krb5_asn.h>
123 #include "ssl_locl.h"
124 #ifndef OPENSSL_NO_KRB5
125 #include "kssl_lcl.h"
126 #endif /* OPENSSL_NO_KRB5 */
127 #include <openssl/md5.h>
129 static SSL_METHOD *ssl3_get_server_method(int ver);
130 static int ssl3_get_client_hello(SSL *s);
131 static int ssl3_check_client_hello(SSL *s);
132 static int ssl3_send_server_hello(SSL *s);
133 static int ssl3_send_server_key_exchange(SSL *s);
134 static int ssl3_send_certificate_request(SSL *s);
135 static int ssl3_send_server_done(SSL *s);
136 static int ssl3_get_client_key_exchange(SSL *s);
137 static int ssl3_get_client_certificate(SSL *s);
138 static int ssl3_get_cert_verify(SSL *s);
139 static int ssl3_send_hello_request(SSL *s);
141 static SSL_METHOD *ssl3_get_server_method(int ver)
143 if (ver == SSL3_VERSION)
144 return(SSLv3_server_method());
149 SSL_METHOD *SSLv3_server_method(void)
152 static SSL_METHOD SSLv3_server_data;
156 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
158 SSLv3_server_data.ssl_accept=ssl3_accept;
159 SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
162 return(&SSLv3_server_data);
165 int ssl3_accept(SSL *s)
168 unsigned long l,Time=time(NULL);
172 int new_state,state,skip=0;
173 int got_new_session=0;
175 RAND_add(&Time,sizeof(Time),0);
179 if (s->info_callback != NULL)
181 else if (s->ctx->info_callback != NULL)
182 cb=s->ctx->info_callback;
184 /* init things to blank */
185 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
190 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
200 case SSL_ST_RENEGOTIATE:
202 /* s->state=SSL_ST_ACCEPT; */
206 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
207 case SSL_ST_OK|SSL_ST_ACCEPT:
210 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
212 if ((s->version>>8) != 3)
214 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
217 s->type=SSL_ST_ACCEPT;
219 if (s->init_buf == NULL)
221 if ((buf=BUF_MEM_new()) == NULL)
226 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
234 if (!ssl3_setup_buffers(s))
242 if (s->state != SSL_ST_RENEGOTIATE)
244 /* Ok, we now need to push on a buffering BIO so that
245 * the output is sent in a way that TCP likes :-)
247 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
249 ssl3_init_finished_mac(s);
250 s->state=SSL3_ST_SR_CLNT_HELLO_A;
251 s->ctx->stats.sess_accept++;
255 /* s->state == SSL_ST_RENEGOTIATE,
256 * we will just send a HelloRequest */
257 s->ctx->stats.sess_accept_renegotiate++;
258 s->state=SSL3_ST_SW_HELLO_REQ_A;
262 case SSL3_ST_SW_HELLO_REQ_A:
263 case SSL3_ST_SW_HELLO_REQ_B:
266 ret=ssl3_send_hello_request(s);
267 if (ret <= 0) goto end;
268 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
269 s->state=SSL3_ST_SW_FLUSH;
272 ssl3_init_finished_mac(s);
275 case SSL3_ST_SW_HELLO_REQ_C:
279 case SSL3_ST_SR_CLNT_HELLO_A:
280 case SSL3_ST_SR_CLNT_HELLO_B:
281 case SSL3_ST_SR_CLNT_HELLO_C:
284 ret=ssl3_get_client_hello(s);
285 if (ret <= 0) goto end;
287 s->state=SSL3_ST_SW_SRVR_HELLO_A;
291 case SSL3_ST_SW_SRVR_HELLO_A:
292 case SSL3_ST_SW_SRVR_HELLO_B:
293 ret=ssl3_send_server_hello(s);
294 if (ret <= 0) goto end;
297 s->state=SSL3_ST_SW_CHANGE_A;
299 s->state=SSL3_ST_SW_CERT_A;
303 case SSL3_ST_SW_CERT_A:
304 case SSL3_ST_SW_CERT_B:
305 /* Check if it is anon DH */
306 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
308 ret=ssl3_send_server_certificate(s);
309 if (ret <= 0) goto end;
313 s->state=SSL3_ST_SW_KEY_EXCH_A;
317 case SSL3_ST_SW_KEY_EXCH_A:
318 case SSL3_ST_SW_KEY_EXCH_B:
319 l=s->s3->tmp.new_cipher->algorithms;
321 /* clear this, it may get reset by
322 * send_server_key_exchange */
323 if ((s->options & SSL_OP_EPHEMERAL_RSA)
324 #ifndef OPENSSL_NO_KRB5
326 #endif /* OPENSSL_NO_KRB5 */
328 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
329 * even when forbidden by protocol specs
330 * (handshake may fail as clients are not required to
331 * be able to handle this) */
332 s->s3->tmp.use_rsa_tmp=1;
334 s->s3->tmp.use_rsa_tmp=0;
336 /* only send if a DH key exchange, fortezza or
337 * RSA but we have a sign only certificate */
338 if (s->s3->tmp.use_rsa_tmp
339 || (l & (SSL_DH|SSL_kFZA))
341 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
342 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
343 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
349 ret=ssl3_send_server_key_exchange(s);
350 if (ret <= 0) goto end;
355 s->state=SSL3_ST_SW_CERT_REQ_A;
359 case SSL3_ST_SW_CERT_REQ_A:
360 case SSL3_ST_SW_CERT_REQ_B:
361 if (/* don't request cert unless asked for it: */
362 !(s->verify_mode & SSL_VERIFY_PEER) ||
363 /* if SSL_VERIFY_CLIENT_ONCE is set,
364 * don't request cert during re-negotiation: */
365 ((s->session->peer != NULL) &&
366 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
367 /* never request cert in anonymous ciphersuites
368 * (see section "Certificate request" in SSL 3 drafts
369 * and in RFC 2246): */
370 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
371 /* ... except when the application insists on verification
372 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
373 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
374 /* never request cert in Kerberos ciphersuites */
375 (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
377 /* no cert request */
379 s->s3->tmp.cert_request=0;
380 s->state=SSL3_ST_SW_SRVR_DONE_A;
384 s->s3->tmp.cert_request=1;
385 ret=ssl3_send_certificate_request(s);
386 if (ret <= 0) goto end;
387 #ifndef NETSCAPE_HANG_BUG
388 s->state=SSL3_ST_SW_SRVR_DONE_A;
390 s->state=SSL3_ST_SW_FLUSH;
391 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
397 case SSL3_ST_SW_SRVR_DONE_A:
398 case SSL3_ST_SW_SRVR_DONE_B:
399 ret=ssl3_send_server_done(s);
400 if (ret <= 0) goto end;
401 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
402 s->state=SSL3_ST_SW_FLUSH;
406 case SSL3_ST_SW_FLUSH:
407 /* number of bytes to be flushed */
408 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
411 s->rwstate=SSL_WRITING;
412 num1=BIO_flush(s->wbio);
413 if (num1 <= 0) { ret= -1; goto end; }
414 s->rwstate=SSL_NOTHING;
417 s->state=s->s3->tmp.next_state;
420 case SSL3_ST_SR_CERT_A:
421 case SSL3_ST_SR_CERT_B:
422 /* Check for second client hello (MS SGC) */
423 ret = ssl3_check_client_hello(s);
427 s->state = SSL3_ST_SR_CLNT_HELLO_C;
429 /* could be sent for a DH cert, even if we
430 * have not asked for it :-) */
431 ret=ssl3_get_client_certificate(s);
432 if (ret <= 0) goto end;
434 s->state=SSL3_ST_SR_KEY_EXCH_A;
438 case SSL3_ST_SR_KEY_EXCH_A:
439 case SSL3_ST_SR_KEY_EXCH_B:
440 ret=ssl3_get_client_key_exchange(s);
441 if (ret <= 0) goto end;
442 s->state=SSL3_ST_SR_CERT_VRFY_A;
445 /* We need to get hashes here so if there is
446 * a client cert, it can be verified */
447 s->method->ssl3_enc->cert_verify_mac(s,
448 &(s->s3->finish_dgst1),
449 &(s->s3->tmp.cert_verify_md[0]));
450 s->method->ssl3_enc->cert_verify_mac(s,
451 &(s->s3->finish_dgst2),
452 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
456 case SSL3_ST_SR_CERT_VRFY_A:
457 case SSL3_ST_SR_CERT_VRFY_B:
459 /* we should decide if we expected this one */
460 ret=ssl3_get_cert_verify(s);
461 if (ret <= 0) goto end;
463 s->state=SSL3_ST_SR_FINISHED_A;
467 case SSL3_ST_SR_FINISHED_A:
468 case SSL3_ST_SR_FINISHED_B:
469 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
470 SSL3_ST_SR_FINISHED_B);
471 if (ret <= 0) goto end;
475 s->state=SSL3_ST_SW_CHANGE_A;
479 case SSL3_ST_SW_CHANGE_A:
480 case SSL3_ST_SW_CHANGE_B:
482 s->session->cipher=s->s3->tmp.new_cipher;
483 if (!s->method->ssl3_enc->setup_key_block(s))
484 { ret= -1; goto end; }
486 ret=ssl3_send_change_cipher_spec(s,
487 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
489 if (ret <= 0) goto end;
490 s->state=SSL3_ST_SW_FINISHED_A;
493 if (!s->method->ssl3_enc->change_cipher_state(s,
494 SSL3_CHANGE_CIPHER_SERVER_WRITE))
502 case SSL3_ST_SW_FINISHED_A:
503 case SSL3_ST_SW_FINISHED_B:
504 ret=ssl3_send_finished(s,
505 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
506 s->method->ssl3_enc->server_finished_label,
507 s->method->ssl3_enc->server_finished_label_len);
508 if (ret <= 0) goto end;
509 s->state=SSL3_ST_SW_FLUSH;
511 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
513 s->s3->tmp.next_state=SSL_ST_OK;
518 /* clean a few things up */
519 ssl3_cleanup_key_block(s);
521 BUF_MEM_free(s->init_buf);
524 /* remove buffering on output */
525 ssl_free_wbio_buffer(s);
529 if (got_new_session) /* skipped if we just sent a HelloRequest */
531 /* actually not necessarily a 'new' session unless
532 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
536 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
538 s->ctx->stats.sess_accept_good++;
540 s->handshake_func=ssl3_accept;
542 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
549 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
555 if (!s->s3->tmp.reuse_message && !skip)
559 if ((ret=BIO_flush(s->wbio)) <= 0)
564 if ((cb != NULL) && (s->state != state))
568 cb(s,SSL_CB_ACCEPT_LOOP,1);
575 /* BIO_flush(s->wbio); */
578 cb(s,SSL_CB_ACCEPT_EXIT,ret);
583 static int ssl3_send_hello_request(SSL *s)
587 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
589 p=(unsigned char *)s->init_buf->data;
590 *(p++)=SSL3_MT_HELLO_REQUEST;
595 s->state=SSL3_ST_SW_HELLO_REQ_B;
596 /* number of bytes to write */
601 /* SSL3_ST_SW_HELLO_REQ_B */
602 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
605 static int ssl3_check_client_hello(SSL *s)
610 /* this function is called when we really expect a Certificate message,
611 * so permit appropriate message length */
612 n=ssl3_get_message(s,
618 if (!ok) return((int)n);
619 s->s3->tmp.reuse_message = 1;
620 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
622 /* Throw away what we have done so far in the current handshake,
623 * which will now be aborted. (A full SSL_clear would be too much.)
624 * I hope that tmp.dh is the only thing that may need to be cleared
625 * when a handshake is not completed ... */
626 #ifndef OPENSSL_NO_DH
627 if (s->s3->tmp.dh != NULL)
629 DH_free(s->s3->tmp.dh);
630 s->s3->tmp.dh = NULL;
638 static int ssl3_get_client_hello(SSL *s)
640 int i,j,ok,al,ret= -1;
643 unsigned char *p,*d,*q;
646 STACK_OF(SSL_CIPHER) *ciphers=NULL;
648 /* We do this so that we will respond with our native type.
649 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
650 * This down switching should be handled by a different method.
651 * If we are SSLv3, we will respond with SSLv3, even if prompted with
654 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
657 s->state=SSL3_ST_SR_CLNT_HELLO_B;
659 n=ssl3_get_message(s,
660 SSL3_ST_SR_CLNT_HELLO_B,
661 SSL3_ST_SR_CLNT_HELLO_C,
662 SSL3_MT_CLIENT_HELLO,
663 SSL3_RT_MAX_PLAIN_LENGTH,
666 if (!ok) return((int)n);
667 d=p=(unsigned char *)s->init_buf->data;
669 /* use version from inside client hello, not from record header
670 * (may differ: see RFC 2246, Appendix E, second paragraph) */
671 s->client_version=(((int)p[0])<<8)|(int)p[1];
674 /* load the client random */
675 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
678 /* get the session-id */
682 /* Versions before 0.9.7 always allow session reuse during renegotiation
683 * (i.e. when s->new_session is true), option
684 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
685 * Maybe this optional behaviour should always have been the default,
686 * but we cannot safely change the default behaviour (or new applications
687 * might be written that become totally unsecure when compiled with
688 * an earlier library version)
690 if (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
692 if (!ssl_get_new_session(s,1))
697 i=ssl_get_prev_session(s,p,j);
699 { /* previous session */
706 if (!ssl_get_new_session(s,1))
713 if ((i == 0) && (j != 0))
715 /* we need a cipher if we are not resuming a session */
716 al=SSL_AD_ILLEGAL_PARAMETER;
717 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
722 /* not enough data */
723 al=SSL_AD_DECODE_ERROR;
724 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
727 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
734 /* If it is a hit, check that the cipher is in the list */
735 if ((s->hit) && (i > 0))
738 id=s->session->cipher->id;
741 printf("client sent %d ciphers\n",sk_num(ciphers));
743 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
745 c=sk_SSL_CIPHER_value(ciphers,i);
747 printf("client [%2d of %2d]:%s\n",
748 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
758 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
760 /* Very bad for multi-threading.... */
761 s->session->cipher=sk_SSL_CIPHER_value(ciphers,
766 /* we need to have the cipher in the cipher
767 * list if we are asked to reuse it */
768 al=SSL_AD_ILLEGAL_PARAMETER;
769 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
780 if (p[j] == 0) break;
787 al=SSL_AD_DECODE_ERROR;
788 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
792 /* Worst case, we will use the NULL compression, but if we have other
793 * options, we will now look for them. We have i-1 compression
794 * algorithms from the client, starting at q. */
795 s->s3->tmp.new_compression=NULL;
796 if (s->ctx->comp_methods != NULL)
797 { /* See if we have a match */
800 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
803 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
816 s->s3->tmp.new_compression=comp;
821 /* TLS does not mind if there is extra stuff */
822 if (s->version == SSL3_VERSION)
826 /* wrong number of bytes,
827 * there could be more to follow */
828 al=SSL_AD_DECODE_ERROR;
829 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
834 /* Given s->session->ciphers and SSL_get_ciphers, we must
839 s->session->compress_meth=(comp == NULL)?0:comp->id;
840 if (s->session->ciphers != NULL)
841 sk_SSL_CIPHER_free(s->session->ciphers);
842 s->session->ciphers=ciphers;
845 al=SSL_AD_ILLEGAL_PARAMETER;
846 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
850 c=ssl3_choose_cipher(s,s->session->ciphers,
855 al=SSL_AD_HANDSHAKE_FAILURE;
856 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
859 s->s3->tmp.new_cipher=c;
863 /* Session-id reuse */
864 #ifdef REUSE_CIPHER_BUG
865 STACK_OF(SSL_CIPHER) *sk;
869 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
871 sk=s->session->ciphers;
872 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
874 c=sk_SSL_CIPHER_value(sk,i);
875 if (c->algorithms & SSL_eNULL)
877 if (SSL_C_IS_EXPORT(c))
881 s->s3->tmp.new_cipher=nc;
883 s->s3->tmp.new_cipher=ec;
885 s->s3->tmp.new_cipher=s->session->cipher;
889 s->s3->tmp.new_cipher=s->session->cipher;
892 /* we now have the following setup.
894 * cipher_list - our prefered list of ciphers
895 * ciphers - the clients prefered list of ciphers
896 * compression - basically ignored right now
897 * ssl version is set - sslv3
898 * s->session - The ssl session has been setup.
899 * s->hit - session reuse flag
900 * s->tmp.new_cipher - the new cipher to use.
907 ssl3_send_alert(s,SSL3_AL_FATAL,al);
910 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
914 static int ssl3_send_server_hello(SSL *s)
919 unsigned long l,Time;
921 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
923 buf=(unsigned char *)s->init_buf->data;
924 p=s->s3->server_random;
925 Time=time(NULL); /* Time */
927 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
928 /* Do the message type and length last */
931 *(p++)=s->version>>8;
932 *(p++)=s->version&0xff;
935 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
938 /* now in theory we have 3 options to sending back the
939 * session id. If it is a re-use, we send back the
940 * old session-id, if it is a new session, we send
941 * back the new session-id or we send back a 0 length
942 * session-id if we want it to be single use.
943 * Currently I will not implement the '0' length session-id
944 * 12-Jan-98 - I'll now support the '0' length stuff.
946 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
947 s->session->session_id_length=0;
949 sl=s->session->session_id_length;
951 memcpy(p,s->session->session_id,sl);
955 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
958 /* put the compression method */
959 if (s->s3->tmp.new_compression == NULL)
962 *(p++)=s->s3->tmp.new_compression->id;
967 *(d++)=SSL3_MT_SERVER_HELLO;
970 s->state=SSL3_ST_CW_CLNT_HELLO_B;
971 /* number of bytes to write */
976 /* SSL3_ST_CW_CLNT_HELLO_B */
977 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
980 static int ssl3_send_server_done(SSL *s)
984 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
986 p=(unsigned char *)s->init_buf->data;
989 *(p++)=SSL3_MT_SERVER_DONE;
994 s->state=SSL3_ST_SW_SRVR_DONE_B;
995 /* number of bytes to write */
1000 /* SSL3_ST_CW_CLNT_HELLO_B */
1001 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1004 static int ssl3_send_server_key_exchange(SSL *s)
1006 #ifndef OPENSSL_NO_RSA
1010 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1013 #ifndef OPENSSL_NO_DH
1017 unsigned char *p,*d;
1027 EVP_MD_CTX_init(&md_ctx);
1028 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1030 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1035 r[0]=r[1]=r[2]=r[3]=NULL;
1037 #ifndef OPENSSL_NO_RSA
1038 if (type & SSL_kRSA)
1041 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1043 rsa=s->cert->rsa_tmp_cb(s,
1044 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1045 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1048 al=SSL_AD_HANDSHAKE_FAILURE;
1049 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1057 al=SSL_AD_HANDSHAKE_FAILURE;
1058 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1063 s->s3->tmp.use_rsa_tmp=1;
1067 #ifndef OPENSSL_NO_DH
1068 if (type & SSL_kEDH)
1071 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1072 dhp=s->cert->dh_tmp_cb(s,
1073 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1074 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1077 al=SSL_AD_HANDSHAKE_FAILURE;
1078 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1082 if (s->s3->tmp.dh != NULL)
1085 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1089 if ((dh=DHparams_dup(dhp)) == NULL)
1091 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1096 if ((dhp->pub_key == NULL ||
1097 dhp->priv_key == NULL ||
1098 (s->options & SSL_OP_SINGLE_DH_USE)))
1100 if(!DH_generate_key(dh))
1102 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1109 dh->pub_key=BN_dup(dhp->pub_key);
1110 dh->priv_key=BN_dup(dhp->priv_key);
1111 if ((dh->pub_key == NULL) ||
1112 (dh->priv_key == NULL))
1114 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1125 al=SSL_AD_HANDSHAKE_FAILURE;
1126 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1129 for (i=0; r[i] != NULL; i++)
1131 nr[i]=BN_num_bytes(r[i]);
1135 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1137 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1140 al=SSL_AD_DECODE_ERROR;
1143 kn=EVP_PKEY_size(pkey);
1151 if (!BUF_MEM_grow(buf,n+4+kn))
1153 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1156 d=(unsigned char *)s->init_buf->data;
1159 for (i=0; r[i] != NULL; i++)
1169 /* n is the length of the params, they start at &(d[4])
1170 * and p points to the space at the end. */
1171 #ifndef OPENSSL_NO_RSA
1172 if (pkey->type == EVP_PKEY_RSA)
1176 for (num=2; num > 0; num--)
1178 EVP_DigestInit(&md_ctx,(num == 2)
1179 ?s->ctx->md5:s->ctx->sha1);
1180 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1181 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1182 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1183 EVP_DigestFinal(&md_ctx,q,
1184 (unsigned int *)&i);
1188 if (RSA_sign(NID_md5_sha1, md_buf, j,
1189 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1191 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1199 #if !defined(OPENSSL_NO_DSA)
1200 if (pkey->type == EVP_PKEY_DSA)
1203 EVP_SignInit(&md_ctx,EVP_dss1());
1204 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1205 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1206 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1207 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1208 (unsigned int *)&i,pkey))
1210 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1219 /* Is this error check actually needed? */
1220 al=SSL_AD_HANDSHAKE_FAILURE;
1221 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1226 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1229 /* we should now have things packed up, so lets send
1235 s->state = SSL3_ST_SW_KEY_EXCH_B;
1236 EVP_MD_CTX_cleanup(&md_ctx);
1237 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1239 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1241 EVP_MD_CTX_cleanup(&md_ctx);
1245 static int ssl3_send_certificate_request(SSL *s)
1247 unsigned char *p,*d;
1249 STACK_OF(X509_NAME) *sk=NULL;
1253 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1257 d=p=(unsigned char *)&(buf->data[4]);
1259 /* get the list of acceptable cert types */
1261 n=ssl3_get_req_cert_type(s,p);
1270 sk=SSL_get_client_CA_list(s);
1274 for (i=0; i<sk_X509_NAME_num(sk); i++)
1276 name=sk_X509_NAME_value(sk,i);
1277 j=i2d_X509_NAME(name,NULL);
1278 if (!BUF_MEM_grow(buf,4+n+j+2))
1280 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1283 p=(unsigned char *)&(buf->data[4+n]);
1284 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1287 i2d_X509_NAME(name,&p);
1294 i2d_X509_NAME(name,&p);
1295 j-=2; s2n(j,d); j+=2;
1301 /* else no CA names */
1302 p=(unsigned char *)&(buf->data[4+off]);
1305 d=(unsigned char *)buf->data;
1306 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1309 /* we should now have things packed up, so lets send
1314 #ifdef NETSCAPE_HANG_BUG
1315 p=(unsigned char *)s->init_buf->data + s->init_num;
1318 *(p++)=SSL3_MT_SERVER_DONE;
1327 /* SSL3_ST_SW_CERT_REQ_B */
1328 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1333 static int ssl3_get_client_key_exchange(SSL *s)
1339 #ifndef OPENSSL_NO_RSA
1341 EVP_PKEY *pkey=NULL;
1343 #ifndef OPENSSL_NO_DH
1347 #ifndef OPENSSL_NO_KRB5
1349 #endif /* OPENSSL_NO_KRB5 */
1351 n=ssl3_get_message(s,
1352 SSL3_ST_SR_KEY_EXCH_A,
1353 SSL3_ST_SR_KEY_EXCH_B,
1354 SSL3_MT_CLIENT_KEY_EXCHANGE,
1358 if (!ok) return((int)n);
1359 p=(unsigned char *)s->init_buf->data;
1361 l=s->s3->tmp.new_cipher->algorithms;
1363 #ifndef OPENSSL_NO_RSA
1366 /* FIX THIS UP EAY EAY EAY EAY */
1367 if (s->s3->tmp.use_rsa_tmp)
1369 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1370 rsa=s->cert->rsa_tmp;
1371 /* Don't do a callback because rsa_tmp should
1372 * be sent already */
1375 al=SSL_AD_HANDSHAKE_FAILURE;
1376 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1383 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1384 if ( (pkey == NULL) ||
1385 (pkey->type != EVP_PKEY_RSA) ||
1386 (pkey->pkey.rsa == NULL))
1388 al=SSL_AD_HANDSHAKE_FAILURE;
1389 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1396 if (s->version > SSL3_VERSION)
1401 if (!(s->options & SSL_OP_TLS_D5_BUG))
1403 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1413 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1417 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1419 al=SSL_AD_DECODE_ERROR;
1420 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1423 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
1425 /* The premaster secret must contain the same version number as the
1426 * ClientHello to detect version rollback attacks (strangely, the
1427 * protocol does not offer such protection for DH ciphersuites).
1428 * However, buggy clients exist that send the negotiated protocol
1429 * version instead if the server does not support the requested
1431 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1432 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1433 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
1435 al=SSL_AD_DECODE_ERROR;
1436 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1446 /* Some decryption failure -- use random value instead as countermeasure
1447 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1448 * (see RFC 2246, section 7.4.7.1).
1449 * But note that due to length and protocol version checking, the
1450 * attack is impractical anyway (see section 5 in D. Bleichenbacher:
1451 * "Chosen Ciphertext Attacks Against Protocols Based on the RSA
1452 * Encryption Standard PKCS #1", CRYPTO '98, LNCS 1462, pp. 1-12).
1455 i = SSL_MAX_MASTER_KEY_LENGTH;
1456 p[0] = s->client_version >> 8;
1457 p[1] = s->client_version & 0xff;
1458 RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */
1462 s->session->master_key_length=
1463 s->method->ssl3_enc->generate_master_secret(s,
1464 s->session->master_key,
1470 #ifndef OPENSSL_NO_DH
1471 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1476 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1478 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1488 if (n == 0L) /* the parameters are in the cert */
1490 al=SSL_AD_HANDSHAKE_FAILURE;
1491 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1496 if (s->s3->tmp.dh == NULL)
1498 al=SSL_AD_HANDSHAKE_FAILURE;
1499 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1503 dh_srvr=s->s3->tmp.dh;
1506 pub=BN_bin2bn(p,i,NULL);
1509 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1513 i=DH_compute_key(p,pub,dh_srvr);
1517 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1521 DH_free(s->s3->tmp.dh);
1526 s->session->master_key_length=
1527 s->method->ssl3_enc->generate_master_secret(s,
1528 s->session->master_key,p,i);
1533 #ifndef OPENSSL_NO_KRB5
1536 krb5_error_code krb5rc;
1537 krb5_data enc_ticket;
1538 krb5_data authenticator;
1540 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1541 EVP_CIPHER_CTX ciph_ctx;
1542 EVP_CIPHER *enc = NULL;
1543 unsigned char iv[EVP_MAX_IV_LENGTH];
1544 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
1545 + EVP_MAX_IV_LENGTH + 1];
1546 int padl, outl = sizeof(pms);
1547 krb5_timestamp authtime = 0;
1548 krb5_ticket_times ttimes;
1550 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
1553 enc_ticket.length = i;
1554 enc_ticket.data = p;
1555 p+=enc_ticket.length;
1558 authenticator.length = i;
1559 authenticator.data = p;
1560 p+=authenticator.length;
1567 if ((unsigned long)n != enc_ticket.length + authenticator.length +
1570 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1571 SSL_R_DATA_LENGTH_TOO_LONG);
1575 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
1579 printf("kssl_sget_tkt rtn %d [%d]\n",
1580 krb5rc, kssl_err.reason);
1582 printf("kssl_err text= %s\n", kssl_err.text);
1583 #endif /* KSSL_DEBUG */
1584 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1589 /* Note: no authenticator is not considered an error,
1590 ** but will return authtime == 0.
1592 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
1593 &authtime, &kssl_err)) != 0)
1596 printf("kssl_check_authent rtn %d [%d]\n",
1597 krb5rc, kssl_err.reason);
1599 printf("kssl_err text= %s\n", kssl_err.text);
1600 #endif /* KSSL_DEBUG */
1601 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1606 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
1608 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, krb5rc);
1613 kssl_ctx_show(kssl_ctx);
1614 #endif /* KSSL_DEBUG */
1616 enc = kssl_map_enc(kssl_ctx->enctype);
1620 memset(iv, 0, EVP_MAX_IV_LENGTH); /* per RFC 1510 */
1622 if (!EVP_DecryptInit(&ciph_ctx,enc,kssl_ctx->key,iv))
1624 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1625 SSL_R_DECRYPTION_FAILED);
1628 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
1629 enc_pms.data, enc_pms.length))
1631 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1632 SSL_R_DECRYPTION_FAILED);
1635 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
1637 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1638 SSL_R_DATA_LENGTH_TOO_LONG);
1641 if (!EVP_DecryptFinal(&ciph_ctx,&(pms[outl]),&padl))
1643 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1644 SSL_R_DECRYPTION_FAILED);
1648 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
1650 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1651 SSL_R_DATA_LENGTH_TOO_LONG);
1654 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1656 s->session->master_key_length=
1657 s->method->ssl3_enc->generate_master_secret(s,
1658 s->session->master_key, pms, outl);
1660 if (kssl_ctx->client_princ)
1662 int len = strlen(kssl_ctx->client_princ);
1663 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
1665 s->session->krb5_client_princ_len = len;
1666 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
1671 /* Was doing kssl_ctx_free() here,
1672 ** but it caused problems for apache.
1673 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
1674 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
1678 #endif /* OPENSSL_NO_KRB5 */
1680 al=SSL_AD_HANDSHAKE_FAILURE;
1681 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1682 SSL_R_UNKNOWN_CIPHER_TYPE);
1688 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1689 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA)
1695 static int ssl3_get_cert_verify(SSL *s)
1697 EVP_PKEY *pkey=NULL;
1704 n=ssl3_get_message(s,
1705 SSL3_ST_SR_CERT_VRFY_A,
1706 SSL3_ST_SR_CERT_VRFY_B,
1711 if (!ok) return((int)n);
1713 if (s->session->peer != NULL)
1715 peer=s->session->peer;
1716 pkey=X509_get_pubkey(peer);
1717 type=X509_certificate_type(peer,pkey);
1725 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
1727 s->s3->tmp.reuse_message=1;
1728 if ((peer != NULL) && (type | EVP_PKT_SIGN))
1730 al=SSL_AD_UNEXPECTED_MESSAGE;
1731 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
1740 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
1741 al=SSL_AD_UNEXPECTED_MESSAGE;
1745 if (!(type & EVP_PKT_SIGN))
1747 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
1748 al=SSL_AD_ILLEGAL_PARAMETER;
1752 if (s->s3->change_cipher_spec)
1754 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
1755 al=SSL_AD_UNEXPECTED_MESSAGE;
1759 /* we now have a signature that we need to verify */
1760 p=(unsigned char *)s->init_buf->data;
1765 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
1766 al=SSL_AD_DECODE_ERROR;
1770 j=EVP_PKEY_size(pkey);
1771 if ((i > j) || (n > j) || (n <= 0))
1773 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
1774 al=SSL_AD_DECODE_ERROR;
1778 #ifndef OPENSSL_NO_RSA
1779 if (pkey->type == EVP_PKEY_RSA)
1781 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
1782 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
1786 al=SSL_AD_DECRYPT_ERROR;
1787 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
1792 al=SSL_AD_DECRYPT_ERROR;
1793 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
1799 #ifndef OPENSSL_NO_DSA
1800 if (pkey->type == EVP_PKEY_DSA)
1802 j=DSA_verify(pkey->save_type,
1803 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
1804 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
1808 al=SSL_AD_DECRYPT_ERROR;
1809 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
1816 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
1817 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
1826 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1829 EVP_PKEY_free(pkey);
1833 static int ssl3_get_client_certificate(SSL *s)
1835 int i,ok,al,ret= -1;
1837 unsigned long l,nc,llen,n;
1838 unsigned char *p,*d,*q;
1839 STACK_OF(X509) *sk=NULL;
1841 n=ssl3_get_message(s,
1848 if (!ok) return((int)n);
1850 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
1852 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
1853 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1855 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1856 al=SSL_AD_HANDSHAKE_FAILURE;
1859 /* If tls asked for a client cert, the client must return a 0 list */
1860 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
1862 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
1863 al=SSL_AD_UNEXPECTED_MESSAGE;
1866 s->s3->tmp.reuse_message=1;
1870 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1872 al=SSL_AD_UNEXPECTED_MESSAGE;
1873 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
1876 d=p=(unsigned char *)s->init_buf->data;
1878 if ((sk=sk_X509_new_null()) == NULL)
1880 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1887 al=SSL_AD_DECODE_ERROR;
1888 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1891 for (nc=0; nc<llen; )
1894 if ((l+nc+3) > llen)
1896 al=SSL_AD_DECODE_ERROR;
1897 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1902 x=d2i_X509(NULL,&p,l);
1905 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
1910 al=SSL_AD_DECODE_ERROR;
1911 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1914 if (!sk_X509_push(sk,x))
1916 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1923 if (sk_X509_num(sk) <= 0)
1925 /* TLS does not mind 0 certs returned */
1926 if (s->version == SSL3_VERSION)
1928 al=SSL_AD_HANDSHAKE_FAILURE;
1929 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
1932 /* Fail for TLS only if we required a certificate */
1933 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
1934 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1936 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1937 al=SSL_AD_HANDSHAKE_FAILURE;
1943 i=ssl_verify_cert_chain(s,sk);
1946 al=ssl_verify_alarm_type(s->verify_result);
1947 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
1952 if (s->session->peer != NULL) /* This should not be needed */
1953 X509_free(s->session->peer);
1954 s->session->peer=sk_X509_shift(sk);
1955 s->session->verify_result = s->verify_result;
1957 /* With the current implementation, sess_cert will always be NULL
1958 * when we arrive here. */
1959 if (s->session->sess_cert == NULL)
1961 s->session->sess_cert = ssl_sess_cert_new();
1962 if (s->session->sess_cert == NULL)
1964 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1968 if (s->session->sess_cert->cert_chain != NULL)
1969 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
1970 s->session->sess_cert->cert_chain=sk;
1971 /* Inconsistency alert: cert_chain does *not* include the
1972 * peer's own certificate, while we do include it in s3_clnt.c */
1980 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1983 if (x != NULL) X509_free(x);
1984 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
1988 int ssl3_send_server_certificate(SSL *s)
1993 if (s->state == SSL3_ST_SW_CERT_A)
1995 x=ssl_get_server_send_cert(s);
1997 /* VRS: allow null cert if auth == KRB5 */
1998 (s->s3->tmp.new_cipher->algorithms
1999 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
2000 != (SSL_aKRB5|SSL_kKRB5))
2002 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
2006 l=ssl3_output_cert_chain(s,x);
2007 s->state=SSL3_ST_SW_CERT_B;
2012 /* SSL3_ST_SW_CERT_B */
2013 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));