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++;
272 * s->state == SSL_ST_RENEGOTIATE, we will just send a
275 s->ctx->stats.sess_accept_renegotiate++;
276 s->state = SSL3_ST_SW_HELLO_REQ_A;
281 case SSL3_ST_SW_HELLO_REQ_A:
282 case SSL3_ST_SW_HELLO_REQ_B:
285 dtls1_clear_record_buffer(s);
286 dtls1_start_timer(s);
287 ret = dtls1_send_hello_request(s);
290 s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A;
291 s->state = SSL3_ST_SW_FLUSH;
294 ssl3_init_finished_mac(s);
297 case SSL3_ST_SW_HELLO_REQ_C:
298 s->state = SSL_ST_OK;
301 case SSL3_ST_SR_CLNT_HELLO_A:
302 case SSL3_ST_SR_CLNT_HELLO_B:
303 case SSL3_ST_SR_CLNT_HELLO_C:
306 ret = ssl3_get_client_hello(s);
311 if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
312 s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
314 s->state = SSL3_ST_SW_SRVR_HELLO_A;
319 * Reflect ClientHello sequence to remain stateless while
323 memcpy(s->s3->write_sequence, s->s3->read_sequence,
324 sizeof(s->s3->write_sequence));
327 /* If we're just listening, stop here */
328 if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A) {
332 * Set expected sequence numbers to continue the handshake.
334 s->d1->handshake_read_seq = 2;
335 s->d1->handshake_write_seq = 1;
336 s->d1->next_handshake_write_seq = 1;
342 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
343 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
345 ret = dtls1_send_hello_verify_request(s);
348 s->state = SSL3_ST_SW_FLUSH;
349 s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A;
351 /* HelloVerifyRequest resets Finished MAC */
352 if (s->version != DTLS1_BAD_VER)
353 ssl3_init_finished_mac(s);
356 #ifndef OPENSSL_NO_SCTP
357 case DTLS1_SCTP_ST_SR_READ_SOCK:
359 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
360 s->s3->in_read_app_data = 2;
361 s->rwstate = SSL_READING;
362 BIO_clear_retry_flags(SSL_get_rbio(s));
363 BIO_set_retry_read(SSL_get_rbio(s));
368 s->state = SSL3_ST_SR_FINISHED_A;
371 case DTLS1_SCTP_ST_SW_WRITE_SOCK:
372 ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
377 if (s->d1->next_state != SSL_ST_OK) {
378 s->s3->in_read_app_data = 2;
379 s->rwstate = SSL_READING;
380 BIO_clear_retry_flags(SSL_get_rbio(s));
381 BIO_set_retry_read(SSL_get_rbio(s));
387 s->state = s->d1->next_state;
391 case SSL3_ST_SW_SRVR_HELLO_A:
392 case SSL3_ST_SW_SRVR_HELLO_B:
394 dtls1_start_timer(s);
395 ret = dtls1_send_server_hello(s);
400 #ifndef OPENSSL_NO_SCTP
402 * Add new shared key for SCTP-Auth, will be ignored if no
405 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
406 DTLS1_SCTP_AUTH_LABEL);
408 if (SSL_export_keying_material(s, sctpauthkey,
409 sizeof(sctpauthkey), labelbuffer,
410 sizeof(labelbuffer), NULL, 0, 0) <= 0) {
412 s->state = SSL_ST_ERR;
416 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
417 sizeof(sctpauthkey), sctpauthkey);
419 #ifndef OPENSSL_NO_TLSEXT
420 if (s->tlsext_ticket_expected)
421 s->state = SSL3_ST_SW_SESSION_TICKET_A;
423 s->state = SSL3_ST_SW_CHANGE_A;
425 s->state = SSL3_ST_SW_CHANGE_A;
428 s->state = SSL3_ST_SW_CERT_A;
432 case SSL3_ST_SW_CERT_A:
433 case SSL3_ST_SW_CERT_B:
434 /* Check if it is anon DH or normal PSK */
435 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
436 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
437 dtls1_start_timer(s);
438 ret = dtls1_send_server_certificate(s);
441 #ifndef OPENSSL_NO_TLSEXT
442 if (s->tlsext_status_expected)
443 s->state = SSL3_ST_SW_CERT_STATUS_A;
445 s->state = SSL3_ST_SW_KEY_EXCH_A;
448 s->state = SSL3_ST_SW_KEY_EXCH_A;
454 s->state = SSL3_ST_SW_KEY_EXCH_A;
459 case SSL3_ST_SW_KEY_EXCH_A:
460 case SSL3_ST_SW_KEY_EXCH_B:
461 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
464 * clear this, it may get reset by
465 * send_server_key_exchange
467 s->s3->tmp.use_rsa_tmp = 0;
470 * only send if a DH key exchange or RSA but we have a sign only
475 * PSK: send ServerKeyExchange if PSK identity hint if
478 #ifndef OPENSSL_NO_PSK
479 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
481 || (alg_k & SSL_kEDH)
482 || (alg_k & SSL_kEECDH)
483 || ((alg_k & SSL_kRSA)
484 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
485 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
486 && EVP_PKEY_size(s->cert->pkeys
487 [SSL_PKEY_RSA_ENC].privatekey) *
488 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
493 dtls1_start_timer(s);
494 ret = dtls1_send_server_key_exchange(s);
500 s->state = SSL3_ST_SW_CERT_REQ_A;
504 case SSL3_ST_SW_CERT_REQ_A:
505 case SSL3_ST_SW_CERT_REQ_B:
506 if ( /* don't request cert unless asked for it: */
507 !(s->verify_mode & SSL_VERIFY_PEER) ||
509 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
510 * during re-negotiation:
512 ((s->session->peer != NULL) &&
513 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
515 * never request cert in anonymous ciphersuites (see
516 * section "Certificate request" in SSL 3 drafts and in
519 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
521 * ... except when the application insists on
522 * verification (against the specs, but s3_clnt.c accepts
525 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
527 * never request cert in Kerberos ciphersuites
529 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
531 * With normal PSK Certificates and Certificate Requests
534 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
535 /* no cert request */
537 s->s3->tmp.cert_request = 0;
538 s->state = SSL3_ST_SW_SRVR_DONE_A;
539 #ifndef OPENSSL_NO_SCTP
540 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
541 s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
542 s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
546 s->s3->tmp.cert_request = 1;
547 dtls1_start_timer(s);
548 ret = dtls1_send_certificate_request(s);
551 #ifndef NETSCAPE_HANG_BUG
552 s->state = SSL3_ST_SW_SRVR_DONE_A;
553 # ifndef OPENSSL_NO_SCTP
554 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
555 s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
556 s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
560 s->state = SSL3_ST_SW_FLUSH;
561 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
562 # ifndef OPENSSL_NO_SCTP
563 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
564 s->d1->next_state = s->s3->tmp.next_state;
565 s->s3->tmp.next_state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
573 case SSL3_ST_SW_SRVR_DONE_A:
574 case SSL3_ST_SW_SRVR_DONE_B:
575 dtls1_start_timer(s);
576 ret = dtls1_send_server_done(s);
579 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
580 s->state = SSL3_ST_SW_FLUSH;
584 case SSL3_ST_SW_FLUSH:
585 s->rwstate = SSL_WRITING;
586 if (BIO_flush(s->wbio) <= 0) {
588 * If the write error was fatal, stop trying
590 if (!BIO_should_retry(s->wbio)) {
591 s->rwstate = SSL_NOTHING;
592 s->state = s->s3->tmp.next_state;
598 s->rwstate = SSL_NOTHING;
599 s->state = s->s3->tmp.next_state;
602 case SSL3_ST_SR_CERT_A:
603 case SSL3_ST_SR_CERT_B:
604 /* Check for second client hello (MS SGC) */
605 ret = ssl3_check_client_hello(s);
610 s->state = SSL3_ST_SR_CLNT_HELLO_C;
612 if (s->s3->tmp.cert_request) {
613 ret = ssl3_get_client_certificate(s);
618 s->state = SSL3_ST_SR_KEY_EXCH_A;
622 case SSL3_ST_SR_KEY_EXCH_A:
623 case SSL3_ST_SR_KEY_EXCH_B:
624 ret = ssl3_get_client_key_exchange(s);
627 #ifndef OPENSSL_NO_SCTP
629 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
632 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
633 DTLS1_SCTP_AUTH_LABEL);
635 if (SSL_export_keying_material(s, sctpauthkey,
636 sizeof(sctpauthkey), labelbuffer,
637 sizeof(labelbuffer), NULL, 0, 0) <= 0) {
639 s->state = SSL_ST_ERR;
643 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
644 sizeof(sctpauthkey), sctpauthkey);
647 s->state = SSL3_ST_SR_CERT_VRFY_A;
652 * For the ECDH ciphersuites when the client sends its ECDH
653 * pub key in a certificate, the CertificateVerify message is
656 s->state = SSL3_ST_SR_FINISHED_A;
659 s->state = SSL3_ST_SR_CERT_VRFY_A;
663 * We need to get hashes here so if there is a client cert,
666 s->method->ssl3_enc->cert_verify_mac(s,
671 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1,
674 [MD5_DIGEST_LENGTH]));
678 case SSL3_ST_SR_CERT_VRFY_A:
679 case SSL3_ST_SR_CERT_VRFY_B:
680 ret = ssl3_get_cert_verify(s);
683 #ifndef OPENSSL_NO_SCTP
684 if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
685 state == SSL_ST_RENEGOTIATE)
686 s->state = DTLS1_SCTP_ST_SR_READ_SOCK;
689 s->state = SSL3_ST_SR_FINISHED_A;
693 case SSL3_ST_SR_FINISHED_A:
694 case SSL3_ST_SR_FINISHED_B:
696 * Enable CCS. Receiving a CCS clears the flag, so make
697 * sure not to re-enable it to ban duplicates. This *should* be the
698 * first time we have received one - but we check anyway to be
700 * s->s3->change_cipher_spec is set when a CCS is
701 * processed in d1_pkt.c, and remains set until
702 * the client's Finished message is read.
704 if (!s->s3->change_cipher_spec)
705 s->d1->change_cipher_spec_ok = 1;
706 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
707 SSL3_ST_SR_FINISHED_B);
712 s->state = SSL_ST_OK;
713 #ifndef OPENSSL_NO_TLSEXT
714 else if (s->tlsext_ticket_expected)
715 s->state = SSL3_ST_SW_SESSION_TICKET_A;
718 s->state = SSL3_ST_SW_CHANGE_A;
722 #ifndef OPENSSL_NO_TLSEXT
723 case SSL3_ST_SW_SESSION_TICKET_A:
724 case SSL3_ST_SW_SESSION_TICKET_B:
725 ret = dtls1_send_newsession_ticket(s);
728 s->state = SSL3_ST_SW_CHANGE_A;
732 case SSL3_ST_SW_CERT_STATUS_A:
733 case SSL3_ST_SW_CERT_STATUS_B:
734 ret = ssl3_send_cert_status(s);
737 s->state = SSL3_ST_SW_KEY_EXCH_A;
743 case SSL3_ST_SW_CHANGE_A:
744 case SSL3_ST_SW_CHANGE_B:
746 s->session->cipher = s->s3->tmp.new_cipher;
747 if (!s->method->ssl3_enc->setup_key_block(s)) {
749 s->state = SSL_ST_ERR;
753 ret = dtls1_send_change_cipher_spec(s,
755 SSL3_ST_SW_CHANGE_B);
760 #ifndef OPENSSL_NO_SCTP
763 * Change to new shared key of SCTP-Auth, will be ignored if
766 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
771 s->state = SSL3_ST_SW_FINISHED_A;
774 if (!s->method->ssl3_enc->change_cipher_state(s,
775 SSL3_CHANGE_CIPHER_SERVER_WRITE))
778 s->state = SSL_ST_ERR;
782 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
785 case SSL3_ST_SW_FINISHED_A:
786 case SSL3_ST_SW_FINISHED_B:
787 ret = dtls1_send_finished(s,
788 SSL3_ST_SW_FINISHED_A,
789 SSL3_ST_SW_FINISHED_B,
791 ssl3_enc->server_finished_label,
793 ssl3_enc->server_finished_label_len);
796 s->state = SSL3_ST_SW_FLUSH;
798 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
800 #ifndef OPENSSL_NO_SCTP
802 * Change to new shared key of SCTP-Auth, will be ignored if
805 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
809 s->s3->tmp.next_state = SSL_ST_OK;
810 #ifndef OPENSSL_NO_SCTP
811 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
812 s->d1->next_state = s->s3->tmp.next_state;
813 s->s3->tmp.next_state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
821 /* clean a few things up */
822 ssl3_cleanup_key_block(s);
825 BUF_MEM_free(s->init_buf);
829 /* remove buffering on output */
830 ssl_free_wbio_buffer(s);
834 if (s->renegotiate == 2) { /* skipped if we just sent a
839 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
841 s->ctx->stats.sess_accept_good++;
843 s->handshake_func = dtls1_accept;
846 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
851 /* done handshaking, next message is client hello */
852 s->d1->handshake_read_seq = 0;
853 /* next message is server hello */
854 s->d1->handshake_write_seq = 0;
855 s->d1->next_handshake_write_seq = 0;
861 SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_UNKNOWN_STATE);
867 if (!s->s3->tmp.reuse_message && !skip) {
869 if ((ret = BIO_flush(s->wbio)) <= 0)
873 if ((cb != NULL) && (s->state != state)) {
874 new_state = s->state;
876 cb(s, SSL_CB_ACCEPT_LOOP, 1);
877 s->state = new_state;
883 /* BIO_flush(s->wbio); */
886 #ifndef OPENSSL_NO_SCTP
888 * Notify SCTP BIO socket to leave handshake mode and prevent stream
889 * identifier other than 0. Will be ignored if no SCTP is used.
891 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
892 s->in_handshake, NULL);
896 cb(s, SSL_CB_ACCEPT_EXIT, ret);
900 int dtls1_send_hello_request(SSL *s)
904 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
905 p = (unsigned char *)s->init_buf->data;
906 p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0);
908 s->state = SSL3_ST_SW_HELLO_REQ_B;
909 /* number of bytes to write */
910 s->init_num = DTLS1_HM_HEADER_LENGTH;
914 * no need to buffer this message, since there are no retransmit
919 /* SSL3_ST_SW_HELLO_REQ_B */
920 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
923 int dtls1_send_hello_verify_request(SSL *s)
925 unsigned int msg_len;
926 unsigned char *msg, *buf, *p;
928 if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) {
929 buf = (unsigned char *)s->init_buf->data;
931 msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
932 *(p++) = s->version >> 8;
933 *(p++) = s->version & 0xFF;
935 if (s->ctx->app_gen_cookie_cb == NULL ||
936 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
937 &(s->d1->cookie_len)) == 0) {
938 SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,
939 ERR_R_INTERNAL_ERROR);
940 s->state = SSL_ST_ERR;
944 *(p++) = (unsigned char)s->d1->cookie_len;
945 memcpy(p, s->d1->cookie, s->d1->cookie_len);
946 p += s->d1->cookie_len;
949 dtls1_set_message_header(s, buf,
950 DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0,
953 s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
954 /* number of bytes to write */
955 s->init_num = p - buf;
959 /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
960 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
963 int dtls1_send_server_hello(SSL *s)
966 unsigned char *p, *d;
971 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
972 buf = (unsigned char *)s->init_buf->data;
973 p = s->s3->server_random;
974 ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE);
975 /* Do the message type and length last */
976 d = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
978 *(p++) = s->version >> 8;
979 *(p++) = s->version & 0xff;
982 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
983 p += SSL3_RANDOM_SIZE;
986 * now in theory we have 3 options to sending back the session id.
987 * If it is a re-use, we send back the old session-id, if it is a new
988 * session, we send back the new session-id or we send back a 0
989 * length session-id if we want it to be single use. Currently I will
990 * not implement the '0' length session-id 12-Jan-98 - I'll now
991 * support the '0' length stuff.
993 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
994 s->session->session_id_length = 0;
996 sl = s->session->session_id_length;
997 if (sl > sizeof s->session->session_id) {
998 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1002 memcpy(p, s->session->session_id, sl);
1005 /* put the cipher */
1006 if (s->s3->tmp.new_cipher == NULL)
1008 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1011 /* put the compression method */
1012 #ifdef OPENSSL_NO_COMP
1015 if (s->s3->tmp.new_compression == NULL)
1018 *(p++) = s->s3->tmp.new_compression->id;
1021 #ifndef OPENSSL_NO_TLSEXT
1022 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1023 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1027 ssl_add_serverhello_tlsext(s, p,
1028 buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
1030 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1039 d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l);
1041 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1042 /* number of bytes to write */
1043 s->init_num = p - buf;
1046 /* buffer the message to handle re-xmits */
1047 dtls1_buffer_message(s, 0);
1050 /* SSL3_ST_SW_SRVR_HELLO_B */
1051 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1054 int dtls1_send_server_done(SSL *s)
1058 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1059 p = (unsigned char *)s->init_buf->data;
1062 p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0);
1064 s->state = SSL3_ST_SW_SRVR_DONE_B;
1065 /* number of bytes to write */
1066 s->init_num = DTLS1_HM_HEADER_LENGTH;
1069 /* buffer the message to handle re-xmits */
1070 dtls1_buffer_message(s, 0);
1073 /* SSL3_ST_SW_SRVR_DONE_B */
1074 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1077 int dtls1_send_server_key_exchange(SSL *s)
1079 #ifndef OPENSSL_NO_RSA
1083 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1086 #ifndef OPENSSL_NO_DH
1087 DH *dh = NULL, *dhp;
1089 #ifndef OPENSSL_NO_ECDH
1090 EC_KEY *ecdh = NULL, *ecdhp;
1091 unsigned char *encodedPoint = NULL;
1094 BN_CTX *bn_ctx = NULL;
1097 unsigned char *p, *d;
1107 EVP_MD_CTX_init(&md_ctx);
1108 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1109 type = s->s3->tmp.new_cipher->algorithm_mkey;
1114 r[0] = r[1] = r[2] = r[3] = NULL;
1116 #ifndef OPENSSL_NO_RSA
1117 if (type & SSL_kRSA) {
1118 rsa = cert->rsa_tmp;
1119 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1120 rsa = s->cert->rsa_tmp_cb(s,
1121 SSL_C_IS_EXPORT(s->s3->
1123 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1126 al = SSL_AD_HANDSHAKE_FAILURE;
1127 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1128 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1132 cert->rsa_tmp = rsa;
1135 al = SSL_AD_HANDSHAKE_FAILURE;
1136 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1137 SSL_R_MISSING_TMP_RSA_KEY);
1142 s->s3->tmp.use_rsa_tmp = 1;
1145 #ifndef OPENSSL_NO_DH
1146 if (type & SSL_kEDH) {
1148 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1149 dhp = s->cert->dh_tmp_cb(s,
1150 SSL_C_IS_EXPORT(s->s3->
1152 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1155 al = SSL_AD_HANDSHAKE_FAILURE;
1156 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1157 SSL_R_MISSING_TMP_DH_KEY);
1161 if (s->s3->tmp.dh != NULL) {
1163 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1164 ERR_R_INTERNAL_ERROR);
1168 if ((dh = DHparams_dup(dhp)) == NULL) {
1169 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1174 if ((dhp->pub_key == NULL ||
1175 dhp->priv_key == NULL ||
1176 (s->options & SSL_OP_SINGLE_DH_USE))) {
1177 if (!DH_generate_key(dh)) {
1178 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1183 dh->pub_key = BN_dup(dhp->pub_key);
1184 dh->priv_key = BN_dup(dhp->priv_key);
1185 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1186 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1196 #ifndef OPENSSL_NO_ECDH
1197 if (type & SSL_kEECDH) {
1198 const EC_GROUP *group;
1200 ecdhp = cert->ecdh_tmp;
1201 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) {
1202 ecdhp = s->cert->ecdh_tmp_cb(s,
1203 SSL_C_IS_EXPORT(s->s3->
1205 SSL_C_EXPORT_PKEYLENGTH(s->
1206 s3->tmp.new_cipher));
1208 if (ecdhp == NULL) {
1209 al = SSL_AD_HANDSHAKE_FAILURE;
1210 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1211 SSL_R_MISSING_TMP_ECDH_KEY);
1215 if (s->s3->tmp.ecdh != NULL) {
1216 EC_KEY_free(s->s3->tmp.ecdh);
1217 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1218 ERR_R_INTERNAL_ERROR);
1222 /* Duplicate the ECDH structure. */
1223 if (ecdhp == NULL) {
1224 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1227 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1228 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1232 s->s3->tmp.ecdh = ecdh;
1233 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1234 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1235 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1236 if (!EC_KEY_generate_key(ecdh)) {
1237 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1243 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1244 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1245 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1246 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1250 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1251 (EC_GROUP_get_degree(group) > 163)) {
1252 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1253 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1258 * XXX: For now, we only support ephemeral ECDH keys over named
1259 * (not generic) curves. For supported named curves, curve_id is
1263 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1265 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1266 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1271 * Encode the public key. First check the size of encoding and
1272 * allocate memory accordingly.
1274 encodedlen = EC_POINT_point2oct(group,
1275 EC_KEY_get0_public_key(ecdh),
1276 POINT_CONVERSION_UNCOMPRESSED,
1279 encodedPoint = (unsigned char *)
1280 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1281 bn_ctx = BN_CTX_new();
1282 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1283 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1284 ERR_R_MALLOC_FAILURE);
1288 encodedlen = EC_POINT_point2oct(group,
1289 EC_KEY_get0_public_key(ecdh),
1290 POINT_CONVERSION_UNCOMPRESSED,
1291 encodedPoint, encodedlen, bn_ctx);
1293 if (encodedlen == 0) {
1294 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1298 BN_CTX_free(bn_ctx);
1302 * XXX: For now, we only support named (not generic) curves in
1303 * ECDH ephemeral key exchanges. In this situation, we need four
1304 * additional bytes to encode the entire ServerECDHParams
1310 * We'll generate the serverKeyExchange message explicitly so we
1311 * can set these to NULLs
1318 #endif /* !OPENSSL_NO_ECDH */
1319 #ifndef OPENSSL_NO_PSK
1320 if (type & SSL_kPSK) {
1322 * reserve size for record length and PSK identity hint
1324 n += 2 + strlen(s->ctx->psk_identity_hint);
1326 #endif /* !OPENSSL_NO_PSK */
1328 al = SSL_AD_HANDSHAKE_FAILURE;
1329 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1330 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1333 for (i = 0; r[i] != NULL; i++) {
1334 nr[i] = BN_num_bytes(r[i]);
1338 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1339 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
1340 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, NULL))
1342 al = SSL_AD_DECODE_ERROR;
1345 kn = EVP_PKEY_size(pkey);
1351 if (!BUF_MEM_grow_clean(buf, n + DTLS1_HM_HEADER_LENGTH + kn)) {
1352 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1355 d = (unsigned char *)s->init_buf->data;
1356 p = &(d[DTLS1_HM_HEADER_LENGTH]);
1358 for (i = 0; r[i] != NULL; i++) {
1364 #ifndef OPENSSL_NO_ECDH
1365 if (type & SSL_kEECDH) {
1367 * XXX: For now, we only support named (not generic) curves. In
1368 * this situation, the serverKeyExchange message has: [1 byte
1369 * CurveType], [2 byte CurveName] [1 byte length of encoded
1370 * point], followed by the actual encoded point itself
1372 *p = NAMED_CURVE_TYPE;
1380 memcpy((unsigned char *)p,
1381 (unsigned char *)encodedPoint, encodedlen);
1382 OPENSSL_free(encodedPoint);
1383 encodedPoint = NULL;
1388 #ifndef OPENSSL_NO_PSK
1389 if (type & SSL_kPSK) {
1390 /* copy PSK identity hint */
1391 s2n(strlen(s->ctx->psk_identity_hint), p);
1392 strncpy((char *)p, s->ctx->psk_identity_hint,
1393 strlen(s->ctx->psk_identity_hint));
1394 p += strlen(s->ctx->psk_identity_hint);
1401 * n is the length of the params, they start at
1402 * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space at the
1405 #ifndef OPENSSL_NO_RSA
1406 if (pkey->type == EVP_PKEY_RSA) {
1409 for (num = 2; num > 0; num--) {
1410 EVP_DigestInit_ex(&md_ctx, (num == 2)
1411 ? s->ctx->md5 : s->ctx->sha1, NULL);
1412 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1414 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1416 EVP_DigestUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]),
1418 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1422 if (RSA_sign(NID_md5_sha1, md_buf, j,
1423 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1424 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
1431 #if !defined(OPENSSL_NO_DSA)
1432 if (pkey->type == EVP_PKEY_DSA) {
1434 EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL);
1435 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1437 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1439 EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n);
1440 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1441 (unsigned int *)&i, pkey)) {
1442 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA);
1449 #if !defined(OPENSSL_NO_ECDSA)
1450 if (pkey->type == EVP_PKEY_EC) {
1451 /* let's do ECDSA */
1452 EVP_SignInit_ex(&md_ctx, EVP_ecdsa(), NULL);
1453 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1455 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1457 EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n);
1458 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1459 (unsigned int *)&i, pkey)) {
1460 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1469 /* Is this error check actually needed? */
1470 al = SSL_AD_HANDSHAKE_FAILURE;
1471 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1472 SSL_R_UNKNOWN_PKEY_TYPE);
1477 d = dtls1_set_message_header(s, d,
1478 SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);
1481 * we should now have things packed up, so lets send it off
1483 s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1486 /* buffer the message to handle re-xmits */
1487 dtls1_buffer_message(s, 0);
1490 s->state = SSL3_ST_SW_KEY_EXCH_B;
1491 EVP_MD_CTX_cleanup(&md_ctx);
1492 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1494 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1496 #ifndef OPENSSL_NO_ECDH
1497 if (encodedPoint != NULL)
1498 OPENSSL_free(encodedPoint);
1499 BN_CTX_free(bn_ctx);
1501 EVP_MD_CTX_cleanup(&md_ctx);
1505 int dtls1_send_certificate_request(SSL *s)
1507 unsigned char *p, *d;
1508 int i, j, nl, off, n;
1509 STACK_OF(X509_NAME) *sk = NULL;
1512 unsigned int msg_len;
1514 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1517 d = p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
1519 /* get the list of acceptable cert types */
1521 n = ssl3_get_req_cert_type(s, p);
1530 sk = SSL_get_client_CA_list(s);
1533 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1534 name = sk_X509_NAME_value(sk, i);
1535 j = i2d_X509_NAME(name, NULL);
1536 if (!BUF_MEM_grow_clean
1537 (buf, DTLS1_HM_HEADER_LENGTH + n + j + 2)) {
1538 SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,
1542 p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + n]);
1543 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
1545 i2d_X509_NAME(name, &p);
1550 i2d_X509_NAME(name, &p);
1559 /* else no CA names */
1560 p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + off]);
1563 d = (unsigned char *)buf->data;
1564 *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
1566 s2n(s->d1->handshake_write_seq, d);
1567 s->d1->handshake_write_seq++;
1570 * we should now have things packed up, so lets send it off
1573 s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1575 #ifdef NETSCAPE_HANG_BUG
1576 /* XXX: what to do about this? */
1577 p = (unsigned char *)s->init_buf->data + s->init_num;
1580 *(p++) = SSL3_MT_SERVER_DONE;
1587 /* XDTLS: set message header ? */
1588 msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
1589 dtls1_set_message_header(s, (void *)s->init_buf->data,
1590 SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0,
1593 /* buffer the message to handle re-xmits */
1594 dtls1_buffer_message(s, 0);
1596 s->state = SSL3_ST_SW_CERT_REQ_B;
1599 /* SSL3_ST_SW_CERT_REQ_B */
1600 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1605 int dtls1_send_server_certificate(SSL *s)
1610 if (s->state == SSL3_ST_SW_CERT_A) {
1611 x = ssl_get_server_send_cert(s);
1613 /* VRS: allow null cert if auth == KRB5 */
1614 if ((s->s3->tmp.new_cipher->algorithm_mkey != SSL_kKRB5) ||
1615 (s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5)) {
1616 SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,
1617 ERR_R_INTERNAL_ERROR);
1622 l = dtls1_output_cert_chain(s, x);
1624 SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1627 s->state = SSL3_ST_SW_CERT_B;
1628 s->init_num = (int)l;
1631 /* buffer the message to handle re-xmits */
1632 dtls1_buffer_message(s, 0);
1635 /* SSL3_ST_SW_CERT_B */
1636 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1639 #ifndef OPENSSL_NO_TLSEXT
1640 int dtls1_send_newsession_ticket(SSL *s)
1642 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
1643 unsigned char *p, *senc, *macstart;
1645 unsigned int hlen, msg_len;
1648 SSL_CTX *tctx = s->initial_ctx;
1649 unsigned char iv[EVP_MAX_IV_LENGTH];
1650 unsigned char key_name[16];
1652 /* get session encoding length */
1653 slen = i2d_SSL_SESSION(s->session, NULL);
1655 * Some length values are 16 bits, so forget it if session is too
1661 * Grow buffer if need be: the length calculation is as follows 12
1662 * (DTLS handshake message header) + 4 (ticket lifetime hint) + 2
1663 * (ticket length) + 16 (key name) + max_iv_len (iv length) +
1664 * session_length + max_enc_block_size (max encrypted session length)
1665 * + max_md_size (HMAC).
1667 if (!BUF_MEM_grow(s->init_buf,
1668 DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH +
1669 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
1671 senc = OPENSSL_malloc(slen);
1675 i2d_SSL_SESSION(s->session, &p);
1677 p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]);
1678 EVP_CIPHER_CTX_init(&ctx);
1679 HMAC_CTX_init(&hctx);
1681 * Initialize HMAC and cipher contexts. If callback present it does
1682 * all the work otherwise use generated values from parent ctx.
1684 if (tctx->tlsext_ticket_key_cb) {
1685 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
1691 RAND_pseudo_bytes(iv, 16);
1692 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
1693 tctx->tlsext_tick_aes_key, iv);
1694 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
1695 tlsext_tick_md(), NULL);
1696 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
1698 l2n(s->session->tlsext_tick_lifetime_hint, p);
1699 /* Skip ticket length for now */
1701 /* Output key name */
1703 memcpy(p, key_name, 16);
1706 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
1707 p += EVP_CIPHER_CTX_iv_length(&ctx);
1708 /* Encrypt session data */
1709 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
1711 EVP_EncryptFinal(&ctx, p, &len);
1713 EVP_CIPHER_CTX_cleanup(&ctx);
1715 HMAC_Update(&hctx, macstart, p - macstart);
1716 HMAC_Final(&hctx, p, &hlen);
1717 HMAC_CTX_cleanup(&hctx);
1720 /* Now write out lengths: p points to end of data written */
1722 len = p - (unsigned char *)(s->init_buf->data);
1724 p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4;
1725 s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p);
1727 /* number of bytes to write */
1729 s->state = SSL3_ST_SW_SESSION_TICKET_B;
1733 /* XDTLS: set message header ? */
1734 msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
1735 dtls1_set_message_header(s, (void *)s->init_buf->data,
1736 SSL3_MT_NEWSESSION_TICKET, msg_len, 0,
1739 /* buffer the message to handle re-xmits */
1740 dtls1_buffer_message(s, 0);
1743 /* SSL3_ST_SW_SESSION_TICKET_B */
1744 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));