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