etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / openssl / dist / ssl / d1_srvr.c
blob41c7dc519f1b76a185674d3fe094f84d7cbd3e0e
1 /* ssl/d1_srvr.c */
2 /*
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5 */
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
11 * are met:
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
19 * distribution.
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
36 * acknowledgment:
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
82 * are met:
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
108 * SUCH DAMAGE.
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.]
116 #include <stdio.h>
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>
127 #endif
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());
136 else
137 return (NULL);
140 IMPLEMENT_dtls1_meth_func(DTLSv1_server_method,
141 dtls1_accept,
142 ssl_undefined_function, dtls1_get_server_method)
144 int dtls1_accept(SSL *s)
146 BUF_MEM *buf;
147 unsigned long Time = (unsigned long)time(NULL);
148 void (*cb) (const SSL *ssl, int type, int val) = NULL;
149 unsigned long alg_k;
150 int ret = -1;
151 int new_state, state, skip = 0;
152 int listen;
153 #ifndef OPENSSL_NO_SCTP
154 unsigned char sctpauthkey[64];
155 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
156 #endif
158 RAND_add(&Time, sizeof(Time), 0);
159 ERR_clear_error();
160 clear_sys_error();
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 */
170 s->in_handshake++;
171 if (!SSL_in_init(s) || SSL_in_before(s))
172 SSL_clear(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);
182 #endif
184 if (s->cert == NULL) {
185 SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
186 return (-1);
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
192 * handshakes anyway.
194 if (s->tlsext_hb_pending) {
195 dtls1_stop_timer(s);
196 s->tlsext_hb_pending = 0;
197 s->tlsext_hb_seq++;
199 #endif
201 for (;;) {
202 state = s->state;
204 switch (s->state) {
205 case SSL_ST_RENEGOTIATE:
206 s->renegotiate = 1;
207 /* s->state=SSL_ST_ACCEPT; */
209 case SSL_ST_BEFORE:
210 case SSL_ST_ACCEPT:
211 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
212 case SSL_ST_OK | SSL_ST_ACCEPT:
214 s->server = 1;
215 if (cb != NULL)
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);
220 return -1;
222 s->type = SSL_ST_ACCEPT;
224 if (s->init_buf == NULL) {
225 if ((buf = BUF_MEM_new()) == NULL) {
226 ret = -1;
227 s->state = SSL_ST_ERR;
228 goto end;
230 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
231 BUF_MEM_free(buf);
232 ret = -1;
233 s->state = SSL_ST_ERR;
234 goto end;
236 s->init_buf = buf;
239 if (!ssl3_setup_buffers(s)) {
240 ret = -1;
241 s->state = SSL_ST_ERR;
242 goto end;
245 s->init_num = 0;
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
256 * SCTP :-)
258 #ifndef OPENSSL_NO_SCTP
259 if (!BIO_dgram_is_sctp(SSL_get_wbio(s)))
260 #endif
261 if (!ssl_init_wbio_buffer(s, 1)) {
262 ret = -1;
263 s->state = SSL_ST_ERR;
264 goto end;
267 ssl3_init_finished_mac(s);
268 s->state = SSL3_ST_SR_CLNT_HELLO_A;
269 s->ctx->stats.sess_accept++;
270 } else {
272 * s->state == SSL_ST_RENEGOTIATE, we will just send a
273 * HelloRequest
275 s->ctx->stats.sess_accept_renegotiate++;
276 s->state = SSL3_ST_SW_HELLO_REQ_A;
279 break;
281 case SSL3_ST_SW_HELLO_REQ_A:
282 case SSL3_ST_SW_HELLO_REQ_B:
284 s->shutdown = 0;
285 dtls1_clear_record_buffer(s);
286 dtls1_start_timer(s);
287 ret = dtls1_send_hello_request(s);
288 if (ret <= 0)
289 goto end;
290 s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A;
291 s->state = SSL3_ST_SW_FLUSH;
292 s->init_num = 0;
294 ssl3_init_finished_mac(s);
295 break;
297 case SSL3_ST_SW_HELLO_REQ_C:
298 s->state = SSL_ST_OK;
299 break;
301 case SSL3_ST_SR_CLNT_HELLO_A:
302 case SSL3_ST_SR_CLNT_HELLO_B:
303 case SSL3_ST_SR_CLNT_HELLO_C:
305 s->shutdown = 0;
306 ret = ssl3_get_client_hello(s);
307 if (ret <= 0)
308 goto end;
309 dtls1_stop_timer(s);
311 if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
312 s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
313 else
314 s->state = SSL3_ST_SW_SRVR_HELLO_A;
316 s->init_num = 0;
319 * Reflect ClientHello sequence to remain stateless while
320 * listening
322 if (listen) {
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) {
329 ret = 2;
330 s->d1->listen = 0;
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;
337 goto end;
340 break;
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);
346 if (ret <= 0)
347 goto end;
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);
354 break;
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));
364 ret = -1;
365 goto end;
368 s->state = SSL3_ST_SR_FINISHED_A;
369 break;
371 case DTLS1_SCTP_ST_SW_WRITE_SOCK:
372 ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
373 if (ret < 0)
374 goto end;
376 if (ret == 0) {
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));
382 ret = -1;
383 goto end;
387 s->state = s->d1->next_state;
388 break;
389 #endif
391 case SSL3_ST_SW_SRVR_HELLO_A:
392 case SSL3_ST_SW_SRVR_HELLO_B:
393 s->renegotiate = 2;
394 dtls1_start_timer(s);
395 ret = dtls1_send_server_hello(s);
396 if (ret <= 0)
397 goto end;
399 if (s->hit) {
400 #ifndef OPENSSL_NO_SCTP
402 * Add new shared key for SCTP-Auth, will be ignored if no
403 * SCTP used.
405 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
406 DTLS1_SCTP_AUTH_LABEL);
408 SSL_export_keying_material(s, sctpauthkey,
409 sizeof(sctpauthkey), labelbuffer,
410 sizeof(labelbuffer), NULL, 0, 0);
412 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
413 sizeof(sctpauthkey), sctpauthkey);
414 #endif
415 #ifndef OPENSSL_NO_TLSEXT
416 if (s->tlsext_ticket_expected)
417 s->state = SSL3_ST_SW_SESSION_TICKET_A;
418 else
419 s->state = SSL3_ST_SW_CHANGE_A;
420 #else
421 s->state = SSL3_ST_SW_CHANGE_A;
422 #endif
423 } else
424 s->state = SSL3_ST_SW_CERT_A;
425 s->init_num = 0;
426 break;
428 case SSL3_ST_SW_CERT_A:
429 case SSL3_ST_SW_CERT_B:
430 /* Check if it is anon DH or normal PSK */
431 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
432 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
433 dtls1_start_timer(s);
434 ret = dtls1_send_server_certificate(s);
435 if (ret <= 0)
436 goto end;
437 #ifndef OPENSSL_NO_TLSEXT
438 if (s->tlsext_status_expected)
439 s->state = SSL3_ST_SW_CERT_STATUS_A;
440 else
441 s->state = SSL3_ST_SW_KEY_EXCH_A;
442 } else {
443 skip = 1;
444 s->state = SSL3_ST_SW_KEY_EXCH_A;
446 #else
447 } else
448 skip = 1;
450 s->state = SSL3_ST_SW_KEY_EXCH_A;
451 #endif
452 s->init_num = 0;
453 break;
455 case SSL3_ST_SW_KEY_EXCH_A:
456 case SSL3_ST_SW_KEY_EXCH_B:
457 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
460 * clear this, it may get reset by
461 * send_server_key_exchange
463 s->s3->tmp.use_rsa_tmp = 0;
466 * only send if a DH key exchange or RSA but we have a sign only
467 * certificate
469 if (0
471 * PSK: send ServerKeyExchange if PSK identity hint if
472 * provided
474 #ifndef OPENSSL_NO_PSK
475 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
476 #endif
477 || (alg_k & SSL_kEDH)
478 || (alg_k & SSL_kEECDH)
479 || ((alg_k & SSL_kRSA)
480 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
481 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
482 && EVP_PKEY_size(s->cert->pkeys
483 [SSL_PKEY_RSA_ENC].privatekey) *
484 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
489 dtls1_start_timer(s);
490 ret = dtls1_send_server_key_exchange(s);
491 if (ret <= 0)
492 goto end;
493 } else
494 skip = 1;
496 s->state = SSL3_ST_SW_CERT_REQ_A;
497 s->init_num = 0;
498 break;
500 case SSL3_ST_SW_CERT_REQ_A:
501 case SSL3_ST_SW_CERT_REQ_B:
502 if ( /* don't request cert unless asked for it: */
503 !(s->verify_mode & SSL_VERIFY_PEER) ||
505 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
506 * during re-negotiation:
508 ((s->session->peer != NULL) &&
509 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
511 * never request cert in anonymous ciphersuites (see
512 * section "Certificate request" in SSL 3 drafts and in
513 * RFC 2246):
515 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
517 * ... except when the application insists on
518 * verification (against the specs, but s3_clnt.c accepts
519 * this for SSL 3)
521 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
523 * never request cert in Kerberos ciphersuites
525 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
527 * With normal PSK Certificates and Certificate Requests
528 * are omitted
530 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
531 /* no cert request */
532 skip = 1;
533 s->s3->tmp.cert_request = 0;
534 s->state = SSL3_ST_SW_SRVR_DONE_A;
535 #ifndef OPENSSL_NO_SCTP
536 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
537 s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
538 s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
540 #endif
541 } else {
542 s->s3->tmp.cert_request = 1;
543 dtls1_start_timer(s);
544 ret = dtls1_send_certificate_request(s);
545 if (ret <= 0)
546 goto end;
547 #ifndef NETSCAPE_HANG_BUG
548 s->state = SSL3_ST_SW_SRVR_DONE_A;
549 # ifndef OPENSSL_NO_SCTP
550 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
551 s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
552 s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
554 # endif
555 #else
556 s->state = SSL3_ST_SW_FLUSH;
557 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
558 # ifndef OPENSSL_NO_SCTP
559 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
560 s->d1->next_state = s->s3->tmp.next_state;
561 s->s3->tmp.next_state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
563 # endif
564 #endif
565 s->init_num = 0;
567 break;
569 case SSL3_ST_SW_SRVR_DONE_A:
570 case SSL3_ST_SW_SRVR_DONE_B:
571 dtls1_start_timer(s);
572 ret = dtls1_send_server_done(s);
573 if (ret <= 0)
574 goto end;
575 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
576 s->state = SSL3_ST_SW_FLUSH;
577 s->init_num = 0;
578 break;
580 case SSL3_ST_SW_FLUSH:
581 s->rwstate = SSL_WRITING;
582 if (BIO_flush(s->wbio) <= 0) {
584 * If the write error was fatal, stop trying
586 if (!BIO_should_retry(s->wbio)) {
587 s->rwstate = SSL_NOTHING;
588 s->state = s->s3->tmp.next_state;
591 ret = -1;
592 goto end;
594 s->rwstate = SSL_NOTHING;
595 s->state = s->s3->tmp.next_state;
596 break;
598 case SSL3_ST_SR_CERT_A:
599 case SSL3_ST_SR_CERT_B:
600 /* Check for second client hello (MS SGC) */
601 ret = ssl3_check_client_hello(s);
602 if (ret <= 0)
603 goto end;
604 if (ret == 2) {
605 dtls1_stop_timer(s);
606 s->state = SSL3_ST_SR_CLNT_HELLO_C;
607 } else {
608 if (s->s3->tmp.cert_request) {
609 ret = ssl3_get_client_certificate(s);
610 if (ret <= 0)
611 goto end;
613 s->init_num = 0;
614 s->state = SSL3_ST_SR_KEY_EXCH_A;
616 break;
618 case SSL3_ST_SR_KEY_EXCH_A:
619 case SSL3_ST_SR_KEY_EXCH_B:
620 ret = ssl3_get_client_key_exchange(s);
621 if (ret <= 0)
622 goto end;
623 #ifndef OPENSSL_NO_SCTP
625 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
626 * used.
628 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
629 DTLS1_SCTP_AUTH_LABEL);
631 SSL_export_keying_material(s, sctpauthkey,
632 sizeof(sctpauthkey), labelbuffer,
633 sizeof(labelbuffer), NULL, 0, 0);
635 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
636 sizeof(sctpauthkey), sctpauthkey);
637 #endif
639 s->state = SSL3_ST_SR_CERT_VRFY_A;
640 s->init_num = 0;
642 if (ret == 2) {
644 * For the ECDH ciphersuites when the client sends its ECDH
645 * pub key in a certificate, the CertificateVerify message is
646 * not sent.
648 s->state = SSL3_ST_SR_FINISHED_A;
649 s->init_num = 0;
650 } else {
651 s->state = SSL3_ST_SR_CERT_VRFY_A;
652 s->init_num = 0;
655 * We need to get hashes here so if there is a client cert,
656 * it can be verified
658 s->method->ssl3_enc->cert_verify_mac(s,
659 NID_md5,
660 &(s->s3->
661 tmp.cert_verify_md
662 [0]));
663 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1,
664 &(s->s3->
665 tmp.cert_verify_md
666 [MD5_DIGEST_LENGTH]));
668 break;
670 case SSL3_ST_SR_CERT_VRFY_A:
671 case SSL3_ST_SR_CERT_VRFY_B:
672 ret = ssl3_get_cert_verify(s);
673 if (ret <= 0)
674 goto end;
675 #ifndef OPENSSL_NO_SCTP
676 if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
677 state == SSL_ST_RENEGOTIATE)
678 s->state = DTLS1_SCTP_ST_SR_READ_SOCK;
679 else
680 #endif
681 s->state = SSL3_ST_SR_FINISHED_A;
682 s->init_num = 0;
683 break;
685 case SSL3_ST_SR_FINISHED_A:
686 case SSL3_ST_SR_FINISHED_B:
688 * Enable CCS. Receiving a CCS clears the flag, so make
689 * sure not to re-enable it to ban duplicates. This *should* be the
690 * first time we have received one - but we check anyway to be
691 * cautious.
692 * s->s3->change_cipher_spec is set when a CCS is
693 * processed in d1_pkt.c, and remains set until
694 * the client's Finished message is read.
696 if (!s->s3->change_cipher_spec)
697 s->d1->change_cipher_spec_ok = 1;
698 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
699 SSL3_ST_SR_FINISHED_B);
700 if (ret <= 0)
701 goto end;
702 dtls1_stop_timer(s);
703 if (s->hit)
704 s->state = SSL_ST_OK;
705 #ifndef OPENSSL_NO_TLSEXT
706 else if (s->tlsext_ticket_expected)
707 s->state = SSL3_ST_SW_SESSION_TICKET_A;
708 #endif
709 else
710 s->state = SSL3_ST_SW_CHANGE_A;
711 s->init_num = 0;
712 break;
714 #ifndef OPENSSL_NO_TLSEXT
715 case SSL3_ST_SW_SESSION_TICKET_A:
716 case SSL3_ST_SW_SESSION_TICKET_B:
717 ret = dtls1_send_newsession_ticket(s);
718 if (ret <= 0)
719 goto end;
720 s->state = SSL3_ST_SW_CHANGE_A;
721 s->init_num = 0;
722 break;
724 case SSL3_ST_SW_CERT_STATUS_A:
725 case SSL3_ST_SW_CERT_STATUS_B:
726 ret = ssl3_send_cert_status(s);
727 if (ret <= 0)
728 goto end;
729 s->state = SSL3_ST_SW_KEY_EXCH_A;
730 s->init_num = 0;
731 break;
733 #endif
735 case SSL3_ST_SW_CHANGE_A:
736 case SSL3_ST_SW_CHANGE_B:
738 s->session->cipher = s->s3->tmp.new_cipher;
739 if (!s->method->ssl3_enc->setup_key_block(s)) {
740 ret = -1;
741 s->state = SSL_ST_ERR;
742 goto end;
745 ret = dtls1_send_change_cipher_spec(s,
746 SSL3_ST_SW_CHANGE_A,
747 SSL3_ST_SW_CHANGE_B);
749 if (ret <= 0)
750 goto end;
752 #ifndef OPENSSL_NO_SCTP
753 if (!s->hit) {
755 * Change to new shared key of SCTP-Auth, will be ignored if
756 * no SCTP used.
758 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
759 0, NULL);
761 #endif
763 s->state = SSL3_ST_SW_FINISHED_A;
764 s->init_num = 0;
766 if (!s->method->ssl3_enc->change_cipher_state(s,
767 SSL3_CHANGE_CIPHER_SERVER_WRITE))
769 ret = -1;
770 s->state = SSL_ST_ERR;
771 goto end;
774 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
775 break;
777 case SSL3_ST_SW_FINISHED_A:
778 case SSL3_ST_SW_FINISHED_B:
779 ret = dtls1_send_finished(s,
780 SSL3_ST_SW_FINISHED_A,
781 SSL3_ST_SW_FINISHED_B,
782 s->method->
783 ssl3_enc->server_finished_label,
784 s->method->
785 ssl3_enc->server_finished_label_len);
786 if (ret <= 0)
787 goto end;
788 s->state = SSL3_ST_SW_FLUSH;
789 if (s->hit) {
790 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
792 #ifndef OPENSSL_NO_SCTP
794 * Change to new shared key of SCTP-Auth, will be ignored if
795 * no SCTP used.
797 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
798 0, NULL);
799 #endif
800 } else {
801 s->s3->tmp.next_state = SSL_ST_OK;
802 #ifndef OPENSSL_NO_SCTP
803 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
804 s->d1->next_state = s->s3->tmp.next_state;
805 s->s3->tmp.next_state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
807 #endif
809 s->init_num = 0;
810 break;
812 case SSL_ST_OK:
813 /* clean a few things up */
814 ssl3_cleanup_key_block(s);
816 #if 0
817 BUF_MEM_free(s->init_buf);
818 s->init_buf = NULL;
819 #endif
821 /* remove buffering on output */
822 ssl_free_wbio_buffer(s);
824 s->init_num = 0;
826 if (s->renegotiate == 2) { /* skipped if we just sent a
827 * HelloRequest */
828 s->renegotiate = 0;
829 s->new_session = 0;
831 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
833 s->ctx->stats.sess_accept_good++;
834 /* s->server=1; */
835 s->handshake_func = dtls1_accept;
837 if (cb != NULL)
838 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
841 ret = 1;
843 /* done handshaking, next message is client hello */
844 s->d1->handshake_read_seq = 0;
845 /* next message is server hello */
846 s->d1->handshake_write_seq = 0;
847 s->d1->next_handshake_write_seq = 0;
848 goto end;
849 /* break; */
851 case SSL_ST_ERR:
852 default:
853 SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_UNKNOWN_STATE);
854 ret = -1;
855 goto end;
856 /* break; */
859 if (!s->s3->tmp.reuse_message && !skip) {
860 if (s->debug) {
861 if ((ret = BIO_flush(s->wbio)) <= 0)
862 goto end;
865 if ((cb != NULL) && (s->state != state)) {
866 new_state = s->state;
867 s->state = state;
868 cb(s, SSL_CB_ACCEPT_LOOP, 1);
869 s->state = new_state;
872 skip = 0;
874 end:
875 /* BIO_flush(s->wbio); */
877 s->in_handshake--;
878 #ifndef OPENSSL_NO_SCTP
880 * Notify SCTP BIO socket to leave handshake mode and prevent stream
881 * identifier other than 0. Will be ignored if no SCTP is used.
883 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
884 s->in_handshake, NULL);
885 #endif
887 if (cb != NULL)
888 cb(s, SSL_CB_ACCEPT_EXIT, ret);
889 return (ret);
892 int dtls1_send_hello_request(SSL *s)
894 unsigned char *p;
896 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
897 p = (unsigned char *)s->init_buf->data;
898 p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0);
900 s->state = SSL3_ST_SW_HELLO_REQ_B;
901 /* number of bytes to write */
902 s->init_num = DTLS1_HM_HEADER_LENGTH;
903 s->init_off = 0;
906 * no need to buffer this message, since there are no retransmit
907 * requests for it
911 /* SSL3_ST_SW_HELLO_REQ_B */
912 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
915 int dtls1_send_hello_verify_request(SSL *s)
917 unsigned int msg_len;
918 unsigned char *msg, *buf, *p;
920 if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) {
921 buf = (unsigned char *)s->init_buf->data;
923 msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
924 *(p++) = s->version >> 8;
925 *(p++) = s->version & 0xFF;
927 if (s->ctx->app_gen_cookie_cb == NULL ||
928 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
929 &(s->d1->cookie_len)) == 0) {
930 SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,
931 ERR_R_INTERNAL_ERROR);
932 s->state = SSL_ST_ERR;
933 return 0;
936 *(p++) = (unsigned char)s->d1->cookie_len;
937 memcpy(p, s->d1->cookie, s->d1->cookie_len);
938 p += s->d1->cookie_len;
939 msg_len = p - msg;
941 dtls1_set_message_header(s, buf,
942 DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0,
943 msg_len);
945 s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
946 /* number of bytes to write */
947 s->init_num = p - buf;
948 s->init_off = 0;
951 /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
952 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
955 int dtls1_send_server_hello(SSL *s)
957 unsigned char *buf;
958 unsigned char *p, *d;
959 int i;
960 unsigned int sl;
961 unsigned long l;
963 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
964 buf = (unsigned char *)s->init_buf->data;
965 p = s->s3->server_random;
966 ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE);
967 /* Do the message type and length last */
968 d = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
970 *(p++) = s->version >> 8;
971 *(p++) = s->version & 0xff;
973 /* Random stuff */
974 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
975 p += SSL3_RANDOM_SIZE;
978 * now in theory we have 3 options to sending back the session id.
979 * If it is a re-use, we send back the old session-id, if it is a new
980 * session, we send back the new session-id or we send back a 0
981 * length session-id if we want it to be single use. Currently I will
982 * not implement the '0' length session-id 12-Jan-98 - I'll now
983 * support the '0' length stuff.
985 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
986 s->session->session_id_length = 0;
988 sl = s->session->session_id_length;
989 if (sl > sizeof s->session->session_id) {
990 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
991 return -1;
993 *(p++) = sl;
994 memcpy(p, s->session->session_id, sl);
995 p += sl;
997 /* put the cipher */
998 if (s->s3->tmp.new_cipher == NULL)
999 return -1;
1000 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1001 p += i;
1003 /* put the compression method */
1004 #ifdef OPENSSL_NO_COMP
1005 *(p++) = 0;
1006 #else
1007 if (s->s3->tmp.new_compression == NULL)
1008 *(p++) = 0;
1009 else
1010 *(p++) = s->s3->tmp.new_compression->id;
1011 #endif
1013 #ifndef OPENSSL_NO_TLSEXT
1014 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1015 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1016 return -1;
1018 if ((p =
1019 ssl_add_serverhello_tlsext(s, p,
1020 buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
1021 NULL) {
1022 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1023 return -1;
1025 #endif
1027 /* do the header */
1028 l = (p - d);
1029 d = buf;
1031 d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l);
1033 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1034 /* number of bytes to write */
1035 s->init_num = p - buf;
1036 s->init_off = 0;
1038 /* buffer the message to handle re-xmits */
1039 dtls1_buffer_message(s, 0);
1042 /* SSL3_ST_SW_SRVR_HELLO_B */
1043 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1046 int dtls1_send_server_done(SSL *s)
1048 unsigned char *p;
1050 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1051 p = (unsigned char *)s->init_buf->data;
1053 /* do the header */
1054 p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0);
1056 s->state = SSL3_ST_SW_SRVR_DONE_B;
1057 /* number of bytes to write */
1058 s->init_num = DTLS1_HM_HEADER_LENGTH;
1059 s->init_off = 0;
1061 /* buffer the message to handle re-xmits */
1062 dtls1_buffer_message(s, 0);
1065 /* SSL3_ST_SW_SRVR_DONE_B */
1066 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1069 int dtls1_send_server_key_exchange(SSL *s)
1071 #ifndef OPENSSL_NO_RSA
1072 unsigned char *q;
1073 int j, num;
1074 RSA *rsa;
1075 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1076 unsigned int u;
1077 #endif
1078 #ifndef OPENSSL_NO_DH
1079 DH *dh = NULL, *dhp;
1080 #endif
1081 #ifndef OPENSSL_NO_ECDH
1082 EC_KEY *ecdh = NULL, *ecdhp;
1083 unsigned char *encodedPoint = NULL;
1084 int encodedlen = 0;
1085 int curve_id = 0;
1086 BN_CTX *bn_ctx = NULL;
1087 #endif
1088 EVP_PKEY *pkey;
1089 unsigned char *p, *d;
1090 int al, i;
1091 unsigned long type;
1092 int n;
1093 CERT *cert;
1094 BIGNUM *r[4];
1095 int nr[4], kn;
1096 BUF_MEM *buf;
1097 EVP_MD_CTX md_ctx;
1099 EVP_MD_CTX_init(&md_ctx);
1100 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1101 type = s->s3->tmp.new_cipher->algorithm_mkey;
1102 cert = s->cert;
1104 buf = s->init_buf;
1106 r[0] = r[1] = r[2] = r[3] = NULL;
1107 n = 0;
1108 #ifndef OPENSSL_NO_RSA
1109 if (type & SSL_kRSA) {
1110 rsa = cert->rsa_tmp;
1111 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1112 rsa = s->cert->rsa_tmp_cb(s,
1113 SSL_C_IS_EXPORT(s->s3->
1114 tmp.new_cipher),
1115 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1116 tmp.new_cipher));
1117 if (rsa == NULL) {
1118 al = SSL_AD_HANDSHAKE_FAILURE;
1119 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1120 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1121 goto f_err;
1123 RSA_up_ref(rsa);
1124 cert->rsa_tmp = rsa;
1126 if (rsa == NULL) {
1127 al = SSL_AD_HANDSHAKE_FAILURE;
1128 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1129 SSL_R_MISSING_TMP_RSA_KEY);
1130 goto f_err;
1132 r[0] = rsa->n;
1133 r[1] = rsa->e;
1134 s->s3->tmp.use_rsa_tmp = 1;
1135 } else
1136 #endif
1137 #ifndef OPENSSL_NO_DH
1138 if (type & SSL_kEDH) {
1139 dhp = cert->dh_tmp;
1140 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1141 dhp = s->cert->dh_tmp_cb(s,
1142 SSL_C_IS_EXPORT(s->s3->
1143 tmp.new_cipher),
1144 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1145 tmp.new_cipher));
1146 if (dhp == NULL) {
1147 al = SSL_AD_HANDSHAKE_FAILURE;
1148 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1149 SSL_R_MISSING_TMP_DH_KEY);
1150 goto f_err;
1153 if (s->s3->tmp.dh != NULL) {
1154 DH_free(dh);
1155 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1156 ERR_R_INTERNAL_ERROR);
1157 goto err;
1160 if ((dh = DHparams_dup(dhp)) == NULL) {
1161 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1162 goto err;
1165 s->s3->tmp.dh = dh;
1166 if ((dhp->pub_key == NULL ||
1167 dhp->priv_key == NULL ||
1168 (s->options & SSL_OP_SINGLE_DH_USE))) {
1169 if (!DH_generate_key(dh)) {
1170 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1171 ERR_R_DH_LIB);
1172 goto err;
1174 } else {
1175 dh->pub_key = BN_dup(dhp->pub_key);
1176 dh->priv_key = BN_dup(dhp->priv_key);
1177 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1178 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1179 ERR_R_DH_LIB);
1180 goto err;
1183 r[0] = dh->p;
1184 r[1] = dh->g;
1185 r[2] = dh->pub_key;
1186 } else
1187 #endif
1188 #ifndef OPENSSL_NO_ECDH
1189 if (type & SSL_kEECDH) {
1190 const EC_GROUP *group;
1192 ecdhp = cert->ecdh_tmp;
1193 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) {
1194 ecdhp = s->cert->ecdh_tmp_cb(s,
1195 SSL_C_IS_EXPORT(s->s3->
1196 tmp.new_cipher),
1197 SSL_C_EXPORT_PKEYLENGTH(s->
1198 s3->tmp.new_cipher));
1200 if (ecdhp == NULL) {
1201 al = SSL_AD_HANDSHAKE_FAILURE;
1202 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1203 SSL_R_MISSING_TMP_ECDH_KEY);
1204 goto f_err;
1207 if (s->s3->tmp.ecdh != NULL) {
1208 EC_KEY_free(s->s3->tmp.ecdh);
1209 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1210 ERR_R_INTERNAL_ERROR);
1211 goto err;
1214 /* Duplicate the ECDH structure. */
1215 if (ecdhp == NULL) {
1216 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1217 goto err;
1219 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1220 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1221 goto err;
1224 s->s3->tmp.ecdh = ecdh;
1225 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1226 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1227 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1228 if (!EC_KEY_generate_key(ecdh)) {
1229 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1230 ERR_R_ECDH_LIB);
1231 goto err;
1235 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1236 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1237 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1238 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1239 goto err;
1242 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1243 (EC_GROUP_get_degree(group) > 163)) {
1244 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1245 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1246 goto err;
1250 * XXX: For now, we only support ephemeral ECDH keys over named
1251 * (not generic) curves. For supported named curves, curve_id is
1252 * non-zero.
1254 if ((curve_id =
1255 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1256 == 0) {
1257 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1258 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1259 goto err;
1263 * Encode the public key. First check the size of encoding and
1264 * allocate memory accordingly.
1266 encodedlen = EC_POINT_point2oct(group,
1267 EC_KEY_get0_public_key(ecdh),
1268 POINT_CONVERSION_UNCOMPRESSED,
1269 NULL, 0, NULL);
1271 encodedPoint = (unsigned char *)
1272 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1273 bn_ctx = BN_CTX_new();
1274 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1275 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1276 ERR_R_MALLOC_FAILURE);
1277 goto err;
1280 encodedlen = EC_POINT_point2oct(group,
1281 EC_KEY_get0_public_key(ecdh),
1282 POINT_CONVERSION_UNCOMPRESSED,
1283 encodedPoint, encodedlen, bn_ctx);
1285 if (encodedlen == 0) {
1286 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1287 goto err;
1290 BN_CTX_free(bn_ctx);
1291 bn_ctx = NULL;
1294 * XXX: For now, we only support named (not generic) curves in
1295 * ECDH ephemeral key exchanges. In this situation, we need four
1296 * additional bytes to encode the entire ServerECDHParams
1297 * structure.
1299 n = 4 + encodedlen;
1302 * We'll generate the serverKeyExchange message explicitly so we
1303 * can set these to NULLs
1305 r[0] = NULL;
1306 r[1] = NULL;
1307 r[2] = NULL;
1308 r[3] = NULL;
1309 } else
1310 #endif /* !OPENSSL_NO_ECDH */
1311 #ifndef OPENSSL_NO_PSK
1312 if (type & SSL_kPSK) {
1314 * reserve size for record length and PSK identity hint
1316 n += 2 + strlen(s->ctx->psk_identity_hint);
1317 } else
1318 #endif /* !OPENSSL_NO_PSK */
1320 al = SSL_AD_HANDSHAKE_FAILURE;
1321 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1322 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1323 goto f_err;
1325 for (i = 0; r[i] != NULL; i++) {
1326 nr[i] = BN_num_bytes(r[i]);
1327 n += 2 + nr[i];
1330 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1331 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
1332 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, NULL))
1333 == NULL) {
1334 al = SSL_AD_DECODE_ERROR;
1335 goto f_err;
1337 kn = EVP_PKEY_size(pkey);
1338 } else {
1339 pkey = NULL;
1340 kn = 0;
1343 if (!BUF_MEM_grow_clean(buf, n + DTLS1_HM_HEADER_LENGTH + kn)) {
1344 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1345 goto err;
1347 d = (unsigned char *)s->init_buf->data;
1348 p = &(d[DTLS1_HM_HEADER_LENGTH]);
1350 for (i = 0; r[i] != NULL; i++) {
1351 s2n(nr[i], p);
1352 BN_bn2bin(r[i], p);
1353 p += nr[i];
1356 #ifndef OPENSSL_NO_ECDH
1357 if (type & SSL_kEECDH) {
1359 * XXX: For now, we only support named (not generic) curves. In
1360 * this situation, the serverKeyExchange message has: [1 byte
1361 * CurveType], [2 byte CurveName] [1 byte length of encoded
1362 * point], followed by the actual encoded point itself
1364 *p = NAMED_CURVE_TYPE;
1365 p += 1;
1366 *p = 0;
1367 p += 1;
1368 *p = curve_id;
1369 p += 1;
1370 *p = encodedlen;
1371 p += 1;
1372 memcpy((unsigned char *)p,
1373 (unsigned char *)encodedPoint, encodedlen);
1374 OPENSSL_free(encodedPoint);
1375 encodedPoint = NULL;
1376 p += encodedlen;
1378 #endif
1380 #ifndef OPENSSL_NO_PSK
1381 if (type & SSL_kPSK) {
1382 /* copy PSK identity hint */
1383 s2n(strlen(s->ctx->psk_identity_hint), p);
1384 strncpy((char *)p, s->ctx->psk_identity_hint,
1385 strlen(s->ctx->psk_identity_hint));
1386 p += strlen(s->ctx->psk_identity_hint);
1388 #endif
1390 /* not anonymous */
1391 if (pkey != NULL) {
1393 * n is the length of the params, they start at
1394 * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space at the
1395 * end.
1397 #ifndef OPENSSL_NO_RSA
1398 if (pkey->type == EVP_PKEY_RSA) {
1399 q = md_buf;
1400 j = 0;
1401 for (num = 2; num > 0; num--) {
1402 EVP_DigestInit_ex(&md_ctx, (num == 2)
1403 ? s->ctx->md5 : s->ctx->sha1, NULL);
1404 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1405 SSL3_RANDOM_SIZE);
1406 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1407 SSL3_RANDOM_SIZE);
1408 EVP_DigestUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]),
1410 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1411 q += i;
1412 j += i;
1414 if (RSA_sign(NID_md5_sha1, md_buf, j,
1415 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1416 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
1417 goto err;
1419 s2n(u, p);
1420 n += u + 2;
1421 } else
1422 #endif
1423 #if !defined(OPENSSL_NO_DSA)
1424 if (pkey->type == EVP_PKEY_DSA) {
1425 /* lets do DSS */
1426 EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL);
1427 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1428 SSL3_RANDOM_SIZE);
1429 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1430 SSL3_RANDOM_SIZE);
1431 EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n);
1432 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1433 (unsigned int *)&i, pkey)) {
1434 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA);
1435 goto err;
1437 s2n(i, p);
1438 n += i + 2;
1439 } else
1440 #endif
1441 #if !defined(OPENSSL_NO_ECDSA)
1442 if (pkey->type == EVP_PKEY_EC) {
1443 /* let's do ECDSA */
1444 EVP_SignInit_ex(&md_ctx, EVP_ecdsa(), NULL);
1445 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1446 SSL3_RANDOM_SIZE);
1447 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1448 SSL3_RANDOM_SIZE);
1449 EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n);
1450 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1451 (unsigned int *)&i, pkey)) {
1452 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1453 ERR_LIB_ECDSA);
1454 goto err;
1456 s2n(i, p);
1457 n += i + 2;
1458 } else
1459 #endif
1461 /* Is this error check actually needed? */
1462 al = SSL_AD_HANDSHAKE_FAILURE;
1463 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1464 SSL_R_UNKNOWN_PKEY_TYPE);
1465 goto f_err;
1469 d = dtls1_set_message_header(s, d,
1470 SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);
1473 * we should now have things packed up, so lets send it off
1475 s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1476 s->init_off = 0;
1478 /* buffer the message to handle re-xmits */
1479 dtls1_buffer_message(s, 0);
1482 s->state = SSL3_ST_SW_KEY_EXCH_B;
1483 EVP_MD_CTX_cleanup(&md_ctx);
1484 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1485 f_err:
1486 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1487 err:
1488 #ifndef OPENSSL_NO_ECDH
1489 if (encodedPoint != NULL)
1490 OPENSSL_free(encodedPoint);
1491 BN_CTX_free(bn_ctx);
1492 #endif
1493 EVP_MD_CTX_cleanup(&md_ctx);
1494 return (-1);
1497 int dtls1_send_certificate_request(SSL *s)
1499 unsigned char *p, *d;
1500 int i, j, nl, off, n;
1501 STACK_OF(X509_NAME) *sk = NULL;
1502 X509_NAME *name;
1503 BUF_MEM *buf;
1504 unsigned int msg_len;
1506 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1507 buf = s->init_buf;
1509 d = p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
1511 /* get the list of acceptable cert types */
1512 p++;
1513 n = ssl3_get_req_cert_type(s, p);
1514 d[0] = n;
1515 p += n;
1516 n++;
1518 off = n;
1519 p += 2;
1520 n += 2;
1522 sk = SSL_get_client_CA_list(s);
1523 nl = 0;
1524 if (sk != NULL) {
1525 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1526 name = sk_X509_NAME_value(sk, i);
1527 j = i2d_X509_NAME(name, NULL);
1528 if (!BUF_MEM_grow_clean
1529 (buf, DTLS1_HM_HEADER_LENGTH + n + j + 2)) {
1530 SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,
1531 ERR_R_BUF_LIB);
1532 goto err;
1534 p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + n]);
1535 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
1536 s2n(j, p);
1537 i2d_X509_NAME(name, &p);
1538 n += 2 + j;
1539 nl += 2 + j;
1540 } else {
1541 d = p;
1542 i2d_X509_NAME(name, &p);
1543 j -= 2;
1544 s2n(j, d);
1545 j += 2;
1546 n += j;
1547 nl += j;
1551 /* else no CA names */
1552 p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + off]);
1553 s2n(nl, p);
1555 d = (unsigned char *)buf->data;
1556 *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
1557 l2n3(n, d);
1558 s2n(s->d1->handshake_write_seq, d);
1559 s->d1->handshake_write_seq++;
1562 * we should now have things packed up, so lets send it off
1565 s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1566 s->init_off = 0;
1567 #ifdef NETSCAPE_HANG_BUG
1568 /* XXX: what to do about this? */
1569 p = (unsigned char *)s->init_buf->data + s->init_num;
1571 /* do the header */
1572 *(p++) = SSL3_MT_SERVER_DONE;
1573 *(p++) = 0;
1574 *(p++) = 0;
1575 *(p++) = 0;
1576 s->init_num += 4;
1577 #endif
1579 /* XDTLS: set message header ? */
1580 msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
1581 dtls1_set_message_header(s, (void *)s->init_buf->data,
1582 SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0,
1583 msg_len);
1585 /* buffer the message to handle re-xmits */
1586 dtls1_buffer_message(s, 0);
1588 s->state = SSL3_ST_SW_CERT_REQ_B;
1591 /* SSL3_ST_SW_CERT_REQ_B */
1592 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1593 err:
1594 return (-1);
1597 int dtls1_send_server_certificate(SSL *s)
1599 unsigned long l;
1600 X509 *x;
1602 if (s->state == SSL3_ST_SW_CERT_A) {
1603 x = ssl_get_server_send_cert(s);
1604 if (x == NULL) {
1605 /* VRS: allow null cert if auth == KRB5 */
1606 if ((s->s3->tmp.new_cipher->algorithm_mkey != SSL_kKRB5) ||
1607 (s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5)) {
1608 SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,
1609 ERR_R_INTERNAL_ERROR);
1610 return (0);
1614 l = dtls1_output_cert_chain(s, x);
1615 if (!l) {
1616 SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1617 return (0);
1619 s->state = SSL3_ST_SW_CERT_B;
1620 s->init_num = (int)l;
1621 s->init_off = 0;
1623 /* buffer the message to handle re-xmits */
1624 dtls1_buffer_message(s, 0);
1627 /* SSL3_ST_SW_CERT_B */
1628 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1631 #ifndef OPENSSL_NO_TLSEXT
1632 int dtls1_send_newsession_ticket(SSL *s)
1634 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
1635 unsigned char *p, *senc, *macstart;
1636 int len, slen;
1637 unsigned int hlen, msg_len;
1638 EVP_CIPHER_CTX ctx;
1639 HMAC_CTX hctx;
1640 SSL_CTX *tctx = s->initial_ctx;
1641 unsigned char iv[EVP_MAX_IV_LENGTH];
1642 unsigned char key_name[16];
1644 /* get session encoding length */
1645 slen = i2d_SSL_SESSION(s->session, NULL);
1647 * Some length values are 16 bits, so forget it if session is too
1648 * long
1650 if (slen > 0xFF00)
1651 return -1;
1653 * Grow buffer if need be: the length calculation is as follows 12
1654 * (DTLS handshake message header) + 4 (ticket lifetime hint) + 2
1655 * (ticket length) + 16 (key name) + max_iv_len (iv length) +
1656 * session_length + max_enc_block_size (max encrypted session length)
1657 * + max_md_size (HMAC).
1659 if (!BUF_MEM_grow(s->init_buf,
1660 DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH +
1661 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
1662 return -1;
1663 senc = OPENSSL_malloc(slen);
1664 if (!senc)
1665 return -1;
1666 p = senc;
1667 i2d_SSL_SESSION(s->session, &p);
1669 p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]);
1670 EVP_CIPHER_CTX_init(&ctx);
1671 HMAC_CTX_init(&hctx);
1673 * Initialize HMAC and cipher contexts. If callback present it does
1674 * all the work otherwise use generated values from parent ctx.
1676 if (tctx->tlsext_ticket_key_cb) {
1677 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
1678 &hctx, 1) < 0) {
1679 OPENSSL_free(senc);
1680 return -1;
1682 } else {
1683 RAND_pseudo_bytes(iv, 16);
1684 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
1685 tctx->tlsext_tick_aes_key, iv);
1686 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
1687 tlsext_tick_md(), NULL);
1688 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
1690 l2n(s->session->tlsext_tick_lifetime_hint, p);
1691 /* Skip ticket length for now */
1692 p += 2;
1693 /* Output key name */
1694 macstart = p;
1695 memcpy(p, key_name, 16);
1696 p += 16;
1697 /* output IV */
1698 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
1699 p += EVP_CIPHER_CTX_iv_length(&ctx);
1700 /* Encrypt session data */
1701 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
1702 p += len;
1703 EVP_EncryptFinal(&ctx, p, &len);
1704 p += len;
1705 EVP_CIPHER_CTX_cleanup(&ctx);
1707 HMAC_Update(&hctx, macstart, p - macstart);
1708 HMAC_Final(&hctx, p, &hlen);
1709 HMAC_CTX_cleanup(&hctx);
1711 p += hlen;
1712 /* Now write out lengths: p points to end of data written */
1713 /* Total length */
1714 len = p - (unsigned char *)(s->init_buf->data);
1715 /* Ticket length */
1716 p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4;
1717 s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p);
1719 /* number of bytes to write */
1720 s->init_num = len;
1721 s->state = SSL3_ST_SW_SESSION_TICKET_B;
1722 s->init_off = 0;
1723 OPENSSL_free(senc);
1725 /* XDTLS: set message header ? */
1726 msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
1727 dtls1_set_message_header(s, (void *)s->init_buf->data,
1728 SSL3_MT_NEWSESSION_TICKET, msg_len, 0,
1729 msg_len);
1731 /* buffer the message to handle re-xmits */
1732 dtls1_buffer_message(s, 0);
1735 /* SSL3_ST_SW_SESSION_TICKET_B */
1736 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1738 #endif