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.]
59 #define REUSE_CIPHER_BUG
60 #define NETSCAPE_HANG_BUG
64 #include <openssl/buffer.h>
65 #include <openssl/rand.h>
66 #include <openssl/objects.h>
67 #include <openssl/md5.h>
68 #include <openssl/sha.h>
69 #include <openssl/evp.h>
70 #include <openssl/x509.h>
77 static SSL_METHOD
*ssl3_get_server_method(int ver
);
78 static int ssl3_get_client_hello(SSL
*s
);
79 static int ssl3_check_client_hello(SSL
*s
);
80 static int ssl3_send_server_hello(SSL
*s
);
81 static int ssl3_send_server_key_exchange(SSL
*s
);
82 static int ssl3_send_certificate_request(SSL
*s
);
83 static int ssl3_send_server_done(SSL
*s
);
84 static int ssl3_get_client_key_exchange(SSL
*s
);
85 static int ssl3_get_client_certificate(SSL
*s
);
86 static int ssl3_get_cert_verify(SSL
*s
);
87 static int ssl3_send_hello_request(SSL
*s
);
89 static SSL_METHOD
*ssl3_get_server_method(int ver
)
91 if (ver
== SSL3_VERSION
)
92 return(SSLv3_server_method());
97 SSL_METHOD
*SSLv3_server_method(void)
100 static SSL_METHOD SSLv3_server_data
;
104 memcpy((char *)&SSLv3_server_data
,(char *)sslv3_base_method(),
106 SSLv3_server_data
.ssl_accept
=ssl3_accept
;
107 SSLv3_server_data
.get_ssl_method
=ssl3_get_server_method
;
110 return(&SSLv3_server_data
);
113 int ssl3_accept(SSL
*s
)
116 unsigned long l
,Time
=time(NULL
);
120 int new_state
,state
,skip
=0;
122 RAND_add(&Time
,sizeof(Time
),0);
126 if (s
->info_callback
!= NULL
)
128 else if (s
->ctx
->info_callback
!= NULL
)
129 cb
=s
->ctx
->info_callback
;
131 /* init things to blank */
132 if (!SSL_in_init(s
) || SSL_in_before(s
)) SSL_clear(s
);
137 SSLerr(SSL_F_SSL3_ACCEPT
,SSL_R_NO_CERTIFICATE_SET
);
147 case SSL_ST_RENEGOTIATE
:
149 /* s->state=SSL_ST_ACCEPT; */
153 case SSL_ST_BEFORE
|SSL_ST_ACCEPT
:
154 case SSL_ST_OK
|SSL_ST_ACCEPT
:
157 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_START
,1);
159 if ((s
->version
>>8) != 3)
161 SSLerr(SSL_F_SSL3_ACCEPT
, SSL_R_INTERNAL_ERROR
);
164 s
->type
=SSL_ST_ACCEPT
;
166 if (s
->init_buf
== NULL
)
168 if ((buf
=BUF_MEM_new()) == NULL
)
173 if (!BUF_MEM_grow(buf
,SSL3_RT_MAX_PLAIN_LENGTH
))
181 if (!ssl3_setup_buffers(s
))
187 /* Ok, we now need to push on a buffering BIO so that
188 * the output is sent in a way that TCP likes :-)
190 if (!ssl_init_wbio_buffer(s
,1)) { ret
= -1; goto end
; }
194 if (s
->state
!= SSL_ST_RENEGOTIATE
)
196 ssl3_init_finished_mac(s
);
197 s
->state
=SSL3_ST_SR_CLNT_HELLO_A
;
198 s
->ctx
->stats
.sess_accept
++;
202 s
->ctx
->stats
.sess_accept_renegotiate
++;
203 s
->state
=SSL3_ST_SW_HELLO_REQ_A
;
207 case SSL3_ST_SW_HELLO_REQ_A
:
208 case SSL3_ST_SW_HELLO_REQ_B
:
211 ret
=ssl3_send_hello_request(s
);
212 if (ret
<= 0) goto end
;
213 s
->s3
->tmp
.next_state
=SSL3_ST_SW_HELLO_REQ_C
;
214 s
->state
=SSL3_ST_SW_FLUSH
;
217 ssl3_init_finished_mac(s
);
220 case SSL3_ST_SW_HELLO_REQ_C
:
226 case SSL3_ST_SR_CLNT_HELLO_A
:
227 case SSL3_ST_SR_CLNT_HELLO_B
:
228 case SSL3_ST_SR_CLNT_HELLO_C
:
231 ret
=ssl3_get_client_hello(s
);
232 if (ret
<= 0) goto end
;
233 s
->state
=SSL3_ST_SW_SRVR_HELLO_A
;
237 case SSL3_ST_SW_SRVR_HELLO_A
:
238 case SSL3_ST_SW_SRVR_HELLO_B
:
239 ret
=ssl3_send_server_hello(s
);
240 if (ret
<= 0) goto end
;
243 s
->state
=SSL3_ST_SW_CHANGE_A
;
245 s
->state
=SSL3_ST_SW_CERT_A
;
249 case SSL3_ST_SW_CERT_A
:
250 case SSL3_ST_SW_CERT_B
:
251 /* Check if it is anon DH */
252 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
))
254 ret
=ssl3_send_server_certificate(s
);
255 if (ret
<= 0) goto end
;
259 s
->state
=SSL3_ST_SW_KEY_EXCH_A
;
263 case SSL3_ST_SW_KEY_EXCH_A
:
264 case SSL3_ST_SW_KEY_EXCH_B
:
265 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
267 /* clear this, it may get reset by
268 * send_server_key_exchange */
269 if ((s
->options
& SSL_OP_EPHEMERAL_RSA
)
274 s
->s3
->tmp
.use_rsa_tmp
=1;
276 s
->s3
->tmp
.use_rsa_tmp
=0;
278 /* only send if a DH key exchange, fortezza or
279 * RSA but we have a sign only certificate */
280 if (s
->s3
->tmp
.use_rsa_tmp
281 || (l
& (SSL_DH
|SSL_kFZA
))
283 && (s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
== NULL
284 || (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
)
285 && EVP_PKEY_size(s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
)*8 > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)
291 ret
=ssl3_send_server_key_exchange(s
);
292 if (ret
<= 0) goto end
;
297 s
->state
=SSL3_ST_SW_CERT_REQ_A
;
301 case SSL3_ST_SW_CERT_REQ_A
:
302 case SSL3_ST_SW_CERT_REQ_B
:
303 if (/* don't request cert unless asked for it: */
304 !(s
->verify_mode
& SSL_VERIFY_PEER
) ||
305 /* if SSL_VERIFY_CLIENT_ONCE is set,
306 * don't request cert during re-negotiation: */
307 ((s
->session
->peer
!= NULL
) &&
308 (s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
)) ||
309 /* never request cert in anonymous ciphersuites
310 * (see section "Certificate request" in SSL 3 drafts
311 * and in RFC 2246): */
312 ((s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
) &&
313 /* ... except when the application insists on verification
314 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
315 !(s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)))
317 /* no cert request */
319 s
->s3
->tmp
.cert_request
=0;
320 s
->state
=SSL3_ST_SW_SRVR_DONE_A
;
324 s
->s3
->tmp
.cert_request
=1;
325 ret
=ssl3_send_certificate_request(s
);
326 if (ret
<= 0) goto end
;
327 #ifndef NETSCAPE_HANG_BUG
328 s
->state
=SSL3_ST_SW_SRVR_DONE_A
;
330 s
->state
=SSL3_ST_SW_FLUSH
;
331 s
->s3
->tmp
.next_state
=SSL3_ST_SR_CERT_A
;
337 case SSL3_ST_SW_SRVR_DONE_A
:
338 case SSL3_ST_SW_SRVR_DONE_B
:
339 ret
=ssl3_send_server_done(s
);
340 if (ret
<= 0) goto end
;
341 s
->s3
->tmp
.next_state
=SSL3_ST_SR_CERT_A
;
342 s
->state
=SSL3_ST_SW_FLUSH
;
346 case SSL3_ST_SW_FLUSH
:
347 /* number of bytes to be flushed */
348 num1
=BIO_ctrl(s
->wbio
,BIO_CTRL_INFO
,0,NULL
);
351 s
->rwstate
=SSL_WRITING
;
352 num1
=BIO_flush(s
->wbio
);
353 if (num1
<= 0) { ret
= -1; goto end
; }
354 s
->rwstate
=SSL_NOTHING
;
357 s
->state
=s
->s3
->tmp
.next_state
;
360 case SSL3_ST_SR_CERT_A
:
361 case SSL3_ST_SR_CERT_B
:
362 /* Check for second client hello (MS SGC) */
363 ret
= ssl3_check_client_hello(s
);
367 s
->state
= SSL3_ST_SR_CLNT_HELLO_C
;
369 /* could be sent for a DH cert, even if we
370 * have not asked for it :-) */
371 ret
=ssl3_get_client_certificate(s
);
372 if (ret
<= 0) goto end
;
374 s
->state
=SSL3_ST_SR_KEY_EXCH_A
;
378 case SSL3_ST_SR_KEY_EXCH_A
:
379 case SSL3_ST_SR_KEY_EXCH_B
:
380 ret
=ssl3_get_client_key_exchange(s
);
381 if (ret
<= 0) goto end
;
382 s
->state
=SSL3_ST_SR_CERT_VRFY_A
;
385 /* We need to get hashes here so if there is
386 * a client cert, it can be verified */
387 s
->method
->ssl3_enc
->cert_verify_mac(s
,
388 &(s
->s3
->finish_dgst1
),
389 &(s
->s3
->tmp
.cert_verify_md
[0]));
390 s
->method
->ssl3_enc
->cert_verify_mac(s
,
391 &(s
->s3
->finish_dgst2
),
392 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]));
396 case SSL3_ST_SR_CERT_VRFY_A
:
397 case SSL3_ST_SR_CERT_VRFY_B
:
399 /* we should decide if we expected this one */
400 ret
=ssl3_get_cert_verify(s
);
401 if (ret
<= 0) goto end
;
403 s
->state
=SSL3_ST_SR_FINISHED_A
;
407 case SSL3_ST_SR_FINISHED_A
:
408 case SSL3_ST_SR_FINISHED_B
:
409 ret
=ssl3_get_finished(s
,SSL3_ST_SR_FINISHED_A
,
410 SSL3_ST_SR_FINISHED_B
);
411 if (ret
<= 0) goto end
;
415 s
->state
=SSL3_ST_SW_CHANGE_A
;
419 case SSL3_ST_SW_CHANGE_A
:
420 case SSL3_ST_SW_CHANGE_B
:
422 s
->session
->cipher
=s
->s3
->tmp
.new_cipher
;
423 if (!s
->method
->ssl3_enc
->setup_key_block(s
))
424 { ret
= -1; goto end
; }
426 ret
=ssl3_send_change_cipher_spec(s
,
427 SSL3_ST_SW_CHANGE_A
,SSL3_ST_SW_CHANGE_B
);
429 if (ret
<= 0) goto end
;
430 s
->state
=SSL3_ST_SW_FINISHED_A
;
433 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
434 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
442 case SSL3_ST_SW_FINISHED_A
:
443 case SSL3_ST_SW_FINISHED_B
:
444 ret
=ssl3_send_finished(s
,
445 SSL3_ST_SW_FINISHED_A
,SSL3_ST_SW_FINISHED_B
,
446 s
->method
->ssl3_enc
->server_finished_label
,
447 s
->method
->ssl3_enc
->server_finished_label_len
);
448 if (ret
<= 0) goto end
;
449 s
->state
=SSL3_ST_SW_FLUSH
;
451 s
->s3
->tmp
.next_state
=SSL3_ST_SR_FINISHED_A
;
453 s
->s3
->tmp
.next_state
=SSL_ST_OK
;
458 /* clean a few things up */
459 ssl3_cleanup_key_block(s
);
461 BUF_MEM_free(s
->init_buf
);
464 /* remove buffering on output */
465 ssl_free_wbio_buffer(s
);
470 ssl_update_cache(s
,SSL_SESS_CACHE_SERVER
);
472 s
->ctx
->stats
.sess_accept_good
++;
474 s
->handshake_func
=ssl3_accept
;
477 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_DONE
,1);
483 SSLerr(SSL_F_SSL3_ACCEPT
,SSL_R_UNKNOWN_STATE
);
489 if (!s
->s3
->tmp
.reuse_message
&& !skip
)
493 if ((ret
=BIO_flush(s
->wbio
)) <= 0)
498 if ((cb
!= NULL
) && (s
->state
!= state
))
502 cb(s
,SSL_CB_ACCEPT_LOOP
,1);
509 /* BIO_flush(s->wbio); */
512 cb(s
,SSL_CB_ACCEPT_EXIT
,ret
);
517 static int ssl3_send_hello_request(SSL
*s
)
521 if (s
->state
== SSL3_ST_SW_HELLO_REQ_A
)
523 p
=(unsigned char *)s
->init_buf
->data
;
524 *(p
++)=SSL3_MT_HELLO_REQUEST
;
529 s
->state
=SSL3_ST_SW_HELLO_REQ_B
;
530 /* number of bytes to write */
535 /* SSL3_ST_SW_HELLO_REQ_B */
536 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
539 static int ssl3_check_client_hello(SSL
*s
)
544 n
=ssl3_get_message(s
,
548 SSL3_RT_MAX_PLAIN_LENGTH
,
550 if (!ok
) return((int)n
);
551 s
->s3
->tmp
.reuse_message
= 1;
552 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_HELLO
)
554 /* Throw away what we have done so far in the current handshake,
555 * which will now be aborted. (A full SSL_clear would be too much.)
556 * I hope that tmp.dh is the only thing that may need to be cleared
557 * when a handshake is not completed ... */
559 if (s
->s3
->tmp
.dh
!= NULL
)
561 DH_free(s
->s3
->tmp
.dh
);
562 s
->s3
->tmp
.dh
= NULL
;
570 static int ssl3_get_client_hello(SSL
*s
)
572 int i
,j
,ok
,al
,ret
= -1;
575 unsigned char *p
,*d
,*q
;
578 STACK_OF(SSL_CIPHER
) *ciphers
=NULL
;
580 /* We do this so that we will respond with our native type.
581 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
582 * This down switching should be handled by a different method.
583 * If we are SSLv3, we will respond with SSLv3, even if prompted with
586 if (s
->state
== SSL3_ST_SR_CLNT_HELLO_A
)
589 s
->state
=SSL3_ST_SR_CLNT_HELLO_B
;
591 n
=ssl3_get_message(s
,
592 SSL3_ST_SR_CLNT_HELLO_B
,
593 SSL3_ST_SR_CLNT_HELLO_C
,
594 SSL3_MT_CLIENT_HELLO
,
595 SSL3_RT_MAX_PLAIN_LENGTH
,
598 if (!ok
) return((int)n
);
599 d
=p
=(unsigned char *)s
->init_buf
->data
;
601 /* use version from inside client hello, not from record header
602 * (may differ: see RFC 2246, Appendix E, second paragraph) */
603 s
->client_version
=(((int)p
[0])<<8)|(int)p
[1];
606 /* load the client random */
607 memcpy(s
->s3
->client_random
,p
,SSL3_RANDOM_SIZE
);
610 /* get the session-id */
616 if (!ssl_get_new_session(s
,1))
621 i
=ssl_get_prev_session(s
,p
,j
);
623 { /* previous session */
630 if (!ssl_get_new_session(s
,1))
637 if ((i
== 0) && (j
!= 0))
639 /* we need a cipher if we are not resuming a session */
640 al
=SSL_AD_ILLEGAL_PARAMETER
;
641 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_CIPHERS_SPECIFIED
);
646 /* not enough data */
647 al
=SSL_AD_DECODE_ERROR
;
648 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
651 if ((i
> 0) && (ssl_bytes_to_cipher_list(s
,p
,i
,&(ciphers
))
658 /* If it is a hit, check that the cipher is in the list */
659 if ((s
->hit
) && (i
> 0))
662 id
=s
->session
->cipher
->id
;
665 printf("client sent %d ciphers\n",sk_num(ciphers
));
667 for (i
=0; i
<sk_SSL_CIPHER_num(ciphers
); i
++)
669 c
=sk_SSL_CIPHER_value(ciphers
,i
);
671 printf("client [%2d of %2d]:%s\n",
672 i
,sk_num(ciphers
),SSL_CIPHER_get_name(c
));
682 if ((s
->options
& SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
) && (sk_SSL_CIPHER_num(ciphers
) == 1))
684 /* Very bad for multi-threading.... */
685 s
->session
->cipher
=sk_SSL_CIPHER_value(ciphers
,
690 /* we need to have the cipher in the cipher
691 * list if we are asked to reuse it */
692 al
=SSL_AD_ILLEGAL_PARAMETER
;
693 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_REQUIRED_CIPHER_MISSING
);
704 if (p
[j
] == 0) break;
711 al
=SSL_AD_DECODE_ERROR
;
712 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_COMPRESSION_SPECIFIED
);
716 /* Worst case, we will use the NULL compression, but if we have other
717 * options, we will now look for them. We have i-1 compression
718 * algorithms from the client, starting at q. */
719 s
->s3
->tmp
.new_compression
=NULL
;
720 if (s
->ctx
->comp_methods
!= NULL
)
721 { /* See if we have a match */
724 nn
=sk_SSL_COMP_num(s
->ctx
->comp_methods
);
727 comp
=sk_SSL_COMP_value(s
->ctx
->comp_methods
,m
);
740 s
->s3
->tmp
.new_compression
=comp
;
745 /* TLS does not mind if there is extra stuff */
746 if (s
->version
== SSL3_VERSION
)
750 /* wrong number of bytes,
751 * there could be more to follow */
752 al
=SSL_AD_DECODE_ERROR
;
753 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
758 /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must
763 s
->session
->compress_meth
=(comp
== NULL
)?0:comp
->id
;
764 if (s
->session
->ciphers
!= NULL
)
765 sk_SSL_CIPHER_free(s
->session
->ciphers
);
766 s
->session
->ciphers
=ciphers
;
769 al
=SSL_AD_ILLEGAL_PARAMETER
;
770 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_CIPHERS_PASSED
);
774 c
=ssl3_choose_cipher(s
,s
->session
->ciphers
,
775 ssl_get_ciphers_by_id(s
));
779 al
=SSL_AD_HANDSHAKE_FAILURE
;
780 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_SHARED_CIPHER
);
783 s
->s3
->tmp
.new_cipher
=c
;
787 /* Session-id reuse */
788 #ifdef REUSE_CIPHER_BUG
789 STACK_OF(SSL_CIPHER
) *sk
;
793 if (s
->options
& SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG
)
795 sk
=s
->session
->ciphers
;
796 for (i
=0; i
<sk_SSL_CIPHER_num(sk
); i
++)
798 c
=sk_SSL_CIPHER_value(sk
,i
);
799 if (c
->algorithms
& SSL_eNULL
)
801 if (SSL_C_IS_EXPORT(c
))
805 s
->s3
->tmp
.new_cipher
=nc
;
807 s
->s3
->tmp
.new_cipher
=ec
;
809 s
->s3
->tmp
.new_cipher
=s
->session
->cipher
;
813 s
->s3
->tmp
.new_cipher
=s
->session
->cipher
;
816 /* we now have the following setup.
818 * cipher_list - our prefered list of ciphers
819 * ciphers - the clients prefered list of ciphers
820 * compression - basically ignored right now
821 * ssl version is set - sslv3
822 * s->session - The ssl session has been setup.
823 * s->hit - session reuse flag
824 * s->tmp.new_cipher - the new cipher to use.
831 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
834 if (ciphers
!= NULL
) sk_SSL_CIPHER_free(ciphers
);
838 static int ssl3_send_server_hello(SSL
*s
)
843 unsigned long l
,Time
;
845 if (s
->state
== SSL3_ST_SW_SRVR_HELLO_A
)
847 buf
=(unsigned char *)s
->init_buf
->data
;
848 p
=s
->s3
->server_random
;
849 Time
=time(NULL
); /* Time */
851 RAND_pseudo_bytes(p
,SSL3_RANDOM_SIZE
-sizeof(Time
));
852 /* Do the message type and length last */
855 *(p
++)=s
->version
>>8;
856 *(p
++)=s
->version
&0xff;
859 memcpy(p
,s
->s3
->server_random
,SSL3_RANDOM_SIZE
);
862 /* now in theory we have 3 options to sending back the
863 * session id. If it is a re-use, we send back the
864 * old session-id, if it is a new session, we send
865 * back the new session-id or we send back a 0 length
866 * session-id if we want it to be single use.
867 * Currently I will not implement the '0' length session-id
868 * 12-Jan-98 - I'll now support the '0' length stuff.
870 if (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
))
871 s
->session
->session_id_length
=0;
873 sl
=s
->session
->session_id_length
;
875 memcpy(p
,s
->session
->session_id
,sl
);
879 i
=ssl3_put_cipher_by_char(s
->s3
->tmp
.new_cipher
,p
);
882 /* put the compression method */
883 if (s
->s3
->tmp
.new_compression
== NULL
)
886 *(p
++)=s
->s3
->tmp
.new_compression
->id
;
891 *(d
++)=SSL3_MT_SERVER_HELLO
;
894 s
->state
=SSL3_ST_CW_CLNT_HELLO_B
;
895 /* number of bytes to write */
900 /* SSL3_ST_CW_CLNT_HELLO_B */
901 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
904 static int ssl3_send_server_done(SSL
*s
)
908 if (s
->state
== SSL3_ST_SW_SRVR_DONE_A
)
910 p
=(unsigned char *)s
->init_buf
->data
;
913 *(p
++)=SSL3_MT_SERVER_DONE
;
918 s
->state
=SSL3_ST_SW_SRVR_DONE_B
;
919 /* number of bytes to write */
924 /* SSL3_ST_CW_CLNT_HELLO_B */
925 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
928 static int ssl3_send_server_key_exchange(SSL
*s
)
934 unsigned char md_buf
[MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
];
951 if (s
->state
== SSL3_ST_SW_KEY_EXCH_A
)
953 type
=s
->s3
->tmp
.new_cipher
->algorithms
& SSL_MKEY_MASK
;
958 r
[0]=r
[1]=r
[2]=r
[3]=NULL
;
964 if ((rsa
== NULL
) && (s
->cert
->rsa_tmp_cb
!= NULL
))
966 rsa
=s
->cert
->rsa_tmp_cb(s
,
967 SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
),
968 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
));
971 al
=SSL_AD_HANDSHAKE_FAILURE
;
972 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_ERROR_GENERATING_TMP_RSA_KEY
);
975 CRYPTO_add(&rsa
->references
,1,CRYPTO_LOCK_RSA
);
980 al
=SSL_AD_HANDSHAKE_FAILURE
;
981 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_RSA_KEY
);
986 s
->s3
->tmp
.use_rsa_tmp
=1;
994 if ((dhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
))
995 dhp
=s
->cert
->dh_tmp_cb(s
,
996 SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
),
997 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
));
1000 al
=SSL_AD_HANDSHAKE_FAILURE
;
1001 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
1005 if (s
->s3
->tmp
.dh
!= NULL
)
1008 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, SSL_R_INTERNAL_ERROR
);
1012 if ((dh
=DHparams_dup(dhp
)) == NULL
)
1014 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1019 if ((dhp
->pub_key
== NULL
||
1020 dhp
->priv_key
== NULL
||
1021 (s
->options
& SSL_OP_SINGLE_DH_USE
)))
1023 if(!DH_generate_key(dh
))
1025 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1032 dh
->pub_key
=BN_dup(dhp
->pub_key
);
1033 dh
->priv_key
=BN_dup(dhp
->priv_key
);
1034 if ((dh
->pub_key
== NULL
) ||
1035 (dh
->priv_key
== NULL
))
1037 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1048 al
=SSL_AD_HANDSHAKE_FAILURE
;
1049 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1052 for (i
=0; r
[i
] != NULL
; i
++)
1054 nr
[i
]=BN_num_bytes(r
[i
]);
1058 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
))
1060 if ((pkey
=ssl_get_sign_pkey(s
,s
->s3
->tmp
.new_cipher
))
1063 al
=SSL_AD_DECODE_ERROR
;
1066 kn
=EVP_PKEY_size(pkey
);
1074 if (!BUF_MEM_grow(buf
,n
+4+kn
))
1076 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_BUF
);
1079 d
=(unsigned char *)s
->init_buf
->data
;
1082 for (i
=0; r
[i
] != NULL
; i
++)
1092 /* n is the length of the params, they start at &(d[4])
1093 * and p points to the space at the end. */
1095 if (pkey
->type
== EVP_PKEY_RSA
)
1099 for (num
=2; num
> 0; num
--)
1101 EVP_DigestInit(&md_ctx
,(num
== 2)
1102 ?s
->ctx
->md5
:s
->ctx
->sha1
);
1103 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1104 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1105 EVP_DigestUpdate(&md_ctx
,&(d
[4]),n
);
1106 EVP_DigestFinal(&md_ctx
,q
,
1107 (unsigned int *)&i
);
1111 if (RSA_sign(NID_md5_sha1
, md_buf
, j
,
1112 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0)
1114 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_RSA
);
1122 #if !defined(NO_DSA)
1123 if (pkey
->type
== EVP_PKEY_DSA
)
1126 EVP_SignInit(&md_ctx
,EVP_dss1());
1127 EVP_SignUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1128 EVP_SignUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1129 EVP_SignUpdate(&md_ctx
,&(d
[4]),n
);
1130 if (!EVP_SignFinal(&md_ctx
,&(p
[2]),
1131 (unsigned int *)&i
,pkey
))
1133 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_DSA
);
1142 /* Is this error check actually needed? */
1143 al
=SSL_AD_HANDSHAKE_FAILURE
;
1144 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNKNOWN_PKEY_TYPE
);
1149 *(d
++)=SSL3_MT_SERVER_KEY_EXCHANGE
;
1152 /* we should now have things packed up, so lets send
1158 s
->state
= SSL3_ST_SW_KEY_EXCH_B
;
1159 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1161 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1166 static int ssl3_send_certificate_request(SSL
*s
)
1168 unsigned char *p
,*d
;
1170 STACK_OF(X509_NAME
) *sk
=NULL
;
1174 if (s
->state
== SSL3_ST_SW_CERT_REQ_A
)
1178 d
=p
=(unsigned char *)&(buf
->data
[4]);
1180 /* get the list of acceptable cert types */
1182 n
=ssl3_get_req_cert_type(s
,p
);
1191 sk
=SSL_get_client_CA_list(s
);
1195 for (i
=0; i
<sk_X509_NAME_num(sk
); i
++)
1197 name
=sk_X509_NAME_value(sk
,i
);
1198 j
=i2d_X509_NAME(name
,NULL
);
1199 if (!BUF_MEM_grow(buf
,4+n
+j
+2))
1201 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST
,ERR_R_BUF_LIB
);
1204 p
=(unsigned char *)&(buf
->data
[4+n
]);
1205 if (!(s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
))
1208 i2d_X509_NAME(name
,&p
);
1215 i2d_X509_NAME(name
,&p
);
1216 j
-=2; s2n(j
,d
); j
+=2;
1222 /* else no CA names */
1223 p
=(unsigned char *)&(buf
->data
[4+off
]);
1226 d
=(unsigned char *)buf
->data
;
1227 *(d
++)=SSL3_MT_CERTIFICATE_REQUEST
;
1230 /* we should now have things packed up, so lets send
1235 #ifdef NETSCAPE_HANG_BUG
1236 p
=(unsigned char *)s
->init_buf
->data
+ s
->init_num
;
1239 *(p
++)=SSL3_MT_SERVER_DONE
;
1248 /* SSL3_ST_SW_CERT_REQ_B */
1249 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1254 static int ssl3_get_client_key_exchange(SSL
*s
)
1262 EVP_PKEY
*pkey
=NULL
;
1270 #endif /* NO_KRB5 */
1272 n
=ssl3_get_message(s
,
1273 SSL3_ST_SR_KEY_EXCH_A
,
1274 SSL3_ST_SR_KEY_EXCH_B
,
1275 SSL3_MT_CLIENT_KEY_EXCHANGE
,
1279 if (!ok
) return((int)n
);
1280 p
=(unsigned char *)s
->init_buf
->data
;
1282 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
1287 /* FIX THIS UP EAY EAY EAY EAY */
1288 if (s
->s3
->tmp
.use_rsa_tmp
)
1290 if ((s
->cert
!= NULL
) && (s
->cert
->rsa_tmp
!= NULL
))
1291 rsa
=s
->cert
->rsa_tmp
;
1292 /* Don't do a callback because rsa_tmp should
1293 * be sent already */
1296 al
=SSL_AD_HANDSHAKE_FAILURE
;
1297 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_RSA_PKEY
);
1304 pkey
=s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
;
1305 if ( (pkey
== NULL
) ||
1306 (pkey
->type
!= EVP_PKEY_RSA
) ||
1307 (pkey
->pkey
.rsa
== NULL
))
1309 al
=SSL_AD_HANDSHAKE_FAILURE
;
1310 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_RSA_CERTIFICATE
);
1317 if (s
->version
> SSL3_VERSION
)
1322 if (!(s
->options
& SSL_OP_TLS_D5_BUG
))
1324 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG
);
1334 i
=RSA_private_decrypt((int)n
,p
,p
,rsa
,RSA_PKCS1_PADDING
);
1336 if (i
!= SSL_MAX_MASTER_KEY_LENGTH
)
1338 al
=SSL_AD_DECODE_ERROR
;
1339 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_RSA_DECRYPT
);
1343 if (!((p
[0] == (s
->client_version
>>8)) && (p
[1] == (s
->client_version
& 0xff))))
1345 /* The premaster secret must contain the same version number as the
1346 * ClientHello to detect version rollback attacks (strangely, the
1347 * protocol does not offer such protection for DH ciphersuites).
1348 * However, buggy clients exist that send the negotiated protocol
1349 * version instead if the server does not support the requested
1351 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1352 if (!((s
->options
& SSL_OP_TLS_ROLLBACK_BUG
) &&
1353 (p
[0] == (s
->version
>>8)) && (p
[1] == (s
->version
& 0xff))))
1355 al
=SSL_AD_DECODE_ERROR
;
1356 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_PROTOCOL_VERSION_NUMBER
);
1361 s
->session
->master_key_length
=
1362 s
->method
->ssl3_enc
->generate_master_secret(s
,
1363 s
->session
->master_key
,
1370 if (l
& (SSL_kEDH
|SSL_kDHr
|SSL_kDHd
))
1375 if (!(s
->options
& SSL_OP_SSLEAY_080_CLIENT_DH_BUG
))
1377 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
1387 if (n
== 0L) /* the parameters are in the cert */
1389 al
=SSL_AD_HANDSHAKE_FAILURE
;
1390 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_UNABLE_TO_DECODE_DH_CERTS
);
1395 if (s
->s3
->tmp
.dh
== NULL
)
1397 al
=SSL_AD_HANDSHAKE_FAILURE
;
1398 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
1402 dh_srvr
=s
->s3
->tmp
.dh
;
1405 pub
=BN_bin2bn(p
,i
,NULL
);
1408 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BN_LIB
);
1412 i
=DH_compute_key(p
,pub
,dh_srvr
);
1416 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1420 DH_free(s
->s3
->tmp
.dh
);
1425 s
->session
->master_key_length
=
1426 s
->method
->ssl3_enc
->generate_master_secret(s
,
1427 s
->session
->master_key
,p
,i
);
1434 krb5_error_code krb5rc
;
1435 KSSL_CTX
*kssl_ctx
= s
->kssl_ctx
;
1437 if (!kssl_ctx
) kssl_ctx
= kssl_ctx_new();
1438 if ((krb5rc
= kssl_sget_tkt(kssl_ctx
,
1439 s
->init_buf
->data
, s
->init_buf
->length
,
1443 printf("kssl_sget_tkt rtn %d [%d]\n",
1444 krb5rc
, kssl_err
.reason
);
1446 printf("kssl_err text= %s\n", kssl_err
.text
);
1447 #endif /* KSSL_DEBUG */
1448 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE
,
1454 kssl_ctx_show(kssl_ctx
);
1455 #endif /* KSSL_DEBUG */
1457 /* 19991013 VRS - 3DES is kind of bogus here,
1458 ** at least until Kerberos supports 3DES. The only
1459 ** real secret is the 8-byte Kerberos session key;
1460 ** the other key material (client_random, server_random)
1461 ** could be sniffed. Nonces may help against replays though.
1463 ** Alternate code for Kerberos Purists:
1465 ** memcpy(s->session->master_key, kssl_ctx->key, kssl_ctx->length);
1466 ** s->session->master_key_length = kssl_ctx->length;
1468 s
->session
->master_key_length
=
1469 s
->method
->ssl3_enc
->generate_master_secret(s
,
1470 s
->session
->master_key
, kssl_ctx
->key
, kssl_ctx
->length
);
1471 /* Was doing kssl_ctx_free() here, but it caused problems for apache.
1472 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
1473 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
1477 #endif /* NO_KRB5 */
1479 al
=SSL_AD_HANDSHAKE_FAILURE
;
1480 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_UNKNOWN_CIPHER_TYPE
);
1486 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1487 #if !defined(NO_DH) || !defined(NO_RSA)
1493 static int ssl3_get_cert_verify(SSL
*s
)
1495 EVP_PKEY
*pkey
=NULL
;
1502 n
=ssl3_get_message(s
,
1503 SSL3_ST_SR_CERT_VRFY_A
,
1504 SSL3_ST_SR_CERT_VRFY_B
,
1509 if (!ok
) return((int)n
);
1511 if (s
->session
->peer
!= NULL
)
1513 peer
=s
->session
->peer
;
1514 pkey
=X509_get_pubkey(peer
);
1515 type
=X509_certificate_type(peer
,pkey
);
1523 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_VERIFY
)
1525 s
->s3
->tmp
.reuse_message
=1;
1526 if ((peer
!= NULL
) && (type
| EVP_PKT_SIGN
))
1528 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1529 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_MISSING_VERIFY_MESSAGE
);
1538 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_NO_CLIENT_CERT_RECEIVED
);
1539 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1543 if (!(type
& EVP_PKT_SIGN
))
1545 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
1546 al
=SSL_AD_ILLEGAL_PARAMETER
;
1550 if (s
->s3
->change_cipher_spec
)
1552 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_CCS_RECEIVED_EARLY
);
1553 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1557 /* we now have a signature that we need to verify */
1558 p
=(unsigned char *)s
->init_buf
->data
;
1563 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_LENGTH_MISMATCH
);
1564 al
=SSL_AD_DECODE_ERROR
;
1568 j
=EVP_PKEY_size(pkey
);
1569 if ((i
> j
) || (n
> j
) || (n
<= 0))
1571 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_WRONG_SIGNATURE_SIZE
);
1572 al
=SSL_AD_DECODE_ERROR
;
1577 if (pkey
->type
== EVP_PKEY_RSA
)
1579 i
=RSA_verify(NID_md5_sha1
, s
->s3
->tmp
.cert_verify_md
,
1580 MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
, p
, i
,
1584 al
=SSL_AD_DECRYPT_ERROR
;
1585 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_DECRYPT
);
1590 al
=SSL_AD_DECRYPT_ERROR
;
1591 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_SIGNATURE
);
1598 if (pkey
->type
== EVP_PKEY_DSA
)
1600 j
=DSA_verify(pkey
->save_type
,
1601 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
1602 SHA_DIGEST_LENGTH
,p
,i
,pkey
->pkey
.dsa
);
1606 al
=SSL_AD_DECRYPT_ERROR
;
1607 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_DSA_SIGNATURE
);
1614 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_INTERNAL_ERROR
);
1615 al
=SSL_AD_UNSUPPORTED_CERTIFICATE
;
1624 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1627 EVP_PKEY_free(pkey
);
1631 static int ssl3_get_client_certificate(SSL
*s
)
1633 int i
,ok
,al
,ret
= -1;
1635 unsigned long l
,nc
,llen
,n
;
1636 unsigned char *p
,*d
,*q
;
1637 STACK_OF(X509
) *sk
=NULL
;
1639 n
=ssl3_get_message(s
,
1643 #if defined(MSDOS) && !defined(WIN32)
1644 1024*30, /* 30k max cert list :-) */
1646 1024*100, /* 100k max cert list :-) */
1650 if (!ok
) return((int)n
);
1652 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_KEY_EXCHANGE
)
1654 if ( (s
->verify_mode
& SSL_VERIFY_PEER
) &&
1655 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
1657 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
1658 al
=SSL_AD_HANDSHAKE_FAILURE
;
1661 /* If tls asked for a client cert, the client must return a 0 list */
1662 if ((s
->version
> SSL3_VERSION
) && s
->s3
->tmp
.cert_request
)
1664 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
);
1665 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1668 s
->s3
->tmp
.reuse_message
=1;
1672 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
)
1674 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1675 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_WRONG_MESSAGE_TYPE
);
1678 d
=p
=(unsigned char *)s
->init_buf
->data
;
1680 if ((sk
=sk_X509_new_null()) == NULL
)
1682 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
1689 al
=SSL_AD_DECODE_ERROR
;
1690 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_LENGTH_MISMATCH
);
1693 for (nc
=0; nc
<llen
; )
1696 if ((l
+nc
+3) > llen
)
1698 al
=SSL_AD_DECODE_ERROR
;
1699 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
1704 x
=d2i_X509(NULL
,&p
,l
);
1707 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_ASN1_LIB
);
1712 al
=SSL_AD_DECODE_ERROR
;
1713 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
1716 if (!sk_X509_push(sk
,x
))
1718 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
1725 if (sk_X509_num(sk
) <= 0)
1727 /* TLS does not mind 0 certs returned */
1728 if (s
->version
== SSL3_VERSION
)
1730 al
=SSL_AD_HANDSHAKE_FAILURE
;
1731 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_NO_CERTIFICATES_RETURNED
);
1734 /* Fail for TLS only if we required a certificate */
1735 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
1736 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
1738 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
1739 al
=SSL_AD_HANDSHAKE_FAILURE
;
1745 i
=ssl_verify_cert_chain(s
,sk
);
1748 al
=ssl_verify_alarm_type(s
->verify_result
);
1749 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_NO_CERTIFICATE_RETURNED
);
1754 if (s
->session
->peer
!= NULL
) /* This should not be needed */
1755 X509_free(s
->session
->peer
);
1756 s
->session
->peer
=sk_X509_shift(sk
);
1757 s
->session
->verify_result
= s
->verify_result
;
1759 /* With the current implementation, sess_cert will always be NULL
1760 * when we arrive here. */
1761 if (s
->session
->sess_cert
== NULL
)
1763 s
->session
->sess_cert
= ssl_sess_cert_new();
1764 if (s
->session
->sess_cert
== NULL
)
1766 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
1770 if (s
->session
->sess_cert
->cert_chain
!= NULL
)
1771 sk_X509_pop_free(s
->session
->sess_cert
->cert_chain
, X509_free
);
1772 s
->session
->sess_cert
->cert_chain
=sk
;
1773 /* Inconsistency alert: cert_chain does *not* include the
1774 * peer's own certificate, while we do include it in s3_clnt.c */
1782 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1785 if (x
!= NULL
) X509_free(x
);
1786 if (sk
!= NULL
) sk_X509_pop_free(sk
,X509_free
);
1790 int ssl3_send_server_certificate(SSL
*s
)
1795 if (s
->state
== SSL3_ST_SW_CERT_A
)
1797 x
=ssl_get_server_send_cert(s
);
1799 /* VRS: allow null cert if auth == KRB5 */
1800 (s
->s3
->tmp
.new_cipher
->algorithms
1801 & (SSL_MKEY_MASK
|SSL_AUTH_MASK
))
1802 != (SSL_aKRB5
|SSL_kKRB5
))
1804 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE
,SSL_R_INTERNAL_ERROR
);
1808 l
=ssl3_output_cert_chain(s
,x
);
1809 s
->state
=SSL3_ST_SW_CERT_B
;
1814 /* SSL3_ST_SW_CERT_B */
1815 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));