diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libssl/d1_srvr.c | 1329 |
1 files changed, 0 insertions, 1329 deletions
diff --git a/src/lib/libssl/d1_srvr.c b/src/lib/libssl/d1_srvr.c deleted file mode 100644 index 698292f33f..0000000000 --- a/src/lib/libssl/d1_srvr.c +++ /dev/null | |||
@@ -1,1329 +0,0 @@ | |||
1 | /* $OpenBSD: d1_srvr.c,v 1.55 2015/06/18 22:51:05 doug Exp $ */ | ||
2 | /* | ||
3 | * DTLS implementation written by Nagendra Modadugu | ||
4 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. | ||
5 | */ | ||
6 | /* ==================================================================== | ||
7 | * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in | ||
18 | * the documentation and/or other materials provided with the | ||
19 | * distribution. | ||
20 | * | ||
21 | * 3. All advertising materials mentioning features or use of this | ||
22 | * software must display the following acknowledgment: | ||
23 | * "This product includes software developed by the OpenSSL Project | ||
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
25 | * | ||
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
27 | * endorse or promote products derived from this software without | ||
28 | * prior written permission. For written permission, please contact | ||
29 | * openssl-core@OpenSSL.org. | ||
30 | * | ||
31 | * 5. Products derived from this software may not be called "OpenSSL" | ||
32 | * nor may "OpenSSL" appear in their names without prior written | ||
33 | * permission of the OpenSSL Project. | ||
34 | * | ||
35 | * 6. Redistributions of any form whatsoever must retain the following | ||
36 | * acknowledgment: | ||
37 | * "This product includes software developed by the OpenSSL Project | ||
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
39 | * | ||
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
52 | * ==================================================================== | ||
53 | * | ||
54 | * This product includes cryptographic software written by Eric Young | ||
55 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
56 | * Hudson (tjh@cryptsoft.com). | ||
57 | * | ||
58 | */ | ||
59 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
60 | * All rights reserved. | ||
61 | * | ||
62 | * This package is an SSL implementation written | ||
63 | * by Eric Young (eay@cryptsoft.com). | ||
64 | * The implementation was written so as to conform with Netscapes SSL. | ||
65 | * | ||
66 | * This library is free for commercial and non-commercial use as long as | ||
67 | * the following conditions are aheared to. The following conditions | ||
68 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
69 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
70 | * included with this distribution is covered by the same copyright terms | ||
71 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
72 | * | ||
73 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
74 | * the code are not to be removed. | ||
75 | * If this package is used in a product, Eric Young should be given attribution | ||
76 | * as the author of the parts of the library used. | ||
77 | * This can be in the form of a textual message at program startup or | ||
78 | * in documentation (online or textual) provided with the package. | ||
79 | * | ||
80 | * Redistribution and use in source and binary forms, with or without | ||
81 | * modification, are permitted provided that the following conditions | ||
82 | * are met: | ||
83 | * 1. Redistributions of source code must retain the copyright | ||
84 | * notice, this list of conditions and the following disclaimer. | ||
85 | * 2. Redistributions in binary form must reproduce the above copyright | ||
86 | * notice, this list of conditions and the following disclaimer in the | ||
87 | * documentation and/or other materials provided with the distribution. | ||
88 | * 3. All advertising materials mentioning features or use of this software | ||
89 | * must display the following acknowledgement: | ||
90 | * "This product includes cryptographic software written by | ||
91 | * Eric Young (eay@cryptsoft.com)" | ||
92 | * The word 'cryptographic' can be left out if the rouines from the library | ||
93 | * being used are not cryptographic related :-). | ||
94 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
95 | * the apps directory (application code) you must include an acknowledgement: | ||
96 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
97 | * | ||
98 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
99 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
100 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
101 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
102 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
103 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
104 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
105 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
106 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
107 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
108 | * SUCH DAMAGE. | ||
109 | * | ||
110 | * The licence and distribution terms for any publically available version or | ||
111 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
112 | * copied and put under another distribution licence | ||
113 | * [including the GNU Public Licence.] | ||
114 | */ | ||
115 | |||
116 | #include <stdio.h> | ||
117 | |||
118 | #include "ssl_locl.h" | ||
119 | |||
120 | #include <openssl/bn.h> | ||
121 | #include <openssl/buffer.h> | ||
122 | #include <openssl/dh.h> | ||
123 | #include <openssl/evp.h> | ||
124 | #include <openssl/md5.h> | ||
125 | #include <openssl/objects.h> | ||
126 | #include <openssl/x509.h> | ||
127 | |||
128 | static const SSL_METHOD *dtls1_get_server_method(int ver); | ||
129 | static int dtls1_send_hello_verify_request(SSL *s); | ||
130 | |||
131 | const SSL_METHOD DTLSv1_server_method_data = { | ||
132 | .version = DTLS1_VERSION, | ||
133 | .ssl_new = dtls1_new, | ||
134 | .ssl_clear = dtls1_clear, | ||
135 | .ssl_free = dtls1_free, | ||
136 | .ssl_accept = dtls1_accept, | ||
137 | .ssl_connect = ssl_undefined_function, | ||
138 | .ssl_read = ssl3_read, | ||
139 | .ssl_peek = ssl3_peek, | ||
140 | .ssl_write = ssl3_write, | ||
141 | .ssl_shutdown = dtls1_shutdown, | ||
142 | .ssl_renegotiate = ssl3_renegotiate, | ||
143 | .ssl_renegotiate_check = ssl3_renegotiate_check, | ||
144 | .ssl_get_message = dtls1_get_message, | ||
145 | .ssl_read_bytes = dtls1_read_bytes, | ||
146 | .ssl_write_bytes = dtls1_write_app_data_bytes, | ||
147 | .ssl_dispatch_alert = dtls1_dispatch_alert, | ||
148 | .ssl_ctrl = dtls1_ctrl, | ||
149 | .ssl_ctx_ctrl = ssl3_ctx_ctrl, | ||
150 | .get_cipher_by_char = ssl3_get_cipher_by_char, | ||
151 | .put_cipher_by_char = ssl3_put_cipher_by_char, | ||
152 | .ssl_pending = ssl3_pending, | ||
153 | .num_ciphers = ssl3_num_ciphers, | ||
154 | .get_cipher = dtls1_get_cipher, | ||
155 | .get_ssl_method = dtls1_get_server_method, | ||
156 | .get_timeout = dtls1_default_timeout, | ||
157 | .ssl3_enc = &DTLSv1_enc_data, | ||
158 | .ssl_version = ssl_undefined_void_function, | ||
159 | .ssl_callback_ctrl = ssl3_callback_ctrl, | ||
160 | .ssl_ctx_callback_ctrl = ssl3_ctx_callback_ctrl, | ||
161 | }; | ||
162 | |||
163 | const SSL_METHOD * | ||
164 | DTLSv1_server_method(void) | ||
165 | { | ||
166 | return &DTLSv1_server_method_data; | ||
167 | } | ||
168 | |||
169 | static const SSL_METHOD * | ||
170 | dtls1_get_server_method(int ver) | ||
171 | { | ||
172 | if (ver == DTLS1_VERSION) | ||
173 | return (DTLSv1_server_method()); | ||
174 | return (NULL); | ||
175 | } | ||
176 | |||
177 | int | ||
178 | dtls1_accept(SSL *s) | ||
179 | { | ||
180 | void (*cb)(const SSL *ssl, int type, int val) = NULL; | ||
181 | unsigned long alg_k; | ||
182 | int ret = -1; | ||
183 | int new_state, state, skip = 0; | ||
184 | int listen; | ||
185 | |||
186 | ERR_clear_error(); | ||
187 | errno = 0; | ||
188 | |||
189 | if (s->info_callback != NULL) | ||
190 | cb = s->info_callback; | ||
191 | else if (s->ctx->info_callback != NULL) | ||
192 | cb = s->ctx->info_callback; | ||
193 | |||
194 | listen = s->d1->listen; | ||
195 | |||
196 | /* init things to blank */ | ||
197 | s->in_handshake++; | ||
198 | if (!SSL_in_init(s) || SSL_in_before(s)) | ||
199 | SSL_clear(s); | ||
200 | |||
201 | s->d1->listen = listen; | ||
202 | |||
203 | if (s->cert == NULL) { | ||
204 | SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_NO_CERTIFICATE_SET); | ||
205 | return (-1); | ||
206 | } | ||
207 | |||
208 | for (;;) { | ||
209 | state = s->state; | ||
210 | |||
211 | switch (s->state) { | ||
212 | case SSL_ST_RENEGOTIATE: | ||
213 | s->renegotiate = 1; | ||
214 | /* s->state=SSL_ST_ACCEPT; */ | ||
215 | |||
216 | case SSL_ST_BEFORE: | ||
217 | case SSL_ST_ACCEPT: | ||
218 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: | ||
219 | case SSL_ST_OK|SSL_ST_ACCEPT: | ||
220 | |||
221 | s->server = 1; | ||
222 | if (cb != NULL) | ||
223 | cb(s, SSL_CB_HANDSHAKE_START, 1); | ||
224 | |||
225 | if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) { | ||
226 | SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR); | ||
227 | return -1; | ||
228 | } | ||
229 | s->type = SSL_ST_ACCEPT; | ||
230 | |||
231 | if (!ssl3_setup_init_buffer(s)) { | ||
232 | ret = -1; | ||
233 | goto end; | ||
234 | } | ||
235 | if (!ssl3_setup_buffers(s)) { | ||
236 | ret = -1; | ||
237 | goto end; | ||
238 | } | ||
239 | |||
240 | s->init_num = 0; | ||
241 | |||
242 | if (s->state != SSL_ST_RENEGOTIATE) { | ||
243 | /* Ok, we now need to push on a buffering BIO so that | ||
244 | * the output is sent in a way that TCP likes :-) | ||
245 | * ...but not with SCTP :-) | ||
246 | */ | ||
247 | if (!ssl_init_wbio_buffer(s, 1)) { | ||
248 | ret = -1; | ||
249 | goto end; | ||
250 | } | ||
251 | |||
252 | if (!ssl3_init_finished_mac(s)) { | ||
253 | ret = -1; | ||
254 | goto end; | ||
255 | } | ||
256 | |||
257 | s->state = SSL3_ST_SR_CLNT_HELLO_A; | ||
258 | s->ctx->stats.sess_accept++; | ||
259 | } else { | ||
260 | /* s->state == SSL_ST_RENEGOTIATE, | ||
261 | * we will just send a HelloRequest */ | ||
262 | s->ctx->stats.sess_accept_renegotiate++; | ||
263 | s->state = SSL3_ST_SW_HELLO_REQ_A; | ||
264 | } | ||
265 | |||
266 | break; | ||
267 | |||
268 | case SSL3_ST_SW_HELLO_REQ_A: | ||
269 | case SSL3_ST_SW_HELLO_REQ_B: | ||
270 | |||
271 | s->shutdown = 0; | ||
272 | dtls1_clear_record_buffer(s); | ||
273 | dtls1_start_timer(s); | ||
274 | ret = dtls1_send_hello_request(s); | ||
275 | if (ret <= 0) | ||
276 | goto end; | ||
277 | s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A; | ||
278 | s->state = SSL3_ST_SW_FLUSH; | ||
279 | s->init_num = 0; | ||
280 | |||
281 | if (!ssl3_init_finished_mac(s)) { | ||
282 | ret = -1; | ||
283 | goto end; | ||
284 | } | ||
285 | break; | ||
286 | |||
287 | case SSL3_ST_SW_HELLO_REQ_C: | ||
288 | s->state = SSL_ST_OK; | ||
289 | break; | ||
290 | |||
291 | case SSL3_ST_SR_CLNT_HELLO_A: | ||
292 | case SSL3_ST_SR_CLNT_HELLO_B: | ||
293 | case SSL3_ST_SR_CLNT_HELLO_C: | ||
294 | |||
295 | s->shutdown = 0; | ||
296 | ret = ssl3_get_client_hello(s); | ||
297 | if (ret <= 0) | ||
298 | goto end; | ||
299 | dtls1_stop_timer(s); | ||
300 | |||
301 | if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) | ||
302 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; | ||
303 | else | ||
304 | s->state = SSL3_ST_SW_SRVR_HELLO_A; | ||
305 | |||
306 | s->init_num = 0; | ||
307 | |||
308 | /* Reflect ClientHello sequence to remain stateless while listening */ | ||
309 | if (listen) { | ||
310 | memcpy(s->s3->write_sequence, s->s3->read_sequence, sizeof(s->s3->write_sequence)); | ||
311 | } | ||
312 | |||
313 | /* If we're just listening, stop here */ | ||
314 | if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A) { | ||
315 | ret = 2; | ||
316 | s->d1->listen = 0; | ||
317 | /* Set expected sequence numbers | ||
318 | * to continue the handshake. | ||
319 | */ | ||
320 | s->d1->handshake_read_seq = 2; | ||
321 | s->d1->handshake_write_seq = 1; | ||
322 | s->d1->next_handshake_write_seq = 1; | ||
323 | goto end; | ||
324 | } | ||
325 | |||
326 | break; | ||
327 | |||
328 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: | ||
329 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: | ||
330 | |||
331 | ret = dtls1_send_hello_verify_request(s); | ||
332 | if (ret <= 0) | ||
333 | goto end; | ||
334 | s->state = SSL3_ST_SW_FLUSH; | ||
335 | s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A; | ||
336 | |||
337 | /* HelloVerifyRequest resets Finished MAC */ | ||
338 | if (s->version != DTLS1_BAD_VER) { | ||
339 | if (!ssl3_init_finished_mac(s)) { | ||
340 | ret = -1; | ||
341 | goto end; | ||
342 | } | ||
343 | } | ||
344 | break; | ||
345 | |||
346 | |||
347 | case SSL3_ST_SW_SRVR_HELLO_A: | ||
348 | case SSL3_ST_SW_SRVR_HELLO_B: | ||
349 | s->renegotiate = 2; | ||
350 | dtls1_start_timer(s); | ||
351 | ret = dtls1_send_server_hello(s); | ||
352 | if (ret <= 0) | ||
353 | goto end; | ||
354 | |||
355 | if (s->hit) { | ||
356 | if (s->tlsext_ticket_expected) | ||
357 | s->state = SSL3_ST_SW_SESSION_TICKET_A; | ||
358 | else | ||
359 | s->state = SSL3_ST_SW_CHANGE_A; | ||
360 | } else | ||
361 | s->state = SSL3_ST_SW_CERT_A; | ||
362 | s->init_num = 0; | ||
363 | break; | ||
364 | |||
365 | case SSL3_ST_SW_CERT_A: | ||
366 | case SSL3_ST_SW_CERT_B: | ||
367 | /* Check if it is anon DH. */ | ||
368 | if (!(s->s3->tmp.new_cipher->algorithm_auth & | ||
369 | SSL_aNULL)) { | ||
370 | dtls1_start_timer(s); | ||
371 | ret = dtls1_send_server_certificate(s); | ||
372 | if (ret <= 0) | ||
373 | goto end; | ||
374 | if (s->tlsext_status_expected) | ||
375 | s->state = SSL3_ST_SW_CERT_STATUS_A; | ||
376 | else | ||
377 | s->state = SSL3_ST_SW_KEY_EXCH_A; | ||
378 | } else { | ||
379 | skip = 1; | ||
380 | s->state = SSL3_ST_SW_KEY_EXCH_A; | ||
381 | } | ||
382 | s->init_num = 0; | ||
383 | break; | ||
384 | |||
385 | case SSL3_ST_SW_KEY_EXCH_A: | ||
386 | case SSL3_ST_SW_KEY_EXCH_B: | ||
387 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
388 | |||
389 | /* Only send if using a DH key exchange. */ | ||
390 | if (alg_k & (SSL_kDHE|SSL_kECDHE)) { | ||
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 | /* | ||
405 | * Determine whether or not we need to request a | ||
406 | * certificate. | ||
407 | * | ||
408 | * Do not request a certificate if: | ||
409 | * | ||
410 | * - We did not ask for it (SSL_VERIFY_PEER is unset). | ||
411 | * | ||
412 | * - SSL_VERIFY_CLIENT_ONCE is set and we are | ||
413 | * renegotiating. | ||
414 | * | ||
415 | * - We are using an anonymous ciphersuites | ||
416 | * (see section "Certificate request" in SSL 3 drafts | ||
417 | * and in RFC 2246) ... except when the application | ||
418 | * insists on verification (against the specs, but | ||
419 | * s3_clnt.c accepts this for SSL 3). | ||
420 | */ | ||
421 | if (!(s->verify_mode & SSL_VERIFY_PEER) || | ||
422 | ((s->session->peer != NULL) && | ||
423 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || | ||
424 | ((s->s3->tmp.new_cipher->algorithm_auth & | ||
425 | SSL_aNULL) && !(s->verify_mode & | ||
426 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) { | ||
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; | ||
437 | s->state = SSL3_ST_SW_SRVR_DONE_A; | ||
438 | s->init_num = 0; | ||
439 | } | ||
440 | break; | ||
441 | |||
442 | case SSL3_ST_SW_SRVR_DONE_A: | ||
443 | case SSL3_ST_SW_SRVR_DONE_B: | ||
444 | dtls1_start_timer(s); | ||
445 | ret = dtls1_send_server_done(s); | ||
446 | if (ret <= 0) | ||
447 | goto end; | ||
448 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; | ||
449 | s->state = SSL3_ST_SW_FLUSH; | ||
450 | s->init_num = 0; | ||
451 | break; | ||
452 | |||
453 | case SSL3_ST_SW_FLUSH: | ||
454 | s->rwstate = SSL_WRITING; | ||
455 | if (BIO_flush(s->wbio) <= 0) { | ||
456 | /* If the write error was fatal, stop trying */ | ||
457 | if (!BIO_should_retry(s->wbio)) { | ||
458 | s->rwstate = SSL_NOTHING; | ||
459 | s->state = s->s3->tmp.next_state; | ||
460 | } | ||
461 | |||
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 | if (s->s3->tmp.cert_request) { | ||
472 | ret = ssl3_get_client_certificate(s); | ||
473 | if (ret <= 0) | ||
474 | goto end; | ||
475 | } | ||
476 | s->init_num = 0; | ||
477 | s->state = SSL3_ST_SR_KEY_EXCH_A; | ||
478 | break; | ||
479 | |||
480 | case SSL3_ST_SR_KEY_EXCH_A: | ||
481 | case SSL3_ST_SR_KEY_EXCH_B: | ||
482 | ret = ssl3_get_client_key_exchange(s); | ||
483 | if (ret <= 0) | ||
484 | goto end; | ||
485 | |||
486 | s->state = SSL3_ST_SR_CERT_VRFY_A; | ||
487 | s->init_num = 0; | ||
488 | |||
489 | if (ret == 2) { | ||
490 | /* For the ECDH ciphersuites when | ||
491 | * the client sends its ECDH pub key in | ||
492 | * a certificate, the CertificateVerify | ||
493 | * message is not sent. | ||
494 | */ | ||
495 | s->state = SSL3_ST_SR_FINISHED_A; | ||
496 | s->init_num = 0; | ||
497 | } else { | ||
498 | s->state = SSL3_ST_SR_CERT_VRFY_A; | ||
499 | s->init_num = 0; | ||
500 | |||
501 | /* We need to get hashes here so if there is | ||
502 | * a client cert, it can be verified */ | ||
503 | s->method->ssl3_enc->cert_verify_mac(s, | ||
504 | NID_md5, &(s->s3->tmp.cert_verify_md[0])); | ||
505 | s->method->ssl3_enc->cert_verify_mac(s, | ||
506 | NID_sha1, | ||
507 | &(s->s3->tmp.cert_verify_md[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 | s->state = SSL3_ST_SR_FINISHED_A; | ||
520 | s->init_num = 0; | ||
521 | break; | ||
522 | |||
523 | case SSL3_ST_SR_FINISHED_A: | ||
524 | case SSL3_ST_SR_FINISHED_B: | ||
525 | s->d1->change_cipher_spec_ok = 1; | ||
526 | ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, | ||
527 | SSL3_ST_SR_FINISHED_B); | ||
528 | if (ret <= 0) | ||
529 | goto end; | ||
530 | dtls1_stop_timer(s); | ||
531 | if (s->hit) | ||
532 | s->state = SSL_ST_OK; | ||
533 | else if (s->tlsext_ticket_expected) | ||
534 | s->state = SSL3_ST_SW_SESSION_TICKET_A; | ||
535 | else | ||
536 | s->state = SSL3_ST_SW_CHANGE_A; | ||
537 | s->init_num = 0; | ||
538 | break; | ||
539 | |||
540 | case SSL3_ST_SW_SESSION_TICKET_A: | ||
541 | case SSL3_ST_SW_SESSION_TICKET_B: | ||
542 | ret = dtls1_send_newsession_ticket(s); | ||
543 | if (ret <= 0) | ||
544 | goto end; | ||
545 | s->state = SSL3_ST_SW_CHANGE_A; | ||
546 | s->init_num = 0; | ||
547 | break; | ||
548 | |||
549 | case SSL3_ST_SW_CERT_STATUS_A: | ||
550 | case SSL3_ST_SW_CERT_STATUS_B: | ||
551 | ret = ssl3_send_cert_status(s); | ||
552 | if (ret <= 0) | ||
553 | goto end; | ||
554 | s->state = SSL3_ST_SW_KEY_EXCH_A; | ||
555 | s->init_num = 0; | ||
556 | break; | ||
557 | |||
558 | |||
559 | case SSL3_ST_SW_CHANGE_A: | ||
560 | case SSL3_ST_SW_CHANGE_B: | ||
561 | |||
562 | s->session->cipher = s->s3->tmp.new_cipher; | ||
563 | if (!s->method->ssl3_enc->setup_key_block(s)) { | ||
564 | ret = -1; | ||
565 | goto end; | ||
566 | } | ||
567 | |||
568 | ret = dtls1_send_change_cipher_spec(s, | ||
569 | SSL3_ST_SW_CHANGE_A, SSL3_ST_SW_CHANGE_B); | ||
570 | |||
571 | if (ret <= 0) | ||
572 | goto end; | ||
573 | |||
574 | |||
575 | s->state = SSL3_ST_SW_FINISHED_A; | ||
576 | s->init_num = 0; | ||
577 | |||
578 | if (!s->method->ssl3_enc->change_cipher_state(s, | ||
579 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) { | ||
580 | ret = -1; | ||
581 | goto end; | ||
582 | } | ||
583 | |||
584 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | ||
585 | break; | ||
586 | |||
587 | case SSL3_ST_SW_FINISHED_A: | ||
588 | case SSL3_ST_SW_FINISHED_B: | ||
589 | ret = dtls1_send_finished(s, | ||
590 | SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B, | ||
591 | s->method->ssl3_enc->server_finished_label, | ||
592 | s->method->ssl3_enc->server_finished_label_len); | ||
593 | if (ret <= 0) | ||
594 | goto end; | ||
595 | s->state = SSL3_ST_SW_FLUSH; | ||
596 | if (s->hit) { | ||
597 | s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A; | ||
598 | |||
599 | } else { | ||
600 | s->s3->tmp.next_state = SSL_ST_OK; | ||
601 | } | ||
602 | s->init_num = 0; | ||
603 | break; | ||
604 | |||
605 | case SSL_ST_OK: | ||
606 | /* clean a few things up */ | ||
607 | ssl3_cleanup_key_block(s); | ||
608 | |||
609 | /* remove buffering on output */ | ||
610 | ssl_free_wbio_buffer(s); | ||
611 | |||
612 | s->init_num = 0; | ||
613 | |||
614 | if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */ | ||
615 | { | ||
616 | s->renegotiate = 0; | ||
617 | s->new_session = 0; | ||
618 | |||
619 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); | ||
620 | |||
621 | s->ctx->stats.sess_accept_good++; | ||
622 | /* s->server=1; */ | ||
623 | s->handshake_func = dtls1_accept; | ||
624 | |||
625 | if (cb != NULL) | ||
626 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | ||
627 | } | ||
628 | |||
629 | ret = 1; | ||
630 | |||
631 | /* done handshaking, next message is client hello */ | ||
632 | s->d1->handshake_read_seq = 0; | ||
633 | /* next message is server hello */ | ||
634 | s->d1->handshake_write_seq = 0; | ||
635 | s->d1->next_handshake_write_seq = 0; | ||
636 | goto end; | ||
637 | /* break; */ | ||
638 | |||
639 | default: | ||
640 | SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_UNKNOWN_STATE); | ||
641 | ret = -1; | ||
642 | goto end; | ||
643 | /* break; */ | ||
644 | } | ||
645 | |||
646 | if (!s->s3->tmp.reuse_message && !skip) { | ||
647 | if (s->debug) { | ||
648 | if ((ret = BIO_flush(s->wbio)) <= 0) | ||
649 | goto end; | ||
650 | } | ||
651 | |||
652 | if ((cb != NULL) && (s->state != state)) { | ||
653 | new_state = s->state; | ||
654 | s->state = state; | ||
655 | cb(s, SSL_CB_ACCEPT_LOOP, 1); | ||
656 | s->state = new_state; | ||
657 | } | ||
658 | } | ||
659 | skip = 0; | ||
660 | } | ||
661 | end: | ||
662 | /* BIO_flush(s->wbio); */ | ||
663 | |||
664 | s->in_handshake--; | ||
665 | |||
666 | if (cb != NULL) | ||
667 | cb(s, SSL_CB_ACCEPT_EXIT, ret); | ||
668 | return (ret); | ||
669 | } | ||
670 | |||
671 | int | ||
672 | dtls1_send_hello_request(SSL *s) | ||
673 | { | ||
674 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) { | ||
675 | ssl3_handshake_msg_start(s, SSL3_MT_HELLO_REQUEST); | ||
676 | ssl3_handshake_msg_finish(s, 0); | ||
677 | |||
678 | s->state = SSL3_ST_SW_HELLO_REQ_B; | ||
679 | } | ||
680 | |||
681 | /* SSL3_ST_SW_HELLO_REQ_B */ | ||
682 | return (ssl3_handshake_write(s)); | ||
683 | } | ||
684 | |||
685 | int | ||
686 | dtls1_send_hello_verify_request(SSL *s) | ||
687 | { | ||
688 | unsigned char *d, *p; | ||
689 | |||
690 | if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) { | ||
691 | d = p = ssl3_handshake_msg_start(s, | ||
692 | DTLS1_MT_HELLO_VERIFY_REQUEST); | ||
693 | |||
694 | *(p++) = s->version >> 8; | ||
695 | *(p++) = s->version & 0xFF; | ||
696 | |||
697 | if (s->ctx->app_gen_cookie_cb == NULL || | ||
698 | s->ctx->app_gen_cookie_cb(s, s->d1->cookie, | ||
699 | &(s->d1->cookie_len)) == 0) { | ||
700 | SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST, | ||
701 | ERR_R_INTERNAL_ERROR); | ||
702 | return 0; | ||
703 | } | ||
704 | |||
705 | *(p++) = (unsigned char) s->d1->cookie_len; | ||
706 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | ||
707 | p += s->d1->cookie_len; | ||
708 | |||
709 | ssl3_handshake_msg_finish(s, p - d); | ||
710 | |||
711 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; | ||
712 | } | ||
713 | |||
714 | /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ | ||
715 | return (ssl3_handshake_write(s)); | ||
716 | } | ||
717 | |||
718 | int | ||
719 | dtls1_send_server_hello(SSL *s) | ||
720 | { | ||
721 | unsigned char *bufend; | ||
722 | unsigned char *p, *d; | ||
723 | unsigned int sl; | ||
724 | |||
725 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) { | ||
726 | d = p = ssl3_handshake_msg_start(s, SSL3_MT_SERVER_HELLO); | ||
727 | |||
728 | *(p++) = s->version >> 8; | ||
729 | *(p++) = s->version & 0xff; | ||
730 | |||
731 | /* Random stuff */ | ||
732 | arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE); | ||
733 | memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE); | ||
734 | p += SSL3_RANDOM_SIZE; | ||
735 | |||
736 | /* now in theory we have 3 options to sending back the | ||
737 | * session id. If it is a re-use, we send back the | ||
738 | * old session-id, if it is a new session, we send | ||
739 | * back the new session-id or we send back a 0 length | ||
740 | * session-id if we want it to be single use. | ||
741 | * Currently I will not implement the '0' length session-id | ||
742 | * 12-Jan-98 - I'll now support the '0' length stuff. | ||
743 | */ | ||
744 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) | ||
745 | s->session->session_id_length = 0; | ||
746 | |||
747 | sl = s->session->session_id_length; | ||
748 | if (sl > sizeof s->session->session_id) { | ||
749 | SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, | ||
750 | ERR_R_INTERNAL_ERROR); | ||
751 | return -1; | ||
752 | } | ||
753 | *(p++) = sl; | ||
754 | memcpy(p, s->session->session_id, sl); | ||
755 | p += sl; | ||
756 | |||
757 | /* put the cipher */ | ||
758 | if (s->s3->tmp.new_cipher == NULL) | ||
759 | return -1; | ||
760 | s2n(ssl3_cipher_get_value(s->s3->tmp.new_cipher), p); | ||
761 | |||
762 | /* put the compression method */ | ||
763 | *(p++) = 0; | ||
764 | |||
765 | bufend = (unsigned char *)s->init_buf->data + | ||
766 | SSL3_RT_MAX_PLAIN_LENGTH; | ||
767 | if ((p = ssl_add_serverhello_tlsext(s, p, bufend)) == NULL) { | ||
768 | SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, | ||
769 | ERR_R_INTERNAL_ERROR); | ||
770 | return -1; | ||
771 | } | ||
772 | |||
773 | ssl3_handshake_msg_finish(s, p - d); | ||
774 | |||
775 | s->state = SSL3_ST_SW_SRVR_HELLO_B; | ||
776 | } | ||
777 | |||
778 | /* SSL3_ST_SW_SRVR_HELLO_B */ | ||
779 | return (ssl3_handshake_write(s)); | ||
780 | } | ||
781 | |||
782 | int | ||
783 | dtls1_send_server_done(SSL *s) | ||
784 | { | ||
785 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) { | ||
786 | ssl3_handshake_msg_start(s, SSL3_MT_SERVER_DONE); | ||
787 | ssl3_handshake_msg_finish(s, 0); | ||
788 | |||
789 | s->state = SSL3_ST_SW_SRVR_DONE_B; | ||
790 | } | ||
791 | |||
792 | /* SSL3_ST_SW_SRVR_DONE_B */ | ||
793 | return (ssl3_handshake_write(s)); | ||
794 | } | ||
795 | |||
796 | int | ||
797 | dtls1_send_server_key_exchange(SSL *s) | ||
798 | { | ||
799 | unsigned char *q; | ||
800 | int j, num; | ||
801 | unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; | ||
802 | unsigned int u; | ||
803 | DH *dh = NULL, *dhp; | ||
804 | EC_KEY *ecdh = NULL, *ecdhp; | ||
805 | unsigned char *encodedPoint = NULL; | ||
806 | int encodedlen = 0; | ||
807 | int curve_id = 0; | ||
808 | BN_CTX *bn_ctx = NULL; | ||
809 | |||
810 | EVP_PKEY *pkey; | ||
811 | unsigned char *p, *d; | ||
812 | int al, i; | ||
813 | unsigned long type; | ||
814 | int n; | ||
815 | CERT *cert; | ||
816 | BIGNUM *r[4]; | ||
817 | int nr[4], kn; | ||
818 | BUF_MEM *buf; | ||
819 | EVP_MD_CTX md_ctx; | ||
820 | |||
821 | EVP_MD_CTX_init(&md_ctx); | ||
822 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) { | ||
823 | type = s->s3->tmp.new_cipher->algorithm_mkey; | ||
824 | cert = s->cert; | ||
825 | |||
826 | buf = s->init_buf; | ||
827 | |||
828 | r[0] = r[1] = r[2] = r[3] = NULL; | ||
829 | n = 0; | ||
830 | |||
831 | if (type & SSL_kDHE) { | ||
832 | dhp = cert->dh_tmp; | ||
833 | if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) | ||
834 | dhp = s->cert->dh_tmp_cb(s, 0, | ||
835 | SSL_C_PKEYLENGTH(s->s3->tmp.new_cipher)); | ||
836 | if (dhp == NULL) { | ||
837 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
838 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, SSL_R_MISSING_TMP_DH_KEY); | ||
839 | goto f_err; | ||
840 | } | ||
841 | |||
842 | if (s->s3->tmp.dh != NULL) { | ||
843 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | ||
844 | goto err; | ||
845 | } | ||
846 | |||
847 | if ((dh = DHparams_dup(dhp)) == NULL) { | ||
848 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
849 | goto err; | ||
850 | } | ||
851 | |||
852 | s->s3->tmp.dh = dh; | ||
853 | if ((dhp->pub_key == NULL || dhp->priv_key == NULL || | ||
854 | (s->options & SSL_OP_SINGLE_DH_USE))) { | ||
855 | if (!DH_generate_key(dh)) { | ||
856 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | ||
857 | ERR_R_DH_LIB); | ||
858 | goto err; | ||
859 | } | ||
860 | } else { | ||
861 | dh->pub_key = BN_dup(dhp->pub_key); | ||
862 | dh->priv_key = BN_dup(dhp->priv_key); | ||
863 | if ((dh->pub_key == NULL) || | ||
864 | (dh->priv_key == NULL)) { | ||
865 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
866 | goto err; | ||
867 | } | ||
868 | } | ||
869 | r[0] = dh->p; | ||
870 | r[1] = dh->g; | ||
871 | r[2] = dh->pub_key; | ||
872 | } else if (type & SSL_kECDHE) { | ||
873 | const EC_GROUP *group; | ||
874 | |||
875 | ecdhp = cert->ecdh_tmp; | ||
876 | if (ecdhp == NULL && s->cert->ecdh_tmp_cb != NULL) | ||
877 | ecdhp = s->cert->ecdh_tmp_cb(s, 0, | ||
878 | SSL_C_PKEYLENGTH(s->s3->tmp.new_cipher)); | ||
879 | if (ecdhp == NULL) { | ||
880 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
881 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, SSL_R_MISSING_TMP_ECDH_KEY); | ||
882 | goto f_err; | ||
883 | } | ||
884 | |||
885 | if (s->s3->tmp.ecdh != NULL) { | ||
886 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | ||
887 | goto err; | ||
888 | } | ||
889 | |||
890 | /* Duplicate the ECDH structure. */ | ||
891 | if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) { | ||
892 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
893 | goto err; | ||
894 | } | ||
895 | s->s3->tmp.ecdh = ecdh; | ||
896 | |||
897 | if ((EC_KEY_get0_public_key(ecdh) == NULL) || | ||
898 | (EC_KEY_get0_private_key(ecdh) == NULL) || | ||
899 | (s->options & SSL_OP_SINGLE_ECDH_USE)) { | ||
900 | if (!EC_KEY_generate_key(ecdh)) { | ||
901 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
902 | goto err; | ||
903 | } | ||
904 | } | ||
905 | |||
906 | if (((group = EC_KEY_get0_group(ecdh)) == NULL) || | ||
907 | (EC_KEY_get0_public_key(ecdh) == NULL) || | ||
908 | (EC_KEY_get0_private_key(ecdh) == NULL)) { | ||
909 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
910 | goto err; | ||
911 | } | ||
912 | |||
913 | /* XXX: For now, we only support ephemeral ECDH | ||
914 | * keys over named (not generic) curves. For | ||
915 | * supported named curves, curve_id is non-zero. | ||
916 | */ | ||
917 | if ((curve_id = tls1_ec_nid2curve_id( | ||
918 | EC_GROUP_get_curve_name(group))) == 0) { | ||
919 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); | ||
920 | goto err; | ||
921 | } | ||
922 | |||
923 | /* Encode the public key. | ||
924 | * First check the size of encoding and | ||
925 | * allocate memory accordingly. | ||
926 | */ | ||
927 | encodedlen = EC_POINT_point2oct(group, | ||
928 | EC_KEY_get0_public_key(ecdh), | ||
929 | POINT_CONVERSION_UNCOMPRESSED, | ||
930 | NULL, 0, NULL); | ||
931 | |||
932 | encodedPoint = malloc(encodedlen); | ||
933 | |||
934 | bn_ctx = BN_CTX_new(); | ||
935 | if ((encodedPoint == NULL) || (bn_ctx == NULL)) { | ||
936 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | ||
937 | goto err; | ||
938 | } | ||
939 | |||
940 | |||
941 | encodedlen = EC_POINT_point2oct(group, | ||
942 | EC_KEY_get0_public_key(ecdh), | ||
943 | POINT_CONVERSION_UNCOMPRESSED, | ||
944 | encodedPoint, encodedlen, bn_ctx); | ||
945 | |||
946 | if (encodedlen == 0) { | ||
947 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
948 | goto err; | ||
949 | } | ||
950 | |||
951 | BN_CTX_free(bn_ctx); | ||
952 | bn_ctx = NULL; | ||
953 | |||
954 | /* XXX: For now, we only support named (not | ||
955 | * generic) curves in ECDH ephemeral key exchanges. | ||
956 | * In this situation, we need four additional bytes | ||
957 | * to encode the entire ServerECDHParams | ||
958 | * structure. | ||
959 | */ | ||
960 | n = 4 + encodedlen; | ||
961 | |||
962 | /* We'll generate the serverKeyExchange message | ||
963 | * explicitly so we can set these to NULLs | ||
964 | */ | ||
965 | r[0] = NULL; | ||
966 | r[1] = NULL; | ||
967 | r[2] = NULL; | ||
968 | r[3] = NULL; | ||
969 | } else { | ||
970 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
971 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | ||
972 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | ||
973 | goto f_err; | ||
974 | } | ||
975 | for (i = 0; r[i] != NULL; i++) { | ||
976 | nr[i] = BN_num_bytes(r[i]); | ||
977 | n += 2 + nr[i]; | ||
978 | } | ||
979 | |||
980 | if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) { | ||
981 | if ((pkey = ssl_get_sign_pkey(s, | ||
982 | s->s3->tmp.new_cipher, NULL)) == NULL) { | ||
983 | al = SSL_AD_DECODE_ERROR; | ||
984 | goto f_err; | ||
985 | } | ||
986 | kn = EVP_PKEY_size(pkey); | ||
987 | } else { | ||
988 | pkey = NULL; | ||
989 | kn = 0; | ||
990 | } | ||
991 | |||
992 | if (!BUF_MEM_grow_clean(buf, n + DTLS1_HM_HEADER_LENGTH + kn)) { | ||
993 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF); | ||
994 | goto err; | ||
995 | } | ||
996 | d = (unsigned char *)s->init_buf->data; | ||
997 | p = &(d[DTLS1_HM_HEADER_LENGTH]); | ||
998 | |||
999 | for (i = 0; r[i] != NULL; i++) { | ||
1000 | s2n(nr[i], p); | ||
1001 | BN_bn2bin(r[i], p); | ||
1002 | p += nr[i]; | ||
1003 | } | ||
1004 | |||
1005 | if (type & SSL_kECDHE) { | ||
1006 | /* XXX: For now, we only support named (not generic) curves. | ||
1007 | * In this situation, the serverKeyExchange message has: | ||
1008 | * [1 byte CurveType], [2 byte CurveName] | ||
1009 | * [1 byte length of encoded point], followed by | ||
1010 | * the actual encoded point itself | ||
1011 | */ | ||
1012 | *p = NAMED_CURVE_TYPE; | ||
1013 | p += 1; | ||
1014 | *p = 0; | ||
1015 | p += 1; | ||
1016 | *p = curve_id; | ||
1017 | p += 1; | ||
1018 | *p = encodedlen; | ||
1019 | p += 1; | ||
1020 | memcpy((unsigned char*)p, | ||
1021 | (unsigned char *)encodedPoint, encodedlen); | ||
1022 | free(encodedPoint); | ||
1023 | encodedPoint = NULL; | ||
1024 | p += encodedlen; | ||
1025 | } | ||
1026 | |||
1027 | |||
1028 | /* not anonymous */ | ||
1029 | if (pkey != NULL) { | ||
1030 | /* n is the length of the params, they start at | ||
1031 | * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space | ||
1032 | * at the end. */ | ||
1033 | if (pkey->type == EVP_PKEY_RSA) { | ||
1034 | q = md_buf; | ||
1035 | j = 0; | ||
1036 | for (num = 2; num > 0; num--) { | ||
1037 | if (!EVP_DigestInit_ex(&md_ctx, (num == 2) | ||
1038 | ? s->ctx->md5 : s->ctx->sha1, NULL)) | ||
1039 | goto err; | ||
1040 | EVP_DigestUpdate(&md_ctx, | ||
1041 | &(s->s3->client_random[0]), | ||
1042 | SSL3_RANDOM_SIZE); | ||
1043 | EVP_DigestUpdate(&md_ctx, | ||
1044 | &(s->s3->server_random[0]), | ||
1045 | SSL3_RANDOM_SIZE); | ||
1046 | EVP_DigestUpdate(&md_ctx, | ||
1047 | &(d[DTLS1_HM_HEADER_LENGTH]), n); | ||
1048 | EVP_DigestFinal_ex(&md_ctx, q, | ||
1049 | (unsigned int *)&i); | ||
1050 | q += i; | ||
1051 | j += i; | ||
1052 | } | ||
1053 | if (RSA_sign(NID_md5_sha1, md_buf, j, &(p[2]), | ||
1054 | &u, pkey->pkey.rsa) <= 0) { | ||
1055 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA); | ||
1056 | goto err; | ||
1057 | } | ||
1058 | s2n(u, p); | ||
1059 | n += u + 2; | ||
1060 | } else | ||
1061 | if (pkey->type == EVP_PKEY_DSA) { | ||
1062 | /* lets do DSS */ | ||
1063 | EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL); | ||
1064 | EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]), SSL3_RANDOM_SIZE); | ||
1065 | EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]), SSL3_RANDOM_SIZE); | ||
1066 | EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n); | ||
1067 | if (!EVP_SignFinal(&md_ctx, &(p[2]), | ||
1068 | (unsigned int *)&i, pkey)) { | ||
1069 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA); | ||
1070 | goto err; | ||
1071 | } | ||
1072 | s2n(i, p); | ||
1073 | n += i + 2; | ||
1074 | } else | ||
1075 | if (pkey->type == EVP_PKEY_EC) { | ||
1076 | /* let's do ECDSA */ | ||
1077 | EVP_SignInit_ex(&md_ctx, EVP_ecdsa(), NULL); | ||
1078 | EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]), SSL3_RANDOM_SIZE); | ||
1079 | EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]), SSL3_RANDOM_SIZE); | ||
1080 | EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n); | ||
1081 | if (!EVP_SignFinal(&md_ctx, &(p[2]), | ||
1082 | (unsigned int *)&i, pkey)) { | ||
1083 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_ECDSA); | ||
1084 | goto err; | ||
1085 | } | ||
1086 | s2n(i, p); | ||
1087 | n += i + 2; | ||
1088 | } else | ||
1089 | { | ||
1090 | /* Is this error check actually needed? */ | ||
1091 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1092 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, SSL_R_UNKNOWN_PKEY_TYPE); | ||
1093 | goto f_err; | ||
1094 | } | ||
1095 | } | ||
1096 | |||
1097 | d = dtls1_set_message_header(s, d, | ||
1098 | SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n); | ||
1099 | |||
1100 | /* we should now have things packed up, so lets send | ||
1101 | * it off */ | ||
1102 | s->init_num = n + DTLS1_HM_HEADER_LENGTH; | ||
1103 | s->init_off = 0; | ||
1104 | |||
1105 | /* buffer the message to handle re-xmits */ | ||
1106 | dtls1_buffer_message(s, 0); | ||
1107 | } | ||
1108 | |||
1109 | s->state = SSL3_ST_SW_KEY_EXCH_B; | ||
1110 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1111 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | ||
1112 | f_err: | ||
1113 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1114 | err: | ||
1115 | free(encodedPoint); | ||
1116 | BN_CTX_free(bn_ctx); | ||
1117 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1118 | return (-1); | ||
1119 | } | ||
1120 | |||
1121 | int | ||
1122 | dtls1_send_certificate_request(SSL *s) | ||
1123 | { | ||
1124 | unsigned char *p, *d; | ||
1125 | int i, j, nl, off, n; | ||
1126 | STACK_OF(X509_NAME) *sk = NULL; | ||
1127 | X509_NAME *name; | ||
1128 | BUF_MEM *buf; | ||
1129 | unsigned int msg_len; | ||
1130 | |||
1131 | if (s->state == SSL3_ST_SW_CERT_REQ_A) { | ||
1132 | buf = s->init_buf; | ||
1133 | |||
1134 | d = p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]); | ||
1135 | |||
1136 | /* get the list of acceptable cert types */ | ||
1137 | p++; | ||
1138 | n = ssl3_get_req_cert_type(s, p); | ||
1139 | d[0] = n; | ||
1140 | p += n; | ||
1141 | n++; | ||
1142 | |||
1143 | off = n; | ||
1144 | p += 2; | ||
1145 | n += 2; | ||
1146 | |||
1147 | sk = SSL_get_client_CA_list(s); | ||
1148 | nl = 0; | ||
1149 | if (sk != NULL) { | ||
1150 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { | ||
1151 | name = sk_X509_NAME_value(sk, i); | ||
1152 | j = i2d_X509_NAME(name, NULL); | ||
1153 | if (!BUF_MEM_grow_clean(buf, DTLS1_HM_HEADER_LENGTH + n + j + 2)) { | ||
1154 | SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB); | ||
1155 | goto err; | ||
1156 | } | ||
1157 | p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + n]); | ||
1158 | s2n(j, p); | ||
1159 | i2d_X509_NAME(name, &p); | ||
1160 | n += 2 + j; | ||
1161 | nl += 2 + j; | ||
1162 | } | ||
1163 | } | ||
1164 | /* else no CA names */ | ||
1165 | p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + off]); | ||
1166 | s2n(nl, p); | ||
1167 | |||
1168 | d = (unsigned char *)buf->data; | ||
1169 | *(d++) = SSL3_MT_CERTIFICATE_REQUEST; | ||
1170 | l2n3(n, d); | ||
1171 | s2n(s->d1->handshake_write_seq, d); | ||
1172 | s->d1->handshake_write_seq++; | ||
1173 | |||
1174 | /* we should now have things packed up, so lets send | ||
1175 | * it off */ | ||
1176 | |||
1177 | s->init_num = n + DTLS1_HM_HEADER_LENGTH; | ||
1178 | s->init_off = 0; | ||
1179 | |||
1180 | /* XDTLS: set message header ? */ | ||
1181 | msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH; | ||
1182 | dtls1_set_message_header(s, (void *)s->init_buf->data, | ||
1183 | SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0, msg_len); | ||
1184 | |||
1185 | /* buffer the message to handle re-xmits */ | ||
1186 | dtls1_buffer_message(s, 0); | ||
1187 | |||
1188 | s->state = SSL3_ST_SW_CERT_REQ_B; | ||
1189 | } | ||
1190 | |||
1191 | /* SSL3_ST_SW_CERT_REQ_B */ | ||
1192 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | ||
1193 | err: | ||
1194 | return (-1); | ||
1195 | } | ||
1196 | |||
1197 | int | ||
1198 | dtls1_send_server_certificate(SSL *s) | ||
1199 | { | ||
1200 | unsigned long l; | ||
1201 | X509 *x; | ||
1202 | |||
1203 | if (s->state == SSL3_ST_SW_CERT_A) { | ||
1204 | x = ssl_get_server_send_cert(s); | ||
1205 | if (x == NULL) { | ||
1206 | SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE, | ||
1207 | 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 | } | ||
1223 | |||
1224 | int | ||
1225 | dtls1_send_newsession_ticket(SSL *s) | ||
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 | /* Some length values are 16 bits, so forget it if session is | ||
1240 | * too long | ||
1241 | */ | ||
1242 | if (slen > 0xFF00) | ||
1243 | return -1; | ||
1244 | /* Grow buffer if need be: the length calculation is as | ||
1245 | * follows 12 (DTLS handshake message header) + | ||
1246 | * 4 (ticket lifetime hint) + 2 (ticket length) + | ||
1247 | * 16 (key name) + max_iv_len (iv length) + | ||
1248 | * session_length + max_enc_block_size (max encrypted session | ||
1249 | * length) + 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 = 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 | /* Initialize HMAC and cipher contexts. If callback present | ||
1265 | * it does all the work otherwise use generated values | ||
1266 | * 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 | free(senc); | ||
1272 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
1273 | return -1; | ||
1274 | } | ||
1275 | } else { | ||
1276 | arc4random_buf(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 | 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, 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 | } | ||