]>
Commit | Line | Data |
---|---|---|
36d16f8e | 1 | /* ssl/d1_srvr.c */ |
40720ce3 | 2 | /* |
36d16f8e | 3 | * DTLS implementation written by Nagendra Modadugu |
40720ce3 | 4 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. |
36d16f8e BL |
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 | |
40720ce3 | 14 | * notice, this list of conditions and the following disclaimer. |
36d16f8e BL |
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. | |
40720ce3 | 65 | * |
36d16f8e BL |
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). | |
40720ce3 | 72 | * |
36d16f8e BL |
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. | |
40720ce3 | 79 | * |
36d16f8e BL |
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 :-). | |
40720ce3 | 94 | * 4. If you include any Windows specific code (or a derivative thereof) from |
36d16f8e BL |
95 | * the apps directory (application code) you must include an acknowledgement: |
96 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
40720ce3 | 97 | * |
36d16f8e BL |
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. | |
40720ce3 | 109 | * |
36d16f8e BL |
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/x509.h> | |
123 | #include <openssl/md5.h> | |
4913b88f | 124 | #ifndef OPENSSL_NO_DH |
40720ce3 | 125 | # include <openssl/dh.h> |
4913b88f | 126 | #endif |
36d16f8e BL |
127 | |
128 | static SSL_METHOD *dtls1_get_server_method(int ver); | |
129 | static int dtls1_send_hello_verify_request(SSL *s); | |
130 | ||
131 | static SSL_METHOD *dtls1_get_server_method(int ver) | |
40720ce3 MC |
132 | { |
133 | if (ver == DTLS1_VERSION) | |
134 | return (DTLSv1_server_method()); | |
135 | else | |
136 | return (NULL); | |
137 | } | |
36d16f8e | 138 | |
222f2246 | 139 | IMPLEMENT_dtls1_meth_func(DTLSv1_server_method, |
40720ce3 MC |
140 | dtls1_accept, |
141 | ssl_undefined_function, dtls1_get_server_method) | |
36d16f8e BL |
142 | |
143 | int dtls1_accept(SSL *s) | |
40720ce3 MC |
144 | { |
145 | BUF_MEM *buf; | |
146 | unsigned long l, Time = (unsigned long)time(NULL); | |
147 | void (*cb) (const SSL *ssl, int type, int val) = NULL; | |
148 | int ret = -1; | |
149 | int new_state, state, skip = 0; | |
150 | int listen; | |
151 | ||
152 | RAND_add(&Time, sizeof(Time), 0); | |
153 | ERR_clear_error(); | |
154 | clear_sys_error(); | |
155 | ||
156 | if (s->info_callback != NULL) | |
157 | cb = s->info_callback; | |
158 | else if (s->ctx->info_callback != NULL) | |
159 | cb = s->ctx->info_callback; | |
160 | ||
161 | listen = s->d1->listen; | |
162 | ||
163 | /* init things to blank */ | |
164 | s->in_handshake++; | |
165 | if (!SSL_in_init(s) || SSL_in_before(s)) | |
166 | SSL_clear(s); | |
167 | ||
168 | s->d1->listen = listen; | |
169 | ||
170 | if (s->cert == NULL) { | |
171 | SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_NO_CERTIFICATE_SET); | |
172 | return (-1); | |
173 | } | |
174 | ||
175 | for (;;) { | |
176 | state = s->state; | |
177 | ||
178 | switch (s->state) { | |
179 | case SSL_ST_RENEGOTIATE: | |
180 | s->new_session = 1; | |
181 | /* s->state=SSL_ST_ACCEPT; */ | |
182 | ||
183 | case SSL_ST_BEFORE: | |
184 | case SSL_ST_ACCEPT: | |
185 | case SSL_ST_BEFORE | SSL_ST_ACCEPT: | |
186 | case SSL_ST_OK | SSL_ST_ACCEPT: | |
187 | ||
188 | s->server = 1; | |
189 | if (cb != NULL) | |
190 | cb(s, SSL_CB_HANDSHAKE_START, 1); | |
191 | ||
192 | if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) { | |
193 | SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR); | |
194 | return -1; | |
195 | } | |
196 | s->type = SSL_ST_ACCEPT; | |
197 | ||
198 | if (s->init_buf == NULL) { | |
199 | if ((buf = BUF_MEM_new()) == NULL) { | |
200 | ret = -1; | |
201 | goto end; | |
202 | } | |
203 | if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { | |
204 | ret = -1; | |
205 | goto end; | |
206 | } | |
207 | s->init_buf = buf; | |
208 | } | |
209 | ||
210 | if (!ssl3_setup_buffers(s)) { | |
211 | ret = -1; | |
212 | goto end; | |
213 | } | |
214 | ||
215 | s->init_num = 0; | |
216 | ||
217 | if (s->state != SSL_ST_RENEGOTIATE) { | |
218 | /* | |
219 | * Ok, we now need to push on a buffering BIO so that the | |
220 | * output is sent in a way that TCP likes :-) | |
221 | */ | |
222 | if (!ssl_init_wbio_buffer(s, 1)) { | |
223 | ret = -1; | |
224 | goto end; | |
225 | } | |
226 | ||
227 | ssl3_init_finished_mac(s); | |
228 | s->state = SSL3_ST_SR_CLNT_HELLO_A; | |
229 | s->ctx->stats.sess_accept++; | |
230 | } else { | |
231 | /* | |
232 | * s->state == SSL_ST_RENEGOTIATE, we will just send a | |
233 | * HelloRequest | |
234 | */ | |
235 | s->ctx->stats.sess_accept_renegotiate++; | |
236 | s->state = SSL3_ST_SW_HELLO_REQ_A; | |
237 | } | |
238 | ||
239 | break; | |
240 | ||
241 | case SSL3_ST_SW_HELLO_REQ_A: | |
242 | case SSL3_ST_SW_HELLO_REQ_B: | |
243 | ||
244 | s->shutdown = 0; | |
245 | dtls1_clear_record_buffer(s); | |
246 | dtls1_start_timer(s); | |
247 | ret = dtls1_send_hello_request(s); | |
248 | if (ret <= 0) | |
249 | goto end; | |
250 | s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A; | |
251 | s->state = SSL3_ST_SW_FLUSH; | |
252 | s->init_num = 0; | |
253 | ||
254 | ssl3_init_finished_mac(s); | |
255 | break; | |
256 | ||
257 | case SSL3_ST_SW_HELLO_REQ_C: | |
258 | s->state = SSL_ST_OK; | |
259 | break; | |
260 | ||
261 | case SSL3_ST_SR_CLNT_HELLO_A: | |
262 | case SSL3_ST_SR_CLNT_HELLO_B: | |
263 | case SSL3_ST_SR_CLNT_HELLO_C: | |
264 | ||
265 | s->shutdown = 0; | |
266 | ret = ssl3_get_client_hello(s); | |
267 | if (ret <= 0) | |
268 | goto end; | |
269 | dtls1_stop_timer(s); | |
270 | ||
271 | if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) | |
272 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; | |
273 | else | |
274 | s->state = SSL3_ST_SW_SRVR_HELLO_A; | |
275 | ||
276 | s->init_num = 0; | |
277 | ||
278 | /* | |
279 | * Reflect ClientHello sequence to remain stateless while | |
280 | * listening | |
281 | */ | |
282 | if (listen) { | |
283 | memcpy(s->s3->write_sequence, s->s3->read_sequence, | |
284 | sizeof(s->s3->write_sequence)); | |
285 | } | |
286 | ||
287 | /* If we're just listening, stop here */ | |
288 | if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A) { | |
289 | ret = 2; | |
290 | s->d1->listen = 0; | |
291 | /* | |
292 | * Set expected sequence numbers to continue the handshake. | |
293 | */ | |
294 | s->d1->handshake_read_seq = 2; | |
295 | s->d1->handshake_write_seq = 1; | |
296 | s->d1->next_handshake_write_seq = 1; | |
297 | goto end; | |
298 | } | |
299 | ||
300 | break; | |
301 | ||
302 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: | |
303 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: | |
304 | ||
305 | ret = dtls1_send_hello_verify_request(s); | |
306 | if (ret <= 0) | |
307 | goto end; | |
308 | s->state = SSL3_ST_SW_FLUSH; | |
309 | s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A; | |
310 | ||
311 | /* HelloVerifyRequests resets Finished MAC */ | |
312 | if (s->client_version != DTLS1_BAD_VER) | |
313 | ssl3_init_finished_mac(s); | |
314 | break; | |
315 | ||
316 | case SSL3_ST_SW_SRVR_HELLO_A: | |
317 | case SSL3_ST_SW_SRVR_HELLO_B: | |
318 | s->new_session = 2; | |
319 | dtls1_start_timer(s); | |
320 | ret = dtls1_send_server_hello(s); | |
321 | if (ret <= 0) | |
322 | goto end; | |
36d16f8e | 323 | |
7a014dce | 324 | #ifndef OPENSSL_NO_TLSEXT |
40720ce3 MC |
325 | if (s->hit) { |
326 | if (s->tlsext_ticket_expected) | |
327 | s->state = SSL3_ST_SW_SESSION_TICKET_A; | |
328 | else | |
329 | s->state = SSL3_ST_SW_CHANGE_A; | |
330 | } | |
7a014dce | 331 | #else |
40720ce3 MC |
332 | if (s->hit) |
333 | s->state = SSL3_ST_SW_CHANGE_A; | |
7a014dce | 334 | #endif |
40720ce3 MC |
335 | else |
336 | s->state = SSL3_ST_SW_CERT_A; | |
337 | s->init_num = 0; | |
338 | break; | |
339 | ||
340 | case SSL3_ST_SW_CERT_A: | |
341 | case SSL3_ST_SW_CERT_B: | |
342 | /* Check if it is anon DH */ | |
343 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) { | |
344 | dtls1_start_timer(s); | |
345 | ret = dtls1_send_server_certificate(s); | |
346 | if (ret <= 0) | |
347 | goto end; | |
7a014dce | 348 | #ifndef OPENSSL_NO_TLSEXT |
40720ce3 MC |
349 | if (s->tlsext_status_expected) |
350 | s->state = SSL3_ST_SW_CERT_STATUS_A; | |
351 | else | |
352 | s->state = SSL3_ST_SW_KEY_EXCH_A; | |
353 | } else { | |
354 | skip = 1; | |
355 | s->state = SSL3_ST_SW_KEY_EXCH_A; | |
356 | } | |
7a014dce | 357 | #else |
40720ce3 MC |
358 | } else |
359 | skip = 1; | |
7a014dce | 360 | |
40720ce3 | 361 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
7a014dce | 362 | #endif |
40720ce3 MC |
363 | s->init_num = 0; |
364 | break; | |
365 | ||
366 | case SSL3_ST_SW_KEY_EXCH_A: | |
367 | case SSL3_ST_SW_KEY_EXCH_B: | |
368 | l = s->s3->tmp.new_cipher->algorithms; | |
369 | ||
370 | /* | |
371 | * clear this, it may get reset by send_server_key_exchange | |
372 | */ | |
373 | s->s3->tmp.use_rsa_tmp = 0; | |
374 | ||
375 | /* | |
376 | * only send if a DH key exchange, fortezza or RSA but we have a | |
377 | * sign only certificate | |
378 | */ | |
379 | if ((l & (SSL_DH | SSL_kFZA)) | |
380 | || ((l & SSL_kRSA) | |
381 | && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL | |
382 | || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) | |
383 | && EVP_PKEY_size(s->cert-> | |
384 | pkeys | |
385 | [SSL_PKEY_RSA_ENC].privatekey) * | |
386 | 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) | |
387 | ) | |
388 | ) | |
389 | ) | |
390 | ) { | |
391 | dtls1_start_timer(s); | |
392 | ret = dtls1_send_server_key_exchange(s); | |
393 | if (ret <= 0) | |
394 | goto end; | |
395 | } else | |
396 | skip = 1; | |
397 | ||
398 | s->state = SSL3_ST_SW_CERT_REQ_A; | |
399 | s->init_num = 0; | |
400 | break; | |
401 | ||
402 | case SSL3_ST_SW_CERT_REQ_A: | |
403 | case SSL3_ST_SW_CERT_REQ_B: | |
404 | if ( /* don't request cert unless asked for it: */ | |
405 | !(s->verify_mode & SSL_VERIFY_PEER) || | |
406 | /* | |
407 | * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert | |
408 | * during re-negotiation: | |
409 | */ | |
410 | ((s->session->peer != NULL) && | |
411 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || | |
412 | /* | |
413 | * never request cert in anonymous ciphersuites (see | |
414 | * section "Certificate request" in SSL 3 drafts and in | |
415 | * RFC 2246): | |
416 | */ | |
417 | ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) && | |
418 | /* | |
419 | * ... except when the application insists on | |
420 | * verification (against the specs, but s3_clnt.c accepts | |
421 | * this for SSL 3) | |
422 | */ | |
423 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || | |
424 | /* | |
425 | * never request cert in Kerberos ciphersuites | |
426 | */ | |
427 | (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5)) { | |
428 | /* no cert request */ | |
429 | skip = 1; | |
430 | s->s3->tmp.cert_request = 0; | |
431 | s->state = SSL3_ST_SW_SRVR_DONE_A; | |
432 | } else { | |
433 | s->s3->tmp.cert_request = 1; | |
434 | dtls1_start_timer(s); | |
435 | ret = dtls1_send_certificate_request(s); | |
436 | if (ret <= 0) | |
437 | goto end; | |
36d16f8e | 438 | #ifndef NETSCAPE_HANG_BUG |
40720ce3 | 439 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
36d16f8e | 440 | #else |
40720ce3 MC |
441 | s->state = SSL3_ST_SW_FLUSH; |
442 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; | |
36d16f8e | 443 | #endif |
40720ce3 MC |
444 | s->init_num = 0; |
445 | } | |
446 | break; | |
447 | ||
448 | case SSL3_ST_SW_SRVR_DONE_A: | |
449 | case SSL3_ST_SW_SRVR_DONE_B: | |
450 | dtls1_start_timer(s); | |
451 | ret = dtls1_send_server_done(s); | |
452 | if (ret <= 0) | |
453 | goto end; | |
454 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; | |
455 | s->state = SSL3_ST_SW_FLUSH; | |
456 | s->init_num = 0; | |
457 | break; | |
458 | ||
459 | case SSL3_ST_SW_FLUSH: | |
460 | s->rwstate = SSL_WRITING; | |
461 | if (BIO_flush(s->wbio) <= 0) { | |
462 | ret = -1; | |
463 | goto end; | |
464 | } | |
465 | s->rwstate = SSL_NOTHING; | |
466 | s->state = s->s3->tmp.next_state; | |
467 | break; | |
468 | ||
469 | case SSL3_ST_SR_CERT_A: | |
470 | case SSL3_ST_SR_CERT_B: | |
471 | /* Check for second client hello (MS SGC) */ | |
472 | ret = ssl3_check_client_hello(s); | |
473 | if (ret <= 0) | |
474 | goto end; | |
475 | if (ret == 2) { | |
476 | dtls1_stop_timer(s); | |
477 | s->state = SSL3_ST_SR_CLNT_HELLO_C; | |
478 | } else { | |
479 | if (s->s3->tmp.cert_request) { | |
480 | ret = ssl3_get_client_certificate(s); | |
481 | if (ret <= 0) | |
482 | goto end; | |
483 | } | |
484 | s->init_num = 0; | |
485 | s->state = SSL3_ST_SR_KEY_EXCH_A; | |
486 | } | |
487 | break; | |
488 | ||
489 | case SSL3_ST_SR_KEY_EXCH_A: | |
490 | case SSL3_ST_SR_KEY_EXCH_B: | |
491 | ret = ssl3_get_client_key_exchange(s); | |
492 | if (ret <= 0) | |
493 | goto end; | |
494 | s->state = SSL3_ST_SR_CERT_VRFY_A; | |
495 | s->init_num = 0; | |
496 | ||
497 | /* | |
498 | * We need to get hashes here so if there is a client cert, it | |
499 | * can be verified | |
500 | */ | |
501 | s->method->ssl3_enc->cert_verify_mac(s, | |
502 | &(s->s3->finish_dgst1), | |
503 | &(s->s3-> | |
504 | tmp.cert_verify_md[0])); | |
505 | s->method->ssl3_enc->cert_verify_mac(s, &(s->s3->finish_dgst2), | |
506 | &(s->s3-> | |
507 | tmp.cert_verify_md | |
508 | [MD5_DIGEST_LENGTH])); | |
509 | ||
510 | break; | |
511 | ||
512 | case SSL3_ST_SR_CERT_VRFY_A: | |
513 | case SSL3_ST_SR_CERT_VRFY_B: | |
514 | ||
515 | s->d1->change_cipher_spec_ok = 1; | |
516 | /* we should decide if we expected this one */ | |
517 | ret = ssl3_get_cert_verify(s); | |
518 | if (ret <= 0) | |
519 | goto end; | |
520 | ||
521 | s->state = SSL3_ST_SR_FINISHED_A; | |
522 | s->init_num = 0; | |
523 | break; | |
524 | ||
525 | case SSL3_ST_SR_FINISHED_A: | |
526 | case SSL3_ST_SR_FINISHED_B: | |
527 | s->d1->change_cipher_spec_ok = 1; | |
528 | ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, | |
529 | SSL3_ST_SR_FINISHED_B); | |
530 | if (ret <= 0) | |
531 | goto end; | |
532 | dtls1_stop_timer(s); | |
533 | if (s->hit) | |
534 | s->state = SSL_ST_OK; | |
7a014dce | 535 | #ifndef OPENSSL_NO_TLSEXT |
40720ce3 MC |
536 | else if (s->tlsext_ticket_expected) |
537 | s->state = SSL3_ST_SW_SESSION_TICKET_A; | |
7a014dce | 538 | #endif |
40720ce3 MC |
539 | else |
540 | s->state = SSL3_ST_SW_CHANGE_A; | |
541 | s->init_num = 0; | |
542 | break; | |
36d16f8e | 543 | |
7a014dce | 544 | #ifndef OPENSSL_NO_TLSEXT |
40720ce3 MC |
545 | case SSL3_ST_SW_SESSION_TICKET_A: |
546 | case SSL3_ST_SW_SESSION_TICKET_B: | |
547 | ret = dtls1_send_newsession_ticket(s); | |
548 | if (ret <= 0) | |
549 | goto end; | |
550 | s->state = SSL3_ST_SW_CHANGE_A; | |
551 | s->init_num = 0; | |
552 | break; | |
553 | ||
554 | case SSL3_ST_SW_CERT_STATUS_A: | |
555 | case SSL3_ST_SW_CERT_STATUS_B: | |
556 | ret = ssl3_send_cert_status(s); | |
557 | if (ret <= 0) | |
558 | goto end; | |
559 | s->state = SSL3_ST_SW_KEY_EXCH_A; | |
560 | s->init_num = 0; | |
561 | break; | |
7a014dce DSH |
562 | |
563 | #endif | |
564 | ||
40720ce3 MC |
565 | case SSL3_ST_SW_CHANGE_A: |
566 | case SSL3_ST_SW_CHANGE_B: | |
567 | ||
568 | s->session->cipher = s->s3->tmp.new_cipher; | |
569 | if (!s->method->ssl3_enc->setup_key_block(s)) { | |
570 | ret = -1; | |
571 | goto end; | |
572 | } | |
573 | ||
574 | ret = dtls1_send_change_cipher_spec(s, | |
575 | SSL3_ST_SW_CHANGE_A, | |
576 | SSL3_ST_SW_CHANGE_B); | |
577 | ||
578 | if (ret <= 0) | |
579 | goto end; | |
580 | s->state = SSL3_ST_SW_FINISHED_A; | |
581 | s->init_num = 0; | |
582 | ||
583 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
584 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) | |
585 | { | |
586 | ret = -1; | |
587 | goto end; | |
588 | } | |
589 | ||
590 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | |
591 | break; | |
592 | ||
593 | case SSL3_ST_SW_FINISHED_A: | |
594 | case SSL3_ST_SW_FINISHED_B: | |
595 | ret = dtls1_send_finished(s, | |
596 | SSL3_ST_SW_FINISHED_A, | |
597 | SSL3_ST_SW_FINISHED_B, | |
598 | s->method-> | |
599 | ssl3_enc->server_finished_label, | |
600 | s->method-> | |
601 | ssl3_enc->server_finished_label_len); | |
602 | if (ret <= 0) | |
603 | goto end; | |
604 | s->state = SSL3_ST_SW_FLUSH; | |
605 | if (s->hit) | |
606 | s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A; | |
607 | else | |
608 | s->s3->tmp.next_state = SSL_ST_OK; | |
609 | s->init_num = 0; | |
610 | break; | |
611 | ||
612 | case SSL_ST_OK: | |
613 | /* clean a few things up */ | |
614 | ssl3_cleanup_key_block(s); | |
36d16f8e BL |
615 | |
616 | #if 0 | |
40720ce3 MC |
617 | BUF_MEM_free(s->init_buf); |
618 | s->init_buf = NULL; | |
36d16f8e BL |
619 | #endif |
620 | ||
40720ce3 MC |
621 | /* remove buffering on output */ |
622 | ssl_free_wbio_buffer(s); | |
623 | ||
624 | s->init_num = 0; | |
625 | ||
626 | if (s->new_session == 2) { /* skipped if we just sent a | |
627 | * HelloRequest */ | |
628 | /* | |
629 | * actually not necessarily a 'new' session unless | |
630 | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set | |
631 | */ | |
632 | ||
633 | s->new_session = 0; | |
634 | ||
635 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); | |
636 | ||
637 | s->ctx->stats.sess_accept_good++; | |
638 | /* s->server=1; */ | |
639 | s->handshake_func = dtls1_accept; | |
640 | ||
641 | if (cb != NULL) | |
642 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | |
643 | } | |
644 | ||
645 | ret = 1; | |
646 | ||
647 | /* done handshaking, next message is client hello */ | |
648 | s->d1->handshake_read_seq = 0; | |
649 | /* next message is server hello */ | |
650 | s->d1->handshake_write_seq = 0; | |
651 | s->d1->next_handshake_write_seq = 0; | |
652 | goto end; | |
653 | /* break; */ | |
654 | ||
655 | default: | |
656 | SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_UNKNOWN_STATE); | |
657 | ret = -1; | |
658 | goto end; | |
659 | /* break; */ | |
660 | } | |
661 | ||
662 | if (!s->s3->tmp.reuse_message && !skip) { | |
663 | if (s->debug) { | |
664 | if ((ret = BIO_flush(s->wbio)) <= 0) | |
665 | goto end; | |
666 | } | |
667 | ||
668 | if ((cb != NULL) && (s->state != state)) { | |
669 | new_state = s->state; | |
670 | s->state = state; | |
671 | cb(s, SSL_CB_ACCEPT_LOOP, 1); | |
672 | s->state = new_state; | |
673 | } | |
674 | } | |
675 | skip = 0; | |
676 | } | |
677 | end: | |
678 | /* BIO_flush(s->wbio); */ | |
679 | ||
680 | s->in_handshake--; | |
681 | if (cb != NULL) | |
682 | cb(s, SSL_CB_ACCEPT_EXIT, ret); | |
683 | return (ret); | |
684 | } | |
36d16f8e BL |
685 | |
686 | int dtls1_send_hello_request(SSL *s) | |
40720ce3 MC |
687 | { |
688 | unsigned char *p; | |
36d16f8e | 689 | |
40720ce3 MC |
690 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) { |
691 | p = (unsigned char *)s->init_buf->data; | |
692 | p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0); | |
36d16f8e | 693 | |
40720ce3 MC |
694 | s->state = SSL3_ST_SW_HELLO_REQ_B; |
695 | /* number of bytes to write */ | |
696 | s->init_num = DTLS1_HM_HEADER_LENGTH; | |
697 | s->init_off = 0; | |
36d16f8e | 698 | |
40720ce3 MC |
699 | /* |
700 | * no need to buffer this message, since there are no retransmit | |
701 | * requests for it | |
702 | */ | |
703 | } | |
36d16f8e | 704 | |
40720ce3 MC |
705 | /* SSL3_ST_SW_HELLO_REQ_B */ |
706 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
707 | } | |
36d16f8e BL |
708 | |
709 | int dtls1_send_hello_verify_request(SSL *s) | |
40720ce3 MC |
710 | { |
711 | unsigned int msg_len; | |
712 | unsigned char *msg, *buf, *p; | |
713 | ||
714 | if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) { | |
715 | buf = (unsigned char *)s->init_buf->data; | |
716 | ||
717 | msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]); | |
718 | if (s->client_version == DTLS1_BAD_VER) | |
719 | *(p++) = DTLS1_BAD_VER >> 8, *(p++) = DTLS1_BAD_VER & 0xff; | |
720 | else | |
721 | *(p++) = s->version >> 8, *(p++) = s->version & 0xFF; | |
722 | ||
723 | if (s->ctx->app_gen_cookie_cb == NULL || | |
724 | s->ctx->app_gen_cookie_cb(s, s->d1->cookie, | |
725 | &(s->d1->cookie_len)) == 0) { | |
726 | SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST, | |
727 | ERR_R_INTERNAL_ERROR); | |
728 | return 0; | |
729 | } | |
730 | ||
731 | *(p++) = (unsigned char)s->d1->cookie_len; | |
732 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | |
733 | p += s->d1->cookie_len; | |
734 | msg_len = p - msg; | |
735 | ||
736 | dtls1_set_message_header(s, buf, | |
737 | DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, | |
738 | msg_len); | |
739 | ||
740 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; | |
741 | /* number of bytes to write */ | |
742 | s->init_num = p - buf; | |
743 | s->init_off = 0; | |
744 | } | |
745 | ||
746 | /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ | |
747 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
748 | } | |
36d16f8e BL |
749 | |
750 | int dtls1_send_server_hello(SSL *s) | |
40720ce3 MC |
751 | { |
752 | unsigned char *buf; | |
753 | unsigned char *p, *d; | |
754 | int i; | |
755 | unsigned int sl; | |
756 | unsigned long l, Time; | |
757 | ||
758 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) { | |
759 | buf = (unsigned char *)s->init_buf->data; | |
760 | p = s->s3->server_random; | |
761 | Time = (unsigned long)time(NULL); /* Time */ | |
762 | l2n(Time, p); | |
763 | RAND_pseudo_bytes(p, SSL3_RANDOM_SIZE - 4); | |
764 | /* Do the message type and length last */ | |
765 | d = p = &(buf[DTLS1_HM_HEADER_LENGTH]); | |
766 | ||
767 | if (s->client_version == DTLS1_BAD_VER) | |
768 | *(p++) = DTLS1_BAD_VER >> 8, *(p++) = DTLS1_BAD_VER & 0xff; | |
769 | else | |
770 | *(p++) = s->version >> 8, *(p++) = s->version & 0xff; | |
771 | ||
772 | /* Random stuff */ | |
773 | memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE); | |
774 | p += SSL3_RANDOM_SIZE; | |
775 | ||
776 | /* | |
777 | * now in theory we have 3 options to sending back the session id. | |
778 | * If it is a re-use, we send back the old session-id, if it is a new | |
779 | * session, we send back the new session-id or we send back a 0 | |
780 | * length session-id if we want it to be single use. Currently I will | |
781 | * not implement the '0' length session-id 12-Jan-98 - I'll now | |
782 | * support the '0' length stuff. | |
783 | */ | |
784 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) | |
785 | s->session->session_id_length = 0; | |
786 | ||
787 | sl = s->session->session_id_length; | |
788 | if (sl > sizeof s->session->session_id) { | |
789 | SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR); | |
790 | return -1; | |
791 | } | |
792 | *(p++) = sl; | |
793 | memcpy(p, s->session->session_id, sl); | |
794 | p += sl; | |
795 | ||
796 | /* put the cipher */ | |
797 | if (s->s3->tmp.new_cipher == NULL) | |
798 | return -1; | |
799 | i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p); | |
800 | p += i; | |
801 | ||
802 | /* put the compression method */ | |
54f51116 | 803 | #ifdef OPENSSL_NO_COMP |
40720ce3 | 804 | *(p++) = 0; |
54f51116 | 805 | #else |
40720ce3 MC |
806 | if (s->s3->tmp.new_compression == NULL) |
807 | *(p++) = 0; | |
808 | else | |
809 | *(p++) = s->s3->tmp.new_compression->id; | |
54f51116 | 810 | #endif |
36d16f8e | 811 | |
82e448b9 | 812 | #ifndef OPENSSL_NO_TLSEXT |
40720ce3 MC |
813 | if ((p = |
814 | ssl_add_serverhello_tlsext(s, p, | |
815 | buf + SSL3_RT_MAX_PLAIN_LENGTH)) == | |
816 | NULL) { | |
817 | SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR); | |
818 | return -1; | |
819 | } | |
82e448b9 DSH |
820 | #endif |
821 | ||
40720ce3 MC |
822 | /* do the header */ |
823 | l = (p - d); | |
824 | d = buf; | |
36d16f8e | 825 | |
40720ce3 | 826 | d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l); |
36d16f8e | 827 | |
40720ce3 MC |
828 | s->state = SSL3_ST_SW_SRVR_HELLO_B; |
829 | /* number of bytes to write */ | |
830 | s->init_num = p - buf; | |
831 | s->init_off = 0; | |
36d16f8e | 832 | |
40720ce3 MC |
833 | /* buffer the message to handle re-xmits */ |
834 | dtls1_buffer_message(s, 0); | |
835 | } | |
36d16f8e | 836 | |
40720ce3 MC |
837 | /* SSL3_ST_SW_SRVR_HELLO_B */ |
838 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
839 | } | |
36d16f8e BL |
840 | |
841 | int dtls1_send_server_done(SSL *s) | |
40720ce3 MC |
842 | { |
843 | unsigned char *p; | |
36d16f8e | 844 | |
40720ce3 MC |
845 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) { |
846 | p = (unsigned char *)s->init_buf->data; | |
36d16f8e | 847 | |
40720ce3 MC |
848 | /* do the header */ |
849 | p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0); | |
36d16f8e | 850 | |
40720ce3 MC |
851 | s->state = SSL3_ST_SW_SRVR_DONE_B; |
852 | /* number of bytes to write */ | |
853 | s->init_num = DTLS1_HM_HEADER_LENGTH; | |
854 | s->init_off = 0; | |
36d16f8e | 855 | |
40720ce3 MC |
856 | /* buffer the message to handle re-xmits */ |
857 | dtls1_buffer_message(s, 0); | |
858 | } | |
36d16f8e | 859 | |
40720ce3 MC |
860 | /* SSL3_ST_SW_SRVR_DONE_B */ |
861 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
862 | } | |
36d16f8e BL |
863 | |
864 | int dtls1_send_server_key_exchange(SSL *s) | |
40720ce3 | 865 | { |
36d16f8e | 866 | #ifndef OPENSSL_NO_RSA |
40720ce3 MC |
867 | unsigned char *q; |
868 | int j, num; | |
869 | RSA *rsa; | |
870 | unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; | |
871 | unsigned int u; | |
36d16f8e BL |
872 | #endif |
873 | #ifndef OPENSSL_NO_DH | |
40720ce3 | 874 | DH *dh = NULL, *dhp; |
36d16f8e | 875 | #endif |
40720ce3 MC |
876 | EVP_PKEY *pkey; |
877 | unsigned char *p, *d; | |
878 | int al, i; | |
879 | unsigned long type; | |
880 | int n; | |
881 | CERT *cert; | |
882 | BIGNUM *r[4]; | |
883 | int nr[4], kn; | |
884 | BUF_MEM *buf; | |
885 | EVP_MD_CTX md_ctx; | |
886 | ||
887 | EVP_MD_CTX_init(&md_ctx); | |
888 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) { | |
889 | type = s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK; | |
890 | cert = s->cert; | |
891 | ||
892 | buf = s->init_buf; | |
893 | ||
894 | r[0] = r[1] = r[2] = r[3] = NULL; | |
895 | n = 0; | |
36d16f8e | 896 | #ifndef OPENSSL_NO_RSA |
40720ce3 MC |
897 | if (type & SSL_kRSA) { |
898 | rsa = cert->rsa_tmp; | |
899 | if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) { | |
900 | rsa = s->cert->rsa_tmp_cb(s, | |
901 | SSL_C_IS_EXPORT(s->s3-> | |
902 | tmp.new_cipher), | |
903 | SSL_C_EXPORT_PKEYLENGTH(s->s3-> | |
904 | tmp.new_cipher)); | |
905 | if (rsa == NULL) { | |
906 | al = SSL_AD_HANDSHAKE_FAILURE; | |
907 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
908 | SSL_R_ERROR_GENERATING_TMP_RSA_KEY); | |
909 | goto f_err; | |
910 | } | |
911 | RSA_up_ref(rsa); | |
912 | cert->rsa_tmp = rsa; | |
913 | } | |
914 | if (rsa == NULL) { | |
915 | al = SSL_AD_HANDSHAKE_FAILURE; | |
916 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
917 | SSL_R_MISSING_TMP_RSA_KEY); | |
918 | goto f_err; | |
919 | } | |
920 | r[0] = rsa->n; | |
921 | r[1] = rsa->e; | |
922 | s->s3->tmp.use_rsa_tmp = 1; | |
923 | } else | |
36d16f8e BL |
924 | #endif |
925 | #ifndef OPENSSL_NO_DH | |
40720ce3 MC |
926 | if (type & SSL_kEDH) { |
927 | dhp = cert->dh_tmp; | |
928 | if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) | |
929 | dhp = s->cert->dh_tmp_cb(s, | |
930 | SSL_C_IS_EXPORT(s->s3-> | |
931 | tmp.new_cipher), | |
932 | SSL_C_EXPORT_PKEYLENGTH(s->s3-> | |
933 | tmp.new_cipher)); | |
934 | if (dhp == NULL) { | |
935 | al = SSL_AD_HANDSHAKE_FAILURE; | |
936 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
937 | SSL_R_MISSING_TMP_DH_KEY); | |
938 | goto f_err; | |
939 | } | |
940 | ||
941 | if (s->s3->tmp.dh != NULL) { | |
942 | DH_free(dh); | |
943 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
944 | ERR_R_INTERNAL_ERROR); | |
945 | goto err; | |
946 | } | |
947 | ||
948 | if ((dh = DHparams_dup(dhp)) == NULL) { | |
949 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); | |
950 | goto err; | |
951 | } | |
952 | ||
953 | s->s3->tmp.dh = dh; | |
954 | if ((dhp->pub_key == NULL || | |
955 | dhp->priv_key == NULL || | |
956 | (s->options & SSL_OP_SINGLE_DH_USE))) { | |
957 | if (!DH_generate_key(dh)) { | |
958 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
959 | ERR_R_DH_LIB); | |
960 | goto err; | |
961 | } | |
962 | } else { | |
963 | dh->pub_key = BN_dup(dhp->pub_key); | |
964 | dh->priv_key = BN_dup(dhp->priv_key); | |
965 | if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) { | |
966 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
967 | ERR_R_DH_LIB); | |
968 | goto err; | |
969 | } | |
970 | } | |
971 | r[0] = dh->p; | |
972 | r[1] = dh->g; | |
973 | r[2] = dh->pub_key; | |
974 | } else | |
36d16f8e | 975 | #endif |
40720ce3 MC |
976 | { |
977 | al = SSL_AD_HANDSHAKE_FAILURE; | |
978 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
979 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | |
980 | goto f_err; | |
981 | } | |
982 | for (i = 0; r[i] != NULL; i++) { | |
983 | nr[i] = BN_num_bytes(r[i]); | |
984 | n += 2 + nr[i]; | |
985 | } | |
986 | ||
987 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) { | |
988 | if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher)) | |
989 | == NULL) { | |
990 | al = SSL_AD_DECODE_ERROR; | |
991 | goto f_err; | |
992 | } | |
993 | kn = EVP_PKEY_size(pkey); | |
994 | } else { | |
995 | pkey = NULL; | |
996 | kn = 0; | |
997 | } | |
998 | ||
999 | if (!BUF_MEM_grow_clean(buf, n + DTLS1_HM_HEADER_LENGTH + kn)) { | |
1000 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF); | |
1001 | goto err; | |
1002 | } | |
1003 | d = (unsigned char *)s->init_buf->data; | |
1004 | p = &(d[DTLS1_HM_HEADER_LENGTH]); | |
1005 | ||
1006 | for (i = 0; r[i] != NULL; i++) { | |
1007 | s2n(nr[i], p); | |
1008 | BN_bn2bin(r[i], p); | |
1009 | p += nr[i]; | |
1010 | } | |
1011 | ||
1012 | /* not anonymous */ | |
1013 | if (pkey != NULL) { | |
1014 | /* | |
1015 | * n is the length of the params, they start at | |
1016 | * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space at the | |
1017 | * end. | |
1018 | */ | |
36d16f8e | 1019 | #ifndef OPENSSL_NO_RSA |
40720ce3 MC |
1020 | if (pkey->type == EVP_PKEY_RSA) { |
1021 | q = md_buf; | |
1022 | j = 0; | |
1023 | for (num = 2; num > 0; num--) { | |
1024 | EVP_DigestInit_ex(&md_ctx, (num == 2) | |
1025 | ? s->ctx->md5 : s->ctx->sha1, NULL); | |
1026 | EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]), | |
1027 | SSL3_RANDOM_SIZE); | |
1028 | EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]), | |
1029 | SSL3_RANDOM_SIZE); | |
1030 | EVP_DigestUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), | |
1031 | n); | |
1032 | EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i); | |
1033 | q += i; | |
1034 | j += i; | |
1035 | } | |
1036 | if (RSA_sign(NID_md5_sha1, md_buf, j, | |
1037 | &(p[2]), &u, pkey->pkey.rsa) <= 0) { | |
1038 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA); | |
1039 | goto err; | |
1040 | } | |
1041 | s2n(u, p); | |
1042 | n += u + 2; | |
1043 | } else | |
36d16f8e BL |
1044 | #endif |
1045 | #if !defined(OPENSSL_NO_DSA) | |
40720ce3 MC |
1046 | if (pkey->type == EVP_PKEY_DSA) { |
1047 | /* lets do DSS */ | |
1048 | EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL); | |
1049 | EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]), | |
1050 | SSL3_RANDOM_SIZE); | |
1051 | EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]), | |
1052 | SSL3_RANDOM_SIZE); | |
1053 | EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n); | |
1054 | if (!EVP_SignFinal(&md_ctx, &(p[2]), | |
1055 | (unsigned int *)&i, pkey)) { | |
1056 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA); | |
1057 | goto err; | |
1058 | } | |
1059 | s2n(i, p); | |
1060 | n += i + 2; | |
1061 | } else | |
36d16f8e | 1062 | #endif |
40720ce3 MC |
1063 | { |
1064 | /* Is this error check actually needed? */ | |
1065 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1066 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
1067 | SSL_R_UNKNOWN_PKEY_TYPE); | |
1068 | goto f_err; | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | d = dtls1_set_message_header(s, d, | |
1073 | SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n); | |
1074 | ||
1075 | /* | |
1076 | * we should now have things packed up, so lets send it off | |
1077 | */ | |
1078 | s->init_num = n + DTLS1_HM_HEADER_LENGTH; | |
1079 | s->init_off = 0; | |
1080 | ||
1081 | /* buffer the message to handle re-xmits */ | |
1082 | dtls1_buffer_message(s, 0); | |
1083 | } | |
1084 | ||
1085 | s->state = SSL3_ST_SW_KEY_EXCH_B; | |
1086 | EVP_MD_CTX_cleanup(&md_ctx); | |
1087 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
1088 | f_err: | |
1089 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1090 | err: | |
1091 | EVP_MD_CTX_cleanup(&md_ctx); | |
1092 | return (-1); | |
1093 | } | |
36d16f8e BL |
1094 | |
1095 | int dtls1_send_certificate_request(SSL *s) | |
40720ce3 MC |
1096 | { |
1097 | unsigned char *p, *d; | |
1098 | int i, j, nl, off, n; | |
1099 | STACK_OF(X509_NAME) *sk = NULL; | |
1100 | X509_NAME *name; | |
1101 | BUF_MEM *buf; | |
1102 | unsigned int msg_len; | |
1103 | ||
1104 | if (s->state == SSL3_ST_SW_CERT_REQ_A) { | |
1105 | buf = s->init_buf; | |
1106 | ||
1107 | d = p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]); | |
1108 | ||
1109 | /* get the list of acceptable cert types */ | |
1110 | p++; | |
1111 | n = ssl3_get_req_cert_type(s, p); | |
1112 | d[0] = n; | |
1113 | p += n; | |
1114 | n++; | |
1115 | ||
1116 | off = n; | |
1117 | p += 2; | |
1118 | n += 2; | |
1119 | ||
1120 | sk = SSL_get_client_CA_list(s); | |
1121 | nl = 0; | |
1122 | if (sk != NULL) { | |
1123 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { | |
1124 | name = sk_X509_NAME_value(sk, i); | |
1125 | j = i2d_X509_NAME(name, NULL); | |
1126 | if (!BUF_MEM_grow_clean | |
1127 | (buf, DTLS1_HM_HEADER_LENGTH + n + j + 2)) { | |
1128 | SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST, | |
1129 | ERR_R_BUF_LIB); | |
1130 | goto err; | |
1131 | } | |
1132 | p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + n]); | |
1133 | if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) { | |
1134 | s2n(j, p); | |
1135 | i2d_X509_NAME(name, &p); | |
1136 | n += 2 + j; | |
1137 | nl += 2 + j; | |
1138 | } else { | |
1139 | d = p; | |
1140 | i2d_X509_NAME(name, &p); | |
1141 | j -= 2; | |
1142 | s2n(j, d); | |
1143 | j += 2; | |
1144 | n += j; | |
1145 | nl += j; | |
1146 | } | |
1147 | } | |
1148 | } | |
1149 | /* else no CA names */ | |
1150 | p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + off]); | |
1151 | s2n(nl, p); | |
1152 | ||
1153 | d = (unsigned char *)buf->data; | |
1154 | *(d++) = SSL3_MT_CERTIFICATE_REQUEST; | |
1155 | l2n3(n, d); | |
1156 | s2n(s->d1->handshake_write_seq, d); | |
1157 | s->d1->handshake_write_seq++; | |
1158 | ||
1159 | /* | |
1160 | * we should now have things packed up, so lets send it off | |
1161 | */ | |
1162 | ||
1163 | s->init_num = n + DTLS1_HM_HEADER_LENGTH; | |
1164 | s->init_off = 0; | |
36d16f8e BL |
1165 | #ifdef NETSCAPE_HANG_BUG |
1166 | /* XXX: what to do about this? */ | |
40720ce3 MC |
1167 | p = (unsigned char *)s->init_buf->data + s->init_num; |
1168 | ||
1169 | /* do the header */ | |
1170 | *(p++) = SSL3_MT_SERVER_DONE; | |
1171 | *(p++) = 0; | |
1172 | *(p++) = 0; | |
1173 | *(p++) = 0; | |
1174 | s->init_num += 4; | |
36d16f8e BL |
1175 | #endif |
1176 | ||
40720ce3 MC |
1177 | /* XDTLS: set message header ? */ |
1178 | msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH; | |
1179 | dtls1_set_message_header(s, (void *)s->init_buf->data, | |
1180 | SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0, | |
1181 | msg_len); | |
25b0e072 | 1182 | |
40720ce3 MC |
1183 | /* buffer the message to handle re-xmits */ |
1184 | dtls1_buffer_message(s, 0); | |
36d16f8e | 1185 | |
40720ce3 MC |
1186 | s->state = SSL3_ST_SW_CERT_REQ_B; |
1187 | } | |
36d16f8e | 1188 | |
40720ce3 MC |
1189 | /* SSL3_ST_SW_CERT_REQ_B */ |
1190 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
1191 | err: | |
1192 | return (-1); | |
1193 | } | |
36d16f8e BL |
1194 | |
1195 | int dtls1_send_server_certificate(SSL *s) | |
40720ce3 MC |
1196 | { |
1197 | unsigned long l; | |
1198 | X509 *x; | |
1199 | ||
1200 | if (s->state == SSL3_ST_SW_CERT_A) { | |
1201 | x = ssl_get_server_send_cert(s); | |
1202 | if (x == NULL && | |
1203 | /* VRS: allow null cert if auth == KRB5 */ | |
1204 | (s->s3->tmp.new_cipher->algorithms | |
1205 | & (SSL_MKEY_MASK | SSL_AUTH_MASK)) | |
1206 | != (SSL_aKRB5 | SSL_kKRB5)) { | |
1207 | SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR); | |
1208 | return (0); | |
1209 | } | |
1210 | ||
1211 | l = dtls1_output_cert_chain(s, x); | |
1212 | s->state = SSL3_ST_SW_CERT_B; | |
1213 | s->init_num = (int)l; | |
1214 | s->init_off = 0; | |
1215 | ||
1216 | /* buffer the message to handle re-xmits */ | |
1217 | dtls1_buffer_message(s, 0); | |
1218 | } | |
1219 | ||
1220 | /* SSL3_ST_SW_CERT_B */ | |
1221 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
1222 | } | |
7a014dce DSH |
1223 | |
1224 | #ifndef OPENSSL_NO_TLSEXT | |
1225 | int dtls1_send_newsession_ticket(SSL *s) | |
40720ce3 MC |
1226 | { |
1227 | if (s->state == SSL3_ST_SW_SESSION_TICKET_A) { | |
1228 | unsigned char *p, *senc, *macstart; | |
1229 | int len, slen; | |
1230 | unsigned int hlen, msg_len; | |
1231 | EVP_CIPHER_CTX ctx; | |
1232 | HMAC_CTX hctx; | |
1233 | SSL_CTX *tctx = s->initial_ctx; | |
1234 | unsigned char iv[EVP_MAX_IV_LENGTH]; | |
1235 | unsigned char key_name[16]; | |
1236 | ||
1237 | /* get session encoding length */ | |
1238 | slen = i2d_SSL_SESSION(s->session, NULL); | |
1239 | /* | |
1240 | * Some length values are 16 bits, so forget it if session is too | |
1241 | * long | |
1242 | */ | |
1243 | if (slen > 0xFF00) | |
1244 | return -1; | |
1245 | /* | |
1246 | * Grow buffer if need be: the length calculation is as follows 12 | |
1247 | * (DTLS handshake message header) + 4 (ticket lifetime hint) + 2 | |
1248 | * (ticket length) + 16 (key name) + max_iv_len (iv length) + | |
1249 | * session_length + max_enc_block_size (max encrypted session length) | |
1250 | * + max_md_size (HMAC). | |
1251 | */ | |
1252 | if (!BUF_MEM_grow(s->init_buf, | |
1253 | DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH + | |
1254 | EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen)) | |
1255 | return -1; | |
1256 | senc = OPENSSL_malloc(slen); | |
1257 | if (!senc) | |
1258 | return -1; | |
1259 | p = senc; | |
1260 | i2d_SSL_SESSION(s->session, &p); | |
1261 | ||
1262 | p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]); | |
1263 | EVP_CIPHER_CTX_init(&ctx); | |
1264 | HMAC_CTX_init(&hctx); | |
1265 | /* | |
1266 | * Initialize HMAC and cipher contexts. If callback present it does | |
1267 | * all the work otherwise use generated values from parent ctx. | |
1268 | */ | |
1269 | if (tctx->tlsext_ticket_key_cb) { | |
1270 | if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, | |
1271 | &hctx, 1) < 0) { | |
1272 | OPENSSL_free(senc); | |
1273 | return -1; | |
1274 | } | |
1275 | } else { | |
1276 | RAND_pseudo_bytes(iv, 16); | |
1277 | EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, | |
1278 | tctx->tlsext_tick_aes_key, iv); | |
1279 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, | |
1280 | tlsext_tick_md(), NULL); | |
1281 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); | |
1282 | } | |
1283 | l2n(s->session->tlsext_tick_lifetime_hint, p); | |
1284 | /* Skip ticket length for now */ | |
1285 | p += 2; | |
1286 | /* Output key name */ | |
1287 | macstart = p; | |
1288 | memcpy(p, key_name, 16); | |
1289 | p += 16; | |
1290 | /* output IV */ | |
1291 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); | |
1292 | p += EVP_CIPHER_CTX_iv_length(&ctx); | |
1293 | /* Encrypt session data */ | |
1294 | EVP_EncryptUpdate(&ctx, p, &len, senc, slen); | |
1295 | p += len; | |
1296 | EVP_EncryptFinal(&ctx, p, &len); | |
1297 | p += len; | |
1298 | EVP_CIPHER_CTX_cleanup(&ctx); | |
1299 | ||
1300 | HMAC_Update(&hctx, macstart, p - macstart); | |
1301 | HMAC_Final(&hctx, p, &hlen); | |
1302 | HMAC_CTX_cleanup(&hctx); | |
1303 | ||
1304 | p += hlen; | |
1305 | /* Now write out lengths: p points to end of data written */ | |
1306 | /* Total length */ | |
1307 | len = p - (unsigned char *)(s->init_buf->data); | |
1308 | /* Ticket length */ | |
1309 | p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4; | |
1310 | s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p); | |
1311 | ||
1312 | /* number of bytes to write */ | |
1313 | s->init_num = len; | |
1314 | s->state = SSL3_ST_SW_SESSION_TICKET_B; | |
1315 | s->init_off = 0; | |
1316 | OPENSSL_free(senc); | |
1317 | ||
1318 | /* XDTLS: set message header ? */ | |
1319 | msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH; | |
1320 | dtls1_set_message_header(s, (void *)s->init_buf->data, | |
1321 | SSL3_MT_NEWSESSION_TICKET, msg_len, 0, | |
1322 | msg_len); | |
1323 | ||
1324 | /* buffer the message to handle re-xmits */ | |
1325 | dtls1_buffer_message(s, 0); | |
1326 | } | |
1327 | ||
1328 | /* SSL3_ST_SW_SESSION_TICKET_B */ | |
1329 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
1330 | } | |
c0e94f82 | 1331 | #endif |