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 #ifndef OPENSSL_NO_KRB5
125 #include <openssl/krb5_asn.h>
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 CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD
);
160 memcpy((char *)&SSLv3_server_data
,(char *)sslv3_base_method(),
162 SSLv3_server_data
.ssl_accept
=ssl3_accept
;
163 SSLv3_server_data
.get_ssl_method
=ssl3_get_server_method
;
167 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD
);
169 return(&SSLv3_server_data
);
172 int ssl3_accept(SSL
*s
)
175 unsigned long l
,Time
=time(NULL
);
176 void (*cb
)(const SSL
*ssl
,int type
,int val
)=NULL
;
179 int new_state
,state
,skip
=0;
181 RAND_add(&Time
,sizeof(Time
),0);
185 if (s
->info_callback
!= NULL
)
187 else if (s
->ctx
->info_callback
!= NULL
)
188 cb
=s
->ctx
->info_callback
;
190 /* init things to blank */
192 if (!SSL_in_init(s
) || SSL_in_before(s
)) SSL_clear(s
);
196 SSLerr(SSL_F_SSL3_ACCEPT
,SSL_R_NO_CERTIFICATE_SET
);
206 case SSL_ST_RENEGOTIATE
:
208 /* s->state=SSL_ST_ACCEPT; */
212 case SSL_ST_BEFORE
|SSL_ST_ACCEPT
:
213 case SSL_ST_OK
|SSL_ST_ACCEPT
:
216 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_START
,1);
218 if ((s
->version
>>8) != 3)
220 SSLerr(SSL_F_SSL3_ACCEPT
, ERR_R_INTERNAL_ERROR
);
223 s
->type
=SSL_ST_ACCEPT
;
225 if (s
->init_buf
== NULL
)
227 if ((buf
=BUF_MEM_new()) == NULL
)
232 if (!BUF_MEM_grow(buf
,SSL3_RT_MAX_PLAIN_LENGTH
))
240 if (!ssl3_setup_buffers(s
))
248 if (s
->state
!= SSL_ST_RENEGOTIATE
)
250 /* Ok, we now need to push on a buffering BIO so that
251 * the output is sent in a way that TCP likes :-)
253 if (!ssl_init_wbio_buffer(s
,1)) { ret
= -1; goto end
; }
255 ssl3_init_finished_mac(s
);
256 s
->state
=SSL3_ST_SR_CLNT_HELLO_A
;
257 s
->ctx
->stats
.sess_accept
++;
261 /* s->state == SSL_ST_RENEGOTIATE,
262 * we will just send a HelloRequest */
263 s
->ctx
->stats
.sess_accept_renegotiate
++;
264 s
->state
=SSL3_ST_SW_HELLO_REQ_A
;
268 case SSL3_ST_SW_HELLO_REQ_A
:
269 case SSL3_ST_SW_HELLO_REQ_B
:
272 ret
=ssl3_send_hello_request(s
);
273 if (ret
<= 0) goto end
;
274 s
->s3
->tmp
.next_state
=SSL3_ST_SW_HELLO_REQ_C
;
275 s
->state
=SSL3_ST_SW_FLUSH
;
278 ssl3_init_finished_mac(s
);
281 case SSL3_ST_SW_HELLO_REQ_C
:
285 case SSL3_ST_SR_CLNT_HELLO_A
:
286 case SSL3_ST_SR_CLNT_HELLO_B
:
287 case SSL3_ST_SR_CLNT_HELLO_C
:
290 ret
=ssl3_get_client_hello(s
);
291 if (ret
<= 0) goto end
;
293 s
->state
=SSL3_ST_SW_SRVR_HELLO_A
;
297 case SSL3_ST_SW_SRVR_HELLO_A
:
298 case SSL3_ST_SW_SRVR_HELLO_B
:
299 ret
=ssl3_send_server_hello(s
);
300 if (ret
<= 0) goto end
;
303 s
->state
=SSL3_ST_SW_CHANGE_A
;
305 s
->state
=SSL3_ST_SW_CERT_A
;
309 case SSL3_ST_SW_CERT_A
:
310 case SSL3_ST_SW_CERT_B
:
311 /* Check if it is anon DH */
312 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
))
314 ret
=ssl3_send_server_certificate(s
);
315 if (ret
<= 0) goto end
;
319 s
->state
=SSL3_ST_SW_KEY_EXCH_A
;
323 case SSL3_ST_SW_KEY_EXCH_A
:
324 case SSL3_ST_SW_KEY_EXCH_B
:
325 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
327 /* clear this, it may get reset by
328 * send_server_key_exchange */
329 if ((s
->options
& SSL_OP_EPHEMERAL_RSA
)
330 #ifndef OPENSSL_NO_KRB5
332 #endif /* OPENSSL_NO_KRB5 */
334 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
335 * even when forbidden by protocol specs
336 * (handshake may fail as clients are not required to
337 * be able to handle this) */
338 s
->s3
->tmp
.use_rsa_tmp
=1;
340 s
->s3
->tmp
.use_rsa_tmp
=0;
342 /* only send if a DH key exchange, fortezza or
343 * RSA but we have a sign only certificate */
344 if (s
->s3
->tmp
.use_rsa_tmp
345 || (l
& (SSL_DH
|SSL_kFZA
))
347 && (s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
== NULL
348 || (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
)
349 && EVP_PKEY_size(s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
)*8 > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)
355 ret
=ssl3_send_server_key_exchange(s
);
356 if (ret
<= 0) goto end
;
361 s
->state
=SSL3_ST_SW_CERT_REQ_A
;
365 case SSL3_ST_SW_CERT_REQ_A
:
366 case SSL3_ST_SW_CERT_REQ_B
:
367 if (/* don't request cert unless asked for it: */
368 !(s
->verify_mode
& SSL_VERIFY_PEER
) ||
369 /* if SSL_VERIFY_CLIENT_ONCE is set,
370 * don't request cert during re-negotiation: */
371 ((s
->session
->peer
!= NULL
) &&
372 (s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
)) ||
373 /* never request cert in anonymous ciphersuites
374 * (see section "Certificate request" in SSL 3 drafts
375 * and in RFC 2246): */
376 ((s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
) &&
377 /* ... except when the application insists on verification
378 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
379 !(s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) ||
380 /* never request cert in Kerberos ciphersuites */
381 (s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aKRB5
))
383 /* no cert request */
385 s
->s3
->tmp
.cert_request
=0;
386 s
->state
=SSL3_ST_SW_SRVR_DONE_A
;
390 s
->s3
->tmp
.cert_request
=1;
391 ret
=ssl3_send_certificate_request(s
);
392 if (ret
<= 0) goto end
;
393 #ifndef NETSCAPE_HANG_BUG
394 s
->state
=SSL3_ST_SW_SRVR_DONE_A
;
396 s
->state
=SSL3_ST_SW_FLUSH
;
397 s
->s3
->tmp
.next_state
=SSL3_ST_SR_CERT_A
;
403 case SSL3_ST_SW_SRVR_DONE_A
:
404 case SSL3_ST_SW_SRVR_DONE_B
:
405 ret
=ssl3_send_server_done(s
);
406 if (ret
<= 0) goto end
;
407 s
->s3
->tmp
.next_state
=SSL3_ST_SR_CERT_A
;
408 s
->state
=SSL3_ST_SW_FLUSH
;
412 case SSL3_ST_SW_FLUSH
:
413 /* number of bytes to be flushed */
414 num1
=BIO_ctrl(s
->wbio
,BIO_CTRL_INFO
,0,NULL
);
417 s
->rwstate
=SSL_WRITING
;
418 num1
=BIO_flush(s
->wbio
);
419 if (num1
<= 0) { ret
= -1; goto end
; }
420 s
->rwstate
=SSL_NOTHING
;
423 s
->state
=s
->s3
->tmp
.next_state
;
426 case SSL3_ST_SR_CERT_A
:
427 case SSL3_ST_SR_CERT_B
:
428 /* Check for second client hello (MS SGC) */
429 ret
= ssl3_check_client_hello(s
);
433 s
->state
= SSL3_ST_SR_CLNT_HELLO_C
;
435 if (s
->s3
->tmp
.cert_request
)
437 ret
=ssl3_get_client_certificate(s
);
438 if (ret
<= 0) goto end
;
441 s
->state
=SSL3_ST_SR_KEY_EXCH_A
;
445 case SSL3_ST_SR_KEY_EXCH_A
:
446 case SSL3_ST_SR_KEY_EXCH_B
:
447 ret
=ssl3_get_client_key_exchange(s
);
448 if (ret
<= 0) goto end
;
449 s
->state
=SSL3_ST_SR_CERT_VRFY_A
;
452 /* We need to get hashes here so if there is
453 * a client cert, it can be verified */
454 s
->method
->ssl3_enc
->cert_verify_mac(s
,
455 &(s
->s3
->finish_dgst1
),
456 &(s
->s3
->tmp
.cert_verify_md
[0]));
457 s
->method
->ssl3_enc
->cert_verify_mac(s
,
458 &(s
->s3
->finish_dgst2
),
459 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]));
463 case SSL3_ST_SR_CERT_VRFY_A
:
464 case SSL3_ST_SR_CERT_VRFY_B
:
466 /* we should decide if we expected this one */
467 ret
=ssl3_get_cert_verify(s
);
468 if (ret
<= 0) goto end
;
470 s
->state
=SSL3_ST_SR_FINISHED_A
;
474 case SSL3_ST_SR_FINISHED_A
:
475 case SSL3_ST_SR_FINISHED_B
:
476 ret
=ssl3_get_finished(s
,SSL3_ST_SR_FINISHED_A
,
477 SSL3_ST_SR_FINISHED_B
);
478 if (ret
<= 0) goto end
;
482 s
->state
=SSL3_ST_SW_CHANGE_A
;
486 case SSL3_ST_SW_CHANGE_A
:
487 case SSL3_ST_SW_CHANGE_B
:
489 s
->session
->cipher
=s
->s3
->tmp
.new_cipher
;
490 if (!s
->method
->ssl3_enc
->setup_key_block(s
))
491 { ret
= -1; goto end
; }
493 ret
=ssl3_send_change_cipher_spec(s
,
494 SSL3_ST_SW_CHANGE_A
,SSL3_ST_SW_CHANGE_B
);
496 if (ret
<= 0) goto end
;
497 s
->state
=SSL3_ST_SW_FINISHED_A
;
500 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
501 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
509 case SSL3_ST_SW_FINISHED_A
:
510 case SSL3_ST_SW_FINISHED_B
:
511 ret
=ssl3_send_finished(s
,
512 SSL3_ST_SW_FINISHED_A
,SSL3_ST_SW_FINISHED_B
,
513 s
->method
->ssl3_enc
->server_finished_label
,
514 s
->method
->ssl3_enc
->server_finished_label_len
);
515 if (ret
<= 0) goto end
;
516 s
->state
=SSL3_ST_SW_FLUSH
;
518 s
->s3
->tmp
.next_state
=SSL3_ST_SR_FINISHED_A
;
520 s
->s3
->tmp
.next_state
=SSL_ST_OK
;
525 /* clean a few things up */
526 ssl3_cleanup_key_block(s
);
528 BUF_MEM_free(s
->init_buf
);
531 /* remove buffering on output */
532 ssl_free_wbio_buffer(s
);
536 if (s
->new_session
== 2) /* skipped if we just sent a HelloRequest */
538 /* actually not necessarily a 'new' session unless
539 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
543 ssl_update_cache(s
,SSL_SESS_CACHE_SERVER
);
545 s
->ctx
->stats
.sess_accept_good
++;
547 s
->handshake_func
=ssl3_accept
;
549 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_DONE
,1);
557 SSLerr(SSL_F_SSL3_ACCEPT
,SSL_R_UNKNOWN_STATE
);
563 if (!s
->s3
->tmp
.reuse_message
&& !skip
)
567 if ((ret
=BIO_flush(s
->wbio
)) <= 0)
572 if ((cb
!= NULL
) && (s
->state
!= state
))
576 cb(s
,SSL_CB_ACCEPT_LOOP
,1);
583 /* BIO_flush(s->wbio); */
587 cb(s
,SSL_CB_ACCEPT_EXIT
,ret
);
591 static int ssl3_send_hello_request(SSL
*s
)
595 if (s
->state
== SSL3_ST_SW_HELLO_REQ_A
)
597 p
=(unsigned char *)s
->init_buf
->data
;
598 *(p
++)=SSL3_MT_HELLO_REQUEST
;
603 s
->state
=SSL3_ST_SW_HELLO_REQ_B
;
604 /* number of bytes to write */
609 /* SSL3_ST_SW_HELLO_REQ_B */
610 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
613 static int ssl3_check_client_hello(SSL
*s
)
618 /* this function is called when we really expect a Certificate message,
619 * so permit appropriate message length */
620 n
=ssl3_get_message(s
,
626 if (!ok
) return((int)n
);
627 s
->s3
->tmp
.reuse_message
= 1;
628 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_HELLO
)
630 /* Throw away what we have done so far in the current handshake,
631 * which will now be aborted. (A full SSL_clear would be too much.)
632 * I hope that tmp.dh is the only thing that may need to be cleared
633 * when a handshake is not completed ... */
634 #ifndef OPENSSL_NO_DH
635 if (s
->s3
->tmp
.dh
!= NULL
)
637 DH_free(s
->s3
->tmp
.dh
);
638 s
->s3
->tmp
.dh
= NULL
;
646 static int ssl3_get_client_hello(SSL
*s
)
648 int i
,j
,ok
,al
,ret
= -1;
651 unsigned char *p
,*d
,*q
;
654 STACK_OF(SSL_CIPHER
) *ciphers
=NULL
;
656 /* We do this so that we will respond with our native type.
657 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
658 * This down switching should be handled by a different method.
659 * If we are SSLv3, we will respond with SSLv3, even if prompted with
662 if (s
->state
== SSL3_ST_SR_CLNT_HELLO_A
)
665 s
->state
=SSL3_ST_SR_CLNT_HELLO_B
;
667 n
=ssl3_get_message(s
,
668 SSL3_ST_SR_CLNT_HELLO_B
,
669 SSL3_ST_SR_CLNT_HELLO_C
,
670 SSL3_MT_CLIENT_HELLO
,
671 SSL3_RT_MAX_PLAIN_LENGTH
,
674 if (!ok
) return((int)n
);
675 d
=p
=(unsigned char *)s
->init_msg
;
677 /* use version from inside client hello, not from record header
678 * (may differ: see RFC 2246, Appendix E, second paragraph) */
679 s
->client_version
=(((int)p
[0])<<8)|(int)p
[1];
682 if (s
->client_version
< s
->version
)
684 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_WRONG_VERSION_NUMBER
);
685 if ((s
->client_version
>>8) == SSL3_VERSION_MAJOR
)
687 /* similar to ssl3_get_record, send alert using remote version number */
688 s
->version
= s
->client_version
;
690 al
= SSL_AD_PROTOCOL_VERSION
;
694 /* load the client random */
695 memcpy(s
->s3
->client_random
,p
,SSL3_RANDOM_SIZE
);
698 /* get the session-id */
702 /* Versions before 0.9.7 always allow session reuse during renegotiation
703 * (i.e. when s->new_session is true), option
704 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
705 * Maybe this optional behaviour should always have been the default,
706 * but we cannot safely change the default behaviour (or new applications
707 * might be written that become totally unsecure when compiled with
708 * an earlier library version)
710 if (j
== 0 || (s
->new_session
&& (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
)))
712 if (!ssl_get_new_session(s
,1))
717 i
=ssl_get_prev_session(s
,p
,j
);
719 { /* previous session */
726 if (!ssl_get_new_session(s
,1))
733 if ((i
== 0) && (j
!= 0))
735 /* we need a cipher if we are not resuming a session */
736 al
=SSL_AD_ILLEGAL_PARAMETER
;
737 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_CIPHERS_SPECIFIED
);
742 /* not enough data */
743 al
=SSL_AD_DECODE_ERROR
;
744 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
747 if ((i
> 0) && (ssl_bytes_to_cipher_list(s
,p
,i
,&(ciphers
))
754 /* If it is a hit, check that the cipher is in the list */
755 if ((s
->hit
) && (i
> 0))
758 id
=s
->session
->cipher
->id
;
761 printf("client sent %d ciphers\n",sk_num(ciphers
));
763 for (i
=0; i
<sk_SSL_CIPHER_num(ciphers
); i
++)
765 c
=sk_SSL_CIPHER_value(ciphers
,i
);
767 printf("client [%2d of %2d]:%s\n",
768 i
,sk_num(ciphers
),SSL_CIPHER_get_name(c
));
778 if ((s
->options
& SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
) && (sk_SSL_CIPHER_num(ciphers
) == 1))
780 /* Very bad for multi-threading.... */
781 s
->session
->cipher
=sk_SSL_CIPHER_value(ciphers
,
786 /* we need to have the cipher in the cipher
787 * list if we are asked to reuse it */
788 al
=SSL_AD_ILLEGAL_PARAMETER
;
789 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_REQUIRED_CIPHER_MISSING
);
799 /* not enough data */
800 al
=SSL_AD_DECODE_ERROR
;
801 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
807 if (p
[j
] == 0) break;
814 al
=SSL_AD_DECODE_ERROR
;
815 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_COMPRESSION_SPECIFIED
);
819 /* Worst case, we will use the NULL compression, but if we have other
820 * options, we will now look for them. We have i-1 compression
821 * algorithms from the client, starting at q. */
822 s
->s3
->tmp
.new_compression
=NULL
;
823 if (s
->ctx
->comp_methods
!= NULL
)
824 { /* See if we have a match */
827 nn
=sk_SSL_COMP_num(s
->ctx
->comp_methods
);
830 comp
=sk_SSL_COMP_value(s
->ctx
->comp_methods
,m
);
843 s
->s3
->tmp
.new_compression
=comp
;
848 /* TLS does not mind if there is extra stuff */
849 #if 0 /* SSL 3.0 does not mind either, so we should disable this test
850 * (was enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b,
851 * in earlier SSLeay/OpenSSL releases this test existed but was buggy) */
852 if (s
->version
== SSL3_VERSION
)
856 /* wrong number of bytes,
857 * there could be more to follow */
858 al
=SSL_AD_DECODE_ERROR
;
859 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
865 /* Given s->session->ciphers and SSL_get_ciphers, we must
870 s
->session
->compress_meth
=(comp
== NULL
)?0:comp
->id
;
871 if (s
->session
->ciphers
!= NULL
)
872 sk_SSL_CIPHER_free(s
->session
->ciphers
);
873 s
->session
->ciphers
=ciphers
;
876 al
=SSL_AD_ILLEGAL_PARAMETER
;
877 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_CIPHERS_PASSED
);
881 c
=ssl3_choose_cipher(s
,s
->session
->ciphers
,
886 al
=SSL_AD_HANDSHAKE_FAILURE
;
887 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_SHARED_CIPHER
);
890 s
->s3
->tmp
.new_cipher
=c
;
894 /* Session-id reuse */
895 #ifdef REUSE_CIPHER_BUG
896 STACK_OF(SSL_CIPHER
) *sk
;
900 if (s
->options
& SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG
)
902 sk
=s
->session
->ciphers
;
903 for (i
=0; i
<sk_SSL_CIPHER_num(sk
); i
++)
905 c
=sk_SSL_CIPHER_value(sk
,i
);
906 if (c
->algorithms
& SSL_eNULL
)
908 if (SSL_C_IS_EXPORT(c
))
912 s
->s3
->tmp
.new_cipher
=nc
;
914 s
->s3
->tmp
.new_cipher
=ec
;
916 s
->s3
->tmp
.new_cipher
=s
->session
->cipher
;
920 s
->s3
->tmp
.new_cipher
=s
->session
->cipher
;
923 /* we now have the following setup.
925 * cipher_list - our prefered list of ciphers
926 * ciphers - the clients prefered list of ciphers
927 * compression - basically ignored right now
928 * ssl version is set - sslv3
929 * s->session - The ssl session has been setup.
930 * s->hit - session reuse flag
931 * s->tmp.new_cipher - the new cipher to use.
938 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
941 if (ciphers
!= NULL
) sk_SSL_CIPHER_free(ciphers
);
945 static int ssl3_send_server_hello(SSL
*s
)
950 unsigned long l
,Time
;
952 if (s
->state
== SSL3_ST_SW_SRVR_HELLO_A
)
954 buf
=(unsigned char *)s
->init_buf
->data
;
955 p
=s
->s3
->server_random
;
956 Time
=time(NULL
); /* Time */
958 RAND_pseudo_bytes(p
,SSL3_RANDOM_SIZE
-sizeof(Time
));
959 /* Do the message type and length last */
962 *(p
++)=s
->version
>>8;
963 *(p
++)=s
->version
&0xff;
966 memcpy(p
,s
->s3
->server_random
,SSL3_RANDOM_SIZE
);
969 /* now in theory we have 3 options to sending back the
970 * session id. If it is a re-use, we send back the
971 * old session-id, if it is a new session, we send
972 * back the new session-id or we send back a 0 length
973 * session-id if we want it to be single use.
974 * Currently I will not implement the '0' length session-id
975 * 12-Jan-98 - I'll now support the '0' length stuff.
977 if (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
))
978 s
->session
->session_id_length
=0;
980 sl
=s
->session
->session_id_length
;
981 if (sl
> sizeof s
->session
->session_id
)
983 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
987 memcpy(p
,s
->session
->session_id
,sl
);
991 i
=ssl3_put_cipher_by_char(s
->s3
->tmp
.new_cipher
,p
);
994 /* put the compression method */
995 if (s
->s3
->tmp
.new_compression
== NULL
)
998 *(p
++)=s
->s3
->tmp
.new_compression
->id
;
1003 *(d
++)=SSL3_MT_SERVER_HELLO
;
1006 s
->state
=SSL3_ST_CW_CLNT_HELLO_B
;
1007 /* number of bytes to write */
1012 /* SSL3_ST_CW_CLNT_HELLO_B */
1013 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1016 static int ssl3_send_server_done(SSL
*s
)
1020 if (s
->state
== SSL3_ST_SW_SRVR_DONE_A
)
1022 p
=(unsigned char *)s
->init_buf
->data
;
1025 *(p
++)=SSL3_MT_SERVER_DONE
;
1030 s
->state
=SSL3_ST_SW_SRVR_DONE_B
;
1031 /* number of bytes to write */
1036 /* SSL3_ST_CW_CLNT_HELLO_B */
1037 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1040 static int ssl3_send_server_key_exchange(SSL
*s
)
1042 #ifndef OPENSSL_NO_RSA
1046 unsigned char md_buf
[MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
];
1049 #ifndef OPENSSL_NO_DH
1053 unsigned char *p
,*d
;
1063 EVP_MD_CTX_init(&md_ctx
);
1064 if (s
->state
== SSL3_ST_SW_KEY_EXCH_A
)
1066 type
=s
->s3
->tmp
.new_cipher
->algorithms
& SSL_MKEY_MASK
;
1071 r
[0]=r
[1]=r
[2]=r
[3]=NULL
;
1073 #ifndef OPENSSL_NO_RSA
1074 if (type
& SSL_kRSA
)
1077 if ((rsa
== NULL
) && (s
->cert
->rsa_tmp_cb
!= NULL
))
1079 rsa
=s
->cert
->rsa_tmp_cb(s
,
1080 SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
),
1081 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
));
1084 al
=SSL_AD_HANDSHAKE_FAILURE
;
1085 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_ERROR_GENERATING_TMP_RSA_KEY
);
1093 al
=SSL_AD_HANDSHAKE_FAILURE
;
1094 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_RSA_KEY
);
1099 s
->s3
->tmp
.use_rsa_tmp
=1;
1103 #ifndef OPENSSL_NO_DH
1104 if (type
& SSL_kEDH
)
1107 if ((dhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
))
1108 dhp
=s
->cert
->dh_tmp_cb(s
,
1109 SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
),
1110 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
));
1113 al
=SSL_AD_HANDSHAKE_FAILURE
;
1114 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
1118 if (s
->s3
->tmp
.dh
!= NULL
)
1121 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1125 if ((dh
=DHparams_dup(dhp
)) == NULL
)
1127 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1132 if ((dhp
->pub_key
== NULL
||
1133 dhp
->priv_key
== NULL
||
1134 (s
->options
& SSL_OP_SINGLE_DH_USE
)))
1136 if(!DH_generate_key(dh
))
1138 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1145 dh
->pub_key
=BN_dup(dhp
->pub_key
);
1146 dh
->priv_key
=BN_dup(dhp
->priv_key
);
1147 if ((dh
->pub_key
== NULL
) ||
1148 (dh
->priv_key
== NULL
))
1150 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1161 al
=SSL_AD_HANDSHAKE_FAILURE
;
1162 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1165 for (i
=0; r
[i
] != NULL
; i
++)
1167 nr
[i
]=BN_num_bytes(r
[i
]);
1171 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
))
1173 if ((pkey
=ssl_get_sign_pkey(s
,s
->s3
->tmp
.new_cipher
))
1176 al
=SSL_AD_DECODE_ERROR
;
1179 kn
=EVP_PKEY_size(pkey
);
1187 if (!BUF_MEM_grow_clean(buf
,n
+4+kn
))
1189 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_BUF
);
1192 d
=(unsigned char *)s
->init_buf
->data
;
1195 for (i
=0; r
[i
] != NULL
; i
++)
1205 /* n is the length of the params, they start at &(d[4])
1206 * and p points to the space at the end. */
1207 #ifndef OPENSSL_NO_RSA
1208 if (pkey
->type
== EVP_PKEY_RSA
)
1212 for (num
=2; num
> 0; num
--)
1214 EVP_DigestInit_ex(&md_ctx
,(num
== 2)
1215 ?s
->ctx
->md5
:s
->ctx
->sha1
, NULL
);
1216 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1217 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1218 EVP_DigestUpdate(&md_ctx
,&(d
[4]),n
);
1219 EVP_DigestFinal_ex(&md_ctx
,q
,
1220 (unsigned int *)&i
);
1224 if (RSA_sign(NID_md5_sha1
, md_buf
, j
,
1225 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0)
1227 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_RSA
);
1235 #if !defined(OPENSSL_NO_DSA)
1236 if (pkey
->type
== EVP_PKEY_DSA
)
1239 EVP_SignInit_ex(&md_ctx
,EVP_dss1(), NULL
);
1240 EVP_SignUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1241 EVP_SignUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1242 EVP_SignUpdate(&md_ctx
,&(d
[4]),n
);
1243 if (!EVP_SignFinal(&md_ctx
,&(p
[2]),
1244 (unsigned int *)&i
,pkey
))
1246 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_DSA
);
1255 /* Is this error check actually needed? */
1256 al
=SSL_AD_HANDSHAKE_FAILURE
;
1257 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNKNOWN_PKEY_TYPE
);
1262 *(d
++)=SSL3_MT_SERVER_KEY_EXCHANGE
;
1265 /* we should now have things packed up, so lets send
1271 s
->state
= SSL3_ST_SW_KEY_EXCH_B
;
1272 EVP_MD_CTX_cleanup(&md_ctx
);
1273 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1275 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1277 EVP_MD_CTX_cleanup(&md_ctx
);
1281 static int ssl3_send_certificate_request(SSL
*s
)
1283 unsigned char *p
,*d
;
1285 STACK_OF(X509_NAME
) *sk
=NULL
;
1289 if (s
->state
== SSL3_ST_SW_CERT_REQ_A
)
1293 d
=p
=(unsigned char *)&(buf
->data
[4]);
1295 /* get the list of acceptable cert types */
1297 n
=ssl3_get_req_cert_type(s
,p
);
1306 sk
=SSL_get_client_CA_list(s
);
1310 for (i
=0; i
<sk_X509_NAME_num(sk
); i
++)
1312 name
=sk_X509_NAME_value(sk
,i
);
1313 j
=i2d_X509_NAME(name
,NULL
);
1314 if (!BUF_MEM_grow_clean(buf
,4+n
+j
+2))
1316 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST
,ERR_R_BUF_LIB
);
1319 p
=(unsigned char *)&(buf
->data
[4+n
]);
1320 if (!(s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
))
1323 i2d_X509_NAME(name
,&p
);
1330 i2d_X509_NAME(name
,&p
);
1331 j
-=2; s2n(j
,d
); j
+=2;
1337 /* else no CA names */
1338 p
=(unsigned char *)&(buf
->data
[4+off
]);
1341 d
=(unsigned char *)buf
->data
;
1342 *(d
++)=SSL3_MT_CERTIFICATE_REQUEST
;
1345 /* we should now have things packed up, so lets send
1350 #ifdef NETSCAPE_HANG_BUG
1351 p
=(unsigned char *)s
->init_buf
->data
+ s
->init_num
;
1354 *(p
++)=SSL3_MT_SERVER_DONE
;
1361 s
->state
= SSL3_ST_SW_CERT_REQ_B
;
1364 /* SSL3_ST_SW_CERT_REQ_B */
1365 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1370 static int ssl3_get_client_key_exchange(SSL
*s
)
1376 #ifndef OPENSSL_NO_RSA
1378 EVP_PKEY
*pkey
=NULL
;
1380 #ifndef OPENSSL_NO_DH
1384 #ifndef OPENSSL_NO_KRB5
1386 #endif /* OPENSSL_NO_KRB5 */
1388 n
=ssl3_get_message(s
,
1389 SSL3_ST_SR_KEY_EXCH_A
,
1390 SSL3_ST_SR_KEY_EXCH_B
,
1391 SSL3_MT_CLIENT_KEY_EXCHANGE
,
1395 if (!ok
) return((int)n
);
1396 p
=(unsigned char *)s
->init_msg
;
1398 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
1400 #ifndef OPENSSL_NO_RSA
1403 /* FIX THIS UP EAY EAY EAY EAY */
1404 if (s
->s3
->tmp
.use_rsa_tmp
)
1406 if ((s
->cert
!= NULL
) && (s
->cert
->rsa_tmp
!= NULL
))
1407 rsa
=s
->cert
->rsa_tmp
;
1408 /* Don't do a callback because rsa_tmp should
1409 * be sent already */
1412 al
=SSL_AD_HANDSHAKE_FAILURE
;
1413 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_RSA_PKEY
);
1420 pkey
=s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
;
1421 if ( (pkey
== NULL
) ||
1422 (pkey
->type
!= EVP_PKEY_RSA
) ||
1423 (pkey
->pkey
.rsa
== NULL
))
1425 al
=SSL_AD_HANDSHAKE_FAILURE
;
1426 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_RSA_CERTIFICATE
);
1433 if (s
->version
> SSL3_VERSION
)
1438 if (!(s
->options
& SSL_OP_TLS_D5_BUG
))
1440 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG
);
1450 i
=RSA_private_decrypt((int)n
,p
,p
,rsa
,RSA_PKCS1_PADDING
);
1454 if (i
!= SSL_MAX_MASTER_KEY_LENGTH
)
1456 al
=SSL_AD_DECODE_ERROR
;
1457 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
1460 if ((al
== -1) && !((p
[0] == (s
->client_version
>>8)) && (p
[1] == (s
->client_version
& 0xff))))
1462 /* The premaster secret must contain the same version number as the
1463 * ClientHello to detect version rollback attacks (strangely, the
1464 * protocol does not offer such protection for DH ciphersuites).
1465 * However, buggy clients exist that send the negotiated protocol
1466 * version instead if the server does not support the requested
1468 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1469 if (!((s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) &&
1470 (p
[0] == (s
->version
>>8)) && (p
[1] == (s
->version
& 0xff))))
1472 al
=SSL_AD_DECODE_ERROR
;
1473 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
1475 /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1476 * (http://eprint.iacr.org/2003/052/) exploits the version
1477 * number check as a "bad version oracle" -- an alert would
1478 * reveal that the plaintext corresponding to some ciphertext
1479 * made up by the adversary is properly formatted except
1480 * that the version number is wrong. To avoid such attacks,
1481 * we should treat this just like any other decryption error. */
1487 /* Some decryption failure -- use random value instead as countermeasure
1488 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1489 * (see RFC 2246, section 7.4.7.1). */
1491 i
= SSL_MAX_MASTER_KEY_LENGTH
;
1492 p
[0] = s
->client_version
>> 8;
1493 p
[1] = s
->client_version
& 0xff;
1494 RAND_pseudo_bytes(p
+2, i
-2); /* should be RAND_bytes, but we cannot work around a failure */
1497 s
->session
->master_key_length
=
1498 s
->method
->ssl3_enc
->generate_master_secret(s
,
1499 s
->session
->master_key
,
1501 OPENSSL_cleanse(p
,i
);
1505 #ifndef OPENSSL_NO_DH
1506 if (l
& (SSL_kEDH
|SSL_kDHr
|SSL_kDHd
))
1511 if (!(s
->options
& SSL_OP_SSLEAY_080_CLIENT_DH_BUG
))
1513 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
1523 if (n
== 0L) /* the parameters are in the cert */
1525 al
=SSL_AD_HANDSHAKE_FAILURE
;
1526 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_UNABLE_TO_DECODE_DH_CERTS
);
1531 if (s
->s3
->tmp
.dh
== NULL
)
1533 al
=SSL_AD_HANDSHAKE_FAILURE
;
1534 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
1538 dh_srvr
=s
->s3
->tmp
.dh
;
1541 pub
=BN_bin2bn(p
,i
,NULL
);
1544 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BN_LIB
);
1548 i
=DH_compute_key(p
,pub
,dh_srvr
);
1552 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1556 DH_free(s
->s3
->tmp
.dh
);
1561 s
->session
->master_key_length
=
1562 s
->method
->ssl3_enc
->generate_master_secret(s
,
1563 s
->session
->master_key
,p
,i
);
1564 OPENSSL_cleanse(p
,i
);
1568 #ifndef OPENSSL_NO_KRB5
1571 krb5_error_code krb5rc
;
1572 krb5_data enc_ticket
;
1573 krb5_data authenticator
;
1575 KSSL_CTX
*kssl_ctx
= s
->kssl_ctx
;
1576 EVP_CIPHER_CTX ciph_ctx
;
1577 EVP_CIPHER
*enc
= NULL
;
1578 unsigned char iv
[EVP_MAX_IV_LENGTH
];
1579 unsigned char pms
[SSL_MAX_MASTER_KEY_LENGTH
1580 + EVP_MAX_BLOCK_LENGTH
];
1582 krb5_timestamp authtime
= 0;
1583 krb5_ticket_times ttimes
;
1585 EVP_CIPHER_CTX_init(&ciph_ctx
);
1587 if (!kssl_ctx
) kssl_ctx
= kssl_ctx_new();
1590 enc_ticket
.length
= i
;
1592 if (n
< enc_ticket
.length
+ 6)
1594 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1595 SSL_R_DATA_LENGTH_TOO_LONG
);
1599 enc_ticket
.data
= (char *)p
;
1600 p
+=enc_ticket
.length
;
1603 authenticator
.length
= i
;
1605 if (n
< enc_ticket
.length
+ authenticator
.length
+ 6)
1607 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1608 SSL_R_DATA_LENGTH_TOO_LONG
);
1612 authenticator
.data
= (char *)p
;
1613 p
+=authenticator
.length
;
1617 enc_pms
.data
= (char *)p
;
1620 /* Note that the length is checked again below,
1623 if(enc_pms
.length
> sizeof pms
)
1625 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1626 SSL_R_DATA_LENGTH_TOO_LONG
);
1630 if (n
!= enc_ticket
.length
+ authenticator
.length
+
1633 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1634 SSL_R_DATA_LENGTH_TOO_LONG
);
1638 if ((krb5rc
= kssl_sget_tkt(kssl_ctx
, &enc_ticket
, &ttimes
,
1642 printf("kssl_sget_tkt rtn %d [%d]\n",
1643 krb5rc
, kssl_err
.reason
);
1645 printf("kssl_err text= %s\n", kssl_err
.text
);
1646 #endif /* KSSL_DEBUG */
1647 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1652 /* Note: no authenticator is not considered an error,
1653 ** but will return authtime == 0.
1655 if ((krb5rc
= kssl_check_authent(kssl_ctx
, &authenticator
,
1656 &authtime
, &kssl_err
)) != 0)
1659 printf("kssl_check_authent rtn %d [%d]\n",
1660 krb5rc
, kssl_err
.reason
);
1662 printf("kssl_err text= %s\n", kssl_err
.text
);
1663 #endif /* KSSL_DEBUG */
1664 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1669 if ((krb5rc
= kssl_validate_times(authtime
, &ttimes
)) != 0)
1671 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, krb5rc
);
1676 kssl_ctx_show(kssl_ctx
);
1677 #endif /* KSSL_DEBUG */
1679 enc
= kssl_map_enc(kssl_ctx
->enctype
);
1683 memset(iv
, 0, sizeof iv
); /* per RFC 1510 */
1685 if (!EVP_DecryptInit_ex(&ciph_ctx
,enc
,NULL
,kssl_ctx
->key
,iv
))
1687 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1688 SSL_R_DECRYPTION_FAILED
);
1691 if (!EVP_DecryptUpdate(&ciph_ctx
, pms
,&outl
,
1692 (unsigned char *)enc_pms
.data
, enc_pms
.length
))
1694 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1695 SSL_R_DECRYPTION_FAILED
);
1698 if (outl
> SSL_MAX_MASTER_KEY_LENGTH
)
1700 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1701 SSL_R_DATA_LENGTH_TOO_LONG
);
1704 if (!EVP_DecryptFinal_ex(&ciph_ctx
,&(pms
[outl
]),&padl
))
1706 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1707 SSL_R_DECRYPTION_FAILED
);
1711 if (outl
> SSL_MAX_MASTER_KEY_LENGTH
)
1713 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1714 SSL_R_DATA_LENGTH_TOO_LONG
);
1717 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
1719 s
->session
->master_key_length
=
1720 s
->method
->ssl3_enc
->generate_master_secret(s
,
1721 s
->session
->master_key
, pms
, outl
);
1723 if (kssl_ctx
->client_princ
)
1725 int len
= strlen(kssl_ctx
->client_princ
);
1726 if ( len
< SSL_MAX_KRB5_PRINCIPAL_LENGTH
)
1728 s
->session
->krb5_client_princ_len
= len
;
1729 memcpy(s
->session
->krb5_client_princ
,kssl_ctx
->client_princ
,len
);
1734 /* Was doing kssl_ctx_free() here,
1735 ** but it caused problems for apache.
1736 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
1737 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
1741 #endif /* OPENSSL_NO_KRB5 */
1743 al
=SSL_AD_HANDSHAKE_FAILURE
;
1744 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1745 SSL_R_UNKNOWN_CIPHER_TYPE
);
1751 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1752 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA)
1758 static int ssl3_get_cert_verify(SSL
*s
)
1760 EVP_PKEY
*pkey
=NULL
;
1767 n
=ssl3_get_message(s
,
1768 SSL3_ST_SR_CERT_VRFY_A
,
1769 SSL3_ST_SR_CERT_VRFY_B
,
1774 if (!ok
) return((int)n
);
1776 if (s
->session
->peer
!= NULL
)
1778 peer
=s
->session
->peer
;
1779 pkey
=X509_get_pubkey(peer
);
1780 type
=X509_certificate_type(peer
,pkey
);
1788 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_VERIFY
)
1790 s
->s3
->tmp
.reuse_message
=1;
1791 if ((peer
!= NULL
) && (type
| EVP_PKT_SIGN
))
1793 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1794 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_MISSING_VERIFY_MESSAGE
);
1803 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_NO_CLIENT_CERT_RECEIVED
);
1804 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1808 if (!(type
& EVP_PKT_SIGN
))
1810 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
1811 al
=SSL_AD_ILLEGAL_PARAMETER
;
1815 if (s
->s3
->change_cipher_spec
)
1817 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_CCS_RECEIVED_EARLY
);
1818 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1822 /* we now have a signature that we need to verify */
1823 p
=(unsigned char *)s
->init_msg
;
1828 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_LENGTH_MISMATCH
);
1829 al
=SSL_AD_DECODE_ERROR
;
1833 j
=EVP_PKEY_size(pkey
);
1834 if ((i
> j
) || (n
> j
) || (n
<= 0))
1836 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_WRONG_SIGNATURE_SIZE
);
1837 al
=SSL_AD_DECODE_ERROR
;
1841 #ifndef OPENSSL_NO_RSA
1842 if (pkey
->type
== EVP_PKEY_RSA
)
1844 i
=RSA_verify(NID_md5_sha1
, s
->s3
->tmp
.cert_verify_md
,
1845 MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
, p
, i
,
1849 al
=SSL_AD_DECRYPT_ERROR
;
1850 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_DECRYPT
);
1855 al
=SSL_AD_DECRYPT_ERROR
;
1856 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_SIGNATURE
);
1862 #ifndef OPENSSL_NO_DSA
1863 if (pkey
->type
== EVP_PKEY_DSA
)
1865 j
=DSA_verify(pkey
->save_type
,
1866 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
1867 SHA_DIGEST_LENGTH
,p
,i
,pkey
->pkey
.dsa
);
1871 al
=SSL_AD_DECRYPT_ERROR
;
1872 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_DSA_SIGNATURE
);
1879 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,ERR_R_INTERNAL_ERROR
);
1880 al
=SSL_AD_UNSUPPORTED_CERTIFICATE
;
1889 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1892 EVP_PKEY_free(pkey
);
1896 static int ssl3_get_client_certificate(SSL
*s
)
1898 int i
,ok
,al
,ret
= -1;
1900 unsigned long l
,nc
,llen
,n
;
1901 unsigned char *p
,*d
,*q
;
1902 STACK_OF(X509
) *sk
=NULL
;
1904 n
=ssl3_get_message(s
,
1911 if (!ok
) return((int)n
);
1913 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_KEY_EXCHANGE
)
1915 if ( (s
->verify_mode
& SSL_VERIFY_PEER
) &&
1916 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
1918 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
1919 al
=SSL_AD_HANDSHAKE_FAILURE
;
1922 /* If tls asked for a client cert, the client must return a 0 list */
1923 if ((s
->version
> SSL3_VERSION
) && s
->s3
->tmp
.cert_request
)
1925 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
);
1926 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1929 s
->s3
->tmp
.reuse_message
=1;
1933 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
)
1935 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1936 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_WRONG_MESSAGE_TYPE
);
1939 d
=p
=(unsigned char *)s
->init_msg
;
1941 if ((sk
=sk_X509_new_null()) == NULL
)
1943 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
1950 al
=SSL_AD_DECODE_ERROR
;
1951 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_LENGTH_MISMATCH
);
1954 for (nc
=0; nc
<llen
; )
1957 if ((l
+nc
+3) > llen
)
1959 al
=SSL_AD_DECODE_ERROR
;
1960 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
1965 x
=d2i_X509(NULL
,&p
,l
);
1968 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_ASN1_LIB
);
1973 al
=SSL_AD_DECODE_ERROR
;
1974 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
1977 if (!sk_X509_push(sk
,x
))
1979 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
1986 if (sk_X509_num(sk
) <= 0)
1988 /* TLS does not mind 0 certs returned */
1989 if (s
->version
== SSL3_VERSION
)
1991 al
=SSL_AD_HANDSHAKE_FAILURE
;
1992 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_NO_CERTIFICATES_RETURNED
);
1995 /* Fail for TLS only if we required a certificate */
1996 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
1997 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
1999 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
2000 al
=SSL_AD_HANDSHAKE_FAILURE
;
2006 i
=ssl_verify_cert_chain(s
,sk
);
2009 al
=ssl_verify_alarm_type(s
->verify_result
);
2010 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_NO_CERTIFICATE_RETURNED
);
2015 if (s
->session
->peer
!= NULL
) /* This should not be needed */
2016 X509_free(s
->session
->peer
);
2017 s
->session
->peer
=sk_X509_shift(sk
);
2018 s
->session
->verify_result
= s
->verify_result
;
2020 /* With the current implementation, sess_cert will always be NULL
2021 * when we arrive here. */
2022 if (s
->session
->sess_cert
== NULL
)
2024 s
->session
->sess_cert
= ssl_sess_cert_new();
2025 if (s
->session
->sess_cert
== NULL
)
2027 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
2031 if (s
->session
->sess_cert
->cert_chain
!= NULL
)
2032 sk_X509_pop_free(s
->session
->sess_cert
->cert_chain
, X509_free
);
2033 s
->session
->sess_cert
->cert_chain
=sk
;
2034 /* Inconsistency alert: cert_chain does *not* include the
2035 * peer's own certificate, while we do include it in s3_clnt.c */
2043 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
2046 if (x
!= NULL
) X509_free(x
);
2047 if (sk
!= NULL
) sk_X509_pop_free(sk
,X509_free
);
2051 int ssl3_send_server_certificate(SSL
*s
)
2056 if (s
->state
== SSL3_ST_SW_CERT_A
)
2058 x
=ssl_get_server_send_cert(s
);
2060 /* VRS: allow null cert if auth == KRB5 */
2061 (s
->s3
->tmp
.new_cipher
->algorithms
2062 & (SSL_MKEY_MASK
|SSL_AUTH_MASK
))
2063 != (SSL_aKRB5
|SSL_kKRB5
))
2065 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE
,ERR_R_INTERNAL_ERROR
);
2069 l
=ssl3_output_cert_chain(s
,x
);
2070 s
->state
=SSL3_ST_SW_CERT_B
;
2075 /* SSL3_ST_SW_CERT_B */
2076 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));