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 "ssl_locl.h"
118 #include "kssl_lcl.h"
119 #include <openssl/buffer.h>
120 #include <openssl/rand.h>
121 #include <openssl/objects.h>
122 #include <openssl/evp.h>
123 #include <openssl/x509.h>
124 #include <openssl/krb5_asn.h>
125 #include <openssl/md5.h>
126 #include "cryptlib.h"
128 static SSL_METHOD *ssl3_get_server_method(int ver);
129 static int ssl3_get_client_hello(SSL *s);
130 static int ssl3_check_client_hello(SSL *s);
131 static int ssl3_send_server_hello(SSL *s);
132 static int ssl3_send_server_key_exchange(SSL *s);
133 static int ssl3_send_certificate_request(SSL *s);
134 static int ssl3_send_server_done(SSL *s);
135 static int ssl3_get_client_key_exchange(SSL *s);
136 static int ssl3_get_client_certificate(SSL *s);
137 static int ssl3_get_cert_verify(SSL *s);
138 static int ssl3_send_hello_request(SSL *s);
140 static SSL_METHOD *ssl3_get_server_method(int ver)
142 if (ver == SSL3_VERSION)
143 return(SSLv3_server_method());
148 SSL_METHOD *SSLv3_server_method(void)
151 static SSL_METHOD SSLv3_server_data;
155 CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
159 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
161 SSLv3_server_data.ssl_accept=ssl3_accept;
162 SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
166 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
168 return(&SSLv3_server_data);
171 int ssl3_accept(SSL *s)
174 unsigned long l,Time=time(NULL);
175 void (*cb)(const SSL *ssl,int type,int val)=NULL;
178 int new_state,state,skip=0;
180 RAND_add(&Time,sizeof(Time),0);
184 if (s->info_callback != NULL)
186 else if (s->ctx->info_callback != NULL)
187 cb=s->ctx->info_callback;
189 /* init things to blank */
191 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
195 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
205 case SSL_ST_RENEGOTIATE:
207 /* s->state=SSL_ST_ACCEPT; */
211 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
212 case SSL_ST_OK|SSL_ST_ACCEPT:
215 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
217 if ((s->version>>8) != 3)
219 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
222 s->type=SSL_ST_ACCEPT;
224 if (s->init_buf == NULL)
226 if ((buf=BUF_MEM_new()) == NULL)
231 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
239 if (!ssl3_setup_buffers(s))
247 if (s->state != SSL_ST_RENEGOTIATE)
249 /* Ok, we now need to push on a buffering BIO so that
250 * the output is sent in a way that TCP likes :-)
252 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
254 ssl3_init_finished_mac(s);
255 s->state=SSL3_ST_SR_CLNT_HELLO_A;
256 s->ctx->stats.sess_accept++;
260 /* s->state == SSL_ST_RENEGOTIATE,
261 * we will just send a HelloRequest */
262 s->ctx->stats.sess_accept_renegotiate++;
263 s->state=SSL3_ST_SW_HELLO_REQ_A;
267 case SSL3_ST_SW_HELLO_REQ_A:
268 case SSL3_ST_SW_HELLO_REQ_B:
271 ret=ssl3_send_hello_request(s);
272 if (ret <= 0) goto end;
273 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
274 s->state=SSL3_ST_SW_FLUSH;
277 ssl3_init_finished_mac(s);
280 case SSL3_ST_SW_HELLO_REQ_C:
284 case SSL3_ST_SR_CLNT_HELLO_A:
285 case SSL3_ST_SR_CLNT_HELLO_B:
286 case SSL3_ST_SR_CLNT_HELLO_C:
289 ret=ssl3_get_client_hello(s);
290 if (ret <= 0) goto end;
292 s->state=SSL3_ST_SW_SRVR_HELLO_A;
296 case SSL3_ST_SW_SRVR_HELLO_A:
297 case SSL3_ST_SW_SRVR_HELLO_B:
298 ret=ssl3_send_server_hello(s);
299 if (ret <= 0) goto end;
302 s->state=SSL3_ST_SW_CHANGE_A;
304 s->state=SSL3_ST_SW_CERT_A;
308 case SSL3_ST_SW_CERT_A:
309 case SSL3_ST_SW_CERT_B:
310 /* Check if it is anon DH */
311 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
313 ret=ssl3_send_server_certificate(s);
314 if (ret <= 0) goto end;
318 s->state=SSL3_ST_SW_KEY_EXCH_A;
322 case SSL3_ST_SW_KEY_EXCH_A:
323 case SSL3_ST_SW_KEY_EXCH_B:
324 l=s->s3->tmp.new_cipher->algorithms;
326 /* clear this, it may get reset by
327 * send_server_key_exchange */
328 if ((s->options & SSL_OP_EPHEMERAL_RSA)
329 #ifndef OPENSSL_NO_KRB5
331 #endif /* OPENSSL_NO_KRB5 */
333 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
334 * even when forbidden by protocol specs
335 * (handshake may fail as clients are not required to
336 * be able to handle this) */
337 s->s3->tmp.use_rsa_tmp=1;
339 s->s3->tmp.use_rsa_tmp=0;
341 /* only send if a DH key exchange, fortezza or
342 * RSA but we have a sign only certificate */
343 if (s->s3->tmp.use_rsa_tmp
344 || (l & (SSL_DH|SSL_kFZA))
346 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
347 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
348 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
354 ret=ssl3_send_server_key_exchange(s);
355 if (ret <= 0) goto end;
360 s->state=SSL3_ST_SW_CERT_REQ_A;
364 case SSL3_ST_SW_CERT_REQ_A:
365 case SSL3_ST_SW_CERT_REQ_B:
366 if (/* don't request cert unless asked for it: */
367 !(s->verify_mode & SSL_VERIFY_PEER) ||
368 /* if SSL_VERIFY_CLIENT_ONCE is set,
369 * don't request cert during re-negotiation: */
370 ((s->session->peer != NULL) &&
371 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
372 /* never request cert in anonymous ciphersuites
373 * (see section "Certificate request" in SSL 3 drafts
374 * and in RFC 2246): */
375 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
376 /* ... except when the application insists on verification
377 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
378 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
379 /* never request cert in Kerberos ciphersuites */
380 (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
382 /* no cert request */
384 s->s3->tmp.cert_request=0;
385 s->state=SSL3_ST_SW_SRVR_DONE_A;
389 s->s3->tmp.cert_request=1;
390 ret=ssl3_send_certificate_request(s);
391 if (ret <= 0) goto end;
392 #ifndef NETSCAPE_HANG_BUG
393 s->state=SSL3_ST_SW_SRVR_DONE_A;
395 s->state=SSL3_ST_SW_FLUSH;
396 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
402 case SSL3_ST_SW_SRVR_DONE_A:
403 case SSL3_ST_SW_SRVR_DONE_B:
404 ret=ssl3_send_server_done(s);
405 if (ret <= 0) goto end;
406 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
407 s->state=SSL3_ST_SW_FLUSH;
411 case SSL3_ST_SW_FLUSH:
412 /* number of bytes to be flushed */
413 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
416 s->rwstate=SSL_WRITING;
417 num1=BIO_flush(s->wbio);
418 if (num1 <= 0) { ret= -1; goto end; }
419 s->rwstate=SSL_NOTHING;
422 s->state=s->s3->tmp.next_state;
425 case SSL3_ST_SR_CERT_A:
426 case SSL3_ST_SR_CERT_B:
427 /* Check for second client hello (MS SGC) */
428 ret = ssl3_check_client_hello(s);
432 s->state = SSL3_ST_SR_CLNT_HELLO_C;
434 /* could be sent for a DH cert, even if we
435 * have not asked for it :-) */
436 ret=ssl3_get_client_certificate(s);
437 if (ret <= 0) goto end;
439 s->state=SSL3_ST_SR_KEY_EXCH_A;
443 case SSL3_ST_SR_KEY_EXCH_A:
444 case SSL3_ST_SR_KEY_EXCH_B:
445 ret=ssl3_get_client_key_exchange(s);
446 if (ret <= 0) goto end;
447 s->state=SSL3_ST_SR_CERT_VRFY_A;
450 /* We need to get hashes here so if there is
451 * a client cert, it can be verified */
452 s->method->ssl3_enc->cert_verify_mac(s,
453 &(s->s3->finish_dgst1),
454 &(s->s3->tmp.cert_verify_md[0]));
455 s->method->ssl3_enc->cert_verify_mac(s,
456 &(s->s3->finish_dgst2),
457 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
461 case SSL3_ST_SR_CERT_VRFY_A:
462 case SSL3_ST_SR_CERT_VRFY_B:
464 /* we should decide if we expected this one */
465 ret=ssl3_get_cert_verify(s);
466 if (ret <= 0) goto end;
468 s->state=SSL3_ST_SR_FINISHED_A;
472 case SSL3_ST_SR_FINISHED_A:
473 case SSL3_ST_SR_FINISHED_B:
474 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
475 SSL3_ST_SR_FINISHED_B);
476 if (ret <= 0) goto end;
480 s->state=SSL3_ST_SW_CHANGE_A;
484 case SSL3_ST_SW_CHANGE_A:
485 case SSL3_ST_SW_CHANGE_B:
487 s->session->cipher=s->s3->tmp.new_cipher;
488 if (!s->method->ssl3_enc->setup_key_block(s))
489 { ret= -1; goto end; }
491 ret=ssl3_send_change_cipher_spec(s,
492 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
494 if (ret <= 0) goto end;
495 s->state=SSL3_ST_SW_FINISHED_A;
498 if (!s->method->ssl3_enc->change_cipher_state(s,
499 SSL3_CHANGE_CIPHER_SERVER_WRITE))
507 case SSL3_ST_SW_FINISHED_A:
508 case SSL3_ST_SW_FINISHED_B:
509 ret=ssl3_send_finished(s,
510 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
511 s->method->ssl3_enc->server_finished_label,
512 s->method->ssl3_enc->server_finished_label_len);
513 if (ret <= 0) goto end;
514 s->state=SSL3_ST_SW_FLUSH;
516 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
518 s->s3->tmp.next_state=SSL_ST_OK;
523 /* clean a few things up */
524 ssl3_cleanup_key_block(s);
526 BUF_MEM_free(s->init_buf);
529 /* remove buffering on output */
530 ssl_free_wbio_buffer(s);
534 if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
536 /* actually not necessarily a 'new' session unless
537 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
541 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
543 s->ctx->stats.sess_accept_good++;
545 s->handshake_func=ssl3_accept;
547 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
555 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
561 if (!s->s3->tmp.reuse_message && !skip)
565 if ((ret=BIO_flush(s->wbio)) <= 0)
570 if ((cb != NULL) && (s->state != state))
574 cb(s,SSL_CB_ACCEPT_LOOP,1);
581 /* BIO_flush(s->wbio); */
585 cb(s,SSL_CB_ACCEPT_EXIT,ret);
589 static int ssl3_send_hello_request(SSL *s)
593 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
595 p=(unsigned char *)s->init_buf->data;
596 *(p++)=SSL3_MT_HELLO_REQUEST;
601 s->state=SSL3_ST_SW_HELLO_REQ_B;
602 /* number of bytes to write */
607 /* SSL3_ST_SW_HELLO_REQ_B */
608 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
611 static int ssl3_check_client_hello(SSL *s)
616 /* this function is called when we really expect a Certificate message,
617 * so permit appropriate message length */
618 n=ssl3_get_message(s,
624 if (!ok) return((int)n);
625 s->s3->tmp.reuse_message = 1;
626 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
628 /* Throw away what we have done so far in the current handshake,
629 * which will now be aborted. (A full SSL_clear would be too much.)
630 * I hope that tmp.dh is the only thing that may need to be cleared
631 * when a handshake is not completed ... */
632 #ifndef OPENSSL_NO_DH
633 if (s->s3->tmp.dh != NULL)
635 DH_free(s->s3->tmp.dh);
636 s->s3->tmp.dh = NULL;
644 static int ssl3_get_client_hello(SSL *s)
646 int i,j,ok,al,ret= -1;
649 unsigned char *p,*d,*q;
652 STACK_OF(SSL_CIPHER) *ciphers=NULL;
654 /* We do this so that we will respond with our native type.
655 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
656 * This down switching should be handled by a different method.
657 * If we are SSLv3, we will respond with SSLv3, even if prompted with
660 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
663 s->state=SSL3_ST_SR_CLNT_HELLO_B;
665 n=ssl3_get_message(s,
666 SSL3_ST_SR_CLNT_HELLO_B,
667 SSL3_ST_SR_CLNT_HELLO_C,
668 SSL3_MT_CLIENT_HELLO,
669 SSL3_RT_MAX_PLAIN_LENGTH,
672 if (!ok) return((int)n);
673 d=p=(unsigned char *)s->init_msg;
675 /* use version from inside client hello, not from record header
676 * (may differ: see RFC 2246, Appendix E, second paragraph) */
677 s->client_version=(((int)p[0])<<8)|(int)p[1];
680 if (s->client_version < s->version)
682 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
683 if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
685 /* similar to ssl3_get_record, send alert using remote version number */
686 s->version = s->client_version;
688 al = SSL_AD_PROTOCOL_VERSION;
692 /* load the client random */
693 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
696 /* get the session-id */
700 /* Versions before 0.9.7 always allow session reuse during renegotiation
701 * (i.e. when s->new_session is true), option
702 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
703 * Maybe this optional behaviour should always have been the default,
704 * but we cannot safely change the default behaviour (or new applications
705 * might be written that become totally unsecure when compiled with
706 * an earlier library version)
708 if (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
710 if (!ssl_get_new_session(s,1))
715 i=ssl_get_prev_session(s,p,j);
717 { /* previous session */
724 if (!ssl_get_new_session(s,1))
731 if ((i == 0) && (j != 0))
733 /* we need a cipher if we are not resuming a session */
734 al=SSL_AD_ILLEGAL_PARAMETER;
735 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
740 /* not enough data */
741 al=SSL_AD_DECODE_ERROR;
742 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
745 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
752 /* If it is a hit, check that the cipher is in the list */
753 if ((s->hit) && (i > 0))
756 id=s->session->cipher->id;
759 printf("client sent %d ciphers\n",sk_num(ciphers));
761 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
763 c=sk_SSL_CIPHER_value(ciphers,i);
765 printf("client [%2d of %2d]:%s\n",
766 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
776 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
778 /* Very bad for multi-threading.... */
779 s->session->cipher=sk_SSL_CIPHER_value(ciphers,
784 /* we need to have the cipher in the cipher
785 * list if we are asked to reuse it */
786 al=SSL_AD_ILLEGAL_PARAMETER;
787 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
797 /* not enough data */
798 al=SSL_AD_DECODE_ERROR;
799 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
805 if (p[j] == 0) break;
812 al=SSL_AD_DECODE_ERROR;
813 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
817 /* Worst case, we will use the NULL compression, but if we have other
818 * options, we will now look for them. We have i-1 compression
819 * algorithms from the client, starting at q. */
820 s->s3->tmp.new_compression=NULL;
821 if (s->ctx->comp_methods != NULL)
822 { /* See if we have a match */
825 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
828 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
841 s->s3->tmp.new_compression=comp;
846 /* TLS does not mind if there is extra stuff */
847 if (s->version == SSL3_VERSION)
851 /* wrong number of bytes,
852 * there could be more to follow */
853 al=SSL_AD_DECODE_ERROR;
854 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
859 /* Given s->session->ciphers and SSL_get_ciphers, we must
864 s->session->compress_meth=(comp == NULL)?0:comp->id;
865 if (s->session->ciphers != NULL)
866 sk_SSL_CIPHER_free(s->session->ciphers);
867 s->session->ciphers=ciphers;
870 al=SSL_AD_ILLEGAL_PARAMETER;
871 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
875 c=ssl3_choose_cipher(s,s->session->ciphers,
880 al=SSL_AD_HANDSHAKE_FAILURE;
881 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
884 s->s3->tmp.new_cipher=c;
888 /* Session-id reuse */
889 #ifdef REUSE_CIPHER_BUG
890 STACK_OF(SSL_CIPHER) *sk;
894 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
896 sk=s->session->ciphers;
897 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
899 c=sk_SSL_CIPHER_value(sk,i);
900 if (c->algorithms & SSL_eNULL)
902 if (SSL_C_IS_EXPORT(c))
906 s->s3->tmp.new_cipher=nc;
908 s->s3->tmp.new_cipher=ec;
910 s->s3->tmp.new_cipher=s->session->cipher;
914 s->s3->tmp.new_cipher=s->session->cipher;
917 /* we now have the following setup.
919 * cipher_list - our prefered list of ciphers
920 * ciphers - the clients prefered list of ciphers
921 * compression - basically ignored right now
922 * ssl version is set - sslv3
923 * s->session - The ssl session has been setup.
924 * s->hit - session reuse flag
925 * s->tmp.new_cipher - the new cipher to use.
932 ssl3_send_alert(s,SSL3_AL_FATAL,al);
935 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
939 static int ssl3_send_server_hello(SSL *s)
944 unsigned long l,Time;
946 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
948 buf=(unsigned char *)s->init_buf->data;
949 p=s->s3->server_random;
950 Time=time(NULL); /* Time */
952 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
953 /* Do the message type and length last */
956 *(p++)=s->version>>8;
957 *(p++)=s->version&0xff;
960 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
963 /* now in theory we have 3 options to sending back the
964 * session id. If it is a re-use, we send back the
965 * old session-id, if it is a new session, we send
966 * back the new session-id or we send back a 0 length
967 * session-id if we want it to be single use.
968 * Currently I will not implement the '0' length session-id
969 * 12-Jan-98 - I'll now support the '0' length stuff.
971 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
972 s->session->session_id_length=0;
974 sl=s->session->session_id_length;
975 if (sl > sizeof s->session->session_id)
977 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
981 memcpy(p,s->session->session_id,sl);
985 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
988 /* put the compression method */
989 if (s->s3->tmp.new_compression == NULL)
992 *(p++)=s->s3->tmp.new_compression->id;
997 *(d++)=SSL3_MT_SERVER_HELLO;
1000 s->state=SSL3_ST_CW_CLNT_HELLO_B;
1001 /* number of bytes to write */
1006 /* SSL3_ST_CW_CLNT_HELLO_B */
1007 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1010 static int ssl3_send_server_done(SSL *s)
1014 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1016 p=(unsigned char *)s->init_buf->data;
1019 *(p++)=SSL3_MT_SERVER_DONE;
1024 s->state=SSL3_ST_SW_SRVR_DONE_B;
1025 /* number of bytes to write */
1030 /* SSL3_ST_CW_CLNT_HELLO_B */
1031 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1034 static int ssl3_send_server_key_exchange(SSL *s)
1036 #ifndef OPENSSL_NO_RSA
1040 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1043 #ifndef OPENSSL_NO_DH
1047 unsigned char *p,*d;
1057 EVP_MD_CTX_init(&md_ctx);
1058 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1060 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1065 r[0]=r[1]=r[2]=r[3]=NULL;
1067 #ifndef OPENSSL_NO_RSA
1068 if (type & SSL_kRSA)
1071 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1073 rsa=s->cert->rsa_tmp_cb(s,
1074 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1075 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1078 al=SSL_AD_HANDSHAKE_FAILURE;
1079 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1087 al=SSL_AD_HANDSHAKE_FAILURE;
1088 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1093 s->s3->tmp.use_rsa_tmp=1;
1097 #ifndef OPENSSL_NO_DH
1098 if (type & SSL_kEDH)
1101 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1102 dhp=s->cert->dh_tmp_cb(s,
1103 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1104 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1107 al=SSL_AD_HANDSHAKE_FAILURE;
1108 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1112 if (s->s3->tmp.dh != NULL)
1115 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1119 if ((dh=DHparams_dup(dhp)) == NULL)
1121 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1126 if ((dhp->pub_key == NULL ||
1127 dhp->priv_key == NULL ||
1128 (s->options & SSL_OP_SINGLE_DH_USE)))
1130 if(!DH_generate_key(dh))
1132 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1139 dh->pub_key=BN_dup(dhp->pub_key);
1140 dh->priv_key=BN_dup(dhp->priv_key);
1141 if ((dh->pub_key == NULL) ||
1142 (dh->priv_key == NULL))
1144 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1155 al=SSL_AD_HANDSHAKE_FAILURE;
1156 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1159 for (i=0; r[i] != NULL; i++)
1161 nr[i]=BN_num_bytes(r[i]);
1165 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1167 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1170 al=SSL_AD_DECODE_ERROR;
1173 kn=EVP_PKEY_size(pkey);
1181 if (!BUF_MEM_grow(buf,n+4+kn))
1183 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1186 d=(unsigned char *)s->init_buf->data;
1189 for (i=0; r[i] != NULL; i++)
1199 /* n is the length of the params, they start at &(d[4])
1200 * and p points to the space at the end. */
1201 #ifndef OPENSSL_NO_RSA
1202 if (pkey->type == EVP_PKEY_RSA)
1206 for (num=2; num > 0; num--)
1208 EVP_DigestInit_ex(&md_ctx,(num == 2)
1209 ?s->ctx->md5:s->ctx->sha1, NULL);
1210 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1211 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1212 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1213 EVP_DigestFinal_ex(&md_ctx,q,
1214 (unsigned int *)&i);
1218 if (RSA_sign(NID_md5_sha1, md_buf, j,
1219 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1221 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1229 #if !defined(OPENSSL_NO_DSA)
1230 if (pkey->type == EVP_PKEY_DSA)
1233 EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
1234 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1235 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1236 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1237 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1238 (unsigned int *)&i,pkey))
1240 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1249 /* Is this error check actually needed? */
1250 al=SSL_AD_HANDSHAKE_FAILURE;
1251 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1256 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1259 /* we should now have things packed up, so lets send
1265 s->state = SSL3_ST_SW_KEY_EXCH_B;
1266 EVP_MD_CTX_cleanup(&md_ctx);
1267 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1269 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1271 EVP_MD_CTX_cleanup(&md_ctx);
1275 static int ssl3_send_certificate_request(SSL *s)
1277 unsigned char *p,*d;
1279 STACK_OF(X509_NAME) *sk=NULL;
1283 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1287 d=p=(unsigned char *)&(buf->data[4]);
1289 /* get the list of acceptable cert types */
1291 n=ssl3_get_req_cert_type(s,p);
1300 sk=SSL_get_client_CA_list(s);
1304 for (i=0; i<sk_X509_NAME_num(sk); i++)
1306 name=sk_X509_NAME_value(sk,i);
1307 j=i2d_X509_NAME(name,NULL);
1308 if (!BUF_MEM_grow(buf,4+n+j+2))
1310 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1313 p=(unsigned char *)&(buf->data[4+n]);
1314 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1317 i2d_X509_NAME(name,&p);
1324 i2d_X509_NAME(name,&p);
1325 j-=2; s2n(j,d); j+=2;
1331 /* else no CA names */
1332 p=(unsigned char *)&(buf->data[4+off]);
1335 d=(unsigned char *)buf->data;
1336 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1339 /* we should now have things packed up, so lets send
1344 #ifdef NETSCAPE_HANG_BUG
1345 p=(unsigned char *)s->init_buf->data + s->init_num;
1348 *(p++)=SSL3_MT_SERVER_DONE;
1357 /* SSL3_ST_SW_CERT_REQ_B */
1358 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1363 static int ssl3_get_client_key_exchange(SSL *s)
1369 #ifndef OPENSSL_NO_RSA
1371 EVP_PKEY *pkey=NULL;
1373 #ifndef OPENSSL_NO_DH
1377 #ifndef OPENSSL_NO_KRB5
1379 #endif /* OPENSSL_NO_KRB5 */
1381 n=ssl3_get_message(s,
1382 SSL3_ST_SR_KEY_EXCH_A,
1383 SSL3_ST_SR_KEY_EXCH_B,
1384 SSL3_MT_CLIENT_KEY_EXCHANGE,
1388 if (!ok) return((int)n);
1389 p=(unsigned char *)s->init_msg;
1391 l=s->s3->tmp.new_cipher->algorithms;
1393 #ifndef OPENSSL_NO_RSA
1396 /* FIX THIS UP EAY EAY EAY EAY */
1397 if (s->s3->tmp.use_rsa_tmp)
1399 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1400 rsa=s->cert->rsa_tmp;
1401 /* Don't do a callback because rsa_tmp should
1402 * be sent already */
1405 al=SSL_AD_HANDSHAKE_FAILURE;
1406 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1413 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1414 if ( (pkey == NULL) ||
1415 (pkey->type != EVP_PKEY_RSA) ||
1416 (pkey->pkey.rsa == NULL))
1418 al=SSL_AD_HANDSHAKE_FAILURE;
1419 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1426 if (s->version > SSL3_VERSION)
1431 if (!(s->options & SSL_OP_TLS_D5_BUG))
1433 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1443 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1447 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1449 al=SSL_AD_DECODE_ERROR;
1450 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1453 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
1455 /* The premaster secret must contain the same version number as the
1456 * ClientHello to detect version rollback attacks (strangely, the
1457 * protocol does not offer such protection for DH ciphersuites).
1458 * However, buggy clients exist that send the negotiated protocol
1459 * version instead if the server does not support the requested
1461 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1462 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1463 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
1465 al=SSL_AD_DECODE_ERROR;
1466 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1476 /* Some decryption failure -- use random value instead as countermeasure
1477 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1478 * (see RFC 2246, section 7.4.7.1).
1479 * But note that due to length and protocol version checking, the
1480 * attack is impractical anyway (see section 5 in D. Bleichenbacher:
1481 * "Chosen Ciphertext Attacks Against Protocols Based on the RSA
1482 * Encryption Standard PKCS #1", CRYPTO '98, LNCS 1462, pp. 1-12).
1485 i = SSL_MAX_MASTER_KEY_LENGTH;
1486 p[0] = s->client_version >> 8;
1487 p[1] = s->client_version & 0xff;
1488 RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */
1492 s->session->master_key_length=
1493 s->method->ssl3_enc->generate_master_secret(s,
1494 s->session->master_key,
1500 #ifndef OPENSSL_NO_DH
1501 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1506 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1508 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1518 if (n == 0L) /* the parameters are in the cert */
1520 al=SSL_AD_HANDSHAKE_FAILURE;
1521 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1526 if (s->s3->tmp.dh == NULL)
1528 al=SSL_AD_HANDSHAKE_FAILURE;
1529 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1533 dh_srvr=s->s3->tmp.dh;
1536 pub=BN_bin2bn(p,i,NULL);
1539 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1543 i=DH_compute_key(p,pub,dh_srvr);
1547 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1551 DH_free(s->s3->tmp.dh);
1556 s->session->master_key_length=
1557 s->method->ssl3_enc->generate_master_secret(s,
1558 s->session->master_key,p,i);
1563 #ifndef OPENSSL_NO_KRB5
1566 krb5_error_code krb5rc;
1567 krb5_data enc_ticket;
1568 krb5_data authenticator;
1570 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1571 EVP_CIPHER_CTX ciph_ctx;
1572 EVP_CIPHER *enc = NULL;
1573 unsigned char iv[EVP_MAX_IV_LENGTH];
1574 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
1575 + EVP_MAX_BLOCK_LENGTH];
1577 krb5_timestamp authtime = 0;
1578 krb5_ticket_times ttimes;
1580 EVP_CIPHER_CTX_init(&ciph_ctx);
1582 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
1585 enc_ticket.length = i;
1586 enc_ticket.data = (char *)p;
1587 p+=enc_ticket.length;
1590 authenticator.length = i;
1591 authenticator.data = (char *)p;
1592 p+=authenticator.length;
1596 enc_pms.data = (char *)p;
1599 /* Note that the length is checked again below,
1602 if(enc_pms.length > sizeof pms)
1604 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1605 SSL_R_DATA_LENGTH_TOO_LONG);
1609 if (n != enc_ticket.length + authenticator.length +
1612 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1613 SSL_R_DATA_LENGTH_TOO_LONG);
1617 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
1621 printf("kssl_sget_tkt rtn %d [%d]\n",
1622 krb5rc, kssl_err.reason);
1624 printf("kssl_err text= %s\n", kssl_err.text);
1625 #endif /* KSSL_DEBUG */
1626 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1631 /* Note: no authenticator is not considered an error,
1632 ** but will return authtime == 0.
1634 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
1635 &authtime, &kssl_err)) != 0)
1638 printf("kssl_check_authent rtn %d [%d]\n",
1639 krb5rc, kssl_err.reason);
1641 printf("kssl_err text= %s\n", kssl_err.text);
1642 #endif /* KSSL_DEBUG */
1643 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1648 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
1650 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, krb5rc);
1655 kssl_ctx_show(kssl_ctx);
1656 #endif /* KSSL_DEBUG */
1658 enc = kssl_map_enc(kssl_ctx->enctype);
1662 memset(iv, 0, EVP_MAX_IV_LENGTH); /* per RFC 1510 */
1664 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
1666 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1667 SSL_R_DECRYPTION_FAILED);
1670 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
1671 (unsigned char *)enc_pms.data, enc_pms.length))
1673 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1674 SSL_R_DECRYPTION_FAILED);
1677 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
1679 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1680 SSL_R_DATA_LENGTH_TOO_LONG);
1683 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
1685 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1686 SSL_R_DECRYPTION_FAILED);
1690 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
1692 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1693 SSL_R_DATA_LENGTH_TOO_LONG);
1696 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1698 s->session->master_key_length=
1699 s->method->ssl3_enc->generate_master_secret(s,
1700 s->session->master_key, pms, outl);
1702 if (kssl_ctx->client_princ)
1704 int len = strlen(kssl_ctx->client_princ);
1705 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
1707 s->session->krb5_client_princ_len = len;
1708 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
1713 /* Was doing kssl_ctx_free() here,
1714 ** but it caused problems for apache.
1715 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
1716 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
1720 #endif /* OPENSSL_NO_KRB5 */
1722 al=SSL_AD_HANDSHAKE_FAILURE;
1723 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1724 SSL_R_UNKNOWN_CIPHER_TYPE);
1730 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1731 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA)
1737 static int ssl3_get_cert_verify(SSL *s)
1739 EVP_PKEY *pkey=NULL;
1746 n=ssl3_get_message(s,
1747 SSL3_ST_SR_CERT_VRFY_A,
1748 SSL3_ST_SR_CERT_VRFY_B,
1753 if (!ok) return((int)n);
1755 if (s->session->peer != NULL)
1757 peer=s->session->peer;
1758 pkey=X509_get_pubkey(peer);
1759 type=X509_certificate_type(peer,pkey);
1767 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
1769 s->s3->tmp.reuse_message=1;
1770 if ((peer != NULL) && (type | EVP_PKT_SIGN))
1772 al=SSL_AD_UNEXPECTED_MESSAGE;
1773 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
1782 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
1783 al=SSL_AD_UNEXPECTED_MESSAGE;
1787 if (!(type & EVP_PKT_SIGN))
1789 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
1790 al=SSL_AD_ILLEGAL_PARAMETER;
1794 if (s->s3->change_cipher_spec)
1796 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
1797 al=SSL_AD_UNEXPECTED_MESSAGE;
1801 /* we now have a signature that we need to verify */
1802 p=(unsigned char *)s->init_msg;
1807 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
1808 al=SSL_AD_DECODE_ERROR;
1812 j=EVP_PKEY_size(pkey);
1813 if ((i > j) || (n > j) || (n <= 0))
1815 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
1816 al=SSL_AD_DECODE_ERROR;
1820 #ifndef OPENSSL_NO_RSA
1821 if (pkey->type == EVP_PKEY_RSA)
1823 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
1824 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
1828 al=SSL_AD_DECRYPT_ERROR;
1829 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
1834 al=SSL_AD_DECRYPT_ERROR;
1835 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
1841 #ifndef OPENSSL_NO_DSA
1842 if (pkey->type == EVP_PKEY_DSA)
1844 j=DSA_verify(pkey->save_type,
1845 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
1846 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
1850 al=SSL_AD_DECRYPT_ERROR;
1851 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
1858 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
1859 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
1868 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1871 EVP_PKEY_free(pkey);
1875 static int ssl3_get_client_certificate(SSL *s)
1877 int i,ok,al,ret= -1;
1879 unsigned long l,nc,llen,n;
1880 unsigned char *p,*d,*q;
1881 STACK_OF(X509) *sk=NULL;
1883 n=ssl3_get_message(s,
1890 if (!ok) return((int)n);
1892 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
1894 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
1895 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1897 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1898 al=SSL_AD_HANDSHAKE_FAILURE;
1901 /* If tls asked for a client cert, the client must return a 0 list */
1902 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
1904 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
1905 al=SSL_AD_UNEXPECTED_MESSAGE;
1908 s->s3->tmp.reuse_message=1;
1912 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1914 al=SSL_AD_UNEXPECTED_MESSAGE;
1915 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
1918 d=p=(unsigned char *)s->init_msg;
1920 if ((sk=sk_X509_new_null()) == NULL)
1922 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1929 al=SSL_AD_DECODE_ERROR;
1930 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1933 for (nc=0; nc<llen; )
1936 if ((l+nc+3) > llen)
1938 al=SSL_AD_DECODE_ERROR;
1939 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1944 x=d2i_X509(NULL,&p,l);
1947 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
1952 al=SSL_AD_DECODE_ERROR;
1953 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1956 if (!sk_X509_push(sk,x))
1958 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1965 if (sk_X509_num(sk) <= 0)
1967 /* TLS does not mind 0 certs returned */
1968 if (s->version == SSL3_VERSION)
1970 al=SSL_AD_HANDSHAKE_FAILURE;
1971 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
1974 /* Fail for TLS only if we required a certificate */
1975 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
1976 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1978 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1979 al=SSL_AD_HANDSHAKE_FAILURE;
1985 i=ssl_verify_cert_chain(s,sk);
1988 al=ssl_verify_alarm_type(s->verify_result);
1989 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
1994 if (s->session->peer != NULL) /* This should not be needed */
1995 X509_free(s->session->peer);
1996 s->session->peer=sk_X509_shift(sk);
1997 s->session->verify_result = s->verify_result;
1999 /* With the current implementation, sess_cert will always be NULL
2000 * when we arrive here. */
2001 if (s->session->sess_cert == NULL)
2003 s->session->sess_cert = ssl_sess_cert_new();
2004 if (s->session->sess_cert == NULL)
2006 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2010 if (s->session->sess_cert->cert_chain != NULL)
2011 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2012 s->session->sess_cert->cert_chain=sk;
2013 /* Inconsistency alert: cert_chain does *not* include the
2014 * peer's own certificate, while we do include it in s3_clnt.c */
2022 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2025 if (x != NULL) X509_free(x);
2026 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
2030 int ssl3_send_server_certificate(SSL *s)
2035 if (s->state == SSL3_ST_SW_CERT_A)
2037 x=ssl_get_server_send_cert(s);
2039 /* VRS: allow null cert if auth == KRB5 */
2040 (s->s3->tmp.new_cipher->algorithms
2041 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
2042 != (SSL_aKRB5|SSL_kKRB5))
2044 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
2048 l=ssl3_output_cert_chain(s,x);
2049 s->state=SSL3_ST_SW_CERT_B;
2054 /* SSL3_ST_SW_CERT_B */
2055 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));