diff options
Diffstat (limited to 'src/lib/libssl/d1_pkt.c')
-rw-r--r-- | src/lib/libssl/d1_pkt.c | 1498 |
1 files changed, 0 insertions, 1498 deletions
diff --git a/src/lib/libssl/d1_pkt.c b/src/lib/libssl/d1_pkt.c deleted file mode 100644 index c997b7f212..0000000000 --- a/src/lib/libssl/d1_pkt.c +++ /dev/null | |||
@@ -1,1498 +0,0 @@ | |||
1 | /* $OpenBSD: d1_pkt.c,v 1.40 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) 1998-2005 The OpenSSL Project. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in | ||
18 | * the documentation and/or other materials provided with the | ||
19 | * distribution. | ||
20 | * | ||
21 | * 3. All advertising materials mentioning features or use of this | ||
22 | * software must display the following acknowledgment: | ||
23 | * "This product includes software developed by the OpenSSL Project | ||
24 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
25 | * | ||
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
27 | * endorse or promote products derived from this software without | ||
28 | * prior written permission. For written permission, please contact | ||
29 | * openssl-core@openssl.org. | ||
30 | * | ||
31 | * 5. Products derived from this software may not be called "OpenSSL" | ||
32 | * nor may "OpenSSL" appear in their names without prior written | ||
33 | * permission of the OpenSSL Project. | ||
34 | * | ||
35 | * 6. Redistributions of any form whatsoever must retain the following | ||
36 | * acknowledgment: | ||
37 | * "This product includes software developed by the OpenSSL Project | ||
38 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
39 | * | ||
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
52 | * ==================================================================== | ||
53 | * | ||
54 | * This product includes cryptographic software written by Eric Young | ||
55 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
56 | * Hudson (tjh@cryptsoft.com). | ||
57 | * | ||
58 | */ | ||
59 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
60 | * All rights reserved. | ||
61 | * | ||
62 | * This package is an SSL implementation written | ||
63 | * by Eric Young (eay@cryptsoft.com). | ||
64 | * The implementation was written so as to conform with Netscapes SSL. | ||
65 | * | ||
66 | * This library is free for commercial and non-commercial use as long as | ||
67 | * the following conditions are aheared to. The following conditions | ||
68 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
69 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
70 | * included with this distribution is covered by the same copyright terms | ||
71 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
72 | * | ||
73 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
74 | * the code are not to be removed. | ||
75 | * If this package is used in a product, Eric Young should be given attribution | ||
76 | * as the author of the parts of the library used. | ||
77 | * This can be in the form of a textual message at program startup or | ||
78 | * in documentation (online or textual) provided with the package. | ||
79 | * | ||
80 | * Redistribution and use in source and binary forms, with or without | ||
81 | * modification, are permitted provided that the following conditions | ||
82 | * are met: | ||
83 | * 1. Redistributions of source code must retain the copyright | ||
84 | * notice, this list of conditions and the following disclaimer. | ||
85 | * 2. Redistributions in binary form must reproduce the above copyright | ||
86 | * notice, this list of conditions and the following disclaimer in the | ||
87 | * documentation and/or other materials provided with the distribution. | ||
88 | * 3. All advertising materials mentioning features or use of this software | ||
89 | * must display the following acknowledgement: | ||
90 | * "This product includes cryptographic software written by | ||
91 | * Eric Young (eay@cryptsoft.com)" | ||
92 | * The word 'cryptographic' can be left out if the rouines from the library | ||
93 | * being used are not cryptographic related :-). | ||
94 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
95 | * the apps directory (application code) you must include an acknowledgement: | ||
96 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
97 | * | ||
98 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
99 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
100 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
101 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
102 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
103 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
104 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
105 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
106 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
107 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
108 | * SUCH DAMAGE. | ||
109 | * | ||
110 | * The licence and distribution terms for any publically available version or | ||
111 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
112 | * copied and put under another distribution licence | ||
113 | * [including the GNU Public Licence.] | ||
114 | */ | ||
115 | |||
116 | #include <machine/endian.h> | ||
117 | |||
118 | #include <errno.h> | ||
119 | #include <stdio.h> | ||
120 | |||
121 | #include "ssl_locl.h" | ||
122 | |||
123 | #include <openssl/buffer.h> | ||
124 | #include <openssl/evp.h> | ||
125 | |||
126 | #include "pqueue.h" | ||
127 | |||
128 | /* mod 128 saturating subtract of two 64-bit values in big-endian order */ | ||
129 | static int | ||
130 | satsub64be(const unsigned char *v1, const unsigned char *v2) | ||
131 | { | ||
132 | int ret, sat, brw, i; | ||
133 | |||
134 | if (sizeof(long) == 8) | ||
135 | do { | ||
136 | long l; | ||
137 | |||
138 | if (BYTE_ORDER == LITTLE_ENDIAN) | ||
139 | break; | ||
140 | /* not reached on little-endians */ | ||
141 | /* following test is redundant, because input is | ||
142 | * always aligned, but I take no chances... */ | ||
143 | if (((size_t)v1 | (size_t)v2) & 0x7) | ||
144 | break; | ||
145 | |||
146 | l = *((long *)v1); | ||
147 | l -= *((long *)v2); | ||
148 | if (l > 128) | ||
149 | return 128; | ||
150 | else if (l<-128) | ||
151 | return -128; | ||
152 | else | ||
153 | return (int)l; | ||
154 | } while (0); | ||
155 | |||
156 | ret = (int)v1[7] - (int)v2[7]; | ||
157 | sat = 0; | ||
158 | brw = ret >> 8; /* brw is either 0 or -1 */ | ||
159 | if (ret & 0x80) { | ||
160 | for (i = 6; i >= 0; i--) { | ||
161 | brw += (int)v1[i]-(int)v2[i]; | ||
162 | sat |= ~brw; | ||
163 | brw >>= 8; | ||
164 | } | ||
165 | } else { | ||
166 | for (i = 6; i >= 0; i--) { | ||
167 | brw += (int)v1[i]-(int)v2[i]; | ||
168 | sat |= brw; | ||
169 | brw >>= 8; | ||
170 | } | ||
171 | } | ||
172 | brw <<= 8; /* brw is either 0 or -256 */ | ||
173 | |||
174 | if (sat & 0xff) | ||
175 | return brw | 0x80; | ||
176 | else | ||
177 | return brw + (ret & 0xFF); | ||
178 | } | ||
179 | |||
180 | static int have_handshake_fragment(SSL *s, int type, unsigned char *buf, | ||
181 | int len, int peek); | ||
182 | static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap); | ||
183 | static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap); | ||
184 | static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, | ||
185 | unsigned int *is_next_epoch); | ||
186 | static int dtls1_buffer_record(SSL *s, record_pqueue *q, | ||
187 | unsigned char *priority); | ||
188 | static int dtls1_process_record(SSL *s); | ||
189 | |||
190 | /* copy buffered record into SSL structure */ | ||
191 | static int | ||
192 | dtls1_copy_record(SSL *s, pitem *item) | ||
193 | { | ||
194 | DTLS1_RECORD_DATA *rdata; | ||
195 | |||
196 | rdata = (DTLS1_RECORD_DATA *)item->data; | ||
197 | |||
198 | free(s->s3->rbuf.buf); | ||
199 | |||
200 | s->packet = rdata->packet; | ||
201 | s->packet_length = rdata->packet_length; | ||
202 | memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER)); | ||
203 | memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD)); | ||
204 | |||
205 | /* Set proper sequence number for mac calculation */ | ||
206 | memcpy(&(s->s3->read_sequence[2]), &(rdata->packet[5]), 6); | ||
207 | |||
208 | return (1); | ||
209 | } | ||
210 | |||
211 | |||
212 | static int | ||
213 | dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority) | ||
214 | { | ||
215 | DTLS1_RECORD_DATA *rdata; | ||
216 | pitem *item; | ||
217 | |||
218 | /* Limit the size of the queue to prevent DOS attacks */ | ||
219 | if (pqueue_size(queue->q) >= 100) | ||
220 | return 0; | ||
221 | |||
222 | rdata = malloc(sizeof(DTLS1_RECORD_DATA)); | ||
223 | item = pitem_new(priority, rdata); | ||
224 | if (rdata == NULL || item == NULL) | ||
225 | goto init_err; | ||
226 | |||
227 | rdata->packet = s->packet; | ||
228 | rdata->packet_length = s->packet_length; | ||
229 | memcpy(&(rdata->rbuf), &(s->s3->rbuf), sizeof(SSL3_BUFFER)); | ||
230 | memcpy(&(rdata->rrec), &(s->s3->rrec), sizeof(SSL3_RECORD)); | ||
231 | |||
232 | item->data = rdata; | ||
233 | |||
234 | |||
235 | s->packet = NULL; | ||
236 | s->packet_length = 0; | ||
237 | memset(&(s->s3->rbuf), 0, sizeof(SSL3_BUFFER)); | ||
238 | memset(&(s->s3->rrec), 0, sizeof(SSL3_RECORD)); | ||
239 | |||
240 | if (!ssl3_setup_buffers(s)) | ||
241 | goto err; | ||
242 | |||
243 | /* insert should not fail, since duplicates are dropped */ | ||
244 | if (pqueue_insert(queue->q, item) == NULL) | ||
245 | goto err; | ||
246 | |||
247 | return (1); | ||
248 | |||
249 | err: | ||
250 | free(rdata->rbuf.buf); | ||
251 | |||
252 | init_err: | ||
253 | SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR); | ||
254 | free(rdata); | ||
255 | pitem_free(item); | ||
256 | return (-1); | ||
257 | } | ||
258 | |||
259 | |||
260 | static int | ||
261 | dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue) | ||
262 | { | ||
263 | pitem *item; | ||
264 | |||
265 | item = pqueue_pop(queue->q); | ||
266 | if (item) { | ||
267 | dtls1_copy_record(s, item); | ||
268 | |||
269 | free(item->data); | ||
270 | pitem_free(item); | ||
271 | |||
272 | return (1); | ||
273 | } | ||
274 | |||
275 | return (0); | ||
276 | } | ||
277 | |||
278 | |||
279 | /* retrieve a buffered record that belongs to the new epoch, i.e., not processed | ||
280 | * yet */ | ||
281 | #define dtls1_get_unprocessed_record(s) \ | ||
282 | dtls1_retrieve_buffered_record((s), \ | ||
283 | &((s)->d1->unprocessed_rcds)) | ||
284 | |||
285 | /* retrieve a buffered record that belongs to the current epoch, ie, processed */ | ||
286 | #define dtls1_get_processed_record(s) \ | ||
287 | dtls1_retrieve_buffered_record((s), \ | ||
288 | &((s)->d1->processed_rcds)) | ||
289 | |||
290 | static int | ||
291 | dtls1_process_buffered_records(SSL *s) | ||
292 | { | ||
293 | pitem *item; | ||
294 | |||
295 | item = pqueue_peek(s->d1->unprocessed_rcds.q); | ||
296 | if (item) { | ||
297 | /* Check if epoch is current. */ | ||
298 | if (s->d1->unprocessed_rcds.epoch != s->d1->r_epoch) | ||
299 | return (1); | ||
300 | /* Nothing to do. */ | ||
301 | |||
302 | /* Process all the records. */ | ||
303 | while (pqueue_peek(s->d1->unprocessed_rcds.q)) { | ||
304 | dtls1_get_unprocessed_record(s); | ||
305 | if (! dtls1_process_record(s)) | ||
306 | return (0); | ||
307 | if (dtls1_buffer_record(s, &(s->d1->processed_rcds), | ||
308 | s->s3->rrec.seq_num) < 0) | ||
309 | return (-1); | ||
310 | } | ||
311 | } | ||
312 | |||
313 | /* sync epoch numbers once all the unprocessed records | ||
314 | * have been processed */ | ||
315 | s->d1->processed_rcds.epoch = s->d1->r_epoch; | ||
316 | s->d1->unprocessed_rcds.epoch = s->d1->r_epoch + 1; | ||
317 | |||
318 | return (1); | ||
319 | } | ||
320 | |||
321 | static int | ||
322 | dtls1_process_record(SSL *s) | ||
323 | { | ||
324 | int i, al; | ||
325 | int enc_err; | ||
326 | SSL_SESSION *sess; | ||
327 | SSL3_RECORD *rr; | ||
328 | unsigned int mac_size, orig_len; | ||
329 | unsigned char md[EVP_MAX_MD_SIZE]; | ||
330 | |||
331 | rr = &(s->s3->rrec); | ||
332 | sess = s->session; | ||
333 | |||
334 | /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, | ||
335 | * and we have that many bytes in s->packet | ||
336 | */ | ||
337 | rr->input = &(s->packet[DTLS1_RT_HEADER_LENGTH]); | ||
338 | |||
339 | /* ok, we can now read from 's->packet' data into 'rr' | ||
340 | * rr->input points at rr->length bytes, which | ||
341 | * need to be copied into rr->data by either | ||
342 | * the decryption or by the decompression | ||
343 | * When the data is 'copied' into the rr->data buffer, | ||
344 | * rr->input will be pointed at the new buffer */ | ||
345 | |||
346 | /* We now have - encrypted [ MAC [ compressed [ plain ] ] ] | ||
347 | * rr->length bytes of encrypted compressed stuff. */ | ||
348 | |||
349 | /* check is not needed I believe */ | ||
350 | if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) { | ||
351 | al = SSL_AD_RECORD_OVERFLOW; | ||
352 | SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG); | ||
353 | goto f_err; | ||
354 | } | ||
355 | |||
356 | /* decrypt in place in 'rr->input' */ | ||
357 | rr->data = rr->input; | ||
358 | |||
359 | enc_err = s->method->ssl3_enc->enc(s, 0); | ||
360 | /* enc_err is: | ||
361 | * 0: (in non-constant time) if the record is publically invalid. | ||
362 | * 1: if the padding is valid | ||
363 | * -1: if the padding is invalid */ | ||
364 | if (enc_err == 0) { | ||
365 | /* For DTLS we simply ignore bad packets. */ | ||
366 | rr->length = 0; | ||
367 | s->packet_length = 0; | ||
368 | goto err; | ||
369 | } | ||
370 | |||
371 | |||
372 | /* r->length is now the compressed data plus mac */ | ||
373 | if ((sess != NULL) && (s->enc_read_ctx != NULL) && | ||
374 | (EVP_MD_CTX_md(s->read_hash) != NULL)) { | ||
375 | /* s->read_hash != NULL => mac_size != -1 */ | ||
376 | unsigned char *mac = NULL; | ||
377 | unsigned char mac_tmp[EVP_MAX_MD_SIZE]; | ||
378 | mac_size = EVP_MD_CTX_size(s->read_hash); | ||
379 | OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE); | ||
380 | |||
381 | /* kludge: *_cbc_remove_padding passes padding length in rr->type */ | ||
382 | orig_len = rr->length + ((unsigned int)rr->type >> 8); | ||
383 | |||
384 | /* orig_len is the length of the record before any padding was | ||
385 | * removed. This is public information, as is the MAC in use, | ||
386 | * therefore we can safely process the record in a different | ||
387 | * amount of time if it's too short to possibly contain a MAC. | ||
388 | */ | ||
389 | if (orig_len < mac_size || | ||
390 | /* CBC records must have a padding length byte too. */ | ||
391 | (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE && | ||
392 | orig_len < mac_size + 1)) { | ||
393 | al = SSL_AD_DECODE_ERROR; | ||
394 | SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT); | ||
395 | goto f_err; | ||
396 | } | ||
397 | |||
398 | if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) { | ||
399 | /* We update the length so that the TLS header bytes | ||
400 | * can be constructed correctly but we need to extract | ||
401 | * the MAC in constant time from within the record, | ||
402 | * without leaking the contents of the padding bytes. | ||
403 | * */ | ||
404 | mac = mac_tmp; | ||
405 | ssl3_cbc_copy_mac(mac_tmp, rr, mac_size, orig_len); | ||
406 | rr->length -= mac_size; | ||
407 | } else { | ||
408 | /* In this case there's no padding, so |orig_len| | ||
409 | * equals |rec->length| and we checked that there's | ||
410 | * enough bytes for |mac_size| above. */ | ||
411 | rr->length -= mac_size; | ||
412 | mac = &rr->data[rr->length]; | ||
413 | } | ||
414 | |||
415 | i = s->method->ssl3_enc->mac(s, md, 0 /* not send */); | ||
416 | if (i < 0 || mac == NULL || timingsafe_memcmp(md, mac, (size_t)mac_size) != 0) | ||
417 | enc_err = -1; | ||
418 | if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size) | ||
419 | enc_err = -1; | ||
420 | } | ||
421 | |||
422 | if (enc_err < 0) { | ||
423 | /* decryption failed, silently discard message */ | ||
424 | rr->length = 0; | ||
425 | s->packet_length = 0; | ||
426 | goto err; | ||
427 | } | ||
428 | |||
429 | if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) { | ||
430 | al = SSL_AD_RECORD_OVERFLOW; | ||
431 | SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG); | ||
432 | goto f_err; | ||
433 | } | ||
434 | |||
435 | rr->off = 0; | ||
436 | /* So at this point the following is true | ||
437 | * ssl->s3->rrec.type is the type of record | ||
438 | * ssl->s3->rrec.length == number of bytes in record | ||
439 | * ssl->s3->rrec.off == offset to first valid byte | ||
440 | * ssl->s3->rrec.data == where to take bytes from, increment | ||
441 | * after use :-). | ||
442 | */ | ||
443 | |||
444 | /* we have pulled in a full packet so zero things */ | ||
445 | s->packet_length = 0; | ||
446 | return (1); | ||
447 | |||
448 | f_err: | ||
449 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
450 | err: | ||
451 | return (0); | ||
452 | } | ||
453 | |||
454 | |||
455 | /* Call this to get a new input record. | ||
456 | * It will return <= 0 if more data is needed, normally due to an error | ||
457 | * or non-blocking IO. | ||
458 | * When it finishes, one packet has been decoded and can be found in | ||
459 | * ssl->s3->rrec.type - is the type of record | ||
460 | * ssl->s3->rrec.data, - data | ||
461 | * ssl->s3->rrec.length, - number of bytes | ||
462 | */ | ||
463 | /* used only by dtls1_read_bytes */ | ||
464 | int | ||
465 | dtls1_get_record(SSL *s) | ||
466 | { | ||
467 | int ssl_major, ssl_minor; | ||
468 | int i, n; | ||
469 | SSL3_RECORD *rr; | ||
470 | unsigned char *p = NULL; | ||
471 | unsigned short version; | ||
472 | DTLS1_BITMAP *bitmap; | ||
473 | unsigned int is_next_epoch; | ||
474 | |||
475 | rr = &(s->s3->rrec); | ||
476 | |||
477 | /* The epoch may have changed. If so, process all the | ||
478 | * pending records. This is a non-blocking operation. */ | ||
479 | if (dtls1_process_buffered_records(s) < 0) | ||
480 | return (-1); | ||
481 | |||
482 | /* if we're renegotiating, then there may be buffered records */ | ||
483 | if (dtls1_get_processed_record(s)) | ||
484 | return 1; | ||
485 | |||
486 | /* get something from the wire */ | ||
487 | again: | ||
488 | /* check if we have the header */ | ||
489 | if ((s->rstate != SSL_ST_READ_BODY) || | ||
490 | (s->packet_length < DTLS1_RT_HEADER_LENGTH)) { | ||
491 | n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0); | ||
492 | /* read timeout is handled by dtls1_read_bytes */ | ||
493 | if (n <= 0) | ||
494 | return(n); /* error or non-blocking */ | ||
495 | |||
496 | /* this packet contained a partial record, dump it */ | ||
497 | if (s->packet_length != DTLS1_RT_HEADER_LENGTH) { | ||
498 | s->packet_length = 0; | ||
499 | goto again; | ||
500 | } | ||
501 | |||
502 | s->rstate = SSL_ST_READ_BODY; | ||
503 | |||
504 | p = s->packet; | ||
505 | |||
506 | /* Pull apart the header into the DTLS1_RECORD */ | ||
507 | rr->type= *(p++); | ||
508 | ssl_major= *(p++); | ||
509 | ssl_minor= *(p++); | ||
510 | version = (ssl_major << 8)|ssl_minor; | ||
511 | |||
512 | /* sequence number is 64 bits, with top 2 bytes = epoch */ | ||
513 | n2s(p, rr->epoch); | ||
514 | |||
515 | memcpy(&(s->s3->read_sequence[2]), p, 6); | ||
516 | p += 6; | ||
517 | |||
518 | n2s(p, rr->length); | ||
519 | |||
520 | /* Lets check version */ | ||
521 | if (!s->first_packet) { | ||
522 | if (version != s->version) { | ||
523 | /* unexpected version, silently discard */ | ||
524 | rr->length = 0; | ||
525 | s->packet_length = 0; | ||
526 | goto again; | ||
527 | } | ||
528 | } | ||
529 | |||
530 | if ((version & 0xff00) != (s->version & 0xff00)) { | ||
531 | /* wrong version, silently discard record */ | ||
532 | rr->length = 0; | ||
533 | s->packet_length = 0; | ||
534 | goto again; | ||
535 | } | ||
536 | |||
537 | if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) { | ||
538 | /* record too long, silently discard it */ | ||
539 | rr->length = 0; | ||
540 | s->packet_length = 0; | ||
541 | goto again; | ||
542 | } | ||
543 | |||
544 | /* now s->rstate == SSL_ST_READ_BODY */ | ||
545 | } | ||
546 | |||
547 | /* s->rstate == SSL_ST_READ_BODY, get and decode the data */ | ||
548 | |||
549 | if (rr->length > s->packet_length - DTLS1_RT_HEADER_LENGTH) { | ||
550 | /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */ | ||
551 | i = rr->length; | ||
552 | n = ssl3_read_n(s, i, i, 1); | ||
553 | if (n <= 0) | ||
554 | return(n); /* error or non-blocking io */ | ||
555 | |||
556 | /* this packet contained a partial record, dump it */ | ||
557 | if (n != i) { | ||
558 | rr->length = 0; | ||
559 | s->packet_length = 0; | ||
560 | goto again; | ||
561 | } | ||
562 | |||
563 | /* now n == rr->length, | ||
564 | * and s->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length */ | ||
565 | } | ||
566 | s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */ | ||
567 | |||
568 | /* match epochs. NULL means the packet is dropped on the floor */ | ||
569 | bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch); | ||
570 | if (bitmap == NULL) { | ||
571 | rr->length = 0; | ||
572 | s->packet_length = 0; | ||
573 | /* dump this record */ | ||
574 | goto again; | ||
575 | /* get another record */ | ||
576 | } | ||
577 | |||
578 | /* Check whether this is a repeat, or aged record. | ||
579 | * Don't check if we're listening and this message is | ||
580 | * a ClientHello. They can look as if they're replayed, | ||
581 | * since they arrive from different connections and | ||
582 | * would be dropped unnecessarily. | ||
583 | */ | ||
584 | if (!(s->d1->listen && rr->type == SSL3_RT_HANDSHAKE && | ||
585 | p != NULL && *p == SSL3_MT_CLIENT_HELLO) && | ||
586 | !dtls1_record_replay_check(s, bitmap)) { | ||
587 | rr->length = 0; | ||
588 | s->packet_length=0; /* dump this record */ | ||
589 | goto again; | ||
590 | /* get another record */ | ||
591 | } | ||
592 | |||
593 | /* just read a 0 length packet */ | ||
594 | if (rr->length == 0) | ||
595 | goto again; | ||
596 | |||
597 | /* If this record is from the next epoch (either HM or ALERT), | ||
598 | * and a handshake is currently in progress, buffer it since it | ||
599 | * cannot be processed at this time. However, do not buffer | ||
600 | * anything while listening. | ||
601 | */ | ||
602 | if (is_next_epoch) { | ||
603 | if ((SSL_in_init(s) || s->in_handshake) && !s->d1->listen) { | ||
604 | if (dtls1_buffer_record(s, &(s->d1->unprocessed_rcds), | ||
605 | rr->seq_num) < 0) | ||
606 | return (-1); | ||
607 | /* Mark receipt of record. */ | ||
608 | dtls1_record_bitmap_update(s, bitmap); | ||
609 | } | ||
610 | rr->length = 0; | ||
611 | s->packet_length = 0; | ||
612 | goto again; | ||
613 | } | ||
614 | |||
615 | if (!dtls1_process_record(s)) { | ||
616 | rr->length = 0; | ||
617 | s->packet_length = 0; | ||
618 | /* dump this record */ | ||
619 | goto again; | ||
620 | /* get another record */ | ||
621 | } | ||
622 | /* Mark receipt of record. */ | ||
623 | dtls1_record_bitmap_update(s, bitmap); | ||
624 | |||
625 | return (1); | ||
626 | |||
627 | } | ||
628 | |||
629 | /* Return up to 'len' payload bytes received in 'type' records. | ||
630 | * 'type' is one of the following: | ||
631 | * | ||
632 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) | ||
633 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) | ||
634 | * - 0 (during a shutdown, no data has to be returned) | ||
635 | * | ||
636 | * If we don't have stored data to work from, read a SSL/TLS record first | ||
637 | * (possibly multiple records if we still don't have anything to return). | ||
638 | * | ||
639 | * This function must handle any surprises the peer may have for us, such as | ||
640 | * Alert records (e.g. close_notify), ChangeCipherSpec records (not really | ||
641 | * a surprise, but handled as if it were), or renegotiation requests. | ||
642 | * Also if record payloads contain fragments too small to process, we store | ||
643 | * them until there is enough for the respective protocol (the record protocol | ||
644 | * may use arbitrary fragmentation and even interleaving): | ||
645 | * Change cipher spec protocol | ||
646 | * just 1 byte needed, no need for keeping anything stored | ||
647 | * Alert protocol | ||
648 | * 2 bytes needed (AlertLevel, AlertDescription) | ||
649 | * Handshake protocol | ||
650 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have | ||
651 | * to detect unexpected Client Hello and Hello Request messages | ||
652 | * here, anything else is handled by higher layers | ||
653 | * Application data protocol | ||
654 | * none of our business | ||
655 | */ | ||
656 | int | ||
657 | dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) | ||
658 | { | ||
659 | int al, i, j, ret; | ||
660 | unsigned int n; | ||
661 | SSL3_RECORD *rr; | ||
662 | void (*cb)(const SSL *ssl, int type2, int val) = NULL; | ||
663 | |||
664 | if (s->s3->rbuf.buf == NULL) /* Not initialized yet */ | ||
665 | if (!ssl3_setup_buffers(s)) | ||
666 | return (-1); | ||
667 | |||
668 | if ((type && | ||
669 | type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) || | ||
670 | (peek && (type != SSL3_RT_APPLICATION_DATA))) { | ||
671 | SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR); | ||
672 | return -1; | ||
673 | } | ||
674 | |||
675 | /* check whether there's a handshake message (client hello?) waiting */ | ||
676 | if ((ret = have_handshake_fragment(s, type, buf, len, peek))) | ||
677 | return ret; | ||
678 | |||
679 | /* Now s->d1->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */ | ||
680 | |||
681 | if (!s->in_handshake && SSL_in_init(s)) | ||
682 | { | ||
683 | /* type == SSL3_RT_APPLICATION_DATA */ | ||
684 | i = s->handshake_func(s); | ||
685 | if (i < 0) | ||
686 | return (i); | ||
687 | if (i == 0) { | ||
688 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE); | ||
689 | return (-1); | ||
690 | } | ||
691 | } | ||
692 | |||
693 | start: | ||
694 | s->rwstate = SSL_NOTHING; | ||
695 | |||
696 | /* s->s3->rrec.type - is the type of record | ||
697 | * s->s3->rrec.data, - data | ||
698 | * s->s3->rrec.off, - offset into 'data' for next read | ||
699 | * s->s3->rrec.length, - number of bytes. */ | ||
700 | rr = &(s->s3->rrec); | ||
701 | |||
702 | /* We are not handshaking and have no data yet, | ||
703 | * so process data buffered during the last handshake | ||
704 | * in advance, if any. | ||
705 | */ | ||
706 | if (s->state == SSL_ST_OK && rr->length == 0) { | ||
707 | pitem *item; | ||
708 | item = pqueue_pop(s->d1->buffered_app_data.q); | ||
709 | if (item) { | ||
710 | |||
711 | dtls1_copy_record(s, item); | ||
712 | |||
713 | free(item->data); | ||
714 | pitem_free(item); | ||
715 | } | ||
716 | } | ||
717 | |||
718 | /* Check for timeout */ | ||
719 | if (dtls1_handle_timeout(s) > 0) | ||
720 | goto start; | ||
721 | |||
722 | /* get new packet if necessary */ | ||
723 | if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) { | ||
724 | ret = dtls1_get_record(s); | ||
725 | if (ret <= 0) { | ||
726 | ret = dtls1_read_failed(s, ret); | ||
727 | /* anything other than a timeout is an error */ | ||
728 | if (ret <= 0) | ||
729 | return (ret); | ||
730 | else | ||
731 | goto start; | ||
732 | } | ||
733 | } | ||
734 | |||
735 | if (s->d1->listen && rr->type != SSL3_RT_HANDSHAKE) { | ||
736 | rr->length = 0; | ||
737 | goto start; | ||
738 | } | ||
739 | |||
740 | /* we now have a packet which can be read and processed */ | ||
741 | |||
742 | if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec, | ||
743 | * reset by ssl3_get_finished */ | ||
744 | && (rr->type != SSL3_RT_HANDSHAKE)) { | ||
745 | /* We now have application data between CCS and Finished. | ||
746 | * Most likely the packets were reordered on their way, so | ||
747 | * buffer the application data for later processing rather | ||
748 | * than dropping the connection. | ||
749 | */ | ||
750 | if (dtls1_buffer_record(s, &(s->d1->buffered_app_data), | ||
751 | rr->seq_num) < 0) { | ||
752 | SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR); | ||
753 | return (-1); | ||
754 | } | ||
755 | rr->length = 0; | ||
756 | goto start; | ||
757 | } | ||
758 | |||
759 | /* If the other end has shut down, throw anything we read away | ||
760 | * (even in 'peek' mode) */ | ||
761 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { | ||
762 | rr->length = 0; | ||
763 | s->rwstate = SSL_NOTHING; | ||
764 | return (0); | ||
765 | } | ||
766 | |||
767 | |||
768 | if (type == rr->type) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ | ||
769 | { | ||
770 | /* make sure that we are not getting application data when we | ||
771 | * are doing a handshake for the first time */ | ||
772 | if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && | ||
773 | (s->enc_read_ctx == NULL)) { | ||
774 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
775 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE); | ||
776 | goto f_err; | ||
777 | } | ||
778 | |||
779 | if (len <= 0) | ||
780 | return (len); | ||
781 | |||
782 | if ((unsigned int)len > rr->length) | ||
783 | n = rr->length; | ||
784 | else | ||
785 | n = (unsigned int)len; | ||
786 | |||
787 | memcpy(buf, &(rr->data[rr->off]), n); | ||
788 | if (!peek) { | ||
789 | rr->length -= n; | ||
790 | rr->off += n; | ||
791 | if (rr->length == 0) { | ||
792 | s->rstate = SSL_ST_READ_HEADER; | ||
793 | rr->off = 0; | ||
794 | } | ||
795 | } | ||
796 | |||
797 | return (n); | ||
798 | } | ||
799 | |||
800 | |||
801 | /* If we get here, then type != rr->type; if we have a handshake | ||
802 | * message, then it was unexpected (Hello Request or Client Hello). */ | ||
803 | |||
804 | /* In case of record types for which we have 'fragment' storage, | ||
805 | * fill that so that we can process the data at a fixed place. | ||
806 | */ | ||
807 | { | ||
808 | unsigned int k, dest_maxlen = 0; | ||
809 | unsigned char *dest = NULL; | ||
810 | unsigned int *dest_len = NULL; | ||
811 | |||
812 | if (rr->type == SSL3_RT_HANDSHAKE) { | ||
813 | dest_maxlen = sizeof s->d1->handshake_fragment; | ||
814 | dest = s->d1->handshake_fragment; | ||
815 | dest_len = &s->d1->handshake_fragment_len; | ||
816 | } else if (rr->type == SSL3_RT_ALERT) { | ||
817 | dest_maxlen = sizeof(s->d1->alert_fragment); | ||
818 | dest = s->d1->alert_fragment; | ||
819 | dest_len = &s->d1->alert_fragment_len; | ||
820 | } | ||
821 | /* else it's a CCS message, or application data or wrong */ | ||
822 | else if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) { | ||
823 | /* Application data while renegotiating | ||
824 | * is allowed. Try again reading. | ||
825 | */ | ||
826 | if (rr->type == SSL3_RT_APPLICATION_DATA) { | ||
827 | BIO *bio; | ||
828 | s->s3->in_read_app_data = 2; | ||
829 | bio = SSL_get_rbio(s); | ||
830 | s->rwstate = SSL_READING; | ||
831 | BIO_clear_retry_flags(bio); | ||
832 | BIO_set_retry_read(bio); | ||
833 | return (-1); | ||
834 | } | ||
835 | |||
836 | /* Not certain if this is the right error handling */ | ||
837 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
838 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD); | ||
839 | goto f_err; | ||
840 | } | ||
841 | |||
842 | if (dest_maxlen > 0) { | ||
843 | /* XDTLS: In a pathalogical case, the Client Hello | ||
844 | * may be fragmented--don't always expect dest_maxlen bytes */ | ||
845 | if (rr->length < dest_maxlen) { | ||
846 | #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE | ||
847 | /* | ||
848 | * for normal alerts rr->length is 2, while | ||
849 | * dest_maxlen is 7 if we were to handle this | ||
850 | * non-existing alert... | ||
851 | */ | ||
852 | FIX ME | ||
853 | #endif | ||
854 | s->rstate = SSL_ST_READ_HEADER; | ||
855 | rr->length = 0; | ||
856 | goto start; | ||
857 | } | ||
858 | |||
859 | /* now move 'n' bytes: */ | ||
860 | for ( k = 0; k < dest_maxlen; k++) { | ||
861 | dest[k] = rr->data[rr->off++]; | ||
862 | rr->length--; | ||
863 | } | ||
864 | *dest_len = dest_maxlen; | ||
865 | } | ||
866 | } | ||
867 | |||
868 | /* s->d1->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE; | ||
869 | * s->d1->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT. | ||
870 | * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */ | ||
871 | |||
872 | /* If we are a client, check for an incoming 'Hello Request': */ | ||
873 | if ((!s->server) && | ||
874 | (s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) && | ||
875 | (s->d1->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) && | ||
876 | (s->session != NULL) && (s->session->cipher != NULL)) { | ||
877 | s->d1->handshake_fragment_len = 0; | ||
878 | |||
879 | if ((s->d1->handshake_fragment[1] != 0) || | ||
880 | (s->d1->handshake_fragment[2] != 0) || | ||
881 | (s->d1->handshake_fragment[3] != 0)) { | ||
882 | al = SSL_AD_DECODE_ERROR; | ||
883 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST); | ||
884 | goto err; | ||
885 | } | ||
886 | |||
887 | /* no need to check sequence number on HELLO REQUEST messages */ | ||
888 | |||
889 | if (s->msg_callback) | ||
890 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, | ||
891 | s->d1->handshake_fragment, 4, s, s->msg_callback_arg); | ||
892 | |||
893 | if (SSL_is_init_finished(s) && | ||
894 | !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) && | ||
895 | !s->s3->renegotiate) { | ||
896 | s->d1->handshake_read_seq++; | ||
897 | s->new_session = 1; | ||
898 | ssl3_renegotiate(s); | ||
899 | if (ssl3_renegotiate_check(s)) { | ||
900 | i = s->handshake_func(s); | ||
901 | if (i < 0) | ||
902 | return (i); | ||
903 | if (i == 0) { | ||
904 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE); | ||
905 | return (-1); | ||
906 | } | ||
907 | |||
908 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) { | ||
909 | if (s->s3->rbuf.left == 0) /* no read-ahead left? */ | ||
910 | { | ||
911 | BIO *bio; | ||
912 | /* In the case where we try to read application data, | ||
913 | * but we trigger an SSL handshake, we return -1 with | ||
914 | * the retry option set. Otherwise renegotiation may | ||
915 | * cause nasty problems in the blocking world */ | ||
916 | s->rwstate = SSL_READING; | ||
917 | bio = SSL_get_rbio(s); | ||
918 | BIO_clear_retry_flags(bio); | ||
919 | BIO_set_retry_read(bio); | ||
920 | return (-1); | ||
921 | } | ||
922 | } | ||
923 | } | ||
924 | } | ||
925 | /* we either finished a handshake or ignored the request, | ||
926 | * now try again to obtain the (application) data we were asked for */ | ||
927 | goto start; | ||
928 | } | ||
929 | |||
930 | if (s->d1->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) { | ||
931 | int alert_level = s->d1->alert_fragment[0]; | ||
932 | int alert_descr = s->d1->alert_fragment[1]; | ||
933 | |||
934 | s->d1->alert_fragment_len = 0; | ||
935 | |||
936 | if (s->msg_callback) | ||
937 | s->msg_callback(0, s->version, SSL3_RT_ALERT, | ||
938 | s->d1->alert_fragment, 2, s, s->msg_callback_arg); | ||
939 | |||
940 | if (s->info_callback != NULL) | ||
941 | cb = s->info_callback; | ||
942 | else if (s->ctx->info_callback != NULL) | ||
943 | cb = s->ctx->info_callback; | ||
944 | |||
945 | if (cb != NULL) { | ||
946 | j = (alert_level << 8) | alert_descr; | ||
947 | cb(s, SSL_CB_READ_ALERT, j); | ||
948 | } | ||
949 | |||
950 | if (alert_level == 1) /* warning */ | ||
951 | { | ||
952 | s->s3->warn_alert = alert_descr; | ||
953 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { | ||
954 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; | ||
955 | return (0); | ||
956 | } | ||
957 | } else if (alert_level == 2) /* fatal */ | ||
958 | { | ||
959 | s->rwstate = SSL_NOTHING; | ||
960 | s->s3->fatal_alert = alert_descr; | ||
961 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr); | ||
962 | ERR_asprintf_error_data("SSL alert number %d", | ||
963 | alert_descr); | ||
964 | s->shutdown|=SSL_RECEIVED_SHUTDOWN; | ||
965 | SSL_CTX_remove_session(s->ctx, s->session); | ||
966 | return (0); | ||
967 | } else { | ||
968 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
969 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE); | ||
970 | goto f_err; | ||
971 | } | ||
972 | |||
973 | goto start; | ||
974 | } | ||
975 | |||
976 | if (s->shutdown & SSL_SENT_SHUTDOWN) /* but we have not received a shutdown */ | ||
977 | { | ||
978 | s->rwstate = SSL_NOTHING; | ||
979 | rr->length = 0; | ||
980 | return (0); | ||
981 | } | ||
982 | |||
983 | if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) { | ||
984 | struct ccs_header_st ccs_hdr; | ||
985 | unsigned int ccs_hdr_len = DTLS1_CCS_HEADER_LENGTH; | ||
986 | |||
987 | dtls1_get_ccs_header(rr->data, &ccs_hdr); | ||
988 | |||
989 | if (s->version == DTLS1_BAD_VER) | ||
990 | ccs_hdr_len = 3; | ||
991 | |||
992 | /* 'Change Cipher Spec' is just a single byte, so we know | ||
993 | * exactly what the record payload has to look like */ | ||
994 | /* XDTLS: check that epoch is consistent */ | ||
995 | if ((rr->length != ccs_hdr_len) || | ||
996 | (rr->off != 0) || (rr->data[0] != SSL3_MT_CCS)) { | ||
997 | i = SSL_AD_ILLEGAL_PARAMETER; | ||
998 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_CHANGE_CIPHER_SPEC); | ||
999 | goto err; | ||
1000 | } | ||
1001 | |||
1002 | rr->length = 0; | ||
1003 | |||
1004 | if (s->msg_callback) | ||
1005 | s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, | ||
1006 | rr->data, 1, s, s->msg_callback_arg); | ||
1007 | |||
1008 | /* We can't process a CCS now, because previous handshake | ||
1009 | * messages are still missing, so just drop it. | ||
1010 | */ | ||
1011 | if (!s->d1->change_cipher_spec_ok) { | ||
1012 | goto start; | ||
1013 | } | ||
1014 | |||
1015 | s->d1->change_cipher_spec_ok = 0; | ||
1016 | |||
1017 | s->s3->change_cipher_spec = 1; | ||
1018 | if (!ssl3_do_change_cipher_spec(s)) | ||
1019 | goto err; | ||
1020 | |||
1021 | /* do this whenever CCS is processed */ | ||
1022 | dtls1_reset_seq_numbers(s, SSL3_CC_READ); | ||
1023 | |||
1024 | if (s->version == DTLS1_BAD_VER) | ||
1025 | s->d1->handshake_read_seq++; | ||
1026 | |||
1027 | |||
1028 | goto start; | ||
1029 | } | ||
1030 | |||
1031 | /* Unexpected handshake message (Client Hello, or protocol violation) */ | ||
1032 | if ((s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) && | ||
1033 | !s->in_handshake) { | ||
1034 | struct hm_header_st msg_hdr; | ||
1035 | |||
1036 | /* this may just be a stale retransmit */ | ||
1037 | dtls1_get_message_header(rr->data, &msg_hdr); | ||
1038 | if (rr->epoch != s->d1->r_epoch) { | ||
1039 | rr->length = 0; | ||
1040 | goto start; | ||
1041 | } | ||
1042 | |||
1043 | /* If we are server, we may have a repeated FINISHED of the | ||
1044 | * client here, then retransmit our CCS and FINISHED. | ||
1045 | */ | ||
1046 | if (msg_hdr.type == SSL3_MT_FINISHED) { | ||
1047 | if (dtls1_check_timeout_num(s) < 0) | ||
1048 | return -1; | ||
1049 | |||
1050 | dtls1_retransmit_buffered_messages(s); | ||
1051 | rr->length = 0; | ||
1052 | goto start; | ||
1053 | } | ||
1054 | |||
1055 | if (((s->state&SSL_ST_MASK) == SSL_ST_OK) && | ||
1056 | !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) { | ||
1057 | s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT; | ||
1058 | s->renegotiate = 1; | ||
1059 | s->new_session = 1; | ||
1060 | } | ||
1061 | i = s->handshake_func(s); | ||
1062 | if (i < 0) | ||
1063 | return (i); | ||
1064 | if (i == 0) { | ||
1065 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE); | ||
1066 | return (-1); | ||
1067 | } | ||
1068 | |||
1069 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) { | ||
1070 | if (s->s3->rbuf.left == 0) /* no read-ahead left? */ | ||
1071 | { | ||
1072 | BIO *bio; | ||
1073 | /* In the case where we try to read application data, | ||
1074 | * but we trigger an SSL handshake, we return -1 with | ||
1075 | * the retry option set. Otherwise renegotiation may | ||
1076 | * cause nasty problems in the blocking world */ | ||
1077 | s->rwstate = SSL_READING; | ||
1078 | bio = SSL_get_rbio(s); | ||
1079 | BIO_clear_retry_flags(bio); | ||
1080 | BIO_set_retry_read(bio); | ||
1081 | return (-1); | ||
1082 | } | ||
1083 | } | ||
1084 | goto start; | ||
1085 | } | ||
1086 | |||
1087 | switch (rr->type) { | ||
1088 | default: | ||
1089 | /* TLS just ignores unknown message types */ | ||
1090 | if (s->version == TLS1_VERSION) { | ||
1091 | rr->length = 0; | ||
1092 | goto start; | ||
1093 | } | ||
1094 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1095 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD); | ||
1096 | goto f_err; | ||
1097 | case SSL3_RT_CHANGE_CIPHER_SPEC: | ||
1098 | case SSL3_RT_ALERT: | ||
1099 | case SSL3_RT_HANDSHAKE: | ||
1100 | /* we already handled all of these, with the possible exception | ||
1101 | * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that | ||
1102 | * should not happen when type != rr->type */ | ||
1103 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1104 | SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR); | ||
1105 | goto f_err; | ||
1106 | case SSL3_RT_APPLICATION_DATA: | ||
1107 | /* At this point, we were expecting handshake data, | ||
1108 | * but have application data. If the library was | ||
1109 | * running inside ssl3_read() (i.e. in_read_app_data | ||
1110 | * is set) and it makes sense to read application data | ||
1111 | * at this point (session renegotiation not yet started), | ||
1112 | * we will indulge it. | ||
1113 | */ | ||
1114 | if (s->s3->in_read_app_data && | ||
1115 | (s->s3->total_renegotiations != 0) && | ||
1116 | (((s->state & SSL_ST_CONNECT) && | ||
1117 | (s->state >= SSL3_ST_CW_CLNT_HELLO_A) && | ||
1118 | (s->state <= SSL3_ST_CR_SRVR_HELLO_A)) || ( | ||
1119 | (s->state & SSL_ST_ACCEPT) && | ||
1120 | (s->state <= SSL3_ST_SW_HELLO_REQ_A) && | ||
1121 | (s->state >= SSL3_ST_SR_CLNT_HELLO_A)))) { | ||
1122 | s->s3->in_read_app_data = 2; | ||
1123 | return (-1); | ||
1124 | } else { | ||
1125 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1126 | SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD); | ||
1127 | goto f_err; | ||
1128 | } | ||
1129 | } | ||
1130 | /* not reached */ | ||
1131 | |||
1132 | f_err: | ||
1133 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1134 | err: | ||
1135 | return (-1); | ||
1136 | } | ||
1137 | |||
1138 | int | ||
1139 | dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len) | ||
1140 | { | ||
1141 | int i; | ||
1142 | |||
1143 | if (SSL_in_init(s) && !s->in_handshake) | ||
1144 | { | ||
1145 | i = s->handshake_func(s); | ||
1146 | if (i < 0) | ||
1147 | return (i); | ||
1148 | if (i == 0) { | ||
1149 | SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE); | ||
1150 | return -1; | ||
1151 | } | ||
1152 | } | ||
1153 | |||
1154 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { | ||
1155 | SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES, SSL_R_DTLS_MESSAGE_TOO_BIG); | ||
1156 | return -1; | ||
1157 | } | ||
1158 | |||
1159 | i = dtls1_write_bytes(s, type, buf_, len); | ||
1160 | return i; | ||
1161 | } | ||
1162 | |||
1163 | |||
1164 | /* this only happens when a client hello is received and a handshake | ||
1165 | * is started. */ | ||
1166 | static int | ||
1167 | have_handshake_fragment(SSL *s, int type, unsigned char *buf, | ||
1168 | int len, int peek) | ||
1169 | { | ||
1170 | |||
1171 | if ((type == SSL3_RT_HANDSHAKE) && (s->d1->handshake_fragment_len > 0)) | ||
1172 | /* (partially) satisfy request from storage */ | ||
1173 | { | ||
1174 | unsigned char *src = s->d1->handshake_fragment; | ||
1175 | unsigned char *dst = buf; | ||
1176 | unsigned int k, n; | ||
1177 | |||
1178 | /* peek == 0 */ | ||
1179 | n = 0; | ||
1180 | while ((len > 0) && (s->d1->handshake_fragment_len > 0)) { | ||
1181 | *dst++ = *src++; | ||
1182 | len--; | ||
1183 | s->d1->handshake_fragment_len--; | ||
1184 | n++; | ||
1185 | } | ||
1186 | /* move any remaining fragment bytes: */ | ||
1187 | for (k = 0; k < s->d1->handshake_fragment_len; k++) | ||
1188 | s->d1->handshake_fragment[k] = *src++; | ||
1189 | return n; | ||
1190 | } | ||
1191 | |||
1192 | return 0; | ||
1193 | } | ||
1194 | |||
1195 | |||
1196 | /* Call this to write data in records of type 'type' | ||
1197 | * It will return <= 0 if not all data has been sent or non-blocking IO. | ||
1198 | */ | ||
1199 | int | ||
1200 | dtls1_write_bytes(SSL *s, int type, const void *buf, int len) | ||
1201 | { | ||
1202 | int i; | ||
1203 | |||
1204 | OPENSSL_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH); | ||
1205 | s->rwstate = SSL_NOTHING; | ||
1206 | i = do_dtls1_write(s, type, buf, len); | ||
1207 | return i; | ||
1208 | } | ||
1209 | |||
1210 | int | ||
1211 | do_dtls1_write(SSL *s, int type, const unsigned char *buf, unsigned int len) | ||
1212 | { | ||
1213 | unsigned char *p, *pseq; | ||
1214 | int i, mac_size, clear = 0; | ||
1215 | int prefix_len = 0; | ||
1216 | SSL3_RECORD *wr; | ||
1217 | SSL3_BUFFER *wb; | ||
1218 | SSL_SESSION *sess; | ||
1219 | int bs; | ||
1220 | |||
1221 | /* first check if there is a SSL3_BUFFER still being written | ||
1222 | * out. This will happen with non blocking IO */ | ||
1223 | if (s->s3->wbuf.left != 0) { | ||
1224 | OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */ | ||
1225 | return (ssl3_write_pending(s, type, buf, len)); | ||
1226 | } | ||
1227 | |||
1228 | /* If we have an alert to send, lets send it */ | ||
1229 | if (s->s3->alert_dispatch) { | ||
1230 | i = s->method->ssl_dispatch_alert(s); | ||
1231 | if (i <= 0) | ||
1232 | return (i); | ||
1233 | /* if it went, fall through and send more stuff */ | ||
1234 | } | ||
1235 | |||
1236 | if (len == 0) | ||
1237 | return 0; | ||
1238 | |||
1239 | wr = &(s->s3->wrec); | ||
1240 | wb = &(s->s3->wbuf); | ||
1241 | sess = s->session; | ||
1242 | |||
1243 | if ((sess == NULL) || (s->enc_write_ctx == NULL) || | ||
1244 | (EVP_MD_CTX_md(s->write_hash) == NULL)) | ||
1245 | clear = 1; | ||
1246 | |||
1247 | if (clear) | ||
1248 | mac_size = 0; | ||
1249 | else { | ||
1250 | mac_size = EVP_MD_CTX_size(s->write_hash); | ||
1251 | if (mac_size < 0) | ||
1252 | goto err; | ||
1253 | } | ||
1254 | |||
1255 | /* DTLS implements explicit IV, so no need for empty fragments. */ | ||
1256 | |||
1257 | p = wb->buf + prefix_len; | ||
1258 | |||
1259 | /* write the header */ | ||
1260 | |||
1261 | *(p++) = type&0xff; | ||
1262 | wr->type = type; | ||
1263 | |||
1264 | *(p++) = (s->version >> 8); | ||
1265 | *(p++) = s->version&0xff; | ||
1266 | |||
1267 | /* field where we are to write out packet epoch, seq num and len */ | ||
1268 | pseq = p; | ||
1269 | |||
1270 | p += 10; | ||
1271 | |||
1272 | /* lets setup the record stuff. */ | ||
1273 | |||
1274 | /* Make space for the explicit IV in case of CBC. | ||
1275 | * (this is a bit of a boundary violation, but what the heck). | ||
1276 | */ | ||
1277 | if (s->enc_write_ctx && | ||
1278 | (EVP_CIPHER_mode( s->enc_write_ctx->cipher ) & EVP_CIPH_CBC_MODE)) | ||
1279 | bs = EVP_CIPHER_block_size(s->enc_write_ctx->cipher); | ||
1280 | else | ||
1281 | bs = 0; | ||
1282 | |||
1283 | wr->data = p + bs; | ||
1284 | /* make room for IV in case of CBC */ | ||
1285 | wr->length = (int)len; | ||
1286 | wr->input = (unsigned char *)buf; | ||
1287 | |||
1288 | /* we now 'read' from wr->input, wr->length bytes into | ||
1289 | * wr->data */ | ||
1290 | |||
1291 | memcpy(wr->data, wr->input, wr->length); | ||
1292 | wr->input = wr->data; | ||
1293 | |||
1294 | /* we should still have the output to wr->data and the input | ||
1295 | * from wr->input. Length should be wr->length. | ||
1296 | * wr->data still points in the wb->buf */ | ||
1297 | |||
1298 | if (mac_size != 0) { | ||
1299 | if (s->method->ssl3_enc->mac(s, &(p[wr->length + bs]), 1) < 0) | ||
1300 | goto err; | ||
1301 | wr->length += mac_size; | ||
1302 | } | ||
1303 | |||
1304 | /* this is true regardless of mac size */ | ||
1305 | wr->input = p; | ||
1306 | wr->data = p; | ||
1307 | |||
1308 | |||
1309 | /* ssl3_enc can only have an error on read */ | ||
1310 | if (bs) /* bs != 0 in case of CBC */ | ||
1311 | { | ||
1312 | arc4random_buf(p, bs); | ||
1313 | /* master IV and last CBC residue stand for | ||
1314 | * the rest of randomness */ | ||
1315 | wr->length += bs; | ||
1316 | } | ||
1317 | |||
1318 | s->method->ssl3_enc->enc(s, 1); | ||
1319 | |||
1320 | /* record length after mac and block padding */ | ||
1321 | /* if (type == SSL3_RT_APPLICATION_DATA || | ||
1322 | (type == SSL3_RT_ALERT && ! SSL_in_init(s))) */ | ||
1323 | |||
1324 | /* there's only one epoch between handshake and app data */ | ||
1325 | |||
1326 | s2n(s->d1->w_epoch, pseq); | ||
1327 | |||
1328 | /* XDTLS: ?? */ | ||
1329 | /* else | ||
1330 | s2n(s->d1->handshake_epoch, pseq); | ||
1331 | */ | ||
1332 | |||
1333 | memcpy(pseq, &(s->s3->write_sequence[2]), 6); | ||
1334 | pseq += 6; | ||
1335 | s2n(wr->length, pseq); | ||
1336 | |||
1337 | /* we should now have | ||
1338 | * wr->data pointing to the encrypted data, which is | ||
1339 | * wr->length long */ | ||
1340 | wr->type=type; /* not needed but helps for debugging */ | ||
1341 | wr->length += DTLS1_RT_HEADER_LENGTH; | ||
1342 | |||
1343 | ssl3_record_sequence_increment(s->s3->write_sequence); | ||
1344 | |||
1345 | /* now let's set up wb */ | ||
1346 | wb->left = prefix_len + wr->length; | ||
1347 | wb->offset = 0; | ||
1348 | |||
1349 | /* memorize arguments so that ssl3_write_pending can detect bad write retries later */ | ||
1350 | s->s3->wpend_tot = len; | ||
1351 | s->s3->wpend_buf = buf; | ||
1352 | s->s3->wpend_type = type; | ||
1353 | s->s3->wpend_ret = len; | ||
1354 | |||
1355 | /* we now just need to write the buffer */ | ||
1356 | return ssl3_write_pending(s, type, buf, len); | ||
1357 | err: | ||
1358 | return -1; | ||
1359 | } | ||
1360 | |||
1361 | |||
1362 | |||
1363 | static int | ||
1364 | dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap) | ||
1365 | { | ||
1366 | int cmp; | ||
1367 | unsigned int shift; | ||
1368 | const unsigned char *seq = s->s3->read_sequence; | ||
1369 | |||
1370 | cmp = satsub64be(seq, bitmap->max_seq_num); | ||
1371 | if (cmp > 0) { | ||
1372 | memcpy (s->s3->rrec.seq_num, seq, 8); | ||
1373 | return 1; /* this record in new */ | ||
1374 | } | ||
1375 | shift = -cmp; | ||
1376 | if (shift >= sizeof(bitmap->map)*8) | ||
1377 | return 0; /* stale, outside the window */ | ||
1378 | else if (bitmap->map & (1UL << shift)) | ||
1379 | return 0; /* record previously received */ | ||
1380 | |||
1381 | memcpy(s->s3->rrec.seq_num, seq, 8); | ||
1382 | return 1; | ||
1383 | } | ||
1384 | |||
1385 | |||
1386 | static void | ||
1387 | dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap) | ||
1388 | { | ||
1389 | int cmp; | ||
1390 | unsigned int shift; | ||
1391 | const unsigned char *seq = s->s3->read_sequence; | ||
1392 | |||
1393 | cmp = satsub64be(seq, bitmap->max_seq_num); | ||
1394 | if (cmp > 0) { | ||
1395 | shift = cmp; | ||
1396 | if (shift < sizeof(bitmap->map)*8) | ||
1397 | bitmap->map <<= shift, bitmap->map |= 1UL; | ||
1398 | else | ||
1399 | bitmap->map = 1UL; | ||
1400 | memcpy(bitmap->max_seq_num, seq, 8); | ||
1401 | } else { | ||
1402 | shift = -cmp; | ||
1403 | if (shift < sizeof(bitmap->map) * 8) | ||
1404 | bitmap->map |= 1UL << shift; | ||
1405 | } | ||
1406 | } | ||
1407 | |||
1408 | |||
1409 | int | ||
1410 | dtls1_dispatch_alert(SSL *s) | ||
1411 | { | ||
1412 | int i, j; | ||
1413 | void (*cb)(const SSL *ssl, int type, int val) = NULL; | ||
1414 | unsigned char buf[DTLS1_AL_HEADER_LENGTH]; | ||
1415 | unsigned char *ptr = &buf[0]; | ||
1416 | |||
1417 | s->s3->alert_dispatch = 0; | ||
1418 | |||
1419 | memset(buf, 0x00, sizeof(buf)); | ||
1420 | *ptr++ = s->s3->send_alert[0]; | ||
1421 | *ptr++ = s->s3->send_alert[1]; | ||
1422 | |||
1423 | #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE | ||
1424 | if (s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) { | ||
1425 | s2n(s->d1->handshake_read_seq, ptr); | ||
1426 | l2n3(s->d1->r_msg_hdr.frag_off, ptr); | ||
1427 | } | ||
1428 | #endif | ||
1429 | |||
1430 | i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf)); | ||
1431 | if (i <= 0) { | ||
1432 | s->s3->alert_dispatch = 1; | ||
1433 | /* fprintf( stderr, "not done with alert\n" ); */ | ||
1434 | } else { | ||
1435 | if (s->s3->send_alert[0] == SSL3_AL_FATAL | ||
1436 | #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE | ||
1437 | || s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE | ||
1438 | #endif | ||
1439 | ) | ||
1440 | (void)BIO_flush(s->wbio); | ||
1441 | |||
1442 | if (s->msg_callback) | ||
1443 | s->msg_callback(1, s->version, SSL3_RT_ALERT, | ||
1444 | s->s3->send_alert, 2, s, s->msg_callback_arg); | ||
1445 | |||
1446 | if (s->info_callback != NULL) | ||
1447 | cb = s->info_callback; | ||
1448 | else if (s->ctx->info_callback != NULL) | ||
1449 | cb = s->ctx->info_callback; | ||
1450 | |||
1451 | if (cb != NULL) { | ||
1452 | j = (s->s3->send_alert[0]<<8)|s->s3->send_alert[1]; | ||
1453 | cb(s, SSL_CB_WRITE_ALERT, j); | ||
1454 | } | ||
1455 | } | ||
1456 | return (i); | ||
1457 | } | ||
1458 | |||
1459 | |||
1460 | static DTLS1_BITMAP * | ||
1461 | dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, unsigned int *is_next_epoch) | ||
1462 | { | ||
1463 | |||
1464 | *is_next_epoch = 0; | ||
1465 | |||
1466 | /* In current epoch, accept HM, CCS, DATA, & ALERT */ | ||
1467 | if (rr->epoch == s->d1->r_epoch) | ||
1468 | return &s->d1->bitmap; | ||
1469 | |||
1470 | /* Only HM and ALERT messages can be from the next epoch */ | ||
1471 | else if (rr->epoch == (unsigned long)(s->d1->r_epoch + 1) && | ||
1472 | (rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) { | ||
1473 | *is_next_epoch = 1; | ||
1474 | return &s->d1->next_bitmap; | ||
1475 | } | ||
1476 | |||
1477 | return NULL; | ||
1478 | } | ||
1479 | |||
1480 | void | ||
1481 | dtls1_reset_seq_numbers(SSL *s, int rw) | ||
1482 | { | ||
1483 | unsigned char *seq; | ||
1484 | unsigned int seq_bytes = sizeof(s->s3->read_sequence); | ||
1485 | |||
1486 | if (rw & SSL3_CC_READ) { | ||
1487 | seq = s->s3->read_sequence; | ||
1488 | s->d1->r_epoch++; | ||
1489 | memcpy(&(s->d1->bitmap), &(s->d1->next_bitmap), sizeof(DTLS1_BITMAP)); | ||
1490 | memset(&(s->d1->next_bitmap), 0x00, sizeof(DTLS1_BITMAP)); | ||
1491 | } else { | ||
1492 | seq = s->s3->write_sequence; | ||
1493 | memcpy(s->d1->last_write_sequence, seq, sizeof(s->s3->write_sequence)); | ||
1494 | s->d1->w_epoch++; | ||
1495 | } | ||
1496 | |||
1497 | memset(seq, 0x00, seq_bytes); | ||
1498 | } | ||