diff options
author | cvs2svn <admin@example.com> | 2015-03-08 16:48:49 +0000 |
---|---|---|
committer | cvs2svn <admin@example.com> | 2015-03-08 16:48:49 +0000 |
commit | decf84ba5550c1656a7fdb51b5b81969590c3f03 (patch) | |
tree | 44872802e872bdfd60730fa9cf01d9d5751251c1 /src/lib/libssl/d1_srvr.c | |
parent | 7a8f138352aa4eb7b65ac4b1a5fe7630fbee1427 (diff) | |
download | openbsd-libressl-v2.1.5.tar.gz openbsd-libressl-v2.1.5.tar.bz2 openbsd-libressl-v2.1.5.zip |
This commit was manufactured by cvs2git to create branch 'OPENBSD_5_7'.libressl-v2.1.5
Diffstat (limited to 'src/lib/libssl/d1_srvr.c')
-rw-r--r-- | src/lib/libssl/d1_srvr.c | 1357 |
1 files changed, 0 insertions, 1357 deletions
diff --git a/src/lib/libssl/d1_srvr.c b/src/lib/libssl/d1_srvr.c deleted file mode 100644 index 4e6d0da3b3..0000000000 --- a/src/lib/libssl/d1_srvr.c +++ /dev/null | |||
@@ -1,1357 +0,0 @@ | |||
1 | /* $OpenBSD: d1_srvr.c,v 1.49 2015/02/09 10:53:28 jsing 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 (s->init_buf == NULL) { | ||
232 | BUF_MEM *buf; | ||
233 | if ((buf = BUF_MEM_new()) == NULL) { | ||
234 | ret = -1; | ||
235 | goto end; | ||
236 | } | ||
237 | if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { | ||
238 | BUF_MEM_free(buf); | ||
239 | ret = -1; | ||
240 | goto end; | ||
241 | } | ||
242 | s->init_buf = buf; | ||
243 | } | ||
244 | |||
245 | if (!ssl3_setup_buffers(s)) { | ||
246 | ret = -1; | ||
247 | goto end; | ||
248 | } | ||
249 | |||
250 | s->init_num = 0; | ||
251 | |||
252 | if (s->state != SSL_ST_RENEGOTIATE) { | ||
253 | /* Ok, we now need to push on a buffering BIO so that | ||
254 | * the output is sent in a way that TCP likes :-) | ||
255 | * ...but not with SCTP :-) | ||
256 | */ | ||
257 | if (!ssl_init_wbio_buffer(s, 1)) { | ||
258 | ret = -1; | ||
259 | goto end; | ||
260 | } | ||
261 | |||
262 | if (!ssl3_init_finished_mac(s)) { | ||
263 | ret = -1; | ||
264 | goto end; | ||
265 | } | ||
266 | |||
267 | s->state = SSL3_ST_SR_CLNT_HELLO_A; | ||
268 | s->ctx->stats.sess_accept++; | ||
269 | } else { | ||
270 | /* s->state == SSL_ST_RENEGOTIATE, | ||
271 | * we will just send a HelloRequest */ | ||
272 | s->ctx->stats.sess_accept_renegotiate++; | ||
273 | s->state = SSL3_ST_SW_HELLO_REQ_A; | ||
274 | } | ||
275 | |||
276 | break; | ||
277 | |||
278 | case SSL3_ST_SW_HELLO_REQ_A: | ||
279 | case SSL3_ST_SW_HELLO_REQ_B: | ||
280 | |||
281 | s->shutdown = 0; | ||
282 | dtls1_clear_record_buffer(s); | ||
283 | dtls1_start_timer(s); | ||
284 | ret = dtls1_send_hello_request(s); | ||
285 | if (ret <= 0) | ||
286 | goto end; | ||
287 | s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A; | ||
288 | s->state = SSL3_ST_SW_FLUSH; | ||
289 | s->init_num = 0; | ||
290 | |||
291 | if (!ssl3_init_finished_mac(s)) { | ||
292 | ret = -1; | ||
293 | goto end; | ||
294 | } | ||
295 | break; | ||
296 | |||
297 | case SSL3_ST_SW_HELLO_REQ_C: | ||
298 | s->state = SSL_ST_OK; | ||
299 | break; | ||
300 | |||
301 | case SSL3_ST_SR_CLNT_HELLO_A: | ||
302 | case SSL3_ST_SR_CLNT_HELLO_B: | ||
303 | case SSL3_ST_SR_CLNT_HELLO_C: | ||
304 | |||
305 | s->shutdown = 0; | ||
306 | ret = ssl3_get_client_hello(s); | ||
307 | if (ret <= 0) | ||
308 | goto end; | ||
309 | dtls1_stop_timer(s); | ||
310 | |||
311 | if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) | ||
312 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; | ||
313 | else | ||
314 | s->state = SSL3_ST_SW_SRVR_HELLO_A; | ||
315 | |||
316 | s->init_num = 0; | ||
317 | |||
318 | /* Reflect ClientHello sequence to remain stateless while listening */ | ||
319 | if (listen) { | ||
320 | memcpy(s->s3->write_sequence, s->s3->read_sequence, sizeof(s->s3->write_sequence)); | ||
321 | } | ||
322 | |||
323 | /* If we're just listening, stop here */ | ||
324 | if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A) { | ||
325 | ret = 2; | ||
326 | s->d1->listen = 0; | ||
327 | /* Set expected sequence numbers | ||
328 | * to continue the handshake. | ||
329 | */ | ||
330 | s->d1->handshake_read_seq = 2; | ||
331 | s->d1->handshake_write_seq = 1; | ||
332 | s->d1->next_handshake_write_seq = 1; | ||
333 | goto end; | ||
334 | } | ||
335 | |||
336 | break; | ||
337 | |||
338 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: | ||
339 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: | ||
340 | |||
341 | ret = dtls1_send_hello_verify_request(s); | ||
342 | if (ret <= 0) | ||
343 | goto end; | ||
344 | s->state = SSL3_ST_SW_FLUSH; | ||
345 | s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A; | ||
346 | |||
347 | /* HelloVerifyRequest resets Finished MAC */ | ||
348 | if (s->version != DTLS1_BAD_VER) { | ||
349 | if (!ssl3_init_finished_mac(s)) { | ||
350 | ret = -1; | ||
351 | goto end; | ||
352 | } | ||
353 | } | ||
354 | break; | ||
355 | |||
356 | |||
357 | case SSL3_ST_SW_SRVR_HELLO_A: | ||
358 | case SSL3_ST_SW_SRVR_HELLO_B: | ||
359 | s->renegotiate = 2; | ||
360 | dtls1_start_timer(s); | ||
361 | ret = dtls1_send_server_hello(s); | ||
362 | if (ret <= 0) | ||
363 | goto end; | ||
364 | |||
365 | if (s->hit) { | ||
366 | if (s->tlsext_ticket_expected) | ||
367 | s->state = SSL3_ST_SW_SESSION_TICKET_A; | ||
368 | else | ||
369 | s->state = SSL3_ST_SW_CHANGE_A; | ||
370 | } else | ||
371 | s->state = SSL3_ST_SW_CERT_A; | ||
372 | s->init_num = 0; | ||
373 | break; | ||
374 | |||
375 | case SSL3_ST_SW_CERT_A: | ||
376 | case SSL3_ST_SW_CERT_B: | ||
377 | /* Check if it is anon DH. */ | ||
378 | if (!(s->s3->tmp.new_cipher->algorithm_auth & | ||
379 | SSL_aNULL)) { | ||
380 | dtls1_start_timer(s); | ||
381 | ret = dtls1_send_server_certificate(s); | ||
382 | if (ret <= 0) | ||
383 | goto end; | ||
384 | if (s->tlsext_status_expected) | ||
385 | s->state = SSL3_ST_SW_CERT_STATUS_A; | ||
386 | else | ||
387 | s->state = SSL3_ST_SW_KEY_EXCH_A; | ||
388 | } else { | ||
389 | skip = 1; | ||
390 | s->state = SSL3_ST_SW_KEY_EXCH_A; | ||
391 | } | ||
392 | s->init_num = 0; | ||
393 | break; | ||
394 | |||
395 | case SSL3_ST_SW_KEY_EXCH_A: | ||
396 | case SSL3_ST_SW_KEY_EXCH_B: | ||
397 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
398 | |||
399 | /* Only send if using a DH key exchange. */ | ||
400 | if (alg_k & (SSL_kDHE|SSL_kECDHE)) { | ||
401 | dtls1_start_timer(s); | ||
402 | ret = dtls1_send_server_key_exchange(s); | ||
403 | if (ret <= 0) | ||
404 | goto end; | ||
405 | } else | ||
406 | skip = 1; | ||
407 | |||
408 | s->state = SSL3_ST_SW_CERT_REQ_A; | ||
409 | s->init_num = 0; | ||
410 | break; | ||
411 | |||
412 | case SSL3_ST_SW_CERT_REQ_A: | ||
413 | case SSL3_ST_SW_CERT_REQ_B: | ||
414 | /* | ||
415 | * Determine whether or not we need to request a | ||
416 | * certificate. | ||
417 | * | ||
418 | * Do not request a certificate if: | ||
419 | * | ||
420 | * - We did not ask for it (SSL_VERIFY_PEER is unset). | ||
421 | * | ||
422 | * - SSL_VERIFY_CLIENT_ONCE is set and we are | ||
423 | * renegotiating. | ||
424 | * | ||
425 | * - We are using an anonymous ciphersuites | ||
426 | * (see section "Certificate request" in SSL 3 drafts | ||
427 | * and in RFC 2246) ... except when the application | ||
428 | * insists on verification (against the specs, but | ||
429 | * s3_clnt.c accepts this for SSL 3). | ||
430 | */ | ||
431 | if (!(s->verify_mode & SSL_VERIFY_PEER) || | ||
432 | ((s->session->peer != NULL) && | ||
433 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || | ||
434 | ((s->s3->tmp.new_cipher->algorithm_auth & | ||
435 | SSL_aNULL) && !(s->verify_mode & | ||
436 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) { | ||
437 | /* no cert request */ | ||
438 | skip = 1; | ||
439 | s->s3->tmp.cert_request = 0; | ||
440 | s->state = SSL3_ST_SW_SRVR_DONE_A; | ||
441 | } else { | ||
442 | s->s3->tmp.cert_request = 1; | ||
443 | dtls1_start_timer(s); | ||
444 | ret = dtls1_send_certificate_request(s); | ||
445 | if (ret <= 0) | ||
446 | goto end; | ||
447 | s->state = SSL3_ST_SW_SRVR_DONE_A; | ||
448 | s->init_num = 0; | ||
449 | } | ||
450 | break; | ||
451 | |||
452 | case SSL3_ST_SW_SRVR_DONE_A: | ||
453 | case SSL3_ST_SW_SRVR_DONE_B: | ||
454 | dtls1_start_timer(s); | ||
455 | ret = dtls1_send_server_done(s); | ||
456 | if (ret <= 0) | ||
457 | goto end; | ||
458 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; | ||
459 | s->state = SSL3_ST_SW_FLUSH; | ||
460 | s->init_num = 0; | ||
461 | break; | ||
462 | |||
463 | case SSL3_ST_SW_FLUSH: | ||
464 | s->rwstate = SSL_WRITING; | ||
465 | if (BIO_flush(s->wbio) <= 0) { | ||
466 | /* If the write error was fatal, stop trying */ | ||
467 | if (!BIO_should_retry(s->wbio)) { | ||
468 | s->rwstate = SSL_NOTHING; | ||
469 | s->state = s->s3->tmp.next_state; | ||
470 | } | ||
471 | |||
472 | ret = -1; | ||
473 | goto end; | ||
474 | } | ||
475 | s->rwstate = SSL_NOTHING; | ||
476 | s->state = s->s3->tmp.next_state; | ||
477 | break; | ||
478 | |||
479 | case SSL3_ST_SR_CERT_A: | ||
480 | case SSL3_ST_SR_CERT_B: | ||
481 | /* Check for second client hello (MS SGC) */ | ||
482 | ret = ssl3_check_client_hello(s); | ||
483 | if (ret <= 0) | ||
484 | goto end; | ||
485 | if (ret == 2) { | ||
486 | dtls1_stop_timer(s); | ||
487 | s->state = SSL3_ST_SR_CLNT_HELLO_C; | ||
488 | } else { | ||
489 | /* could be sent for a DH cert, even if we | ||
490 | * have not asked for it :-) */ | ||
491 | ret = ssl3_get_client_certificate(s); | ||
492 | if (ret <= 0) | ||
493 | goto end; | ||
494 | s->init_num = 0; | ||
495 | s->state = SSL3_ST_SR_KEY_EXCH_A; | ||
496 | } | ||
497 | break; | ||
498 | |||
499 | case SSL3_ST_SR_KEY_EXCH_A: | ||
500 | case SSL3_ST_SR_KEY_EXCH_B: | ||
501 | ret = ssl3_get_client_key_exchange(s); | ||
502 | if (ret <= 0) | ||
503 | goto end; | ||
504 | |||
505 | s->state = SSL3_ST_SR_CERT_VRFY_A; | ||
506 | s->init_num = 0; | ||
507 | |||
508 | if (ret == 2) { | ||
509 | /* For the ECDH ciphersuites when | ||
510 | * the client sends its ECDH pub key in | ||
511 | * a certificate, the CertificateVerify | ||
512 | * message is not sent. | ||
513 | */ | ||
514 | s->state = SSL3_ST_SR_FINISHED_A; | ||
515 | s->init_num = 0; | ||
516 | } else { | ||
517 | s->state = SSL3_ST_SR_CERT_VRFY_A; | ||
518 | s->init_num = 0; | ||
519 | |||
520 | /* We need to get hashes here so if there is | ||
521 | * a client cert, it can be verified */ | ||
522 | s->method->ssl3_enc->cert_verify_mac(s, | ||
523 | NID_md5, &(s->s3->tmp.cert_verify_md[0])); | ||
524 | s->method->ssl3_enc->cert_verify_mac(s, | ||
525 | NID_sha1, | ||
526 | &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH])); | ||
527 | } | ||
528 | break; | ||
529 | |||
530 | case SSL3_ST_SR_CERT_VRFY_A: | ||
531 | case SSL3_ST_SR_CERT_VRFY_B: | ||
532 | |||
533 | s->d1->change_cipher_spec_ok = 1; | ||
534 | /* we should decide if we expected this one */ | ||
535 | ret = ssl3_get_cert_verify(s); | ||
536 | if (ret <= 0) | ||
537 | goto end; | ||
538 | s->state = SSL3_ST_SR_FINISHED_A; | ||
539 | s->init_num = 0; | ||
540 | break; | ||
541 | |||
542 | case SSL3_ST_SR_FINISHED_A: | ||
543 | case SSL3_ST_SR_FINISHED_B: | ||
544 | s->d1->change_cipher_spec_ok = 1; | ||
545 | ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, | ||
546 | SSL3_ST_SR_FINISHED_B); | ||
547 | if (ret <= 0) | ||
548 | goto end; | ||
549 | dtls1_stop_timer(s); | ||
550 | if (s->hit) | ||
551 | s->state = SSL_ST_OK; | ||
552 | else if (s->tlsext_ticket_expected) | ||
553 | s->state = SSL3_ST_SW_SESSION_TICKET_A; | ||
554 | else | ||
555 | s->state = SSL3_ST_SW_CHANGE_A; | ||
556 | s->init_num = 0; | ||
557 | break; | ||
558 | |||
559 | case SSL3_ST_SW_SESSION_TICKET_A: | ||
560 | case SSL3_ST_SW_SESSION_TICKET_B: | ||
561 | ret = dtls1_send_newsession_ticket(s); | ||
562 | if (ret <= 0) | ||
563 | goto end; | ||
564 | s->state = SSL3_ST_SW_CHANGE_A; | ||
565 | s->init_num = 0; | ||
566 | break; | ||
567 | |||
568 | case SSL3_ST_SW_CERT_STATUS_A: | ||
569 | case SSL3_ST_SW_CERT_STATUS_B: | ||
570 | ret = ssl3_send_cert_status(s); | ||
571 | if (ret <= 0) | ||
572 | goto end; | ||
573 | s->state = SSL3_ST_SW_KEY_EXCH_A; | ||
574 | s->init_num = 0; | ||
575 | break; | ||
576 | |||
577 | |||
578 | case SSL3_ST_SW_CHANGE_A: | ||
579 | case SSL3_ST_SW_CHANGE_B: | ||
580 | |||
581 | s->session->cipher = s->s3->tmp.new_cipher; | ||
582 | if (!s->method->ssl3_enc->setup_key_block(s)) { | ||
583 | ret = -1; | ||
584 | goto end; | ||
585 | } | ||
586 | |||
587 | ret = dtls1_send_change_cipher_spec(s, | ||
588 | SSL3_ST_SW_CHANGE_A, SSL3_ST_SW_CHANGE_B); | ||
589 | |||
590 | if (ret <= 0) | ||
591 | goto end; | ||
592 | |||
593 | |||
594 | s->state = SSL3_ST_SW_FINISHED_A; | ||
595 | s->init_num = 0; | ||
596 | |||
597 | if (!s->method->ssl3_enc->change_cipher_state(s, | ||
598 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) { | ||
599 | ret = -1; | ||
600 | goto end; | ||
601 | } | ||
602 | |||
603 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | ||
604 | break; | ||
605 | |||
606 | case SSL3_ST_SW_FINISHED_A: | ||
607 | case SSL3_ST_SW_FINISHED_B: | ||
608 | ret = dtls1_send_finished(s, | ||
609 | SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B, | ||
610 | s->method->ssl3_enc->server_finished_label, | ||
611 | s->method->ssl3_enc->server_finished_label_len); | ||
612 | if (ret <= 0) | ||
613 | goto end; | ||
614 | s->state = SSL3_ST_SW_FLUSH; | ||
615 | if (s->hit) { | ||
616 | s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A; | ||
617 | |||
618 | } else { | ||
619 | s->s3->tmp.next_state = SSL_ST_OK; | ||
620 | } | ||
621 | s->init_num = 0; | ||
622 | break; | ||
623 | |||
624 | case SSL_ST_OK: | ||
625 | /* clean a few things up */ | ||
626 | ssl3_cleanup_key_block(s); | ||
627 | |||
628 | /* remove buffering on output */ | ||
629 | ssl_free_wbio_buffer(s); | ||
630 | |||
631 | s->init_num = 0; | ||
632 | |||
633 | if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */ | ||
634 | { | ||
635 | s->renegotiate = 0; | ||
636 | s->new_session = 0; | ||
637 | |||
638 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); | ||
639 | |||
640 | s->ctx->stats.sess_accept_good++; | ||
641 | /* s->server=1; */ | ||
642 | s->handshake_func = dtls1_accept; | ||
643 | |||
644 | if (cb != NULL) | ||
645 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | ||
646 | } | ||
647 | |||
648 | ret = 1; | ||
649 | |||
650 | /* done handshaking, next message is client hello */ | ||
651 | s->d1->handshake_read_seq = 0; | ||
652 | /* next message is server hello */ | ||
653 | s->d1->handshake_write_seq = 0; | ||
654 | s->d1->next_handshake_write_seq = 0; | ||
655 | goto end; | ||
656 | /* break; */ | ||
657 | |||
658 | default: | ||
659 | SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_UNKNOWN_STATE); | ||
660 | ret = -1; | ||
661 | goto end; | ||
662 | /* break; */ | ||
663 | } | ||
664 | |||
665 | if (!s->s3->tmp.reuse_message && !skip) { | ||
666 | if (s->debug) { | ||
667 | if ((ret = BIO_flush(s->wbio)) <= 0) | ||
668 | goto end; | ||
669 | } | ||
670 | |||
671 | if ((cb != NULL) && (s->state != state)) { | ||
672 | new_state = s->state; | ||
673 | s->state = state; | ||
674 | cb(s, SSL_CB_ACCEPT_LOOP, 1); | ||
675 | s->state = new_state; | ||
676 | } | ||
677 | } | ||
678 | skip = 0; | ||
679 | } | ||
680 | end: | ||
681 | /* BIO_flush(s->wbio); */ | ||
682 | |||
683 | s->in_handshake--; | ||
684 | |||
685 | if (cb != NULL) | ||
686 | cb(s, SSL_CB_ACCEPT_EXIT, ret); | ||
687 | return (ret); | ||
688 | } | ||
689 | |||
690 | int | ||
691 | dtls1_send_hello_request(SSL *s) | ||
692 | { | ||
693 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) { | ||
694 | ssl3_handshake_msg_start(s, SSL3_MT_HELLO_REQUEST); | ||
695 | ssl3_handshake_msg_finish(s, 0); | ||
696 | |||
697 | s->state = SSL3_ST_SW_HELLO_REQ_B; | ||
698 | } | ||
699 | |||
700 | /* SSL3_ST_SW_HELLO_REQ_B */ | ||
701 | return (ssl3_handshake_write(s)); | ||
702 | } | ||
703 | |||
704 | int | ||
705 | dtls1_send_hello_verify_request(SSL *s) | ||
706 | { | ||
707 | unsigned char *d, *p; | ||
708 | |||
709 | if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) { | ||
710 | d = p = ssl3_handshake_msg_start(s, | ||
711 | DTLS1_MT_HELLO_VERIFY_REQUEST); | ||
712 | |||
713 | *(p++) = s->version >> 8; | ||
714 | *(p++) = s->version & 0xFF; | ||
715 | |||
716 | if (s->ctx->app_gen_cookie_cb == NULL || | ||
717 | s->ctx->app_gen_cookie_cb(s, s->d1->cookie, | ||
718 | &(s->d1->cookie_len)) == 0) { | ||
719 | SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST, | ||
720 | ERR_R_INTERNAL_ERROR); | ||
721 | return 0; | ||
722 | } | ||
723 | |||
724 | *(p++) = (unsigned char) s->d1->cookie_len; | ||
725 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | ||
726 | p += s->d1->cookie_len; | ||
727 | |||
728 | ssl3_handshake_msg_finish(s, p - d); | ||
729 | |||
730 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; | ||
731 | } | ||
732 | |||
733 | /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ | ||
734 | return (ssl3_handshake_write(s)); | ||
735 | } | ||
736 | |||
737 | int | ||
738 | dtls1_send_server_hello(SSL *s) | ||
739 | { | ||
740 | unsigned char *bufend; | ||
741 | unsigned char *p, *d; | ||
742 | unsigned int sl; | ||
743 | |||
744 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) { | ||
745 | d = p = ssl3_handshake_msg_start(s, SSL3_MT_SERVER_HELLO); | ||
746 | |||
747 | *(p++) = s->version >> 8; | ||
748 | *(p++) = s->version & 0xff; | ||
749 | |||
750 | /* Random stuff */ | ||
751 | arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE); | ||
752 | memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE); | ||
753 | p += SSL3_RANDOM_SIZE; | ||
754 | |||
755 | /* now in theory we have 3 options to sending back the | ||
756 | * session id. If it is a re-use, we send back the | ||
757 | * old session-id, if it is a new session, we send | ||
758 | * back the new session-id or we send back a 0 length | ||
759 | * session-id if we want it to be single use. | ||
760 | * Currently I will not implement the '0' length session-id | ||
761 | * 12-Jan-98 - I'll now support the '0' length stuff. | ||
762 | */ | ||
763 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) | ||
764 | s->session->session_id_length = 0; | ||
765 | |||
766 | sl = s->session->session_id_length; | ||
767 | if (sl > sizeof s->session->session_id) { | ||
768 | SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, | ||
769 | ERR_R_INTERNAL_ERROR); | ||
770 | return -1; | ||
771 | } | ||
772 | *(p++) = sl; | ||
773 | memcpy(p, s->session->session_id, sl); | ||
774 | p += sl; | ||
775 | |||
776 | /* put the cipher */ | ||
777 | if (s->s3->tmp.new_cipher == NULL) | ||
778 | return -1; | ||
779 | s2n(ssl3_cipher_get_value(s->s3->tmp.new_cipher), p); | ||
780 | |||
781 | /* put the compression method */ | ||
782 | *(p++) = 0; | ||
783 | |||
784 | bufend = (unsigned char *)s->init_buf->data + | ||
785 | SSL3_RT_MAX_PLAIN_LENGTH; | ||
786 | if ((p = ssl_add_serverhello_tlsext(s, p, bufend)) == NULL) { | ||
787 | SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, | ||
788 | ERR_R_INTERNAL_ERROR); | ||
789 | return -1; | ||
790 | } | ||
791 | |||
792 | ssl3_handshake_msg_finish(s, p - d); | ||
793 | |||
794 | s->state = SSL3_ST_SW_SRVR_HELLO_B; | ||
795 | } | ||
796 | |||
797 | /* SSL3_ST_SW_SRVR_HELLO_B */ | ||
798 | return (ssl3_handshake_write(s)); | ||
799 | } | ||
800 | |||
801 | int | ||
802 | dtls1_send_server_done(SSL *s) | ||
803 | { | ||
804 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) { | ||
805 | ssl3_handshake_msg_start(s, SSL3_MT_SERVER_DONE); | ||
806 | ssl3_handshake_msg_finish(s, 0); | ||
807 | |||
808 | s->state = SSL3_ST_SW_SRVR_DONE_B; | ||
809 | } | ||
810 | |||
811 | /* SSL3_ST_SW_SRVR_DONE_B */ | ||
812 | return (ssl3_handshake_write(s)); | ||
813 | } | ||
814 | |||
815 | int | ||
816 | dtls1_send_server_key_exchange(SSL *s) | ||
817 | { | ||
818 | unsigned char *q; | ||
819 | int j, num; | ||
820 | unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; | ||
821 | unsigned int u; | ||
822 | DH *dh = NULL, *dhp; | ||
823 | EC_KEY *ecdh = NULL, *ecdhp; | ||
824 | unsigned char *encodedPoint = NULL; | ||
825 | int encodedlen = 0; | ||
826 | int curve_id = 0; | ||
827 | BN_CTX *bn_ctx = NULL; | ||
828 | |||
829 | EVP_PKEY *pkey; | ||
830 | unsigned char *p, *d; | ||
831 | int al, i; | ||
832 | unsigned long type; | ||
833 | int n; | ||
834 | CERT *cert; | ||
835 | BIGNUM *r[4]; | ||
836 | int nr[4], kn; | ||
837 | BUF_MEM *buf; | ||
838 | EVP_MD_CTX md_ctx; | ||
839 | |||
840 | EVP_MD_CTX_init(&md_ctx); | ||
841 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) { | ||
842 | type = s->s3->tmp.new_cipher->algorithm_mkey; | ||
843 | cert = s->cert; | ||
844 | |||
845 | buf = s->init_buf; | ||
846 | |||
847 | r[0] = r[1] = r[2] = r[3] = NULL; | ||
848 | n = 0; | ||
849 | |||
850 | if (type & SSL_kDHE) { | ||
851 | dhp = cert->dh_tmp; | ||
852 | if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) | ||
853 | dhp = s->cert->dh_tmp_cb(s, 0, | ||
854 | SSL_C_PKEYLENGTH(s->s3->tmp.new_cipher)); | ||
855 | if (dhp == NULL) { | ||
856 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
857 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, SSL_R_MISSING_TMP_DH_KEY); | ||
858 | goto f_err; | ||
859 | } | ||
860 | |||
861 | if (s->s3->tmp.dh != NULL) { | ||
862 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | ||
863 | goto err; | ||
864 | } | ||
865 | |||
866 | if ((dh = DHparams_dup(dhp)) == NULL) { | ||
867 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
868 | goto err; | ||
869 | } | ||
870 | |||
871 | s->s3->tmp.dh = dh; | ||
872 | if ((dhp->pub_key == NULL || dhp->priv_key == NULL || | ||
873 | (s->options & SSL_OP_SINGLE_DH_USE))) { | ||
874 | if (!DH_generate_key(dh)) { | ||
875 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | ||
876 | ERR_R_DH_LIB); | ||
877 | goto err; | ||
878 | } | ||
879 | } else { | ||
880 | dh->pub_key = BN_dup(dhp->pub_key); | ||
881 | dh->priv_key = BN_dup(dhp->priv_key); | ||
882 | if ((dh->pub_key == NULL) || | ||
883 | (dh->priv_key == NULL)) { | ||
884 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
885 | goto err; | ||
886 | } | ||
887 | } | ||
888 | r[0] = dh->p; | ||
889 | r[1] = dh->g; | ||
890 | r[2] = dh->pub_key; | ||
891 | } else if (type & SSL_kECDHE) { | ||
892 | const EC_GROUP *group; | ||
893 | |||
894 | ecdhp = cert->ecdh_tmp; | ||
895 | if (ecdhp == NULL && s->cert->ecdh_tmp_cb != NULL) | ||
896 | ecdhp = s->cert->ecdh_tmp_cb(s, 0, | ||
897 | SSL_C_PKEYLENGTH(s->s3->tmp.new_cipher)); | ||
898 | if (ecdhp == NULL) { | ||
899 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
900 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, SSL_R_MISSING_TMP_ECDH_KEY); | ||
901 | goto f_err; | ||
902 | } | ||
903 | |||
904 | if (s->s3->tmp.ecdh != NULL) { | ||
905 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | ||
906 | goto err; | ||
907 | } | ||
908 | |||
909 | /* Duplicate the ECDH structure. */ | ||
910 | if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) { | ||
911 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
912 | goto err; | ||
913 | } | ||
914 | s->s3->tmp.ecdh = ecdh; | ||
915 | |||
916 | if ((EC_KEY_get0_public_key(ecdh) == NULL) || | ||
917 | (EC_KEY_get0_private_key(ecdh) == NULL) || | ||
918 | (s->options & SSL_OP_SINGLE_ECDH_USE)) { | ||
919 | if (!EC_KEY_generate_key(ecdh)) { | ||
920 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
921 | goto err; | ||
922 | } | ||
923 | } | ||
924 | |||
925 | if (((group = EC_KEY_get0_group(ecdh)) == NULL) || | ||
926 | (EC_KEY_get0_public_key(ecdh) == NULL) || | ||
927 | (EC_KEY_get0_private_key(ecdh) == NULL)) { | ||
928 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
929 | goto err; | ||
930 | } | ||
931 | |||
932 | /* XXX: For now, we only support ephemeral ECDH | ||
933 | * keys over named (not generic) curves. For | ||
934 | * supported named curves, curve_id is non-zero. | ||
935 | */ | ||
936 | if ((curve_id = tls1_ec_nid2curve_id( | ||
937 | EC_GROUP_get_curve_name(group))) == 0) { | ||
938 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); | ||
939 | goto err; | ||
940 | } | ||
941 | |||
942 | /* Encode the public key. | ||
943 | * First check the size of encoding and | ||
944 | * allocate memory accordingly. | ||
945 | */ | ||
946 | encodedlen = EC_POINT_point2oct(group, | ||
947 | EC_KEY_get0_public_key(ecdh), | ||
948 | POINT_CONVERSION_UNCOMPRESSED, | ||
949 | NULL, 0, NULL); | ||
950 | |||
951 | encodedPoint = malloc(encodedlen); | ||
952 | |||
953 | bn_ctx = BN_CTX_new(); | ||
954 | if ((encodedPoint == NULL) || (bn_ctx == NULL)) { | ||
955 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | ||
956 | goto err; | ||
957 | } | ||
958 | |||
959 | |||
960 | encodedlen = EC_POINT_point2oct(group, | ||
961 | EC_KEY_get0_public_key(ecdh), | ||
962 | POINT_CONVERSION_UNCOMPRESSED, | ||
963 | encodedPoint, encodedlen, bn_ctx); | ||
964 | |||
965 | if (encodedlen == 0) { | ||
966 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
967 | goto err; | ||
968 | } | ||
969 | |||
970 | BN_CTX_free(bn_ctx); | ||
971 | bn_ctx = NULL; | ||
972 | |||
973 | /* XXX: For now, we only support named (not | ||
974 | * generic) curves in ECDH ephemeral key exchanges. | ||
975 | * In this situation, we need four additional bytes | ||
976 | * to encode the entire ServerECDHParams | ||
977 | * structure. | ||
978 | */ | ||
979 | n = 4 + encodedlen; | ||
980 | |||
981 | /* We'll generate the serverKeyExchange message | ||
982 | * explicitly so we can set these to NULLs | ||
983 | */ | ||
984 | r[0] = NULL; | ||
985 | r[1] = NULL; | ||
986 | r[2] = NULL; | ||
987 | r[3] = NULL; | ||
988 | } else { | ||
989 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
990 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, | ||
991 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | ||
992 | goto f_err; | ||
993 | } | ||
994 | for (i = 0; r[i] != NULL; i++) { | ||
995 | nr[i] = BN_num_bytes(r[i]); | ||
996 | n += 2 + nr[i]; | ||
997 | } | ||
998 | |||
999 | if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) { | ||
1000 | if ((pkey = ssl_get_sign_pkey(s, | ||
1001 | s->s3->tmp.new_cipher, NULL)) == NULL) { | ||
1002 | al = SSL_AD_DECODE_ERROR; | ||
1003 | goto f_err; | ||
1004 | } | ||
1005 | kn = EVP_PKEY_size(pkey); | ||
1006 | } else { | ||
1007 | pkey = NULL; | ||
1008 | kn = 0; | ||
1009 | } | ||
1010 | |||
1011 | if (!BUF_MEM_grow_clean(buf, n + DTLS1_HM_HEADER_LENGTH + kn)) { | ||
1012 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF); | ||
1013 | goto err; | ||
1014 | } | ||
1015 | d = (unsigned char *)s->init_buf->data; | ||
1016 | p = &(d[DTLS1_HM_HEADER_LENGTH]); | ||
1017 | |||
1018 | for (i = 0; r[i] != NULL; i++) { | ||
1019 | s2n(nr[i], p); | ||
1020 | BN_bn2bin(r[i], p); | ||
1021 | p += nr[i]; | ||
1022 | } | ||
1023 | |||
1024 | if (type & SSL_kECDHE) { | ||
1025 | /* XXX: For now, we only support named (not generic) curves. | ||
1026 | * In this situation, the serverKeyExchange message has: | ||
1027 | * [1 byte CurveType], [2 byte CurveName] | ||
1028 | * [1 byte length of encoded point], followed by | ||
1029 | * the actual encoded point itself | ||
1030 | */ | ||
1031 | *p = NAMED_CURVE_TYPE; | ||
1032 | p += 1; | ||
1033 | *p = 0; | ||
1034 | p += 1; | ||
1035 | *p = curve_id; | ||
1036 | p += 1; | ||
1037 | *p = encodedlen; | ||
1038 | p += 1; | ||
1039 | memcpy((unsigned char*)p, | ||
1040 | (unsigned char *)encodedPoint, encodedlen); | ||
1041 | free(encodedPoint); | ||
1042 | encodedPoint = NULL; | ||
1043 | p += encodedlen; | ||
1044 | } | ||
1045 | |||
1046 | |||
1047 | /* not anonymous */ | ||
1048 | if (pkey != NULL) { | ||
1049 | /* n is the length of the params, they start at | ||
1050 | * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space | ||
1051 | * at the end. */ | ||
1052 | if (pkey->type == EVP_PKEY_RSA) { | ||
1053 | q = md_buf; | ||
1054 | j = 0; | ||
1055 | for (num = 2; num > 0; num--) { | ||
1056 | if (!EVP_DigestInit_ex(&md_ctx, (num == 2) | ||
1057 | ? s->ctx->md5 : s->ctx->sha1, NULL)) | ||
1058 | goto err; | ||
1059 | EVP_DigestUpdate(&md_ctx, | ||
1060 | &(s->s3->client_random[0]), | ||
1061 | SSL3_RANDOM_SIZE); | ||
1062 | EVP_DigestUpdate(&md_ctx, | ||
1063 | &(s->s3->server_random[0]), | ||
1064 | SSL3_RANDOM_SIZE); | ||
1065 | EVP_DigestUpdate(&md_ctx, | ||
1066 | &(d[DTLS1_HM_HEADER_LENGTH]), n); | ||
1067 | EVP_DigestFinal_ex(&md_ctx, q, | ||
1068 | (unsigned int *)&i); | ||
1069 | q += i; | ||
1070 | j += i; | ||
1071 | } | ||
1072 | if (RSA_sign(NID_md5_sha1, md_buf, j, &(p[2]), | ||
1073 | &u, pkey->pkey.rsa) <= 0) { | ||
1074 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA); | ||
1075 | goto err; | ||
1076 | } | ||
1077 | s2n(u, p); | ||
1078 | n += u + 2; | ||
1079 | } else | ||
1080 | if (pkey->type == EVP_PKEY_DSA) { | ||
1081 | /* lets do DSS */ | ||
1082 | EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL); | ||
1083 | EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]), SSL3_RANDOM_SIZE); | ||
1084 | EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]), SSL3_RANDOM_SIZE); | ||
1085 | EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n); | ||
1086 | if (!EVP_SignFinal(&md_ctx, &(p[2]), | ||
1087 | (unsigned int *)&i, pkey)) { | ||
1088 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA); | ||
1089 | goto err; | ||
1090 | } | ||
1091 | s2n(i, p); | ||
1092 | n += i + 2; | ||
1093 | } else | ||
1094 | if (pkey->type == EVP_PKEY_EC) { | ||
1095 | /* let's do ECDSA */ | ||
1096 | EVP_SignInit_ex(&md_ctx, EVP_ecdsa(), NULL); | ||
1097 | EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]), SSL3_RANDOM_SIZE); | ||
1098 | EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]), SSL3_RANDOM_SIZE); | ||
1099 | EVP_SignUpdate(&md_ctx, &(d[DTLS1_HM_HEADER_LENGTH]), n); | ||
1100 | if (!EVP_SignFinal(&md_ctx, &(p[2]), | ||
1101 | (unsigned int *)&i, pkey)) { | ||
1102 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_ECDSA); | ||
1103 | goto err; | ||
1104 | } | ||
1105 | s2n(i, p); | ||
1106 | n += i + 2; | ||
1107 | } else | ||
1108 | { | ||
1109 | /* Is this error check actually needed? */ | ||
1110 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1111 | SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, SSL_R_UNKNOWN_PKEY_TYPE); | ||
1112 | goto f_err; | ||
1113 | } | ||
1114 | } | ||
1115 | |||
1116 | d = dtls1_set_message_header(s, d, | ||
1117 | SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n); | ||
1118 | |||
1119 | /* we should now have things packed up, so lets send | ||
1120 | * it off */ | ||
1121 | s->init_num = n + DTLS1_HM_HEADER_LENGTH; | ||
1122 | s->init_off = 0; | ||
1123 | |||
1124 | /* buffer the message to handle re-xmits */ | ||
1125 | dtls1_buffer_message(s, 0); | ||
1126 | } | ||
1127 | |||
1128 | s->state = SSL3_ST_SW_KEY_EXCH_B; | ||
1129 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1130 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | ||
1131 | f_err: | ||
1132 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1133 | err: | ||
1134 | free(encodedPoint); | ||
1135 | BN_CTX_free(bn_ctx); | ||
1136 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1137 | return (-1); | ||
1138 | } | ||
1139 | |||
1140 | int | ||
1141 | dtls1_send_certificate_request(SSL *s) | ||
1142 | { | ||
1143 | unsigned char *p, *d; | ||
1144 | int i, j, nl, off, n; | ||
1145 | STACK_OF(X509_NAME) *sk = NULL; | ||
1146 | X509_NAME *name; | ||
1147 | BUF_MEM *buf; | ||
1148 | unsigned int msg_len; | ||
1149 | |||
1150 | if (s->state == SSL3_ST_SW_CERT_REQ_A) { | ||
1151 | buf = s->init_buf; | ||
1152 | |||
1153 | d = p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]); | ||
1154 | |||
1155 | /* get the list of acceptable cert types */ | ||
1156 | p++; | ||
1157 | n = ssl3_get_req_cert_type(s, p); | ||
1158 | d[0] = n; | ||
1159 | p += n; | ||
1160 | n++; | ||
1161 | |||
1162 | off = n; | ||
1163 | p += 2; | ||
1164 | n += 2; | ||
1165 | |||
1166 | sk = SSL_get_client_CA_list(s); | ||
1167 | nl = 0; | ||
1168 | if (sk != NULL) { | ||
1169 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { | ||
1170 | name = sk_X509_NAME_value(sk, i); | ||
1171 | j = i2d_X509_NAME(name, NULL); | ||
1172 | if (!BUF_MEM_grow_clean(buf, DTLS1_HM_HEADER_LENGTH + n + j + 2)) { | ||
1173 | SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB); | ||
1174 | goto err; | ||
1175 | } | ||
1176 | p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + n]); | ||
1177 | if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) { | ||
1178 | s2n(j, p); | ||
1179 | i2d_X509_NAME(name, &p); | ||
1180 | n += 2 + j; | ||
1181 | nl += 2 + j; | ||
1182 | } else { | ||
1183 | d = p; | ||
1184 | i2d_X509_NAME(name, &p); | ||
1185 | j -= 2; | ||
1186 | s2n(j, d); | ||
1187 | j += 2; | ||
1188 | n += j; | ||
1189 | nl += j; | ||
1190 | } | ||
1191 | } | ||
1192 | } | ||
1193 | /* else no CA names */ | ||
1194 | p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH + off]); | ||
1195 | s2n(nl, p); | ||
1196 | |||
1197 | d = (unsigned char *)buf->data; | ||
1198 | *(d++) = SSL3_MT_CERTIFICATE_REQUEST; | ||
1199 | l2n3(n, d); | ||
1200 | s2n(s->d1->handshake_write_seq, d); | ||
1201 | s->d1->handshake_write_seq++; | ||
1202 | |||
1203 | /* we should now have things packed up, so lets send | ||
1204 | * it off */ | ||
1205 | |||
1206 | s->init_num = n + DTLS1_HM_HEADER_LENGTH; | ||
1207 | s->init_off = 0; | ||
1208 | |||
1209 | /* XDTLS: set message header ? */ | ||
1210 | msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH; | ||
1211 | dtls1_set_message_header(s, (void *)s->init_buf->data, | ||
1212 | SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0, msg_len); | ||
1213 | |||
1214 | /* buffer the message to handle re-xmits */ | ||
1215 | dtls1_buffer_message(s, 0); | ||
1216 | |||
1217 | s->state = SSL3_ST_SW_CERT_REQ_B; | ||
1218 | } | ||
1219 | |||
1220 | /* SSL3_ST_SW_CERT_REQ_B */ | ||
1221 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | ||
1222 | err: | ||
1223 | return (-1); | ||
1224 | } | ||
1225 | |||
1226 | int | ||
1227 | dtls1_send_server_certificate(SSL *s) | ||
1228 | { | ||
1229 | unsigned long l; | ||
1230 | X509 *x; | ||
1231 | |||
1232 | if (s->state == SSL3_ST_SW_CERT_A) { | ||
1233 | x = ssl_get_server_send_cert(s); | ||
1234 | if (x == NULL) { | ||
1235 | SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE, | ||
1236 | ERR_R_INTERNAL_ERROR); | ||
1237 | return (0); | ||
1238 | } | ||
1239 | |||
1240 | l = dtls1_output_cert_chain(s, x); | ||
1241 | s->state = SSL3_ST_SW_CERT_B; | ||
1242 | s->init_num = (int)l; | ||
1243 | s->init_off = 0; | ||
1244 | |||
1245 | /* buffer the message to handle re-xmits */ | ||
1246 | dtls1_buffer_message(s, 0); | ||
1247 | } | ||
1248 | |||
1249 | /* SSL3_ST_SW_CERT_B */ | ||
1250 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | ||
1251 | } | ||
1252 | |||
1253 | int | ||
1254 | dtls1_send_newsession_ticket(SSL *s) | ||
1255 | { | ||
1256 | if (s->state == SSL3_ST_SW_SESSION_TICKET_A) { | ||
1257 | unsigned char *p, *senc, *macstart; | ||
1258 | int len, slen; | ||
1259 | unsigned int hlen, msg_len; | ||
1260 | EVP_CIPHER_CTX ctx; | ||
1261 | HMAC_CTX hctx; | ||
1262 | SSL_CTX *tctx = s->initial_ctx; | ||
1263 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
1264 | unsigned char key_name[16]; | ||
1265 | |||
1266 | /* get session encoding length */ | ||
1267 | slen = i2d_SSL_SESSION(s->session, NULL); | ||
1268 | /* Some length values are 16 bits, so forget it if session is | ||
1269 | * too long | ||
1270 | */ | ||
1271 | if (slen > 0xFF00) | ||
1272 | return -1; | ||
1273 | /* Grow buffer if need be: the length calculation is as | ||
1274 | * follows 12 (DTLS handshake message header) + | ||
1275 | * 4 (ticket lifetime hint) + 2 (ticket length) + | ||
1276 | * 16 (key name) + max_iv_len (iv length) + | ||
1277 | * session_length + max_enc_block_size (max encrypted session | ||
1278 | * length) + max_md_size (HMAC). | ||
1279 | */ | ||
1280 | if (!BUF_MEM_grow(s->init_buf, | ||
1281 | DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH + | ||
1282 | EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen)) | ||
1283 | return -1; | ||
1284 | senc = malloc(slen); | ||
1285 | if (!senc) | ||
1286 | return -1; | ||
1287 | p = senc; | ||
1288 | i2d_SSL_SESSION(s->session, &p); | ||
1289 | |||
1290 | p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]); | ||
1291 | EVP_CIPHER_CTX_init(&ctx); | ||
1292 | HMAC_CTX_init(&hctx); | ||
1293 | /* Initialize HMAC and cipher contexts. If callback present | ||
1294 | * it does all the work otherwise use generated values | ||
1295 | * from parent ctx. | ||
1296 | */ | ||
1297 | if (tctx->tlsext_ticket_key_cb) { | ||
1298 | if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, | ||
1299 | &hctx, 1) < 0) { | ||
1300 | free(senc); | ||
1301 | return -1; | ||
1302 | } | ||
1303 | } else { | ||
1304 | arc4random_buf(iv, 16); | ||
1305 | EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, | ||
1306 | tctx->tlsext_tick_aes_key, iv); | ||
1307 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, | ||
1308 | tlsext_tick_md(), NULL); | ||
1309 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); | ||
1310 | } | ||
1311 | l2n(s->session->tlsext_tick_lifetime_hint, p); | ||
1312 | /* Skip ticket length for now */ | ||
1313 | p += 2; | ||
1314 | /* Output key name */ | ||
1315 | macstart = p; | ||
1316 | memcpy(p, key_name, 16); | ||
1317 | p += 16; | ||
1318 | /* output IV */ | ||
1319 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); | ||
1320 | p += EVP_CIPHER_CTX_iv_length(&ctx); | ||
1321 | /* Encrypt session data */ | ||
1322 | EVP_EncryptUpdate(&ctx, p, &len, senc, slen); | ||
1323 | p += len; | ||
1324 | EVP_EncryptFinal(&ctx, p, &len); | ||
1325 | p += len; | ||
1326 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
1327 | |||
1328 | HMAC_Update(&hctx, macstart, p - macstart); | ||
1329 | HMAC_Final(&hctx, p, &hlen); | ||
1330 | HMAC_CTX_cleanup(&hctx); | ||
1331 | |||
1332 | p += hlen; | ||
1333 | /* Now write out lengths: p points to end of data written */ | ||
1334 | /* Total length */ | ||
1335 | len = p - (unsigned char *)(s->init_buf->data); | ||
1336 | /* Ticket length */ | ||
1337 | p = (unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4; | ||
1338 | s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p); | ||
1339 | |||
1340 | /* number of bytes to write */ | ||
1341 | s->init_num = len; | ||
1342 | s->state = SSL3_ST_SW_SESSION_TICKET_B; | ||
1343 | s->init_off = 0; | ||
1344 | free(senc); | ||
1345 | |||
1346 | /* XDTLS: set message header ? */ | ||
1347 | msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH; | ||
1348 | dtls1_set_message_header(s, (void *)s->init_buf->data, | ||
1349 | SSL3_MT_NEWSESSION_TICKET, msg_len, 0, msg_len); | ||
1350 | |||
1351 | /* buffer the message to handle re-xmits */ | ||
1352 | dtls1_buffer_message(s, 0); | ||
1353 | } | ||
1354 | |||
1355 | /* SSL3_ST_SW_SESSION_TICKET_B */ | ||
1356 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | ||
1357 | } | ||