]>
Commit | Line | Data |
---|---|---|
36d16f8e BL |
1 | /* ssl/d1_clnt.c */ |
2 | /* | |
3 | * DTLS implementation written by Nagendra Modadugu | |
4 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. | |
5 | */ | |
6 | /* ==================================================================== | |
52b8dad8 | 7 | * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. |
36d16f8e BL |
8 | * |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * | |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * openssl-core@OpenSSL.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | |
60 | * All rights reserved. | |
61 | * | |
62 | * This package is an SSL implementation written | |
63 | * by Eric Young (eay@cryptsoft.com). | |
64 | * The implementation was written so as to conform with Netscapes SSL. | |
65 | * | |
66 | * This library is free for commercial and non-commercial use as long as | |
67 | * the following conditions are aheared to. The following conditions | |
68 | * apply to all code found in this distribution, be it the RC4, RSA, | |
69 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
70 | * included with this distribution is covered by the same copyright terms | |
71 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
72 | * | |
73 | * Copyright remains Eric Young's, and as such any Copyright notices in | |
74 | * the code are not to be removed. | |
75 | * If this package is used in a product, Eric Young should be given attribution | |
76 | * as the author of the parts of the library used. | |
77 | * This can be in the form of a textual message at program startup or | |
78 | * in documentation (online or textual) provided with the package. | |
79 | * | |
80 | * Redistribution and use in source and binary forms, with or without | |
81 | * modification, are permitted provided that the following conditions | |
82 | * are met: | |
83 | * 1. Redistributions of source code must retain the copyright | |
84 | * notice, this list of conditions and the following disclaimer. | |
85 | * 2. Redistributions in binary form must reproduce the above copyright | |
86 | * notice, this list of conditions and the following disclaimer in the | |
87 | * documentation and/or other materials provided with the distribution. | |
88 | * 3. All advertising materials mentioning features or use of this software | |
89 | * must display the following acknowledgement: | |
90 | * "This product includes cryptographic software written by | |
91 | * Eric Young (eay@cryptsoft.com)" | |
92 | * The word 'cryptographic' can be left out if the rouines from the library | |
93 | * being used are not cryptographic related :-). | |
94 | * 4. If you include any Windows specific code (or a derivative thereof) from | |
95 | * the apps directory (application code) you must include an acknowledgement: | |
96 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
97 | * | |
98 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | |
99 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
100 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
101 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
102 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
103 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
104 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
105 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
106 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
107 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
108 | * SUCH DAMAGE. | |
109 | * | |
110 | * The licence and distribution terms for any publically available version or | |
111 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
112 | * copied and put under another distribution licence | |
113 | * [including the GNU Public Licence.] | |
114 | */ | |
115 | ||
116 | #include <stdio.h> | |
117 | #include "ssl_locl.h" | |
ef236ec3 DSH |
118 | #ifndef OPENSSL_NO_KRB5 |
119 | #include "kssl_lcl.h" | |
120 | #endif | |
36d16f8e BL |
121 | #include <openssl/buffer.h> |
122 | #include <openssl/rand.h> | |
123 | #include <openssl/objects.h> | |
124 | #include <openssl/evp.h> | |
125 | #include <openssl/md5.h> | |
1e26a8ba | 126 | #include <openssl/bn.h> |
3eeaab4b NL |
127 | #ifndef OPENSSL_NO_DH |
128 | #include <openssl/dh.h> | |
129 | #endif | |
36d16f8e | 130 | |
4ebb342f | 131 | static const SSL_METHOD *dtls1_get_client_method(int ver); |
36d16f8e BL |
132 | static int dtls1_get_hello_verify(SSL *s); |
133 | ||
4ebb342f | 134 | static const SSL_METHOD *dtls1_get_client_method(int ver) |
36d16f8e | 135 | { |
8711efb4 | 136 | if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER) |
36d16f8e BL |
137 | return(DTLSv1_client_method()); |
138 | else | |
139 | return(NULL); | |
140 | } | |
141 | ||
f3b656b2 DSH |
142 | IMPLEMENT_dtls1_meth_func(DTLSv1_client_method, |
143 | ssl_undefined_function, | |
144 | dtls1_connect, | |
145 | dtls1_get_client_method) | |
36d16f8e BL |
146 | |
147 | int dtls1_connect(SSL *s) | |
148 | { | |
149 | BUF_MEM *buf=NULL; | |
7e69565f | 150 | unsigned long Time=(unsigned long)time(NULL); |
36d16f8e BL |
151 | long num1; |
152 | void (*cb)(const SSL *ssl,int type,int val)=NULL; | |
153 | int ret= -1; | |
154 | int new_state,state,skip=0;; | |
155 | ||
156 | RAND_add(&Time,sizeof(Time),0); | |
157 | ERR_clear_error(); | |
158 | clear_sys_error(); | |
159 | ||
160 | if (s->info_callback != NULL) | |
161 | cb=s->info_callback; | |
162 | else if (s->ctx->info_callback != NULL) | |
163 | cb=s->ctx->info_callback; | |
164 | ||
165 | s->in_handshake++; | |
166 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); | |
167 | ||
168 | for (;;) | |
169 | { | |
170 | state=s->state; | |
171 | ||
172 | switch(s->state) | |
173 | { | |
174 | case SSL_ST_RENEGOTIATE: | |
175 | s->new_session=1; | |
176 | s->state=SSL_ST_CONNECT; | |
177 | s->ctx->stats.sess_connect_renegotiate++; | |
178 | /* break */ | |
179 | case SSL_ST_BEFORE: | |
180 | case SSL_ST_CONNECT: | |
181 | case SSL_ST_BEFORE|SSL_ST_CONNECT: | |
182 | case SSL_ST_OK|SSL_ST_CONNECT: | |
183 | ||
184 | s->server=0; | |
185 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); | |
186 | ||
8711efb4 DSH |
187 | if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) && |
188 | (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00)) | |
36d16f8e | 189 | { |
beb056b3 | 190 | SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
191 | ret = -1; |
192 | goto end; | |
193 | } | |
194 | ||
195 | /* s->version=SSL3_VERSION; */ | |
196 | s->type=SSL_ST_CONNECT; | |
197 | ||
198 | if (s->init_buf == NULL) | |
199 | { | |
200 | if ((buf=BUF_MEM_new()) == NULL) | |
201 | { | |
202 | ret= -1; | |
203 | goto end; | |
204 | } | |
205 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) | |
206 | { | |
207 | ret= -1; | |
208 | goto end; | |
209 | } | |
210 | s->init_buf=buf; | |
211 | buf=NULL; | |
212 | } | |
213 | ||
214 | if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } | |
215 | ||
216 | /* setup buffing BIO */ | |
217 | if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; } | |
218 | ||
219 | /* don't push the buffering BIO quite yet */ | |
220 | ||
36d16f8e BL |
221 | s->state=SSL3_ST_CW_CLNT_HELLO_A; |
222 | s->ctx->stats.sess_connect++; | |
223 | s->init_num=0; | |
e7adda52 AP |
224 | /* mark client_random uninitialized */ |
225 | memset(s->s3->client_random,0,sizeof(s->s3->client_random)); | |
36d16f8e BL |
226 | break; |
227 | ||
228 | case SSL3_ST_CW_CLNT_HELLO_A: | |
229 | case SSL3_ST_CW_CLNT_HELLO_B: | |
230 | ||
231 | s->shutdown=0; | |
0d97d00b AP |
232 | |
233 | /* every DTLS ClientHello resets Finished MAC */ | |
234 | ssl3_init_finished_mac(s); | |
235 | ||
eb38b26d | 236 | dtls1_start_timer(s); |
36d16f8e BL |
237 | ret=dtls1_client_hello(s); |
238 | if (ret <= 0) goto end; | |
239 | ||
240 | if ( s->d1->send_cookie) | |
241 | { | |
242 | s->state=SSL3_ST_CW_FLUSH; | |
243 | s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A; | |
244 | } | |
245 | else | |
246 | s->state=SSL3_ST_CR_SRVR_HELLO_A; | |
247 | ||
248 | s->init_num=0; | |
249 | ||
250 | /* turn on buffering for the next lot of output */ | |
251 | if (s->bbio != s->wbio) | |
252 | s->wbio=BIO_push(s->bbio,s->wbio); | |
253 | ||
254 | break; | |
255 | ||
256 | case SSL3_ST_CR_SRVR_HELLO_A: | |
257 | case SSL3_ST_CR_SRVR_HELLO_B: | |
258 | ret=ssl3_get_server_hello(s); | |
259 | if (ret <= 0) goto end; | |
260 | else | |
261 | { | |
eb38b26d | 262 | dtls1_stop_timer(s); |
36d16f8e BL |
263 | if (s->hit) |
264 | s->state=SSL3_ST_CR_FINISHED_A; | |
265 | else | |
266 | s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; | |
267 | } | |
268 | s->init_num=0; | |
269 | break; | |
270 | ||
271 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: | |
272 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B: | |
273 | ||
274 | ret = dtls1_get_hello_verify(s); | |
275 | if ( ret <= 0) | |
276 | goto end; | |
eb38b26d | 277 | dtls1_stop_timer(s); |
36d16f8e BL |
278 | if ( s->d1->send_cookie) /* start again, with a cookie */ |
279 | s->state=SSL3_ST_CW_CLNT_HELLO_A; | |
280 | else | |
281 | s->state = SSL3_ST_CR_CERT_A; | |
282 | s->init_num = 0; | |
283 | break; | |
284 | ||
285 | case SSL3_ST_CR_CERT_A: | |
286 | case SSL3_ST_CR_CERT_B: | |
287 | /* Check if it is anon DH */ | |
52b8dad8 | 288 | if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) |
36d16f8e BL |
289 | { |
290 | ret=ssl3_get_server_certificate(s); | |
291 | if (ret <= 0) goto end; | |
292 | } | |
293 | else | |
294 | skip=1; | |
295 | s->state=SSL3_ST_CR_KEY_EXCH_A; | |
296 | s->init_num=0; | |
297 | break; | |
298 | ||
299 | case SSL3_ST_CR_KEY_EXCH_A: | |
300 | case SSL3_ST_CR_KEY_EXCH_B: | |
301 | ret=ssl3_get_key_exchange(s); | |
302 | if (ret <= 0) goto end; | |
303 | s->state=SSL3_ST_CR_CERT_REQ_A; | |
304 | s->init_num=0; | |
305 | ||
306 | /* at this point we check that we have the | |
307 | * required stuff from the server */ | |
308 | if (!ssl3_check_cert_and_algorithm(s)) | |
309 | { | |
310 | ret= -1; | |
311 | goto end; | |
312 | } | |
313 | break; | |
314 | ||
315 | case SSL3_ST_CR_CERT_REQ_A: | |
316 | case SSL3_ST_CR_CERT_REQ_B: | |
317 | ret=ssl3_get_certificate_request(s); | |
318 | if (ret <= 0) goto end; | |
319 | s->state=SSL3_ST_CR_SRVR_DONE_A; | |
320 | s->init_num=0; | |
321 | break; | |
322 | ||
323 | case SSL3_ST_CR_SRVR_DONE_A: | |
324 | case SSL3_ST_CR_SRVR_DONE_B: | |
325 | ret=ssl3_get_server_done(s); | |
326 | if (ret <= 0) goto end; | |
327 | if (s->s3->tmp.cert_req) | |
328 | s->state=SSL3_ST_CW_CERT_A; | |
329 | else | |
330 | s->state=SSL3_ST_CW_KEY_EXCH_A; | |
331 | s->init_num=0; | |
332 | ||
333 | break; | |
334 | ||
335 | case SSL3_ST_CW_CERT_A: | |
336 | case SSL3_ST_CW_CERT_B: | |
337 | case SSL3_ST_CW_CERT_C: | |
338 | case SSL3_ST_CW_CERT_D: | |
eb38b26d | 339 | dtls1_start_timer(s); |
36d16f8e BL |
340 | ret=dtls1_send_client_certificate(s); |
341 | if (ret <= 0) goto end; | |
342 | s->state=SSL3_ST_CW_KEY_EXCH_A; | |
343 | s->init_num=0; | |
344 | break; | |
345 | ||
346 | case SSL3_ST_CW_KEY_EXCH_A: | |
347 | case SSL3_ST_CW_KEY_EXCH_B: | |
eb38b26d | 348 | dtls1_start_timer(s); |
36d16f8e BL |
349 | ret=dtls1_send_client_key_exchange(s); |
350 | if (ret <= 0) goto end; | |
36d16f8e BL |
351 | /* EAY EAY EAY need to check for DH fix cert |
352 | * sent back */ | |
353 | /* For TLS, cert_req is set to 2, so a cert chain | |
354 | * of nothing is sent, but no verify packet is sent */ | |
355 | if (s->s3->tmp.cert_req == 1) | |
356 | { | |
357 | s->state=SSL3_ST_CW_CERT_VRFY_A; | |
358 | } | |
359 | else | |
360 | { | |
361 | s->state=SSL3_ST_CW_CHANGE_A; | |
362 | s->s3->change_cipher_spec=0; | |
363 | } | |
364 | ||
365 | s->init_num=0; | |
366 | break; | |
367 | ||
368 | case SSL3_ST_CW_CERT_VRFY_A: | |
369 | case SSL3_ST_CW_CERT_VRFY_B: | |
eb38b26d | 370 | dtls1_start_timer(s); |
36d16f8e BL |
371 | ret=dtls1_send_client_verify(s); |
372 | if (ret <= 0) goto end; | |
373 | s->state=SSL3_ST_CW_CHANGE_A; | |
374 | s->init_num=0; | |
375 | s->s3->change_cipher_spec=0; | |
376 | break; | |
377 | ||
378 | case SSL3_ST_CW_CHANGE_A: | |
379 | case SSL3_ST_CW_CHANGE_B: | |
eb38b26d | 380 | dtls1_start_timer(s); |
36d16f8e BL |
381 | ret=dtls1_send_change_cipher_spec(s, |
382 | SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); | |
383 | if (ret <= 0) goto end; | |
384 | s->state=SSL3_ST_CW_FINISHED_A; | |
385 | s->init_num=0; | |
386 | ||
387 | s->session->cipher=s->s3->tmp.new_cipher; | |
09b6c2ef DSH |
388 | #ifdef OPENSSL_NO_COMP |
389 | s->session->compress_meth=0; | |
390 | #else | |
36d16f8e BL |
391 | if (s->s3->tmp.new_compression == NULL) |
392 | s->session->compress_meth=0; | |
393 | else | |
394 | s->session->compress_meth= | |
395 | s->s3->tmp.new_compression->id; | |
09b6c2ef | 396 | #endif |
36d16f8e BL |
397 | if (!s->method->ssl3_enc->setup_key_block(s)) |
398 | { | |
399 | ret= -1; | |
400 | goto end; | |
401 | } | |
402 | ||
403 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
404 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) | |
405 | { | |
406 | ret= -1; | |
407 | goto end; | |
408 | } | |
409 | ||
410 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | |
411 | break; | |
412 | ||
413 | case SSL3_ST_CW_FINISHED_A: | |
414 | case SSL3_ST_CW_FINISHED_B: | |
eb38b26d | 415 | dtls1_start_timer(s); |
36d16f8e BL |
416 | ret=dtls1_send_finished(s, |
417 | SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, | |
418 | s->method->ssl3_enc->client_finished_label, | |
419 | s->method->ssl3_enc->client_finished_label_len); | |
420 | if (ret <= 0) goto end; | |
421 | s->state=SSL3_ST_CW_FLUSH; | |
422 | ||
423 | /* clear flags */ | |
424 | s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER; | |
425 | if (s->hit) | |
426 | { | |
427 | s->s3->tmp.next_state=SSL_ST_OK; | |
428 | if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) | |
429 | { | |
430 | s->state=SSL_ST_OK; | |
431 | s->s3->flags|=SSL3_FLAGS_POP_BUFFER; | |
432 | s->s3->delay_buf_pop_ret=0; | |
433 | } | |
434 | } | |
435 | else | |
436 | { | |
437 | s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; | |
438 | } | |
439 | s->init_num=0; | |
440 | break; | |
441 | ||
442 | case SSL3_ST_CR_FINISHED_A: | |
443 | case SSL3_ST_CR_FINISHED_B: | |
444 | ||
445 | ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, | |
446 | SSL3_ST_CR_FINISHED_B); | |
447 | if (ret <= 0) goto end; | |
eb38b26d | 448 | dtls1_stop_timer(s); |
36d16f8e BL |
449 | |
450 | if (s->hit) | |
451 | s->state=SSL3_ST_CW_CHANGE_A; | |
452 | else | |
453 | s->state=SSL_ST_OK; | |
454 | s->init_num=0; | |
455 | break; | |
456 | ||
457 | case SSL3_ST_CW_FLUSH: | |
458 | /* number of bytes to be flushed */ | |
459 | num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); | |
460 | if (num1 > 0) | |
461 | { | |
462 | s->rwstate=SSL_WRITING; | |
463 | num1=BIO_flush(s->wbio); | |
464 | if (num1 <= 0) { ret= -1; goto end; } | |
465 | s->rwstate=SSL_NOTHING; | |
466 | } | |
467 | ||
468 | s->state=s->s3->tmp.next_state; | |
469 | break; | |
470 | ||
471 | case SSL_ST_OK: | |
472 | /* clean a few things up */ | |
473 | ssl3_cleanup_key_block(s); | |
474 | ||
475 | #if 0 | |
476 | if (s->init_buf != NULL) | |
477 | { | |
478 | BUF_MEM_free(s->init_buf); | |
479 | s->init_buf=NULL; | |
480 | } | |
481 | #endif | |
482 | ||
483 | /* If we are not 'joining' the last two packets, | |
484 | * remove the buffering now */ | |
485 | if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) | |
486 | ssl_free_wbio_buffer(s); | |
487 | /* else do it later in ssl3_write */ | |
488 | ||
489 | s->init_num=0; | |
490 | s->new_session=0; | |
491 | ||
492 | ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); | |
493 | if (s->hit) s->ctx->stats.sess_hit++; | |
494 | ||
495 | ret=1; | |
496 | /* s->server=0; */ | |
497 | s->handshake_func=dtls1_connect; | |
498 | s->ctx->stats.sess_connect_good++; | |
499 | ||
500 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); | |
501 | ||
502 | /* done with handshaking */ | |
503 | s->d1->handshake_read_seq = 0; | |
e5fa864f | 504 | s->d1->next_handshake_write_seq = 0; |
36d16f8e BL |
505 | goto end; |
506 | /* break; */ | |
507 | ||
508 | default: | |
beb056b3 | 509 | SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE); |
36d16f8e BL |
510 | ret= -1; |
511 | goto end; | |
512 | /* break; */ | |
513 | } | |
514 | ||
515 | /* did we do anything */ | |
516 | if (!s->s3->tmp.reuse_message && !skip) | |
517 | { | |
518 | if (s->debug) | |
519 | { | |
520 | if ((ret=BIO_flush(s->wbio)) <= 0) | |
521 | goto end; | |
522 | } | |
523 | ||
524 | if ((cb != NULL) && (s->state != state)) | |
525 | { | |
526 | new_state=s->state; | |
527 | s->state=state; | |
528 | cb(s,SSL_CB_CONNECT_LOOP,1); | |
529 | s->state=new_state; | |
530 | } | |
531 | } | |
532 | skip=0; | |
533 | } | |
534 | end: | |
535 | s->in_handshake--; | |
536 | if (buf != NULL) | |
537 | BUF_MEM_free(buf); | |
538 | if (cb != NULL) | |
539 | cb(s,SSL_CB_CONNECT_EXIT,ret); | |
540 | return(ret); | |
541 | } | |
542 | ||
543 | int dtls1_client_hello(SSL *s) | |
544 | { | |
545 | unsigned char *buf; | |
546 | unsigned char *p,*d; | |
20a90e3a | 547 | unsigned int i,j; |
36d16f8e BL |
548 | unsigned long Time,l; |
549 | SSL_COMP *comp; | |
550 | ||
551 | buf=(unsigned char *)s->init_buf->data; | |
552 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) | |
553 | { | |
554 | if ((s->session == NULL) || | |
555 | (s->session->ssl_version != s->version) || | |
556 | (s->session->not_resumable)) | |
557 | { | |
558 | if (!ssl_get_new_session(s,0)) | |
559 | goto err; | |
560 | } | |
561 | /* else use the pre-loaded session */ | |
562 | ||
563 | p=s->s3->client_random; | |
e7adda52 AP |
564 | |
565 | /* if client_random is initialized, reuse it, we are | |
566 | * required to use same upon reply to HelloVerify */ | |
567 | for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ; | |
568 | if (i==sizeof(s->s3->client_random)) | |
569 | { | |
570 | Time=(unsigned long)time(NULL); /* Time */ | |
571 | l2n(Time,p); | |
572 | RAND_pseudo_bytes(p,sizeof(s->s3->client_random)-4); | |
573 | } | |
36d16f8e BL |
574 | |
575 | /* Do the message type and length last */ | |
576 | d=p= &(buf[DTLS1_HM_HEADER_LENGTH]); | |
577 | ||
578 | *(p++)=s->version>>8; | |
579 | *(p++)=s->version&0xff; | |
580 | s->client_version=s->version; | |
581 | ||
582 | /* Random stuff */ | |
583 | memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); | |
584 | p+=SSL3_RANDOM_SIZE; | |
585 | ||
586 | /* Session ID */ | |
587 | if (s->new_session) | |
588 | i=0; | |
589 | else | |
590 | i=s->session->session_id_length; | |
591 | *(p++)=i; | |
592 | if (i != 0) | |
593 | { | |
594 | if (i > sizeof s->session->session_id) | |
595 | { | |
beb056b3 | 596 | SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
597 | goto err; |
598 | } | |
599 | memcpy(p,s->session->session_id,i); | |
600 | p+=i; | |
601 | } | |
602 | ||
603 | /* cookie stuff */ | |
604 | if ( s->d1->cookie_len > sizeof(s->d1->cookie)) | |
605 | { | |
beb056b3 | 606 | SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
607 | goto err; |
608 | } | |
609 | *(p++) = s->d1->cookie_len; | |
610 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | |
611 | p += s->d1->cookie_len; | |
612 | ||
613 | /* Ciphers supported */ | |
c6c2e313 | 614 | i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0); |
36d16f8e BL |
615 | if (i == 0) |
616 | { | |
beb056b3 | 617 | SSLerr(SSL_F_DTLS1_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE); |
36d16f8e BL |
618 | goto err; |
619 | } | |
620 | s2n(i,p); | |
621 | p+=i; | |
622 | ||
623 | /* COMPRESSION */ | |
624 | if (s->ctx->comp_methods == NULL) | |
625 | j=0; | |
626 | else | |
627 | j=sk_SSL_COMP_num(s->ctx->comp_methods); | |
628 | *(p++)=1+j; | |
629 | for (i=0; i<j; i++) | |
630 | { | |
631 | comp=sk_SSL_COMP_value(s->ctx->comp_methods,i); | |
632 | *(p++)=comp->id; | |
633 | } | |
634 | *(p++)=0; /* Add the NULL method */ | |
635 | ||
636 | l=(p-d); | |
637 | d=buf; | |
638 | ||
639 | d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l); | |
640 | ||
641 | s->state=SSL3_ST_CW_CLNT_HELLO_B; | |
642 | /* number of bytes to write */ | |
643 | s->init_num=p-buf; | |
644 | s->init_off=0; | |
645 | ||
646 | /* buffer the message to handle re-xmits */ | |
647 | dtls1_buffer_message(s, 0); | |
648 | } | |
649 | ||
650 | /* SSL3_ST_CW_CLNT_HELLO_B */ | |
651 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | |
652 | err: | |
653 | return(-1); | |
654 | } | |
655 | ||
656 | static int dtls1_get_hello_verify(SSL *s) | |
657 | { | |
658 | int n, al, ok = 0; | |
659 | unsigned char *data; | |
660 | unsigned int cookie_len; | |
661 | ||
662 | n=s->method->ssl_get_message(s, | |
663 | DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, | |
664 | DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B, | |
665 | -1, | |
666 | s->max_cert_list, | |
667 | &ok); | |
668 | ||
669 | if (!ok) return((int)n); | |
670 | ||
671 | if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) | |
672 | { | |
673 | s->d1->send_cookie = 0; | |
674 | s->s3->tmp.reuse_message=1; | |
675 | return(1); | |
676 | } | |
677 | ||
678 | data = (unsigned char *)s->init_msg; | |
679 | ||
680 | if ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff))) | |
681 | { | |
beb056b3 | 682 | SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION); |
36d16f8e BL |
683 | s->version=(s->version&0xff00)|data[1]; |
684 | al = SSL_AD_PROTOCOL_VERSION; | |
685 | goto f_err; | |
686 | } | |
687 | data+=2; | |
688 | ||
689 | cookie_len = *(data++); | |
690 | if ( cookie_len > sizeof(s->d1->cookie)) | |
691 | { | |
692 | al=SSL_AD_ILLEGAL_PARAMETER; | |
693 | goto f_err; | |
694 | } | |
695 | ||
696 | memcpy(s->d1->cookie, data, cookie_len); | |
697 | s->d1->cookie_len = cookie_len; | |
698 | ||
699 | s->d1->send_cookie = 1; | |
700 | return 1; | |
701 | ||
702 | f_err: | |
703 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
704 | return -1; | |
705 | } | |
706 | ||
707 | int dtls1_send_client_key_exchange(SSL *s) | |
708 | { | |
709 | unsigned char *p,*d; | |
710 | int n; | |
52b8dad8 | 711 | unsigned long alg_k; |
36d16f8e BL |
712 | #ifndef OPENSSL_NO_RSA |
713 | unsigned char *q; | |
714 | EVP_PKEY *pkey=NULL; | |
715 | #endif | |
716 | #ifndef OPENSSL_NO_KRB5 | |
717 | KSSL_ERR kssl_err; | |
718 | #endif /* OPENSSL_NO_KRB5 */ | |
719 | ||
720 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) | |
721 | { | |
722 | d=(unsigned char *)s->init_buf->data; | |
723 | p= &(d[DTLS1_HM_HEADER_LENGTH]); | |
52b8dad8 BM |
724 | |
725 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; | |
36d16f8e BL |
726 | |
727 | /* Fool emacs indentation */ | |
728 | if (0) {} | |
729 | #ifndef OPENSSL_NO_RSA | |
52b8dad8 | 730 | else if (alg_k & SSL_kRSA) |
36d16f8e BL |
731 | { |
732 | RSA *rsa; | |
733 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; | |
734 | ||
735 | if (s->session->sess_cert->peer_rsa_tmp != NULL) | |
736 | rsa=s->session->sess_cert->peer_rsa_tmp; | |
737 | else | |
738 | { | |
739 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); | |
740 | if ((pkey == NULL) || | |
741 | (pkey->type != EVP_PKEY_RSA) || | |
742 | (pkey->pkey.rsa == NULL)) | |
743 | { | |
beb056b3 | 744 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
745 | goto err; |
746 | } | |
747 | rsa=pkey->pkey.rsa; | |
748 | EVP_PKEY_free(pkey); | |
749 | } | |
750 | ||
751 | tmp_buf[0]=s->client_version>>8; | |
752 | tmp_buf[1]=s->client_version&0xff; | |
753 | if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) | |
754 | goto err; | |
755 | ||
756 | s->session->master_key_length=sizeof tmp_buf; | |
757 | ||
758 | q=p; | |
e7adda52 | 759 | /* Fix buf for TLS and [incidentally] DTLS */ |
36d16f8e BL |
760 | if (s->version > SSL3_VERSION) |
761 | p+=2; | |
762 | n=RSA_public_encrypt(sizeof tmp_buf, | |
763 | tmp_buf,p,rsa,RSA_PKCS1_PADDING); | |
764 | #ifdef PKCS1_CHECK | |
765 | if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++; | |
766 | if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70; | |
767 | #endif | |
768 | if (n <= 0) | |
769 | { | |
beb056b3 | 770 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT); |
36d16f8e BL |
771 | goto err; |
772 | } | |
773 | ||
e7adda52 | 774 | /* Fix buf for TLS and [incidentally] DTLS */ |
36d16f8e BL |
775 | if (s->version > SSL3_VERSION) |
776 | { | |
777 | s2n(n,q); | |
778 | n+=2; | |
779 | } | |
780 | ||
781 | s->session->master_key_length= | |
782 | s->method->ssl3_enc->generate_master_secret(s, | |
783 | s->session->master_key, | |
784 | tmp_buf,sizeof tmp_buf); | |
785 | OPENSSL_cleanse(tmp_buf,sizeof tmp_buf); | |
786 | } | |
787 | #endif | |
788 | #ifndef OPENSSL_NO_KRB5 | |
52b8dad8 | 789 | else if (alg_k & SSL_kKRB5) |
36d16f8e BL |
790 | { |
791 | krb5_error_code krb5rc; | |
792 | KSSL_CTX *kssl_ctx = s->kssl_ctx; | |
793 | /* krb5_data krb5_ap_req; */ | |
794 | krb5_data *enc_ticket; | |
795 | krb5_data authenticator, *authp = NULL; | |
796 | EVP_CIPHER_CTX ciph_ctx; | |
ef236ec3 | 797 | const EVP_CIPHER *enc = NULL; |
36d16f8e BL |
798 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
799 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; | |
800 | unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH | |
801 | + EVP_MAX_IV_LENGTH]; | |
802 | int padl, outl = sizeof(epms); | |
803 | ||
804 | EVP_CIPHER_CTX_init(&ciph_ctx); | |
805 | ||
806 | #ifdef KSSL_DEBUG | |
807 | printf("ssl3_send_client_key_exchange(%lx & %lx)\n", | |
52b8dad8 | 808 | alg_k, SSL_kKRB5); |
36d16f8e BL |
809 | #endif /* KSSL_DEBUG */ |
810 | ||
811 | authp = NULL; | |
812 | #ifdef KRB5SENDAUTH | |
813 | if (KRB5SENDAUTH) authp = &authenticator; | |
814 | #endif /* KRB5SENDAUTH */ | |
815 | ||
816 | krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, | |
817 | &kssl_err); | |
818 | enc = kssl_map_enc(kssl_ctx->enctype); | |
819 | if (enc == NULL) | |
820 | goto err; | |
821 | #ifdef KSSL_DEBUG | |
822 | { | |
823 | printf("kssl_cget_tkt rtn %d\n", krb5rc); | |
824 | if (krb5rc && kssl_err.text) | |
825 | printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text); | |
826 | } | |
827 | #endif /* KSSL_DEBUG */ | |
828 | ||
829 | if (krb5rc) | |
830 | { | |
831 | ssl3_send_alert(s,SSL3_AL_FATAL, | |
832 | SSL_AD_HANDSHAKE_FAILURE); | |
beb056b3 | 833 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, |
36d16f8e BL |
834 | kssl_err.reason); |
835 | goto err; | |
836 | } | |
837 | ||
838 | /* 20010406 VRS - Earlier versions used KRB5 AP_REQ | |
839 | ** in place of RFC 2712 KerberosWrapper, as in: | |
840 | ** | |
841 | ** Send ticket (copy to *p, set n = length) | |
842 | ** n = krb5_ap_req.length; | |
843 | ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length); | |
844 | ** if (krb5_ap_req.data) | |
845 | ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req); | |
846 | ** | |
847 | ** Now using real RFC 2712 KerberosWrapper | |
848 | ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>) | |
849 | ** Note: 2712 "opaque" types are here replaced | |
850 | ** with a 2-byte length followed by the value. | |
851 | ** Example: | |
852 | ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms | |
853 | ** Where "xx xx" = length bytes. Shown here with | |
854 | ** optional authenticator omitted. | |
855 | */ | |
856 | ||
857 | /* KerberosWrapper.Ticket */ | |
858 | s2n(enc_ticket->length,p); | |
859 | memcpy(p, enc_ticket->data, enc_ticket->length); | |
860 | p+= enc_ticket->length; | |
861 | n = enc_ticket->length + 2; | |
862 | ||
863 | /* KerberosWrapper.Authenticator */ | |
864 | if (authp && authp->length) | |
865 | { | |
866 | s2n(authp->length,p); | |
867 | memcpy(p, authp->data, authp->length); | |
868 | p+= authp->length; | |
869 | n+= authp->length + 2; | |
870 | ||
871 | free(authp->data); | |
872 | authp->data = NULL; | |
873 | authp->length = 0; | |
874 | } | |
875 | else | |
876 | { | |
877 | s2n(0,p);/* null authenticator length */ | |
878 | n+=2; | |
879 | } | |
880 | ||
881 | if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0) | |
882 | goto err; | |
883 | ||
884 | /* 20010420 VRS. Tried it this way; failed. | |
885 | ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); | |
886 | ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx, | |
887 | ** kssl_ctx->length); | |
888 | ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); | |
889 | */ | |
890 | ||
891 | memset(iv, 0, sizeof iv); /* per RFC 1510 */ | |
892 | EVP_EncryptInit_ex(&ciph_ctx,enc, NULL, | |
893 | kssl_ctx->key,iv); | |
894 | EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf, | |
895 | sizeof tmp_buf); | |
896 | EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); | |
897 | outl += padl; | |
ef236ec3 | 898 | if (outl > (int)sizeof epms) |
36d16f8e | 899 | { |
beb056b3 | 900 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
901 | goto err; |
902 | } | |
903 | EVP_CIPHER_CTX_cleanup(&ciph_ctx); | |
904 | ||
905 | /* KerberosWrapper.EncryptedPreMasterSecret */ | |
906 | s2n(outl,p); | |
907 | memcpy(p, epms, outl); | |
908 | p+=outl; | |
909 | n+=outl + 2; | |
910 | ||
911 | s->session->master_key_length= | |
912 | s->method->ssl3_enc->generate_master_secret(s, | |
913 | s->session->master_key, | |
914 | tmp_buf, sizeof tmp_buf); | |
915 | ||
916 | OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); | |
917 | OPENSSL_cleanse(epms, outl); | |
918 | } | |
919 | #endif | |
920 | #ifndef OPENSSL_NO_DH | |
52b8dad8 | 921 | else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
36d16f8e BL |
922 | { |
923 | DH *dh_srvr,*dh_clnt; | |
924 | ||
925 | if (s->session->sess_cert->peer_dh_tmp != NULL) | |
926 | dh_srvr=s->session->sess_cert->peer_dh_tmp; | |
927 | else | |
928 | { | |
929 | /* we get them from the cert */ | |
930 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); | |
beb056b3 | 931 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); |
36d16f8e BL |
932 | goto err; |
933 | } | |
934 | ||
935 | /* generate a new random key */ | |
936 | if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL) | |
937 | { | |
beb056b3 | 938 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); |
36d16f8e BL |
939 | goto err; |
940 | } | |
941 | if (!DH_generate_key(dh_clnt)) | |
942 | { | |
beb056b3 | 943 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); |
36d16f8e BL |
944 | goto err; |
945 | } | |
946 | ||
947 | /* use the 'p' output buffer for the DH key, but | |
948 | * make sure to clear it out afterwards */ | |
949 | ||
950 | n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt); | |
951 | ||
952 | if (n <= 0) | |
953 | { | |
beb056b3 | 954 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); |
36d16f8e BL |
955 | goto err; |
956 | } | |
957 | ||
958 | /* generate master key from the result */ | |
959 | s->session->master_key_length= | |
960 | s->method->ssl3_enc->generate_master_secret(s, | |
961 | s->session->master_key,p,n); | |
962 | /* clean up */ | |
963 | memset(p,0,n); | |
964 | ||
965 | /* send off the data */ | |
966 | n=BN_num_bytes(dh_clnt->pub_key); | |
967 | s2n(n,p); | |
968 | BN_bn2bin(dh_clnt->pub_key,p); | |
969 | n+=2; | |
970 | ||
971 | DH_free(dh_clnt); | |
972 | ||
973 | /* perhaps clean things up a bit EAY EAY EAY EAY*/ | |
974 | } | |
975 | #endif | |
976 | else | |
977 | { | |
978 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); | |
beb056b3 | 979 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
980 | goto err; |
981 | } | |
982 | ||
983 | d = dtls1_set_message_header(s, d, | |
984 | SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n); | |
985 | /* | |
986 | *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE; | |
987 | l2n3(n,d); | |
988 | l2n(s->d1->handshake_write_seq,d); | |
989 | s->d1->handshake_write_seq++; | |
990 | */ | |
991 | ||
992 | s->state=SSL3_ST_CW_KEY_EXCH_B; | |
993 | /* number of bytes to write */ | |
994 | s->init_num=n+DTLS1_HM_HEADER_LENGTH; | |
995 | s->init_off=0; | |
996 | ||
997 | /* buffer the message to handle re-xmits */ | |
998 | dtls1_buffer_message(s, 0); | |
999 | } | |
1000 | ||
1001 | /* SSL3_ST_CW_KEY_EXCH_B */ | |
1002 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | |
1003 | err: | |
1004 | return(-1); | |
1005 | } | |
1006 | ||
1007 | int dtls1_send_client_verify(SSL *s) | |
1008 | { | |
1009 | unsigned char *p,*d; | |
1010 | unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; | |
1011 | EVP_PKEY *pkey; | |
1012 | #ifndef OPENSSL_NO_RSA | |
1013 | unsigned u=0; | |
1014 | #endif | |
1015 | unsigned long n; | |
1016 | #ifndef OPENSSL_NO_DSA | |
1017 | int j; | |
1018 | #endif | |
1019 | ||
1020 | if (s->state == SSL3_ST_CW_CERT_VRFY_A) | |
1021 | { | |
1022 | d=(unsigned char *)s->init_buf->data; | |
1023 | p= &(d[DTLS1_HM_HEADER_LENGTH]); | |
1024 | pkey=s->cert->key->privatekey; | |
1025 | ||
81025661 DSH |
1026 | s->method->ssl3_enc->cert_verify_mac(s, |
1027 | NID_sha1, | |
36d16f8e BL |
1028 | &(data[MD5_DIGEST_LENGTH])); |
1029 | ||
1030 | #ifndef OPENSSL_NO_RSA | |
1031 | if (pkey->type == EVP_PKEY_RSA) | |
1032 | { | |
1033 | s->method->ssl3_enc->cert_verify_mac(s, | |
81025661 DSH |
1034 | NID_md5, |
1035 | &(data[0])); | |
36d16f8e BL |
1036 | if (RSA_sign(NID_md5_sha1, data, |
1037 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, | |
1038 | &(p[2]), &u, pkey->pkey.rsa) <= 0 ) | |
1039 | { | |
beb056b3 | 1040 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB); |
36d16f8e BL |
1041 | goto err; |
1042 | } | |
1043 | s2n(u,p); | |
1044 | n=u+2; | |
1045 | } | |
1046 | else | |
1047 | #endif | |
1048 | #ifndef OPENSSL_NO_DSA | |
1049 | if (pkey->type == EVP_PKEY_DSA) | |
1050 | { | |
1051 | if (!DSA_sign(pkey->save_type, | |
1052 | &(data[MD5_DIGEST_LENGTH]), | |
1053 | SHA_DIGEST_LENGTH,&(p[2]), | |
1054 | (unsigned int *)&j,pkey->pkey.dsa)) | |
1055 | { | |
beb056b3 | 1056 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB); |
36d16f8e BL |
1057 | goto err; |
1058 | } | |
1059 | s2n(j,p); | |
1060 | n=j+2; | |
1061 | } | |
1062 | else | |
1063 | #endif | |
1064 | { | |
beb056b3 | 1065 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
1066 | goto err; |
1067 | } | |
1068 | ||
1069 | d = dtls1_set_message_header(s, d, | |
1070 | SSL3_MT_CERTIFICATE_VERIFY, n, 0, n) ; | |
1071 | ||
1072 | s->init_num=(int)n+DTLS1_HM_HEADER_LENGTH; | |
1073 | s->init_off=0; | |
1074 | ||
1075 | /* buffer the message to handle re-xmits */ | |
1076 | dtls1_buffer_message(s, 0); | |
1077 | ||
1078 | s->state = SSL3_ST_CW_CERT_VRFY_B; | |
1079 | } | |
1080 | ||
1081 | /* s->state = SSL3_ST_CW_CERT_VRFY_B */ | |
1082 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | |
1083 | err: | |
1084 | return(-1); | |
1085 | } | |
1086 | ||
1087 | int dtls1_send_client_certificate(SSL *s) | |
1088 | { | |
1089 | X509 *x509=NULL; | |
1090 | EVP_PKEY *pkey=NULL; | |
1091 | int i; | |
1092 | unsigned long l; | |
1093 | ||
1094 | if (s->state == SSL3_ST_CW_CERT_A) | |
1095 | { | |
1096 | if ((s->cert == NULL) || | |
1097 | (s->cert->key->x509 == NULL) || | |
1098 | (s->cert->key->privatekey == NULL)) | |
1099 | s->state=SSL3_ST_CW_CERT_B; | |
1100 | else | |
1101 | s->state=SSL3_ST_CW_CERT_C; | |
1102 | } | |
1103 | ||
1104 | /* We need to get a client cert */ | |
1105 | if (s->state == SSL3_ST_CW_CERT_B) | |
1106 | { | |
1107 | /* If we get an error, we need to | |
1108 | * ssl->rwstate=SSL_X509_LOOKUP; return(-1); | |
1109 | * We then get retied later */ | |
1110 | i=0; | |
368888bc | 1111 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
36d16f8e BL |
1112 | if (i < 0) |
1113 | { | |
1114 | s->rwstate=SSL_X509_LOOKUP; | |
1115 | return(-1); | |
1116 | } | |
1117 | s->rwstate=SSL_NOTHING; | |
1118 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) | |
1119 | { | |
1120 | s->state=SSL3_ST_CW_CERT_B; | |
1121 | if ( !SSL_use_certificate(s,x509) || | |
1122 | !SSL_use_PrivateKey(s,pkey)) | |
1123 | i=0; | |
1124 | } | |
1125 | else if (i == 1) | |
1126 | { | |
1127 | i=0; | |
beb056b3 | 1128 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
36d16f8e BL |
1129 | } |
1130 | ||
1131 | if (x509 != NULL) X509_free(x509); | |
1132 | if (pkey != NULL) EVP_PKEY_free(pkey); | |
1133 | if (i == 0) | |
1134 | { | |
1135 | if (s->version == SSL3_VERSION) | |
1136 | { | |
1137 | s->s3->tmp.cert_req=0; | |
1138 | ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE); | |
1139 | return(1); | |
1140 | } | |
1141 | else | |
1142 | { | |
1143 | s->s3->tmp.cert_req=2; | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | /* Ok, we have a cert */ | |
1148 | s->state=SSL3_ST_CW_CERT_C; | |
1149 | } | |
1150 | ||
1151 | if (s->state == SSL3_ST_CW_CERT_C) | |
1152 | { | |
1153 | s->state=SSL3_ST_CW_CERT_D; | |
1154 | l=dtls1_output_cert_chain(s, | |
1155 | (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509); | |
1156 | s->init_num=(int)l; | |
1157 | s->init_off=0; | |
1158 | ||
1159 | /* set header called by dtls1_output_cert_chain() */ | |
1160 | ||
1161 | /* buffer the message to handle re-xmits */ | |
1162 | dtls1_buffer_message(s, 0); | |
1163 | } | |
1164 | /* SSL3_ST_CW_CERT_D */ | |
1165 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | |
1166 | } | |
1167 | ||
1168 |