3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
6 /* ====================================================================
7 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60 * All rights reserved.
62 * This package is an SSL implementation written
63 * by Eric Young (eay@cryptsoft.com).
64 * The implementation was written so as to conform with Netscapes SSL.
66 * This library is free for commercial and non-commercial use as long as
67 * the following conditions are aheared to. The following conditions
68 * apply to all code found in this distribution, be it the RC4, RSA,
69 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
70 * included with this distribution is covered by the same copyright terms
71 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
73 * Copyright remains Eric Young's, and as such any Copyright notices in
74 * the code are not to be removed.
75 * If this package is used in a product, Eric Young should be given attribution
76 * as the author of the parts of the library used.
77 * This can be in the form of a textual message at program startup or
78 * in documentation (online or textual) provided with the package.
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
83 * 1. Redistributions of source code must retain the copyright
84 * notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
88 * 3. All advertising materials mentioning features or use of this software
89 * must display the following acknowledgement:
90 * "This product includes cryptographic software written by
91 * Eric Young (eay@cryptsoft.com)"
92 * The word 'cryptographic' can be left out if the rouines from the library
93 * being used are not cryptographic related :-).
94 * 4. If you include any Windows specific code (or a derivative thereof) from
95 * the apps directory (application code) you must include an acknowledgement:
96 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
98 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
110 * The licence and distribution terms for any publically available version or
111 * derivative of this code cannot be changed. i.e. this code cannot simply be
112 * copied and put under another distribution licence
113 * [including the GNU Public Licence.]
117 #include "ssl_locl.h"
118 #include <openssl/buffer.h>
119 #include <openssl/rand.h>
120 #include <openssl/objects.h>
121 #include <openssl/evp.h>
122 #include <openssl/x509.h>
123 #include <openssl/md5.h>
124 #include <openssl/bn.h>
125 #ifndef OPENSSL_NO_DH
126 # include <openssl/dh.h>
129 static const SSL_METHOD *dtls1_get_server_method(int ver);
130 static int dtls1_send_hello_verify_request(SSL *s);
132 static const SSL_METHOD *dtls1_get_server_method(int ver)
134 if (ver == DTLS1_VERSION)
135 return (DTLSv1_server_method());
140 IMPLEMENT_dtls1_meth_func(DTLSv1_server_method,
142 ssl_undefined_function, dtls1_get_server_method)
144 int dtls1_accept(SSL *s)
147 unsigned long Time = (unsigned long)time(NULL);
148 void (*cb) (const SSL *ssl, int type, int val) = NULL;
151 int new_state, state, skip = 0;
153 #ifndef OPENSSL_NO_SCTP
154 unsigned char sctpauthkey[64];
155 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
158 RAND_add(&Time, sizeof(Time), 0);
162 if (s->info_callback != NULL)
163 cb = s->info_callback;
164 else if (s->ctx->info_callback != NULL)
165 cb = s->ctx->info_callback;
167 listen = s->d1->listen;
169 /* init things to blank */
171 if (!SSL_in_init(s) || SSL_in_before(s))
174 s->d1->listen = listen;
175 #ifndef OPENSSL_NO_SCTP
177 * Notify SCTP BIO socket to enter handshake mode and prevent stream
178 * identifier other than 0. Will be ignored if no SCTP is used.
180 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
181 s->in_handshake, NULL);
184 if (s->cert == NULL) {
185 SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
188 #ifndef OPENSSL_NO_HEARTBEATS
190 * If we're awaiting a HeartbeatResponse, pretend we already got and
191 * don't await it anymore, because Heartbeats don't make sense during
194 if (s->tlsext_hb_pending) {
196 s->tlsext_hb_pending = 0;
205 case SSL_ST_RENEGOTIATE:
207 /* s->state=SSL_ST_ACCEPT; */
211 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
212 case SSL_ST_OK | SSL_ST_ACCEPT:
216 cb(s, SSL_CB_HANDSHAKE_START, 1);
218 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {
219 SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR);
222 s->type = SSL_ST_ACCEPT;
224 if (s->init_buf == NULL) {
225 if ((buf = BUF_MEM_new()) == NULL) {
227 s->state = SSL_ST_ERR;
230 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
233 s->state = SSL_ST_ERR;
239 if (!ssl3_setup_buffers(s)) {
241 s->state = SSL_ST_ERR;
246 s->d1->change_cipher_spec_ok = 0;
248 * Should have been reset by ssl3_get_finished, too.
250 s->s3->change_cipher_spec = 0;
252 if (s->state != SSL_ST_RENEGOTIATE) {
254 * Ok, we now need to push on a buffering BIO so that the
255 * output is sent in a way that TCP likes :-) ...but not with
258 #ifndef OPENSSL_NO_SCTP
259 if (!BIO_dgram_is_sctp(SSL_get_wbio(s)))
261 if (!ssl_init_wbio_buffer(s, 1)) {
263 s->state = SSL_ST_ERR;
267 ssl3_init_finished_mac(s);
268 s->state = SSL3_ST_SR_CLNT_HELLO_A;
269 s->ctx->stats.sess_accept++;
270 } else if (!s->s3->send_connection_binding &&
272 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
274 * Server attempting to renegotiate with client that doesn't
275 * support secure renegotiation.
277 SSLerr(SSL_F_DTLS1_ACCEPT,
278 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
279 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
281 s->state = SSL_ST_ERR;
285 * s->state == SSL_ST_RENEGOTIATE, we will just send a
288 s->ctx->stats.sess_accept_renegotiate++;
289 s->state = SSL3_ST_SW_HELLO_REQ_A;
294 case SSL3_ST_SW_HELLO_REQ_A:
295 case SSL3_ST_SW_HELLO_REQ_B:
298 dtls1_clear_sent_buffer(s);
299 dtls1_start_timer(s);
300 ret = dtls1_send_hello_request(s);
303 s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A;
304 s->state = SSL3_ST_SW_FLUSH;
307 ssl3_init_finished_mac(s);
310 case SSL3_ST_SW_HELLO_REQ_C:
311 s->state = SSL_ST_OK;
314 case SSL3_ST_SR_CLNT_HELLO_A:
315 case SSL3_ST_SR_CLNT_HELLO_B:
316 case SSL3_ST_SR_CLNT_HELLO_C:
319 ret = ssl3_get_client_hello(s);
324 if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
325 s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
327 s->state = SSL3_ST_SW_SRVR_HELLO_A;
332 * Reflect ClientHello sequence to remain stateless while
336 memcpy(s->s3->write_sequence, s->s3->read_sequence,
337 sizeof(s->s3->write_sequence));
340 /* If we're just listening, stop here */
341 if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A) {
345 * Set expected sequence numbers to continue the handshake.
347 s->d1->handshake_read_seq = 2;
348 s->d1->handshake_write_seq = 1;
349 s->d1->next_handshake_write_seq = 1;
355 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
356 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
358 ret = dtls1_send_hello_verify_request(s);
361 s->state = SSL3_ST_SW_FLUSH;
362 s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A;
364 /* HelloVerifyRequest resets Finished MAC */
365 if (s->version != DTLS1_BAD_VER)
366 ssl3_init_finished_mac(s);
369 #ifndef OPENSSL_NO_SCTP
370 case DTLS1_SCTP_ST_SR_READ_SOCK:
372 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
373 s->s3->in_read_app_data = 2;
374 s->rwstate = SSL_READING;
375 BIO_clear_retry_flags(SSL_get_rbio(s));
376 BIO_set_retry_read(SSL_get_rbio(s));
381 s->state = SSL3_ST_SR_FINISHED_A;
384 case DTLS1_SCTP_ST_SW_WRITE_SOCK:
385 ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
390 if (s->d1->next_state != SSL_ST_OK) {
391 s->s3->in_read_app_data = 2;
392 s->rwstate = SSL_READING;
393 BIO_clear_retry_flags(SSL_get_rbio(s));
394 BIO_set_retry_read(SSL_get_rbio(s));
400 s->state = s->d1->next_state;
404 case SSL3_ST_SW_SRVR_HELLO_A:
405 case SSL3_ST_SW_SRVR_HELLO_B:
407 dtls1_start_timer(s);
408 ret = dtls1_send_server_hello(s);
413 #ifndef OPENSSL_NO_SCTP
415 * Add new shared key for SCTP-Auth, will be ignored if no
418 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
419 DTLS1_SCTP_AUTH_LABEL);
421 if (SSL_export_keying_material(s, sctpauthkey,
422 sizeof(sctpauthkey), labelbuffer,
423 sizeof(labelbuffer), NULL, 0, 0) <= 0) {
425 s->state = SSL_ST_ERR;
429 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
430 sizeof(sctpauthkey), sctpauthkey);
432 #ifndef OPENSSL_NO_TLSEXT
433 if (s->tlsext_ticket_expected)
434 s->state = SSL3_ST_SW_SESSION_TICKET_A;
436 s->state = SSL3_ST_SW_CHANGE_A;
438 s->state = SSL3_ST_SW_CHANGE_A;
441 s->state = SSL3_ST_SW_CERT_A;
445 case SSL3_ST_SW_CERT_A:
446 case SSL3_ST_SW_CERT_B:
447 /* Check if it is anon DH or normal PSK */
448 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
449 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
450 dtls1_start_timer(s);
451 ret = dtls1_send_server_certificate(s);
454 #ifndef OPENSSL_NO_TLSEXT
455 if (s->tlsext_status_expected)
456 s->state = SSL3_ST_SW_CERT_STATUS_A;
458 s->state = SSL3_ST_SW_KEY_EXCH_A;
461 s->state = SSL3_ST_SW_KEY_EXCH_A;
467 s->state = SSL3_ST_SW_KEY_EXCH_A;
472 case SSL3_ST_SW_KEY_EXCH_A:
473 case SSL3_ST_SW_KEY_EXCH_B:
474 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
477 * clear this, it may get reset by
478 * send_server_key_exchange
480 s->s3->tmp.use_rsa_tmp = 0;
483 * only send if a DH key exchange or RSA but we have a sign only
488 * PSK: send ServerKeyExchange if PSK identity hint if
491 #ifndef OPENSSL_NO_PSK
492 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
494 || (alg_k & SSL_kEDH)
495 || (alg_k & SSL_kEECDH)
496 || ((alg_k & SSL_kRSA)
497 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
498 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
499 && EVP_PKEY_size(s->cert->pkeys
500 [SSL_PKEY_RSA_ENC].privatekey) *
501 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
506 dtls1_start_timer(s);
507 ret = dtls1_send_server_key_exchange(s);
513 s->state = SSL3_ST_SW_CERT_REQ_A;
517 case SSL3_ST_SW_CERT_REQ_A:
518 case SSL3_ST_SW_CERT_REQ_B:
519 if ( /* don't request cert unless asked for it: */
520 !(s->verify_mode & SSL_VERIFY_PEER) ||
522 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
523 * during re-negotiation:
525 ((s->session->peer != NULL) &&
526 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
528 * never request cert in anonymous ciphersuites (see
529 * section "Certificate request" in SSL 3 drafts and in
532 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
534 * ... except when the application insists on
535 * verification (against the specs, but s3_clnt.c accepts
538 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
540 * never request cert in Kerberos ciphersuites
542 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
544 * With normal PSK Certificates and Certificate Requests
547 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
548 /* no cert request */
550 s->s3->tmp.cert_request = 0;
551 s->state = SSL3_ST_SW_SRVR_DONE_A;
552 #ifndef OPENSSL_NO_SCTP
553 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
554 s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
555 s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
559 s->s3->tmp.cert_request = 1;
560 dtls1_start_timer(s);
561 ret = dtls1_send_certificate_request(s);
564 #ifndef NETSCAPE_HANG_BUG
565 s->state = SSL3_ST_SW_SRVR_DONE_A;
566 # ifndef OPENSSL_NO_SCTP
567 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
568 s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
569 s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
573 s->state = SSL3_ST_SW_FLUSH;
574 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
575 # ifndef OPENSSL_NO_SCTP
576 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
577 s->d1->next_state = s->s3->tmp.next_state;
578 s->s3->tmp.next_state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
586 case SSL3_ST_SW_SRVR_DONE_A:
587 case SSL3_ST_SW_SRVR_DONE_B:
588 dtls1_start_timer(s);
589 ret = dtls1_send_server_done(s);
592 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
593 s->state = SSL3_ST_SW_FLUSH;
597 case SSL3_ST_SW_FLUSH:
598 s->rwstate = SSL_WRITING;
599 if (BIO_flush(s->wbio) <= 0) {
601 * If the write error was fatal, stop trying
603 if (!BIO_should_retry(s->wbio)) {
604 s->rwstate = SSL_NOTHING;
605 s->state = s->s3->tmp.next_state;
611 s->rwstate = SSL_NOTHING;
612 s->state = s->s3->tmp.next_state;
615 case SSL3_ST_SR_CERT_A:
616 case SSL3_ST_SR_CERT_B:
617 /* Check for second client hello (MS SGC) */
618 ret = ssl3_check_client_hello(s);
623 s->state = SSL3_ST_SR_CLNT_HELLO_C;
625 if (s->s3->tmp.cert_request) {
626 ret = ssl3_get_client_certificate(s);
631 s->state = SSL3_ST_SR_KEY_EXCH_A;
635 case SSL3_ST_SR_KEY_EXCH_A:
636 case SSL3_ST_SR_KEY_EXCH_B:
637 ret = ssl3_get_client_key_exchange(s);
640 #ifndef OPENSSL_NO_SCTP
642 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
645 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
646 DTLS1_SCTP_AUTH_LABEL);
648 if (SSL_export_keying_material(s, sctpauthkey,
649 sizeof(sctpauthkey), labelbuffer,
650 sizeof(labelbuffer), NULL, 0, 0) <= 0) {
652 s->state = SSL_ST_ERR;
656 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
657 sizeof(sctpauthkey), sctpauthkey);
660 s->state = SSL3_ST_SR_CERT_VRFY_A;
665 * For the ECDH ciphersuites when the client sends its ECDH
666 * pub key in a certificate, the CertificateVerify message is
669 s->state = SSL3_ST_SR_FINISHED_A;
672 s->state = SSL3_ST_SR_CERT_VRFY_A;
676 * We need to get hashes here so if there is a client cert,
679 s->method->ssl3_enc->cert_verify_mac(s,
684 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1,
687 [MD5_DIGEST_LENGTH]));
691 case SSL3_ST_SR_CERT_VRFY_A:
692 case SSL3_ST_SR_CERT_VRFY_B:
693 ret = ssl3_get_cert_verify(s);
696 #ifndef OPENSSL_NO_SCTP
697 if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
698 state == SSL_ST_RENEGOTIATE)
699 s->state = DTLS1_SCTP_ST_SR_READ_SOCK;
702 s->state = SSL3_ST_SR_FINISHED_A;
706 case SSL3_ST_SR_FINISHED_A:
707 case SSL3_ST_SR_FINISHED_B:
709 * Enable CCS. Receiving a CCS clears the flag, so make
710 * sure not to re-enable it to ban duplicates. This *should* be the
711 * first time we have received one - but we check anyway to be
713 * s->s3->change_cipher_spec is set when a CCS is
714 * processed in d1_pkt.c, and remains set until
715 * the client's Finished message is read.
717 if (!s->s3->change_cipher_spec)
718 s->d1->change_cipher_spec_ok = 1;
719 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
720 SSL3_ST_SR_FINISHED_B);
725 s->state = SSL_ST_OK;
726 #ifndef OPENSSL_NO_TLSEXT
727 else if (s->tlsext_ticket_expected)
728 s->state = SSL3_ST_SW_SESSION_TICKET_A;
731 s->state = SSL3_ST_SW_CHANGE_A;
735 #ifndef OPENSSL_NO_TLSEXT
736 case SSL3_ST_SW_SESSION_TICKET_A:
737 case SSL3_ST_SW_SESSION_TICKET_B:
738 ret = dtls1_send_newsession_ticket(s);
741 s->state = SSL3_ST_SW_CHANGE_A;
745 case SSL3_ST_SW_CERT_STATUS_A:
746 case SSL3_ST_SW_CERT_STATUS_B:
747 ret = ssl3_send_cert_status(s);
750 s->state = SSL3_ST_SW_KEY_EXCH_A;
756 case SSL3_ST_SW_CHANGE_A:
757 case SSL3_ST_SW_CHANGE_B:
759 s->session->cipher = s->s3->tmp.new_cipher;
760 if (!s->method->ssl3_enc->setup_key_block(s)) {
762 s->state = SSL_ST_ERR;
766 ret = dtls1_send_change_cipher_spec(s,
768 SSL3_ST_SW_CHANGE_B);
773 #ifndef OPENSSL_NO_SCTP
776 * Change to new shared key of SCTP-Auth, will be ignored if
779 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
784 s->state = SSL3_ST_SW_FINISHED_A;
787 if (!s->method->ssl3_enc->change_cipher_state(s,
788 SSL3_CHANGE_CIPHER_SERVER_WRITE))
791 s->state = SSL_ST_ERR;
795 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
798 case SSL3_ST_SW_FINISHED_A:
799 case SSL3_ST_SW_FINISHED_B:
800 ret = dtls1_send_finished(s,
801 SSL3_ST_SW_FINISHED_A,
802 SSL3_ST_SW_FINISHED_B,
804 ssl3_enc->server_finished_label,
806 ssl3_enc->server_finished_label_len);
809 s->state = SSL3_ST_SW_FLUSH;
811 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
813 #ifndef OPENSSL_NO_SCTP
815 * Change to new shared key of SCTP-Auth, will be ignored if
818 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
822 s->s3->tmp.next_state = SSL_ST_OK;
823 #ifndef OPENSSL_NO_SCTP
824 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
825 s->d1->next_state = s->s3->tmp.next_state;
826 s->s3->tmp.next_state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
834 /* clean a few things up */
835 ssl3_cleanup_key_block(s);
838 BUF_MEM_free(s->init_buf);
842 /* remove buffering on output */
843 ssl_free_wbio_buffer(s);
847 if (s->renegotiate == 2) { /* skipped if we just sent a
852 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
854 s->ctx->stats.sess_accept_good++;
856 s->handshake_func = dtls1_accept;
859 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
864 /* done handshaking, next message is client hello */
865 s->d1->handshake_read_seq = 0;
866 /* next message is server hello */
867 s->d1->handshake_write_seq = 0;
868 s->d1->next_handshake_write_seq = 0;
869 dtls1_clear_received_buffer(s);
875 SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_UNKNOWN_STATE);
881 if (!s->s3->tmp.reuse_message && !skip) {
883 if ((ret = BIO_flush(s->wbio)) <= 0)
887 if ((cb != NULL) && (s->state != state)) {
888 new_state = s->state;
890 cb(s, SSL_CB_ACCEPT_LOOP, 1);
891 s->state = new_state;
897 /* BIO_flush(s->wbio); */
900 #ifndef OPENSSL_NO_SCTP
902 * Notify SCTP BIO socket to leave handshake mode and prevent stream
903 * identifier other than 0. Will be ignored if no SCTP is used.
905 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
906 s->in_handshake, NULL);
910 cb(s, SSL_CB_ACCEPT_EXIT, ret);
914 int dtls1_send_hello_request(SSL *s)
918 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
919 p = (unsigned char *)s->init_buf->data;
920 p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0);
922 s->state = SSL3_ST_SW_HELLO_REQ_B;
923 /* number of bytes to write */
924 s->init_num = DTLS1_HM_HEADER_LENGTH;
928 * no need to buffer this message, since there are no retransmit
933 /* SSL3_ST_SW_HELLO_REQ_B */
934 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
937 int dtls1_send_hello_verify_request(SSL *s)
939 unsigned int msg_len;
940 unsigned char *msg, *buf, *p;
942 if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) {
943 buf = (unsigned char *)s->init_buf->data;
945 msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
946 *(p++) = s->version >> 8;
947 *(p++) = s->version & 0xFF;
949 if (s->ctx->app_gen_cookie_cb == NULL ||
950 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
951 &(s->d1->cookie_len)) == 0) {
952 SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,
953 ERR_R_INTERNAL_ERROR);
954 s->state = SSL_ST_ERR;
958 *(p++) = (unsigned char)s->d1->cookie_len;
959 memcpy(p, s->d1->cookie, s->d1->cookie_len);
960 p += s->d1->cookie_len;
963 dtls1_set_message_header(s, buf,
964 DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0,
967 s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
968 /* number of bytes to write */
969 s->init_num = p - buf;
973 /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
974 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
977 int dtls1_send_server_hello(SSL *s)
980 unsigned char *p, *d;
985 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
986 buf = (unsigned char *)s->init_buf->data;
987 p = s->s3->server_random;
988 ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE);
989 /* Do the message type and length last */
990 d = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
992 *(p++) = s->version >> 8;
993 *(p++) = s->version & 0xff;
996 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
997 p += SSL3_RANDOM_SIZE;
1000 * now in theory we have 3 options to sending back the session id.
1001 * If it is a re-use, we send back the old session-id, if it is a new
1002 * session, we send back the new session-id or we send back a 0
1003 * length session-id if we want it to be single use. Currently I will
1004 * not implement the '0' length session-id 12-Jan-98 - I'll now
1005 * support the '0' length stuff.
1007 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
1008 s->session->session_id_length = 0;
1010 sl = s->session->session_id_length;
1011 if (sl > sizeof s->session->session_id) {
1012 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1016 memcpy(p, s->session->session_id, sl);
1019 /* put the cipher */
1020 if (s->s3->tmp.new_cipher == NULL)
1022 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1025 /* put the compression method */
1026 #ifdef OPENSSL_NO_COMP
1029 if (s->s3->tmp.new_compression == NULL)
1032 *(p++) = s->s3->tmp.new_compression->id;
1035 #ifndef OPENSSL_NO_TLSEXT
1036 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1037 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1041 ssl_add_serverhello_tlsext(s, p,
1042 buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
1044 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1053 d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l);
1055 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1056 /* number of bytes to write */
1057 s->init_num = p - buf;
1060 /* buffer the message to handle re-xmits */
1061 dtls1_buffer_message(s, 0);
1064 /* SSL3_ST_SW_SRVR_HELLO_B */
1065 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1068 int dtls1_send_server_done(SSL *s)
1072 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1073 p = (unsigned char *)s->init_buf->data;
1076 p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0);
1078 s->state = SSL3_ST_SW_SRVR_DONE_B;
1079 /* number of bytes to write */
1080 s->init_num = DTLS1_HM_HEADER_LENGTH;
1083 /* buffer the message to handle re-xmits */
1084 dtls1_buffer_message(s, 0);
1087 /* SSL3_ST_SW_SRVR_DONE_B */
1088 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1091 int dtls1_send_server_key_exchange(SSL *s)
1093 #ifndef OPENSSL_NO_RSA
1097 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1100 #ifndef OPENSSL_NO_DH
1101 DH *dh = NULL, *dhp;
1103 #ifndef OPENSSL_NO_ECDH
1104 EC_KEY *ecdh = NULL, *ecdhp;
1105 unsigned char *encodedPoint = NULL;
1108 BN_CTX *bn_ctx = NULL;
1111 unsigned char *p, *d;
1121 EVP_MD_CTX_init(&md_ctx);
1122 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1123 type = s->s3->tmp.new_cipher->algorithm_mkey;
1128 r[0] = r[1] = r[2] = r[3] = NULL;
1130 #ifndef OPENSSL_NO_RSA
1131 if (type & SSL_kRSA) {
1132 rsa = cert->rsa_tmp;
1133 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1134 rsa = s->cert->rsa_tmp_cb(s,
1135 SSL_C_IS_EXPORT(s->s3->
1137 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1140 al = SSL_AD_HANDSHAKE_FAILURE;
1141 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1142 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1146 cert->rsa_tmp = rsa;
1149 al = SSL_AD_HANDSHAKE_FAILURE;
1150 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1151 SSL_R_MISSING_TMP_RSA_KEY);
1156 s->s3->tmp.use_rsa_tmp = 1;
1159 #ifndef OPENSSL_NO_DH
1160 if (type & SSL_kEDH) {
1162 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1163 dhp = s->cert->dh_tmp_cb(s,
1164 SSL_C_IS_EXPORT(s->s3->
1166 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1169 al = SSL_AD_HANDSHAKE_FAILURE;
1170 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1171 SSL_R_MISSING_TMP_DH_KEY);
1175 if (s->s3->tmp.dh != NULL) {
1177 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1178 ERR_R_INTERNAL_ERROR);
1182 if ((dh = DHparams_dup(dhp)) == NULL) {
1183 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1188 if ((dhp->pub_key == NULL ||
1189 dhp->priv_key == NULL ||
1190 (s->options & SSL_OP_SINGLE_DH_USE))) {
1191 if (!DH_generate_key(dh)) {
1192 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1197 dh->pub_key = BN_dup(dhp->pub_key);
1198 dh->priv_key = BN_dup(dhp->priv_key);
1199 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1200 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1210 #ifndef OPENSSL_NO_ECDH
1211 if (type & SSL_kEECDH) {
1212 const EC_GROUP *group;
1214 ecdhp = cert->ecdh_tmp;
1215 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) {
1216 ecdhp = s->cert->ecdh_tmp_cb(s,
1217 SSL_C_IS_EXPORT(s->s3->
1219 SSL_C_EXPORT_PKEYLENGTH(s->
1220 s3->tmp.new_cipher));
1222 if (ecdhp == NULL) {
1223 al = SSL_AD_HANDSHAKE_FAILURE;
1224 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1225 SSL_R_MISSING_TMP_ECDH_KEY);
1229 if (s->s3->tmp.ecdh != NULL) {
1230 EC_KEY_free(s->s3->tmp.ecdh);
1231 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1232 ERR_R_INTERNAL_ERROR);
1236 /* Duplicate the ECDH structure. */
1237 if (ecdhp == NULL) {
1238 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1241 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1242 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1246 s->s3->tmp.ecdh = ecdh;
1247 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1248 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1249 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1250 if (!EC_KEY_generate_key(ecdh)) {
1251 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1257 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1258 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1259 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1260 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1264 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1265 (EC_GROUP_get_degree(group) > 163)) {
1266 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1267 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1272 * XXX: For now, we only support ephemeral ECDH keys over named
1273 * (not generic) curves. For supported named curves, curve_id is
1277 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1279 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1280 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1285 * Encode the public key. First check the size of encoding and
1286 * allocate memory accordingly.
1288 encodedlen = EC_POINT_point2oct(group,
1289 EC_KEY_get0_public_key(ecdh),
1290 POINT_CONVERSION_UNCOMPRESSED,
1293 encodedPoint = (unsigned char *)
1294 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1295 bn_ctx = BN_CTX_new();
1296 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1297 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1298 ERR_R_MALLOC_FAILURE);
1302 encodedlen = EC_POINT_point2oct(group,
1303 EC_KEY_get0_public_key(ecdh),
1304 POINT_CONVERSION_UNCOMPRESSED,
1305 encodedPoint, encodedlen, bn_ctx);
1307 if (encodedlen == 0) {
1308 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1312 BN_CTX_free(bn_ctx);
1316 * XXX: For now, we only support named (not generic) curves in
1317 * ECDH ephemeral key exchanges. In this situation, we need four
1318 * additional bytes to encode the entire ServerECDHParams
1324 * We'll generate the serverKeyExchange message explicitly so we
1325 * can set these to NULLs
1332 #endif /* !OPENSSL_NO_ECDH */
1333 #ifndef OPENSSL_NO_PSK
1334 if (type & SSL_kPSK) {
1336 * reserve size for record length and PSK identity hint
1338 n += 2 + strlen(s->ctx->psk_identity_hint);
1340 #endif /* !OPENSSL_NO_PSK */
1342 al = SSL_AD_HANDSHAKE_FAILURE;
1343 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1344 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1347 for (i = 0; r[i] != NULL; i++) {
1348 nr[i] = BN_num_bytes(r[i]);
1352 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1353 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
1354 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, NULL))
1356 al = SSL_AD_DECODE_ERROR;
1359 kn = EVP_PKEY_size(pkey);
1365 if (!BUF_MEM_grow_clean(buf, n + DTLS1_HM_HEADER_LENGTH + kn)) {
1366 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1369 d = (unsigned char *)s->init_buf->data;
1370 p = &(d[DTLS1_HM_HEADER_LENGTH]);
1372 for (i = 0; r[i] != NULL; i++) {
1378 #ifndef OPENSSL_NO_ECDH
1379 if (type & SSL_kEECDH) {
1381 * XXX: For now, we only support named (not generic) curves. In
1382 * this situation, the serverKeyExchange message has: [1 byte
1383 * CurveType], [2 byte CurveName] [1 byte length of encoded
1384 * point], followed by the actual encoded point itself
1386 *p = NAMED_CURVE_TYPE;
1394 memcpy((unsigned char *)p,
1395 (unsigned char *)encodedPoint, encodedlen);
1396 OPENSSL_free(encodedPoint);
1397 encodedPoint = NULL;
1402 #ifndef OPENSSL_NO_PSK
1403 if (type & SSL_kPSK) {
1404 /* copy PSK identity hint */
1405 s2n(strlen(s->ctx->psk_identity_hint), p);
1406 strncpy((char *)p, s->ctx->psk_identity_hint,
1407 strlen(s->ctx->psk_identity_hint));
1408 p += strlen(s->ctx->psk_identity_hint);
1415 * n is the length of the params, they start at
1416 * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space at the
1419 #ifndef OPENSSL_NO_RSA
1420 if (pkey->type == EVP_PKEY_RSA) {
1423 for (num = 2; num > 0; num--) {
1424 EVP_DigestInit_ex(&md_ctx, (num == 2)
1425 ? s->ctx->md5 : s->ctx->sha1, NULL);
1426 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1428 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1430 EVP_DigestUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]),
1432 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1436 if (RSA_sign(NID_md5_sha1, md_buf, j,
1437 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1438 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
1445 #if !defined(OPENSSL_NO_DSA)
1446 if (pkey->type == EVP_PKEY_DSA) {
1448 EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL);
1449 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1451 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1453 EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n);
1454 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1455 (unsigned int *)&i, pkey)) {
1456 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA);
1463 #if !defined(OPENSSL_NO_ECDSA)
1464 if (pkey->type == EVP_PKEY_EC) {
1465 /* let's do ECDSA */
1466 EVP_SignInit_ex(&md_ctx, EVP_ecdsa(), NULL);
1467 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1469 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1471 EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n);
1472 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1473 (unsigned int *)&i, pkey)) {
1474 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1483 /* Is this error check actually needed? */
1484 al = SSL_AD_HANDSHAKE_FAILURE;
1485 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1486 SSL_R_UNKNOWN_PKEY_TYPE);
1491 d = dtls1_set_message_header(s, d,
1492 SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);
1495 * we should now have things packed up, so lets send it off
1497 s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1500 /* buffer the message to handle re-xmits */
1501 dtls1_buffer_message(s, 0);
1504 s->state = SSL3_ST_SW_KEY_EXCH_B;
1505 EVP_MD_CTX_cleanup(&md_ctx);
1506 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1508 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1510 #ifndef OPENSSL_NO_ECDH
1511 if (encodedPoint != NULL)
1512 OPENSSL_free(encodedPoint);
1513 BN_CTX_free(bn_ctx);
1515 EVP_MD_CTX_cleanup(&md_ctx);
1519 int dtls1_send_certificate_request(SSL *s)
1521 unsigned char *p, *d;
1522 int i, j, nl, off, n;
1523 STACK_OF(X509_NAME) *sk = NULL;
1526 unsigned int msg_len;
1528 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1531 d = p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
1533 /* get the list of acceptable cert types */
1535 n = ssl3_get_req_cert_type(s, p);
1544 sk = SSL_get_client_CA_list(s);
1547 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1548 name = sk_X509_NAME_value(sk, i);
1549 j = i2d_X509_NAME(name, NULL);
1550 if (!BUF_MEM_grow_clean
1551 (buf, DTLS1_HM_HEADER_LENGTH + n + j + 2)) {
1552 SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,
1556 p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + n]);
1557 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
1559 i2d_X509_NAME(name, &p);
1564 i2d_X509_NAME(name, &p);
1573 /* else no CA names */
1574 p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + off]);
1577 d = (unsigned char *)buf->data;
1578 *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
1580 s2n(s->d1->handshake_write_seq, d);
1581 s->d1->handshake_write_seq++;
1584 * we should now have things packed up, so lets send it off
1587 s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1589 #ifdef NETSCAPE_HANG_BUG
1590 /* XXX: what to do about this? */
1591 p = (unsigned char *)s->init_buf->data + s->init_num;
1594 *(p++) = SSL3_MT_SERVER_DONE;
1601 /* XDTLS: set message header ? */
1602 msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
1603 dtls1_set_message_header(s, (void *)s->init_buf->data,
1604 SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0,
1607 /* buffer the message to handle re-xmits */
1608 dtls1_buffer_message(s, 0);
1610 s->state = SSL3_ST_SW_CERT_REQ_B;
1613 /* SSL3_ST_SW_CERT_REQ_B */
1614 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1619 int dtls1_send_server_certificate(SSL *s)
1624 if (s->state == SSL3_ST_SW_CERT_A) {
1625 x = ssl_get_server_send_cert(s);
1627 /* VRS: allow null cert if auth == KRB5 */
1628 if ((s->s3->tmp.new_cipher->algorithm_mkey != SSL_kKRB5) ||
1629 (s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5)) {
1630 SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,
1631 ERR_R_INTERNAL_ERROR);
1636 l = dtls1_output_cert_chain(s, x);
1638 SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1641 s->state = SSL3_ST_SW_CERT_B;
1642 s->init_num = (int)l;
1645 /* buffer the message to handle re-xmits */
1646 dtls1_buffer_message(s, 0);
1649 /* SSL3_ST_SW_CERT_B */
1650 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1653 #ifndef OPENSSL_NO_TLSEXT
1654 int dtls1_send_newsession_ticket(SSL *s)
1656 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
1657 unsigned char *p, *senc, *macstart;
1659 unsigned int hlen, msg_len;
1662 SSL_CTX *tctx = s->initial_ctx;
1663 unsigned char iv[EVP_MAX_IV_LENGTH];
1664 unsigned char key_name[16];
1666 /* get session encoding length */
1667 slen = i2d_SSL_SESSION(s->session, NULL);
1669 * Some length values are 16 bits, so forget it if session is too
1675 * Grow buffer if need be: the length calculation is as follows 12
1676 * (DTLS handshake message header) + 4 (ticket lifetime hint) + 2
1677 * (ticket length) + 16 (key name) + max_iv_len (iv length) +
1678 * session_length + max_enc_block_size (max encrypted session length)
1679 * + max_md_size (HMAC).
1681 if (!BUF_MEM_grow(s->init_buf,
1682 DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH +
1683 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
1685 senc = OPENSSL_malloc(slen);
1689 i2d_SSL_SESSION(s->session, &p);
1691 p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]);
1692 EVP_CIPHER_CTX_init(&ctx);
1693 HMAC_CTX_init(&hctx);
1695 * Initialize HMAC and cipher contexts. If callback present it does
1696 * all the work otherwise use generated values from parent ctx.
1698 if (tctx->tlsext_ticket_key_cb) {
1699 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
1705 RAND_pseudo_bytes(iv, 16);
1706 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
1707 tctx->tlsext_tick_aes_key, iv);
1708 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
1709 tlsext_tick_md(), NULL);
1710 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
1712 l2n(s->session->tlsext_tick_lifetime_hint, p);
1713 /* Skip ticket length for now */
1715 /* Output key name */
1717 memcpy(p, key_name, 16);
1720 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
1721 p += EVP_CIPHER_CTX_iv_length(&ctx);
1722 /* Encrypt session data */
1723 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
1725 EVP_EncryptFinal(&ctx, p, &len);
1727 EVP_CIPHER_CTX_cleanup(&ctx);
1729 HMAC_Update(&hctx, macstart, p - macstart);
1730 HMAC_Final(&hctx, p, &hlen);
1731 HMAC_CTX_cleanup(&hctx);
1734 /* Now write out lengths: p points to end of data written */
1736 len = p - (unsigned char *)(s->init_buf->data);
1738 p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4;
1739 s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p);
1741 /* number of bytes to write */
1743 s->state = SSL3_ST_SW_SESSION_TICKET_B;
1747 /* XDTLS: set message header ? */
1748 msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
1749 dtls1_set_message_header(s, (void *)s->init_buf->data,
1750 SSL3_MT_NEWSESSION_TICKET, msg_len, 0,
1753 /* buffer the message to handle re-xmits */
1754 dtls1_buffer_message(s, 0);
1757 /* SSL3_ST_SW_SESSION_TICKET_B */
1758 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));