xref: /minix3/crypto/external/bsd/openssl/dist/ssl/d1_clnt.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ssl/d1_clnt.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 #ifndef OPENSSL_NO_KRB5
119ebfedea0SLionel Sambuc # include "kssl_lcl.h"
120ebfedea0SLionel Sambuc #endif
121ebfedea0SLionel Sambuc #include <openssl/buffer.h>
122ebfedea0SLionel Sambuc #include <openssl/rand.h>
123ebfedea0SLionel Sambuc #include <openssl/objects.h>
124ebfedea0SLionel Sambuc #include <openssl/evp.h>
125ebfedea0SLionel Sambuc #include <openssl/md5.h>
126ebfedea0SLionel Sambuc #include <openssl/bn.h>
127ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
128ebfedea0SLionel Sambuc # include <openssl/dh.h>
129ebfedea0SLionel Sambuc #endif
130ebfedea0SLionel Sambuc 
131ebfedea0SLionel Sambuc static const SSL_METHOD *dtls1_get_client_method(int ver);
132ebfedea0SLionel Sambuc static int dtls1_get_hello_verify(SSL *s);
133ebfedea0SLionel Sambuc 
dtls1_get_client_method(int ver)134ebfedea0SLionel Sambuc static const SSL_METHOD *dtls1_get_client_method(int ver)
135ebfedea0SLionel Sambuc {
136ebfedea0SLionel Sambuc     if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER)
137ebfedea0SLionel Sambuc         return (DTLSv1_client_method());
138ebfedea0SLionel Sambuc     else
139ebfedea0SLionel Sambuc         return (NULL);
140ebfedea0SLionel Sambuc }
141ebfedea0SLionel Sambuc 
IMPLEMENT_dtls1_meth_func(DTLSv1_client_method,ssl_undefined_function,dtls1_connect,dtls1_get_client_method)142ebfedea0SLionel Sambuc IMPLEMENT_dtls1_meth_func(DTLSv1_client_method,
143ebfedea0SLionel Sambuc                           ssl_undefined_function,
144*0a6a1f1dSLionel Sambuc                           dtls1_connect, dtls1_get_client_method)
145ebfedea0SLionel Sambuc 
146ebfedea0SLionel Sambuc int dtls1_connect(SSL *s)
147ebfedea0SLionel Sambuc {
148ebfedea0SLionel Sambuc     BUF_MEM *buf = NULL;
149ebfedea0SLionel Sambuc     unsigned long Time = (unsigned long)time(NULL);
150ebfedea0SLionel Sambuc     void (*cb) (const SSL *ssl, int type, int val) = NULL;
151ebfedea0SLionel Sambuc     int ret = -1;
152ebfedea0SLionel Sambuc     int new_state, state, skip = 0;
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     s->in_handshake++;
168*0a6a1f1dSLionel Sambuc     if (!SSL_in_init(s) || SSL_in_before(s))
169*0a6a1f1dSLionel Sambuc         SSL_clear(s);
170ebfedea0SLionel Sambuc 
171ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
172*0a6a1f1dSLionel Sambuc     /*
173*0a6a1f1dSLionel Sambuc      * Notify SCTP BIO socket to enter handshake mode and prevent stream
174*0a6a1f1dSLionel Sambuc      * identifier other than 0. Will be ignored if no SCTP is used.
175ebfedea0SLionel Sambuc      */
176*0a6a1f1dSLionel Sambuc     BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
177*0a6a1f1dSLionel Sambuc              s->in_handshake, NULL);
178ebfedea0SLionel Sambuc #endif
179ebfedea0SLionel Sambuc 
180ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HEARTBEATS
181*0a6a1f1dSLionel Sambuc     /*
182*0a6a1f1dSLionel Sambuc      * If we're awaiting a HeartbeatResponse, pretend we already got and
183*0a6a1f1dSLionel Sambuc      * don't await it anymore, because Heartbeats don't make sense during
184*0a6a1f1dSLionel Sambuc      * handshakes anyway.
185ebfedea0SLionel Sambuc      */
186*0a6a1f1dSLionel Sambuc     if (s->tlsext_hb_pending) {
187ebfedea0SLionel Sambuc         dtls1_stop_timer(s);
188ebfedea0SLionel Sambuc         s->tlsext_hb_pending = 0;
189ebfedea0SLionel Sambuc         s->tlsext_hb_seq++;
190ebfedea0SLionel Sambuc     }
191ebfedea0SLionel Sambuc #endif
192ebfedea0SLionel Sambuc 
193*0a6a1f1dSLionel Sambuc     for (;;) {
194ebfedea0SLionel Sambuc         state = s->state;
195ebfedea0SLionel Sambuc 
196*0a6a1f1dSLionel Sambuc         switch (s->state) {
197ebfedea0SLionel Sambuc         case SSL_ST_RENEGOTIATE:
198ebfedea0SLionel Sambuc             s->renegotiate = 1;
199ebfedea0SLionel Sambuc             s->state = SSL_ST_CONNECT;
200ebfedea0SLionel Sambuc             s->ctx->stats.sess_connect_renegotiate++;
201ebfedea0SLionel Sambuc             /* break */
202ebfedea0SLionel Sambuc         case SSL_ST_BEFORE:
203ebfedea0SLionel Sambuc         case SSL_ST_CONNECT:
204ebfedea0SLionel Sambuc         case SSL_ST_BEFORE | SSL_ST_CONNECT:
205ebfedea0SLionel Sambuc         case SSL_ST_OK | SSL_ST_CONNECT:
206ebfedea0SLionel Sambuc 
207ebfedea0SLionel Sambuc             s->server = 0;
208*0a6a1f1dSLionel Sambuc             if (cb != NULL)
209*0a6a1f1dSLionel Sambuc                 cb(s, SSL_CB_HANDSHAKE_START, 1);
210ebfedea0SLionel Sambuc 
211ebfedea0SLionel Sambuc             if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
212*0a6a1f1dSLionel Sambuc                 (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00)) {
213ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);
214ebfedea0SLionel Sambuc                 ret = -1;
215*0a6a1f1dSLionel Sambuc                 s->state = SSL_ST_ERR;
216ebfedea0SLionel Sambuc                 goto end;
217ebfedea0SLionel Sambuc             }
218ebfedea0SLionel Sambuc 
219ebfedea0SLionel Sambuc             /* s->version=SSL3_VERSION; */
220ebfedea0SLionel Sambuc             s->type = SSL_ST_CONNECT;
221ebfedea0SLionel Sambuc 
222*0a6a1f1dSLionel Sambuc             if (s->init_buf == NULL) {
223*0a6a1f1dSLionel Sambuc                 if ((buf = BUF_MEM_new()) == NULL) {
224ebfedea0SLionel Sambuc                     ret = -1;
225*0a6a1f1dSLionel Sambuc                     s->state = SSL_ST_ERR;
226ebfedea0SLionel Sambuc                     goto end;
227ebfedea0SLionel Sambuc                 }
228*0a6a1f1dSLionel Sambuc                 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
229ebfedea0SLionel Sambuc                     ret = -1;
230*0a6a1f1dSLionel Sambuc                     s->state = SSL_ST_ERR;
231ebfedea0SLionel Sambuc                     goto end;
232ebfedea0SLionel Sambuc                 }
233ebfedea0SLionel Sambuc                 s->init_buf = buf;
234ebfedea0SLionel Sambuc                 buf = NULL;
235ebfedea0SLionel Sambuc             }
236ebfedea0SLionel Sambuc 
237*0a6a1f1dSLionel Sambuc             if (!ssl3_setup_buffers(s)) {
238*0a6a1f1dSLionel Sambuc                 ret = -1;
239*0a6a1f1dSLionel Sambuc                 s->state = SSL_ST_ERR;
240*0a6a1f1dSLionel Sambuc                 goto end;
241*0a6a1f1dSLionel Sambuc             }
242ebfedea0SLionel Sambuc 
243ebfedea0SLionel Sambuc             /* setup buffing BIO */
244*0a6a1f1dSLionel Sambuc             if (!ssl_init_wbio_buffer(s, 0)) {
245*0a6a1f1dSLionel Sambuc                 ret = -1;
246*0a6a1f1dSLionel Sambuc                 s->state = SSL_ST_ERR;
247*0a6a1f1dSLionel Sambuc                 goto end;
248*0a6a1f1dSLionel Sambuc             }
249ebfedea0SLionel Sambuc 
250ebfedea0SLionel Sambuc             /* don't push the buffering BIO quite yet */
251ebfedea0SLionel Sambuc 
252ebfedea0SLionel Sambuc             s->state = SSL3_ST_CW_CLNT_HELLO_A;
253ebfedea0SLionel Sambuc             s->ctx->stats.sess_connect++;
254ebfedea0SLionel Sambuc             s->init_num = 0;
255ebfedea0SLionel Sambuc             /* mark client_random uninitialized */
256ebfedea0SLionel Sambuc             memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
257ebfedea0SLionel Sambuc             s->d1->send_cookie = 0;
258ebfedea0SLionel Sambuc             s->hit = 0;
259*0a6a1f1dSLionel Sambuc             s->d1->change_cipher_spec_ok = 0;
260*0a6a1f1dSLionel Sambuc             /*
261*0a6a1f1dSLionel Sambuc              * Should have been reset by ssl3_get_finished, too.
262*0a6a1f1dSLionel Sambuc              */
263*0a6a1f1dSLionel Sambuc             s->s3->change_cipher_spec = 0;
264ebfedea0SLionel Sambuc             break;
265ebfedea0SLionel Sambuc 
266ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
267ebfedea0SLionel Sambuc         case DTLS1_SCTP_ST_CR_READ_SOCK:
268ebfedea0SLionel Sambuc 
269*0a6a1f1dSLionel Sambuc             if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
270ebfedea0SLionel Sambuc                 s->s3->in_read_app_data = 2;
271ebfedea0SLionel Sambuc                 s->rwstate = SSL_READING;
272ebfedea0SLionel Sambuc                 BIO_clear_retry_flags(SSL_get_rbio(s));
273ebfedea0SLionel Sambuc                 BIO_set_retry_read(SSL_get_rbio(s));
274ebfedea0SLionel Sambuc                 ret = -1;
275ebfedea0SLionel Sambuc                 goto end;
276ebfedea0SLionel Sambuc             }
277ebfedea0SLionel Sambuc 
278ebfedea0SLionel Sambuc             s->state = s->s3->tmp.next_state;
279ebfedea0SLionel Sambuc             break;
280ebfedea0SLionel Sambuc 
281ebfedea0SLionel Sambuc         case DTLS1_SCTP_ST_CW_WRITE_SOCK:
282ebfedea0SLionel Sambuc             /* read app data until dry event */
283ebfedea0SLionel Sambuc 
284ebfedea0SLionel Sambuc             ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
285*0a6a1f1dSLionel Sambuc             if (ret < 0)
286*0a6a1f1dSLionel Sambuc                 goto end;
287ebfedea0SLionel Sambuc 
288*0a6a1f1dSLionel Sambuc             if (ret == 0) {
289ebfedea0SLionel Sambuc                 s->s3->in_read_app_data = 2;
290ebfedea0SLionel Sambuc                 s->rwstate = SSL_READING;
291ebfedea0SLionel Sambuc                 BIO_clear_retry_flags(SSL_get_rbio(s));
292ebfedea0SLionel Sambuc                 BIO_set_retry_read(SSL_get_rbio(s));
293ebfedea0SLionel Sambuc                 ret = -1;
294ebfedea0SLionel Sambuc                 goto end;
295ebfedea0SLionel Sambuc             }
296ebfedea0SLionel Sambuc 
297ebfedea0SLionel Sambuc             s->state = s->d1->next_state;
298ebfedea0SLionel Sambuc             break;
299ebfedea0SLionel Sambuc #endif
300ebfedea0SLionel Sambuc 
301ebfedea0SLionel Sambuc         case SSL3_ST_CW_CLNT_HELLO_A:
302ebfedea0SLionel Sambuc         case SSL3_ST_CW_CLNT_HELLO_B:
303ebfedea0SLionel Sambuc 
304ebfedea0SLionel Sambuc             s->shutdown = 0;
305ebfedea0SLionel Sambuc 
306ebfedea0SLionel Sambuc             /* every DTLS ClientHello resets Finished MAC */
307ebfedea0SLionel Sambuc             ssl3_init_finished_mac(s);
308ebfedea0SLionel Sambuc 
309ebfedea0SLionel Sambuc             dtls1_start_timer(s);
310ebfedea0SLionel Sambuc             ret = dtls1_client_hello(s);
311*0a6a1f1dSLionel Sambuc             if (ret <= 0)
312*0a6a1f1dSLionel Sambuc                 goto end;
313ebfedea0SLionel Sambuc 
314*0a6a1f1dSLionel Sambuc             if (s->d1->send_cookie) {
315ebfedea0SLionel Sambuc                 s->state = SSL3_ST_CW_FLUSH;
316ebfedea0SLionel Sambuc                 s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
317*0a6a1f1dSLionel Sambuc             } else
318ebfedea0SLionel Sambuc                 s->state = SSL3_ST_CR_SRVR_HELLO_A;
319ebfedea0SLionel Sambuc 
320ebfedea0SLionel Sambuc             s->init_num = 0;
321ebfedea0SLionel Sambuc 
322ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
323ebfedea0SLionel Sambuc             /* Disable buffering for SCTP */
324*0a6a1f1dSLionel Sambuc             if (!BIO_dgram_is_sctp(SSL_get_wbio(s))) {
325ebfedea0SLionel Sambuc #endif
326*0a6a1f1dSLionel Sambuc                 /*
327*0a6a1f1dSLionel Sambuc                  * turn on buffering for the next lot of output
328*0a6a1f1dSLionel Sambuc                  */
329ebfedea0SLionel Sambuc                 if (s->bbio != s->wbio)
330ebfedea0SLionel Sambuc                     s->wbio = BIO_push(s->bbio, s->wbio);
331ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
332ebfedea0SLionel Sambuc             }
333ebfedea0SLionel Sambuc #endif
334ebfedea0SLionel Sambuc 
335ebfedea0SLionel Sambuc             break;
336ebfedea0SLionel Sambuc 
337ebfedea0SLionel Sambuc         case SSL3_ST_CR_SRVR_HELLO_A:
338ebfedea0SLionel Sambuc         case SSL3_ST_CR_SRVR_HELLO_B:
339ebfedea0SLionel Sambuc             ret = ssl3_get_server_hello(s);
340*0a6a1f1dSLionel Sambuc             if (ret <= 0)
341*0a6a1f1dSLionel Sambuc                 goto end;
342*0a6a1f1dSLionel Sambuc             else {
343*0a6a1f1dSLionel Sambuc                 if (s->hit) {
344ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
345*0a6a1f1dSLionel Sambuc                     /*
346*0a6a1f1dSLionel Sambuc                      * Add new shared key for SCTP-Auth, will be ignored if
347*0a6a1f1dSLionel Sambuc                      * no SCTP used.
348ebfedea0SLionel Sambuc                      */
349*0a6a1f1dSLionel Sambuc                     snprintf((char *)labelbuffer,
350*0a6a1f1dSLionel Sambuc                              sizeof(DTLS1_SCTP_AUTH_LABEL),
351ebfedea0SLionel Sambuc                              DTLS1_SCTP_AUTH_LABEL);
352ebfedea0SLionel Sambuc 
353ebfedea0SLionel Sambuc                     SSL_export_keying_material(s, sctpauthkey,
354*0a6a1f1dSLionel Sambuc                                                sizeof(sctpauthkey),
355*0a6a1f1dSLionel Sambuc                                                labelbuffer,
356*0a6a1f1dSLionel Sambuc                                                sizeof(labelbuffer), NULL, 0,
357*0a6a1f1dSLionel Sambuc                                                0);
358ebfedea0SLionel Sambuc 
359*0a6a1f1dSLionel Sambuc                     BIO_ctrl(SSL_get_wbio(s),
360*0a6a1f1dSLionel Sambuc                              BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
361ebfedea0SLionel Sambuc                              sizeof(sctpauthkey), sctpauthkey);
362ebfedea0SLionel Sambuc #endif
363ebfedea0SLionel Sambuc 
364ebfedea0SLionel Sambuc                     s->state = SSL3_ST_CR_FINISHED_A;
365*0a6a1f1dSLionel Sambuc                 } else
366ebfedea0SLionel Sambuc                     s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
367ebfedea0SLionel Sambuc             }
368ebfedea0SLionel Sambuc             s->init_num = 0;
369ebfedea0SLionel Sambuc             break;
370ebfedea0SLionel Sambuc 
371ebfedea0SLionel Sambuc         case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
372ebfedea0SLionel Sambuc         case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
373ebfedea0SLionel Sambuc 
374ebfedea0SLionel Sambuc             ret = dtls1_get_hello_verify(s);
375ebfedea0SLionel Sambuc             if (ret <= 0)
376ebfedea0SLionel Sambuc                 goto end;
377ebfedea0SLionel Sambuc             dtls1_stop_timer(s);
378ebfedea0SLionel Sambuc             if (s->d1->send_cookie) /* start again, with a cookie */
379ebfedea0SLionel Sambuc                 s->state = SSL3_ST_CW_CLNT_HELLO_A;
380ebfedea0SLionel Sambuc             else
381ebfedea0SLionel Sambuc                 s->state = SSL3_ST_CR_CERT_A;
382ebfedea0SLionel Sambuc             s->init_num = 0;
383ebfedea0SLionel Sambuc             break;
384ebfedea0SLionel Sambuc 
385ebfedea0SLionel Sambuc         case SSL3_ST_CR_CERT_A:
386ebfedea0SLionel Sambuc         case SSL3_ST_CR_CERT_B:
387ebfedea0SLionel Sambuc             /* Check if it is anon DH or PSK */
388ebfedea0SLionel Sambuc             if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
389*0a6a1f1dSLionel Sambuc                 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
390ebfedea0SLionel Sambuc                 ret = ssl3_get_server_certificate(s);
391*0a6a1f1dSLionel Sambuc                 if (ret <= 0)
392*0a6a1f1dSLionel Sambuc                     goto end;
393ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
394ebfedea0SLionel Sambuc                 if (s->tlsext_status_expected)
395ebfedea0SLionel Sambuc                     s->state = SSL3_ST_CR_CERT_STATUS_A;
396ebfedea0SLionel Sambuc                 else
397ebfedea0SLionel Sambuc                     s->state = SSL3_ST_CR_KEY_EXCH_A;
398*0a6a1f1dSLionel Sambuc             } else {
399ebfedea0SLionel Sambuc                 skip = 1;
400ebfedea0SLionel Sambuc                 s->state = SSL3_ST_CR_KEY_EXCH_A;
401ebfedea0SLionel Sambuc             }
402ebfedea0SLionel Sambuc #else
403*0a6a1f1dSLionel Sambuc             } else
404ebfedea0SLionel Sambuc                 skip = 1;
405ebfedea0SLionel Sambuc 
406ebfedea0SLionel Sambuc             s->state = SSL3_ST_CR_KEY_EXCH_A;
407ebfedea0SLionel Sambuc #endif
408ebfedea0SLionel Sambuc             s->init_num = 0;
409ebfedea0SLionel Sambuc             break;
410ebfedea0SLionel Sambuc 
411ebfedea0SLionel Sambuc         case SSL3_ST_CR_KEY_EXCH_A:
412ebfedea0SLionel Sambuc         case SSL3_ST_CR_KEY_EXCH_B:
413ebfedea0SLionel Sambuc             ret = ssl3_get_key_exchange(s);
414*0a6a1f1dSLionel Sambuc             if (ret <= 0)
415*0a6a1f1dSLionel Sambuc                 goto end;
416ebfedea0SLionel Sambuc             s->state = SSL3_ST_CR_CERT_REQ_A;
417ebfedea0SLionel Sambuc             s->init_num = 0;
418ebfedea0SLionel Sambuc 
419*0a6a1f1dSLionel Sambuc             /*
420*0a6a1f1dSLionel Sambuc              * at this point we check that we have the required stuff from
421*0a6a1f1dSLionel Sambuc              * the server
422*0a6a1f1dSLionel Sambuc              */
423*0a6a1f1dSLionel Sambuc             if (!ssl3_check_cert_and_algorithm(s)) {
424ebfedea0SLionel Sambuc                 ret = -1;
425*0a6a1f1dSLionel Sambuc                 s->state = SSL_ST_ERR;
426ebfedea0SLionel Sambuc                 goto end;
427ebfedea0SLionel Sambuc             }
428ebfedea0SLionel Sambuc             break;
429ebfedea0SLionel Sambuc 
430ebfedea0SLionel Sambuc         case SSL3_ST_CR_CERT_REQ_A:
431ebfedea0SLionel Sambuc         case SSL3_ST_CR_CERT_REQ_B:
432ebfedea0SLionel Sambuc             ret = ssl3_get_certificate_request(s);
433*0a6a1f1dSLionel Sambuc             if (ret <= 0)
434*0a6a1f1dSLionel Sambuc                 goto end;
435ebfedea0SLionel Sambuc             s->state = SSL3_ST_CR_SRVR_DONE_A;
436ebfedea0SLionel Sambuc             s->init_num = 0;
437ebfedea0SLionel Sambuc             break;
438ebfedea0SLionel Sambuc 
439ebfedea0SLionel Sambuc         case SSL3_ST_CR_SRVR_DONE_A:
440ebfedea0SLionel Sambuc         case SSL3_ST_CR_SRVR_DONE_B:
441ebfedea0SLionel Sambuc             ret = ssl3_get_server_done(s);
442*0a6a1f1dSLionel Sambuc             if (ret <= 0)
443*0a6a1f1dSLionel Sambuc                 goto end;
444ebfedea0SLionel Sambuc             dtls1_stop_timer(s);
445ebfedea0SLionel Sambuc             if (s->s3->tmp.cert_req)
446ebfedea0SLionel Sambuc                 s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
447ebfedea0SLionel Sambuc             else
448ebfedea0SLionel Sambuc                 s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
449ebfedea0SLionel Sambuc             s->init_num = 0;
450ebfedea0SLionel Sambuc 
451ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
452ebfedea0SLionel Sambuc             if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
453ebfedea0SLionel Sambuc                 state == SSL_ST_RENEGOTIATE)
454ebfedea0SLionel Sambuc                 s->state = DTLS1_SCTP_ST_CR_READ_SOCK;
455ebfedea0SLionel Sambuc             else
456ebfedea0SLionel Sambuc #endif
457ebfedea0SLionel Sambuc                 s->state = s->s3->tmp.next_state;
458ebfedea0SLionel Sambuc             break;
459ebfedea0SLionel Sambuc 
460ebfedea0SLionel Sambuc         case SSL3_ST_CW_CERT_A:
461ebfedea0SLionel Sambuc         case SSL3_ST_CW_CERT_B:
462ebfedea0SLionel Sambuc         case SSL3_ST_CW_CERT_C:
463ebfedea0SLionel Sambuc         case SSL3_ST_CW_CERT_D:
464ebfedea0SLionel Sambuc             dtls1_start_timer(s);
465ebfedea0SLionel Sambuc             ret = dtls1_send_client_certificate(s);
466*0a6a1f1dSLionel Sambuc             if (ret <= 0)
467*0a6a1f1dSLionel Sambuc                 goto end;
468ebfedea0SLionel Sambuc             s->state = SSL3_ST_CW_KEY_EXCH_A;
469ebfedea0SLionel Sambuc             s->init_num = 0;
470ebfedea0SLionel Sambuc             break;
471ebfedea0SLionel Sambuc 
472ebfedea0SLionel Sambuc         case SSL3_ST_CW_KEY_EXCH_A:
473ebfedea0SLionel Sambuc         case SSL3_ST_CW_KEY_EXCH_B:
474ebfedea0SLionel Sambuc             dtls1_start_timer(s);
475ebfedea0SLionel Sambuc             ret = dtls1_send_client_key_exchange(s);
476*0a6a1f1dSLionel Sambuc             if (ret <= 0)
477*0a6a1f1dSLionel Sambuc                 goto end;
478ebfedea0SLionel Sambuc 
479ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
480*0a6a1f1dSLionel Sambuc             /*
481*0a6a1f1dSLionel Sambuc              * Add new shared key for SCTP-Auth, will be ignored if no SCTP
482*0a6a1f1dSLionel Sambuc              * used.
483ebfedea0SLionel Sambuc              */
484ebfedea0SLionel Sambuc             snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
485ebfedea0SLionel Sambuc                      DTLS1_SCTP_AUTH_LABEL);
486ebfedea0SLionel Sambuc 
487ebfedea0SLionel Sambuc             SSL_export_keying_material(s, sctpauthkey,
488ebfedea0SLionel Sambuc                                        sizeof(sctpauthkey), labelbuffer,
489ebfedea0SLionel Sambuc                                        sizeof(labelbuffer), NULL, 0, 0);
490ebfedea0SLionel Sambuc 
491ebfedea0SLionel Sambuc             BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
492ebfedea0SLionel Sambuc                      sizeof(sctpauthkey), sctpauthkey);
493ebfedea0SLionel Sambuc #endif
494ebfedea0SLionel Sambuc 
495*0a6a1f1dSLionel Sambuc             /*
496*0a6a1f1dSLionel Sambuc              * EAY EAY EAY need to check for DH fix cert sent back
497*0a6a1f1dSLionel Sambuc              */
498*0a6a1f1dSLionel Sambuc             /*
499*0a6a1f1dSLionel Sambuc              * For TLS, cert_req is set to 2, so a cert chain of nothing is
500*0a6a1f1dSLionel Sambuc              * sent, but no verify packet is sent
501*0a6a1f1dSLionel Sambuc              */
502*0a6a1f1dSLionel Sambuc             if (s->s3->tmp.cert_req == 1) {
503ebfedea0SLionel Sambuc                 s->state = SSL3_ST_CW_CERT_VRFY_A;
504*0a6a1f1dSLionel Sambuc             } else {
505ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
506*0a6a1f1dSLionel Sambuc                 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
507ebfedea0SLionel Sambuc                     s->d1->next_state = SSL3_ST_CW_CHANGE_A;
508ebfedea0SLionel Sambuc                     s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
509*0a6a1f1dSLionel Sambuc                 } else
510ebfedea0SLionel Sambuc #endif
511ebfedea0SLionel Sambuc                     s->state = SSL3_ST_CW_CHANGE_A;
512ebfedea0SLionel Sambuc             }
513ebfedea0SLionel Sambuc 
514ebfedea0SLionel Sambuc             s->init_num = 0;
515ebfedea0SLionel Sambuc             break;
516ebfedea0SLionel Sambuc 
517ebfedea0SLionel Sambuc         case SSL3_ST_CW_CERT_VRFY_A:
518ebfedea0SLionel Sambuc         case SSL3_ST_CW_CERT_VRFY_B:
519ebfedea0SLionel Sambuc             dtls1_start_timer(s);
520ebfedea0SLionel Sambuc             ret = dtls1_send_client_verify(s);
521*0a6a1f1dSLionel Sambuc             if (ret <= 0)
522*0a6a1f1dSLionel Sambuc                 goto end;
523ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
524*0a6a1f1dSLionel Sambuc             if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
525ebfedea0SLionel Sambuc                 s->d1->next_state = SSL3_ST_CW_CHANGE_A;
526ebfedea0SLionel Sambuc                 s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
527*0a6a1f1dSLionel Sambuc             } else
528ebfedea0SLionel Sambuc #endif
529ebfedea0SLionel Sambuc                 s->state = SSL3_ST_CW_CHANGE_A;
530ebfedea0SLionel Sambuc             s->init_num = 0;
531ebfedea0SLionel Sambuc             break;
532ebfedea0SLionel Sambuc 
533ebfedea0SLionel Sambuc         case SSL3_ST_CW_CHANGE_A:
534ebfedea0SLionel Sambuc         case SSL3_ST_CW_CHANGE_B:
535ebfedea0SLionel Sambuc             if (!s->hit)
536ebfedea0SLionel Sambuc                 dtls1_start_timer(s);
537ebfedea0SLionel Sambuc             ret = dtls1_send_change_cipher_spec(s,
538*0a6a1f1dSLionel Sambuc                                                 SSL3_ST_CW_CHANGE_A,
539*0a6a1f1dSLionel Sambuc                                                 SSL3_ST_CW_CHANGE_B);
540*0a6a1f1dSLionel Sambuc             if (ret <= 0)
541*0a6a1f1dSLionel Sambuc                 goto end;
542ebfedea0SLionel Sambuc 
543ebfedea0SLionel Sambuc             s->state = SSL3_ST_CW_FINISHED_A;
544ebfedea0SLionel Sambuc             s->init_num = 0;
545ebfedea0SLionel Sambuc 
546ebfedea0SLionel Sambuc             s->session->cipher = s->s3->tmp.new_cipher;
547ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_COMP
548ebfedea0SLionel Sambuc             s->session->compress_meth = 0;
549ebfedea0SLionel Sambuc #else
550ebfedea0SLionel Sambuc             if (s->s3->tmp.new_compression == NULL)
551ebfedea0SLionel Sambuc                 s->session->compress_meth = 0;
552ebfedea0SLionel Sambuc             else
553*0a6a1f1dSLionel Sambuc                 s->session->compress_meth = s->s3->tmp.new_compression->id;
554ebfedea0SLionel Sambuc #endif
555*0a6a1f1dSLionel Sambuc             if (!s->method->ssl3_enc->setup_key_block(s)) {
556ebfedea0SLionel Sambuc                 ret = -1;
557*0a6a1f1dSLionel Sambuc                 s->state = SSL_ST_ERR;
558ebfedea0SLionel Sambuc                 goto end;
559ebfedea0SLionel Sambuc             }
560ebfedea0SLionel Sambuc 
561ebfedea0SLionel Sambuc             if (!s->method->ssl3_enc->change_cipher_state(s,
562ebfedea0SLionel Sambuc                                                           SSL3_CHANGE_CIPHER_CLIENT_WRITE))
563ebfedea0SLionel Sambuc             {
564ebfedea0SLionel Sambuc                 ret = -1;
565*0a6a1f1dSLionel Sambuc                 s->state = SSL_ST_ERR;
566ebfedea0SLionel Sambuc                 goto end;
567ebfedea0SLionel Sambuc             }
568*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_SCTP
569*0a6a1f1dSLionel Sambuc             if (s->hit) {
570*0a6a1f1dSLionel Sambuc                 /*
571*0a6a1f1dSLionel Sambuc                  * Change to new shared key of SCTP-Auth, will be ignored if
572*0a6a1f1dSLionel Sambuc                  * no SCTP used.
573*0a6a1f1dSLionel Sambuc                  */
574*0a6a1f1dSLionel Sambuc                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
575*0a6a1f1dSLionel Sambuc                          0, NULL);
576*0a6a1f1dSLionel Sambuc             }
577*0a6a1f1dSLionel Sambuc #endif
578ebfedea0SLionel Sambuc 
579ebfedea0SLionel Sambuc             dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
580ebfedea0SLionel Sambuc             break;
581ebfedea0SLionel Sambuc 
582ebfedea0SLionel Sambuc         case SSL3_ST_CW_FINISHED_A:
583ebfedea0SLionel Sambuc         case SSL3_ST_CW_FINISHED_B:
584ebfedea0SLionel Sambuc             if (!s->hit)
585ebfedea0SLionel Sambuc                 dtls1_start_timer(s);
586ebfedea0SLionel Sambuc             ret = dtls1_send_finished(s,
587*0a6a1f1dSLionel Sambuc                                       SSL3_ST_CW_FINISHED_A,
588*0a6a1f1dSLionel Sambuc                                       SSL3_ST_CW_FINISHED_B,
589*0a6a1f1dSLionel Sambuc                                       s->method->
590*0a6a1f1dSLionel Sambuc                                       ssl3_enc->client_finished_label,
591*0a6a1f1dSLionel Sambuc                                       s->method->
592*0a6a1f1dSLionel Sambuc                                       ssl3_enc->client_finished_label_len);
593*0a6a1f1dSLionel Sambuc             if (ret <= 0)
594*0a6a1f1dSLionel Sambuc                 goto end;
595ebfedea0SLionel Sambuc             s->state = SSL3_ST_CW_FLUSH;
596ebfedea0SLionel Sambuc 
597ebfedea0SLionel Sambuc             /* clear flags */
598ebfedea0SLionel Sambuc             s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
599*0a6a1f1dSLionel Sambuc             if (s->hit) {
600ebfedea0SLionel Sambuc                 s->s3->tmp.next_state = SSL_ST_OK;
601ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
602*0a6a1f1dSLionel Sambuc                 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
603ebfedea0SLionel Sambuc                     s->d1->next_state = s->s3->tmp.next_state;
604ebfedea0SLionel Sambuc                     s->s3->tmp.next_state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
605ebfedea0SLionel Sambuc                 }
606ebfedea0SLionel Sambuc #endif
607*0a6a1f1dSLionel Sambuc                 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
608ebfedea0SLionel Sambuc                     s->state = SSL_ST_OK;
609ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
610*0a6a1f1dSLionel Sambuc                     if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
611ebfedea0SLionel Sambuc                         s->d1->next_state = SSL_ST_OK;
612ebfedea0SLionel Sambuc                         s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
613ebfedea0SLionel Sambuc                     }
614ebfedea0SLionel Sambuc #endif
615ebfedea0SLionel Sambuc                     s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
616ebfedea0SLionel Sambuc                     s->s3->delay_buf_pop_ret = 0;
617ebfedea0SLionel Sambuc                 }
618*0a6a1f1dSLionel Sambuc             } else {
619*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_SCTP
620*0a6a1f1dSLionel Sambuc                 /*
621*0a6a1f1dSLionel Sambuc                  * Change to new shared key of SCTP-Auth, will be ignored if
622*0a6a1f1dSLionel Sambuc                  * no SCTP used.
623*0a6a1f1dSLionel Sambuc                  */
624*0a6a1f1dSLionel Sambuc                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
625*0a6a1f1dSLionel Sambuc                          0, NULL);
626*0a6a1f1dSLionel Sambuc #endif
627*0a6a1f1dSLionel Sambuc 
628ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
629*0a6a1f1dSLionel Sambuc                 /*
630*0a6a1f1dSLionel Sambuc                  * Allow NewSessionTicket if ticket expected
631*0a6a1f1dSLionel Sambuc                  */
632ebfedea0SLionel Sambuc                 if (s->tlsext_ticket_expected)
633ebfedea0SLionel Sambuc                     s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
634ebfedea0SLionel Sambuc                 else
635ebfedea0SLionel Sambuc #endif
636ebfedea0SLionel Sambuc 
637ebfedea0SLionel Sambuc                     s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
638ebfedea0SLionel Sambuc             }
639ebfedea0SLionel Sambuc             s->init_num = 0;
640ebfedea0SLionel Sambuc             break;
641ebfedea0SLionel Sambuc 
642ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
643ebfedea0SLionel Sambuc         case SSL3_ST_CR_SESSION_TICKET_A:
644ebfedea0SLionel Sambuc         case SSL3_ST_CR_SESSION_TICKET_B:
645ebfedea0SLionel Sambuc             ret = ssl3_get_new_session_ticket(s);
646*0a6a1f1dSLionel Sambuc             if (ret <= 0)
647*0a6a1f1dSLionel Sambuc                 goto end;
648ebfedea0SLionel Sambuc             s->state = SSL3_ST_CR_FINISHED_A;
649ebfedea0SLionel Sambuc             s->init_num = 0;
650ebfedea0SLionel Sambuc             break;
651ebfedea0SLionel Sambuc 
652ebfedea0SLionel Sambuc         case SSL3_ST_CR_CERT_STATUS_A:
653ebfedea0SLionel Sambuc         case SSL3_ST_CR_CERT_STATUS_B:
654ebfedea0SLionel Sambuc             ret = ssl3_get_cert_status(s);
655*0a6a1f1dSLionel Sambuc             if (ret <= 0)
656*0a6a1f1dSLionel Sambuc                 goto end;
657ebfedea0SLionel Sambuc             s->state = SSL3_ST_CR_KEY_EXCH_A;
658ebfedea0SLionel Sambuc             s->init_num = 0;
659ebfedea0SLionel Sambuc             break;
660ebfedea0SLionel Sambuc #endif
661ebfedea0SLionel Sambuc 
662ebfedea0SLionel Sambuc         case SSL3_ST_CR_FINISHED_A:
663ebfedea0SLionel Sambuc         case SSL3_ST_CR_FINISHED_B:
664ebfedea0SLionel Sambuc             s->d1->change_cipher_spec_ok = 1;
665ebfedea0SLionel Sambuc             ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
666ebfedea0SLionel Sambuc                                     SSL3_ST_CR_FINISHED_B);
667*0a6a1f1dSLionel Sambuc             if (ret <= 0)
668*0a6a1f1dSLionel Sambuc                 goto end;
669ebfedea0SLionel Sambuc             dtls1_stop_timer(s);
670ebfedea0SLionel Sambuc 
671ebfedea0SLionel Sambuc             if (s->hit)
672ebfedea0SLionel Sambuc                 s->state = SSL3_ST_CW_CHANGE_A;
673ebfedea0SLionel Sambuc             else
674ebfedea0SLionel Sambuc                 s->state = SSL_ST_OK;
675ebfedea0SLionel Sambuc 
676ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
677ebfedea0SLionel Sambuc             if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
678*0a6a1f1dSLionel Sambuc                 state == SSL_ST_RENEGOTIATE) {
679ebfedea0SLionel Sambuc                 s->d1->next_state = s->state;
680ebfedea0SLionel Sambuc                 s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
681ebfedea0SLionel Sambuc             }
682ebfedea0SLionel Sambuc #endif
683ebfedea0SLionel Sambuc 
684ebfedea0SLionel Sambuc             s->init_num = 0;
685ebfedea0SLionel Sambuc             break;
686ebfedea0SLionel Sambuc 
687ebfedea0SLionel Sambuc         case SSL3_ST_CW_FLUSH:
688ebfedea0SLionel Sambuc             s->rwstate = SSL_WRITING;
689*0a6a1f1dSLionel Sambuc             if (BIO_flush(s->wbio) <= 0) {
690*0a6a1f1dSLionel Sambuc                 /*
691*0a6a1f1dSLionel Sambuc                  * If the write error was fatal, stop trying
692*0a6a1f1dSLionel Sambuc                  */
693*0a6a1f1dSLionel Sambuc                 if (!BIO_should_retry(s->wbio)) {
694ebfedea0SLionel Sambuc                     s->rwstate = SSL_NOTHING;
695ebfedea0SLionel Sambuc                     s->state = s->s3->tmp.next_state;
696ebfedea0SLionel Sambuc                 }
697ebfedea0SLionel Sambuc 
698ebfedea0SLionel Sambuc                 ret = -1;
699ebfedea0SLionel Sambuc                 goto end;
700ebfedea0SLionel Sambuc             }
701ebfedea0SLionel Sambuc             s->rwstate = SSL_NOTHING;
702ebfedea0SLionel Sambuc             s->state = s->s3->tmp.next_state;
703ebfedea0SLionel Sambuc             break;
704ebfedea0SLionel Sambuc 
705ebfedea0SLionel Sambuc         case SSL_ST_OK:
706ebfedea0SLionel Sambuc             /* clean a few things up */
707ebfedea0SLionel Sambuc             ssl3_cleanup_key_block(s);
708ebfedea0SLionel Sambuc 
709ebfedea0SLionel Sambuc #if 0
710*0a6a1f1dSLionel Sambuc             if (s->init_buf != NULL) {
711ebfedea0SLionel Sambuc                 BUF_MEM_free(s->init_buf);
712ebfedea0SLionel Sambuc                 s->init_buf = NULL;
713ebfedea0SLionel Sambuc             }
714ebfedea0SLionel Sambuc #endif
715ebfedea0SLionel Sambuc 
716*0a6a1f1dSLionel Sambuc             /*
717*0a6a1f1dSLionel Sambuc              * If we are not 'joining' the last two packets, remove the
718*0a6a1f1dSLionel Sambuc              * buffering now
719*0a6a1f1dSLionel Sambuc              */
720ebfedea0SLionel Sambuc             if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
721ebfedea0SLionel Sambuc                 ssl_free_wbio_buffer(s);
722ebfedea0SLionel Sambuc             /* else do it later in ssl3_write */
723ebfedea0SLionel Sambuc 
724ebfedea0SLionel Sambuc             s->init_num = 0;
725ebfedea0SLionel Sambuc             s->renegotiate = 0;
726ebfedea0SLionel Sambuc             s->new_session = 0;
727ebfedea0SLionel Sambuc 
728ebfedea0SLionel Sambuc             ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
729*0a6a1f1dSLionel Sambuc             if (s->hit)
730*0a6a1f1dSLionel Sambuc                 s->ctx->stats.sess_hit++;
731ebfedea0SLionel Sambuc 
732ebfedea0SLionel Sambuc             ret = 1;
733ebfedea0SLionel Sambuc             /* s->server=0; */
734ebfedea0SLionel Sambuc             s->handshake_func = dtls1_connect;
735ebfedea0SLionel Sambuc             s->ctx->stats.sess_connect_good++;
736ebfedea0SLionel Sambuc 
737*0a6a1f1dSLionel Sambuc             if (cb != NULL)
738*0a6a1f1dSLionel Sambuc                 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
739ebfedea0SLionel Sambuc 
740ebfedea0SLionel Sambuc             /* done with handshaking */
741ebfedea0SLionel Sambuc             s->d1->handshake_read_seq = 0;
742ebfedea0SLionel Sambuc             s->d1->next_handshake_write_seq = 0;
743ebfedea0SLionel Sambuc             goto end;
744ebfedea0SLionel Sambuc             /* break; */
745ebfedea0SLionel Sambuc 
746*0a6a1f1dSLionel Sambuc         case SSL_ST_ERR:
747ebfedea0SLionel Sambuc         default:
748ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_CONNECT, SSL_R_UNKNOWN_STATE);
749ebfedea0SLionel Sambuc             ret = -1;
750ebfedea0SLionel Sambuc             goto end;
751ebfedea0SLionel Sambuc             /* break; */
752ebfedea0SLionel Sambuc         }
753ebfedea0SLionel Sambuc 
754ebfedea0SLionel Sambuc         /* did we do anything */
755*0a6a1f1dSLionel Sambuc         if (!s->s3->tmp.reuse_message && !skip) {
756*0a6a1f1dSLionel Sambuc             if (s->debug) {
757ebfedea0SLionel Sambuc                 if ((ret = BIO_flush(s->wbio)) <= 0)
758ebfedea0SLionel Sambuc                     goto end;
759ebfedea0SLionel Sambuc             }
760ebfedea0SLionel Sambuc 
761*0a6a1f1dSLionel Sambuc             if ((cb != NULL) && (s->state != state)) {
762ebfedea0SLionel Sambuc                 new_state = s->state;
763ebfedea0SLionel Sambuc                 s->state = state;
764ebfedea0SLionel Sambuc                 cb(s, SSL_CB_CONNECT_LOOP, 1);
765ebfedea0SLionel Sambuc                 s->state = new_state;
766ebfedea0SLionel Sambuc             }
767ebfedea0SLionel Sambuc         }
768ebfedea0SLionel Sambuc         skip = 0;
769ebfedea0SLionel Sambuc     }
770ebfedea0SLionel Sambuc  end:
771ebfedea0SLionel Sambuc     s->in_handshake--;
772ebfedea0SLionel Sambuc 
773ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
774*0a6a1f1dSLionel Sambuc     /*
775*0a6a1f1dSLionel Sambuc      * Notify SCTP BIO socket to leave handshake mode and allow stream
776*0a6a1f1dSLionel Sambuc      * identifier other than 0. Will be ignored if no SCTP is used.
777ebfedea0SLionel Sambuc      */
778*0a6a1f1dSLionel Sambuc     BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
779*0a6a1f1dSLionel Sambuc              s->in_handshake, NULL);
780ebfedea0SLionel Sambuc #endif
781ebfedea0SLionel Sambuc 
782ebfedea0SLionel Sambuc     if (buf != NULL)
783ebfedea0SLionel Sambuc         BUF_MEM_free(buf);
784ebfedea0SLionel Sambuc     if (cb != NULL)
785ebfedea0SLionel Sambuc         cb(s, SSL_CB_CONNECT_EXIT, ret);
786ebfedea0SLionel Sambuc     return (ret);
787ebfedea0SLionel Sambuc }
788ebfedea0SLionel Sambuc 
dtls1_client_hello(SSL * s)789ebfedea0SLionel Sambuc int dtls1_client_hello(SSL *s)
790ebfedea0SLionel Sambuc {
791ebfedea0SLionel Sambuc     unsigned char *buf;
792ebfedea0SLionel Sambuc     unsigned char *p, *d;
793ebfedea0SLionel Sambuc     unsigned int i, j;
794*0a6a1f1dSLionel Sambuc     unsigned long l;
795ebfedea0SLionel Sambuc     SSL_COMP *comp;
796ebfedea0SLionel Sambuc 
797ebfedea0SLionel Sambuc     buf = (unsigned char *)s->init_buf->data;
798*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
799ebfedea0SLionel Sambuc         SSL_SESSION *sess = s->session;
800*0a6a1f1dSLionel Sambuc         if ((s->session == NULL) || (s->session->ssl_version != s->version) ||
801ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_TLSEXT
802ebfedea0SLionel Sambuc             !sess->session_id_length ||
803ebfedea0SLionel Sambuc #else
804ebfedea0SLionel Sambuc             (!sess->session_id_length && !sess->tlsext_tick) ||
805ebfedea0SLionel Sambuc #endif
806*0a6a1f1dSLionel Sambuc             (s->session->not_resumable)) {
807ebfedea0SLionel Sambuc             if (!ssl_get_new_session(s, 0))
808ebfedea0SLionel Sambuc                 goto err;
809ebfedea0SLionel Sambuc         }
810ebfedea0SLionel Sambuc         /* else use the pre-loaded session */
811ebfedea0SLionel Sambuc 
812ebfedea0SLionel Sambuc         p = s->s3->client_random;
813ebfedea0SLionel Sambuc 
814*0a6a1f1dSLionel Sambuc         /*
815*0a6a1f1dSLionel Sambuc          * if client_random is initialized, reuse it, we are required to use
816*0a6a1f1dSLionel Sambuc          * same upon reply to HelloVerify
817*0a6a1f1dSLionel Sambuc          */
818ebfedea0SLionel Sambuc         for (i = 0; p[i] == '\0' && i < sizeof(s->s3->client_random); i++) ;
819ebfedea0SLionel Sambuc         if (i == sizeof(s->s3->client_random))
820*0a6a1f1dSLionel Sambuc             ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random));
821ebfedea0SLionel Sambuc 
822ebfedea0SLionel Sambuc         /* Do the message type and length last */
823ebfedea0SLionel Sambuc         d = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
824ebfedea0SLionel Sambuc 
825ebfedea0SLionel Sambuc         *(p++) = s->version >> 8;
826ebfedea0SLionel Sambuc         *(p++) = s->version & 0xff;
827ebfedea0SLionel Sambuc         s->client_version = s->version;
828ebfedea0SLionel Sambuc 
829ebfedea0SLionel Sambuc         /* Random stuff */
830ebfedea0SLionel Sambuc         memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
831ebfedea0SLionel Sambuc         p += SSL3_RANDOM_SIZE;
832ebfedea0SLionel Sambuc 
833ebfedea0SLionel Sambuc         /* Session ID */
834ebfedea0SLionel Sambuc         if (s->new_session)
835ebfedea0SLionel Sambuc             i = 0;
836ebfedea0SLionel Sambuc         else
837ebfedea0SLionel Sambuc             i = s->session->session_id_length;
838ebfedea0SLionel Sambuc         *(p++) = i;
839*0a6a1f1dSLionel Sambuc         if (i != 0) {
840*0a6a1f1dSLionel Sambuc             if (i > sizeof s->session->session_id) {
841ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
842ebfedea0SLionel Sambuc                 goto err;
843ebfedea0SLionel Sambuc             }
844ebfedea0SLionel Sambuc             memcpy(p, s->session->session_id, i);
845ebfedea0SLionel Sambuc             p += i;
846ebfedea0SLionel Sambuc         }
847ebfedea0SLionel Sambuc 
848ebfedea0SLionel Sambuc         /* cookie stuff */
849*0a6a1f1dSLionel Sambuc         if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
850ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
851ebfedea0SLionel Sambuc             goto err;
852ebfedea0SLionel Sambuc         }
853ebfedea0SLionel Sambuc         *(p++) = s->d1->cookie_len;
854ebfedea0SLionel Sambuc         memcpy(p, s->d1->cookie, s->d1->cookie_len);
855ebfedea0SLionel Sambuc         p += s->d1->cookie_len;
856ebfedea0SLionel Sambuc 
857ebfedea0SLionel Sambuc         /* Ciphers supported */
858ebfedea0SLionel Sambuc         i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
859*0a6a1f1dSLionel Sambuc         if (i == 0) {
860ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
861ebfedea0SLionel Sambuc             goto err;
862ebfedea0SLionel Sambuc         }
863ebfedea0SLionel Sambuc         s2n(i, p);
864ebfedea0SLionel Sambuc         p += i;
865ebfedea0SLionel Sambuc 
866ebfedea0SLionel Sambuc         /* COMPRESSION */
867ebfedea0SLionel Sambuc         if (s->ctx->comp_methods == NULL)
868ebfedea0SLionel Sambuc             j = 0;
869ebfedea0SLionel Sambuc         else
870ebfedea0SLionel Sambuc             j = sk_SSL_COMP_num(s->ctx->comp_methods);
871ebfedea0SLionel Sambuc         *(p++) = 1 + j;
872*0a6a1f1dSLionel Sambuc         for (i = 0; i < j; i++) {
873ebfedea0SLionel Sambuc             comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
874ebfedea0SLionel Sambuc             *(p++) = comp->id;
875ebfedea0SLionel Sambuc         }
876ebfedea0SLionel Sambuc         *(p++) = 0;             /* Add the NULL method */
877ebfedea0SLionel Sambuc 
878ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
879*0a6a1f1dSLionel Sambuc         /* TLS extensions */
880*0a6a1f1dSLionel Sambuc         if (ssl_prepare_clienthello_tlsext(s) <= 0) {
881*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_DTLS1_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
882*0a6a1f1dSLionel Sambuc             goto err;
883*0a6a1f1dSLionel Sambuc         }
884*0a6a1f1dSLionel Sambuc         if ((p =
885*0a6a1f1dSLionel Sambuc              ssl_add_clienthello_tlsext(s, p,
886*0a6a1f1dSLionel Sambuc                                         buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
887*0a6a1f1dSLionel Sambuc             NULL) {
888ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
889ebfedea0SLionel Sambuc             goto err;
890ebfedea0SLionel Sambuc         }
891ebfedea0SLionel Sambuc #endif
892ebfedea0SLionel Sambuc 
893ebfedea0SLionel Sambuc         l = (p - d);
894ebfedea0SLionel Sambuc         d = buf;
895ebfedea0SLionel Sambuc 
896ebfedea0SLionel Sambuc         d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l);
897ebfedea0SLionel Sambuc 
898ebfedea0SLionel Sambuc         s->state = SSL3_ST_CW_CLNT_HELLO_B;
899ebfedea0SLionel Sambuc         /* number of bytes to write */
900ebfedea0SLionel Sambuc         s->init_num = p - buf;
901ebfedea0SLionel Sambuc         s->init_off = 0;
902ebfedea0SLionel Sambuc 
903ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
904ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
905ebfedea0SLionel Sambuc     }
906ebfedea0SLionel Sambuc 
907ebfedea0SLionel Sambuc     /* SSL3_ST_CW_CLNT_HELLO_B */
908ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
909ebfedea0SLionel Sambuc  err:
910ebfedea0SLionel Sambuc     return (-1);
911ebfedea0SLionel Sambuc }
912ebfedea0SLionel Sambuc 
dtls1_get_hello_verify(SSL * s)913ebfedea0SLionel Sambuc static int dtls1_get_hello_verify(SSL *s)
914ebfedea0SLionel Sambuc {
915ebfedea0SLionel Sambuc     int n, al, ok = 0;
916ebfedea0SLionel Sambuc     unsigned char *data;
917ebfedea0SLionel Sambuc     unsigned int cookie_len;
918ebfedea0SLionel Sambuc 
919ebfedea0SLionel Sambuc     n = s->method->ssl_get_message(s,
920ebfedea0SLionel Sambuc                                    DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
921ebfedea0SLionel Sambuc                                    DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
922*0a6a1f1dSLionel Sambuc                                    -1, s->max_cert_list, &ok);
923ebfedea0SLionel Sambuc 
924*0a6a1f1dSLionel Sambuc     if (!ok)
925*0a6a1f1dSLionel Sambuc         return ((int)n);
926ebfedea0SLionel Sambuc 
927*0a6a1f1dSLionel Sambuc     if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
928ebfedea0SLionel Sambuc         s->d1->send_cookie = 0;
929ebfedea0SLionel Sambuc         s->s3->tmp.reuse_message = 1;
930ebfedea0SLionel Sambuc         return (1);
931ebfedea0SLionel Sambuc     }
932ebfedea0SLionel Sambuc 
933ebfedea0SLionel Sambuc     data = (unsigned char *)s->init_msg;
934ebfedea0SLionel Sambuc 
935*0a6a1f1dSLionel Sambuc     if ((data[0] != (s->version >> 8)) || (data[1] != (s->version & 0xff))) {
936ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY, SSL_R_WRONG_SSL_VERSION);
937ebfedea0SLionel Sambuc         s->version = (s->version & 0xff00) | data[1];
938ebfedea0SLionel Sambuc         al = SSL_AD_PROTOCOL_VERSION;
939ebfedea0SLionel Sambuc         goto f_err;
940ebfedea0SLionel Sambuc     }
941ebfedea0SLionel Sambuc     data += 2;
942ebfedea0SLionel Sambuc 
943ebfedea0SLionel Sambuc     cookie_len = *(data++);
944*0a6a1f1dSLionel Sambuc     if (cookie_len > sizeof(s->d1->cookie)) {
945ebfedea0SLionel Sambuc         al = SSL_AD_ILLEGAL_PARAMETER;
946ebfedea0SLionel Sambuc         goto f_err;
947ebfedea0SLionel Sambuc     }
948ebfedea0SLionel Sambuc 
949ebfedea0SLionel Sambuc     memcpy(s->d1->cookie, data, cookie_len);
950ebfedea0SLionel Sambuc     s->d1->cookie_len = cookie_len;
951ebfedea0SLionel Sambuc 
952ebfedea0SLionel Sambuc     s->d1->send_cookie = 1;
953ebfedea0SLionel Sambuc     return 1;
954ebfedea0SLionel Sambuc 
955ebfedea0SLionel Sambuc  f_err:
956ebfedea0SLionel Sambuc     ssl3_send_alert(s, SSL3_AL_FATAL, al);
957*0a6a1f1dSLionel Sambuc     s->state = SSL_ST_ERR;
958ebfedea0SLionel Sambuc     return -1;
959ebfedea0SLionel Sambuc }
960ebfedea0SLionel Sambuc 
dtls1_send_client_key_exchange(SSL * s)961ebfedea0SLionel Sambuc int dtls1_send_client_key_exchange(SSL *s)
962ebfedea0SLionel Sambuc {
963ebfedea0SLionel Sambuc     unsigned char *p, *d;
964ebfedea0SLionel Sambuc     int n;
965ebfedea0SLionel Sambuc     unsigned long alg_k;
966ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
967ebfedea0SLionel Sambuc     unsigned char *q;
968ebfedea0SLionel Sambuc     EVP_PKEY *pkey = NULL;
969ebfedea0SLionel Sambuc #endif
970ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
971ebfedea0SLionel Sambuc     KSSL_ERR kssl_err;
972ebfedea0SLionel Sambuc #endif                          /* OPENSSL_NO_KRB5 */
973ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
974ebfedea0SLionel Sambuc     EC_KEY *clnt_ecdh = NULL;
975ebfedea0SLionel Sambuc     const EC_POINT *srvr_ecpoint = NULL;
976ebfedea0SLionel Sambuc     EVP_PKEY *srvr_pub_pkey = NULL;
977ebfedea0SLionel Sambuc     unsigned char *encodedPoint = NULL;
978ebfedea0SLionel Sambuc     int encoded_pt_len = 0;
979ebfedea0SLionel Sambuc     BN_CTX *bn_ctx = NULL;
980ebfedea0SLionel Sambuc #endif
981ebfedea0SLionel Sambuc 
982*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
983ebfedea0SLionel Sambuc         d = (unsigned char *)s->init_buf->data;
984ebfedea0SLionel Sambuc         p = &(d[DTLS1_HM_HEADER_LENGTH]);
985ebfedea0SLionel Sambuc 
986ebfedea0SLionel Sambuc         alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
987ebfedea0SLionel Sambuc 
988ebfedea0SLionel Sambuc         /* Fool emacs indentation */
989*0a6a1f1dSLionel Sambuc         if (0) {
990*0a6a1f1dSLionel Sambuc         }
991ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
992*0a6a1f1dSLionel Sambuc         else if (alg_k & SSL_kRSA) {
993ebfedea0SLionel Sambuc             RSA *rsa;
994ebfedea0SLionel Sambuc             unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
995ebfedea0SLionel Sambuc 
996*0a6a1f1dSLionel Sambuc             if (s->session->sess_cert == NULL) {
997*0a6a1f1dSLionel Sambuc                 /*
998*0a6a1f1dSLionel Sambuc                  * We should always have a server certificate with SSL_kRSA.
999*0a6a1f1dSLionel Sambuc                  */
1000*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1001*0a6a1f1dSLionel Sambuc                        ERR_R_INTERNAL_ERROR);
1002*0a6a1f1dSLionel Sambuc                 goto err;
1003*0a6a1f1dSLionel Sambuc             }
1004*0a6a1f1dSLionel Sambuc 
1005ebfedea0SLionel Sambuc             if (s->session->sess_cert->peer_rsa_tmp != NULL)
1006ebfedea0SLionel Sambuc                 rsa = s->session->sess_cert->peer_rsa_tmp;
1007*0a6a1f1dSLionel Sambuc             else {
1008*0a6a1f1dSLionel Sambuc                 pkey =
1009*0a6a1f1dSLionel Sambuc                     X509_get_pubkey(s->session->
1010*0a6a1f1dSLionel Sambuc                                     sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
1011*0a6a1f1dSLionel Sambuc                                     x509);
1012*0a6a1f1dSLionel Sambuc                 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
1013*0a6a1f1dSLionel Sambuc                     || (pkey->pkey.rsa == NULL)) {
1014*0a6a1f1dSLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1015*0a6a1f1dSLionel Sambuc                            ERR_R_INTERNAL_ERROR);
1016ebfedea0SLionel Sambuc                     goto err;
1017ebfedea0SLionel Sambuc                 }
1018ebfedea0SLionel Sambuc                 rsa = pkey->pkey.rsa;
1019ebfedea0SLionel Sambuc                 EVP_PKEY_free(pkey);
1020ebfedea0SLionel Sambuc             }
1021ebfedea0SLionel Sambuc 
1022ebfedea0SLionel Sambuc             tmp_buf[0] = s->client_version >> 8;
1023ebfedea0SLionel Sambuc             tmp_buf[1] = s->client_version & 0xff;
1024ebfedea0SLionel Sambuc             if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
1025ebfedea0SLionel Sambuc                 goto err;
1026ebfedea0SLionel Sambuc 
1027ebfedea0SLionel Sambuc             s->session->master_key_length = sizeof tmp_buf;
1028ebfedea0SLionel Sambuc 
1029ebfedea0SLionel Sambuc             q = p;
1030ebfedea0SLionel Sambuc             /* Fix buf for TLS and [incidentally] DTLS */
1031ebfedea0SLionel Sambuc             if (s->version > SSL3_VERSION)
1032ebfedea0SLionel Sambuc                 p += 2;
1033ebfedea0SLionel Sambuc             n = RSA_public_encrypt(sizeof tmp_buf,
1034ebfedea0SLionel Sambuc                                    tmp_buf, p, rsa, RSA_PKCS1_PADDING);
1035ebfedea0SLionel Sambuc # ifdef PKCS1_CHECK
1036*0a6a1f1dSLionel Sambuc             if (s->options & SSL_OP_PKCS1_CHECK_1)
1037*0a6a1f1dSLionel Sambuc                 p[1]++;
1038*0a6a1f1dSLionel Sambuc             if (s->options & SSL_OP_PKCS1_CHECK_2)
1039*0a6a1f1dSLionel Sambuc                 tmp_buf[0] = 0x70;
1040ebfedea0SLionel Sambuc # endif
1041*0a6a1f1dSLionel Sambuc             if (n <= 0) {
1042*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1043*0a6a1f1dSLionel Sambuc                        SSL_R_BAD_RSA_ENCRYPT);
1044ebfedea0SLionel Sambuc                 goto err;
1045ebfedea0SLionel Sambuc             }
1046ebfedea0SLionel Sambuc 
1047ebfedea0SLionel Sambuc             /* Fix buf for TLS and [incidentally] DTLS */
1048*0a6a1f1dSLionel Sambuc             if (s->version > SSL3_VERSION) {
1049ebfedea0SLionel Sambuc                 s2n(n, q);
1050ebfedea0SLionel Sambuc                 n += 2;
1051ebfedea0SLionel Sambuc             }
1052ebfedea0SLionel Sambuc 
1053ebfedea0SLionel Sambuc             s->session->master_key_length =
1054ebfedea0SLionel Sambuc                 s->method->ssl3_enc->generate_master_secret(s,
1055*0a6a1f1dSLionel Sambuc                                                             s->
1056*0a6a1f1dSLionel Sambuc                                                             session->master_key,
1057*0a6a1f1dSLionel Sambuc                                                             tmp_buf,
1058*0a6a1f1dSLionel Sambuc                                                             sizeof tmp_buf);
1059ebfedea0SLionel Sambuc             OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
1060ebfedea0SLionel Sambuc         }
1061ebfedea0SLionel Sambuc #endif
1062ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
1063*0a6a1f1dSLionel Sambuc         else if (alg_k & SSL_kKRB5) {
1064ebfedea0SLionel Sambuc             krb5_error_code krb5rc;
1065ebfedea0SLionel Sambuc             KSSL_CTX *kssl_ctx = s->kssl_ctx;
1066ebfedea0SLionel Sambuc             /*  krb5_data   krb5_ap_req;  */
1067ebfedea0SLionel Sambuc             krb5_data *enc_ticket;
1068ebfedea0SLionel Sambuc             krb5_data authenticator, *authp = NULL;
1069ebfedea0SLionel Sambuc             EVP_CIPHER_CTX ciph_ctx;
1070ebfedea0SLionel Sambuc             const EVP_CIPHER *enc = NULL;
1071ebfedea0SLionel Sambuc             unsigned char iv[EVP_MAX_IV_LENGTH];
1072ebfedea0SLionel Sambuc             unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1073*0a6a1f1dSLionel Sambuc             unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
1074ebfedea0SLionel Sambuc             int padl, outl = sizeof(epms);
1075ebfedea0SLionel Sambuc 
1076ebfedea0SLionel Sambuc             EVP_CIPHER_CTX_init(&ciph_ctx);
1077ebfedea0SLionel Sambuc 
1078ebfedea0SLionel Sambuc # ifdef KSSL_DEBUG
1079ebfedea0SLionel Sambuc             printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
1080ebfedea0SLionel Sambuc                    alg_k, SSL_kKRB5);
1081ebfedea0SLionel Sambuc # endif                         /* KSSL_DEBUG */
1082ebfedea0SLionel Sambuc 
1083ebfedea0SLionel Sambuc             authp = NULL;
1084ebfedea0SLionel Sambuc # ifdef KRB5SENDAUTH
1085*0a6a1f1dSLionel Sambuc             if (KRB5SENDAUTH)
1086*0a6a1f1dSLionel Sambuc                 authp = &authenticator;
1087ebfedea0SLionel Sambuc # endif                         /* KRB5SENDAUTH */
1088ebfedea0SLionel Sambuc 
1089*0a6a1f1dSLionel Sambuc             krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
1090ebfedea0SLionel Sambuc             enc = kssl_map_enc(kssl_ctx->enctype);
1091ebfedea0SLionel Sambuc             if (enc == NULL)
1092ebfedea0SLionel Sambuc                 goto err;
1093ebfedea0SLionel Sambuc # ifdef KSSL_DEBUG
1094ebfedea0SLionel Sambuc             {
1095ebfedea0SLionel Sambuc                 printf("kssl_cget_tkt rtn %d\n", krb5rc);
1096ebfedea0SLionel Sambuc                 if (krb5rc && kssl_err.text)
1097ebfedea0SLionel Sambuc                     printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
1098ebfedea0SLionel Sambuc             }
1099ebfedea0SLionel Sambuc # endif                         /* KSSL_DEBUG */
1100ebfedea0SLionel Sambuc 
1101*0a6a1f1dSLionel Sambuc             if (krb5rc) {
1102*0a6a1f1dSLionel Sambuc                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1103*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
1104ebfedea0SLionel Sambuc                 goto err;
1105ebfedea0SLionel Sambuc             }
1106ebfedea0SLionel Sambuc 
1107*0a6a1f1dSLionel Sambuc             /*-
1108*0a6a1f1dSLionel Sambuc              *   20010406 VRS - Earlier versions used KRB5 AP_REQ
1109ebfedea0SLionel Sambuc             **  in place of RFC 2712 KerberosWrapper, as in:
1110ebfedea0SLionel Sambuc             **
1111ebfedea0SLionel Sambuc             **  Send ticket (copy to *p, set n = length)
1112ebfedea0SLionel Sambuc             **  n = krb5_ap_req.length;
1113ebfedea0SLionel Sambuc             **  memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
1114ebfedea0SLionel Sambuc             **  if (krb5_ap_req.data)
1115ebfedea0SLionel Sambuc             **    kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
1116ebfedea0SLionel Sambuc             **
1117ebfedea0SLionel Sambuc             **  Now using real RFC 2712 KerberosWrapper
1118ebfedea0SLionel Sambuc             **  (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
1119ebfedea0SLionel Sambuc             **  Note: 2712 "opaque" types are here replaced
1120ebfedea0SLionel Sambuc             **  with a 2-byte length followed by the value.
1121ebfedea0SLionel Sambuc             **  Example:
1122ebfedea0SLionel Sambuc             **  KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
1123ebfedea0SLionel Sambuc             **  Where "xx xx" = length bytes.  Shown here with
1124ebfedea0SLionel Sambuc             **  optional authenticator omitted.
1125ebfedea0SLionel Sambuc             */
1126ebfedea0SLionel Sambuc 
1127ebfedea0SLionel Sambuc             /*  KerberosWrapper.Ticket              */
1128ebfedea0SLionel Sambuc             s2n(enc_ticket->length, p);
1129ebfedea0SLionel Sambuc             memcpy(p, enc_ticket->data, enc_ticket->length);
1130ebfedea0SLionel Sambuc             p += enc_ticket->length;
1131ebfedea0SLionel Sambuc             n = enc_ticket->length + 2;
1132ebfedea0SLionel Sambuc 
1133ebfedea0SLionel Sambuc             /*  KerberosWrapper.Authenticator       */
1134*0a6a1f1dSLionel Sambuc             if (authp && authp->length) {
1135ebfedea0SLionel Sambuc                 s2n(authp->length, p);
1136ebfedea0SLionel Sambuc                 memcpy(p, authp->data, authp->length);
1137ebfedea0SLionel Sambuc                 p += authp->length;
1138ebfedea0SLionel Sambuc                 n += authp->length + 2;
1139ebfedea0SLionel Sambuc 
1140ebfedea0SLionel Sambuc                 free(authp->data);
1141ebfedea0SLionel Sambuc                 authp->data = NULL;
1142ebfedea0SLionel Sambuc                 authp->length = 0;
1143*0a6a1f1dSLionel Sambuc             } else {
1144ebfedea0SLionel Sambuc                 s2n(0, p);      /* null authenticator length */
1145ebfedea0SLionel Sambuc                 n += 2;
1146ebfedea0SLionel Sambuc             }
1147ebfedea0SLionel Sambuc 
1148ebfedea0SLionel Sambuc             if (RAND_bytes(tmp_buf, sizeof tmp_buf) <= 0)
1149ebfedea0SLionel Sambuc                 goto err;
1150ebfedea0SLionel Sambuc 
1151*0a6a1f1dSLionel Sambuc             /*-
1152*0a6a1f1dSLionel Sambuc              *  20010420 VRS.  Tried it this way; failed.
1153*0a6a1f1dSLionel Sambuc              *      EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
1154*0a6a1f1dSLionel Sambuc              *      EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
1155*0a6a1f1dSLionel Sambuc              *                              kssl_ctx->length);
1156*0a6a1f1dSLionel Sambuc              *      EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
1157ebfedea0SLionel Sambuc              */
1158ebfedea0SLionel Sambuc 
1159ebfedea0SLionel Sambuc             memset(iv, 0, sizeof iv); /* per RFC 1510 */
1160*0a6a1f1dSLionel Sambuc             EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
1161ebfedea0SLionel Sambuc             EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
1162ebfedea0SLionel Sambuc                               sizeof tmp_buf);
1163ebfedea0SLionel Sambuc             EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
1164ebfedea0SLionel Sambuc             outl += padl;
1165*0a6a1f1dSLionel Sambuc             if (outl > (int)sizeof epms) {
1166*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1167*0a6a1f1dSLionel Sambuc                        ERR_R_INTERNAL_ERROR);
1168ebfedea0SLionel Sambuc                 goto err;
1169ebfedea0SLionel Sambuc             }
1170ebfedea0SLionel Sambuc             EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1171ebfedea0SLionel Sambuc 
1172ebfedea0SLionel Sambuc             /*  KerberosWrapper.EncryptedPreMasterSecret    */
1173ebfedea0SLionel Sambuc             s2n(outl, p);
1174ebfedea0SLionel Sambuc             memcpy(p, epms, outl);
1175ebfedea0SLionel Sambuc             p += outl;
1176ebfedea0SLionel Sambuc             n += outl + 2;
1177ebfedea0SLionel Sambuc 
1178ebfedea0SLionel Sambuc             s->session->master_key_length =
1179ebfedea0SLionel Sambuc                 s->method->ssl3_enc->generate_master_secret(s,
1180*0a6a1f1dSLionel Sambuc                                                             s->
1181*0a6a1f1dSLionel Sambuc                                                             session->master_key,
1182*0a6a1f1dSLionel Sambuc                                                             tmp_buf,
1183*0a6a1f1dSLionel Sambuc                                                             sizeof tmp_buf);
1184ebfedea0SLionel Sambuc 
1185ebfedea0SLionel Sambuc             OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
1186ebfedea0SLionel Sambuc             OPENSSL_cleanse(epms, outl);
1187ebfedea0SLionel Sambuc         }
1188ebfedea0SLionel Sambuc #endif
1189ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
1190*0a6a1f1dSLionel Sambuc         else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
1191ebfedea0SLionel Sambuc             DH *dh_srvr, *dh_clnt;
1192ebfedea0SLionel Sambuc 
1193*0a6a1f1dSLionel Sambuc             if (s->session->sess_cert == NULL) {
1194*0a6a1f1dSLionel Sambuc                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1195*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1196*0a6a1f1dSLionel Sambuc                        SSL_R_UNEXPECTED_MESSAGE);
1197*0a6a1f1dSLionel Sambuc                 goto err;
1198*0a6a1f1dSLionel Sambuc             }
1199*0a6a1f1dSLionel Sambuc 
1200ebfedea0SLionel Sambuc             if (s->session->sess_cert->peer_dh_tmp != NULL)
1201ebfedea0SLionel Sambuc                 dh_srvr = s->session->sess_cert->peer_dh_tmp;
1202*0a6a1f1dSLionel Sambuc             else {
1203ebfedea0SLionel Sambuc                 /* we get them from the cert */
1204ebfedea0SLionel Sambuc                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1205*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1206*0a6a1f1dSLionel Sambuc                        SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1207ebfedea0SLionel Sambuc                 goto err;
1208ebfedea0SLionel Sambuc             }
1209ebfedea0SLionel Sambuc 
1210ebfedea0SLionel Sambuc             /* generate a new random key */
1211*0a6a1f1dSLionel Sambuc             if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
1212ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1213ebfedea0SLionel Sambuc                 goto err;
1214ebfedea0SLionel Sambuc             }
1215*0a6a1f1dSLionel Sambuc             if (!DH_generate_key(dh_clnt)) {
1216ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1217ebfedea0SLionel Sambuc                 goto err;
1218ebfedea0SLionel Sambuc             }
1219ebfedea0SLionel Sambuc 
1220*0a6a1f1dSLionel Sambuc             /*
1221*0a6a1f1dSLionel Sambuc              * use the 'p' output buffer for the DH key, but make sure to
1222*0a6a1f1dSLionel Sambuc              * clear it out afterwards
1223*0a6a1f1dSLionel Sambuc              */
1224ebfedea0SLionel Sambuc 
1225ebfedea0SLionel Sambuc             n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
1226ebfedea0SLionel Sambuc 
1227*0a6a1f1dSLionel Sambuc             if (n <= 0) {
1228ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1229ebfedea0SLionel Sambuc                 goto err;
1230ebfedea0SLionel Sambuc             }
1231ebfedea0SLionel Sambuc 
1232ebfedea0SLionel Sambuc             /* generate master key from the result */
1233ebfedea0SLionel Sambuc             s->session->master_key_length =
1234ebfedea0SLionel Sambuc                 s->method->ssl3_enc->generate_master_secret(s,
1235*0a6a1f1dSLionel Sambuc                                                             s->
1236*0a6a1f1dSLionel Sambuc                                                             session->master_key,
1237*0a6a1f1dSLionel Sambuc                                                             p, n);
1238ebfedea0SLionel Sambuc             /* clean up */
1239ebfedea0SLionel Sambuc             memset(p, 0, n);
1240ebfedea0SLionel Sambuc 
1241ebfedea0SLionel Sambuc             /* send off the data */
1242ebfedea0SLionel Sambuc             n = BN_num_bytes(dh_clnt->pub_key);
1243ebfedea0SLionel Sambuc             s2n(n, p);
1244ebfedea0SLionel Sambuc             BN_bn2bin(dh_clnt->pub_key, p);
1245ebfedea0SLionel Sambuc             n += 2;
1246ebfedea0SLionel Sambuc 
1247ebfedea0SLionel Sambuc             DH_free(dh_clnt);
1248ebfedea0SLionel Sambuc 
1249ebfedea0SLionel Sambuc             /* perhaps clean things up a bit EAY EAY EAY EAY */
1250ebfedea0SLionel Sambuc         }
1251ebfedea0SLionel Sambuc #endif
1252ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1253*0a6a1f1dSLionel Sambuc         else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
1254ebfedea0SLionel Sambuc             const EC_GROUP *srvr_group = NULL;
1255ebfedea0SLionel Sambuc             EC_KEY *tkey;
1256ebfedea0SLionel Sambuc             int ecdh_clnt_cert = 0;
1257ebfedea0SLionel Sambuc             int field_size = 0;
1258ebfedea0SLionel Sambuc 
1259*0a6a1f1dSLionel Sambuc             if (s->session->sess_cert == NULL) {
1260*0a6a1f1dSLionel Sambuc                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1261*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1262*0a6a1f1dSLionel Sambuc                        SSL_R_UNEXPECTED_MESSAGE);
1263*0a6a1f1dSLionel Sambuc                 goto err;
1264*0a6a1f1dSLionel Sambuc             }
1265*0a6a1f1dSLionel Sambuc 
1266*0a6a1f1dSLionel Sambuc             /*
1267*0a6a1f1dSLionel Sambuc              * Did we send out the client's ECDH share for use in premaster
1268*0a6a1f1dSLionel Sambuc              * computation as part of client certificate? If so, set
1269*0a6a1f1dSLionel Sambuc              * ecdh_clnt_cert to 1.
1270ebfedea0SLionel Sambuc              */
1271*0a6a1f1dSLionel Sambuc             if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
1272*0a6a1f1dSLionel Sambuc                 /*
1273*0a6a1f1dSLionel Sambuc                  * XXX: For now, we do not support client authentication
1274*0a6a1f1dSLionel Sambuc                  * using ECDH certificates. To add such support, one needs to
1275*0a6a1f1dSLionel Sambuc                  * add code that checks for appropriate conditions and sets
1276*0a6a1f1dSLionel Sambuc                  * ecdh_clnt_cert to 1. For example, the cert have an ECC key
1277*0a6a1f1dSLionel Sambuc                  * on the same curve as the server's and the key should be
1278*0a6a1f1dSLionel Sambuc                  * authorized for key agreement. One also needs to add code
1279*0a6a1f1dSLionel Sambuc                  * in ssl3_connect to skip sending the certificate verify
1280*0a6a1f1dSLionel Sambuc                  * message. if ((s->cert->key->privatekey != NULL) &&
1281*0a6a1f1dSLionel Sambuc                  * (s->cert->key->privatekey->type == EVP_PKEY_EC) && ...)
1282ebfedea0SLionel Sambuc                  * ecdh_clnt_cert = 1;
1283ebfedea0SLionel Sambuc                  */
1284ebfedea0SLionel Sambuc             }
1285ebfedea0SLionel Sambuc 
1286*0a6a1f1dSLionel Sambuc             if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
1287ebfedea0SLionel Sambuc                 tkey = s->session->sess_cert->peer_ecdh_tmp;
1288*0a6a1f1dSLionel Sambuc             } else {
1289ebfedea0SLionel Sambuc                 /* Get the Server Public Key from Cert */
1290*0a6a1f1dSLionel Sambuc                 srvr_pub_pkey =
1291*0a6a1f1dSLionel Sambuc                     X509_get_pubkey(s->session->
1292ebfedea0SLionel Sambuc                                     sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1293*0a6a1f1dSLionel Sambuc                 if ((srvr_pub_pkey == NULL)
1294*0a6a1f1dSLionel Sambuc                     || (srvr_pub_pkey->type != EVP_PKEY_EC)
1295*0a6a1f1dSLionel Sambuc                     || (srvr_pub_pkey->pkey.ec == NULL)) {
1296ebfedea0SLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1297ebfedea0SLionel Sambuc                            ERR_R_INTERNAL_ERROR);
1298ebfedea0SLionel Sambuc                     goto err;
1299ebfedea0SLionel Sambuc                 }
1300ebfedea0SLionel Sambuc 
1301ebfedea0SLionel Sambuc                 tkey = srvr_pub_pkey->pkey.ec;
1302ebfedea0SLionel Sambuc             }
1303ebfedea0SLionel Sambuc 
1304ebfedea0SLionel Sambuc             srvr_group = EC_KEY_get0_group(tkey);
1305ebfedea0SLionel Sambuc             srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1306ebfedea0SLionel Sambuc 
1307*0a6a1f1dSLionel Sambuc             if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
1308ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1309ebfedea0SLionel Sambuc                        ERR_R_INTERNAL_ERROR);
1310ebfedea0SLionel Sambuc                 goto err;
1311ebfedea0SLionel Sambuc             }
1312ebfedea0SLionel Sambuc 
1313*0a6a1f1dSLionel Sambuc             if ((clnt_ecdh = EC_KEY_new()) == NULL) {
1314*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1315*0a6a1f1dSLionel Sambuc                        ERR_R_MALLOC_FAILURE);
1316ebfedea0SLionel Sambuc                 goto err;
1317ebfedea0SLionel Sambuc             }
1318ebfedea0SLionel Sambuc 
1319*0a6a1f1dSLionel Sambuc             if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1320ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
1321ebfedea0SLionel Sambuc                 goto err;
1322ebfedea0SLionel Sambuc             }
1323*0a6a1f1dSLionel Sambuc             if (ecdh_clnt_cert) {
1324*0a6a1f1dSLionel Sambuc                 /*
1325*0a6a1f1dSLionel Sambuc                  * Reuse key info from our certificate We only need our
1326*0a6a1f1dSLionel Sambuc                  * private key to perform the ECDH computation.
1327ebfedea0SLionel Sambuc                  */
1328ebfedea0SLionel Sambuc                 const BIGNUM *priv_key;
1329ebfedea0SLionel Sambuc                 tkey = s->cert->key->privatekey->pkey.ec;
1330ebfedea0SLionel Sambuc                 priv_key = EC_KEY_get0_private_key(tkey);
1331*0a6a1f1dSLionel Sambuc                 if (priv_key == NULL) {
1332*0a6a1f1dSLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1333*0a6a1f1dSLionel Sambuc                            ERR_R_MALLOC_FAILURE);
1334ebfedea0SLionel Sambuc                     goto err;
1335ebfedea0SLionel Sambuc                 }
1336*0a6a1f1dSLionel Sambuc                 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
1337*0a6a1f1dSLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1338*0a6a1f1dSLionel Sambuc                            ERR_R_EC_LIB);
1339ebfedea0SLionel Sambuc                     goto err;
1340ebfedea0SLionel Sambuc                 }
1341*0a6a1f1dSLionel Sambuc             } else {
1342ebfedea0SLionel Sambuc                 /* Generate a new ECDH key pair */
1343*0a6a1f1dSLionel Sambuc                 if (!(EC_KEY_generate_key(clnt_ecdh))) {
1344*0a6a1f1dSLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1345*0a6a1f1dSLionel Sambuc                            ERR_R_ECDH_LIB);
1346ebfedea0SLionel Sambuc                     goto err;
1347ebfedea0SLionel Sambuc                 }
1348ebfedea0SLionel Sambuc             }
1349ebfedea0SLionel Sambuc 
1350*0a6a1f1dSLionel Sambuc             /*
1351*0a6a1f1dSLionel Sambuc              * use the 'p' output buffer for the ECDH key, but make sure to
1352*0a6a1f1dSLionel Sambuc              * clear it out afterwards
1353ebfedea0SLionel Sambuc              */
1354ebfedea0SLionel Sambuc 
1355ebfedea0SLionel Sambuc             field_size = EC_GROUP_get_degree(srvr_group);
1356*0a6a1f1dSLionel Sambuc             if (field_size <= 0) {
1357*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1358ebfedea0SLionel Sambuc                 goto err;
1359ebfedea0SLionel Sambuc             }
1360*0a6a1f1dSLionel Sambuc             n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
1361*0a6a1f1dSLionel Sambuc                                  clnt_ecdh, NULL);
1362*0a6a1f1dSLionel Sambuc             if (n <= 0) {
1363*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1364ebfedea0SLionel Sambuc                 goto err;
1365ebfedea0SLionel Sambuc             }
1366ebfedea0SLionel Sambuc 
1367ebfedea0SLionel Sambuc             /* generate master key from the result */
1368*0a6a1f1dSLionel Sambuc             s->session->master_key_length =
1369*0a6a1f1dSLionel Sambuc                 s->method->ssl3_enc->generate_master_secret(s,
1370*0a6a1f1dSLionel Sambuc                                                             s->
1371*0a6a1f1dSLionel Sambuc                                                             session->master_key,
1372ebfedea0SLionel Sambuc                                                             p, n);
1373ebfedea0SLionel Sambuc 
1374ebfedea0SLionel Sambuc             memset(p, 0, n);    /* clean up */
1375ebfedea0SLionel Sambuc 
1376*0a6a1f1dSLionel Sambuc             if (ecdh_clnt_cert) {
1377ebfedea0SLionel Sambuc                 /* Send empty client key exch message */
1378ebfedea0SLionel Sambuc                 n = 0;
1379*0a6a1f1dSLionel Sambuc             } else {
1380*0a6a1f1dSLionel Sambuc                 /*
1381*0a6a1f1dSLionel Sambuc                  * First check the size of encoding and allocate memory
1382*0a6a1f1dSLionel Sambuc                  * accordingly.
1383ebfedea0SLionel Sambuc                  */
1384ebfedea0SLionel Sambuc                 encoded_pt_len =
1385ebfedea0SLionel Sambuc                     EC_POINT_point2oct(srvr_group,
1386ebfedea0SLionel Sambuc                                        EC_KEY_get0_public_key(clnt_ecdh),
1387ebfedea0SLionel Sambuc                                        POINT_CONVERSION_UNCOMPRESSED,
1388ebfedea0SLionel Sambuc                                        NULL, 0, NULL);
1389ebfedea0SLionel Sambuc 
1390ebfedea0SLionel Sambuc                 encodedPoint = (unsigned char *)
1391*0a6a1f1dSLionel Sambuc                     OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
1392ebfedea0SLionel Sambuc                 bn_ctx = BN_CTX_new();
1393*0a6a1f1dSLionel Sambuc                 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1394*0a6a1f1dSLionel Sambuc                     SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1395*0a6a1f1dSLionel Sambuc                            ERR_R_MALLOC_FAILURE);
1396ebfedea0SLionel Sambuc                     goto err;
1397ebfedea0SLionel Sambuc                 }
1398ebfedea0SLionel Sambuc 
1399ebfedea0SLionel Sambuc                 /* Encode the public key */
1400ebfedea0SLionel Sambuc                 n = EC_POINT_point2oct(srvr_group,
1401ebfedea0SLionel Sambuc                                        EC_KEY_get0_public_key(clnt_ecdh),
1402ebfedea0SLionel Sambuc                                        POINT_CONVERSION_UNCOMPRESSED,
1403ebfedea0SLionel Sambuc                                        encodedPoint, encoded_pt_len, bn_ctx);
1404ebfedea0SLionel Sambuc 
1405ebfedea0SLionel Sambuc                 *p = n;         /* length of encoded point */
1406ebfedea0SLionel Sambuc                 /* Encoded point will be copied here */
1407ebfedea0SLionel Sambuc                 p += 1;
1408ebfedea0SLionel Sambuc                 /* copy the point */
1409ebfedea0SLionel Sambuc                 memcpy((unsigned char *)p, encodedPoint, n);
1410ebfedea0SLionel Sambuc                 /* increment n to account for length field */
1411ebfedea0SLionel Sambuc                 n += 1;
1412ebfedea0SLionel Sambuc             }
1413ebfedea0SLionel Sambuc 
1414ebfedea0SLionel Sambuc             /* Free allocated memory */
1415ebfedea0SLionel Sambuc             BN_CTX_free(bn_ctx);
1416*0a6a1f1dSLionel Sambuc             if (encodedPoint != NULL)
1417*0a6a1f1dSLionel Sambuc                 OPENSSL_free(encodedPoint);
1418ebfedea0SLionel Sambuc             if (clnt_ecdh != NULL)
1419ebfedea0SLionel Sambuc                 EC_KEY_free(clnt_ecdh);
1420ebfedea0SLionel Sambuc             EVP_PKEY_free(srvr_pub_pkey);
1421ebfedea0SLionel Sambuc         }
1422ebfedea0SLionel Sambuc #endif                          /* !OPENSSL_NO_ECDH */
1423ebfedea0SLionel Sambuc 
1424ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
1425*0a6a1f1dSLionel Sambuc         else if (alg_k & SSL_kPSK) {
1426ebfedea0SLionel Sambuc             char identity[PSK_MAX_IDENTITY_LEN];
1427ebfedea0SLionel Sambuc             unsigned char *t = NULL;
1428ebfedea0SLionel Sambuc             unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
1429ebfedea0SLionel Sambuc             unsigned int pre_ms_len = 0, psk_len = 0;
1430ebfedea0SLionel Sambuc             int psk_err = 1;
1431ebfedea0SLionel Sambuc 
1432ebfedea0SLionel Sambuc             n = 0;
1433*0a6a1f1dSLionel Sambuc             if (s->psk_client_callback == NULL) {
1434ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1435ebfedea0SLionel Sambuc                        SSL_R_PSK_NO_CLIENT_CB);
1436ebfedea0SLionel Sambuc                 goto err;
1437ebfedea0SLionel Sambuc             }
1438ebfedea0SLionel Sambuc 
1439ebfedea0SLionel Sambuc             psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
1440ebfedea0SLionel Sambuc                                              identity, PSK_MAX_IDENTITY_LEN,
1441*0a6a1f1dSLionel Sambuc                                              psk_or_pre_ms,
1442*0a6a1f1dSLionel Sambuc                                              sizeof(psk_or_pre_ms));
1443*0a6a1f1dSLionel Sambuc             if (psk_len > PSK_MAX_PSK_LEN) {
1444ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1445ebfedea0SLionel Sambuc                        ERR_R_INTERNAL_ERROR);
1446ebfedea0SLionel Sambuc                 goto psk_err;
1447*0a6a1f1dSLionel Sambuc             } else if (psk_len == 0) {
1448ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1449ebfedea0SLionel Sambuc                        SSL_R_PSK_IDENTITY_NOT_FOUND);
1450ebfedea0SLionel Sambuc                 goto psk_err;
1451ebfedea0SLionel Sambuc             }
1452ebfedea0SLionel Sambuc 
1453ebfedea0SLionel Sambuc             /* create PSK pre_master_secret */
1454ebfedea0SLionel Sambuc             pre_ms_len = 2 + psk_len + 2 + psk_len;
1455ebfedea0SLionel Sambuc             t = psk_or_pre_ms;
1456ebfedea0SLionel Sambuc             memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
1457ebfedea0SLionel Sambuc             s2n(psk_len, t);
1458ebfedea0SLionel Sambuc             memset(t, 0, psk_len);
1459ebfedea0SLionel Sambuc             t += psk_len;
1460ebfedea0SLionel Sambuc             s2n(psk_len, t);
1461ebfedea0SLionel Sambuc 
1462ebfedea0SLionel Sambuc             if (s->session->psk_identity_hint != NULL)
1463ebfedea0SLionel Sambuc                 OPENSSL_free(s->session->psk_identity_hint);
1464*0a6a1f1dSLionel Sambuc             s->session->psk_identity_hint =
1465*0a6a1f1dSLionel Sambuc                 BUF_strdup(s->ctx->psk_identity_hint);
1466*0a6a1f1dSLionel Sambuc             if (s->ctx->psk_identity_hint != NULL
1467*0a6a1f1dSLionel Sambuc                 && s->session->psk_identity_hint == NULL) {
1468ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1469ebfedea0SLionel Sambuc                        ERR_R_MALLOC_FAILURE);
1470ebfedea0SLionel Sambuc                 goto psk_err;
1471ebfedea0SLionel Sambuc             }
1472ebfedea0SLionel Sambuc 
1473ebfedea0SLionel Sambuc             if (s->session->psk_identity != NULL)
1474ebfedea0SLionel Sambuc                 OPENSSL_free(s->session->psk_identity);
1475ebfedea0SLionel Sambuc             s->session->psk_identity = BUF_strdup(identity);
1476*0a6a1f1dSLionel Sambuc             if (s->session->psk_identity == NULL) {
1477ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1478ebfedea0SLionel Sambuc                        ERR_R_MALLOC_FAILURE);
1479ebfedea0SLionel Sambuc                 goto psk_err;
1480ebfedea0SLionel Sambuc             }
1481ebfedea0SLionel Sambuc 
1482ebfedea0SLionel Sambuc             s->session->master_key_length =
1483ebfedea0SLionel Sambuc                 s->method->ssl3_enc->generate_master_secret(s,
1484*0a6a1f1dSLionel Sambuc                                                             s->
1485*0a6a1f1dSLionel Sambuc                                                             session->master_key,
1486*0a6a1f1dSLionel Sambuc                                                             psk_or_pre_ms,
1487*0a6a1f1dSLionel Sambuc                                                             pre_ms_len);
1488ebfedea0SLionel Sambuc             n = strlen(identity);
1489ebfedea0SLionel Sambuc             s2n(n, p);
1490ebfedea0SLionel Sambuc             memcpy(p, identity, n);
1491ebfedea0SLionel Sambuc             n += 2;
1492ebfedea0SLionel Sambuc             psk_err = 0;
1493ebfedea0SLionel Sambuc  psk_err:
1494ebfedea0SLionel Sambuc             OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
1495ebfedea0SLionel Sambuc             OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
1496*0a6a1f1dSLionel Sambuc             if (psk_err != 0) {
1497ebfedea0SLionel Sambuc                 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1498ebfedea0SLionel Sambuc                 goto err;
1499ebfedea0SLionel Sambuc             }
1500ebfedea0SLionel Sambuc         }
1501ebfedea0SLionel Sambuc #endif
1502*0a6a1f1dSLionel Sambuc         else {
1503ebfedea0SLionel Sambuc             ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1504*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1505*0a6a1f1dSLionel Sambuc                    ERR_R_INTERNAL_ERROR);
1506ebfedea0SLionel Sambuc             goto err;
1507ebfedea0SLionel Sambuc         }
1508ebfedea0SLionel Sambuc 
1509ebfedea0SLionel Sambuc         d = dtls1_set_message_header(s, d,
1510ebfedea0SLionel Sambuc                                      SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n);
1511*0a6a1f1dSLionel Sambuc         /*-
1512ebfedea0SLionel Sambuc          *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1513ebfedea0SLionel Sambuc          l2n3(n,d);
1514ebfedea0SLionel Sambuc          l2n(s->d1->handshake_write_seq,d);
1515ebfedea0SLionel Sambuc          s->d1->handshake_write_seq++;
1516ebfedea0SLionel Sambuc         */
1517ebfedea0SLionel Sambuc 
1518ebfedea0SLionel Sambuc         s->state = SSL3_ST_CW_KEY_EXCH_B;
1519ebfedea0SLionel Sambuc         /* number of bytes to write */
1520ebfedea0SLionel Sambuc         s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1521ebfedea0SLionel Sambuc         s->init_off = 0;
1522ebfedea0SLionel Sambuc 
1523ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1524ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
1525ebfedea0SLionel Sambuc     }
1526ebfedea0SLionel Sambuc 
1527ebfedea0SLionel Sambuc     /* SSL3_ST_CW_KEY_EXCH_B */
1528ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1529ebfedea0SLionel Sambuc  err:
1530ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1531ebfedea0SLionel Sambuc     BN_CTX_free(bn_ctx);
1532*0a6a1f1dSLionel Sambuc     if (encodedPoint != NULL)
1533*0a6a1f1dSLionel Sambuc         OPENSSL_free(encodedPoint);
1534ebfedea0SLionel Sambuc     if (clnt_ecdh != NULL)
1535ebfedea0SLionel Sambuc         EC_KEY_free(clnt_ecdh);
1536ebfedea0SLionel Sambuc     EVP_PKEY_free(srvr_pub_pkey);
1537ebfedea0SLionel Sambuc #endif
1538ebfedea0SLionel Sambuc     return (-1);
1539ebfedea0SLionel Sambuc }
1540ebfedea0SLionel Sambuc 
dtls1_send_client_verify(SSL * s)1541ebfedea0SLionel Sambuc int dtls1_send_client_verify(SSL *s)
1542ebfedea0SLionel Sambuc {
1543ebfedea0SLionel Sambuc     unsigned char *p, *d;
1544ebfedea0SLionel Sambuc     unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1545ebfedea0SLionel Sambuc     EVP_PKEY *pkey;
1546ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1547ebfedea0SLionel Sambuc     unsigned u = 0;
1548ebfedea0SLionel Sambuc #endif
1549ebfedea0SLionel Sambuc     unsigned long n;
1550ebfedea0SLionel Sambuc #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
1551ebfedea0SLionel Sambuc     int j;
1552ebfedea0SLionel Sambuc #endif
1553ebfedea0SLionel Sambuc 
1554*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
1555ebfedea0SLionel Sambuc         d = (unsigned char *)s->init_buf->data;
1556ebfedea0SLionel Sambuc         p = &(d[DTLS1_HM_HEADER_LENGTH]);
1557ebfedea0SLionel Sambuc         pkey = s->cert->key->privatekey;
1558ebfedea0SLionel Sambuc 
1559ebfedea0SLionel Sambuc         s->method->ssl3_enc->cert_verify_mac(s,
1560ebfedea0SLionel Sambuc                                              NID_sha1,
1561ebfedea0SLionel Sambuc                                              &(data[MD5_DIGEST_LENGTH]));
1562ebfedea0SLionel Sambuc 
1563ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1564*0a6a1f1dSLionel Sambuc         if (pkey->type == EVP_PKEY_RSA) {
1565*0a6a1f1dSLionel Sambuc             s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
1566ebfedea0SLionel Sambuc             if (RSA_sign(NID_md5_sha1, data,
1567ebfedea0SLionel Sambuc                          MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
1568*0a6a1f1dSLionel Sambuc                          &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1569ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
1570ebfedea0SLionel Sambuc                 goto err;
1571ebfedea0SLionel Sambuc             }
1572ebfedea0SLionel Sambuc             s2n(u, p);
1573ebfedea0SLionel Sambuc             n = u + 2;
1574*0a6a1f1dSLionel Sambuc         } else
1575ebfedea0SLionel Sambuc #endif
1576ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA
1577*0a6a1f1dSLionel Sambuc         if (pkey->type == EVP_PKEY_DSA) {
1578ebfedea0SLionel Sambuc             if (!DSA_sign(pkey->save_type,
1579ebfedea0SLionel Sambuc                           &(data[MD5_DIGEST_LENGTH]),
1580ebfedea0SLionel Sambuc                           SHA_DIGEST_LENGTH, &(p[2]),
1581*0a6a1f1dSLionel Sambuc                           (unsigned int *)&j, pkey->pkey.dsa)) {
1582ebfedea0SLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
1583ebfedea0SLionel Sambuc                 goto err;
1584ebfedea0SLionel Sambuc             }
1585ebfedea0SLionel Sambuc             s2n(j, p);
1586ebfedea0SLionel Sambuc             n = j + 2;
1587*0a6a1f1dSLionel Sambuc         } else
1588ebfedea0SLionel Sambuc #endif
1589ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDSA
1590*0a6a1f1dSLionel Sambuc         if (pkey->type == EVP_PKEY_EC) {
1591ebfedea0SLionel Sambuc             if (!ECDSA_sign(pkey->save_type,
1592ebfedea0SLionel Sambuc                             &(data[MD5_DIGEST_LENGTH]),
1593ebfedea0SLionel Sambuc                             SHA_DIGEST_LENGTH, &(p[2]),
1594*0a6a1f1dSLionel Sambuc                             (unsigned int *)&j, pkey->pkey.ec)) {
1595*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
1596ebfedea0SLionel Sambuc                 goto err;
1597ebfedea0SLionel Sambuc             }
1598ebfedea0SLionel Sambuc             s2n(j, p);
1599ebfedea0SLionel Sambuc             n = j + 2;
1600*0a6a1f1dSLionel Sambuc         } else
1601ebfedea0SLionel Sambuc #endif
1602ebfedea0SLionel Sambuc         {
1603ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
1604ebfedea0SLionel Sambuc             goto err;
1605ebfedea0SLionel Sambuc         }
1606ebfedea0SLionel Sambuc 
1607ebfedea0SLionel Sambuc         d = dtls1_set_message_header(s, d,
1608ebfedea0SLionel Sambuc                                      SSL3_MT_CERTIFICATE_VERIFY, n, 0, n);
1609ebfedea0SLionel Sambuc 
1610ebfedea0SLionel Sambuc         s->init_num = (int)n + DTLS1_HM_HEADER_LENGTH;
1611ebfedea0SLionel Sambuc         s->init_off = 0;
1612ebfedea0SLionel Sambuc 
1613ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1614ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
1615ebfedea0SLionel Sambuc 
1616ebfedea0SLionel Sambuc         s->state = SSL3_ST_CW_CERT_VRFY_B;
1617ebfedea0SLionel Sambuc     }
1618ebfedea0SLionel Sambuc 
1619ebfedea0SLionel Sambuc     /* s->state = SSL3_ST_CW_CERT_VRFY_B */
1620ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1621ebfedea0SLionel Sambuc  err:
1622ebfedea0SLionel Sambuc     return (-1);
1623ebfedea0SLionel Sambuc }
1624ebfedea0SLionel Sambuc 
dtls1_send_client_certificate(SSL * s)1625ebfedea0SLionel Sambuc int dtls1_send_client_certificate(SSL *s)
1626ebfedea0SLionel Sambuc {
1627ebfedea0SLionel Sambuc     X509 *x509 = NULL;
1628ebfedea0SLionel Sambuc     EVP_PKEY *pkey = NULL;
1629ebfedea0SLionel Sambuc     int i;
1630ebfedea0SLionel Sambuc     unsigned long l;
1631ebfedea0SLionel Sambuc 
1632*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_CW_CERT_A) {
1633ebfedea0SLionel Sambuc         if ((s->cert == NULL) ||
1634ebfedea0SLionel Sambuc             (s->cert->key->x509 == NULL) ||
1635ebfedea0SLionel Sambuc             (s->cert->key->privatekey == NULL))
1636ebfedea0SLionel Sambuc             s->state = SSL3_ST_CW_CERT_B;
1637ebfedea0SLionel Sambuc         else
1638ebfedea0SLionel Sambuc             s->state = SSL3_ST_CW_CERT_C;
1639ebfedea0SLionel Sambuc     }
1640ebfedea0SLionel Sambuc 
1641ebfedea0SLionel Sambuc     /* We need to get a client cert */
1642*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_CW_CERT_B) {
1643*0a6a1f1dSLionel Sambuc         /*
1644*0a6a1f1dSLionel Sambuc          * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
1645*0a6a1f1dSLionel Sambuc          * return(-1); We then get retied later
1646*0a6a1f1dSLionel Sambuc          */
1647ebfedea0SLionel Sambuc         i = 0;
1648ebfedea0SLionel Sambuc         i = ssl_do_client_cert_cb(s, &x509, &pkey);
1649*0a6a1f1dSLionel Sambuc         if (i < 0) {
1650ebfedea0SLionel Sambuc             s->rwstate = SSL_X509_LOOKUP;
1651ebfedea0SLionel Sambuc             return (-1);
1652ebfedea0SLionel Sambuc         }
1653ebfedea0SLionel Sambuc         s->rwstate = SSL_NOTHING;
1654*0a6a1f1dSLionel Sambuc         if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
1655ebfedea0SLionel Sambuc             s->state = SSL3_ST_CW_CERT_B;
1656*0a6a1f1dSLionel Sambuc             if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
1657ebfedea0SLionel Sambuc                 i = 0;
1658*0a6a1f1dSLionel Sambuc         } else if (i == 1) {
1659ebfedea0SLionel Sambuc             i = 0;
1660*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,
1661*0a6a1f1dSLionel Sambuc                    SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1662ebfedea0SLionel Sambuc         }
1663ebfedea0SLionel Sambuc 
1664*0a6a1f1dSLionel Sambuc         if (x509 != NULL)
1665*0a6a1f1dSLionel Sambuc             X509_free(x509);
1666*0a6a1f1dSLionel Sambuc         if (pkey != NULL)
1667*0a6a1f1dSLionel Sambuc             EVP_PKEY_free(pkey);
1668*0a6a1f1dSLionel Sambuc         if (i == 0) {
1669*0a6a1f1dSLionel Sambuc             if (s->version == SSL3_VERSION) {
1670ebfedea0SLionel Sambuc                 s->s3->tmp.cert_req = 0;
1671ebfedea0SLionel Sambuc                 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1672ebfedea0SLionel Sambuc                 return (1);
1673*0a6a1f1dSLionel Sambuc             } else {
1674ebfedea0SLionel Sambuc                 s->s3->tmp.cert_req = 2;
1675ebfedea0SLionel Sambuc             }
1676ebfedea0SLionel Sambuc         }
1677ebfedea0SLionel Sambuc 
1678ebfedea0SLionel Sambuc         /* Ok, we have a cert */
1679ebfedea0SLionel Sambuc         s->state = SSL3_ST_CW_CERT_C;
1680ebfedea0SLionel Sambuc     }
1681ebfedea0SLionel Sambuc 
1682*0a6a1f1dSLionel Sambuc     if (s->state == SSL3_ST_CW_CERT_C) {
1683ebfedea0SLionel Sambuc         s->state = SSL3_ST_CW_CERT_D;
1684ebfedea0SLionel Sambuc         l = dtls1_output_cert_chain(s,
1685*0a6a1f1dSLionel Sambuc                                     (s->s3->tmp.cert_req ==
1686*0a6a1f1dSLionel Sambuc                                      2) ? NULL : s->cert->key->x509);
1687*0a6a1f1dSLionel Sambuc         if (!l) {
1688*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1689*0a6a1f1dSLionel Sambuc             ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1690*0a6a1f1dSLionel Sambuc             return 0;
1691*0a6a1f1dSLionel Sambuc         }
1692ebfedea0SLionel Sambuc         s->init_num = (int)l;
1693ebfedea0SLionel Sambuc         s->init_off = 0;
1694ebfedea0SLionel Sambuc 
1695ebfedea0SLionel Sambuc         /* set header called by dtls1_output_cert_chain() */
1696ebfedea0SLionel Sambuc 
1697ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1698ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
1699ebfedea0SLionel Sambuc     }
1700ebfedea0SLionel Sambuc     /* SSL3_ST_CW_CERT_D */
1701ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1702ebfedea0SLionel Sambuc }
1703