]>
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) | |
6f1f3c66 | 383 | && EVP_PKEY_size(s->cert->pkeys |
40720ce3 MC |
384 | [SSL_PKEY_RSA_ENC].privatekey) * |
385 | 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) | |
386 | ) | |
387 | ) | |
388 | ) | |
389 | ) { | |
390 | dtls1_start_timer(s); | |
391 | ret = dtls1_send_server_key_exchange(s); | |
392 | if (ret <= 0) | |
393 | goto end; | |
394 | } else | |
395 | skip = 1; | |
396 | ||
397 | s->state = SSL3_ST_SW_CERT_REQ_A; | |
398 | s->init_num = 0; | |
399 | break; | |
400 | ||
401 | case SSL3_ST_SW_CERT_REQ_A: | |
402 | case SSL3_ST_SW_CERT_REQ_B: | |
403 | if ( /* don't request cert unless asked for it: */ | |
404 | !(s->verify_mode & SSL_VERIFY_PEER) || | |
405 | /* | |
406 | * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert | |
407 | * during re-negotiation: | |
408 | */ | |
409 | ((s->session->peer != NULL) && | |
410 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || | |
411 | /* | |
412 | * never request cert in anonymous ciphersuites (see | |
413 | * section "Certificate request" in SSL 3 drafts and in | |
414 | * RFC 2246): | |
415 | */ | |
416 | ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) && | |
417 | /* | |
418 | * ... except when the application insists on | |
419 | * verification (against the specs, but s3_clnt.c accepts | |
420 | * this for SSL 3) | |
421 | */ | |
422 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || | |
423 | /* | |
424 | * never request cert in Kerberos ciphersuites | |
425 | */ | |
426 | (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5)) { | |
427 | /* no cert request */ | |
428 | skip = 1; | |
429 | s->s3->tmp.cert_request = 0; | |
430 | s->state = SSL3_ST_SW_SRVR_DONE_A; | |
431 | } else { | |
432 | s->s3->tmp.cert_request = 1; | |
433 | dtls1_start_timer(s); | |
434 | ret = dtls1_send_certificate_request(s); | |
435 | if (ret <= 0) | |
436 | goto end; | |
36d16f8e | 437 | #ifndef NETSCAPE_HANG_BUG |
40720ce3 | 438 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
36d16f8e | 439 | #else |
40720ce3 MC |
440 | s->state = SSL3_ST_SW_FLUSH; |
441 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; | |
36d16f8e | 442 | #endif |
40720ce3 MC |
443 | s->init_num = 0; |
444 | } | |
445 | break; | |
446 | ||
447 | case SSL3_ST_SW_SRVR_DONE_A: | |
448 | case SSL3_ST_SW_SRVR_DONE_B: | |
449 | dtls1_start_timer(s); | |
450 | ret = dtls1_send_server_done(s); | |
451 | if (ret <= 0) | |
452 | goto end; | |
453 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; | |
454 | s->state = SSL3_ST_SW_FLUSH; | |
455 | s->init_num = 0; | |
456 | break; | |
457 | ||
458 | case SSL3_ST_SW_FLUSH: | |
459 | s->rwstate = SSL_WRITING; | |
460 | if (BIO_flush(s->wbio) <= 0) { | |
461 | ret = -1; | |
462 | goto end; | |
463 | } | |
464 | s->rwstate = SSL_NOTHING; | |
465 | s->state = s->s3->tmp.next_state; | |
466 | break; | |
467 | ||
468 | case SSL3_ST_SR_CERT_A: | |
469 | case SSL3_ST_SR_CERT_B: | |
470 | /* Check for second client hello (MS SGC) */ | |
471 | ret = ssl3_check_client_hello(s); | |
472 | if (ret <= 0) | |
473 | goto end; | |
474 | if (ret == 2) { | |
475 | dtls1_stop_timer(s); | |
476 | s->state = SSL3_ST_SR_CLNT_HELLO_C; | |
477 | } else { | |
478 | if (s->s3->tmp.cert_request) { | |
479 | ret = ssl3_get_client_certificate(s); | |
480 | if (ret <= 0) | |
481 | goto end; | |
482 | } | |
483 | s->init_num = 0; | |
484 | s->state = SSL3_ST_SR_KEY_EXCH_A; | |
485 | } | |
486 | break; | |
487 | ||
488 | case SSL3_ST_SR_KEY_EXCH_A: | |
489 | case SSL3_ST_SR_KEY_EXCH_B: | |
490 | ret = ssl3_get_client_key_exchange(s); | |
491 | if (ret <= 0) | |
492 | goto end; | |
493 | s->state = SSL3_ST_SR_CERT_VRFY_A; | |
494 | s->init_num = 0; | |
495 | ||
496 | /* | |
497 | * We need to get hashes here so if there is a client cert, it | |
498 | * can be verified | |
499 | */ | |
500 | s->method->ssl3_enc->cert_verify_mac(s, | |
501 | &(s->s3->finish_dgst1), | |
502 | &(s->s3-> | |
503 | tmp.cert_verify_md[0])); | |
504 | s->method->ssl3_enc->cert_verify_mac(s, &(s->s3->finish_dgst2), | |
505 | &(s->s3-> | |
506 | tmp.cert_verify_md | |
507 | [MD5_DIGEST_LENGTH])); | |
508 | ||
509 | break; | |
510 | ||
511 | case SSL3_ST_SR_CERT_VRFY_A: | |
512 | case SSL3_ST_SR_CERT_VRFY_B: | |
513 | ||
514 | s->d1->change_cipher_spec_ok = 1; | |
515 | /* we should decide if we expected this one */ | |
516 | ret = ssl3_get_cert_verify(s); | |
517 | if (ret <= 0) | |
518 | goto end; | |
519 | ||
520 | s->state = SSL3_ST_SR_FINISHED_A; | |
521 | s->init_num = 0; | |
522 | break; | |
523 | ||
524 | case SSL3_ST_SR_FINISHED_A: | |
525 | case SSL3_ST_SR_FINISHED_B: | |
526 | s->d1->change_cipher_spec_ok = 1; | |
527 | ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, | |
528 | SSL3_ST_SR_FINISHED_B); | |
529 | if (ret <= 0) | |
530 | goto end; | |
531 | dtls1_stop_timer(s); | |
532 | if (s->hit) | |
533 | s->state = SSL_ST_OK; | |
7a014dce | 534 | #ifndef OPENSSL_NO_TLSEXT |
40720ce3 MC |
535 | else if (s->tlsext_ticket_expected) |
536 | s->state = SSL3_ST_SW_SESSION_TICKET_A; | |
7a014dce | 537 | #endif |
40720ce3 MC |
538 | else |
539 | s->state = SSL3_ST_SW_CHANGE_A; | |
540 | s->init_num = 0; | |
541 | break; | |
36d16f8e | 542 | |
7a014dce | 543 | #ifndef OPENSSL_NO_TLSEXT |
40720ce3 MC |
544 | case SSL3_ST_SW_SESSION_TICKET_A: |
545 | case SSL3_ST_SW_SESSION_TICKET_B: | |
546 | ret = dtls1_send_newsession_ticket(s); | |
547 | if (ret <= 0) | |
548 | goto end; | |
549 | s->state = SSL3_ST_SW_CHANGE_A; | |
550 | s->init_num = 0; | |
551 | break; | |
552 | ||
553 | case SSL3_ST_SW_CERT_STATUS_A: | |
554 | case SSL3_ST_SW_CERT_STATUS_B: | |
555 | ret = ssl3_send_cert_status(s); | |
556 | if (ret <= 0) | |
557 | goto end; | |
558 | s->state = SSL3_ST_SW_KEY_EXCH_A; | |
559 | s->init_num = 0; | |
560 | break; | |
7a014dce DSH |
561 | |
562 | #endif | |
563 | ||
40720ce3 MC |
564 | case SSL3_ST_SW_CHANGE_A: |
565 | case SSL3_ST_SW_CHANGE_B: | |
566 | ||
567 | s->session->cipher = s->s3->tmp.new_cipher; | |
568 | if (!s->method->ssl3_enc->setup_key_block(s)) { | |
569 | ret = -1; | |
570 | goto end; | |
571 | } | |
572 | ||
573 | ret = dtls1_send_change_cipher_spec(s, | |
574 | SSL3_ST_SW_CHANGE_A, | |
575 | SSL3_ST_SW_CHANGE_B); | |
576 | ||
577 | if (ret <= 0) | |
578 | goto end; | |
579 | s->state = SSL3_ST_SW_FINISHED_A; | |
580 | s->init_num = 0; | |
581 | ||
582 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
583 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) | |
584 | { | |
585 | ret = -1; | |
586 | goto end; | |
587 | } | |
588 | ||
589 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | |
590 | break; | |
591 | ||
592 | case SSL3_ST_SW_FINISHED_A: | |
593 | case SSL3_ST_SW_FINISHED_B: | |
594 | ret = dtls1_send_finished(s, | |
595 | SSL3_ST_SW_FINISHED_A, | |
596 | SSL3_ST_SW_FINISHED_B, | |
597 | s->method-> | |
598 | ssl3_enc->server_finished_label, | |
599 | s->method-> | |
600 | ssl3_enc->server_finished_label_len); | |
601 | if (ret <= 0) | |
602 | goto end; | |
603 | s->state = SSL3_ST_SW_FLUSH; | |
604 | if (s->hit) | |
605 | s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A; | |
606 | else | |
607 | s->s3->tmp.next_state = SSL_ST_OK; | |
608 | s->init_num = 0; | |
609 | break; | |
610 | ||
611 | case SSL_ST_OK: | |
612 | /* clean a few things up */ | |
613 | ssl3_cleanup_key_block(s); | |
36d16f8e BL |
614 | |
615 | #if 0 | |
40720ce3 MC |
616 | BUF_MEM_free(s->init_buf); |
617 | s->init_buf = NULL; | |
36d16f8e BL |
618 | #endif |
619 | ||
40720ce3 MC |
620 | /* remove buffering on output */ |
621 | ssl_free_wbio_buffer(s); | |
622 | ||
623 | s->init_num = 0; | |
624 | ||
625 | if (s->new_session == 2) { /* skipped if we just sent a | |
626 | * HelloRequest */ | |
627 | /* | |
628 | * actually not necessarily a 'new' session unless | |
629 | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set | |
630 | */ | |
631 | ||
632 | s->new_session = 0; | |
633 | ||
634 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); | |
635 | ||
636 | s->ctx->stats.sess_accept_good++; | |
637 | /* s->server=1; */ | |
638 | s->handshake_func = dtls1_accept; | |
639 | ||
640 | if (cb != NULL) | |
641 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | |
642 | } | |
643 | ||
644 | ret = 1; | |
645 | ||
646 | /* done handshaking, next message is client hello */ | |
647 | s->d1->handshake_read_seq = 0; | |
648 | /* next message is server hello */ | |
649 | s->d1->handshake_write_seq = 0; | |
650 | s->d1->next_handshake_write_seq = 0; | |
651 | goto end; | |
652 | /* break; */ | |
653 | ||
654 | default: | |
655 | SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_UNKNOWN_STATE); | |
656 | ret = -1; | |
657 | goto end; | |
658 | /* break; */ | |
659 | } | |
660 | ||
661 | if (!s->s3->tmp.reuse_message && !skip) { | |
662 | if (s->debug) { | |
663 | if ((ret = BIO_flush(s->wbio)) <= 0) | |
664 | goto end; | |
665 | } | |
666 | ||
667 | if ((cb != NULL) && (s->state != state)) { | |
668 | new_state = s->state; | |
669 | s->state = state; | |
670 | cb(s, SSL_CB_ACCEPT_LOOP, 1); | |
671 | s->state = new_state; | |
672 | } | |
673 | } | |
674 | skip = 0; | |
675 | } | |
676 | end: | |
677 | /* BIO_flush(s->wbio); */ | |
678 | ||
679 | s->in_handshake--; | |
680 | if (cb != NULL) | |
681 | cb(s, SSL_CB_ACCEPT_EXIT, ret); | |
682 | return (ret); | |
683 | } | |
36d16f8e BL |
684 | |
685 | int dtls1_send_hello_request(SSL *s) | |
40720ce3 MC |
686 | { |
687 | unsigned char *p; | |
36d16f8e | 688 | |
40720ce3 MC |
689 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) { |
690 | p = (unsigned char *)s->init_buf->data; | |
691 | p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0); | |
36d16f8e | 692 | |
40720ce3 MC |
693 | s->state = SSL3_ST_SW_HELLO_REQ_B; |
694 | /* number of bytes to write */ | |
695 | s->init_num = DTLS1_HM_HEADER_LENGTH; | |
696 | s->init_off = 0; | |
36d16f8e | 697 | |
40720ce3 MC |
698 | /* |
699 | * no need to buffer this message, since there are no retransmit | |
700 | * requests for it | |
701 | */ | |
702 | } | |
36d16f8e | 703 | |
40720ce3 MC |
704 | /* SSL3_ST_SW_HELLO_REQ_B */ |
705 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
706 | } | |
36d16f8e BL |
707 | |
708 | int dtls1_send_hello_verify_request(SSL *s) | |
40720ce3 MC |
709 | { |
710 | unsigned int msg_len; | |
711 | unsigned char *msg, *buf, *p; | |
712 | ||
713 | if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) { | |
714 | buf = (unsigned char *)s->init_buf->data; | |
715 | ||
716 | msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]); | |
717 | if (s->client_version == DTLS1_BAD_VER) | |
718 | *(p++) = DTLS1_BAD_VER >> 8, *(p++) = DTLS1_BAD_VER & 0xff; | |
719 | else | |
720 | *(p++) = s->version >> 8, *(p++) = s->version & 0xFF; | |
721 | ||
722 | if (s->ctx->app_gen_cookie_cb == NULL || | |
723 | s->ctx->app_gen_cookie_cb(s, s->d1->cookie, | |
724 | &(s->d1->cookie_len)) == 0) { | |
725 | SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST, | |
726 | ERR_R_INTERNAL_ERROR); | |
727 | return 0; | |
728 | } | |
729 | ||
730 | *(p++) = (unsigned char)s->d1->cookie_len; | |
731 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | |
732 | p += s->d1->cookie_len; | |
733 | msg_len = p - msg; | |
734 | ||
735 | dtls1_set_message_header(s, buf, | |
736 | DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, | |
737 | msg_len); | |
738 | ||
739 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; | |
740 | /* number of bytes to write */ | |
741 | s->init_num = p - buf; | |
742 | s->init_off = 0; | |
743 | } | |
744 | ||
745 | /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ | |
746 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
747 | } | |
36d16f8e BL |
748 | |
749 | int dtls1_send_server_hello(SSL *s) | |
40720ce3 MC |
750 | { |
751 | unsigned char *buf; | |
752 | unsigned char *p, *d; | |
753 | int i; | |
754 | unsigned int sl; | |
755 | unsigned long l, Time; | |
756 | ||
757 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) { | |
758 | buf = (unsigned char *)s->init_buf->data; | |
759 | p = s->s3->server_random; | |
760 | Time = (unsigned long)time(NULL); /* Time */ | |
761 | l2n(Time, p); | |
762 | RAND_pseudo_bytes(p, SSL3_RANDOM_SIZE - 4); | |
763 | /* Do the message type and length last */ | |
764 | d = p = &(buf[DTLS1_HM_HEADER_LENGTH]); | |
765 | ||
766 | if (s->client_version == DTLS1_BAD_VER) | |
767 | *(p++) = DTLS1_BAD_VER >> 8, *(p++) = DTLS1_BAD_VER & 0xff; | |
768 | else | |
769 | *(p++) = s->version >> 8, *(p++) = s->version & 0xff; | |
770 | ||
771 | /* Random stuff */ | |
772 | memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE); | |
773 | p += SSL3_RANDOM_SIZE; | |
774 | ||
775 | /* | |
776 | * now in theory we have 3 options to sending back the session id. | |
777 | * If it is a re-use, we send back the old session-id, if it is a new | |
778 | * session, we send back the new session-id or we send back a 0 | |
779 | * length session-id if we want it to be single use. Currently I will | |
780 | * not implement the '0' length session-id 12-Jan-98 - I'll now | |
781 | * support the '0' length stuff. | |
782 | */ | |
783 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) | |
784 | s->session->session_id_length = 0; | |
785 | ||
786 | sl = s->session->session_id_length; | |
787 | if (sl > sizeof s->session->session_id) { | |
788 | SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR); | |
789 | return -1; | |
790 | } | |
791 | *(p++) = sl; | |
792 | memcpy(p, s->session->session_id, sl); | |
793 | p += sl; | |
794 | ||
795 | /* put the cipher */ | |
796 | if (s->s3->tmp.new_cipher == NULL) | |
797 | return -1; | |
798 | i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p); | |
799 | p += i; | |
800 | ||
801 | /* put the compression method */ | |
54f51116 | 802 | #ifdef OPENSSL_NO_COMP |
40720ce3 | 803 | *(p++) = 0; |
54f51116 | 804 | #else |
40720ce3 MC |
805 | if (s->s3->tmp.new_compression == NULL) |
806 | *(p++) = 0; | |
807 | else | |
808 | *(p++) = s->s3->tmp.new_compression->id; | |
54f51116 | 809 | #endif |
36d16f8e | 810 | |
82e448b9 | 811 | #ifndef OPENSSL_NO_TLSEXT |
40720ce3 MC |
812 | if ((p = |
813 | ssl_add_serverhello_tlsext(s, p, | |
814 | buf + SSL3_RT_MAX_PLAIN_LENGTH)) == | |
815 | NULL) { | |
816 | SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR); | |
817 | return -1; | |
818 | } | |
82e448b9 DSH |
819 | #endif |
820 | ||
40720ce3 MC |
821 | /* do the header */ |
822 | l = (p - d); | |
823 | d = buf; | |
36d16f8e | 824 | |
40720ce3 | 825 | d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l); |
36d16f8e | 826 | |
40720ce3 MC |
827 | s->state = SSL3_ST_SW_SRVR_HELLO_B; |
828 | /* number of bytes to write */ | |
829 | s->init_num = p - buf; | |
830 | s->init_off = 0; | |
36d16f8e | 831 | |
40720ce3 MC |
832 | /* buffer the message to handle re-xmits */ |
833 | dtls1_buffer_message(s, 0); | |
834 | } | |
36d16f8e | 835 | |
40720ce3 MC |
836 | /* SSL3_ST_SW_SRVR_HELLO_B */ |
837 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
838 | } | |
36d16f8e BL |
839 | |
840 | int dtls1_send_server_done(SSL *s) | |
40720ce3 MC |
841 | { |
842 | unsigned char *p; | |
36d16f8e | 843 | |
40720ce3 MC |
844 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) { |
845 | p = (unsigned char *)s->init_buf->data; | |
36d16f8e | 846 | |
40720ce3 MC |
847 | /* do the header */ |
848 | p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0); | |
36d16f8e | 849 | |
40720ce3 MC |
850 | s->state = SSL3_ST_SW_SRVR_DONE_B; |
851 | /* number of bytes to write */ | |
852 | s->init_num = DTLS1_HM_HEADER_LENGTH; | |
853 | s->init_off = 0; | |
36d16f8e | 854 | |
40720ce3 MC |
855 | /* buffer the message to handle re-xmits */ |
856 | dtls1_buffer_message(s, 0); | |
857 | } | |
36d16f8e | 858 | |
40720ce3 MC |
859 | /* SSL3_ST_SW_SRVR_DONE_B */ |
860 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
861 | } | |
36d16f8e BL |
862 | |
863 | int dtls1_send_server_key_exchange(SSL *s) | |
40720ce3 | 864 | { |
36d16f8e | 865 | #ifndef OPENSSL_NO_RSA |
40720ce3 MC |
866 | unsigned char *q; |
867 | int j, num; | |
868 | RSA *rsa; | |
869 | unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; | |
870 | unsigned int u; | |
36d16f8e BL |
871 | #endif |
872 | #ifndef OPENSSL_NO_DH | |
40720ce3 | 873 | DH *dh = NULL, *dhp; |
36d16f8e | 874 | #endif |
40720ce3 MC |
875 | EVP_PKEY *pkey; |
876 | unsigned char *p, *d; | |
877 | int al, i; | |
878 | unsigned long type; | |
879 | int n; | |
880 | CERT *cert; | |
881 | BIGNUM *r[4]; | |
882 | int nr[4], kn; | |
883 | BUF_MEM *buf; | |
884 | EVP_MD_CTX md_ctx; | |
885 | ||
886 | EVP_MD_CTX_init(&md_ctx); | |
887 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) { | |
888 | type = s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK; | |
889 | cert = s->cert; | |
890 | ||
891 | buf = s->init_buf; | |
892 | ||
893 | r[0] = r[1] = r[2] = r[3] = NULL; | |
894 | n = 0; | |
36d16f8e | 895 | #ifndef OPENSSL_NO_RSA |
40720ce3 MC |
896 | if (type & SSL_kRSA) { |
897 | rsa = cert->rsa_tmp; | |
898 | if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) { | |
899 | rsa = s->cert->rsa_tmp_cb(s, | |
900 | SSL_C_IS_EXPORT(s->s3-> | |
901 | tmp.new_cipher), | |
902 | SSL_C_EXPORT_PKEYLENGTH(s->s3-> | |
903 | tmp.new_cipher)); | |
904 | if (rsa == NULL) { | |
905 | al = SSL_AD_HANDSHAKE_FAILURE; | |
906 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
907 | SSL_R_ERROR_GENERATING_TMP_RSA_KEY); | |
908 | goto f_err; | |
909 | } | |
910 | RSA_up_ref(rsa); | |
911 | cert->rsa_tmp = rsa; | |
912 | } | |
913 | if (rsa == NULL) { | |
914 | al = SSL_AD_HANDSHAKE_FAILURE; | |
915 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
916 | SSL_R_MISSING_TMP_RSA_KEY); | |
917 | goto f_err; | |
918 | } | |
919 | r[0] = rsa->n; | |
920 | r[1] = rsa->e; | |
921 | s->s3->tmp.use_rsa_tmp = 1; | |
922 | } else | |
36d16f8e BL |
923 | #endif |
924 | #ifndef OPENSSL_NO_DH | |
40720ce3 MC |
925 | if (type & SSL_kEDH) { |
926 | dhp = cert->dh_tmp; | |
927 | if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) | |
928 | dhp = s->cert->dh_tmp_cb(s, | |
929 | SSL_C_IS_EXPORT(s->s3-> | |
930 | tmp.new_cipher), | |
931 | SSL_C_EXPORT_PKEYLENGTH(s->s3-> | |
932 | tmp.new_cipher)); | |
933 | if (dhp == NULL) { | |
934 | al = SSL_AD_HANDSHAKE_FAILURE; | |
935 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
936 | SSL_R_MISSING_TMP_DH_KEY); | |
937 | goto f_err; | |
938 | } | |
939 | ||
940 | if (s->s3->tmp.dh != NULL) { | |
941 | DH_free(dh); | |
942 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
943 | ERR_R_INTERNAL_ERROR); | |
944 | goto err; | |
945 | } | |
946 | ||
947 | if ((dh = DHparams_dup(dhp)) == NULL) { | |
948 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); | |
949 | goto err; | |
950 | } | |
951 | ||
952 | s->s3->tmp.dh = dh; | |
953 | if ((dhp->pub_key == NULL || | |
954 | dhp->priv_key == NULL || | |
955 | (s->options & SSL_OP_SINGLE_DH_USE))) { | |
956 | if (!DH_generate_key(dh)) { | |
957 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
958 | ERR_R_DH_LIB); | |
959 | goto err; | |
960 | } | |
961 | } else { | |
962 | dh->pub_key = BN_dup(dhp->pub_key); | |
963 | dh->priv_key = BN_dup(dhp->priv_key); | |
964 | if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) { | |
965 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
966 | ERR_R_DH_LIB); | |
967 | goto err; | |
968 | } | |
969 | } | |
970 | r[0] = dh->p; | |
971 | r[1] = dh->g; | |
972 | r[2] = dh->pub_key; | |
973 | } else | |
36d16f8e | 974 | #endif |
40720ce3 MC |
975 | { |
976 | al = SSL_AD_HANDSHAKE_FAILURE; | |
977 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
978 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | |
979 | goto f_err; | |
980 | } | |
981 | for (i = 0; r[i] != NULL; i++) { | |
982 | nr[i] = BN_num_bytes(r[i]); | |
983 | n += 2 + nr[i]; | |
984 | } | |
985 | ||
986 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) { | |
987 | if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher)) | |
988 | == NULL) { | |
989 | al = SSL_AD_DECODE_ERROR; | |
990 | goto f_err; | |
991 | } | |
992 | kn = EVP_PKEY_size(pkey); | |
993 | } else { | |
994 | pkey = NULL; | |
995 | kn = 0; | |
996 | } | |
997 | ||
998 | if (!BUF_MEM_grow_clean(buf, n + DTLS1_HM_HEADER_LENGTH + kn)) { | |
999 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF); | |
1000 | goto err; | |
1001 | } | |
1002 | d = (unsigned char *)s->init_buf->data; | |
1003 | p = &(d[DTLS1_HM_HEADER_LENGTH]); | |
1004 | ||
1005 | for (i = 0; r[i] != NULL; i++) { | |
1006 | s2n(nr[i], p); | |
1007 | BN_bn2bin(r[i], p); | |
1008 | p += nr[i]; | |
1009 | } | |
1010 | ||
1011 | /* not anonymous */ | |
1012 | if (pkey != NULL) { | |
1013 | /* | |
1014 | * n is the length of the params, they start at | |
1015 | * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space at the | |
1016 | * end. | |
1017 | */ | |
36d16f8e | 1018 | #ifndef OPENSSL_NO_RSA |
40720ce3 MC |
1019 | if (pkey->type == EVP_PKEY_RSA) { |
1020 | q = md_buf; | |
1021 | j = 0; | |
1022 | for (num = 2; num > 0; num--) { | |
1023 | EVP_DigestInit_ex(&md_ctx, (num == 2) | |
1024 | ? s->ctx->md5 : s->ctx->sha1, NULL); | |
1025 | EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]), | |
1026 | SSL3_RANDOM_SIZE); | |
1027 | EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]), | |
1028 | SSL3_RANDOM_SIZE); | |
1029 | EVP_DigestUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), | |
1030 | n); | |
1031 | EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i); | |
1032 | q += i; | |
1033 | j += i; | |
1034 | } | |
1035 | if (RSA_sign(NID_md5_sha1, md_buf, j, | |
1036 | &(p[2]), &u, pkey->pkey.rsa) <= 0) { | |
1037 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA); | |
1038 | goto err; | |
1039 | } | |
1040 | s2n(u, p); | |
1041 | n += u + 2; | |
1042 | } else | |
36d16f8e BL |
1043 | #endif |
1044 | #if !defined(OPENSSL_NO_DSA) | |
40720ce3 MC |
1045 | if (pkey->type == EVP_PKEY_DSA) { |
1046 | /* lets do DSS */ | |
1047 | EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL); | |
1048 | EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]), | |
1049 | SSL3_RANDOM_SIZE); | |
1050 | EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]), | |
1051 | SSL3_RANDOM_SIZE); | |
1052 | EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n); | |
1053 | if (!EVP_SignFinal(&md_ctx, &(p[2]), | |
1054 | (unsigned int *)&i, pkey)) { | |
1055 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA); | |
1056 | goto err; | |
1057 | } | |
1058 | s2n(i, p); | |
1059 | n += i + 2; | |
1060 | } else | |
36d16f8e | 1061 | #endif |
40720ce3 MC |
1062 | { |
1063 | /* Is this error check actually needed? */ | |
1064 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1065 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | |
1066 | SSL_R_UNKNOWN_PKEY_TYPE); | |
1067 | goto f_err; | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | d = dtls1_set_message_header(s, d, | |
1072 | SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n); | |
1073 | ||
1074 | /* | |
1075 | * we should now have things packed up, so lets send it off | |
1076 | */ | |
1077 | s->init_num = n + DTLS1_HM_HEADER_LENGTH; | |
1078 | s->init_off = 0; | |
1079 | ||
1080 | /* buffer the message to handle re-xmits */ | |
1081 | dtls1_buffer_message(s, 0); | |
1082 | } | |
1083 | ||
1084 | s->state = SSL3_ST_SW_KEY_EXCH_B; | |
1085 | EVP_MD_CTX_cleanup(&md_ctx); | |
1086 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
1087 | f_err: | |
1088 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1089 | err: | |
1090 | EVP_MD_CTX_cleanup(&md_ctx); | |
1091 | return (-1); | |
1092 | } | |
36d16f8e BL |
1093 | |
1094 | int dtls1_send_certificate_request(SSL *s) | |
40720ce3 MC |
1095 | { |
1096 | unsigned char *p, *d; | |
1097 | int i, j, nl, off, n; | |
1098 | STACK_OF(X509_NAME) *sk = NULL; | |
1099 | X509_NAME *name; | |
1100 | BUF_MEM *buf; | |
1101 | unsigned int msg_len; | |
1102 | ||
1103 | if (s->state == SSL3_ST_SW_CERT_REQ_A) { | |
1104 | buf = s->init_buf; | |
1105 | ||
1106 | d = p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]); | |
1107 | ||
1108 | /* get the list of acceptable cert types */ | |
1109 | p++; | |
1110 | n = ssl3_get_req_cert_type(s, p); | |
1111 | d[0] = n; | |
1112 | p += n; | |
1113 | n++; | |
1114 | ||
1115 | off = n; | |
1116 | p += 2; | |
1117 | n += 2; | |
1118 | ||
1119 | sk = SSL_get_client_CA_list(s); | |
1120 | nl = 0; | |
1121 | if (sk != NULL) { | |
1122 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { | |
1123 | name = sk_X509_NAME_value(sk, i); | |
1124 | j = i2d_X509_NAME(name, NULL); | |
1125 | if (!BUF_MEM_grow_clean | |
1126 | (buf, DTLS1_HM_HEADER_LENGTH + n + j + 2)) { | |
1127 | SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST, | |
1128 | ERR_R_BUF_LIB); | |
1129 | goto err; | |
1130 | } | |
1131 | p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + n]); | |
1132 | if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) { | |
1133 | s2n(j, p); | |
1134 | i2d_X509_NAME(name, &p); | |
1135 | n += 2 + j; | |
1136 | nl += 2 + j; | |
1137 | } else { | |
1138 | d = p; | |
1139 | i2d_X509_NAME(name, &p); | |
1140 | j -= 2; | |
1141 | s2n(j, d); | |
1142 | j += 2; | |
1143 | n += j; | |
1144 | nl += j; | |
1145 | } | |
1146 | } | |
1147 | } | |
1148 | /* else no CA names */ | |
1149 | p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + off]); | |
1150 | s2n(nl, p); | |
1151 | ||
1152 | d = (unsigned char *)buf->data; | |
1153 | *(d++) = SSL3_MT_CERTIFICATE_REQUEST; | |
1154 | l2n3(n, d); | |
1155 | s2n(s->d1->handshake_write_seq, d); | |
1156 | s->d1->handshake_write_seq++; | |
1157 | ||
1158 | /* | |
1159 | * we should now have things packed up, so lets send it off | |
1160 | */ | |
1161 | ||
1162 | s->init_num = n + DTLS1_HM_HEADER_LENGTH; | |
1163 | s->init_off = 0; | |
36d16f8e BL |
1164 | #ifdef NETSCAPE_HANG_BUG |
1165 | /* XXX: what to do about this? */ | |
40720ce3 MC |
1166 | p = (unsigned char *)s->init_buf->data + s->init_num; |
1167 | ||
1168 | /* do the header */ | |
1169 | *(p++) = SSL3_MT_SERVER_DONE; | |
1170 | *(p++) = 0; | |
1171 | *(p++) = 0; | |
1172 | *(p++) = 0; | |
1173 | s->init_num += 4; | |
36d16f8e BL |
1174 | #endif |
1175 | ||
40720ce3 MC |
1176 | /* XDTLS: set message header ? */ |
1177 | msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH; | |
1178 | dtls1_set_message_header(s, (void *)s->init_buf->data, | |
1179 | SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0, | |
1180 | msg_len); | |
25b0e072 | 1181 | |
40720ce3 MC |
1182 | /* buffer the message to handle re-xmits */ |
1183 | dtls1_buffer_message(s, 0); | |
36d16f8e | 1184 | |
40720ce3 MC |
1185 | s->state = SSL3_ST_SW_CERT_REQ_B; |
1186 | } | |
36d16f8e | 1187 | |
40720ce3 MC |
1188 | /* SSL3_ST_SW_CERT_REQ_B */ |
1189 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
1190 | err: | |
1191 | return (-1); | |
1192 | } | |
36d16f8e BL |
1193 | |
1194 | int dtls1_send_server_certificate(SSL *s) | |
40720ce3 MC |
1195 | { |
1196 | unsigned long l; | |
1197 | X509 *x; | |
1198 | ||
1199 | if (s->state == SSL3_ST_SW_CERT_A) { | |
1200 | x = ssl_get_server_send_cert(s); | |
1201 | if (x == NULL && | |
1202 | /* VRS: allow null cert if auth == KRB5 */ | |
1203 | (s->s3->tmp.new_cipher->algorithms | |
1204 | & (SSL_MKEY_MASK | SSL_AUTH_MASK)) | |
1205 | != (SSL_aKRB5 | SSL_kKRB5)) { | |
1206 | SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR); | |
1207 | return (0); | |
1208 | } | |
1209 | ||
1210 | l = dtls1_output_cert_chain(s, x); | |
1211 | s->state = SSL3_ST_SW_CERT_B; | |
1212 | s->init_num = (int)l; | |
1213 | s->init_off = 0; | |
1214 | ||
1215 | /* buffer the message to handle re-xmits */ | |
1216 | dtls1_buffer_message(s, 0); | |
1217 | } | |
1218 | ||
1219 | /* SSL3_ST_SW_CERT_B */ | |
1220 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
1221 | } | |
7a014dce DSH |
1222 | |
1223 | #ifndef OPENSSL_NO_TLSEXT | |
1224 | int dtls1_send_newsession_ticket(SSL *s) | |
40720ce3 MC |
1225 | { |
1226 | if (s->state == SSL3_ST_SW_SESSION_TICKET_A) { | |
1227 | unsigned char *p, *senc, *macstart; | |
1228 | int len, slen; | |
1229 | unsigned int hlen, msg_len; | |
1230 | EVP_CIPHER_CTX ctx; | |
1231 | HMAC_CTX hctx; | |
1232 | SSL_CTX *tctx = s->initial_ctx; | |
1233 | unsigned char iv[EVP_MAX_IV_LENGTH]; | |
1234 | unsigned char key_name[16]; | |
1235 | ||
1236 | /* get session encoding length */ | |
1237 | slen = i2d_SSL_SESSION(s->session, NULL); | |
1238 | /* | |
1239 | * Some length values are 16 bits, so forget it if session is too | |
1240 | * long | |
1241 | */ | |
1242 | if (slen > 0xFF00) | |
1243 | return -1; | |
1244 | /* | |
1245 | * Grow buffer if need be: the length calculation is as follows 12 | |
1246 | * (DTLS handshake message header) + 4 (ticket lifetime hint) + 2 | |
1247 | * (ticket length) + 16 (key name) + max_iv_len (iv length) + | |
1248 | * session_length + max_enc_block_size (max encrypted session length) | |
1249 | * + max_md_size (HMAC). | |
1250 | */ | |
1251 | if (!BUF_MEM_grow(s->init_buf, | |
1252 | DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH + | |
1253 | EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen)) | |
1254 | return -1; | |
1255 | senc = OPENSSL_malloc(slen); | |
1256 | if (!senc) | |
1257 | return -1; | |
1258 | p = senc; | |
1259 | i2d_SSL_SESSION(s->session, &p); | |
1260 | ||
1261 | p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]); | |
1262 | EVP_CIPHER_CTX_init(&ctx); | |
1263 | HMAC_CTX_init(&hctx); | |
1264 | /* | |
1265 | * Initialize HMAC and cipher contexts. If callback present it does | |
1266 | * all the work otherwise use generated values from parent ctx. | |
1267 | */ | |
1268 | if (tctx->tlsext_ticket_key_cb) { | |
1269 | if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, | |
1270 | &hctx, 1) < 0) { | |
1271 | OPENSSL_free(senc); | |
1272 | return -1; | |
1273 | } | |
1274 | } else { | |
1275 | RAND_pseudo_bytes(iv, 16); | |
1276 | EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, | |
1277 | tctx->tlsext_tick_aes_key, iv); | |
1278 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, | |
1279 | tlsext_tick_md(), NULL); | |
1280 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); | |
1281 | } | |
1282 | l2n(s->session->tlsext_tick_lifetime_hint, p); | |
1283 | /* Skip ticket length for now */ | |
1284 | p += 2; | |
1285 | /* Output key name */ | |
1286 | macstart = p; | |
1287 | memcpy(p, key_name, 16); | |
1288 | p += 16; | |
1289 | /* output IV */ | |
1290 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); | |
1291 | p += EVP_CIPHER_CTX_iv_length(&ctx); | |
1292 | /* Encrypt session data */ | |
1293 | EVP_EncryptUpdate(&ctx, p, &len, senc, slen); | |
1294 | p += len; | |
1295 | EVP_EncryptFinal(&ctx, p, &len); | |
1296 | p += len; | |
1297 | EVP_CIPHER_CTX_cleanup(&ctx); | |
1298 | ||
1299 | HMAC_Update(&hctx, macstart, p - macstart); | |
1300 | HMAC_Final(&hctx, p, &hlen); | |
1301 | HMAC_CTX_cleanup(&hctx); | |
1302 | ||
1303 | p += hlen; | |
1304 | /* Now write out lengths: p points to end of data written */ | |
1305 | /* Total length */ | |
1306 | len = p - (unsigned char *)(s->init_buf->data); | |
1307 | /* Ticket length */ | |
1308 | p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4; | |
1309 | s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p); | |
1310 | ||
1311 | /* number of bytes to write */ | |
1312 | s->init_num = len; | |
1313 | s->state = SSL3_ST_SW_SESSION_TICKET_B; | |
1314 | s->init_off = 0; | |
1315 | OPENSSL_free(senc); | |
1316 | ||
1317 | /* XDTLS: set message header ? */ | |
1318 | msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH; | |
1319 | dtls1_set_message_header(s, (void *)s->init_buf->data, | |
1320 | SSL3_MT_NEWSESSION_TICKET, msg_len, 0, | |
1321 | msg_len); | |
1322 | ||
1323 | /* buffer the message to handle re-xmits */ | |
1324 | dtls1_buffer_message(s, 0); | |
1325 | } | |
1326 | ||
1327 | /* SSL3_ST_SW_SESSION_TICKET_B */ | |
1328 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | |
1329 | } | |
c0e94f82 | 1330 | #endif |