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 #include "kssl_lcl.h"
125 #include <openssl/md5.h>
127 static SSL_METHOD
*ssl3_get_server_method(int ver
);
128 static int ssl3_get_client_hello(SSL
*s
);
129 static int ssl3_check_client_hello(SSL
*s
);
130 static int ssl3_send_server_hello(SSL
*s
);
131 static int ssl3_send_server_key_exchange(SSL
*s
);
132 static int ssl3_send_certificate_request(SSL
*s
);
133 static int ssl3_send_server_done(SSL
*s
);
134 static int ssl3_get_client_key_exchange(SSL
*s
);
135 static int ssl3_get_client_certificate(SSL
*s
);
136 static int ssl3_get_cert_verify(SSL
*s
);
137 static int ssl3_send_hello_request(SSL
*s
);
139 static SSL_METHOD
*ssl3_get_server_method(int ver
)
141 if (ver
== SSL3_VERSION
)
142 return(SSLv3_server_method());
147 SSL_METHOD
*SSLv3_server_method(void)
150 static SSL_METHOD SSLv3_server_data
;
154 memcpy((char *)&SSLv3_server_data
,(char *)sslv3_base_method(),
156 SSLv3_server_data
.ssl_accept
=ssl3_accept
;
157 SSLv3_server_data
.get_ssl_method
=ssl3_get_server_method
;
160 return(&SSLv3_server_data
);
163 int ssl3_accept(SSL
*s
)
166 unsigned long l
,Time
=time(NULL
);
170 int new_state
,state
,skip
=0;
171 int got_new_session
=0;
173 RAND_add(&Time
,sizeof(Time
),0);
177 if (s
->info_callback
!= NULL
)
179 else if (s
->ctx
->info_callback
!= NULL
)
180 cb
=s
->ctx
->info_callback
;
182 /* init things to blank */
183 if (!SSL_in_init(s
) || SSL_in_before(s
)) SSL_clear(s
);
188 SSLerr(SSL_F_SSL3_ACCEPT
,SSL_R_NO_CERTIFICATE_SET
);
198 case SSL_ST_RENEGOTIATE
:
200 /* s->state=SSL_ST_ACCEPT; */
204 case SSL_ST_BEFORE
|SSL_ST_ACCEPT
:
205 case SSL_ST_OK
|SSL_ST_ACCEPT
:
208 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_START
,1);
210 if ((s
->version
>>8) != 3)
212 SSLerr(SSL_F_SSL3_ACCEPT
, ERR_R_INTERNAL_ERROR
);
215 s
->type
=SSL_ST_ACCEPT
;
217 if (s
->init_buf
== NULL
)
219 if ((buf
=BUF_MEM_new()) == NULL
)
224 if (!BUF_MEM_grow(buf
,SSL3_RT_MAX_PLAIN_LENGTH
))
232 if (!ssl3_setup_buffers(s
))
240 if (s
->state
!= SSL_ST_RENEGOTIATE
)
242 /* Ok, we now need to push on a buffering BIO so that
243 * the output is sent in a way that TCP likes :-)
245 if (!ssl_init_wbio_buffer(s
,1)) { ret
= -1; goto end
; }
247 ssl3_init_finished_mac(s
);
248 s
->state
=SSL3_ST_SR_CLNT_HELLO_A
;
249 s
->ctx
->stats
.sess_accept
++;
253 /* s->state == SSL_ST_RENEGOTIATE,
254 * we will just send a HelloRequest */
255 s
->ctx
->stats
.sess_accept_renegotiate
++;
256 s
->state
=SSL3_ST_SW_HELLO_REQ_A
;
260 case SSL3_ST_SW_HELLO_REQ_A
:
261 case SSL3_ST_SW_HELLO_REQ_B
:
264 ret
=ssl3_send_hello_request(s
);
265 if (ret
<= 0) goto end
;
266 s
->s3
->tmp
.next_state
=SSL3_ST_SW_HELLO_REQ_C
;
267 s
->state
=SSL3_ST_SW_FLUSH
;
270 ssl3_init_finished_mac(s
);
273 case SSL3_ST_SW_HELLO_REQ_C
:
277 case SSL3_ST_SR_CLNT_HELLO_A
:
278 case SSL3_ST_SR_CLNT_HELLO_B
:
279 case SSL3_ST_SR_CLNT_HELLO_C
:
282 ret
=ssl3_get_client_hello(s
);
283 if (ret
<= 0) goto end
;
285 s
->state
=SSL3_ST_SW_SRVR_HELLO_A
;
289 case SSL3_ST_SW_SRVR_HELLO_A
:
290 case SSL3_ST_SW_SRVR_HELLO_B
:
291 ret
=ssl3_send_server_hello(s
);
292 if (ret
<= 0) goto end
;
295 s
->state
=SSL3_ST_SW_CHANGE_A
;
297 s
->state
=SSL3_ST_SW_CERT_A
;
301 case SSL3_ST_SW_CERT_A
:
302 case SSL3_ST_SW_CERT_B
:
303 /* Check if it is anon DH */
304 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
))
306 ret
=ssl3_send_server_certificate(s
);
307 if (ret
<= 0) goto end
;
311 s
->state
=SSL3_ST_SW_KEY_EXCH_A
;
315 case SSL3_ST_SW_KEY_EXCH_A
:
316 case SSL3_ST_SW_KEY_EXCH_B
:
317 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
319 /* clear this, it may get reset by
320 * send_server_key_exchange */
321 if ((s
->options
& SSL_OP_EPHEMERAL_RSA
)
322 #ifndef OPENSSL_NO_KRB5
324 #endif /* OPENSSL_NO_KRB5 */
326 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
327 * even when forbidden by protocol specs
328 * (handshake may fail as clients are not required to
329 * be able to handle this) */
330 s
->s3
->tmp
.use_rsa_tmp
=1;
332 s
->s3
->tmp
.use_rsa_tmp
=0;
334 /* only send if a DH key exchange, fortezza or
335 * RSA but we have a sign only certificate */
336 if (s
->s3
->tmp
.use_rsa_tmp
337 || (l
& (SSL_DH
|SSL_kFZA
))
339 && (s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
== NULL
340 || (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
)
341 && EVP_PKEY_size(s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
)*8 > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)
347 ret
=ssl3_send_server_key_exchange(s
);
348 if (ret
<= 0) goto end
;
353 s
->state
=SSL3_ST_SW_CERT_REQ_A
;
357 case SSL3_ST_SW_CERT_REQ_A
:
358 case SSL3_ST_SW_CERT_REQ_B
:
359 if (/* don't request cert unless asked for it: */
360 !(s
->verify_mode
& SSL_VERIFY_PEER
) ||
361 /* if SSL_VERIFY_CLIENT_ONCE is set,
362 * don't request cert during re-negotiation: */
363 ((s
->session
->peer
!= NULL
) &&
364 (s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
)) ||
365 /* never request cert in anonymous ciphersuites
366 * (see section "Certificate request" in SSL 3 drafts
367 * and in RFC 2246): */
368 ((s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
) &&
369 /* ... except when the application insists on verification
370 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
371 !(s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) ||
372 /* never request cert in Kerberos ciphersuites */
373 (s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aKRB5
))
375 /* no cert request */
377 s
->s3
->tmp
.cert_request
=0;
378 s
->state
=SSL3_ST_SW_SRVR_DONE_A
;
382 s
->s3
->tmp
.cert_request
=1;
383 ret
=ssl3_send_certificate_request(s
);
384 if (ret
<= 0) goto end
;
385 #ifndef NETSCAPE_HANG_BUG
386 s
->state
=SSL3_ST_SW_SRVR_DONE_A
;
388 s
->state
=SSL3_ST_SW_FLUSH
;
389 s
->s3
->tmp
.next_state
=SSL3_ST_SR_CERT_A
;
395 case SSL3_ST_SW_SRVR_DONE_A
:
396 case SSL3_ST_SW_SRVR_DONE_B
:
397 ret
=ssl3_send_server_done(s
);
398 if (ret
<= 0) goto end
;
399 s
->s3
->tmp
.next_state
=SSL3_ST_SR_CERT_A
;
400 s
->state
=SSL3_ST_SW_FLUSH
;
404 case SSL3_ST_SW_FLUSH
:
405 /* number of bytes to be flushed */
406 num1
=BIO_ctrl(s
->wbio
,BIO_CTRL_INFO
,0,NULL
);
409 s
->rwstate
=SSL_WRITING
;
410 num1
=BIO_flush(s
->wbio
);
411 if (num1
<= 0) { ret
= -1; goto end
; }
412 s
->rwstate
=SSL_NOTHING
;
415 s
->state
=s
->s3
->tmp
.next_state
;
418 case SSL3_ST_SR_CERT_A
:
419 case SSL3_ST_SR_CERT_B
:
420 /* Check for second client hello (MS SGC) */
421 ret
= ssl3_check_client_hello(s
);
425 s
->state
= SSL3_ST_SR_CLNT_HELLO_C
;
427 /* could be sent for a DH cert, even if we
428 * have not asked for it :-) */
429 ret
=ssl3_get_client_certificate(s
);
430 if (ret
<= 0) goto end
;
432 s
->state
=SSL3_ST_SR_KEY_EXCH_A
;
436 case SSL3_ST_SR_KEY_EXCH_A
:
437 case SSL3_ST_SR_KEY_EXCH_B
:
438 ret
=ssl3_get_client_key_exchange(s
);
439 if (ret
<= 0) goto end
;
440 s
->state
=SSL3_ST_SR_CERT_VRFY_A
;
443 /* We need to get hashes here so if there is
444 * a client cert, it can be verified */
445 s
->method
->ssl3_enc
->cert_verify_mac(s
,
446 &(s
->s3
->finish_dgst1
),
447 &(s
->s3
->tmp
.cert_verify_md
[0]));
448 s
->method
->ssl3_enc
->cert_verify_mac(s
,
449 &(s
->s3
->finish_dgst2
),
450 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]));
454 case SSL3_ST_SR_CERT_VRFY_A
:
455 case SSL3_ST_SR_CERT_VRFY_B
:
457 /* we should decide if we expected this one */
458 ret
=ssl3_get_cert_verify(s
);
459 if (ret
<= 0) goto end
;
461 s
->state
=SSL3_ST_SR_FINISHED_A
;
465 case SSL3_ST_SR_FINISHED_A
:
466 case SSL3_ST_SR_FINISHED_B
:
467 ret
=ssl3_get_finished(s
,SSL3_ST_SR_FINISHED_A
,
468 SSL3_ST_SR_FINISHED_B
);
469 if (ret
<= 0) goto end
;
473 s
->state
=SSL3_ST_SW_CHANGE_A
;
477 case SSL3_ST_SW_CHANGE_A
:
478 case SSL3_ST_SW_CHANGE_B
:
480 s
->session
->cipher
=s
->s3
->tmp
.new_cipher
;
481 if (!s
->method
->ssl3_enc
->setup_key_block(s
))
482 { ret
= -1; goto end
; }
484 ret
=ssl3_send_change_cipher_spec(s
,
485 SSL3_ST_SW_CHANGE_A
,SSL3_ST_SW_CHANGE_B
);
487 if (ret
<= 0) goto end
;
488 s
->state
=SSL3_ST_SW_FINISHED_A
;
491 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
492 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
500 case SSL3_ST_SW_FINISHED_A
:
501 case SSL3_ST_SW_FINISHED_B
:
502 ret
=ssl3_send_finished(s
,
503 SSL3_ST_SW_FINISHED_A
,SSL3_ST_SW_FINISHED_B
,
504 s
->method
->ssl3_enc
->server_finished_label
,
505 s
->method
->ssl3_enc
->server_finished_label_len
);
506 if (ret
<= 0) goto end
;
507 s
->state
=SSL3_ST_SW_FLUSH
;
509 s
->s3
->tmp
.next_state
=SSL3_ST_SR_FINISHED_A
;
511 s
->s3
->tmp
.next_state
=SSL_ST_OK
;
516 /* clean a few things up */
517 ssl3_cleanup_key_block(s
);
519 BUF_MEM_free(s
->init_buf
);
522 /* remove buffering on output */
523 ssl_free_wbio_buffer(s
);
527 if (got_new_session
) /* skipped if we just sent a HelloRequest */
529 /* actually not necessarily a 'new' session unless
530 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
534 ssl_update_cache(s
,SSL_SESS_CACHE_SERVER
);
536 s
->ctx
->stats
.sess_accept_good
++;
538 s
->handshake_func
=ssl3_accept
;
540 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_DONE
,1);
548 SSLerr(SSL_F_SSL3_ACCEPT
,SSL_R_UNKNOWN_STATE
);
554 if (!s
->s3
->tmp
.reuse_message
&& !skip
)
558 if ((ret
=BIO_flush(s
->wbio
)) <= 0)
563 if ((cb
!= NULL
) && (s
->state
!= state
))
567 cb(s
,SSL_CB_ACCEPT_LOOP
,1);
574 /* BIO_flush(s->wbio); */
577 cb(s
,SSL_CB_ACCEPT_EXIT
,ret
);
582 static int ssl3_send_hello_request(SSL
*s
)
586 if (s
->state
== SSL3_ST_SW_HELLO_REQ_A
)
588 p
=(unsigned char *)s
->init_buf
->data
;
589 *(p
++)=SSL3_MT_HELLO_REQUEST
;
594 s
->state
=SSL3_ST_SW_HELLO_REQ_B
;
595 /* number of bytes to write */
600 /* SSL3_ST_SW_HELLO_REQ_B */
601 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
604 static int ssl3_check_client_hello(SSL
*s
)
609 /* this function is called when we really expect a Certificate message,
610 * so permit appropriate message length */
611 n
=ssl3_get_message(s
,
617 if (!ok
) return((int)n
);
618 s
->s3
->tmp
.reuse_message
= 1;
619 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_HELLO
)
621 /* Throw away what we have done so far in the current handshake,
622 * which will now be aborted. (A full SSL_clear would be too much.)
623 * I hope that tmp.dh is the only thing that may need to be cleared
624 * when a handshake is not completed ... */
625 #ifndef OPENSSL_NO_DH
626 if (s
->s3
->tmp
.dh
!= NULL
)
628 DH_free(s
->s3
->tmp
.dh
);
629 s
->s3
->tmp
.dh
= NULL
;
637 static int ssl3_get_client_hello(SSL
*s
)
639 int i
,j
,ok
,al
,ret
= -1;
642 unsigned char *p
,*d
,*q
;
645 STACK_OF(SSL_CIPHER
) *ciphers
=NULL
;
647 /* We do this so that we will respond with our native type.
648 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
649 * This down switching should be handled by a different method.
650 * If we are SSLv3, we will respond with SSLv3, even if prompted with
653 if (s
->state
== SSL3_ST_SR_CLNT_HELLO_A
)
656 s
->state
=SSL3_ST_SR_CLNT_HELLO_B
;
658 n
=ssl3_get_message(s
,
659 SSL3_ST_SR_CLNT_HELLO_B
,
660 SSL3_ST_SR_CLNT_HELLO_C
,
661 SSL3_MT_CLIENT_HELLO
,
662 SSL3_RT_MAX_PLAIN_LENGTH
,
665 if (!ok
) return((int)n
);
666 d
=p
=(unsigned char *)s
->init_msg
;
668 /* use version from inside client hello, not from record header
669 * (may differ: see RFC 2246, Appendix E, second paragraph) */
670 s
->client_version
=(((int)p
[0])<<8)|(int)p
[1];
673 /* load the client random */
674 memcpy(s
->s3
->client_random
,p
,SSL3_RANDOM_SIZE
);
677 /* get the session-id */
681 /* Versions before 0.9.7 always allow session reuse during renegotiation
682 * (i.e. when s->new_session is true), option
683 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
684 * Maybe this optional behaviour should always have been the default,
685 * but we cannot safely change the default behaviour (or new applications
686 * might be written that become totally unsecure when compiled with
687 * an earlier library version)
689 if (j
== 0 || (s
->new_session
&& (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
)))
691 if (!ssl_get_new_session(s
,1))
696 i
=ssl_get_prev_session(s
,p
,j
);
698 { /* previous session */
705 if (!ssl_get_new_session(s
,1))
712 if ((i
== 0) && (j
!= 0))
714 /* we need a cipher if we are not resuming a session */
715 al
=SSL_AD_ILLEGAL_PARAMETER
;
716 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_CIPHERS_SPECIFIED
);
721 /* not enough data */
722 al
=SSL_AD_DECODE_ERROR
;
723 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
726 if ((i
> 0) && (ssl_bytes_to_cipher_list(s
,p
,i
,&(ciphers
))
733 /* If it is a hit, check that the cipher is in the list */
734 if ((s
->hit
) && (i
> 0))
737 id
=s
->session
->cipher
->id
;
740 printf("client sent %d ciphers\n",sk_num(ciphers
));
742 for (i
=0; i
<sk_SSL_CIPHER_num(ciphers
); i
++)
744 c
=sk_SSL_CIPHER_value(ciphers
,i
);
746 printf("client [%2d of %2d]:%s\n",
747 i
,sk_num(ciphers
),SSL_CIPHER_get_name(c
));
757 if ((s
->options
& SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
) && (sk_SSL_CIPHER_num(ciphers
) == 1))
759 /* Very bad for multi-threading.... */
760 s
->session
->cipher
=sk_SSL_CIPHER_value(ciphers
,
765 /* we need to have the cipher in the cipher
766 * list if we are asked to reuse it */
767 al
=SSL_AD_ILLEGAL_PARAMETER
;
768 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_REQUIRED_CIPHER_MISSING
);
779 if (p
[j
] == 0) break;
786 al
=SSL_AD_DECODE_ERROR
;
787 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_COMPRESSION_SPECIFIED
);
791 /* Worst case, we will use the NULL compression, but if we have other
792 * options, we will now look for them. We have i-1 compression
793 * algorithms from the client, starting at q. */
794 s
->s3
->tmp
.new_compression
=NULL
;
795 if (s
->ctx
->comp_methods
!= NULL
)
796 { /* See if we have a match */
799 nn
=sk_SSL_COMP_num(s
->ctx
->comp_methods
);
802 comp
=sk_SSL_COMP_value(s
->ctx
->comp_methods
,m
);
815 s
->s3
->tmp
.new_compression
=comp
;
820 /* TLS does not mind if there is extra stuff */
821 if (s
->version
== SSL3_VERSION
)
825 /* wrong number of bytes,
826 * there could be more to follow */
827 al
=SSL_AD_DECODE_ERROR
;
828 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
833 /* Given s->session->ciphers and SSL_get_ciphers, we must
838 s
->session
->compress_meth
=(comp
== NULL
)?0:comp
->id
;
839 if (s
->session
->ciphers
!= NULL
)
840 sk_SSL_CIPHER_free(s
->session
->ciphers
);
841 s
->session
->ciphers
=ciphers
;
844 al
=SSL_AD_ILLEGAL_PARAMETER
;
845 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_CIPHERS_PASSED
);
849 c
=ssl3_choose_cipher(s
,s
->session
->ciphers
,
854 al
=SSL_AD_HANDSHAKE_FAILURE
;
855 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_SHARED_CIPHER
);
858 s
->s3
->tmp
.new_cipher
=c
;
862 /* Session-id reuse */
863 #ifdef REUSE_CIPHER_BUG
864 STACK_OF(SSL_CIPHER
) *sk
;
868 if (s
->options
& SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG
)
870 sk
=s
->session
->ciphers
;
871 for (i
=0; i
<sk_SSL_CIPHER_num(sk
); i
++)
873 c
=sk_SSL_CIPHER_value(sk
,i
);
874 if (c
->algorithms
& SSL_eNULL
)
876 if (SSL_C_IS_EXPORT(c
))
880 s
->s3
->tmp
.new_cipher
=nc
;
882 s
->s3
->tmp
.new_cipher
=ec
;
884 s
->s3
->tmp
.new_cipher
=s
->session
->cipher
;
888 s
->s3
->tmp
.new_cipher
=s
->session
->cipher
;
891 /* we now have the following setup.
893 * cipher_list - our prefered list of ciphers
894 * ciphers - the clients prefered list of ciphers
895 * compression - basically ignored right now
896 * ssl version is set - sslv3
897 * s->session - The ssl session has been setup.
898 * s->hit - session reuse flag
899 * s->tmp.new_cipher - the new cipher to use.
906 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
909 if (ciphers
!= NULL
) sk_SSL_CIPHER_free(ciphers
);
913 static int ssl3_send_server_hello(SSL
*s
)
918 unsigned long l
,Time
;
920 if (s
->state
== SSL3_ST_SW_SRVR_HELLO_A
)
922 buf
=(unsigned char *)s
->init_buf
->data
;
923 p
=s
->s3
->server_random
;
924 Time
=time(NULL
); /* Time */
926 RAND_pseudo_bytes(p
,SSL3_RANDOM_SIZE
-sizeof(Time
));
927 /* Do the message type and length last */
930 *(p
++)=s
->version
>>8;
931 *(p
++)=s
->version
&0xff;
934 memcpy(p
,s
->s3
->server_random
,SSL3_RANDOM_SIZE
);
937 /* now in theory we have 3 options to sending back the
938 * session id. If it is a re-use, we send back the
939 * old session-id, if it is a new session, we send
940 * back the new session-id or we send back a 0 length
941 * session-id if we want it to be single use.
942 * Currently I will not implement the '0' length session-id
943 * 12-Jan-98 - I'll now support the '0' length stuff.
945 if (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
))
946 s
->session
->session_id_length
=0;
948 sl
=s
->session
->session_id_length
;
950 memcpy(p
,s
->session
->session_id
,sl
);
954 i
=ssl3_put_cipher_by_char(s
->s3
->tmp
.new_cipher
,p
);
957 /* put the compression method */
958 if (s
->s3
->tmp
.new_compression
== NULL
)
961 *(p
++)=s
->s3
->tmp
.new_compression
->id
;
966 *(d
++)=SSL3_MT_SERVER_HELLO
;
969 s
->state
=SSL3_ST_CW_CLNT_HELLO_B
;
970 /* number of bytes to write */
975 /* SSL3_ST_CW_CLNT_HELLO_B */
976 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
979 static int ssl3_send_server_done(SSL
*s
)
983 if (s
->state
== SSL3_ST_SW_SRVR_DONE_A
)
985 p
=(unsigned char *)s
->init_buf
->data
;
988 *(p
++)=SSL3_MT_SERVER_DONE
;
993 s
->state
=SSL3_ST_SW_SRVR_DONE_B
;
994 /* number of bytes to write */
999 /* SSL3_ST_CW_CLNT_HELLO_B */
1000 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1003 static int ssl3_send_server_key_exchange(SSL
*s
)
1005 #ifndef OPENSSL_NO_RSA
1009 unsigned char md_buf
[MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
];
1012 #ifndef OPENSSL_NO_DH
1016 unsigned char *p
,*d
;
1026 EVP_MD_CTX_init(&md_ctx
);
1027 if (s
->state
== SSL3_ST_SW_KEY_EXCH_A
)
1029 type
=s
->s3
->tmp
.new_cipher
->algorithms
& SSL_MKEY_MASK
;
1034 r
[0]=r
[1]=r
[2]=r
[3]=NULL
;
1036 #ifndef OPENSSL_NO_RSA
1037 if (type
& SSL_kRSA
)
1040 if ((rsa
== NULL
) && (s
->cert
->rsa_tmp_cb
!= NULL
))
1042 rsa
=s
->cert
->rsa_tmp_cb(s
,
1043 SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
),
1044 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
));
1047 al
=SSL_AD_HANDSHAKE_FAILURE
;
1048 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_ERROR_GENERATING_TMP_RSA_KEY
);
1056 al
=SSL_AD_HANDSHAKE_FAILURE
;
1057 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_RSA_KEY
);
1062 s
->s3
->tmp
.use_rsa_tmp
=1;
1066 #ifndef OPENSSL_NO_DH
1067 if (type
& SSL_kEDH
)
1070 if ((dhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
))
1071 dhp
=s
->cert
->dh_tmp_cb(s
,
1072 SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
),
1073 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
));
1076 al
=SSL_AD_HANDSHAKE_FAILURE
;
1077 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
1081 if (s
->s3
->tmp
.dh
!= NULL
)
1084 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1088 if ((dh
=DHparams_dup(dhp
)) == NULL
)
1090 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1095 if ((dhp
->pub_key
== NULL
||
1096 dhp
->priv_key
== NULL
||
1097 (s
->options
& SSL_OP_SINGLE_DH_USE
)))
1099 if(!DH_generate_key(dh
))
1101 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1108 dh
->pub_key
=BN_dup(dhp
->pub_key
);
1109 dh
->priv_key
=BN_dup(dhp
->priv_key
);
1110 if ((dh
->pub_key
== NULL
) ||
1111 (dh
->priv_key
== NULL
))
1113 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1124 al
=SSL_AD_HANDSHAKE_FAILURE
;
1125 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1128 for (i
=0; r
[i
] != NULL
; i
++)
1130 nr
[i
]=BN_num_bytes(r
[i
]);
1134 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
))
1136 if ((pkey
=ssl_get_sign_pkey(s
,s
->s3
->tmp
.new_cipher
))
1139 al
=SSL_AD_DECODE_ERROR
;
1142 kn
=EVP_PKEY_size(pkey
);
1150 if (!BUF_MEM_grow(buf
,n
+4+kn
))
1152 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_BUF
);
1155 d
=(unsigned char *)s
->init_buf
->data
;
1158 for (i
=0; r
[i
] != NULL
; i
++)
1168 /* n is the length of the params, they start at &(d[4])
1169 * and p points to the space at the end. */
1170 #ifndef OPENSSL_NO_RSA
1171 if (pkey
->type
== EVP_PKEY_RSA
)
1175 for (num
=2; num
> 0; num
--)
1177 EVP_DigestInit_ex(&md_ctx
,(num
== 2)
1178 ?s
->ctx
->md5
:s
->ctx
->sha1
, NULL
);
1179 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1180 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1181 EVP_DigestUpdate(&md_ctx
,&(d
[4]),n
);
1182 EVP_DigestFinal_ex(&md_ctx
,q
,
1183 (unsigned int *)&i
);
1187 if (RSA_sign(NID_md5_sha1
, md_buf
, j
,
1188 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0)
1190 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_RSA
);
1198 #if !defined(OPENSSL_NO_DSA)
1199 if (pkey
->type
== EVP_PKEY_DSA
)
1202 EVP_SignInit_ex(&md_ctx
,EVP_dss1(), NULL
);
1203 EVP_SignUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1204 EVP_SignUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1205 EVP_SignUpdate(&md_ctx
,&(d
[4]),n
);
1206 if (!EVP_SignFinal(&md_ctx
,&(p
[2]),
1207 (unsigned int *)&i
,pkey
))
1209 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_DSA
);
1218 /* Is this error check actually needed? */
1219 al
=SSL_AD_HANDSHAKE_FAILURE
;
1220 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNKNOWN_PKEY_TYPE
);
1225 *(d
++)=SSL3_MT_SERVER_KEY_EXCHANGE
;
1228 /* we should now have things packed up, so lets send
1234 s
->state
= SSL3_ST_SW_KEY_EXCH_B
;
1235 EVP_MD_CTX_cleanup(&md_ctx
);
1236 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1238 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1240 EVP_MD_CTX_cleanup(&md_ctx
);
1244 static int ssl3_send_certificate_request(SSL
*s
)
1246 unsigned char *p
,*d
;
1248 STACK_OF(X509_NAME
) *sk
=NULL
;
1252 if (s
->state
== SSL3_ST_SW_CERT_REQ_A
)
1256 d
=p
=(unsigned char *)&(buf
->data
[4]);
1258 /* get the list of acceptable cert types */
1260 n
=ssl3_get_req_cert_type(s
,p
);
1269 sk
=SSL_get_client_CA_list(s
);
1273 for (i
=0; i
<sk_X509_NAME_num(sk
); i
++)
1275 name
=sk_X509_NAME_value(sk
,i
);
1276 j
=i2d_X509_NAME(name
,NULL
);
1277 if (!BUF_MEM_grow(buf
,4+n
+j
+2))
1279 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST
,ERR_R_BUF_LIB
);
1282 p
=(unsigned char *)&(buf
->data
[4+n
]);
1283 if (!(s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
))
1286 i2d_X509_NAME(name
,&p
);
1293 i2d_X509_NAME(name
,&p
);
1294 j
-=2; s2n(j
,d
); j
+=2;
1300 /* else no CA names */
1301 p
=(unsigned char *)&(buf
->data
[4+off
]);
1304 d
=(unsigned char *)buf
->data
;
1305 *(d
++)=SSL3_MT_CERTIFICATE_REQUEST
;
1308 /* we should now have things packed up, so lets send
1313 #ifdef NETSCAPE_HANG_BUG
1314 p
=(unsigned char *)s
->init_buf
->data
+ s
->init_num
;
1317 *(p
++)=SSL3_MT_SERVER_DONE
;
1326 /* SSL3_ST_SW_CERT_REQ_B */
1327 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1332 static int ssl3_get_client_key_exchange(SSL
*s
)
1338 #ifndef OPENSSL_NO_RSA
1340 EVP_PKEY
*pkey
=NULL
;
1342 #ifndef OPENSSL_NO_DH
1346 #ifndef OPENSSL_NO_KRB5
1348 #endif /* OPENSSL_NO_KRB5 */
1350 n
=ssl3_get_message(s
,
1351 SSL3_ST_SR_KEY_EXCH_A
,
1352 SSL3_ST_SR_KEY_EXCH_B
,
1353 SSL3_MT_CLIENT_KEY_EXCHANGE
,
1357 if (!ok
) return((int)n
);
1358 p
=(unsigned char *)s
->init_msg
;
1360 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
1362 #ifndef OPENSSL_NO_RSA
1365 /* FIX THIS UP EAY EAY EAY EAY */
1366 if (s
->s3
->tmp
.use_rsa_tmp
)
1368 if ((s
->cert
!= NULL
) && (s
->cert
->rsa_tmp
!= NULL
))
1369 rsa
=s
->cert
->rsa_tmp
;
1370 /* Don't do a callback because rsa_tmp should
1371 * be sent already */
1374 al
=SSL_AD_HANDSHAKE_FAILURE
;
1375 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_RSA_PKEY
);
1382 pkey
=s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
;
1383 if ( (pkey
== NULL
) ||
1384 (pkey
->type
!= EVP_PKEY_RSA
) ||
1385 (pkey
->pkey
.rsa
== NULL
))
1387 al
=SSL_AD_HANDSHAKE_FAILURE
;
1388 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_RSA_CERTIFICATE
);
1395 if (s
->version
> SSL3_VERSION
)
1400 if (!(s
->options
& SSL_OP_TLS_D5_BUG
))
1402 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG
);
1412 i
=RSA_private_decrypt((int)n
,p
,p
,rsa
,RSA_PKCS1_PADDING
);
1416 if (i
!= SSL_MAX_MASTER_KEY_LENGTH
)
1418 al
=SSL_AD_DECODE_ERROR
;
1419 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_RSA_DECRYPT
);
1422 if ((al
== -1) && !((p
[0] == (s
->client_version
>>8)) && (p
[1] == (s
->client_version
& 0xff))))
1424 /* The premaster secret must contain the same version number as the
1425 * ClientHello to detect version rollback attacks (strangely, the
1426 * protocol does not offer such protection for DH ciphersuites).
1427 * However, buggy clients exist that send the negotiated protocol
1428 * version instead if the server does not support the requested
1430 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1431 if (!((s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) &&
1432 (p
[0] == (s
->version
>>8)) && (p
[1] == (s
->version
& 0xff))))
1434 al
=SSL_AD_DECODE_ERROR
;
1435 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_PROTOCOL_VERSION_NUMBER
);
1445 /* Some decryption failure -- use random value instead as countermeasure
1446 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1447 * (see RFC 2246, section 7.4.7.1).
1448 * But note that due to length and protocol version checking, the
1449 * attack is impractical anyway (see section 5 in D. Bleichenbacher:
1450 * "Chosen Ciphertext Attacks Against Protocols Based on the RSA
1451 * Encryption Standard PKCS #1", CRYPTO '98, LNCS 1462, pp. 1-12).
1454 i
= SSL_MAX_MASTER_KEY_LENGTH
;
1455 p
[0] = s
->client_version
>> 8;
1456 p
[1] = s
->client_version
& 0xff;
1457 RAND_pseudo_bytes(p
+2, i
-2); /* should be RAND_bytes, but we cannot work around a failure */
1461 s
->session
->master_key_length
=
1462 s
->method
->ssl3_enc
->generate_master_secret(s
,
1463 s
->session
->master_key
,
1469 #ifndef OPENSSL_NO_DH
1470 if (l
& (SSL_kEDH
|SSL_kDHr
|SSL_kDHd
))
1475 if (!(s
->options
& SSL_OP_SSLEAY_080_CLIENT_DH_BUG
))
1477 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
1487 if (n
== 0L) /* the parameters are in the cert */
1489 al
=SSL_AD_HANDSHAKE_FAILURE
;
1490 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_UNABLE_TO_DECODE_DH_CERTS
);
1495 if (s
->s3
->tmp
.dh
== NULL
)
1497 al
=SSL_AD_HANDSHAKE_FAILURE
;
1498 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
1502 dh_srvr
=s
->s3
->tmp
.dh
;
1505 pub
=BN_bin2bn(p
,i
,NULL
);
1508 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BN_LIB
);
1512 i
=DH_compute_key(p
,pub
,dh_srvr
);
1516 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1520 DH_free(s
->s3
->tmp
.dh
);
1525 s
->session
->master_key_length
=
1526 s
->method
->ssl3_enc
->generate_master_secret(s
,
1527 s
->session
->master_key
,p
,i
);
1532 #ifndef OPENSSL_NO_KRB5
1535 krb5_error_code krb5rc
;
1536 krb5_data enc_ticket
;
1537 krb5_data authenticator
;
1539 KSSL_CTX
*kssl_ctx
= s
->kssl_ctx
;
1540 EVP_CIPHER_CTX ciph_ctx
;
1541 EVP_CIPHER
*enc
= NULL
;
1542 unsigned char iv
[EVP_MAX_IV_LENGTH
];
1543 unsigned char pms
[SSL_MAX_MASTER_KEY_LENGTH
1544 + EVP_MAX_IV_LENGTH
+ 1];
1545 int padl
, outl
= sizeof(pms
);
1546 krb5_timestamp authtime
= 0;
1547 krb5_ticket_times ttimes
;
1549 if (!kssl_ctx
) kssl_ctx
= kssl_ctx_new();
1552 enc_ticket
.length
= i
;
1553 enc_ticket
.data
= p
;
1554 p
+=enc_ticket
.length
;
1557 authenticator
.length
= i
;
1558 authenticator
.data
= p
;
1559 p
+=authenticator
.length
;
1566 if ((unsigned long)n
!= enc_ticket
.length
+ authenticator
.length
+
1569 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1570 SSL_R_DATA_LENGTH_TOO_LONG
);
1574 if ((krb5rc
= kssl_sget_tkt(kssl_ctx
, &enc_ticket
, &ttimes
,
1578 printf("kssl_sget_tkt rtn %d [%d]\n",
1579 krb5rc
, kssl_err
.reason
);
1581 printf("kssl_err text= %s\n", kssl_err
.text
);
1582 #endif /* KSSL_DEBUG */
1583 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1588 /* Note: no authenticator is not considered an error,
1589 ** but will return authtime == 0.
1591 if ((krb5rc
= kssl_check_authent(kssl_ctx
, &authenticator
,
1592 &authtime
, &kssl_err
)) != 0)
1595 printf("kssl_check_authent rtn %d [%d]\n",
1596 krb5rc
, kssl_err
.reason
);
1598 printf("kssl_err text= %s\n", kssl_err
.text
);
1599 #endif /* KSSL_DEBUG */
1600 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1605 if ((krb5rc
= kssl_validate_times(authtime
, &ttimes
)) != 0)
1607 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
, krb5rc
);
1612 kssl_ctx_show(kssl_ctx
);
1613 #endif /* KSSL_DEBUG */
1615 enc
= kssl_map_enc(kssl_ctx
->enctype
);
1619 memset(iv
, 0, EVP_MAX_IV_LENGTH
); /* per RFC 1510 */
1621 if (!EVP_DecryptInit_ex(&ciph_ctx
,enc
,kssl_ctx
->key
,iv
))
1623 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1624 SSL_R_DECRYPTION_FAILED
);
1627 if (!EVP_DecryptUpdate(&ciph_ctx
, pms
,&outl
,
1628 enc_pms
.data
, enc_pms
.length
))
1630 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1631 SSL_R_DECRYPTION_FAILED
);
1634 if (outl
> SSL_MAX_MASTER_KEY_LENGTH
)
1636 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1637 SSL_R_DATA_LENGTH_TOO_LONG
);
1640 if (!EVP_DecryptFinal_ex(&ciph_ctx
,&(pms
[outl
]),&padl
))
1642 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1643 SSL_R_DECRYPTION_FAILED
);
1647 if (outl
> SSL_MAX_MASTER_KEY_LENGTH
)
1649 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1650 SSL_R_DATA_LENGTH_TOO_LONG
);
1653 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
1655 s
->session
->master_key_length
=
1656 s
->method
->ssl3_enc
->generate_master_secret(s
,
1657 s
->session
->master_key
, pms
, outl
);
1659 if (kssl_ctx
->client_princ
)
1661 int len
= strlen(kssl_ctx
->client_princ
);
1662 if ( len
< SSL_MAX_KRB5_PRINCIPAL_LENGTH
)
1664 s
->session
->krb5_client_princ_len
= len
;
1665 memcpy(s
->session
->krb5_client_princ
,kssl_ctx
->client_princ
,len
);
1670 /* Was doing kssl_ctx_free() here,
1671 ** but it caused problems for apache.
1672 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
1673 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
1677 #endif /* OPENSSL_NO_KRB5 */
1679 al
=SSL_AD_HANDSHAKE_FAILURE
;
1680 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
1681 SSL_R_UNKNOWN_CIPHER_TYPE
);
1687 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1688 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA)
1694 static int ssl3_get_cert_verify(SSL
*s
)
1696 EVP_PKEY
*pkey
=NULL
;
1703 n
=ssl3_get_message(s
,
1704 SSL3_ST_SR_CERT_VRFY_A
,
1705 SSL3_ST_SR_CERT_VRFY_B
,
1710 if (!ok
) return((int)n
);
1712 if (s
->session
->peer
!= NULL
)
1714 peer
=s
->session
->peer
;
1715 pkey
=X509_get_pubkey(peer
);
1716 type
=X509_certificate_type(peer
,pkey
);
1724 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_VERIFY
)
1726 s
->s3
->tmp
.reuse_message
=1;
1727 if ((peer
!= NULL
) && (type
| EVP_PKT_SIGN
))
1729 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1730 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_MISSING_VERIFY_MESSAGE
);
1739 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_NO_CLIENT_CERT_RECEIVED
);
1740 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1744 if (!(type
& EVP_PKT_SIGN
))
1746 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
1747 al
=SSL_AD_ILLEGAL_PARAMETER
;
1751 if (s
->s3
->change_cipher_spec
)
1753 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_CCS_RECEIVED_EARLY
);
1754 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1758 /* we now have a signature that we need to verify */
1759 p
=(unsigned char *)s
->init_msg
;
1764 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_LENGTH_MISMATCH
);
1765 al
=SSL_AD_DECODE_ERROR
;
1769 j
=EVP_PKEY_size(pkey
);
1770 if ((i
> j
) || (n
> j
) || (n
<= 0))
1772 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_WRONG_SIGNATURE_SIZE
);
1773 al
=SSL_AD_DECODE_ERROR
;
1777 #ifndef OPENSSL_NO_RSA
1778 if (pkey
->type
== EVP_PKEY_RSA
)
1780 i
=RSA_verify(NID_md5_sha1
, s
->s3
->tmp
.cert_verify_md
,
1781 MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
, p
, i
,
1785 al
=SSL_AD_DECRYPT_ERROR
;
1786 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_DECRYPT
);
1791 al
=SSL_AD_DECRYPT_ERROR
;
1792 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_SIGNATURE
);
1798 #ifndef OPENSSL_NO_DSA
1799 if (pkey
->type
== EVP_PKEY_DSA
)
1801 j
=DSA_verify(pkey
->save_type
,
1802 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
1803 SHA_DIGEST_LENGTH
,p
,i
,pkey
->pkey
.dsa
);
1807 al
=SSL_AD_DECRYPT_ERROR
;
1808 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_DSA_SIGNATURE
);
1815 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,ERR_R_INTERNAL_ERROR
);
1816 al
=SSL_AD_UNSUPPORTED_CERTIFICATE
;
1825 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1828 EVP_PKEY_free(pkey
);
1832 static int ssl3_get_client_certificate(SSL
*s
)
1834 int i
,ok
,al
,ret
= -1;
1836 unsigned long l
,nc
,llen
,n
;
1837 unsigned char *p
,*d
,*q
;
1838 STACK_OF(X509
) *sk
=NULL
;
1840 n
=ssl3_get_message(s
,
1847 if (!ok
) return((int)n
);
1849 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_KEY_EXCHANGE
)
1851 if ( (s
->verify_mode
& SSL_VERIFY_PEER
) &&
1852 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
1854 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
1855 al
=SSL_AD_HANDSHAKE_FAILURE
;
1858 /* If tls asked for a client cert, the client must return a 0 list */
1859 if ((s
->version
> SSL3_VERSION
) && s
->s3
->tmp
.cert_request
)
1861 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
);
1862 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1865 s
->s3
->tmp
.reuse_message
=1;
1869 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
)
1871 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1872 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_WRONG_MESSAGE_TYPE
);
1875 d
=p
=(unsigned char *)s
->init_msg
;
1877 if ((sk
=sk_X509_new_null()) == NULL
)
1879 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
1886 al
=SSL_AD_DECODE_ERROR
;
1887 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_LENGTH_MISMATCH
);
1890 for (nc
=0; nc
<llen
; )
1893 if ((l
+nc
+3) > llen
)
1895 al
=SSL_AD_DECODE_ERROR
;
1896 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
1901 x
=d2i_X509(NULL
,&p
,l
);
1904 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_ASN1_LIB
);
1909 al
=SSL_AD_DECODE_ERROR
;
1910 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
1913 if (!sk_X509_push(sk
,x
))
1915 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
1922 if (sk_X509_num(sk
) <= 0)
1924 /* TLS does not mind 0 certs returned */
1925 if (s
->version
== SSL3_VERSION
)
1927 al
=SSL_AD_HANDSHAKE_FAILURE
;
1928 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_NO_CERTIFICATES_RETURNED
);
1931 /* Fail for TLS only if we required a certificate */
1932 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
1933 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
1935 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
1936 al
=SSL_AD_HANDSHAKE_FAILURE
;
1942 i
=ssl_verify_cert_chain(s
,sk
);
1945 al
=ssl_verify_alarm_type(s
->verify_result
);
1946 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_NO_CERTIFICATE_RETURNED
);
1951 if (s
->session
->peer
!= NULL
) /* This should not be needed */
1952 X509_free(s
->session
->peer
);
1953 s
->session
->peer
=sk_X509_shift(sk
);
1954 s
->session
->verify_result
= s
->verify_result
;
1956 /* With the current implementation, sess_cert will always be NULL
1957 * when we arrive here. */
1958 if (s
->session
->sess_cert
== NULL
)
1960 s
->session
->sess_cert
= ssl_sess_cert_new();
1961 if (s
->session
->sess_cert
== NULL
)
1963 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
1967 if (s
->session
->sess_cert
->cert_chain
!= NULL
)
1968 sk_X509_pop_free(s
->session
->sess_cert
->cert_chain
, X509_free
);
1969 s
->session
->sess_cert
->cert_chain
=sk
;
1970 /* Inconsistency alert: cert_chain does *not* include the
1971 * peer's own certificate, while we do include it in s3_clnt.c */
1979 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1982 if (x
!= NULL
) X509_free(x
);
1983 if (sk
!= NULL
) sk_X509_pop_free(sk
,X509_free
);
1987 int ssl3_send_server_certificate(SSL
*s
)
1992 if (s
->state
== SSL3_ST_SW_CERT_A
)
1994 x
=ssl_get_server_send_cert(s
);
1996 /* VRS: allow null cert if auth == KRB5 */
1997 (s
->s3
->tmp
.new_cipher
->algorithms
1998 & (SSL_MKEY_MASK
|SSL_AUTH_MASK
))
1999 != (SSL_aKRB5
|SSL_kKRB5
))
2001 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE
,ERR_R_INTERNAL_ERROR
);
2005 l
=ssl3_output_cert_chain(s
,x
);
2006 s
->state
=SSL3_ST_SW_CERT_B
;
2011 /* SSL3_ST_SW_CERT_B */
2012 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));