xref: /minix3/crypto/external/bsd/openssl/dist/ssl/d1_srvr.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ssl/d1_srvr.c */
2ebfedea0SLionel Sambuc /*
3ebfedea0SLionel Sambuc  * DTLS implementation written by Nagendra Modadugu
4ebfedea0SLionel Sambuc  * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5ebfedea0SLionel Sambuc  */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc  * Copyright (c) 1999-2007 The OpenSSL Project.  All rights reserved.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc  * are met:
12ebfedea0SLionel Sambuc  *
13ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc  *    distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
23ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc  *
26ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc  *    openssl-core@OpenSSL.org.
30ebfedea0SLionel Sambuc  *
31ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc  *
35ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc  *    acknowledgment:
37ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc  *
40ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc  * ====================================================================
53ebfedea0SLionel Sambuc  *
54ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
56ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc  *
58ebfedea0SLionel Sambuc  */
59ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60ebfedea0SLionel Sambuc  * All rights reserved.
61ebfedea0SLionel Sambuc  *
62ebfedea0SLionel Sambuc  * This package is an SSL implementation written
63ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
64ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
65ebfedea0SLionel Sambuc  *
66ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
67ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
68ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
69ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
70ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
71ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
72ebfedea0SLionel Sambuc  *
73ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
74ebfedea0SLionel Sambuc  * the code are not to be removed.
75ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
76ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
77ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
78ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
79ebfedea0SLionel Sambuc  *
80ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
81ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
82ebfedea0SLionel Sambuc  * are met:
83ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
84ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
85ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
86ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
87ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
88ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
89ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
90ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
91ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
92ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
93ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
94ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
95ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
96ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
97ebfedea0SLionel Sambuc  *
98ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108ebfedea0SLionel Sambuc  * SUCH DAMAGE.
109ebfedea0SLionel Sambuc  *
110ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
111ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
112ebfedea0SLionel Sambuc  * copied and put under another distribution licence
113ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
114ebfedea0SLionel Sambuc  */
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc #include <stdio.h>
117ebfedea0SLionel Sambuc #include "ssl_locl.h"
118ebfedea0SLionel Sambuc #include <openssl/buffer.h>
119ebfedea0SLionel Sambuc #include <openssl/rand.h>
120ebfedea0SLionel Sambuc #include <openssl/objects.h>
121ebfedea0SLionel Sambuc #include <openssl/evp.h>
122ebfedea0SLionel Sambuc #include <openssl/x509.h>
123ebfedea0SLionel Sambuc #include <openssl/md5.h>
124ebfedea0SLionel Sambuc #include <openssl/bn.h>
125ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
126ebfedea0SLionel Sambuc # include <openssl/dh.h>
127ebfedea0SLionel Sambuc #endif
128ebfedea0SLionel Sambuc 
129ebfedea0SLionel Sambuc static const SSL_METHOD *dtls1_get_server_method(int ver);
130ebfedea0SLionel Sambuc static int dtls1_send_hello_verify_request(SSL *s);
131ebfedea0SLionel Sambuc 
dtls1_get_server_method(int ver)132ebfedea0SLionel Sambuc static const SSL_METHOD *dtls1_get_server_method(int ver)
133ebfedea0SLionel Sambuc {
134ebfedea0SLionel Sambuc     if (ver == DTLS1_VERSION)
135ebfedea0SLionel Sambuc         return (DTLSv1_server_method());
136ebfedea0SLionel Sambuc     else
137ebfedea0SLionel Sambuc         return (NULL);
138ebfedea0SLionel Sambuc }
139ebfedea0SLionel Sambuc 
IMPLEMENT_dtls1_meth_func(DTLSv1_server_method,dtls1_accept,ssl_undefined_function,dtls1_get_server_method)140ebfedea0SLionel Sambuc IMPLEMENT_dtls1_meth_func(DTLSv1_server_method,
141ebfedea0SLionel Sambuc                           dtls1_accept,
142*0a6a1f1dSLionel Sambuc                           ssl_undefined_function, dtls1_get_server_method)
143ebfedea0SLionel Sambuc 
144ebfedea0SLionel Sambuc int dtls1_accept(SSL *s)
145ebfedea0SLionel Sambuc {
146ebfedea0SLionel Sambuc     BUF_MEM *buf;
147ebfedea0SLionel Sambuc     unsigned long Time = (unsigned long)time(NULL);
148ebfedea0SLionel Sambuc     void (*cb) (const SSL *ssl, int type, int val) = NULL;
149ebfedea0SLionel Sambuc     unsigned long alg_k;
150ebfedea0SLionel Sambuc     int ret = -1;
151ebfedea0SLionel Sambuc     int new_state, state, skip = 0;
152ebfedea0SLionel Sambuc     int listen;
153ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
154ebfedea0SLionel Sambuc     unsigned char sctpauthkey[64];
155ebfedea0SLionel Sambuc     char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
156ebfedea0SLionel Sambuc #endif
157ebfedea0SLionel Sambuc 
158ebfedea0SLionel Sambuc     RAND_add(&Time, sizeof(Time), 0);
159ebfedea0SLionel Sambuc     ERR_clear_error();
160ebfedea0SLionel Sambuc     clear_sys_error();
161ebfedea0SLionel Sambuc 
162ebfedea0SLionel Sambuc     if (s->info_callback != NULL)
163ebfedea0SLionel Sambuc         cb = s->info_callback;
164ebfedea0SLionel Sambuc     else if (s->ctx->info_callback != NULL)
165ebfedea0SLionel Sambuc         cb = s->ctx->info_callback;
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc     listen = s->d1->listen;
168ebfedea0SLionel Sambuc 
169ebfedea0SLionel Sambuc     /* init things to blank */
170ebfedea0SLionel Sambuc     s->in_handshake++;
171*0a6a1f1dSLionel Sambuc     if (!SSL_in_init(s) || SSL_in_before(s))
172*0a6a1f1dSLionel Sambuc         SSL_clear(s);
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc     s->d1->listen = listen;
175ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
176*0a6a1f1dSLionel Sambuc     /*
177*0a6a1f1dSLionel Sambuc      * Notify SCTP BIO socket to enter handshake mode and prevent stream
178*0a6a1f1dSLionel Sambuc      * identifier other than 0. Will be ignored if no SCTP is used.
179ebfedea0SLionel Sambuc      */
180*0a6a1f1dSLionel Sambuc     BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
181*0a6a1f1dSLionel Sambuc              s->in_handshake, NULL);
182ebfedea0SLionel Sambuc #endif
183ebfedea0SLionel Sambuc 
184*0a6a1f1dSLionel Sambuc     if (s->cert == NULL) {
185ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
186ebfedea0SLionel Sambuc         return (-1);
187ebfedea0SLionel Sambuc     }
188ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HEARTBEATS
189*0a6a1f1dSLionel Sambuc     /*
190*0a6a1f1dSLionel Sambuc      * If we're awaiting a HeartbeatResponse, pretend we already got and
191*0a6a1f1dSLionel Sambuc      * don't await it anymore, because Heartbeats don't make sense during
192*0a6a1f1dSLionel Sambuc      * handshakes anyway.
193ebfedea0SLionel Sambuc      */
194*0a6a1f1dSLionel Sambuc     if (s->tlsext_hb_pending) {
195ebfedea0SLionel Sambuc         dtls1_stop_timer(s);
196ebfedea0SLionel Sambuc         s->tlsext_hb_pending = 0;
197ebfedea0SLionel Sambuc         s->tlsext_hb_seq++;
198ebfedea0SLionel Sambuc     }
199ebfedea0SLionel Sambuc #endif
200ebfedea0SLionel Sambuc 
201*0a6a1f1dSLionel Sambuc     for (;;) {
202ebfedea0SLionel Sambuc         state = s->state;
203ebfedea0SLionel Sambuc 
204*0a6a1f1dSLionel Sambuc         switch (s->state) {
205ebfedea0SLionel Sambuc         case SSL_ST_RENEGOTIATE:
206ebfedea0SLionel Sambuc             s->renegotiate = 1;
207ebfedea0SLionel Sambuc             /* s->state=SSL_ST_ACCEPT; */
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc         case SSL_ST_BEFORE:
210ebfedea0SLionel Sambuc         case SSL_ST_ACCEPT:
211ebfedea0SLionel Sambuc         case SSL_ST_BEFORE | SSL_ST_ACCEPT:
212ebfedea0SLionel Sambuc         case SSL_ST_OK | SSL_ST_ACCEPT:
213ebfedea0SLionel Sambuc 
214ebfedea0SLionel Sambuc             s->server = 1;
215*0a6a1f1dSLionel Sambuc             if (cb != NULL)
216*0a6a1f1dSLionel Sambuc                 cb(s, SSL_CB_HANDSHAKE_START, 1);
217ebfedea0SLionel Sambuc 
218*0a6a1f1dSLionel Sambuc             if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {
219ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR);
220ebfedea0SLionel Sambuc                 return -1;
221ebfedea0SLionel Sambuc             }
222ebfedea0SLionel Sambuc             s->type = SSL_ST_ACCEPT;
223ebfedea0SLionel Sambuc 
224*0a6a1f1dSLionel Sambuc             if (s->init_buf == NULL) {
225*0a6a1f1dSLionel Sambuc                 if ((buf = BUF_MEM_new()) == NULL) {
226ebfedea0SLionel Sambuc                     ret = -1;
227*0a6a1f1dSLionel Sambuc                     s->state = SSL_ST_ERR;
228ebfedea0SLionel Sambuc                     goto end;
229ebfedea0SLionel Sambuc                 }
230*0a6a1f1dSLionel Sambuc                 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
231*0a6a1f1dSLionel Sambuc                     BUF_MEM_free(buf);
232ebfedea0SLionel Sambuc                     ret = -1;
233*0a6a1f1dSLionel Sambuc                     s->state = SSL_ST_ERR;
234ebfedea0SLionel Sambuc                     goto end;
235ebfedea0SLionel Sambuc                 }
236ebfedea0SLionel Sambuc                 s->init_buf = buf;
237ebfedea0SLionel Sambuc             }
238ebfedea0SLionel Sambuc 
239*0a6a1f1dSLionel Sambuc             if (!ssl3_setup_buffers(s)) {
240ebfedea0SLionel Sambuc                 ret = -1;
241*0a6a1f1dSLionel Sambuc                 s->state = SSL_ST_ERR;
242ebfedea0SLionel Sambuc                 goto end;
243ebfedea0SLionel Sambuc             }
244ebfedea0SLionel Sambuc 
245ebfedea0SLionel Sambuc             s->init_num = 0;
246*0a6a1f1dSLionel Sambuc             s->d1->change_cipher_spec_ok = 0;
247*0a6a1f1dSLionel Sambuc             /*
248*0a6a1f1dSLionel Sambuc              * Should have been reset by ssl3_get_finished, too.
249*0a6a1f1dSLionel Sambuc              */
250*0a6a1f1dSLionel Sambuc             s->s3->change_cipher_spec = 0;
251ebfedea0SLionel Sambuc 
252*0a6a1f1dSLionel Sambuc             if (s->state != SSL_ST_RENEGOTIATE) {
253*0a6a1f1dSLionel Sambuc                 /*
254*0a6a1f1dSLionel Sambuc                  * Ok, we now need to push on a buffering BIO so that the
255*0a6a1f1dSLionel Sambuc                  * output is sent in a way that TCP likes :-) ...but not with
256*0a6a1f1dSLionel Sambuc                  * SCTP :-)
257ebfedea0SLionel Sambuc                  */
258ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
259ebfedea0SLionel Sambuc                 if (!BIO_dgram_is_sctp(SSL_get_wbio(s)))
260ebfedea0SLionel Sambuc #endif
261*0a6a1f1dSLionel Sambuc                     if (!ssl_init_wbio_buffer(s, 1)) {
262*0a6a1f1dSLionel Sambuc                         ret = -1;
263*0a6a1f1dSLionel Sambuc                         s->state = SSL_ST_ERR;
264*0a6a1f1dSLionel Sambuc                         goto end;
265*0a6a1f1dSLionel Sambuc                     }
266ebfedea0SLionel Sambuc 
267ebfedea0SLionel Sambuc                 ssl3_init_finished_mac(s);
268ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SR_CLNT_HELLO_A;
269ebfedea0SLionel Sambuc                 s->ctx->stats.sess_accept++;
270*0a6a1f1dSLionel Sambuc             } else {
271*0a6a1f1dSLionel Sambuc                 /*
272*0a6a1f1dSLionel Sambuc                  * s->state == SSL_ST_RENEGOTIATE, we will just send a
273*0a6a1f1dSLionel Sambuc                  * HelloRequest
274*0a6a1f1dSLionel Sambuc                  */
275ebfedea0SLionel Sambuc                 s->ctx->stats.sess_accept_renegotiate++;
276ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SW_HELLO_REQ_A;
277ebfedea0SLionel Sambuc             }
278ebfedea0SLionel Sambuc 
279ebfedea0SLionel Sambuc             break;
280ebfedea0SLionel Sambuc 
281ebfedea0SLionel Sambuc         case SSL3_ST_SW_HELLO_REQ_A:
282ebfedea0SLionel Sambuc         case SSL3_ST_SW_HELLO_REQ_B:
283ebfedea0SLionel Sambuc 
284ebfedea0SLionel Sambuc             s->shutdown = 0;
285*0a6a1f1dSLionel Sambuc             dtls1_clear_record_buffer(s);
286ebfedea0SLionel Sambuc             dtls1_start_timer(s);
287ebfedea0SLionel Sambuc             ret = dtls1_send_hello_request(s);
288*0a6a1f1dSLionel Sambuc             if (ret <= 0)
289*0a6a1f1dSLionel Sambuc                 goto end;
290*0a6a1f1dSLionel Sambuc             s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A;
291ebfedea0SLionel Sambuc             s->state = SSL3_ST_SW_FLUSH;
292ebfedea0SLionel Sambuc             s->init_num = 0;
293ebfedea0SLionel Sambuc 
294ebfedea0SLionel Sambuc             ssl3_init_finished_mac(s);
295ebfedea0SLionel Sambuc             break;
296ebfedea0SLionel Sambuc 
297ebfedea0SLionel Sambuc         case SSL3_ST_SW_HELLO_REQ_C:
298ebfedea0SLionel Sambuc             s->state = SSL_ST_OK;
299ebfedea0SLionel Sambuc             break;
300ebfedea0SLionel Sambuc 
301ebfedea0SLionel Sambuc         case SSL3_ST_SR_CLNT_HELLO_A:
302ebfedea0SLionel Sambuc         case SSL3_ST_SR_CLNT_HELLO_B:
303ebfedea0SLionel Sambuc         case SSL3_ST_SR_CLNT_HELLO_C:
304ebfedea0SLionel Sambuc 
305ebfedea0SLionel Sambuc             s->shutdown = 0;
306ebfedea0SLionel Sambuc             ret = ssl3_get_client_hello(s);
307*0a6a1f1dSLionel Sambuc             if (ret <= 0)
308*0a6a1f1dSLionel Sambuc                 goto end;
309ebfedea0SLionel Sambuc             dtls1_stop_timer(s);
310ebfedea0SLionel Sambuc 
311ebfedea0SLionel Sambuc             if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
312ebfedea0SLionel Sambuc                 s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
313ebfedea0SLionel Sambuc             else
314ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SW_SRVR_HELLO_A;
315ebfedea0SLionel Sambuc 
316ebfedea0SLionel Sambuc             s->init_num = 0;
317ebfedea0SLionel Sambuc 
318*0a6a1f1dSLionel Sambuc             /*
319*0a6a1f1dSLionel Sambuc              * Reflect ClientHello sequence to remain stateless while
320*0a6a1f1dSLionel Sambuc              * listening
321*0a6a1f1dSLionel Sambuc              */
322*0a6a1f1dSLionel Sambuc             if (listen) {
323*0a6a1f1dSLionel Sambuc                 memcpy(s->s3->write_sequence, s->s3->read_sequence,
324*0a6a1f1dSLionel Sambuc                        sizeof(s->s3->write_sequence));
325ebfedea0SLionel Sambuc             }
326ebfedea0SLionel Sambuc 
327ebfedea0SLionel Sambuc             /* If we're just listening, stop here */
328*0a6a1f1dSLionel Sambuc             if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A) {
329ebfedea0SLionel Sambuc                 ret = 2;
330ebfedea0SLionel Sambuc                 s->d1->listen = 0;
331*0a6a1f1dSLionel Sambuc                 /*
332*0a6a1f1dSLionel Sambuc                  * Set expected sequence numbers to continue the handshake.
333ebfedea0SLionel Sambuc                  */
334ebfedea0SLionel Sambuc                 s->d1->handshake_read_seq = 2;
335ebfedea0SLionel Sambuc                 s->d1->handshake_write_seq = 1;
336ebfedea0SLionel Sambuc                 s->d1->next_handshake_write_seq = 1;
337ebfedea0SLionel Sambuc                 goto end;
338ebfedea0SLionel Sambuc             }
339ebfedea0SLionel Sambuc 
340ebfedea0SLionel Sambuc             break;
341ebfedea0SLionel Sambuc 
342ebfedea0SLionel Sambuc         case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
343ebfedea0SLionel Sambuc         case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
344ebfedea0SLionel Sambuc 
345ebfedea0SLionel Sambuc             ret = dtls1_send_hello_verify_request(s);
346*0a6a1f1dSLionel Sambuc             if (ret <= 0)
347*0a6a1f1dSLionel Sambuc                 goto end;
348ebfedea0SLionel Sambuc             s->state = SSL3_ST_SW_FLUSH;
349ebfedea0SLionel Sambuc             s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A;
350ebfedea0SLionel Sambuc 
351ebfedea0SLionel Sambuc             /* HelloVerifyRequest resets Finished MAC */
352ebfedea0SLionel Sambuc             if (s->version != DTLS1_BAD_VER)
353ebfedea0SLionel Sambuc                 ssl3_init_finished_mac(s);
354ebfedea0SLionel Sambuc             break;
355ebfedea0SLionel Sambuc 
356ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
357ebfedea0SLionel Sambuc         case DTLS1_SCTP_ST_SR_READ_SOCK:
358ebfedea0SLionel Sambuc 
359*0a6a1f1dSLionel Sambuc             if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
360ebfedea0SLionel Sambuc                 s->s3->in_read_app_data = 2;
361ebfedea0SLionel Sambuc                 s->rwstate = SSL_READING;
362ebfedea0SLionel Sambuc                 BIO_clear_retry_flags(SSL_get_rbio(s));
363ebfedea0SLionel Sambuc                 BIO_set_retry_read(SSL_get_rbio(s));
364ebfedea0SLionel Sambuc                 ret = -1;
365ebfedea0SLionel Sambuc                 goto end;
366ebfedea0SLionel Sambuc             }
367ebfedea0SLionel Sambuc 
368ebfedea0SLionel Sambuc             s->state = SSL3_ST_SR_FINISHED_A;
369ebfedea0SLionel Sambuc             break;
370ebfedea0SLionel Sambuc 
371ebfedea0SLionel Sambuc         case DTLS1_SCTP_ST_SW_WRITE_SOCK:
372ebfedea0SLionel Sambuc             ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
373*0a6a1f1dSLionel Sambuc             if (ret < 0)
374*0a6a1f1dSLionel Sambuc                 goto end;
375ebfedea0SLionel Sambuc 
376*0a6a1f1dSLionel Sambuc             if (ret == 0) {
377*0a6a1f1dSLionel Sambuc                 if (s->d1->next_state != SSL_ST_OK) {
378ebfedea0SLionel Sambuc                     s->s3->in_read_app_data = 2;
379ebfedea0SLionel Sambuc                     s->rwstate = SSL_READING;
380ebfedea0SLionel Sambuc                     BIO_clear_retry_flags(SSL_get_rbio(s));
381ebfedea0SLionel Sambuc                     BIO_set_retry_read(SSL_get_rbio(s));
382ebfedea0SLionel Sambuc                     ret = -1;
383ebfedea0SLionel Sambuc                     goto end;
384ebfedea0SLionel Sambuc                 }
385ebfedea0SLionel Sambuc             }
386ebfedea0SLionel Sambuc 
387ebfedea0SLionel Sambuc             s->state = s->d1->next_state;
388ebfedea0SLionel Sambuc             break;
389ebfedea0SLionel Sambuc #endif
390ebfedea0SLionel Sambuc 
391ebfedea0SLionel Sambuc         case SSL3_ST_SW_SRVR_HELLO_A:
392ebfedea0SLionel Sambuc         case SSL3_ST_SW_SRVR_HELLO_B:
393ebfedea0SLionel Sambuc             s->renegotiate = 2;
394ebfedea0SLionel Sambuc             dtls1_start_timer(s);
395ebfedea0SLionel Sambuc             ret = dtls1_send_server_hello(s);
396*0a6a1f1dSLionel Sambuc             if (ret <= 0)
397*0a6a1f1dSLionel Sambuc                 goto end;
398ebfedea0SLionel Sambuc 
399*0a6a1f1dSLionel Sambuc             if (s->hit) {
400ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
401*0a6a1f1dSLionel Sambuc                 /*
402*0a6a1f1dSLionel Sambuc                  * Add new shared key for SCTP-Auth, will be ignored if no
403*0a6a1f1dSLionel Sambuc                  * SCTP used.
404ebfedea0SLionel Sambuc                  */
405ebfedea0SLionel Sambuc                 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
406ebfedea0SLionel Sambuc                          DTLS1_SCTP_AUTH_LABEL);
407ebfedea0SLionel Sambuc 
408ebfedea0SLionel Sambuc                 SSL_export_keying_material(s, sctpauthkey,
409ebfedea0SLionel Sambuc                                            sizeof(sctpauthkey), labelbuffer,
410ebfedea0SLionel Sambuc                                            sizeof(labelbuffer), NULL, 0, 0);
411ebfedea0SLionel Sambuc 
412ebfedea0SLionel Sambuc                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
413ebfedea0SLionel Sambuc                          sizeof(sctpauthkey), sctpauthkey);
414ebfedea0SLionel Sambuc #endif
415ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
416ebfedea0SLionel Sambuc                 if (s->tlsext_ticket_expected)
417ebfedea0SLionel Sambuc                     s->state = SSL3_ST_SW_SESSION_TICKET_A;
418ebfedea0SLionel Sambuc                 else
419ebfedea0SLionel Sambuc                     s->state = SSL3_ST_SW_CHANGE_A;
420ebfedea0SLionel Sambuc #else
421ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SW_CHANGE_A;
422ebfedea0SLionel Sambuc #endif
423*0a6a1f1dSLionel Sambuc             } else
424ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SW_CERT_A;
425ebfedea0SLionel Sambuc             s->init_num = 0;
426ebfedea0SLionel Sambuc             break;
427ebfedea0SLionel Sambuc 
428ebfedea0SLionel Sambuc         case SSL3_ST_SW_CERT_A:
429ebfedea0SLionel Sambuc         case SSL3_ST_SW_CERT_B:
430ebfedea0SLionel Sambuc             /* Check if it is anon DH or normal PSK */
431ebfedea0SLionel Sambuc             if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
432*0a6a1f1dSLionel Sambuc                 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
433ebfedea0SLionel Sambuc                 dtls1_start_timer(s);
434ebfedea0SLionel Sambuc                 ret = dtls1_send_server_certificate(s);
435*0a6a1f1dSLionel Sambuc                 if (ret <= 0)
436*0a6a1f1dSLionel Sambuc                     goto end;
437ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
438ebfedea0SLionel Sambuc                 if (s->tlsext_status_expected)
439ebfedea0SLionel Sambuc                     s->state = SSL3_ST_SW_CERT_STATUS_A;
440ebfedea0SLionel Sambuc                 else
441ebfedea0SLionel Sambuc                     s->state = SSL3_ST_SW_KEY_EXCH_A;
442*0a6a1f1dSLionel Sambuc             } else {
443ebfedea0SLionel Sambuc                 skip = 1;
444ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SW_KEY_EXCH_A;
445ebfedea0SLionel Sambuc             }
446ebfedea0SLionel Sambuc #else
447*0a6a1f1dSLionel Sambuc             } else
448ebfedea0SLionel Sambuc                 skip = 1;
449ebfedea0SLionel Sambuc 
450ebfedea0SLionel Sambuc             s->state = SSL3_ST_SW_KEY_EXCH_A;
451ebfedea0SLionel Sambuc #endif
452ebfedea0SLionel Sambuc             s->init_num = 0;
453ebfedea0SLionel Sambuc             break;
454ebfedea0SLionel Sambuc 
455ebfedea0SLionel Sambuc         case SSL3_ST_SW_KEY_EXCH_A:
456ebfedea0SLionel Sambuc         case SSL3_ST_SW_KEY_EXCH_B:
457ebfedea0SLionel Sambuc             alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
458ebfedea0SLionel Sambuc 
459*0a6a1f1dSLionel Sambuc             /*
460*0a6a1f1dSLionel Sambuc              * clear this, it may get reset by
461*0a6a1f1dSLionel Sambuc              * send_server_key_exchange
462*0a6a1f1dSLionel Sambuc              */
463ebfedea0SLionel Sambuc             s->s3->tmp.use_rsa_tmp = 0;
464ebfedea0SLionel Sambuc 
465*0a6a1f1dSLionel Sambuc             /*
466*0a6a1f1dSLionel Sambuc              * only send if a DH key exchange or RSA but we have a sign only
467*0a6a1f1dSLionel Sambuc              * certificate
468*0a6a1f1dSLionel Sambuc              */
469*0a6a1f1dSLionel Sambuc             if (0
470*0a6a1f1dSLionel Sambuc                 /*
471*0a6a1f1dSLionel Sambuc                  * PSK: send ServerKeyExchange if PSK identity hint if
472*0a6a1f1dSLionel Sambuc                  * provided
473*0a6a1f1dSLionel Sambuc                  */
474ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
475ebfedea0SLionel Sambuc                 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
476ebfedea0SLionel Sambuc #endif
477*0a6a1f1dSLionel Sambuc                 || (alg_k & SSL_kEDH)
478ebfedea0SLionel Sambuc                 || (alg_k & SSL_kEECDH)
479ebfedea0SLionel Sambuc                 || ((alg_k & SSL_kRSA)
480ebfedea0SLionel Sambuc                     && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
481ebfedea0SLionel Sambuc                         || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
482*0a6a1f1dSLionel Sambuc                             && EVP_PKEY_size(s->cert->pkeys
483*0a6a1f1dSLionel Sambuc                                              [SSL_PKEY_RSA_ENC].privatekey) *
484*0a6a1f1dSLionel Sambuc                             8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
485ebfedea0SLionel Sambuc                         )
486ebfedea0SLionel Sambuc                     )
487ebfedea0SLionel Sambuc                 )
488*0a6a1f1dSLionel Sambuc                 ) {
489ebfedea0SLionel Sambuc                 dtls1_start_timer(s);
490ebfedea0SLionel Sambuc                 ret = dtls1_send_server_key_exchange(s);
491*0a6a1f1dSLionel Sambuc                 if (ret <= 0)
492*0a6a1f1dSLionel Sambuc                     goto end;
493*0a6a1f1dSLionel Sambuc             } else
494ebfedea0SLionel Sambuc                 skip = 1;
495ebfedea0SLionel Sambuc 
496ebfedea0SLionel Sambuc             s->state = SSL3_ST_SW_CERT_REQ_A;
497ebfedea0SLionel Sambuc             s->init_num = 0;
498ebfedea0SLionel Sambuc             break;
499ebfedea0SLionel Sambuc 
500ebfedea0SLionel Sambuc         case SSL3_ST_SW_CERT_REQ_A:
501ebfedea0SLionel Sambuc         case SSL3_ST_SW_CERT_REQ_B:
502ebfedea0SLionel Sambuc             if (                /* don't request cert unless asked for it: */
503ebfedea0SLionel Sambuc                    !(s->verify_mode & SSL_VERIFY_PEER) ||
504*0a6a1f1dSLionel Sambuc                    /*
505*0a6a1f1dSLionel Sambuc                     * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
506*0a6a1f1dSLionel Sambuc                     * during re-negotiation:
507*0a6a1f1dSLionel Sambuc                     */
508ebfedea0SLionel Sambuc                    ((s->session->peer != NULL) &&
509ebfedea0SLionel Sambuc                     (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
510*0a6a1f1dSLionel Sambuc                    /*
511*0a6a1f1dSLionel Sambuc                     * never request cert in anonymous ciphersuites (see
512*0a6a1f1dSLionel Sambuc                     * section "Certificate request" in SSL 3 drafts and in
513*0a6a1f1dSLionel Sambuc                     * RFC 2246):
514*0a6a1f1dSLionel Sambuc                     */
515ebfedea0SLionel Sambuc                    ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
516*0a6a1f1dSLionel Sambuc                     /*
517*0a6a1f1dSLionel Sambuc                      * ... except when the application insists on
518*0a6a1f1dSLionel Sambuc                      * verification (against the specs, but s3_clnt.c accepts
519*0a6a1f1dSLionel Sambuc                      * this for SSL 3)
520*0a6a1f1dSLionel Sambuc                      */
521ebfedea0SLionel Sambuc                     !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
522*0a6a1f1dSLionel Sambuc                    /*
523*0a6a1f1dSLionel Sambuc                     * never request cert in Kerberos ciphersuites
524*0a6a1f1dSLionel Sambuc                     */
525ebfedea0SLionel Sambuc                    (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
526*0a6a1f1dSLionel Sambuc                    /*
527*0a6a1f1dSLionel Sambuc                     * With normal PSK Certificates and Certificate Requests
528*0a6a1f1dSLionel Sambuc                     * are omitted
529*0a6a1f1dSLionel Sambuc                     */
530*0a6a1f1dSLionel Sambuc                    || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
531ebfedea0SLionel Sambuc                 /* no cert request */
532ebfedea0SLionel Sambuc                 skip = 1;
533ebfedea0SLionel Sambuc                 s->s3->tmp.cert_request = 0;
534ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SW_SRVR_DONE_A;
535ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
536*0a6a1f1dSLionel Sambuc                 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
537ebfedea0SLionel Sambuc                     s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
538ebfedea0SLionel Sambuc                     s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
539ebfedea0SLionel Sambuc                 }
540ebfedea0SLionel Sambuc #endif
541*0a6a1f1dSLionel Sambuc             } else {
542ebfedea0SLionel Sambuc                 s->s3->tmp.cert_request = 1;
543ebfedea0SLionel Sambuc                 dtls1_start_timer(s);
544ebfedea0SLionel Sambuc                 ret = dtls1_send_certificate_request(s);
545*0a6a1f1dSLionel Sambuc                 if (ret <= 0)
546*0a6a1f1dSLionel Sambuc                     goto end;
547ebfedea0SLionel Sambuc #ifndef NETSCAPE_HANG_BUG
548ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SW_SRVR_DONE_A;
549ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SCTP
550*0a6a1f1dSLionel Sambuc                 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
551ebfedea0SLionel Sambuc                     s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A;
552ebfedea0SLionel Sambuc                     s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
553ebfedea0SLionel Sambuc                 }
554ebfedea0SLionel Sambuc # endif
555ebfedea0SLionel Sambuc #else
556ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SW_FLUSH;
557ebfedea0SLionel Sambuc                 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
558ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SCTP
559*0a6a1f1dSLionel Sambuc                 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
560ebfedea0SLionel Sambuc                     s->d1->next_state = s->s3->tmp.next_state;
561ebfedea0SLionel Sambuc                     s->s3->tmp.next_state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
562ebfedea0SLionel Sambuc                 }
563ebfedea0SLionel Sambuc # endif
564ebfedea0SLionel Sambuc #endif
565ebfedea0SLionel Sambuc                 s->init_num = 0;
566ebfedea0SLionel Sambuc             }
567ebfedea0SLionel Sambuc             break;
568ebfedea0SLionel Sambuc 
569ebfedea0SLionel Sambuc         case SSL3_ST_SW_SRVR_DONE_A:
570ebfedea0SLionel Sambuc         case SSL3_ST_SW_SRVR_DONE_B:
571ebfedea0SLionel Sambuc             dtls1_start_timer(s);
572ebfedea0SLionel Sambuc             ret = dtls1_send_server_done(s);
573*0a6a1f1dSLionel Sambuc             if (ret <= 0)
574*0a6a1f1dSLionel Sambuc                 goto end;
575ebfedea0SLionel Sambuc             s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
576ebfedea0SLionel Sambuc             s->state = SSL3_ST_SW_FLUSH;
577ebfedea0SLionel Sambuc             s->init_num = 0;
578ebfedea0SLionel Sambuc             break;
579ebfedea0SLionel Sambuc 
580ebfedea0SLionel Sambuc         case SSL3_ST_SW_FLUSH:
581ebfedea0SLionel Sambuc             s->rwstate = SSL_WRITING;
582*0a6a1f1dSLionel Sambuc             if (BIO_flush(s->wbio) <= 0) {
583*0a6a1f1dSLionel Sambuc                 /*
584*0a6a1f1dSLionel Sambuc                  * If the write error was fatal, stop trying
585*0a6a1f1dSLionel Sambuc                  */
586*0a6a1f1dSLionel Sambuc                 if (!BIO_should_retry(s->wbio)) {
587ebfedea0SLionel Sambuc                     s->rwstate = SSL_NOTHING;
588ebfedea0SLionel Sambuc                     s->state = s->s3->tmp.next_state;
589ebfedea0SLionel Sambuc                 }
590ebfedea0SLionel Sambuc 
591ebfedea0SLionel Sambuc                 ret = -1;
592ebfedea0SLionel Sambuc                 goto end;
593ebfedea0SLionel Sambuc             }
594ebfedea0SLionel Sambuc             s->rwstate = SSL_NOTHING;
595ebfedea0SLionel Sambuc             s->state = s->s3->tmp.next_state;
596ebfedea0SLionel Sambuc             break;
597ebfedea0SLionel Sambuc 
598ebfedea0SLionel Sambuc         case SSL3_ST_SR_CERT_A:
599ebfedea0SLionel Sambuc         case SSL3_ST_SR_CERT_B:
600ebfedea0SLionel Sambuc             /* Check for second client hello (MS SGC) */
601ebfedea0SLionel Sambuc             ret = ssl3_check_client_hello(s);
602ebfedea0SLionel Sambuc             if (ret <= 0)
603ebfedea0SLionel Sambuc                 goto end;
604*0a6a1f1dSLionel Sambuc             if (ret == 2) {
605ebfedea0SLionel Sambuc                 dtls1_stop_timer(s);
606ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SR_CLNT_HELLO_C;
607*0a6a1f1dSLionel Sambuc             } else {
608*0a6a1f1dSLionel Sambuc                 if (s->s3->tmp.cert_request) {
609ebfedea0SLionel Sambuc                     ret = ssl3_get_client_certificate(s);
610*0a6a1f1dSLionel Sambuc                     if (ret <= 0)
611*0a6a1f1dSLionel Sambuc                         goto end;
612*0a6a1f1dSLionel Sambuc                 }
613ebfedea0SLionel Sambuc                 s->init_num = 0;
614ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SR_KEY_EXCH_A;
615ebfedea0SLionel Sambuc             }
616ebfedea0SLionel Sambuc             break;
617ebfedea0SLionel Sambuc 
618ebfedea0SLionel Sambuc         case SSL3_ST_SR_KEY_EXCH_A:
619ebfedea0SLionel Sambuc         case SSL3_ST_SR_KEY_EXCH_B:
620ebfedea0SLionel Sambuc             ret = ssl3_get_client_key_exchange(s);
621*0a6a1f1dSLionel Sambuc             if (ret <= 0)
622*0a6a1f1dSLionel Sambuc                 goto end;
623ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
624*0a6a1f1dSLionel Sambuc             /*
625*0a6a1f1dSLionel Sambuc              * Add new shared key for SCTP-Auth, will be ignored if no SCTP
626*0a6a1f1dSLionel Sambuc              * used.
627ebfedea0SLionel Sambuc              */
628ebfedea0SLionel Sambuc             snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
629ebfedea0SLionel Sambuc                      DTLS1_SCTP_AUTH_LABEL);
630ebfedea0SLionel Sambuc 
631ebfedea0SLionel Sambuc             SSL_export_keying_material(s, sctpauthkey,
632ebfedea0SLionel Sambuc                                        sizeof(sctpauthkey), labelbuffer,
633ebfedea0SLionel Sambuc                                        sizeof(labelbuffer), NULL, 0, 0);
634ebfedea0SLionel Sambuc 
635ebfedea0SLionel Sambuc             BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
636ebfedea0SLionel Sambuc                      sizeof(sctpauthkey), sctpauthkey);
637ebfedea0SLionel Sambuc #endif
638ebfedea0SLionel Sambuc 
639ebfedea0SLionel Sambuc             s->state = SSL3_ST_SR_CERT_VRFY_A;
640ebfedea0SLionel Sambuc             s->init_num = 0;
641ebfedea0SLionel Sambuc 
642*0a6a1f1dSLionel Sambuc             if (ret == 2) {
643*0a6a1f1dSLionel Sambuc                 /*
644*0a6a1f1dSLionel Sambuc                  * For the ECDH ciphersuites when the client sends its ECDH
645*0a6a1f1dSLionel Sambuc                  * pub key in a certificate, the CertificateVerify message is
646*0a6a1f1dSLionel Sambuc                  * not sent.
647ebfedea0SLionel Sambuc                  */
648ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SR_FINISHED_A;
649ebfedea0SLionel Sambuc                 s->init_num = 0;
650*0a6a1f1dSLionel Sambuc             } else {
651ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SR_CERT_VRFY_A;
652ebfedea0SLionel Sambuc                 s->init_num = 0;
653ebfedea0SLionel Sambuc 
654*0a6a1f1dSLionel Sambuc                 /*
655*0a6a1f1dSLionel Sambuc                  * We need to get hashes here so if there is a client cert,
656*0a6a1f1dSLionel Sambuc                  * it can be verified
657*0a6a1f1dSLionel Sambuc                  */
658ebfedea0SLionel Sambuc                 s->method->ssl3_enc->cert_verify_mac(s,
659ebfedea0SLionel Sambuc                                                      NID_md5,
660*0a6a1f1dSLionel Sambuc                                                      &(s->s3->
661*0a6a1f1dSLionel Sambuc                                                        tmp.cert_verify_md
662*0a6a1f1dSLionel Sambuc                                                        [0]));
663*0a6a1f1dSLionel Sambuc                 s->method->ssl3_enc->cert_verify_mac(s, NID_sha1,
664*0a6a1f1dSLionel Sambuc                                                      &(s->s3->
665*0a6a1f1dSLionel Sambuc                                                        tmp.cert_verify_md
666*0a6a1f1dSLionel Sambuc                                                        [MD5_DIGEST_LENGTH]));
667ebfedea0SLionel Sambuc             }
668ebfedea0SLionel Sambuc             break;
669ebfedea0SLionel Sambuc 
670ebfedea0SLionel Sambuc         case SSL3_ST_SR_CERT_VRFY_A:
671ebfedea0SLionel Sambuc         case SSL3_ST_SR_CERT_VRFY_B:
672ebfedea0SLionel Sambuc             ret = ssl3_get_cert_verify(s);
673*0a6a1f1dSLionel Sambuc             if (ret <= 0)
674*0a6a1f1dSLionel Sambuc                 goto end;
675ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
676ebfedea0SLionel Sambuc             if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
677ebfedea0SLionel Sambuc                 state == SSL_ST_RENEGOTIATE)
678ebfedea0SLionel Sambuc                 s->state = DTLS1_SCTP_ST_SR_READ_SOCK;
679ebfedea0SLionel Sambuc             else
680ebfedea0SLionel Sambuc #endif
681ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SR_FINISHED_A;
682ebfedea0SLionel Sambuc             s->init_num = 0;
683ebfedea0SLionel Sambuc             break;
684ebfedea0SLionel Sambuc 
685ebfedea0SLionel Sambuc         case SSL3_ST_SR_FINISHED_A:
686ebfedea0SLionel Sambuc         case SSL3_ST_SR_FINISHED_B:
687*0a6a1f1dSLionel Sambuc             /*
688*0a6a1f1dSLionel Sambuc              * Enable CCS. Receiving a CCS clears the flag, so make
689*0a6a1f1dSLionel Sambuc              * sure not to re-enable it to ban duplicates. This *should* be the
690*0a6a1f1dSLionel Sambuc              * first time we have received one - but we check anyway to be
691*0a6a1f1dSLionel Sambuc              * cautious.
692*0a6a1f1dSLionel Sambuc              * s->s3->change_cipher_spec is set when a CCS is
693*0a6a1f1dSLionel Sambuc              * processed in d1_pkt.c, and remains set until
694*0a6a1f1dSLionel Sambuc              * the client's Finished message is read.
695*0a6a1f1dSLionel Sambuc              */
696*0a6a1f1dSLionel Sambuc             if (!s->s3->change_cipher_spec)
697ebfedea0SLionel Sambuc                 s->d1->change_cipher_spec_ok = 1;
698ebfedea0SLionel Sambuc             ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
699ebfedea0SLionel Sambuc                                     SSL3_ST_SR_FINISHED_B);
700*0a6a1f1dSLionel Sambuc             if (ret <= 0)
701*0a6a1f1dSLionel Sambuc                 goto end;
702ebfedea0SLionel Sambuc             dtls1_stop_timer(s);
703ebfedea0SLionel Sambuc             if (s->hit)
704ebfedea0SLionel Sambuc                 s->state = SSL_ST_OK;
705ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
706ebfedea0SLionel Sambuc             else if (s->tlsext_ticket_expected)
707ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SW_SESSION_TICKET_A;
708ebfedea0SLionel Sambuc #endif
709ebfedea0SLionel Sambuc             else
710ebfedea0SLionel Sambuc                 s->state = SSL3_ST_SW_CHANGE_A;
711ebfedea0SLionel Sambuc             s->init_num = 0;
712ebfedea0SLionel Sambuc             break;
713ebfedea0SLionel Sambuc 
714ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
715ebfedea0SLionel Sambuc         case SSL3_ST_SW_SESSION_TICKET_A:
716ebfedea0SLionel Sambuc         case SSL3_ST_SW_SESSION_TICKET_B:
717ebfedea0SLionel Sambuc             ret = dtls1_send_newsession_ticket(s);
718*0a6a1f1dSLionel Sambuc             if (ret <= 0)
719*0a6a1f1dSLionel Sambuc                 goto end;
720ebfedea0SLionel Sambuc             s->state = SSL3_ST_SW_CHANGE_A;
721ebfedea0SLionel Sambuc             s->init_num = 0;
722ebfedea0SLionel Sambuc             break;
723ebfedea0SLionel Sambuc 
724ebfedea0SLionel Sambuc         case SSL3_ST_SW_CERT_STATUS_A:
725ebfedea0SLionel Sambuc         case SSL3_ST_SW_CERT_STATUS_B:
726ebfedea0SLionel Sambuc             ret = ssl3_send_cert_status(s);
727*0a6a1f1dSLionel Sambuc             if (ret <= 0)
728*0a6a1f1dSLionel Sambuc                 goto end;
729ebfedea0SLionel Sambuc             s->state = SSL3_ST_SW_KEY_EXCH_A;
730ebfedea0SLionel Sambuc             s->init_num = 0;
731ebfedea0SLionel Sambuc             break;
732ebfedea0SLionel Sambuc 
733ebfedea0SLionel Sambuc #endif
734ebfedea0SLionel Sambuc 
735ebfedea0SLionel Sambuc         case SSL3_ST_SW_CHANGE_A:
736ebfedea0SLionel Sambuc         case SSL3_ST_SW_CHANGE_B:
737ebfedea0SLionel Sambuc 
738ebfedea0SLionel Sambuc             s->session->cipher = s->s3->tmp.new_cipher;
739*0a6a1f1dSLionel Sambuc             if (!s->method->ssl3_enc->setup_key_block(s)) {
740*0a6a1f1dSLionel Sambuc                 ret = -1;
741*0a6a1f1dSLionel Sambuc                 s->state = SSL_ST_ERR;
742*0a6a1f1dSLionel Sambuc                 goto end;
743*0a6a1f1dSLionel Sambuc             }
744ebfedea0SLionel Sambuc 
745ebfedea0SLionel Sambuc             ret = dtls1_send_change_cipher_spec(s,
746*0a6a1f1dSLionel Sambuc                                                 SSL3_ST_SW_CHANGE_A,
747*0a6a1f1dSLionel Sambuc                                                 SSL3_ST_SW_CHANGE_B);
748ebfedea0SLionel Sambuc 
749*0a6a1f1dSLionel Sambuc             if (ret <= 0)
750*0a6a1f1dSLionel Sambuc                 goto end;
751ebfedea0SLionel Sambuc 
752ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
753*0a6a1f1dSLionel Sambuc             if (!s->hit) {
754*0a6a1f1dSLionel Sambuc                 /*
755*0a6a1f1dSLionel Sambuc                  * Change to new shared key of SCTP-Auth, will be ignored if
756*0a6a1f1dSLionel Sambuc                  * no SCTP used.
757ebfedea0SLionel Sambuc                  */
758*0a6a1f1dSLionel Sambuc                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
759*0a6a1f1dSLionel Sambuc                          0, NULL);
760*0a6a1f1dSLionel Sambuc             }
761ebfedea0SLionel Sambuc #endif
762ebfedea0SLionel Sambuc 
763ebfedea0SLionel Sambuc             s->state = SSL3_ST_SW_FINISHED_A;
764ebfedea0SLionel Sambuc             s->init_num = 0;
765ebfedea0SLionel Sambuc 
766ebfedea0SLionel Sambuc             if (!s->method->ssl3_enc->change_cipher_state(s,
767ebfedea0SLionel Sambuc                                                           SSL3_CHANGE_CIPHER_SERVER_WRITE))
768ebfedea0SLionel Sambuc             {
769ebfedea0SLionel Sambuc                 ret = -1;
770*0a6a1f1dSLionel Sambuc                 s->state = SSL_ST_ERR;
771ebfedea0SLionel Sambuc                 goto end;
772ebfedea0SLionel Sambuc             }
773ebfedea0SLionel Sambuc 
774ebfedea0SLionel Sambuc             dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
775ebfedea0SLionel Sambuc             break;
776ebfedea0SLionel Sambuc 
777ebfedea0SLionel Sambuc         case SSL3_ST_SW_FINISHED_A:
778ebfedea0SLionel Sambuc         case SSL3_ST_SW_FINISHED_B:
779ebfedea0SLionel Sambuc             ret = dtls1_send_finished(s,
780*0a6a1f1dSLionel Sambuc                                       SSL3_ST_SW_FINISHED_A,
781*0a6a1f1dSLionel Sambuc                                       SSL3_ST_SW_FINISHED_B,
782*0a6a1f1dSLionel Sambuc                                       s->method->
783*0a6a1f1dSLionel Sambuc                                       ssl3_enc->server_finished_label,
784*0a6a1f1dSLionel Sambuc                                       s->method->
785*0a6a1f1dSLionel Sambuc                                       ssl3_enc->server_finished_label_len);
786*0a6a1f1dSLionel Sambuc             if (ret <= 0)
787*0a6a1f1dSLionel Sambuc                 goto end;
788ebfedea0SLionel Sambuc             s->state = SSL3_ST_SW_FLUSH;
789*0a6a1f1dSLionel Sambuc             if (s->hit) {
790ebfedea0SLionel Sambuc                 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
791*0a6a1f1dSLionel Sambuc 
792*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_SCTP
793*0a6a1f1dSLionel Sambuc                 /*
794*0a6a1f1dSLionel Sambuc                  * Change to new shared key of SCTP-Auth, will be ignored if
795*0a6a1f1dSLionel Sambuc                  * no SCTP used.
796*0a6a1f1dSLionel Sambuc                  */
797*0a6a1f1dSLionel Sambuc                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
798*0a6a1f1dSLionel Sambuc                          0, NULL);
799*0a6a1f1dSLionel Sambuc #endif
800*0a6a1f1dSLionel Sambuc             } else {
801ebfedea0SLionel Sambuc                 s->s3->tmp.next_state = SSL_ST_OK;
802ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
803*0a6a1f1dSLionel Sambuc                 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
804ebfedea0SLionel Sambuc                     s->d1->next_state = s->s3->tmp.next_state;
805ebfedea0SLionel Sambuc                     s->s3->tmp.next_state = DTLS1_SCTP_ST_SW_WRITE_SOCK;
806ebfedea0SLionel Sambuc                 }
807ebfedea0SLionel Sambuc #endif
808ebfedea0SLionel Sambuc             }
809ebfedea0SLionel Sambuc             s->init_num = 0;
810ebfedea0SLionel Sambuc             break;
811ebfedea0SLionel Sambuc 
812ebfedea0SLionel Sambuc         case SSL_ST_OK:
813ebfedea0SLionel Sambuc             /* clean a few things up */
814ebfedea0SLionel Sambuc             ssl3_cleanup_key_block(s);
815ebfedea0SLionel Sambuc 
816ebfedea0SLionel Sambuc #if 0
817ebfedea0SLionel Sambuc             BUF_MEM_free(s->init_buf);
818ebfedea0SLionel Sambuc             s->init_buf = NULL;
819ebfedea0SLionel Sambuc #endif
820ebfedea0SLionel Sambuc 
821ebfedea0SLionel Sambuc             /* remove buffering on output */
822ebfedea0SLionel Sambuc             ssl_free_wbio_buffer(s);
823ebfedea0SLionel Sambuc 
824ebfedea0SLionel Sambuc             s->init_num = 0;
825ebfedea0SLionel Sambuc 
826*0a6a1f1dSLionel Sambuc             if (s->renegotiate == 2) { /* skipped if we just sent a
827*0a6a1f1dSLionel Sambuc                                         * HelloRequest */
828ebfedea0SLionel Sambuc                 s->renegotiate = 0;
829ebfedea0SLionel Sambuc                 s->new_session = 0;
830ebfedea0SLionel Sambuc 
831ebfedea0SLionel Sambuc                 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
832ebfedea0SLionel Sambuc 
833ebfedea0SLionel Sambuc                 s->ctx->stats.sess_accept_good++;
834ebfedea0SLionel Sambuc                 /* s->server=1; */
835ebfedea0SLionel Sambuc                 s->handshake_func = dtls1_accept;
836ebfedea0SLionel Sambuc 
837*0a6a1f1dSLionel Sambuc                 if (cb != NULL)
838*0a6a1f1dSLionel Sambuc                     cb(s, SSL_CB_HANDSHAKE_DONE, 1);
839ebfedea0SLionel Sambuc             }
840ebfedea0SLionel Sambuc 
841ebfedea0SLionel Sambuc             ret = 1;
842ebfedea0SLionel Sambuc 
843ebfedea0SLionel Sambuc             /* done handshaking, next message is client hello */
844ebfedea0SLionel Sambuc             s->d1->handshake_read_seq = 0;
845ebfedea0SLionel Sambuc             /* next message is server hello */
846ebfedea0SLionel Sambuc             s->d1->handshake_write_seq = 0;
847ebfedea0SLionel Sambuc             s->d1->next_handshake_write_seq = 0;
848ebfedea0SLionel Sambuc             goto end;
849ebfedea0SLionel Sambuc             /* break; */
850ebfedea0SLionel Sambuc 
851*0a6a1f1dSLionel Sambuc         case SSL_ST_ERR:
852ebfedea0SLionel Sambuc         default:
853ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_UNKNOWN_STATE);
854ebfedea0SLionel Sambuc             ret = -1;
855ebfedea0SLionel Sambuc             goto end;
856ebfedea0SLionel Sambuc             /* break; */
857ebfedea0SLionel Sambuc         }
858ebfedea0SLionel Sambuc 
859*0a6a1f1dSLionel Sambuc         if (!s->s3->tmp.reuse_message && !skip) {
860*0a6a1f1dSLionel Sambuc             if (s->debug) {
861ebfedea0SLionel Sambuc                 if ((ret = BIO_flush(s->wbio)) <= 0)
862ebfedea0SLionel Sambuc                     goto end;
863ebfedea0SLionel Sambuc             }
864ebfedea0SLionel Sambuc 
865*0a6a1f1dSLionel Sambuc             if ((cb != NULL) && (s->state != state)) {
866ebfedea0SLionel Sambuc                 new_state = s->state;
867ebfedea0SLionel Sambuc                 s->state = state;
868ebfedea0SLionel Sambuc                 cb(s, SSL_CB_ACCEPT_LOOP, 1);
869ebfedea0SLionel Sambuc                 s->state = new_state;
870ebfedea0SLionel Sambuc             }
871ebfedea0SLionel Sambuc         }
872ebfedea0SLionel Sambuc         skip = 0;
873ebfedea0SLionel Sambuc     }
874ebfedea0SLionel Sambuc  end:
875ebfedea0SLionel Sambuc     /* BIO_flush(s->wbio); */
876ebfedea0SLionel Sambuc 
877ebfedea0SLionel Sambuc     s->in_handshake--;
878ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
879*0a6a1f1dSLionel Sambuc     /*
880*0a6a1f1dSLionel Sambuc      * Notify SCTP BIO socket to leave handshake mode and prevent stream
881*0a6a1f1dSLionel Sambuc      * identifier other than 0. Will be ignored if no SCTP is used.
882ebfedea0SLionel Sambuc      */
883*0a6a1f1dSLionel Sambuc     BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
884*0a6a1f1dSLionel Sambuc              s->in_handshake, NULL);
885ebfedea0SLionel Sambuc #endif
886ebfedea0SLionel Sambuc 
887ebfedea0SLionel Sambuc     if (cb != NULL)
888ebfedea0SLionel Sambuc         cb(s, SSL_CB_ACCEPT_EXIT, ret);
889ebfedea0SLionel Sambuc     return (ret);
890ebfedea0SLionel Sambuc }
891ebfedea0SLionel Sambuc 
dtls1_send_hello_request(SSL * s)892ebfedea0SLionel Sambuc int dtls1_send_hello_request(SSL *s)
893ebfedea0SLionel Sambuc {
894ebfedea0SLionel Sambuc     unsigned char *p;
895ebfedea0SLionel Sambuc 
896*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
897ebfedea0SLionel Sambuc         p = (unsigned char *)s->init_buf->data;
898ebfedea0SLionel Sambuc         p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0);
899ebfedea0SLionel Sambuc 
900ebfedea0SLionel Sambuc         s->state = SSL3_ST_SW_HELLO_REQ_B;
901ebfedea0SLionel Sambuc         /* number of bytes to write */
902ebfedea0SLionel Sambuc         s->init_num = DTLS1_HM_HEADER_LENGTH;
903ebfedea0SLionel Sambuc         s->init_off = 0;
904ebfedea0SLionel Sambuc 
905*0a6a1f1dSLionel Sambuc         /*
906*0a6a1f1dSLionel Sambuc          * no need to buffer this message, since there are no retransmit
907*0a6a1f1dSLionel Sambuc          * requests for it
908*0a6a1f1dSLionel Sambuc          */
909ebfedea0SLionel Sambuc     }
910ebfedea0SLionel Sambuc 
911ebfedea0SLionel Sambuc     /* SSL3_ST_SW_HELLO_REQ_B */
912ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
913ebfedea0SLionel Sambuc }
914ebfedea0SLionel Sambuc 
dtls1_send_hello_verify_request(SSL * s)915ebfedea0SLionel Sambuc int dtls1_send_hello_verify_request(SSL *s)
916ebfedea0SLionel Sambuc {
917ebfedea0SLionel Sambuc     unsigned int msg_len;
918ebfedea0SLionel Sambuc     unsigned char *msg, *buf, *p;
919ebfedea0SLionel Sambuc 
920*0a6a1f1dSLionel Sambuc     if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) {
921ebfedea0SLionel Sambuc         buf = (unsigned char *)s->init_buf->data;
922ebfedea0SLionel Sambuc 
923ebfedea0SLionel Sambuc         msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
924ebfedea0SLionel Sambuc         *(p++) = s->version >> 8;
925ebfedea0SLionel Sambuc         *(p++) = s->version & 0xFF;
926ebfedea0SLionel Sambuc 
927ebfedea0SLionel Sambuc         if (s->ctx->app_gen_cookie_cb == NULL ||
928ebfedea0SLionel Sambuc             s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
929*0a6a1f1dSLionel Sambuc                                       &(s->d1->cookie_len)) == 0) {
930*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,
931*0a6a1f1dSLionel Sambuc                    ERR_R_INTERNAL_ERROR);
932*0a6a1f1dSLionel Sambuc             s->state = SSL_ST_ERR;
933ebfedea0SLionel Sambuc             return 0;
934ebfedea0SLionel Sambuc         }
935ebfedea0SLionel Sambuc 
936ebfedea0SLionel Sambuc         *(p++) = (unsigned char)s->d1->cookie_len;
937ebfedea0SLionel Sambuc         memcpy(p, s->d1->cookie, s->d1->cookie_len);
938ebfedea0SLionel Sambuc         p += s->d1->cookie_len;
939ebfedea0SLionel Sambuc         msg_len = p - msg;
940ebfedea0SLionel Sambuc 
941ebfedea0SLionel Sambuc         dtls1_set_message_header(s, buf,
942*0a6a1f1dSLionel Sambuc                                  DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0,
943*0a6a1f1dSLionel Sambuc                                  msg_len);
944ebfedea0SLionel Sambuc 
945ebfedea0SLionel Sambuc         s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
946ebfedea0SLionel Sambuc         /* number of bytes to write */
947ebfedea0SLionel Sambuc         s->init_num = p - buf;
948ebfedea0SLionel Sambuc         s->init_off = 0;
949ebfedea0SLionel Sambuc     }
950ebfedea0SLionel Sambuc 
951ebfedea0SLionel Sambuc     /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
952ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
953ebfedea0SLionel Sambuc }
954ebfedea0SLionel Sambuc 
dtls1_send_server_hello(SSL * s)955ebfedea0SLionel Sambuc int dtls1_send_server_hello(SSL *s)
956ebfedea0SLionel Sambuc {
957ebfedea0SLionel Sambuc     unsigned char *buf;
958ebfedea0SLionel Sambuc     unsigned char *p, *d;
959ebfedea0SLionel Sambuc     int i;
960ebfedea0SLionel Sambuc     unsigned int sl;
961*0a6a1f1dSLionel Sambuc     unsigned long l;
962ebfedea0SLionel Sambuc 
963*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
964ebfedea0SLionel Sambuc         buf = (unsigned char *)s->init_buf->data;
965ebfedea0SLionel Sambuc         p = s->s3->server_random;
966*0a6a1f1dSLionel Sambuc         ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE);
967ebfedea0SLionel Sambuc         /* Do the message type and length last */
968ebfedea0SLionel Sambuc         d = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
969ebfedea0SLionel Sambuc 
970ebfedea0SLionel Sambuc         *(p++) = s->version >> 8;
971ebfedea0SLionel Sambuc         *(p++) = s->version & 0xff;
972ebfedea0SLionel Sambuc 
973ebfedea0SLionel Sambuc         /* Random stuff */
974ebfedea0SLionel Sambuc         memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
975ebfedea0SLionel Sambuc         p += SSL3_RANDOM_SIZE;
976ebfedea0SLionel Sambuc 
977*0a6a1f1dSLionel Sambuc         /*
978*0a6a1f1dSLionel Sambuc          * now in theory we have 3 options to sending back the session id.
979*0a6a1f1dSLionel Sambuc          * If it is a re-use, we send back the old session-id, if it is a new
980*0a6a1f1dSLionel Sambuc          * session, we send back the new session-id or we send back a 0
981*0a6a1f1dSLionel Sambuc          * length session-id if we want it to be single use. Currently I will
982*0a6a1f1dSLionel Sambuc          * not implement the '0' length session-id 12-Jan-98 - I'll now
983*0a6a1f1dSLionel Sambuc          * support the '0' length stuff.
984ebfedea0SLionel Sambuc          */
985ebfedea0SLionel Sambuc         if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
986ebfedea0SLionel Sambuc             s->session->session_id_length = 0;
987ebfedea0SLionel Sambuc 
988ebfedea0SLionel Sambuc         sl = s->session->session_id_length;
989*0a6a1f1dSLionel Sambuc         if (sl > sizeof s->session->session_id) {
990ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
991ebfedea0SLionel Sambuc             return -1;
992ebfedea0SLionel Sambuc         }
993ebfedea0SLionel Sambuc         *(p++) = sl;
994ebfedea0SLionel Sambuc         memcpy(p, s->session->session_id, sl);
995ebfedea0SLionel Sambuc         p += sl;
996ebfedea0SLionel Sambuc 
997ebfedea0SLionel Sambuc         /* put the cipher */
998ebfedea0SLionel Sambuc         if (s->s3->tmp.new_cipher == NULL)
999ebfedea0SLionel Sambuc             return -1;
1000ebfedea0SLionel Sambuc         i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1001ebfedea0SLionel Sambuc         p += i;
1002ebfedea0SLionel Sambuc 
1003ebfedea0SLionel Sambuc         /* put the compression method */
1004ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_COMP
1005ebfedea0SLionel Sambuc         *(p++) = 0;
1006ebfedea0SLionel Sambuc #else
1007ebfedea0SLionel Sambuc         if (s->s3->tmp.new_compression == NULL)
1008ebfedea0SLionel Sambuc             *(p++) = 0;
1009ebfedea0SLionel Sambuc         else
1010ebfedea0SLionel Sambuc             *(p++) = s->s3->tmp.new_compression->id;
1011ebfedea0SLionel Sambuc #endif
1012ebfedea0SLionel Sambuc 
1013ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
1014*0a6a1f1dSLionel Sambuc         if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1015*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1016*0a6a1f1dSLionel Sambuc             return -1;
1017*0a6a1f1dSLionel Sambuc         }
1018*0a6a1f1dSLionel Sambuc         if ((p =
1019*0a6a1f1dSLionel Sambuc              ssl_add_serverhello_tlsext(s, p,
1020*0a6a1f1dSLionel Sambuc                                         buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
1021*0a6a1f1dSLionel Sambuc             NULL) {
1022ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1023ebfedea0SLionel Sambuc             return -1;
1024ebfedea0SLionel Sambuc         }
1025ebfedea0SLionel Sambuc #endif
1026ebfedea0SLionel Sambuc 
1027ebfedea0SLionel Sambuc         /* do the header */
1028ebfedea0SLionel Sambuc         l = (p - d);
1029ebfedea0SLionel Sambuc         d = buf;
1030ebfedea0SLionel Sambuc 
1031ebfedea0SLionel Sambuc         d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l);
1032ebfedea0SLionel Sambuc 
1033ebfedea0SLionel Sambuc         s->state = SSL3_ST_SW_SRVR_HELLO_B;
1034ebfedea0SLionel Sambuc         /* number of bytes to write */
1035ebfedea0SLionel Sambuc         s->init_num = p - buf;
1036ebfedea0SLionel Sambuc         s->init_off = 0;
1037ebfedea0SLionel Sambuc 
1038ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1039ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
1040ebfedea0SLionel Sambuc     }
1041ebfedea0SLionel Sambuc 
1042ebfedea0SLionel Sambuc     /* SSL3_ST_SW_SRVR_HELLO_B */
1043ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1044ebfedea0SLionel Sambuc }
1045ebfedea0SLionel Sambuc 
dtls1_send_server_done(SSL * s)1046ebfedea0SLionel Sambuc int dtls1_send_server_done(SSL *s)
1047ebfedea0SLionel Sambuc {
1048ebfedea0SLionel Sambuc     unsigned char *p;
1049ebfedea0SLionel Sambuc 
1050*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1051ebfedea0SLionel Sambuc         p = (unsigned char *)s->init_buf->data;
1052ebfedea0SLionel Sambuc 
1053ebfedea0SLionel Sambuc         /* do the header */
1054ebfedea0SLionel Sambuc         p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0);
1055ebfedea0SLionel Sambuc 
1056ebfedea0SLionel Sambuc         s->state = SSL3_ST_SW_SRVR_DONE_B;
1057ebfedea0SLionel Sambuc         /* number of bytes to write */
1058ebfedea0SLionel Sambuc         s->init_num = DTLS1_HM_HEADER_LENGTH;
1059ebfedea0SLionel Sambuc         s->init_off = 0;
1060ebfedea0SLionel Sambuc 
1061ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1062ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
1063ebfedea0SLionel Sambuc     }
1064ebfedea0SLionel Sambuc 
1065ebfedea0SLionel Sambuc     /* SSL3_ST_SW_SRVR_DONE_B */
1066ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1067ebfedea0SLionel Sambuc }
1068ebfedea0SLionel Sambuc 
dtls1_send_server_key_exchange(SSL * s)1069ebfedea0SLionel Sambuc int dtls1_send_server_key_exchange(SSL *s)
1070ebfedea0SLionel Sambuc {
1071ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1072ebfedea0SLionel Sambuc     unsigned char *q;
1073ebfedea0SLionel Sambuc     int j, num;
1074ebfedea0SLionel Sambuc     RSA *rsa;
1075ebfedea0SLionel Sambuc     unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1076ebfedea0SLionel Sambuc     unsigned int u;
1077ebfedea0SLionel Sambuc #endif
1078ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
1079ebfedea0SLionel Sambuc     DH *dh = NULL, *dhp;
1080ebfedea0SLionel Sambuc #endif
1081ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1082ebfedea0SLionel Sambuc     EC_KEY *ecdh = NULL, *ecdhp;
1083ebfedea0SLionel Sambuc     unsigned char *encodedPoint = NULL;
1084ebfedea0SLionel Sambuc     int encodedlen = 0;
1085ebfedea0SLionel Sambuc     int curve_id = 0;
1086ebfedea0SLionel Sambuc     BN_CTX *bn_ctx = NULL;
1087ebfedea0SLionel Sambuc #endif
1088ebfedea0SLionel Sambuc     EVP_PKEY *pkey;
1089ebfedea0SLionel Sambuc     unsigned char *p, *d;
1090ebfedea0SLionel Sambuc     int al, i;
1091ebfedea0SLionel Sambuc     unsigned long type;
1092ebfedea0SLionel Sambuc     int n;
1093ebfedea0SLionel Sambuc     CERT *cert;
1094ebfedea0SLionel Sambuc     BIGNUM *r[4];
1095ebfedea0SLionel Sambuc     int nr[4], kn;
1096ebfedea0SLionel Sambuc     BUF_MEM *buf;
1097ebfedea0SLionel Sambuc     EVP_MD_CTX md_ctx;
1098ebfedea0SLionel Sambuc 
1099ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&md_ctx);
1100*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1101ebfedea0SLionel Sambuc         type = s->s3->tmp.new_cipher->algorithm_mkey;
1102ebfedea0SLionel Sambuc         cert = s->cert;
1103ebfedea0SLionel Sambuc 
1104ebfedea0SLionel Sambuc         buf = s->init_buf;
1105ebfedea0SLionel Sambuc 
1106ebfedea0SLionel Sambuc         r[0] = r[1] = r[2] = r[3] = NULL;
1107ebfedea0SLionel Sambuc         n = 0;
1108ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1109*0a6a1f1dSLionel Sambuc         if (type & SSL_kRSA) {
1110ebfedea0SLionel Sambuc             rsa = cert->rsa_tmp;
1111*0a6a1f1dSLionel Sambuc             if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1112ebfedea0SLionel Sambuc                 rsa = s->cert->rsa_tmp_cb(s,
1113*0a6a1f1dSLionel Sambuc                                           SSL_C_IS_EXPORT(s->s3->
1114*0a6a1f1dSLionel Sambuc                                                           tmp.new_cipher),
1115*0a6a1f1dSLionel Sambuc                                           SSL_C_EXPORT_PKEYLENGTH(s->s3->
1116*0a6a1f1dSLionel Sambuc                                                                   tmp.new_cipher));
1117*0a6a1f1dSLionel Sambuc                 if (rsa == NULL) {
1118ebfedea0SLionel Sambuc                     al = SSL_AD_HANDSHAKE_FAILURE;
1119*0a6a1f1dSLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1120*0a6a1f1dSLionel Sambuc                            SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1121ebfedea0SLionel Sambuc                     goto f_err;
1122ebfedea0SLionel Sambuc                 }
1123ebfedea0SLionel Sambuc                 RSA_up_ref(rsa);
1124ebfedea0SLionel Sambuc                 cert->rsa_tmp = rsa;
1125ebfedea0SLionel Sambuc             }
1126*0a6a1f1dSLionel Sambuc             if (rsa == NULL) {
1127ebfedea0SLionel Sambuc                 al = SSL_AD_HANDSHAKE_FAILURE;
1128*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1129*0a6a1f1dSLionel Sambuc                        SSL_R_MISSING_TMP_RSA_KEY);
1130ebfedea0SLionel Sambuc                 goto f_err;
1131ebfedea0SLionel Sambuc             }
1132ebfedea0SLionel Sambuc             r[0] = rsa->n;
1133ebfedea0SLionel Sambuc             r[1] = rsa->e;
1134ebfedea0SLionel Sambuc             s->s3->tmp.use_rsa_tmp = 1;
1135*0a6a1f1dSLionel Sambuc         } else
1136ebfedea0SLionel Sambuc #endif
1137ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
1138*0a6a1f1dSLionel Sambuc         if (type & SSL_kEDH) {
1139ebfedea0SLionel Sambuc             dhp = cert->dh_tmp;
1140ebfedea0SLionel Sambuc             if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1141ebfedea0SLionel Sambuc                 dhp = s->cert->dh_tmp_cb(s,
1142*0a6a1f1dSLionel Sambuc                                          SSL_C_IS_EXPORT(s->s3->
1143*0a6a1f1dSLionel Sambuc                                                          tmp.new_cipher),
1144*0a6a1f1dSLionel Sambuc                                          SSL_C_EXPORT_PKEYLENGTH(s->s3->
1145*0a6a1f1dSLionel Sambuc                                                                  tmp.new_cipher));
1146*0a6a1f1dSLionel Sambuc             if (dhp == NULL) {
1147ebfedea0SLionel Sambuc                 al = SSL_AD_HANDSHAKE_FAILURE;
1148*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1149*0a6a1f1dSLionel Sambuc                        SSL_R_MISSING_TMP_DH_KEY);
1150ebfedea0SLionel Sambuc                 goto f_err;
1151ebfedea0SLionel Sambuc             }
1152ebfedea0SLionel Sambuc 
1153*0a6a1f1dSLionel Sambuc             if (s->s3->tmp.dh != NULL) {
1154ebfedea0SLionel Sambuc                 DH_free(dh);
1155*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1156*0a6a1f1dSLionel Sambuc                        ERR_R_INTERNAL_ERROR);
1157ebfedea0SLionel Sambuc                 goto err;
1158ebfedea0SLionel Sambuc             }
1159ebfedea0SLionel Sambuc 
1160*0a6a1f1dSLionel Sambuc             if ((dh = DHparams_dup(dhp)) == NULL) {
1161ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1162ebfedea0SLionel Sambuc                 goto err;
1163ebfedea0SLionel Sambuc             }
1164ebfedea0SLionel Sambuc 
1165ebfedea0SLionel Sambuc             s->s3->tmp.dh = dh;
1166ebfedea0SLionel Sambuc             if ((dhp->pub_key == NULL ||
1167ebfedea0SLionel Sambuc                  dhp->priv_key == NULL ||
1168*0a6a1f1dSLionel Sambuc                  (s->options & SSL_OP_SINGLE_DH_USE))) {
1169*0a6a1f1dSLionel Sambuc                 if (!DH_generate_key(dh)) {
1170ebfedea0SLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1171ebfedea0SLionel Sambuc                            ERR_R_DH_LIB);
1172ebfedea0SLionel Sambuc                     goto err;
1173ebfedea0SLionel Sambuc                 }
1174*0a6a1f1dSLionel Sambuc             } else {
1175ebfedea0SLionel Sambuc                 dh->pub_key = BN_dup(dhp->pub_key);
1176ebfedea0SLionel Sambuc                 dh->priv_key = BN_dup(dhp->priv_key);
1177*0a6a1f1dSLionel Sambuc                 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1178*0a6a1f1dSLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1179*0a6a1f1dSLionel Sambuc                            ERR_R_DH_LIB);
1180ebfedea0SLionel Sambuc                     goto err;
1181ebfedea0SLionel Sambuc                 }
1182ebfedea0SLionel Sambuc             }
1183ebfedea0SLionel Sambuc             r[0] = dh->p;
1184ebfedea0SLionel Sambuc             r[1] = dh->g;
1185ebfedea0SLionel Sambuc             r[2] = dh->pub_key;
1186*0a6a1f1dSLionel Sambuc         } else
1187ebfedea0SLionel Sambuc #endif
1188ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1189*0a6a1f1dSLionel Sambuc         if (type & SSL_kEECDH) {
1190ebfedea0SLionel Sambuc             const EC_GROUP *group;
1191ebfedea0SLionel Sambuc 
1192ebfedea0SLionel Sambuc             ecdhp = cert->ecdh_tmp;
1193*0a6a1f1dSLionel Sambuc             if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) {
1194ebfedea0SLionel Sambuc                 ecdhp = s->cert->ecdh_tmp_cb(s,
1195*0a6a1f1dSLionel Sambuc                                              SSL_C_IS_EXPORT(s->s3->
1196*0a6a1f1dSLionel Sambuc                                                              tmp.new_cipher),
1197*0a6a1f1dSLionel Sambuc                                              SSL_C_EXPORT_PKEYLENGTH(s->
1198*0a6a1f1dSLionel Sambuc                                                                      s3->tmp.new_cipher));
1199ebfedea0SLionel Sambuc             }
1200*0a6a1f1dSLionel Sambuc             if (ecdhp == NULL) {
1201ebfedea0SLionel Sambuc                 al = SSL_AD_HANDSHAKE_FAILURE;
1202*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1203*0a6a1f1dSLionel Sambuc                        SSL_R_MISSING_TMP_ECDH_KEY);
1204ebfedea0SLionel Sambuc                 goto f_err;
1205ebfedea0SLionel Sambuc             }
1206ebfedea0SLionel Sambuc 
1207*0a6a1f1dSLionel Sambuc             if (s->s3->tmp.ecdh != NULL) {
1208ebfedea0SLionel Sambuc                 EC_KEY_free(s->s3->tmp.ecdh);
1209*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1210*0a6a1f1dSLionel Sambuc                        ERR_R_INTERNAL_ERROR);
1211ebfedea0SLionel Sambuc                 goto err;
1212ebfedea0SLionel Sambuc             }
1213ebfedea0SLionel Sambuc 
1214ebfedea0SLionel Sambuc             /* Duplicate the ECDH structure. */
1215*0a6a1f1dSLionel Sambuc             if (ecdhp == NULL) {
1216ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1217ebfedea0SLionel Sambuc                 goto err;
1218ebfedea0SLionel Sambuc             }
1219*0a6a1f1dSLionel Sambuc             if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1220ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1221ebfedea0SLionel Sambuc                 goto err;
1222ebfedea0SLionel Sambuc             }
1223ebfedea0SLionel Sambuc 
1224ebfedea0SLionel Sambuc             s->s3->tmp.ecdh = ecdh;
1225ebfedea0SLionel Sambuc             if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1226ebfedea0SLionel Sambuc                 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1227*0a6a1f1dSLionel Sambuc                 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1228*0a6a1f1dSLionel Sambuc                 if (!EC_KEY_generate_key(ecdh)) {
1229*0a6a1f1dSLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1230*0a6a1f1dSLionel Sambuc                            ERR_R_ECDH_LIB);
1231ebfedea0SLionel Sambuc                     goto err;
1232ebfedea0SLionel Sambuc                 }
1233ebfedea0SLionel Sambuc             }
1234ebfedea0SLionel Sambuc 
1235ebfedea0SLionel Sambuc             if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1236ebfedea0SLionel Sambuc                 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1237*0a6a1f1dSLionel Sambuc                 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1238ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1239ebfedea0SLionel Sambuc                 goto err;
1240ebfedea0SLionel Sambuc             }
1241ebfedea0SLionel Sambuc 
1242ebfedea0SLionel Sambuc             if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1243*0a6a1f1dSLionel Sambuc                 (EC_GROUP_get_degree(group) > 163)) {
1244*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1245*0a6a1f1dSLionel Sambuc                        SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1246ebfedea0SLionel Sambuc                 goto err;
1247ebfedea0SLionel Sambuc             }
1248ebfedea0SLionel Sambuc 
1249*0a6a1f1dSLionel Sambuc             /*
1250*0a6a1f1dSLionel Sambuc              * XXX: For now, we only support ephemeral ECDH keys over named
1251*0a6a1f1dSLionel Sambuc              * (not generic) curves. For supported named curves, curve_id is
1252*0a6a1f1dSLionel Sambuc              * non-zero.
1253ebfedea0SLionel Sambuc              */
1254ebfedea0SLionel Sambuc             if ((curve_id =
1255ebfedea0SLionel Sambuc                  tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1256*0a6a1f1dSLionel Sambuc                 == 0) {
1257*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1258*0a6a1f1dSLionel Sambuc                        SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1259ebfedea0SLionel Sambuc                 goto err;
1260ebfedea0SLionel Sambuc             }
1261ebfedea0SLionel Sambuc 
1262*0a6a1f1dSLionel Sambuc             /*
1263*0a6a1f1dSLionel Sambuc              * Encode the public key. First check the size of encoding and
1264ebfedea0SLionel Sambuc              * allocate memory accordingly.
1265ebfedea0SLionel Sambuc              */
1266ebfedea0SLionel Sambuc             encodedlen = EC_POINT_point2oct(group,
1267ebfedea0SLionel Sambuc                                             EC_KEY_get0_public_key(ecdh),
1268ebfedea0SLionel Sambuc                                             POINT_CONVERSION_UNCOMPRESSED,
1269ebfedea0SLionel Sambuc                                             NULL, 0, NULL);
1270ebfedea0SLionel Sambuc 
1271ebfedea0SLionel Sambuc             encodedPoint = (unsigned char *)
1272ebfedea0SLionel Sambuc                 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1273ebfedea0SLionel Sambuc             bn_ctx = BN_CTX_new();
1274*0a6a1f1dSLionel Sambuc             if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1275*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1276*0a6a1f1dSLionel Sambuc                        ERR_R_MALLOC_FAILURE);
1277ebfedea0SLionel Sambuc                 goto err;
1278ebfedea0SLionel Sambuc             }
1279ebfedea0SLionel Sambuc 
1280ebfedea0SLionel Sambuc             encodedlen = EC_POINT_point2oct(group,
1281ebfedea0SLionel Sambuc                                             EC_KEY_get0_public_key(ecdh),
1282ebfedea0SLionel Sambuc                                             POINT_CONVERSION_UNCOMPRESSED,
1283ebfedea0SLionel Sambuc                                             encodedPoint, encodedlen, bn_ctx);
1284ebfedea0SLionel Sambuc 
1285*0a6a1f1dSLionel Sambuc             if (encodedlen == 0) {
1286ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1287ebfedea0SLionel Sambuc                 goto err;
1288ebfedea0SLionel Sambuc             }
1289ebfedea0SLionel Sambuc 
1290*0a6a1f1dSLionel Sambuc             BN_CTX_free(bn_ctx);
1291*0a6a1f1dSLionel Sambuc             bn_ctx = NULL;
1292ebfedea0SLionel Sambuc 
1293*0a6a1f1dSLionel Sambuc             /*
1294*0a6a1f1dSLionel Sambuc              * XXX: For now, we only support named (not generic) curves in
1295*0a6a1f1dSLionel Sambuc              * ECDH ephemeral key exchanges. In this situation, we need four
1296*0a6a1f1dSLionel Sambuc              * additional bytes to encode the entire ServerECDHParams
1297ebfedea0SLionel Sambuc              * structure.
1298ebfedea0SLionel Sambuc              */
1299ebfedea0SLionel Sambuc             n = 4 + encodedlen;
1300ebfedea0SLionel Sambuc 
1301*0a6a1f1dSLionel Sambuc             /*
1302*0a6a1f1dSLionel Sambuc              * We'll generate the serverKeyExchange message explicitly so we
1303*0a6a1f1dSLionel Sambuc              * can set these to NULLs
1304ebfedea0SLionel Sambuc              */
1305ebfedea0SLionel Sambuc             r[0] = NULL;
1306ebfedea0SLionel Sambuc             r[1] = NULL;
1307ebfedea0SLionel Sambuc             r[2] = NULL;
1308ebfedea0SLionel Sambuc             r[3] = NULL;
1309*0a6a1f1dSLionel Sambuc         } else
1310ebfedea0SLionel Sambuc #endif                          /* !OPENSSL_NO_ECDH */
1311ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
1312*0a6a1f1dSLionel Sambuc         if (type & SSL_kPSK) {
1313*0a6a1f1dSLionel Sambuc             /*
1314*0a6a1f1dSLionel Sambuc              * reserve size for record length and PSK identity hint
1315*0a6a1f1dSLionel Sambuc              */
1316ebfedea0SLionel Sambuc             n += 2 + strlen(s->ctx->psk_identity_hint);
1317*0a6a1f1dSLionel Sambuc         } else
1318ebfedea0SLionel Sambuc #endif                          /* !OPENSSL_NO_PSK */
1319ebfedea0SLionel Sambuc         {
1320ebfedea0SLionel Sambuc             al = SSL_AD_HANDSHAKE_FAILURE;
1321*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1322*0a6a1f1dSLionel Sambuc                    SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1323ebfedea0SLionel Sambuc             goto f_err;
1324ebfedea0SLionel Sambuc         }
1325*0a6a1f1dSLionel Sambuc         for (i = 0; r[i] != NULL; i++) {
1326ebfedea0SLionel Sambuc             nr[i] = BN_num_bytes(r[i]);
1327ebfedea0SLionel Sambuc             n += 2 + nr[i];
1328ebfedea0SLionel Sambuc         }
1329ebfedea0SLionel Sambuc 
1330ebfedea0SLionel Sambuc         if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1331*0a6a1f1dSLionel Sambuc             && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
1332ebfedea0SLionel Sambuc             if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, NULL))
1333*0a6a1f1dSLionel Sambuc                 == NULL) {
1334ebfedea0SLionel Sambuc                 al = SSL_AD_DECODE_ERROR;
1335ebfedea0SLionel Sambuc                 goto f_err;
1336ebfedea0SLionel Sambuc             }
1337ebfedea0SLionel Sambuc             kn = EVP_PKEY_size(pkey);
1338*0a6a1f1dSLionel Sambuc         } else {
1339ebfedea0SLionel Sambuc             pkey = NULL;
1340ebfedea0SLionel Sambuc             kn = 0;
1341ebfedea0SLionel Sambuc         }
1342ebfedea0SLionel Sambuc 
1343*0a6a1f1dSLionel Sambuc         if (!BUF_MEM_grow_clean(buf, n + DTLS1_HM_HEADER_LENGTH + kn)) {
1344ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1345ebfedea0SLionel Sambuc             goto err;
1346ebfedea0SLionel Sambuc         }
1347ebfedea0SLionel Sambuc         d = (unsigned char *)s->init_buf->data;
1348ebfedea0SLionel Sambuc         p = &(d[DTLS1_HM_HEADER_LENGTH]);
1349ebfedea0SLionel Sambuc 
1350*0a6a1f1dSLionel Sambuc         for (i = 0; r[i] != NULL; i++) {
1351ebfedea0SLionel Sambuc             s2n(nr[i], p);
1352ebfedea0SLionel Sambuc             BN_bn2bin(r[i], p);
1353ebfedea0SLionel Sambuc             p += nr[i];
1354ebfedea0SLionel Sambuc         }
1355ebfedea0SLionel Sambuc 
1356ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1357*0a6a1f1dSLionel Sambuc         if (type & SSL_kEECDH) {
1358*0a6a1f1dSLionel Sambuc             /*
1359*0a6a1f1dSLionel Sambuc              * XXX: For now, we only support named (not generic) curves. In
1360*0a6a1f1dSLionel Sambuc              * this situation, the serverKeyExchange message has: [1 byte
1361*0a6a1f1dSLionel Sambuc              * CurveType], [2 byte CurveName] [1 byte length of encoded
1362*0a6a1f1dSLionel Sambuc              * point], followed by the actual encoded point itself
1363ebfedea0SLionel Sambuc              */
1364ebfedea0SLionel Sambuc             *p = NAMED_CURVE_TYPE;
1365ebfedea0SLionel Sambuc             p += 1;
1366ebfedea0SLionel Sambuc             *p = 0;
1367ebfedea0SLionel Sambuc             p += 1;
1368ebfedea0SLionel Sambuc             *p = curve_id;
1369ebfedea0SLionel Sambuc             p += 1;
1370ebfedea0SLionel Sambuc             *p = encodedlen;
1371ebfedea0SLionel Sambuc             p += 1;
1372ebfedea0SLionel Sambuc             memcpy((unsigned char *)p,
1373*0a6a1f1dSLionel Sambuc                    (unsigned char *)encodedPoint, encodedlen);
1374ebfedea0SLionel Sambuc             OPENSSL_free(encodedPoint);
1375*0a6a1f1dSLionel Sambuc             encodedPoint = NULL;
1376ebfedea0SLionel Sambuc             p += encodedlen;
1377ebfedea0SLionel Sambuc         }
1378ebfedea0SLionel Sambuc #endif
1379ebfedea0SLionel Sambuc 
1380ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
1381*0a6a1f1dSLionel Sambuc         if (type & SSL_kPSK) {
1382ebfedea0SLionel Sambuc             /* copy PSK identity hint */
1383ebfedea0SLionel Sambuc             s2n(strlen(s->ctx->psk_identity_hint), p);
1384*0a6a1f1dSLionel Sambuc             strncpy((char *)p, s->ctx->psk_identity_hint,
1385*0a6a1f1dSLionel Sambuc                     strlen(s->ctx->psk_identity_hint));
1386ebfedea0SLionel Sambuc             p += strlen(s->ctx->psk_identity_hint);
1387ebfedea0SLionel Sambuc         }
1388ebfedea0SLionel Sambuc #endif
1389ebfedea0SLionel Sambuc 
1390ebfedea0SLionel Sambuc         /* not anonymous */
1391*0a6a1f1dSLionel Sambuc         if (pkey != NULL) {
1392*0a6a1f1dSLionel Sambuc             /*
1393*0a6a1f1dSLionel Sambuc              * n is the length of the params, they start at
1394*0a6a1f1dSLionel Sambuc              * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space at the
1395*0a6a1f1dSLionel Sambuc              * end.
1396*0a6a1f1dSLionel Sambuc              */
1397ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1398*0a6a1f1dSLionel Sambuc             if (pkey->type == EVP_PKEY_RSA) {
1399ebfedea0SLionel Sambuc                 q = md_buf;
1400ebfedea0SLionel Sambuc                 j = 0;
1401*0a6a1f1dSLionel Sambuc                 for (num = 2; num > 0; num--) {
1402ebfedea0SLionel Sambuc                     EVP_DigestInit_ex(&md_ctx, (num == 2)
1403ebfedea0SLionel Sambuc                                       ? s->ctx->md5 : s->ctx->sha1, NULL);
1404*0a6a1f1dSLionel Sambuc                     EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1405*0a6a1f1dSLionel Sambuc                                      SSL3_RANDOM_SIZE);
1406*0a6a1f1dSLionel Sambuc                     EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1407*0a6a1f1dSLionel Sambuc                                      SSL3_RANDOM_SIZE);
1408*0a6a1f1dSLionel Sambuc                     EVP_DigestUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]),
1409*0a6a1f1dSLionel Sambuc                                      n);
1410*0a6a1f1dSLionel Sambuc                     EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1411ebfedea0SLionel Sambuc                     q += i;
1412ebfedea0SLionel Sambuc                     j += i;
1413ebfedea0SLionel Sambuc                 }
1414ebfedea0SLionel Sambuc                 if (RSA_sign(NID_md5_sha1, md_buf, j,
1415*0a6a1f1dSLionel Sambuc                              &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1416ebfedea0SLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
1417ebfedea0SLionel Sambuc                     goto err;
1418ebfedea0SLionel Sambuc                 }
1419ebfedea0SLionel Sambuc                 s2n(u, p);
1420ebfedea0SLionel Sambuc                 n += u + 2;
1421*0a6a1f1dSLionel Sambuc             } else
1422ebfedea0SLionel Sambuc #endif
1423ebfedea0SLionel Sambuc #if !defined(OPENSSL_NO_DSA)
1424*0a6a1f1dSLionel Sambuc             if (pkey->type == EVP_PKEY_DSA) {
1425ebfedea0SLionel Sambuc                 /* lets do DSS */
1426ebfedea0SLionel Sambuc                 EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL);
1427*0a6a1f1dSLionel Sambuc                 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1428*0a6a1f1dSLionel Sambuc                                SSL3_RANDOM_SIZE);
1429*0a6a1f1dSLionel Sambuc                 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1430*0a6a1f1dSLionel Sambuc                                SSL3_RANDOM_SIZE);
1431ebfedea0SLionel Sambuc                 EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n);
1432ebfedea0SLionel Sambuc                 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1433*0a6a1f1dSLionel Sambuc                                    (unsigned int *)&i, pkey)) {
1434ebfedea0SLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA);
1435ebfedea0SLionel Sambuc                     goto err;
1436ebfedea0SLionel Sambuc                 }
1437ebfedea0SLionel Sambuc                 s2n(i, p);
1438ebfedea0SLionel Sambuc                 n += i + 2;
1439*0a6a1f1dSLionel Sambuc             } else
1440ebfedea0SLionel Sambuc #endif
1441ebfedea0SLionel Sambuc #if !defined(OPENSSL_NO_ECDSA)
1442*0a6a1f1dSLionel Sambuc             if (pkey->type == EVP_PKEY_EC) {
1443ebfedea0SLionel Sambuc                 /* let's do ECDSA */
1444ebfedea0SLionel Sambuc                 EVP_SignInit_ex(&md_ctx, EVP_ecdsa(), NULL);
1445*0a6a1f1dSLionel Sambuc                 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1446*0a6a1f1dSLionel Sambuc                                SSL3_RANDOM_SIZE);
1447*0a6a1f1dSLionel Sambuc                 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1448*0a6a1f1dSLionel Sambuc                                SSL3_RANDOM_SIZE);
1449ebfedea0SLionel Sambuc                 EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n);
1450ebfedea0SLionel Sambuc                 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1451*0a6a1f1dSLionel Sambuc                                    (unsigned int *)&i, pkey)) {
1452*0a6a1f1dSLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1453*0a6a1f1dSLionel Sambuc                            ERR_LIB_ECDSA);
1454ebfedea0SLionel Sambuc                     goto err;
1455ebfedea0SLionel Sambuc                 }
1456ebfedea0SLionel Sambuc                 s2n(i, p);
1457ebfedea0SLionel Sambuc                 n += i + 2;
1458*0a6a1f1dSLionel Sambuc             } else
1459ebfedea0SLionel Sambuc #endif
1460ebfedea0SLionel Sambuc             {
1461ebfedea0SLionel Sambuc                 /* Is this error check actually needed? */
1462ebfedea0SLionel Sambuc                 al = SSL_AD_HANDSHAKE_FAILURE;
1463*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1464*0a6a1f1dSLionel Sambuc                        SSL_R_UNKNOWN_PKEY_TYPE);
1465ebfedea0SLionel Sambuc                 goto f_err;
1466ebfedea0SLionel Sambuc             }
1467ebfedea0SLionel Sambuc         }
1468ebfedea0SLionel Sambuc 
1469ebfedea0SLionel Sambuc         d = dtls1_set_message_header(s, d,
1470ebfedea0SLionel Sambuc                                      SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);
1471ebfedea0SLionel Sambuc 
1472*0a6a1f1dSLionel Sambuc         /*
1473*0a6a1f1dSLionel Sambuc          * we should now have things packed up, so lets send it off
1474*0a6a1f1dSLionel Sambuc          */
1475ebfedea0SLionel Sambuc         s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1476ebfedea0SLionel Sambuc         s->init_off = 0;
1477ebfedea0SLionel Sambuc 
1478ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1479ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
1480ebfedea0SLionel Sambuc     }
1481ebfedea0SLionel Sambuc 
1482ebfedea0SLionel Sambuc     s->state = SSL3_ST_SW_KEY_EXCH_B;
1483ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&md_ctx);
1484ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1485ebfedea0SLionel Sambuc  f_err:
1486ebfedea0SLionel Sambuc     ssl3_send_alert(s, SSL3_AL_FATAL, al);
1487ebfedea0SLionel Sambuc  err:
1488ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1489*0a6a1f1dSLionel Sambuc     if (encodedPoint != NULL)
1490*0a6a1f1dSLionel Sambuc         OPENSSL_free(encodedPoint);
1491ebfedea0SLionel Sambuc     BN_CTX_free(bn_ctx);
1492ebfedea0SLionel Sambuc #endif
1493ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&md_ctx);
1494ebfedea0SLionel Sambuc     return (-1);
1495ebfedea0SLionel Sambuc }
1496ebfedea0SLionel Sambuc 
dtls1_send_certificate_request(SSL * s)1497ebfedea0SLionel Sambuc int dtls1_send_certificate_request(SSL *s)
1498ebfedea0SLionel Sambuc {
1499ebfedea0SLionel Sambuc     unsigned char *p, *d;
1500ebfedea0SLionel Sambuc     int i, j, nl, off, n;
1501ebfedea0SLionel Sambuc     STACK_OF(X509_NAME) *sk = NULL;
1502ebfedea0SLionel Sambuc     X509_NAME *name;
1503ebfedea0SLionel Sambuc     BUF_MEM *buf;
1504ebfedea0SLionel Sambuc     unsigned int msg_len;
1505ebfedea0SLionel Sambuc 
1506*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1507ebfedea0SLionel Sambuc         buf = s->init_buf;
1508ebfedea0SLionel Sambuc 
1509ebfedea0SLionel Sambuc         d = p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
1510ebfedea0SLionel Sambuc 
1511ebfedea0SLionel Sambuc         /* get the list of acceptable cert types */
1512ebfedea0SLionel Sambuc         p++;
1513ebfedea0SLionel Sambuc         n = ssl3_get_req_cert_type(s, p);
1514ebfedea0SLionel Sambuc         d[0] = n;
1515ebfedea0SLionel Sambuc         p += n;
1516ebfedea0SLionel Sambuc         n++;
1517ebfedea0SLionel Sambuc 
1518ebfedea0SLionel Sambuc         off = n;
1519ebfedea0SLionel Sambuc         p += 2;
1520ebfedea0SLionel Sambuc         n += 2;
1521ebfedea0SLionel Sambuc 
1522ebfedea0SLionel Sambuc         sk = SSL_get_client_CA_list(s);
1523ebfedea0SLionel Sambuc         nl = 0;
1524*0a6a1f1dSLionel Sambuc         if (sk != NULL) {
1525*0a6a1f1dSLionel Sambuc             for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1526ebfedea0SLionel Sambuc                 name = sk_X509_NAME_value(sk, i);
1527ebfedea0SLionel Sambuc                 j = i2d_X509_NAME(name, NULL);
1528*0a6a1f1dSLionel Sambuc                 if (!BUF_MEM_grow_clean
1529*0a6a1f1dSLionel Sambuc                     (buf, DTLS1_HM_HEADER_LENGTH + n + j + 2)) {
1530*0a6a1f1dSLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,
1531*0a6a1f1dSLionel Sambuc                            ERR_R_BUF_LIB);
1532ebfedea0SLionel Sambuc                     goto err;
1533ebfedea0SLionel Sambuc                 }
1534ebfedea0SLionel Sambuc                 p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + n]);
1535*0a6a1f1dSLionel Sambuc                 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
1536ebfedea0SLionel Sambuc                     s2n(j, p);
1537ebfedea0SLionel Sambuc                     i2d_X509_NAME(name, &p);
1538ebfedea0SLionel Sambuc                     n += 2 + j;
1539ebfedea0SLionel Sambuc                     nl += 2 + j;
1540*0a6a1f1dSLionel Sambuc                 } else {
1541ebfedea0SLionel Sambuc                     d = p;
1542ebfedea0SLionel Sambuc                     i2d_X509_NAME(name, &p);
1543*0a6a1f1dSLionel Sambuc                     j -= 2;
1544*0a6a1f1dSLionel Sambuc                     s2n(j, d);
1545*0a6a1f1dSLionel Sambuc                     j += 2;
1546ebfedea0SLionel Sambuc                     n += j;
1547ebfedea0SLionel Sambuc                     nl += j;
1548ebfedea0SLionel Sambuc                 }
1549ebfedea0SLionel Sambuc             }
1550ebfedea0SLionel Sambuc         }
1551ebfedea0SLionel Sambuc         /* else no CA names */
1552ebfedea0SLionel Sambuc         p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + off]);
1553ebfedea0SLionel Sambuc         s2n(nl, p);
1554ebfedea0SLionel Sambuc 
1555ebfedea0SLionel Sambuc         d = (unsigned char *)buf->data;
1556ebfedea0SLionel Sambuc         *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
1557ebfedea0SLionel Sambuc         l2n3(n, d);
1558ebfedea0SLionel Sambuc         s2n(s->d1->handshake_write_seq, d);
1559ebfedea0SLionel Sambuc         s->d1->handshake_write_seq++;
1560ebfedea0SLionel Sambuc 
1561*0a6a1f1dSLionel Sambuc         /*
1562*0a6a1f1dSLionel Sambuc          * we should now have things packed up, so lets send it off
1563*0a6a1f1dSLionel Sambuc          */
1564ebfedea0SLionel Sambuc 
1565ebfedea0SLionel Sambuc         s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1566ebfedea0SLionel Sambuc         s->init_off = 0;
1567ebfedea0SLionel Sambuc #ifdef NETSCAPE_HANG_BUG
1568ebfedea0SLionel Sambuc /* XXX: what to do about this? */
1569ebfedea0SLionel Sambuc         p = (unsigned char *)s->init_buf->data + s->init_num;
1570ebfedea0SLionel Sambuc 
1571ebfedea0SLionel Sambuc         /* do the header */
1572ebfedea0SLionel Sambuc         *(p++) = SSL3_MT_SERVER_DONE;
1573ebfedea0SLionel Sambuc         *(p++) = 0;
1574ebfedea0SLionel Sambuc         *(p++) = 0;
1575ebfedea0SLionel Sambuc         *(p++) = 0;
1576ebfedea0SLionel Sambuc         s->init_num += 4;
1577ebfedea0SLionel Sambuc #endif
1578ebfedea0SLionel Sambuc 
1579ebfedea0SLionel Sambuc         /* XDTLS:  set message header ? */
1580ebfedea0SLionel Sambuc         msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
1581ebfedea0SLionel Sambuc         dtls1_set_message_header(s, (void *)s->init_buf->data,
1582*0a6a1f1dSLionel Sambuc                                  SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0,
1583*0a6a1f1dSLionel Sambuc                                  msg_len);
1584ebfedea0SLionel Sambuc 
1585ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1586ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
1587ebfedea0SLionel Sambuc 
1588ebfedea0SLionel Sambuc         s->state = SSL3_ST_SW_CERT_REQ_B;
1589ebfedea0SLionel Sambuc     }
1590ebfedea0SLionel Sambuc 
1591ebfedea0SLionel Sambuc     /* SSL3_ST_SW_CERT_REQ_B */
1592ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1593ebfedea0SLionel Sambuc  err:
1594ebfedea0SLionel Sambuc     return (-1);
1595ebfedea0SLionel Sambuc }
1596ebfedea0SLionel Sambuc 
dtls1_send_server_certificate(SSL * s)1597ebfedea0SLionel Sambuc int dtls1_send_server_certificate(SSL *s)
1598ebfedea0SLionel Sambuc {
1599ebfedea0SLionel Sambuc     unsigned long l;
1600ebfedea0SLionel Sambuc     X509 *x;
1601ebfedea0SLionel Sambuc 
1602*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_SW_CERT_A) {
1603ebfedea0SLionel Sambuc         x = ssl_get_server_send_cert(s);
1604*0a6a1f1dSLionel Sambuc         if (x == NULL) {
1605ebfedea0SLionel Sambuc             /* VRS: allow null cert if auth == KRB5 */
1606ebfedea0SLionel Sambuc             if ((s->s3->tmp.new_cipher->algorithm_mkey != SSL_kKRB5) ||
1607*0a6a1f1dSLionel Sambuc                 (s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5)) {
1608*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,
1609*0a6a1f1dSLionel Sambuc                        ERR_R_INTERNAL_ERROR);
1610ebfedea0SLionel Sambuc                 return (0);
1611ebfedea0SLionel Sambuc             }
1612ebfedea0SLionel Sambuc         }
1613ebfedea0SLionel Sambuc 
1614ebfedea0SLionel Sambuc         l = dtls1_output_cert_chain(s, x);
1615*0a6a1f1dSLionel Sambuc         if (!l) {
1616*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1617*0a6a1f1dSLionel Sambuc             return (0);
1618*0a6a1f1dSLionel Sambuc         }
1619ebfedea0SLionel Sambuc         s->state = SSL3_ST_SW_CERT_B;
1620ebfedea0SLionel Sambuc         s->init_num = (int)l;
1621ebfedea0SLionel Sambuc         s->init_off = 0;
1622ebfedea0SLionel Sambuc 
1623ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1624ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
1625ebfedea0SLionel Sambuc     }
1626ebfedea0SLionel Sambuc 
1627ebfedea0SLionel Sambuc     /* SSL3_ST_SW_CERT_B */
1628ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1629ebfedea0SLionel Sambuc }
1630ebfedea0SLionel Sambuc 
1631ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
dtls1_send_newsession_ticket(SSL * s)1632ebfedea0SLionel Sambuc int dtls1_send_newsession_ticket(SSL *s)
1633ebfedea0SLionel Sambuc {
1634*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
1635ebfedea0SLionel Sambuc         unsigned char *p, *senc, *macstart;
1636ebfedea0SLionel Sambuc         int len, slen;
1637ebfedea0SLionel Sambuc         unsigned int hlen, msg_len;
1638ebfedea0SLionel Sambuc         EVP_CIPHER_CTX ctx;
1639ebfedea0SLionel Sambuc         HMAC_CTX hctx;
1640ebfedea0SLionel Sambuc         SSL_CTX *tctx = s->initial_ctx;
1641ebfedea0SLionel Sambuc         unsigned char iv[EVP_MAX_IV_LENGTH];
1642ebfedea0SLionel Sambuc         unsigned char key_name[16];
1643ebfedea0SLionel Sambuc 
1644ebfedea0SLionel Sambuc         /* get session encoding length */
1645ebfedea0SLionel Sambuc         slen = i2d_SSL_SESSION(s->session, NULL);
1646*0a6a1f1dSLionel Sambuc         /*
1647*0a6a1f1dSLionel Sambuc          * Some length values are 16 bits, so forget it if session is too
1648*0a6a1f1dSLionel Sambuc          * long
1649ebfedea0SLionel Sambuc          */
1650ebfedea0SLionel Sambuc         if (slen > 0xFF00)
1651ebfedea0SLionel Sambuc             return -1;
1652*0a6a1f1dSLionel Sambuc         /*
1653*0a6a1f1dSLionel Sambuc          * Grow buffer if need be: the length calculation is as follows 12
1654*0a6a1f1dSLionel Sambuc          * (DTLS handshake message header) + 4 (ticket lifetime hint) + 2
1655*0a6a1f1dSLionel Sambuc          * (ticket length) + 16 (key name) + max_iv_len (iv length) +
1656*0a6a1f1dSLionel Sambuc          * session_length + max_enc_block_size (max encrypted session length)
1657*0a6a1f1dSLionel Sambuc          * + max_md_size (HMAC).
1658ebfedea0SLionel Sambuc          */
1659ebfedea0SLionel Sambuc         if (!BUF_MEM_grow(s->init_buf,
1660ebfedea0SLionel Sambuc                           DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH +
1661ebfedea0SLionel Sambuc                           EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
1662ebfedea0SLionel Sambuc             return -1;
1663ebfedea0SLionel Sambuc         senc = OPENSSL_malloc(slen);
1664ebfedea0SLionel Sambuc         if (!senc)
1665ebfedea0SLionel Sambuc             return -1;
1666ebfedea0SLionel Sambuc         p = senc;
1667ebfedea0SLionel Sambuc         i2d_SSL_SESSION(s->session, &p);
1668ebfedea0SLionel Sambuc 
1669ebfedea0SLionel Sambuc         p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]);
1670ebfedea0SLionel Sambuc         EVP_CIPHER_CTX_init(&ctx);
1671ebfedea0SLionel Sambuc         HMAC_CTX_init(&hctx);
1672*0a6a1f1dSLionel Sambuc         /*
1673*0a6a1f1dSLionel Sambuc          * Initialize HMAC and cipher contexts. If callback present it does
1674*0a6a1f1dSLionel Sambuc          * all the work otherwise use generated values from parent ctx.
1675ebfedea0SLionel Sambuc          */
1676*0a6a1f1dSLionel Sambuc         if (tctx->tlsext_ticket_key_cb) {
1677ebfedea0SLionel Sambuc             if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
1678*0a6a1f1dSLionel Sambuc                                            &hctx, 1) < 0) {
1679ebfedea0SLionel Sambuc                 OPENSSL_free(senc);
1680ebfedea0SLionel Sambuc                 return -1;
1681ebfedea0SLionel Sambuc             }
1682*0a6a1f1dSLionel Sambuc         } else {
1683ebfedea0SLionel Sambuc             RAND_pseudo_bytes(iv, 16);
1684ebfedea0SLionel Sambuc             EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
1685ebfedea0SLionel Sambuc                                tctx->tlsext_tick_aes_key, iv);
1686ebfedea0SLionel Sambuc             HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
1687ebfedea0SLionel Sambuc                          tlsext_tick_md(), NULL);
1688ebfedea0SLionel Sambuc             memcpy(key_name, tctx->tlsext_tick_key_name, 16);
1689ebfedea0SLionel Sambuc         }
1690ebfedea0SLionel Sambuc         l2n(s->session->tlsext_tick_lifetime_hint, p);
1691ebfedea0SLionel Sambuc         /* Skip ticket length for now */
1692ebfedea0SLionel Sambuc         p += 2;
1693ebfedea0SLionel Sambuc         /* Output key name */
1694ebfedea0SLionel Sambuc         macstart = p;
1695ebfedea0SLionel Sambuc         memcpy(p, key_name, 16);
1696ebfedea0SLionel Sambuc         p += 16;
1697ebfedea0SLionel Sambuc         /* output IV */
1698ebfedea0SLionel Sambuc         memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
1699ebfedea0SLionel Sambuc         p += EVP_CIPHER_CTX_iv_length(&ctx);
1700ebfedea0SLionel Sambuc         /* Encrypt session data */
1701ebfedea0SLionel Sambuc         EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
1702ebfedea0SLionel Sambuc         p += len;
1703ebfedea0SLionel Sambuc         EVP_EncryptFinal(&ctx, p, &len);
1704ebfedea0SLionel Sambuc         p += len;
1705ebfedea0SLionel Sambuc         EVP_CIPHER_CTX_cleanup(&ctx);
1706ebfedea0SLionel Sambuc 
1707ebfedea0SLionel Sambuc         HMAC_Update(&hctx, macstart, p - macstart);
1708ebfedea0SLionel Sambuc         HMAC_Final(&hctx, p, &hlen);
1709ebfedea0SLionel Sambuc         HMAC_CTX_cleanup(&hctx);
1710ebfedea0SLionel Sambuc 
1711ebfedea0SLionel Sambuc         p += hlen;
1712ebfedea0SLionel Sambuc         /* Now write out lengths: p points to end of data written */
1713ebfedea0SLionel Sambuc         /* Total length */
1714ebfedea0SLionel Sambuc         len = p - (unsigned char *)(s->init_buf->data);
1715ebfedea0SLionel Sambuc         /* Ticket length */
1716ebfedea0SLionel Sambuc         p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4;
1717ebfedea0SLionel Sambuc         s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p);
1718ebfedea0SLionel Sambuc 
1719ebfedea0SLionel Sambuc         /* number of bytes to write */
1720ebfedea0SLionel Sambuc         s->init_num = len;
1721ebfedea0SLionel Sambuc         s->state = SSL3_ST_SW_SESSION_TICKET_B;
1722ebfedea0SLionel Sambuc         s->init_off = 0;
1723ebfedea0SLionel Sambuc         OPENSSL_free(senc);
1724ebfedea0SLionel Sambuc 
1725ebfedea0SLionel Sambuc         /* XDTLS:  set message header ? */
1726ebfedea0SLionel Sambuc         msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
1727ebfedea0SLionel Sambuc         dtls1_set_message_header(s, (void *)s->init_buf->data,
1728*0a6a1f1dSLionel Sambuc                                  SSL3_MT_NEWSESSION_TICKET, msg_len, 0,
1729*0a6a1f1dSLionel Sambuc                                  msg_len);
1730ebfedea0SLionel Sambuc 
1731ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1732ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
1733ebfedea0SLionel Sambuc     }
1734ebfedea0SLionel Sambuc 
1735ebfedea0SLionel Sambuc     /* SSL3_ST_SW_SESSION_TICKET_B */
1736ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1737ebfedea0SLionel Sambuc }
1738ebfedea0SLionel Sambuc #endif
1739