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
70 /* SSLerr(SSL_F_SSL3_ACCEPT,ERR_R_MALLOC_FAILURE);
71 * SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
72 * SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
73 * SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_MALLOC_FAILURE);
74 * SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
78 static SSL_METHOD
*ssl3_get_server_method(int ver
);
79 static int ssl3_get_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_cert_verify(SSL
*s
);
85 static int ssl3_get_client_key_exchange(SSL
*s
);
86 static int ssl3_get_client_certificate(SSL
*s
);
87 static int ssl3_send_hello_request(SSL
*s
);
91 static SSL_METHOD
*ssl3_get_server_method();
92 static int ssl3_get_client_hello();
93 static int ssl3_send_server_hello();
94 static int ssl3_send_server_key_exchange();
95 static int ssl3_send_certificate_request();
96 static int ssl3_send_server_done();
97 static int ssl3_get_cert_verify();
98 static int ssl3_get_client_key_exchange();
99 static int ssl3_get_client_certificate();
100 static int ssl3_send_hello_request();
104 static SSL_METHOD
*ssl3_get_server_method(ver
)
107 if (ver
== SSL3_VERSION
)
108 return(SSLv3_server_method());
113 SSL_METHOD
*SSLv3_server_method()
116 static SSL_METHOD SSLv3_server_data
;
121 memcpy((char *)&SSLv3_server_data
,(char *)sslv3_base_method(),
123 SSLv3_server_data
.ssl_accept
=ssl3_accept
;
124 SSLv3_server_data
.get_ssl_method
=ssl3_get_server_method
;
126 return(&SSLv3_server_data
);
133 unsigned long l
,Time
=time(NULL
);
138 int new_state
,state
,skip
=0;
140 RAND_seed(&Time
,sizeof(Time
));
144 if (s
->info_callback
!= NULL
)
146 else if (s
->ctx
->info_callback
!= NULL
)
147 cb
=s
->ctx
->info_callback
;
149 /* init things to blank */
150 if (!SSL_in_init(s
) || SSL_in_before(s
)) SSL_clear(s
);
154 /* FIX THIS EAY EAY EAY */
155 /* we don't actually need a cert, we just need a cert or a DH_tmp */
156 if (((s
->session
== NULL
) || (s
->session
->cert
== NULL
)) &&
159 SSLerr(SSL_F_SSL3_ACCEPT
,SSL_R_NO_CERTIFICATE_SET
);
171 case SSL_ST_RENEGOTIATE
:
173 /* s->state=SSL_ST_ACCEPT; */
177 case SSL_ST_BEFORE
|SSL_ST_ACCEPT
:
178 case SSL_ST_OK
|SSL_ST_ACCEPT
:
181 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_START
,1);
183 if ((s
->version
>>8) != 3)
185 /* s->version=SSL3_VERSION; */
186 s
->type
=SSL_ST_ACCEPT
;
188 if (s
->init_buf
== NULL
)
190 if ((buf
=BUF_MEM_new()) == NULL
)
195 if (!BUF_MEM_grow(buf
,SSL3_RT_MAX_PLAIN_LENGTH
))
203 if (!ssl3_setup_buffers(s
))
209 /* Ok, we now need to push on a buffering BIO so that
210 * the output is sent in a way that TCP likes :-)
212 if (!ssl_init_wbio_buffer(s
,1)) { ret
= -1; goto end
; }
216 if (s
->state
!= SSL_ST_RENEGOTIATE
)
218 s
->state
=SSL3_ST_SR_CLNT_HELLO_A
;
219 ssl3_init_finished_mac(s
);
220 s
->ctx
->stats
.sess_accept
++;
224 s
->ctx
->stats
.sess_accept_renegotiate
++;
225 s
->state
=SSL3_ST_SW_HELLO_REQ_A
;
229 case SSL3_ST_SW_HELLO_REQ_A
:
230 case SSL3_ST_SW_HELLO_REQ_B
:
233 ret
=ssl3_send_hello_request(s
);
234 if (ret
<= 0) goto end
;
235 s
->s3
->tmp
.next_state
=SSL3_ST_SW_HELLO_REQ_C
;
236 s
->state
=SSL3_ST_SW_FLUSH
;
239 ssl3_init_finished_mac(s
);
242 case SSL3_ST_SW_HELLO_REQ_C
:
248 case SSL3_ST_SR_CLNT_HELLO_A
:
249 case SSL3_ST_SR_CLNT_HELLO_B
:
250 case SSL3_ST_SR_CLNT_HELLO_C
:
253 ret
=ssl3_get_client_hello(s
);
254 if (ret
<= 0) goto end
;
255 s
->state
=SSL3_ST_SW_SRVR_HELLO_A
;
259 case SSL3_ST_SW_SRVR_HELLO_A
:
260 case SSL3_ST_SW_SRVR_HELLO_B
:
261 ret
=ssl3_send_server_hello(s
);
262 if (ret
<= 0) goto end
;
265 s
->state
=SSL3_ST_SW_CHANGE_A
;
267 s
->state
=SSL3_ST_SW_CERT_A
;
271 case SSL3_ST_SW_CERT_A
:
272 case SSL3_ST_SW_CERT_B
:
273 /* Check if it is anon DH */
274 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
))
276 ret
=ssl3_send_server_certificate(s
);
277 if (ret
<= 0) goto end
;
281 s
->state
=SSL3_ST_SW_KEY_EXCH_A
;
285 case SSL3_ST_SW_KEY_EXCH_A
:
286 case SSL3_ST_SW_KEY_EXCH_B
:
287 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
288 if (s
->session
->cert
== NULL
)
292 CRYPTO_add(&s
->cert
->references
,1,CRYPTO_LOCK_SSL_CERT
);
293 s
->session
->cert
=s
->cert
;
297 CRYPTO_add(&s
->ctx
->default_cert
->references
,1,CRYPTO_LOCK_SSL_CERT
);
298 s
->session
->cert
=s
->ctx
->default_cert
;
303 /* clear this, it may get reset by
304 * send_server_key_exchange */
305 if (s
->options
& SSL_OP_EPHEMERAL_RSA
)
306 s
->s3
->tmp
.use_rsa_tmp
=1;
308 s
->s3
->tmp
.use_rsa_tmp
=0;
310 /* only send if a DH key exchange, fortezza or
311 * RSA but we have a sign only certificate */
312 if (s
->s3
->tmp
.use_rsa_tmp
313 || (l
& (SSL_DH
|SSL_kFZA
))
315 && (ct
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
== NULL
317 && EVP_PKEY_size(ct
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
)*8 > SSL_EXPORT_PKEYLENGTH(l
)
323 ret
=ssl3_send_server_key_exchange(s
);
324 if (ret
<= 0) goto end
;
329 s
->state
=SSL3_ST_SW_CERT_REQ_A
;
333 case SSL3_ST_SW_CERT_REQ_A
:
334 case SSL3_ST_SW_CERT_REQ_B
:
335 if (!(s
->verify_mode
& SSL_VERIFY_PEER
) ||
336 ((s
->session
->peer
!= NULL
) &&
337 (s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
)))
339 /* no cert request */
341 s
->s3
->tmp
.cert_request
=0;
342 s
->state
=SSL3_ST_SW_SRVR_DONE_A
;
346 s
->s3
->tmp
.cert_request
=1;
347 ret
=ssl3_send_certificate_request(s
);
348 if (ret
<= 0) goto end
;
349 s
->state
=SSL3_ST_SW_SRVR_DONE_A
;
354 case SSL3_ST_SW_SRVR_DONE_A
:
355 case SSL3_ST_SW_SRVR_DONE_B
:
356 ret
=ssl3_send_server_done(s
);
357 if (ret
<= 0) goto end
;
358 s
->s3
->tmp
.next_state
=SSL3_ST_SR_CERT_A
;
359 s
->state
=SSL3_ST_SW_FLUSH
;
363 case SSL3_ST_SW_FLUSH
:
364 /* number of bytes to be flushed */
365 num1
=BIO_ctrl(s
->wbio
,BIO_CTRL_INFO
,0,NULL
);
368 s
->rwstate
=SSL_WRITING
;
369 num1
=BIO_flush(s
->wbio
);
370 if (num1
<= 0) { ret
= -1; goto end
; }
371 s
->rwstate
=SSL_NOTHING
;
374 s
->state
=s
->s3
->tmp
.next_state
;
377 case SSL3_ST_SR_CERT_A
:
378 case SSL3_ST_SR_CERT_B
:
379 /* could be sent for a DH cert, even if we
380 * have not asked for it :-) */
381 ret
=ssl3_get_client_certificate(s
);
382 if (ret
<= 0) goto end
;
384 s
->state
=SSL3_ST_SR_KEY_EXCH_A
;
387 case SSL3_ST_SR_KEY_EXCH_A
:
388 case SSL3_ST_SR_KEY_EXCH_B
:
389 ret
=ssl3_get_client_key_exchange(s
);
390 if (ret
<= 0) goto end
;
391 s
->state
=SSL3_ST_SR_CERT_VRFY_A
;
394 /* We need to get hashes here so if there is
395 * a client cert, it can be verified */
396 s
->method
->ssl3_enc
->cert_verify_mac(s
,
397 &(s
->s3
->finish_dgst1
),
398 &(s
->s3
->tmp
.finish_md
[0]));
399 s
->method
->ssl3_enc
->cert_verify_mac(s
,
400 &(s
->s3
->finish_dgst2
),
401 &(s
->s3
->tmp
.finish_md
[MD5_DIGEST_LENGTH
]));
405 case SSL3_ST_SR_CERT_VRFY_A
:
406 case SSL3_ST_SR_CERT_VRFY_B
:
408 /* we should decide if we expected this one */
409 ret
=ssl3_get_cert_verify(s
);
410 if (ret
<= 0) goto end
;
412 s
->state
=SSL3_ST_SR_FINISHED_A
;
416 case SSL3_ST_SR_FINISHED_A
:
417 case SSL3_ST_SR_FINISHED_B
:
418 ret
=ssl3_get_finished(s
,SSL3_ST_SR_FINISHED_A
,
419 SSL3_ST_SR_FINISHED_B
);
420 if (ret
<= 0) goto end
;
424 s
->state
=SSL3_ST_SW_CHANGE_A
;
428 case SSL3_ST_SW_CHANGE_A
:
429 case SSL3_ST_SW_CHANGE_B
:
431 s
->session
->cipher
=s
->s3
->tmp
.new_cipher
;
432 if (!s
->method
->ssl3_enc
->setup_key_block(s
))
433 { ret
= -1; goto end
; }
435 ret
=ssl3_send_change_cipher_spec(s
,
436 SSL3_ST_SW_CHANGE_A
,SSL3_ST_SW_CHANGE_B
);
438 if (ret
<= 0) goto end
;
439 s
->state
=SSL3_ST_SW_FINISHED_A
;
442 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
443 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
451 case SSL3_ST_SW_FINISHED_A
:
452 case SSL3_ST_SW_FINISHED_B
:
453 ret
=ssl3_send_finished(s
,
454 SSL3_ST_SW_FINISHED_A
,SSL3_ST_SW_FINISHED_B
,
455 s
->method
->ssl3_enc
->server_finished
,
456 s
->method
->ssl3_enc
->server_finished_len
);
457 if (ret
<= 0) goto end
;
458 s
->state
=SSL3_ST_SW_FLUSH
;
460 s
->s3
->tmp
.next_state
=SSL3_ST_SR_FINISHED_A
;
462 s
->s3
->tmp
.next_state
=SSL_ST_OK
;
467 /* clean a few things up */
468 ssl3_cleanup_key_block(s
);
470 BUF_MEM_free(s
->init_buf
);
473 /* remove buffering on output */
474 ssl_free_wbio_buffer(s
);
479 ssl_update_cache(s
,SSL_SESS_CACHE_SERVER
);
481 s
->ctx
->stats
.sess_accept_good
++;
483 s
->handshake_func
=ssl3_accept
;
486 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_DONE
,1);
492 SSLerr(SSL_F_SSL3_ACCEPT
,SSL_R_UNKNOWN_STATE
);
498 if (!s
->s3
->tmp
.reuse_message
&& !skip
)
502 if ((ret
=BIO_flush(s
->wbio
)) <= 0)
507 if ((cb
!= NULL
) && (s
->state
!= state
))
511 cb(s
,SSL_CB_ACCEPT_LOOP
,1);
518 /* BIO_flush(s->wbio); */
521 cb(s
,SSL_CB_ACCEPT_EXIT
,ret
);
526 static int ssl3_send_hello_request(s
)
531 if (s
->state
== SSL3_ST_SW_HELLO_REQ_A
)
533 p
=(unsigned char *)s
->init_buf
->data
;
534 *(p
++)=SSL3_MT_CLIENT_REQUEST
;
539 s
->state
=SSL3_ST_SW_HELLO_REQ_B
;
540 /* number of bytes to write */
545 /* SSL3_ST_SW_HELLO_REQ_B */
546 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
549 static int ssl3_get_client_hello(s
)
552 int i
,j
,ok
,al
,ret
= -1;
555 unsigned char *p
,*d
,*q
;
560 /* We do this so that we will respond with our native type.
561 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
562 * This down switching should be handled by a different method.
563 * If we are SSLv3, we will respond with SSLv3, even if prompted with
566 if (s
->state
== SSL3_ST_SR_CLNT_HELLO_A
)
569 s
->state
=SSL3_ST_SR_CLNT_HELLO_B
;
571 n
=ssl3_get_message(s
,
572 SSL3_ST_SR_CLNT_HELLO_B
,
573 SSL3_ST_SR_CLNT_HELLO_C
,
574 SSL3_MT_CLIENT_HELLO
,
575 SSL3_RT_MAX_PLAIN_LENGTH
,
578 if (!ok
) return((int)n
);
579 d
=p
=(unsigned char *)s
->init_buf
->data
;
581 /* The version number has already been checked in ssl3_get_message.
582 * I a native TLSv1/SSLv3 method, the match must be correct except
583 * perhaps for the first message */
584 /* s->client_version=(((int)p[0])<<8)|(int)p[1]; */
587 /* load the client random */
588 memcpy(s
->s3
->client_random
,p
,SSL3_RANDOM_SIZE
);
591 /* get the session-id */
597 if (!ssl_get_new_session(s
,1))
602 i
=ssl_get_prev_session(s
,p
,j
);
604 { /* previous session */
609 if (!ssl_get_new_session(s
,1))
616 if ((i
== 0) && (j
!= 0))
618 /* we need a cipher if we are not resuming a session */
619 al
=SSL_AD_ILLEGAL_PARAMETER
;
620 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_CIPHERS_SPECIFIED
);
625 /* not enough data */
626 al
=SSL_AD_DECODE_ERROR
;
627 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
630 if ((i
> 0) && (ssl_bytes_to_cipher_list(s
,p
,i
,&(ciphers
))
637 /* If it is a hit, check that the cipher is in the list */
638 if ((s
->hit
) && (i
> 0))
641 id
=s
->session
->cipher
->id
;
644 printf("client sent %d ciphers\n",sk_num(ciphers
));
646 for (i
=0; i
<sk_num(ciphers
); i
++)
648 c
=(SSL_CIPHER
*)sk_value(ciphers
,i
);
650 printf("client [%2d of %2d]:%s\n",
651 i
,sk_num(ciphers
),SSL_CIPHER_get_name(c
));
661 if ((s
->options
& SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
) && (sk_num(ciphers
) == 1))
663 /* Very bad for multi-threading.... */
665 (SSL_CIPHER
*)sk_value(ciphers
,0);
669 /* we need to have the cipher in the cipher
670 * list if we are asked to reuse it */
671 al
=SSL_AD_ILLEGAL_PARAMETER
;
672 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_REQUIRED_CIPHER_MISSING
);
683 if (p
[j
] == 0) break;
690 al
=SSL_AD_DECODE_ERROR
;
691 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_COMPRESSION_SPECIFIED
);
695 /* Worst case, we will use the NULL compression, but if we have other
696 * options, we will now look for them. We have i-1 compression
697 * algorithms from the client, starting at q. */
698 s
->s3
->tmp
.new_compression
=NULL
;
699 if (s
->ctx
->comp_methods
!= NULL
)
700 { /* See if we have a match */
703 nn
=sk_num(s
->ctx
->comp_methods
);
706 comp
=(SSL_COMP
*)sk_value(s
->ctx
->comp_methods
,m
);
719 s
->s3
->tmp
.new_compression
=comp
;
724 /* TLS does not mind if there is extra stuff */
725 if (s
->version
== SSL3_VERSION
)
729 /* wrong number of bytes,
730 * there could be more to follow */
731 al
=SSL_AD_DECODE_ERROR
;
732 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
737 /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must
742 s
->session
->compress_meth
=(comp
== NULL
)?0:comp
->id
;
743 if (s
->session
->ciphers
!= NULL
)
744 sk_free(s
->session
->ciphers
);
745 s
->session
->ciphers
=ciphers
;
748 al
=SSL_AD_ILLEGAL_PARAMETER
;
749 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_CIPHERS_PASSED
);
753 c
=ssl3_choose_cipher(s
,s
->session
->ciphers
,
754 ssl_get_ciphers_by_id(s
));
758 al
=SSL_AD_HANDSHAKE_FAILURE
;
759 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_SHARED_CIPHER
);
762 s
->s3
->tmp
.new_cipher
=c
;
766 /* Session-id reuse */
767 #ifdef REUSE_CIPHER_BUG
772 if (s
->options
& SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG
)
774 sk
=s
->session
->ciphers
;
775 for (i
=0; i
<sk_num(sk
); i
++)
777 c
=(SSL_CIPHER
*)sk_value(sk
,i
);
778 if (c
->algorithms
& SSL_eNULL
)
780 if (SSL_C_IS_EXPORT(c
))
784 s
->s3
->tmp
.new_cipher
=nc
;
786 s
->s3
->tmp
.new_cipher
=ec
;
788 s
->s3
->tmp
.new_cipher
=s
->session
->cipher
;
792 s
->s3
->tmp
.new_cipher
=s
->session
->cipher
;
795 /* we now have the following setup.
797 * cipher_list - our prefered list of ciphers
798 * ciphers - the clients prefered list of ciphers
799 * compression - basically ignored right now
800 * ssl version is set - sslv3
801 * s->session - The ssl session has been setup.
802 * s->hit - sesson reuse flag
803 * s->tmp.new_cipher - the new cipher to use.
810 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
813 if (ciphers
!= NULL
) sk_free(ciphers
);
817 static int ssl3_send_server_hello(s
)
823 unsigned long l
,Time
;
825 if (s
->state
== SSL3_ST_SW_SRVR_HELLO_A
)
827 buf
=(unsigned char *)s
->init_buf
->data
;
828 p
=s
->s3
->server_random
;
829 Time
=time(NULL
); /* Time */
831 RAND_bytes(p
,SSL3_RANDOM_SIZE
-sizeof(Time
));
832 /* Do the message type and length last */
835 *(p
++)=s
->version
>>8;
836 *(p
++)=s
->version
&0xff;
839 memcpy(p
,s
->s3
->server_random
,SSL3_RANDOM_SIZE
);
842 /* now in theory we have 3 options to sending back the
843 * session id. If it is a re-use, we send back the
844 * old session-id, if it is a new session, we send
845 * back the new session-id or we send back a 0 length
846 * session-id if we want it to be single use.
847 * Currently I will not implement the '0' length session-id
848 * 12-Jan-98 - I'll now support the '0' length stuff.
850 if (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
))
851 s
->session
->session_id_length
=0;
853 sl
=s
->session
->session_id_length
;
855 memcpy(p
,s
->session
->session_id
,sl
);
859 i
=ssl3_put_cipher_by_char(s
->s3
->tmp
.new_cipher
,p
);
862 /* put the compression method */
863 if (s
->s3
->tmp
.new_compression
== NULL
)
866 *(p
++)=s
->s3
->tmp
.new_compression
->id
;
871 *(d
++)=SSL3_MT_SERVER_HELLO
;
874 s
->state
=SSL3_ST_CW_CLNT_HELLO_B
;
875 /* number of bytes to write */
880 /* SSL3_ST_CW_CLNT_HELLO_B */
881 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
884 static int ssl3_send_server_done(s
)
889 if (s
->state
== SSL3_ST_SW_SRVR_DONE_A
)
891 p
=(unsigned char *)s
->init_buf
->data
;
894 *(p
++)=SSL3_MT_SERVER_DONE
;
899 s
->state
=SSL3_ST_SW_SRVR_DONE_B
;
900 /* number of bytes to write */
905 /* SSL3_ST_CW_CLNT_HELLO_B */
906 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
909 static int ssl3_send_server_key_exchange(s
)
916 unsigned char md_buf
[MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
];
932 if (s
->state
== SSL3_ST_SW_KEY_EXCH_A
)
934 type
=s
->s3
->tmp
.new_cipher
->algorithms
& SSL_MKEY_MASK
;
935 cert
=s
->session
->cert
;
939 r
[0]=r
[1]=r
[2]=r
[3]=NULL
;
945 if ((rsa
== NULL
) && (s
->ctx
->default_cert
->rsa_tmp_cb
!= NULL
))
947 rsa
=s
->ctx
->default_cert
->rsa_tmp_cb(s
,
948 !SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
));
949 CRYPTO_add(&rsa
->references
,1,CRYPTO_LOCK_RSA
);
954 al
=SSL_AD_HANDSHAKE_FAILURE
;
955 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_RSA_KEY
);
960 s
->s3
->tmp
.use_rsa_tmp
=1;
968 if ((dhp
== NULL
) && (cert
->dh_tmp_cb
!= NULL
))
969 dhp
=cert
->dh_tmp_cb(s
,
970 !SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
));
973 al
=SSL_AD_HANDSHAKE_FAILURE
;
974 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
977 if ((dh
=DHparams_dup(dhp
)) == NULL
)
979 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_DH_LIB
);
984 if ((dhp
->pub_key
== NULL
||
985 dhp
->priv_key
== NULL
||
986 (s
->options
& SSL_OP_SINGLE_DH_USE
)))
988 if(!DH_generate_key(dh
))
990 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
997 dh
->pub_key
=BN_dup(dhp
->pub_key
);
998 dh
->priv_key
=BN_dup(dhp
->priv_key
);
999 if ((dh
->pub_key
== NULL
) ||
1000 (dh
->priv_key
== NULL
))
1002 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1013 al
=SSL_AD_HANDSHAKE_FAILURE
;
1014 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1017 for (i
=0; r
[i
] != NULL
; i
++)
1019 nr
[i
]=BN_num_bytes(r
[i
]);
1023 if (!(s
->s3
->tmp
.new_cipher
->algorithms
& SSL_aNULL
))
1025 if ((pkey
=ssl_get_sign_pkey(s
,s
->s3
->tmp
.new_cipher
))
1028 al
=SSL_AD_DECODE_ERROR
;
1031 kn
=EVP_PKEY_size(pkey
);
1039 if (!BUF_MEM_grow(buf
,n
+4+kn
))
1041 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_BUF
);
1044 d
=(unsigned char *)s
->init_buf
->data
;
1047 for (i
=0; r
[i
] != NULL
; i
++)
1057 /* n is the length of the params, they start at &(d[4])
1058 * and p points to the space at the end. */
1060 if (pkey
->type
== EVP_PKEY_RSA
)
1064 for (num
=2; num
> 0; num
--)
1066 EVP_DigestInit(&md_ctx
,(num
== 2)
1067 ?s
->ctx
->md5
:s
->ctx
->sha1
);
1068 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1069 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1070 EVP_DigestUpdate(&md_ctx
,&(d
[4]),n
);
1071 EVP_DigestFinal(&md_ctx
,q
,
1072 (unsigned int *)&i
);
1076 i
=RSA_private_encrypt(j
,md_buf
,&(p
[2]),
1077 pkey
->pkey
.rsa
,RSA_PKCS1_PADDING
);
1080 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_RSA
);
1088 #if !defined(NO_DSA)
1089 if (pkey
->type
== EVP_PKEY_DSA
)
1092 EVP_SignInit(&md_ctx
,EVP_dss1());
1093 EVP_SignUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
1094 EVP_SignUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
1095 EVP_SignUpdate(&md_ctx
,&(d
[4]),n
);
1096 if (!EVP_SignFinal(&md_ctx
,&(p
[2]),
1097 (unsigned int *)&i
,pkey
))
1099 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_DSA
);
1108 /* Is this error check actually needed? */
1109 al
=SSL_AD_HANDSHAKE_FAILURE
;
1110 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNKNOWN_PKEY_TYPE
);
1115 *(d
++)=SSL3_MT_SERVER_KEY_EXCHANGE
;
1118 /* we should now have things packed up, so lets send
1124 /* SSL3_ST_SW_KEY_EXCH_B */
1125 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1127 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1132 static int ssl3_send_certificate_request(s
)
1135 unsigned char *p
,*d
;
1141 if (s
->state
== SSL3_ST_SW_CERT_REQ_A
)
1145 d
=p
=(unsigned char *)&(buf
->data
[4]);
1147 /* get the list of acceptable cert types */
1149 n
=ssl3_get_req_cert_type(s
,p
);
1158 sk
=SSL_get_client_CA_list(s
);
1162 for (i
=0; i
<sk_num(sk
); i
++)
1164 name
=(X509_NAME
*)sk_value(sk
,i
);
1165 j
=i2d_X509_NAME(name
,NULL
);
1166 if (!BUF_MEM_grow(buf
,4+n
+j
+2))
1168 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST
,ERR_R_BUF_LIB
);
1171 p
=(unsigned char *)&(buf
->data
[4+n
]);
1172 if (!(s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
))
1175 i2d_X509_NAME(name
,&p
);
1182 i2d_X509_NAME(name
,&p
);
1183 j
-=2; s2n(j
,d
); j
+=2;
1189 /* else no CA names */
1190 p
=(unsigned char *)&(buf
->data
[4+off
]);
1193 d
=(unsigned char *)buf
->data
;
1194 *(d
++)=SSL3_MT_CERTIFICATE_REQUEST
;
1197 /* we should now have things packed up, so lets send
1204 /* SSL3_ST_SW_CERT_REQ_B */
1205 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
1210 static int ssl3_get_client_key_exchange(s
)
1218 EVP_PKEY
*pkey
=NULL
;
1224 n
=ssl3_get_message(s
,
1225 SSL3_ST_SR_KEY_EXCH_A
,
1226 SSL3_ST_SR_KEY_EXCH_B
,
1227 SSL3_MT_CLIENT_KEY_EXCHANGE
,
1231 if (!ok
) return((int)n
);
1232 p
=(unsigned char *)s
->init_buf
->data
;
1234 l
=s
->s3
->tmp
.new_cipher
->algorithms
;
1239 /* FIX THIS UP EAY EAY EAY EAY */
1240 if (s
->s3
->tmp
.use_rsa_tmp
)
1242 if ((s
->session
->cert
!= NULL
) &&
1243 (s
->session
->cert
->rsa_tmp
!= NULL
))
1244 rsa
=s
->session
->cert
->rsa_tmp
;
1245 else if ((s
->ctx
->default_cert
!= NULL
) &&
1246 (s
->ctx
->default_cert
->rsa_tmp
!= NULL
))
1247 rsa
=s
->ctx
->default_cert
->rsa_tmp
;
1248 /* Don't do a callback because rsa_tmp should
1249 * be sent already */
1252 al
=SSL_AD_HANDSHAKE_FAILURE
;
1253 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_RSA_PKEY
);
1260 pkey
=s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
;
1261 if ( (pkey
== NULL
) ||
1262 (pkey
->type
!= EVP_PKEY_RSA
) ||
1263 (pkey
->pkey
.rsa
== NULL
))
1265 al
=SSL_AD_HANDSHAKE_FAILURE
;
1266 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_RSA_CERTIFICATE
);
1273 if (s
->version
> SSL3_VERSION
)
1278 if (!(s
->options
& SSL_OP_TLS_D5_BUG
))
1280 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG
);
1290 i
=RSA_private_decrypt((int)n
,p
,p
,rsa
,RSA_PKCS1_PADDING
);
1293 /* If a bad decrypt, use a random master key */
1294 if ((i
!= SSL_MAX_MASTER_KEY_LENGTH
) ||
1295 ((p
[0] != (s
->client_version
>>8)) ||
1296 (p
[1] != (s
->client_version
& 0xff))))
1300 if ((i
== SSL_MAX_MASTER_KEY_LENGTH
) &&
1301 (p
[0] == (s
->version
>>8)) &&
1304 if (s
->options
& SSL_OP_TLS_ROLLBACK_BUG
)
1309 p
[0]=(s
->version
>>8);
1310 p
[1]=(s
->version
& 0xff);
1311 RAND_bytes(&(p
[2]),SSL_MAX_MASTER_KEY_LENGTH
-2);
1312 i
=SSL_MAX_MASTER_KEY_LENGTH
;
1314 /* else, an SSLeay bug, ssl only server, tls client */
1317 if (i
!= SSL_MAX_MASTER_KEY_LENGTH
)
1319 al
=SSL_AD_DECODE_ERROR
;
1320 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_RSA_DECRYPT
);
1324 if ((p
[0] != (s
->version
>>8)) || (p
[1] != (s
->version
& 0xff)))
1326 al
=SSL_AD_DECODE_ERROR
;
1327 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_PROTOCOL_VERSION_NUMBER
);
1332 s
->session
->master_key_length
=
1333 s
->method
->ssl3_enc
->generate_master_secret(s
,
1334 s
->session
->master_key
,
1341 if (l
& (SSL_kEDH
|SSL_kDHr
|SSL_kDHd
))
1346 if (!(s
->options
& SSL_OP_SSLEAY_080_CLIENT_DH_BUG
))
1348 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
1358 if (n
== 0L) /* the parameters are in the cert */
1360 al
=SSL_AD_HANDSHAKE_FAILURE
;
1361 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_UNABLE_TO_DECODE_DH_CERTS
);
1366 if (s
->s3
->tmp
.dh
== NULL
)
1368 al
=SSL_AD_HANDSHAKE_FAILURE
;
1369 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
1373 dh_srvr
=s
->s3
->tmp
.dh
;
1376 pub
=BN_bin2bn(p
,i
,NULL
);
1379 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BN_LIB
);
1383 i
=DH_compute_key(p
,pub
,dh_srvr
);
1387 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1391 DH_free(s
->s3
->tmp
.dh
);
1396 s
->session
->master_key_length
=
1397 s
->method
->ssl3_enc
->generate_master_secret(s
,
1398 s
->session
->master_key
,p
,i
);
1403 al
=SSL_AD_HANDSHAKE_FAILURE
;
1404 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_UNKNOWN_CIPHER_TYPE
);
1410 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1411 #if !defined(NO_DH) || !defined(NO_RSA)
1417 static int ssl3_get_cert_verify(s
)
1420 EVP_PKEY
*pkey
=NULL
;
1427 n
=ssl3_get_message(s
,
1428 SSL3_ST_SR_CERT_VRFY_A
,
1429 SSL3_ST_SR_CERT_VRFY_B
,
1434 if (!ok
) return((int)n
);
1436 if (s
->session
->peer
!= NULL
)
1438 peer
=s
->session
->peer
;
1439 pkey
=X509_get_pubkey(peer
);
1440 type
=X509_certificate_type(peer
,pkey
);
1448 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_VERIFY
)
1450 s
->s3
->tmp
.reuse_message
=1;
1451 if ((peer
!= NULL
) && (type
| EVP_PKT_SIGN
))
1453 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1454 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_MISSING_VERIFY_MESSAGE
);
1463 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_NO_CLIENT_CERT_RECEIVED
);
1464 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1468 if (!(type
& EVP_PKT_SIGN
))
1470 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
1471 al
=SSL_AD_ILLEGAL_PARAMETER
;
1475 if (s
->s3
->change_cipher_spec
)
1477 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_CCS_RECEIVED_EARLY
);
1478 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1482 /* we now have a signature that we need to verify */
1483 p
=(unsigned char *)s
->init_buf
->data
;
1488 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_LENGTH_MISMATCH
);
1489 al
=SSL_AD_DECODE_ERROR
;
1493 j
=EVP_PKEY_size(pkey
);
1494 if ((i
> j
) || (n
> j
) || (n
<= 0))
1496 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_WRONG_SIGNATURE_SIZE
);
1497 al
=SSL_AD_DECODE_ERROR
;
1502 if (pkey
->type
== EVP_PKEY_RSA
)
1504 i
=RSA_public_decrypt(i
,p
,p
,pkey
->pkey
.rsa
,RSA_PKCS1_PADDING
);
1507 al
=SSL_AD_DECRYPT_ERROR
;
1508 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_DECRYPT
);
1511 if ((i
!= (MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
)) ||
1512 memcmp(&(s
->s3
->tmp
.finish_md
[0]),p
,
1513 MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
))
1515 al
=SSL_AD_DECRYPT_ERROR
;
1516 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_SIGNATURE
);
1523 if (pkey
->type
== EVP_PKEY_DSA
)
1525 j
=DSA_verify(pkey
->save_type
,
1526 &(s
->s3
->tmp
.finish_md
[MD5_DIGEST_LENGTH
]),
1527 SHA_DIGEST_LENGTH
,p
,i
,pkey
->pkey
.dsa
);
1531 al
=SSL_AD_DECRYPT_ERROR
;
1532 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_DSA_SIGNATURE
);
1539 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_INTERNAL_ERROR
);
1540 al
=SSL_AD_UNSUPPORTED_CERTIFICATE
;
1549 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1552 EVP_PKEY_free(pkey
);
1556 static int ssl3_get_client_certificate(s
)
1559 int i
,ok
,al
,ret
= -1;
1561 unsigned long l
,nc
,llen
,n
;
1562 unsigned char *p
,*d
,*q
;
1565 n
=ssl3_get_message(s
,
1569 #if defined(MSDOS) && !defined(WIN32)
1570 1024*30, /* 30k max cert list :-) */
1572 1024*100, /* 100k max cert list :-) */
1576 if (!ok
) return((int)n
);
1578 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_KEY_EXCHANGE
)
1580 if ( (s
->verify_mode
& SSL_VERIFY_PEER
) &&
1581 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
1583 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
1584 al
=SSL_AD_HANDSHAKE_FAILURE
;
1587 /* If tls asked for a client cert we must return a 0 list */
1588 if ((s
->version
> SSL3_VERSION
) && s
->s3
->tmp
.cert_request
)
1590 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
);
1591 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1594 s
->s3
->tmp
.reuse_message
=1;
1598 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
)
1600 al
=SSL_AD_UNEXPECTED_MESSAGE
;
1601 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_WRONG_MESSAGE_TYPE
);
1604 d
=p
=(unsigned char *)s
->init_buf
->data
;
1606 if ((sk
=sk_new_null()) == NULL
)
1608 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
1615 al
=SSL_AD_DECODE_ERROR
;
1616 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_LENGTH_MISMATCH
);
1619 for (nc
=0; nc
<llen
; )
1622 if ((l
+nc
+3) > llen
)
1624 al
=SSL_AD_DECODE_ERROR
;
1625 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
1630 x
=d2i_X509(NULL
,&p
,l
);
1633 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_ASN1_LIB
);
1638 al
=SSL_AD_DECODE_ERROR
;
1639 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
1642 if (!sk_push(sk
,(char *)x
))
1644 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
1651 if (sk_num(sk
) <= 0)
1653 /* TLS does not mind 0 certs returned */
1654 if (s
->version
== SSL3_VERSION
)
1656 al
=SSL_AD_HANDSHAKE_FAILURE
;
1657 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_NO_CERTIFICATES_RETURNED
);
1660 /* Fail for TLS only if we required a certificate */
1661 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
1662 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
1664 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
1665 al
=SSL_AD_HANDSHAKE_FAILURE
;
1671 i
=ssl_verify_cert_chain(s
,sk
);
1674 al
=ssl_verify_alarm_type(s
->verify_result
);
1675 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_NO_CERTIFICATE_RETURNED
);
1680 /* This should not be needed */
1681 if (s
->session
->peer
!= NULL
)
1682 X509_free(s
->session
->peer
);
1683 s
->session
->peer
=(X509
*)sk_shift(sk
);
1689 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1692 if (x
!= NULL
) X509_free(x
);
1693 if (sk
!= NULL
) sk_pop_free(sk
,X509_free
);
1697 int ssl3_send_server_certificate(s
)
1703 if (s
->state
== SSL3_ST_SW_CERT_A
)
1705 x
=ssl_get_server_send_cert(s
);
1708 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE
,SSL_R_INTERNAL_ERROR
);
1712 l
=ssl3_output_cert_chain(s
,x
);
1713 s
->state
=SSL3_ST_SW_CERT_B
;
1718 /* SSL3_ST_SW_CERT_B */
1719 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));