]>
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 | /* ==================================================================== | |
7 | * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. | |
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" | |
118 | #include <openssl/buffer.h> | |
119 | #include <openssl/rand.h> | |
120 | #include <openssl/objects.h> | |
121 | #include <openssl/evp.h> | |
122 | #include <openssl/md5.h> | |
3eeaab4b NL |
123 | #include <openssl/bn.h> |
124 | #ifndef OPENSSL_NO_DH | |
125 | #include <openssl/dh.h> | |
126 | #endif | |
36d16f8e | 127 | |
4ebb342f | 128 | static const SSL_METHOD *dtls1_get_client_method(int ver); |
36d16f8e BL |
129 | static int dtls1_get_hello_verify(SSL *s); |
130 | ||
4ebb342f | 131 | static const SSL_METHOD *dtls1_get_client_method(int ver) |
36d16f8e BL |
132 | { |
133 | if (ver == DTLS1_VERSION) | |
134 | return(DTLSv1_client_method()); | |
135 | else | |
136 | return(NULL); | |
137 | } | |
138 | ||
f3b656b2 DSH |
139 | IMPLEMENT_dtls1_meth_func(DTLSv1_client_method, |
140 | ssl_undefined_function, | |
141 | dtls1_connect, | |
142 | dtls1_get_client_method) | |
36d16f8e BL |
143 | |
144 | int dtls1_connect(SSL *s) | |
145 | { | |
146 | BUF_MEM *buf=NULL; | |
147 | unsigned long Time=time(NULL),l; | |
148 | long num1; | |
149 | void (*cb)(const SSL *ssl,int type,int val)=NULL; | |
150 | int ret= -1; | |
151 | int new_state,state,skip=0;; | |
152 | ||
153 | RAND_add(&Time,sizeof(Time),0); | |
154 | ERR_clear_error(); | |
155 | clear_sys_error(); | |
156 | ||
157 | if (s->info_callback != NULL) | |
158 | cb=s->info_callback; | |
159 | else if (s->ctx->info_callback != NULL) | |
160 | cb=s->ctx->info_callback; | |
161 | ||
162 | s->in_handshake++; | |
163 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); | |
164 | ||
165 | for (;;) | |
166 | { | |
167 | state=s->state; | |
168 | ||
169 | switch(s->state) | |
170 | { | |
171 | case SSL_ST_RENEGOTIATE: | |
172 | s->new_session=1; | |
173 | s->state=SSL_ST_CONNECT; | |
174 | s->ctx->stats.sess_connect_renegotiate++; | |
175 | /* break */ | |
176 | case SSL_ST_BEFORE: | |
177 | case SSL_ST_CONNECT: | |
178 | case SSL_ST_BEFORE|SSL_ST_CONNECT: | |
179 | case SSL_ST_OK|SSL_ST_CONNECT: | |
180 | ||
181 | s->server=0; | |
182 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); | |
183 | ||
184 | if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00)) | |
185 | { | |
beb056b3 | 186 | SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
187 | ret = -1; |
188 | goto end; | |
189 | } | |
190 | ||
191 | /* s->version=SSL3_VERSION; */ | |
192 | s->type=SSL_ST_CONNECT; | |
193 | ||
194 | if (s->init_buf == NULL) | |
195 | { | |
196 | if ((buf=BUF_MEM_new()) == NULL) | |
197 | { | |
198 | ret= -1; | |
199 | goto end; | |
200 | } | |
201 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) | |
202 | { | |
203 | ret= -1; | |
204 | goto end; | |
205 | } | |
206 | s->init_buf=buf; | |
207 | buf=NULL; | |
208 | } | |
209 | ||
210 | if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } | |
211 | ||
212 | /* setup buffing BIO */ | |
213 | if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; } | |
214 | ||
215 | /* don't push the buffering BIO quite yet */ | |
216 | ||
217 | ssl3_init_finished_mac(s); | |
218 | ||
219 | s->state=SSL3_ST_CW_CLNT_HELLO_A; | |
220 | s->ctx->stats.sess_connect++; | |
221 | s->init_num=0; | |
222 | break; | |
223 | ||
224 | case SSL3_ST_CW_CLNT_HELLO_A: | |
225 | case SSL3_ST_CW_CLNT_HELLO_B: | |
226 | ||
227 | s->shutdown=0; | |
228 | ret=dtls1_client_hello(s); | |
229 | if (ret <= 0) goto end; | |
230 | ||
231 | if ( s->d1->send_cookie) | |
232 | { | |
233 | s->state=SSL3_ST_CW_FLUSH; | |
234 | s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A; | |
235 | } | |
236 | else | |
237 | s->state=SSL3_ST_CR_SRVR_HELLO_A; | |
238 | ||
239 | s->init_num=0; | |
240 | ||
241 | /* turn on buffering for the next lot of output */ | |
242 | if (s->bbio != s->wbio) | |
243 | s->wbio=BIO_push(s->bbio,s->wbio); | |
244 | ||
245 | break; | |
246 | ||
247 | case SSL3_ST_CR_SRVR_HELLO_A: | |
248 | case SSL3_ST_CR_SRVR_HELLO_B: | |
249 | ret=ssl3_get_server_hello(s); | |
250 | if (ret <= 0) goto end; | |
251 | else | |
252 | { | |
253 | if (s->hit) | |
254 | s->state=SSL3_ST_CR_FINISHED_A; | |
255 | else | |
256 | s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; | |
257 | } | |
258 | s->init_num=0; | |
259 | break; | |
260 | ||
261 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: | |
262 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B: | |
263 | ||
264 | ret = dtls1_get_hello_verify(s); | |
265 | if ( ret <= 0) | |
266 | goto end; | |
267 | if ( s->d1->send_cookie) /* start again, with a cookie */ | |
268 | s->state=SSL3_ST_CW_CLNT_HELLO_A; | |
269 | else | |
270 | s->state = SSL3_ST_CR_CERT_A; | |
271 | s->init_num = 0; | |
272 | break; | |
273 | ||
274 | case SSL3_ST_CR_CERT_A: | |
275 | case SSL3_ST_CR_CERT_B: | |
276 | /* Check if it is anon DH */ | |
277 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) | |
278 | { | |
279 | ret=ssl3_get_server_certificate(s); | |
280 | if (ret <= 0) goto end; | |
281 | } | |
282 | else | |
283 | skip=1; | |
284 | s->state=SSL3_ST_CR_KEY_EXCH_A; | |
285 | s->init_num=0; | |
286 | break; | |
287 | ||
288 | case SSL3_ST_CR_KEY_EXCH_A: | |
289 | case SSL3_ST_CR_KEY_EXCH_B: | |
290 | ret=ssl3_get_key_exchange(s); | |
291 | if (ret <= 0) goto end; | |
292 | s->state=SSL3_ST_CR_CERT_REQ_A; | |
293 | s->init_num=0; | |
294 | ||
295 | /* at this point we check that we have the | |
296 | * required stuff from the server */ | |
297 | if (!ssl3_check_cert_and_algorithm(s)) | |
298 | { | |
299 | ret= -1; | |
300 | goto end; | |
301 | } | |
302 | break; | |
303 | ||
304 | case SSL3_ST_CR_CERT_REQ_A: | |
305 | case SSL3_ST_CR_CERT_REQ_B: | |
306 | ret=ssl3_get_certificate_request(s); | |
307 | if (ret <= 0) goto end; | |
308 | s->state=SSL3_ST_CR_SRVR_DONE_A; | |
309 | s->init_num=0; | |
310 | break; | |
311 | ||
312 | case SSL3_ST_CR_SRVR_DONE_A: | |
313 | case SSL3_ST_CR_SRVR_DONE_B: | |
314 | ret=ssl3_get_server_done(s); | |
315 | if (ret <= 0) goto end; | |
316 | if (s->s3->tmp.cert_req) | |
317 | s->state=SSL3_ST_CW_CERT_A; | |
318 | else | |
319 | s->state=SSL3_ST_CW_KEY_EXCH_A; | |
320 | s->init_num=0; | |
321 | ||
322 | break; | |
323 | ||
324 | case SSL3_ST_CW_CERT_A: | |
325 | case SSL3_ST_CW_CERT_B: | |
326 | case SSL3_ST_CW_CERT_C: | |
327 | case SSL3_ST_CW_CERT_D: | |
328 | ret=dtls1_send_client_certificate(s); | |
329 | if (ret <= 0) goto end; | |
330 | s->state=SSL3_ST_CW_KEY_EXCH_A; | |
331 | s->init_num=0; | |
332 | break; | |
333 | ||
334 | case SSL3_ST_CW_KEY_EXCH_A: | |
335 | case SSL3_ST_CW_KEY_EXCH_B: | |
336 | ret=dtls1_send_client_key_exchange(s); | |
337 | if (ret <= 0) goto end; | |
338 | l=s->s3->tmp.new_cipher->algorithms; | |
339 | /* EAY EAY EAY need to check for DH fix cert | |
340 | * sent back */ | |
341 | /* For TLS, cert_req is set to 2, so a cert chain | |
342 | * of nothing is sent, but no verify packet is sent */ | |
343 | if (s->s3->tmp.cert_req == 1) | |
344 | { | |
345 | s->state=SSL3_ST_CW_CERT_VRFY_A; | |
346 | } | |
347 | else | |
348 | { | |
349 | s->state=SSL3_ST_CW_CHANGE_A; | |
350 | s->s3->change_cipher_spec=0; | |
351 | } | |
352 | ||
353 | s->init_num=0; | |
354 | break; | |
355 | ||
356 | case SSL3_ST_CW_CERT_VRFY_A: | |
357 | case SSL3_ST_CW_CERT_VRFY_B: | |
358 | ret=dtls1_send_client_verify(s); | |
359 | if (ret <= 0) goto end; | |
360 | s->state=SSL3_ST_CW_CHANGE_A; | |
361 | s->init_num=0; | |
362 | s->s3->change_cipher_spec=0; | |
363 | break; | |
364 | ||
365 | case SSL3_ST_CW_CHANGE_A: | |
366 | case SSL3_ST_CW_CHANGE_B: | |
367 | ret=dtls1_send_change_cipher_spec(s, | |
368 | SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); | |
369 | if (ret <= 0) goto end; | |
370 | s->state=SSL3_ST_CW_FINISHED_A; | |
371 | s->init_num=0; | |
372 | ||
373 | s->session->cipher=s->s3->tmp.new_cipher; | |
374 | if (s->s3->tmp.new_compression == NULL) | |
375 | s->session->compress_meth=0; | |
376 | else | |
377 | s->session->compress_meth= | |
378 | s->s3->tmp.new_compression->id; | |
379 | if (!s->method->ssl3_enc->setup_key_block(s)) | |
380 | { | |
381 | ret= -1; | |
382 | goto end; | |
383 | } | |
384 | ||
385 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
386 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) | |
387 | { | |
388 | ret= -1; | |
389 | goto end; | |
390 | } | |
391 | ||
392 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | |
393 | break; | |
394 | ||
395 | case SSL3_ST_CW_FINISHED_A: | |
396 | case SSL3_ST_CW_FINISHED_B: | |
397 | ret=dtls1_send_finished(s, | |
398 | SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, | |
399 | s->method->ssl3_enc->client_finished_label, | |
400 | s->method->ssl3_enc->client_finished_label_len); | |
401 | if (ret <= 0) goto end; | |
402 | s->state=SSL3_ST_CW_FLUSH; | |
403 | ||
404 | /* clear flags */ | |
405 | s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER; | |
406 | if (s->hit) | |
407 | { | |
408 | s->s3->tmp.next_state=SSL_ST_OK; | |
409 | if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) | |
410 | { | |
411 | s->state=SSL_ST_OK; | |
412 | s->s3->flags|=SSL3_FLAGS_POP_BUFFER; | |
413 | s->s3->delay_buf_pop_ret=0; | |
414 | } | |
415 | } | |
416 | else | |
417 | { | |
418 | s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; | |
419 | } | |
420 | s->init_num=0; | |
421 | break; | |
422 | ||
423 | case SSL3_ST_CR_FINISHED_A: | |
424 | case SSL3_ST_CR_FINISHED_B: | |
425 | ||
426 | ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, | |
427 | SSL3_ST_CR_FINISHED_B); | |
428 | if (ret <= 0) goto end; | |
429 | ||
430 | if (s->hit) | |
431 | s->state=SSL3_ST_CW_CHANGE_A; | |
432 | else | |
433 | s->state=SSL_ST_OK; | |
434 | s->init_num=0; | |
435 | break; | |
436 | ||
437 | case SSL3_ST_CW_FLUSH: | |
438 | /* number of bytes to be flushed */ | |
439 | num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); | |
440 | if (num1 > 0) | |
441 | { | |
442 | s->rwstate=SSL_WRITING; | |
443 | num1=BIO_flush(s->wbio); | |
444 | if (num1 <= 0) { ret= -1; goto end; } | |
445 | s->rwstate=SSL_NOTHING; | |
446 | } | |
447 | ||
448 | s->state=s->s3->tmp.next_state; | |
449 | break; | |
450 | ||
451 | case SSL_ST_OK: | |
452 | /* clean a few things up */ | |
453 | ssl3_cleanup_key_block(s); | |
454 | ||
455 | #if 0 | |
456 | if (s->init_buf != NULL) | |
457 | { | |
458 | BUF_MEM_free(s->init_buf); | |
459 | s->init_buf=NULL; | |
460 | } | |
461 | #endif | |
462 | ||
463 | /* If we are not 'joining' the last two packets, | |
464 | * remove the buffering now */ | |
465 | if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) | |
466 | ssl_free_wbio_buffer(s); | |
467 | /* else do it later in ssl3_write */ | |
468 | ||
469 | s->init_num=0; | |
470 | s->new_session=0; | |
471 | ||
472 | ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); | |
473 | if (s->hit) s->ctx->stats.sess_hit++; | |
474 | ||
475 | ret=1; | |
476 | /* s->server=0; */ | |
477 | s->handshake_func=dtls1_connect; | |
478 | s->ctx->stats.sess_connect_good++; | |
479 | ||
480 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); | |
481 | ||
482 | /* done with handshaking */ | |
483 | s->d1->handshake_read_seq = 0; | |
484 | goto end; | |
485 | /* break; */ | |
486 | ||
487 | default: | |
beb056b3 | 488 | SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE); |
36d16f8e BL |
489 | ret= -1; |
490 | goto end; | |
491 | /* break; */ | |
492 | } | |
493 | ||
494 | /* did we do anything */ | |
495 | if (!s->s3->tmp.reuse_message && !skip) | |
496 | { | |
497 | if (s->debug) | |
498 | { | |
499 | if ((ret=BIO_flush(s->wbio)) <= 0) | |
500 | goto end; | |
501 | } | |
502 | ||
503 | if ((cb != NULL) && (s->state != state)) | |
504 | { | |
505 | new_state=s->state; | |
506 | s->state=state; | |
507 | cb(s,SSL_CB_CONNECT_LOOP,1); | |
508 | s->state=new_state; | |
509 | } | |
510 | } | |
511 | skip=0; | |
512 | } | |
513 | end: | |
514 | s->in_handshake--; | |
515 | if (buf != NULL) | |
516 | BUF_MEM_free(buf); | |
517 | if (cb != NULL) | |
518 | cb(s,SSL_CB_CONNECT_EXIT,ret); | |
519 | return(ret); | |
520 | } | |
521 | ||
522 | int dtls1_client_hello(SSL *s) | |
523 | { | |
524 | unsigned char *buf; | |
525 | unsigned char *p,*d; | |
20a90e3a | 526 | unsigned int i,j; |
36d16f8e BL |
527 | unsigned long Time,l; |
528 | SSL_COMP *comp; | |
529 | ||
530 | buf=(unsigned char *)s->init_buf->data; | |
531 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) | |
532 | { | |
533 | if ((s->session == NULL) || | |
534 | (s->session->ssl_version != s->version) || | |
535 | (s->session->not_resumable)) | |
536 | { | |
537 | if (!ssl_get_new_session(s,0)) | |
538 | goto err; | |
539 | } | |
540 | /* else use the pre-loaded session */ | |
541 | ||
542 | p=s->s3->client_random; | |
543 | Time=time(NULL); /* Time */ | |
544 | l2n(Time,p); | |
545 | RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time)); | |
546 | ||
547 | /* Do the message type and length last */ | |
548 | d=p= &(buf[DTLS1_HM_HEADER_LENGTH]); | |
549 | ||
550 | *(p++)=s->version>>8; | |
551 | *(p++)=s->version&0xff; | |
552 | s->client_version=s->version; | |
553 | ||
554 | /* Random stuff */ | |
555 | memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); | |
556 | p+=SSL3_RANDOM_SIZE; | |
557 | ||
558 | /* Session ID */ | |
559 | if (s->new_session) | |
560 | i=0; | |
561 | else | |
562 | i=s->session->session_id_length; | |
563 | *(p++)=i; | |
564 | if (i != 0) | |
565 | { | |
566 | if (i > sizeof s->session->session_id) | |
567 | { | |
beb056b3 | 568 | SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
569 | goto err; |
570 | } | |
571 | memcpy(p,s->session->session_id,i); | |
572 | p+=i; | |
573 | } | |
574 | ||
575 | /* cookie stuff */ | |
576 | if ( s->d1->cookie_len > sizeof(s->d1->cookie)) | |
577 | { | |
beb056b3 | 578 | SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
579 | goto err; |
580 | } | |
581 | *(p++) = s->d1->cookie_len; | |
582 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | |
583 | p += s->d1->cookie_len; | |
584 | ||
585 | /* Ciphers supported */ | |
c6c2e313 | 586 | i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0); |
36d16f8e BL |
587 | if (i == 0) |
588 | { | |
beb056b3 | 589 | SSLerr(SSL_F_DTLS1_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE); |
36d16f8e BL |
590 | goto err; |
591 | } | |
592 | s2n(i,p); | |
593 | p+=i; | |
594 | ||
595 | /* COMPRESSION */ | |
596 | if (s->ctx->comp_methods == NULL) | |
597 | j=0; | |
598 | else | |
599 | j=sk_SSL_COMP_num(s->ctx->comp_methods); | |
600 | *(p++)=1+j; | |
601 | for (i=0; i<j; i++) | |
602 | { | |
603 | comp=sk_SSL_COMP_value(s->ctx->comp_methods,i); | |
604 | *(p++)=comp->id; | |
605 | } | |
606 | *(p++)=0; /* Add the NULL method */ | |
607 | ||
608 | l=(p-d); | |
609 | d=buf; | |
610 | ||
611 | d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l); | |
612 | ||
613 | s->state=SSL3_ST_CW_CLNT_HELLO_B; | |
614 | /* number of bytes to write */ | |
615 | s->init_num=p-buf; | |
616 | s->init_off=0; | |
617 | ||
618 | /* buffer the message to handle re-xmits */ | |
619 | dtls1_buffer_message(s, 0); | |
620 | } | |
621 | ||
622 | /* SSL3_ST_CW_CLNT_HELLO_B */ | |
623 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | |
624 | err: | |
625 | return(-1); | |
626 | } | |
627 | ||
628 | static int dtls1_get_hello_verify(SSL *s) | |
629 | { | |
630 | int n, al, ok = 0; | |
631 | unsigned char *data; | |
632 | unsigned int cookie_len; | |
633 | ||
634 | n=s->method->ssl_get_message(s, | |
635 | DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, | |
636 | DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B, | |
637 | -1, | |
638 | s->max_cert_list, | |
639 | &ok); | |
640 | ||
641 | if (!ok) return((int)n); | |
642 | ||
643 | if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) | |
644 | { | |
645 | s->d1->send_cookie = 0; | |
646 | s->s3->tmp.reuse_message=1; | |
647 | return(1); | |
648 | } | |
649 | ||
650 | data = (unsigned char *)s->init_msg; | |
651 | ||
652 | if ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff))) | |
653 | { | |
beb056b3 | 654 | SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION); |
36d16f8e BL |
655 | s->version=(s->version&0xff00)|data[1]; |
656 | al = SSL_AD_PROTOCOL_VERSION; | |
657 | goto f_err; | |
658 | } | |
659 | data+=2; | |
660 | ||
661 | cookie_len = *(data++); | |
662 | if ( cookie_len > sizeof(s->d1->cookie)) | |
663 | { | |
664 | al=SSL_AD_ILLEGAL_PARAMETER; | |
665 | goto f_err; | |
666 | } | |
667 | ||
668 | memcpy(s->d1->cookie, data, cookie_len); | |
669 | s->d1->cookie_len = cookie_len; | |
670 | ||
671 | s->d1->send_cookie = 1; | |
672 | return 1; | |
673 | ||
674 | f_err: | |
675 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
676 | return -1; | |
677 | } | |
678 | ||
679 | int dtls1_send_client_key_exchange(SSL *s) | |
680 | { | |
681 | unsigned char *p,*d; | |
682 | int n; | |
683 | unsigned long l; | |
684 | #ifndef OPENSSL_NO_RSA | |
685 | unsigned char *q; | |
686 | EVP_PKEY *pkey=NULL; | |
687 | #endif | |
688 | #ifndef OPENSSL_NO_KRB5 | |
689 | KSSL_ERR kssl_err; | |
690 | #endif /* OPENSSL_NO_KRB5 */ | |
691 | ||
692 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) | |
693 | { | |
694 | d=(unsigned char *)s->init_buf->data; | |
695 | p= &(d[DTLS1_HM_HEADER_LENGTH]); | |
696 | ||
697 | l=s->s3->tmp.new_cipher->algorithms; | |
698 | ||
699 | /* Fool emacs indentation */ | |
700 | if (0) {} | |
701 | #ifndef OPENSSL_NO_RSA | |
702 | else if (l & SSL_kRSA) | |
703 | { | |
704 | RSA *rsa; | |
705 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; | |
706 | ||
707 | if (s->session->sess_cert->peer_rsa_tmp != NULL) | |
708 | rsa=s->session->sess_cert->peer_rsa_tmp; | |
709 | else | |
710 | { | |
711 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); | |
712 | if ((pkey == NULL) || | |
713 | (pkey->type != EVP_PKEY_RSA) || | |
714 | (pkey->pkey.rsa == NULL)) | |
715 | { | |
beb056b3 | 716 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
717 | goto err; |
718 | } | |
719 | rsa=pkey->pkey.rsa; | |
720 | EVP_PKEY_free(pkey); | |
721 | } | |
722 | ||
723 | tmp_buf[0]=s->client_version>>8; | |
724 | tmp_buf[1]=s->client_version&0xff; | |
725 | if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) | |
726 | goto err; | |
727 | ||
728 | s->session->master_key_length=sizeof tmp_buf; | |
729 | ||
730 | q=p; | |
731 | /* Fix buf for TLS and beyond */ | |
732 | if (s->version > SSL3_VERSION) | |
733 | p+=2; | |
734 | n=RSA_public_encrypt(sizeof tmp_buf, | |
735 | tmp_buf,p,rsa,RSA_PKCS1_PADDING); | |
736 | #ifdef PKCS1_CHECK | |
737 | if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++; | |
738 | if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70; | |
739 | #endif | |
740 | if (n <= 0) | |
741 | { | |
beb056b3 | 742 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT); |
36d16f8e BL |
743 | goto err; |
744 | } | |
745 | ||
746 | /* Fix buf for TLS and beyond */ | |
747 | if (s->version > SSL3_VERSION) | |
748 | { | |
749 | s2n(n,q); | |
750 | n+=2; | |
751 | } | |
752 | ||
753 | s->session->master_key_length= | |
754 | s->method->ssl3_enc->generate_master_secret(s, | |
755 | s->session->master_key, | |
756 | tmp_buf,sizeof tmp_buf); | |
757 | OPENSSL_cleanse(tmp_buf,sizeof tmp_buf); | |
758 | } | |
759 | #endif | |
760 | #ifndef OPENSSL_NO_KRB5 | |
761 | else if (l & SSL_kKRB5) | |
762 | { | |
763 | krb5_error_code krb5rc; | |
764 | KSSL_CTX *kssl_ctx = s->kssl_ctx; | |
765 | /* krb5_data krb5_ap_req; */ | |
766 | krb5_data *enc_ticket; | |
767 | krb5_data authenticator, *authp = NULL; | |
768 | EVP_CIPHER_CTX ciph_ctx; | |
769 | EVP_CIPHER *enc = NULL; | |
770 | unsigned char iv[EVP_MAX_IV_LENGTH]; | |
771 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; | |
772 | unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH | |
773 | + EVP_MAX_IV_LENGTH]; | |
774 | int padl, outl = sizeof(epms); | |
775 | ||
776 | EVP_CIPHER_CTX_init(&ciph_ctx); | |
777 | ||
778 | #ifdef KSSL_DEBUG | |
779 | printf("ssl3_send_client_key_exchange(%lx & %lx)\n", | |
780 | l, SSL_kKRB5); | |
781 | #endif /* KSSL_DEBUG */ | |
782 | ||
783 | authp = NULL; | |
784 | #ifdef KRB5SENDAUTH | |
785 | if (KRB5SENDAUTH) authp = &authenticator; | |
786 | #endif /* KRB5SENDAUTH */ | |
787 | ||
788 | krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, | |
789 | &kssl_err); | |
790 | enc = kssl_map_enc(kssl_ctx->enctype); | |
791 | if (enc == NULL) | |
792 | goto err; | |
793 | #ifdef KSSL_DEBUG | |
794 | { | |
795 | printf("kssl_cget_tkt rtn %d\n", krb5rc); | |
796 | if (krb5rc && kssl_err.text) | |
797 | printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text); | |
798 | } | |
799 | #endif /* KSSL_DEBUG */ | |
800 | ||
801 | if (krb5rc) | |
802 | { | |
803 | ssl3_send_alert(s,SSL3_AL_FATAL, | |
804 | SSL_AD_HANDSHAKE_FAILURE); | |
beb056b3 | 805 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, |
36d16f8e BL |
806 | kssl_err.reason); |
807 | goto err; | |
808 | } | |
809 | ||
810 | /* 20010406 VRS - Earlier versions used KRB5 AP_REQ | |
811 | ** in place of RFC 2712 KerberosWrapper, as in: | |
812 | ** | |
813 | ** Send ticket (copy to *p, set n = length) | |
814 | ** n = krb5_ap_req.length; | |
815 | ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length); | |
816 | ** if (krb5_ap_req.data) | |
817 | ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req); | |
818 | ** | |
819 | ** Now using real RFC 2712 KerberosWrapper | |
820 | ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>) | |
821 | ** Note: 2712 "opaque" types are here replaced | |
822 | ** with a 2-byte length followed by the value. | |
823 | ** Example: | |
824 | ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms | |
825 | ** Where "xx xx" = length bytes. Shown here with | |
826 | ** optional authenticator omitted. | |
827 | */ | |
828 | ||
829 | /* KerberosWrapper.Ticket */ | |
830 | s2n(enc_ticket->length,p); | |
831 | memcpy(p, enc_ticket->data, enc_ticket->length); | |
832 | p+= enc_ticket->length; | |
833 | n = enc_ticket->length + 2; | |
834 | ||
835 | /* KerberosWrapper.Authenticator */ | |
836 | if (authp && authp->length) | |
837 | { | |
838 | s2n(authp->length,p); | |
839 | memcpy(p, authp->data, authp->length); | |
840 | p+= authp->length; | |
841 | n+= authp->length + 2; | |
842 | ||
843 | free(authp->data); | |
844 | authp->data = NULL; | |
845 | authp->length = 0; | |
846 | } | |
847 | else | |
848 | { | |
849 | s2n(0,p);/* null authenticator length */ | |
850 | n+=2; | |
851 | } | |
852 | ||
853 | if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0) | |
854 | goto err; | |
855 | ||
856 | /* 20010420 VRS. Tried it this way; failed. | |
857 | ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); | |
858 | ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx, | |
859 | ** kssl_ctx->length); | |
860 | ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); | |
861 | */ | |
862 | ||
863 | memset(iv, 0, sizeof iv); /* per RFC 1510 */ | |
864 | EVP_EncryptInit_ex(&ciph_ctx,enc, NULL, | |
865 | kssl_ctx->key,iv); | |
866 | EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf, | |
867 | sizeof tmp_buf); | |
868 | EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); | |
869 | outl += padl; | |
870 | if (outl > sizeof epms) | |
871 | { | |
beb056b3 | 872 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
873 | goto err; |
874 | } | |
875 | EVP_CIPHER_CTX_cleanup(&ciph_ctx); | |
876 | ||
877 | /* KerberosWrapper.EncryptedPreMasterSecret */ | |
878 | s2n(outl,p); | |
879 | memcpy(p, epms, outl); | |
880 | p+=outl; | |
881 | n+=outl + 2; | |
882 | ||
883 | s->session->master_key_length= | |
884 | s->method->ssl3_enc->generate_master_secret(s, | |
885 | s->session->master_key, | |
886 | tmp_buf, sizeof tmp_buf); | |
887 | ||
888 | OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); | |
889 | OPENSSL_cleanse(epms, outl); | |
890 | } | |
891 | #endif | |
892 | #ifndef OPENSSL_NO_DH | |
893 | else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) | |
894 | { | |
895 | DH *dh_srvr,*dh_clnt; | |
896 | ||
897 | if (s->session->sess_cert->peer_dh_tmp != NULL) | |
898 | dh_srvr=s->session->sess_cert->peer_dh_tmp; | |
899 | else | |
900 | { | |
901 | /* we get them from the cert */ | |
902 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); | |
beb056b3 | 903 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); |
36d16f8e BL |
904 | goto err; |
905 | } | |
906 | ||
907 | /* generate a new random key */ | |
908 | if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL) | |
909 | { | |
beb056b3 | 910 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); |
36d16f8e BL |
911 | goto err; |
912 | } | |
913 | if (!DH_generate_key(dh_clnt)) | |
914 | { | |
beb056b3 | 915 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); |
36d16f8e BL |
916 | goto err; |
917 | } | |
918 | ||
919 | /* use the 'p' output buffer for the DH key, but | |
920 | * make sure to clear it out afterwards */ | |
921 | ||
922 | n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt); | |
923 | ||
924 | if (n <= 0) | |
925 | { | |
beb056b3 | 926 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); |
36d16f8e BL |
927 | goto err; |
928 | } | |
929 | ||
930 | /* generate master key from the result */ | |
931 | s->session->master_key_length= | |
932 | s->method->ssl3_enc->generate_master_secret(s, | |
933 | s->session->master_key,p,n); | |
934 | /* clean up */ | |
935 | memset(p,0,n); | |
936 | ||
937 | /* send off the data */ | |
938 | n=BN_num_bytes(dh_clnt->pub_key); | |
939 | s2n(n,p); | |
940 | BN_bn2bin(dh_clnt->pub_key,p); | |
941 | n+=2; | |
942 | ||
943 | DH_free(dh_clnt); | |
944 | ||
945 | /* perhaps clean things up a bit EAY EAY EAY EAY*/ | |
946 | } | |
947 | #endif | |
948 | else | |
949 | { | |
950 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); | |
beb056b3 | 951 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
952 | goto err; |
953 | } | |
954 | ||
955 | d = dtls1_set_message_header(s, d, | |
956 | SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n); | |
957 | /* | |
958 | *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE; | |
959 | l2n3(n,d); | |
960 | l2n(s->d1->handshake_write_seq,d); | |
961 | s->d1->handshake_write_seq++; | |
962 | */ | |
963 | ||
964 | s->state=SSL3_ST_CW_KEY_EXCH_B; | |
965 | /* number of bytes to write */ | |
966 | s->init_num=n+DTLS1_HM_HEADER_LENGTH; | |
967 | s->init_off=0; | |
968 | ||
969 | /* buffer the message to handle re-xmits */ | |
970 | dtls1_buffer_message(s, 0); | |
971 | } | |
972 | ||
973 | /* SSL3_ST_CW_KEY_EXCH_B */ | |
974 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | |
975 | err: | |
976 | return(-1); | |
977 | } | |
978 | ||
979 | int dtls1_send_client_verify(SSL *s) | |
980 | { | |
981 | unsigned char *p,*d; | |
982 | unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; | |
983 | EVP_PKEY *pkey; | |
984 | #ifndef OPENSSL_NO_RSA | |
985 | unsigned u=0; | |
986 | #endif | |
987 | unsigned long n; | |
988 | #ifndef OPENSSL_NO_DSA | |
989 | int j; | |
990 | #endif | |
991 | ||
992 | if (s->state == SSL3_ST_CW_CERT_VRFY_A) | |
993 | { | |
994 | d=(unsigned char *)s->init_buf->data; | |
995 | p= &(d[DTLS1_HM_HEADER_LENGTH]); | |
996 | pkey=s->cert->key->privatekey; | |
997 | ||
998 | s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2), | |
999 | &(data[MD5_DIGEST_LENGTH])); | |
1000 | ||
1001 | #ifndef OPENSSL_NO_RSA | |
1002 | if (pkey->type == EVP_PKEY_RSA) | |
1003 | { | |
1004 | s->method->ssl3_enc->cert_verify_mac(s, | |
1005 | &(s->s3->finish_dgst1),&(data[0])); | |
1006 | if (RSA_sign(NID_md5_sha1, data, | |
1007 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, | |
1008 | &(p[2]), &u, pkey->pkey.rsa) <= 0 ) | |
1009 | { | |
beb056b3 | 1010 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB); |
36d16f8e BL |
1011 | goto err; |
1012 | } | |
1013 | s2n(u,p); | |
1014 | n=u+2; | |
1015 | } | |
1016 | else | |
1017 | #endif | |
1018 | #ifndef OPENSSL_NO_DSA | |
1019 | if (pkey->type == EVP_PKEY_DSA) | |
1020 | { | |
1021 | if (!DSA_sign(pkey->save_type, | |
1022 | &(data[MD5_DIGEST_LENGTH]), | |
1023 | SHA_DIGEST_LENGTH,&(p[2]), | |
1024 | (unsigned int *)&j,pkey->pkey.dsa)) | |
1025 | { | |
beb056b3 | 1026 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB); |
36d16f8e BL |
1027 | goto err; |
1028 | } | |
1029 | s2n(j,p); | |
1030 | n=j+2; | |
1031 | } | |
1032 | else | |
1033 | #endif | |
1034 | { | |
beb056b3 | 1035 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR); |
36d16f8e BL |
1036 | goto err; |
1037 | } | |
1038 | ||
1039 | d = dtls1_set_message_header(s, d, | |
1040 | SSL3_MT_CERTIFICATE_VERIFY, n, 0, n) ; | |
1041 | ||
1042 | s->init_num=(int)n+DTLS1_HM_HEADER_LENGTH; | |
1043 | s->init_off=0; | |
1044 | ||
1045 | /* buffer the message to handle re-xmits */ | |
1046 | dtls1_buffer_message(s, 0); | |
1047 | ||
1048 | s->state = SSL3_ST_CW_CERT_VRFY_B; | |
1049 | } | |
1050 | ||
1051 | /* s->state = SSL3_ST_CW_CERT_VRFY_B */ | |
1052 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | |
1053 | err: | |
1054 | return(-1); | |
1055 | } | |
1056 | ||
1057 | int dtls1_send_client_certificate(SSL *s) | |
1058 | { | |
1059 | X509 *x509=NULL; | |
1060 | EVP_PKEY *pkey=NULL; | |
1061 | int i; | |
1062 | unsigned long l; | |
1063 | ||
1064 | if (s->state == SSL3_ST_CW_CERT_A) | |
1065 | { | |
1066 | if ((s->cert == NULL) || | |
1067 | (s->cert->key->x509 == NULL) || | |
1068 | (s->cert->key->privatekey == NULL)) | |
1069 | s->state=SSL3_ST_CW_CERT_B; | |
1070 | else | |
1071 | s->state=SSL3_ST_CW_CERT_C; | |
1072 | } | |
1073 | ||
1074 | /* We need to get a client cert */ | |
1075 | if (s->state == SSL3_ST_CW_CERT_B) | |
1076 | { | |
1077 | /* If we get an error, we need to | |
1078 | * ssl->rwstate=SSL_X509_LOOKUP; return(-1); | |
1079 | * We then get retied later */ | |
1080 | i=0; | |
1081 | if (s->ctx->client_cert_cb != NULL) | |
1082 | i=s->ctx->client_cert_cb(s,&(x509),&(pkey)); | |
1083 | if (i < 0) | |
1084 | { | |
1085 | s->rwstate=SSL_X509_LOOKUP; | |
1086 | return(-1); | |
1087 | } | |
1088 | s->rwstate=SSL_NOTHING; | |
1089 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) | |
1090 | { | |
1091 | s->state=SSL3_ST_CW_CERT_B; | |
1092 | if ( !SSL_use_certificate(s,x509) || | |
1093 | !SSL_use_PrivateKey(s,pkey)) | |
1094 | i=0; | |
1095 | } | |
1096 | else if (i == 1) | |
1097 | { | |
1098 | i=0; | |
beb056b3 | 1099 | SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
36d16f8e BL |
1100 | } |
1101 | ||
1102 | if (x509 != NULL) X509_free(x509); | |
1103 | if (pkey != NULL) EVP_PKEY_free(pkey); | |
1104 | if (i == 0) | |
1105 | { | |
1106 | if (s->version == SSL3_VERSION) | |
1107 | { | |
1108 | s->s3->tmp.cert_req=0; | |
1109 | ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE); | |
1110 | return(1); | |
1111 | } | |
1112 | else | |
1113 | { | |
1114 | s->s3->tmp.cert_req=2; | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | /* Ok, we have a cert */ | |
1119 | s->state=SSL3_ST_CW_CERT_C; | |
1120 | } | |
1121 | ||
1122 | if (s->state == SSL3_ST_CW_CERT_C) | |
1123 | { | |
1124 | s->state=SSL3_ST_CW_CERT_D; | |
1125 | l=dtls1_output_cert_chain(s, | |
1126 | (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509); | |
1127 | s->init_num=(int)l; | |
1128 | s->init_off=0; | |
1129 | ||
1130 | /* set header called by dtls1_output_cert_chain() */ | |
1131 | ||
1132 | /* buffer the message to handle re-xmits */ | |
1133 | dtls1_buffer_message(s, 0); | |
1134 | } | |
1135 | /* SSL3_ST_CW_CERT_D */ | |
1136 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); | |
1137 | } | |
1138 | ||
1139 |