Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* DTLS implementation written by Nagendra Modadugu |
| 2 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. */ |
| 3 | /* ==================================================================== |
| 4 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * |
| 18 | * 3. All advertising materials mentioning features or use of this |
| 19 | * software must display the following acknowledgment: |
| 20 | * "This product includes software developed by the OpenSSL Project |
| 21 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 22 | * |
| 23 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 24 | * endorse or promote products derived from this software without |
| 25 | * prior written permission. For written permission, please contact |
| 26 | * openssl-core@openssl.org. |
| 27 | * |
| 28 | * 5. Products derived from this software may not be called "OpenSSL" |
| 29 | * nor may "OpenSSL" appear in their names without prior written |
| 30 | * permission of the OpenSSL Project. |
| 31 | * |
| 32 | * 6. Redistributions of any form whatsoever must retain the following |
| 33 | * acknowledgment: |
| 34 | * "This product includes software developed by the OpenSSL Project |
| 35 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 36 | * |
| 37 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 38 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 39 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 40 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 41 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 42 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 43 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 44 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 45 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 46 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 48 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 49 | * ==================================================================== |
| 50 | * |
| 51 | * This product includes cryptographic software written by Eric Young |
| 52 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 53 | * Hudson (tjh@cryptsoft.com). |
| 54 | * |
| 55 | */ |
| 56 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 57 | * All rights reserved. |
| 58 | * |
| 59 | * This package is an SSL implementation written |
| 60 | * by Eric Young (eay@cryptsoft.com). |
| 61 | * The implementation was written so as to conform with Netscapes SSL. |
| 62 | * |
| 63 | * This library is free for commercial and non-commercial use as long as |
| 64 | * the following conditions are aheared to. The following conditions |
| 65 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 66 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 67 | * included with this distribution is covered by the same copyright terms |
| 68 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 69 | * |
| 70 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 71 | * the code are not to be removed. |
| 72 | * If this package is used in a product, Eric Young should be given attribution |
| 73 | * as the author of the parts of the library used. |
| 74 | * This can be in the form of a textual message at program startup or |
| 75 | * in documentation (online or textual) provided with the package. |
| 76 | * |
| 77 | * Redistribution and use in source and binary forms, with or without |
| 78 | * modification, are permitted provided that the following conditions |
| 79 | * are met: |
| 80 | * 1. Redistributions of source code must retain the copyright |
| 81 | * notice, this list of conditions and the following disclaimer. |
| 82 | * 2. Redistributions in binary form must reproduce the above copyright |
| 83 | * notice, this list of conditions and the following disclaimer in the |
| 84 | * documentation and/or other materials provided with the distribution. |
| 85 | * 3. All advertising materials mentioning features or use of this software |
| 86 | * must display the following acknowledgement: |
| 87 | * "This product includes cryptographic software written by |
| 88 | * Eric Young (eay@cryptsoft.com)" |
| 89 | * The word 'cryptographic' can be left out if the rouines from the library |
| 90 | * being used are not cryptographic related :-). |
| 91 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 92 | * the apps directory (application code) you must include an acknowledgement: |
| 93 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 94 | * |
| 95 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 96 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 97 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 98 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 99 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 100 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 101 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 102 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 103 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 104 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 105 | * SUCH DAMAGE. |
| 106 | * |
| 107 | * The licence and distribution terms for any publically available version or |
| 108 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 109 | * copied and put under another distribution licence |
| 110 | * [including the GNU Public Licence.] */ |
| 111 | |
| 112 | #include <stdio.h> |
| 113 | #include <errno.h> |
| 114 | #include <assert.h> |
| 115 | |
| 116 | #include <openssl/buf.h> |
| 117 | #include <openssl/mem.h> |
| 118 | #include <openssl/evp.h> |
| 119 | #include <openssl/err.h> |
| 120 | #include <openssl/rand.h> |
| 121 | |
| 122 | #include "ssl_locl.h" |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 123 | |
| 124 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | /* mod 128 saturating subtract of two 64-bit values in big-endian order */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 126 | static int satsub64be(const uint8_t *v1, const uint8_t *v2) { |
| 127 | int ret, sat, brw, i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 129 | if (sizeof(long) == 8) { |
| 130 | do { |
| 131 | const union { |
| 132 | long one; |
| 133 | char little; |
| 134 | } is_endian = {1}; |
| 135 | long l; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 137 | if (is_endian.little) { |
| 138 | break; |
| 139 | } |
| 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 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 147 | l = *((long *)v1); |
| 148 | l -= *((long *)v2); |
| 149 | if (l > 128) { |
| 150 | return 128; |
| 151 | } else if (l < -128) { |
| 152 | return -128; |
| 153 | } else { |
| 154 | return (int)l; |
| 155 | } |
| 156 | } while (0); |
| 157 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 159 | ret = (int)v1[7] - (int)v2[7]; |
| 160 | sat = 0; |
| 161 | brw = ret >> 8; /* brw is either 0 or -1 */ |
| 162 | if (ret & 0x80) { |
| 163 | for (i = 6; i >= 0; i--) { |
| 164 | brw += (int)v1[i] - (int)v2[i]; |
| 165 | sat |= ~brw; |
| 166 | brw >>= 8; |
| 167 | } |
| 168 | } else { |
| 169 | for (i = 6; i >= 0; i--) { |
| 170 | brw += (int)v1[i] - (int)v2[i]; |
| 171 | sat |= brw; |
| 172 | brw >>= 8; |
| 173 | } |
| 174 | } |
| 175 | brw <<= 8; /* brw is either 0 or -256 */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 176 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 177 | if (sat & 0xff) { |
| 178 | return brw | 0x80; |
| 179 | } else { |
| 180 | return brw + (ret & 0xFF); |
| 181 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap); |
| 185 | static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 186 | static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, |
| 187 | unsigned int *is_next_epoch); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | static int dtls1_buffer_record(SSL *s, record_pqueue *q, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 189 | uint8_t *priority); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | static int dtls1_process_record(SSL *s); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 191 | static int do_dtls1_write(SSL *s, int type, const uint8_t *buf, |
| 192 | unsigned int len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | |
| 194 | /* copy buffered record into SSL structure */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 195 | static int dtls1_copy_record(SSL *s, pitem *item) { |
| 196 | DTLS1_RECORD_DATA *rdata; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 198 | rdata = (DTLS1_RECORD_DATA *)item->data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 199 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 200 | if (s->s3->rbuf.buf != NULL) { |
| 201 | OPENSSL_free(s->s3->rbuf.buf); |
| 202 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 204 | s->packet = rdata->packet; |
| 205 | s->packet_length = rdata->packet_length; |
| 206 | memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER)); |
| 207 | memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 209 | /* Set proper sequence number for mac calculation */ |
| 210 | memcpy(&(s->s3->read_sequence[2]), &(rdata->packet[5]), 6); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 212 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 215 | static int dtls1_buffer_record(SSL *s, record_pqueue *queue, |
| 216 | uint8_t *priority) { |
| 217 | DTLS1_RECORD_DATA *rdata; |
| 218 | pitem *item; |
| 219 | |
| 220 | /* Limit the size of the queue to prevent DOS attacks */ |
| 221 | if (pqueue_size(queue->q) >= 100) { |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | rdata = OPENSSL_malloc(sizeof(DTLS1_RECORD_DATA)); |
| 226 | item = pitem_new(priority, rdata); |
| 227 | if (rdata == NULL || item == NULL) { |
| 228 | if (rdata != NULL) { |
| 229 | OPENSSL_free(rdata); |
| 230 | } |
| 231 | if (item != NULL) { |
| 232 | pitem_free(item); |
| 233 | } |
| 234 | |
| 235 | OPENSSL_PUT_ERROR(SSL, dtls1_buffer_record, ERR_R_INTERNAL_ERROR); |
Adam Langley | 44e2709 | 2015-01-08 12:02:28 -0800 | [diff] [blame] | 236 | return -1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | rdata->packet = s->packet; |
| 240 | rdata->packet_length = s->packet_length; |
| 241 | memcpy(&(rdata->rbuf), &(s->s3->rbuf), sizeof(SSL3_BUFFER)); |
| 242 | memcpy(&(rdata->rrec), &(s->s3->rrec), sizeof(SSL3_RECORD)); |
| 243 | |
| 244 | item->data = rdata; |
| 245 | |
| 246 | s->packet = NULL; |
| 247 | s->packet_length = 0; |
| 248 | memset(&(s->s3->rbuf), 0, sizeof(SSL3_BUFFER)); |
| 249 | memset(&(s->s3->rrec), 0, sizeof(SSL3_RECORD)); |
| 250 | |
| 251 | if (!ssl3_setup_buffers(s)) { |
Adam Langley | 44e2709 | 2015-01-08 12:02:28 -0800 | [diff] [blame] | 252 | goto internal_error; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* insert should not fail, since duplicates are dropped */ |
| 256 | if (pqueue_insert(queue->q, item) == NULL) { |
Adam Langley | 44e2709 | 2015-01-08 12:02:28 -0800 | [diff] [blame] | 257 | goto internal_error; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return 1; |
Adam Langley | 44e2709 | 2015-01-08 12:02:28 -0800 | [diff] [blame] | 261 | |
| 262 | internal_error: |
| 263 | OPENSSL_PUT_ERROR(SSL, dtls1_buffer_record, ERR_R_INTERNAL_ERROR); |
| 264 | if (rdata->rbuf.buf != NULL) { |
| 265 | OPENSSL_free(rdata->rbuf.buf); |
| 266 | } |
| 267 | OPENSSL_free(rdata); |
| 268 | pitem_free(item); |
| 269 | return -1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue) { |
| 273 | pitem *item; |
| 274 | |
| 275 | item = pqueue_pop(queue->q); |
| 276 | if (item) { |
| 277 | dtls1_copy_record(s, item); |
| 278 | |
| 279 | OPENSSL_free(item->data); |
| 280 | pitem_free(item); |
| 281 | |
| 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* retrieve a buffered record that belongs to the new epoch, i.e., not |
| 289 | * processed yet */ |
| 290 | #define dtls1_get_unprocessed_record(s) \ |
| 291 | dtls1_retrieve_buffered_record((s), &((s)->d1->unprocessed_rcds)) |
| 292 | |
| 293 | /* retrieve a buffered record that belongs to the current epoch, i.e., |
| 294 | * processed */ |
| 295 | #define dtls1_get_processed_record(s) \ |
| 296 | dtls1_retrieve_buffered_record((s), &((s)->d1->processed_rcds)) |
| 297 | |
| 298 | static int dtls1_process_buffered_records(SSL *s) { |
| 299 | pitem *item; |
| 300 | |
| 301 | item = pqueue_peek(s->d1->unprocessed_rcds.q); |
| 302 | if (item) { |
| 303 | /* Check if epoch is current. */ |
| 304 | if (s->d1->unprocessed_rcds.epoch != s->d1->r_epoch) { |
| 305 | return 1; /* Nothing to do. */ |
| 306 | } |
| 307 | |
| 308 | /* Process all the records. */ |
| 309 | while (pqueue_peek(s->d1->unprocessed_rcds.q)) { |
| 310 | dtls1_get_unprocessed_record(s); |
| 311 | if (!dtls1_process_record(s)) { |
| 312 | return 0; |
| 313 | } |
Adam Langley | 44e2709 | 2015-01-08 12:02:28 -0800 | [diff] [blame] | 314 | if (dtls1_buffer_record(s, &(s->d1->processed_rcds), |
| 315 | s->s3->rrec.seq_num) < 0) { |
| 316 | return -1; |
| 317 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | /* sync epoch numbers once all the unprocessed records have been processed */ |
| 322 | s->d1->processed_rcds.epoch = s->d1->r_epoch; |
| 323 | s->d1->unprocessed_rcds.epoch = s->d1->r_epoch + 1; |
| 324 | |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | static int dtls1_process_record(SSL *s) { |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 329 | int al; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 330 | SSL3_RECORD *rr; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 331 | |
| 332 | rr = &(s->s3->rrec); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 333 | |
| 334 | /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, and |
| 335 | * we have that many bytes in s->packet. */ |
| 336 | rr->input = &(s->packet[DTLS1_RT_HEADER_LENGTH]); |
| 337 | |
| 338 | /* ok, we can now read from 's->packet' data into 'rr' rr->input points at |
| 339 | * rr->length bytes, which need to be copied into rr->data by either the |
| 340 | * decryption or by the decompression When the data is 'copied' into the |
| 341 | * rr->data buffer, rr->input will be pointed at the new buffer */ |
| 342 | |
| 343 | /* We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length bytes |
| 344 | * of encrypted compressed stuff. */ |
| 345 | |
| 346 | /* check is not needed I believe */ |
| 347 | if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) { |
| 348 | al = SSL_AD_RECORD_OVERFLOW; |
| 349 | OPENSSL_PUT_ERROR(SSL, dtls1_process_record, |
| 350 | SSL_R_ENCRYPTED_LENGTH_TOO_LONG); |
| 351 | goto f_err; |
| 352 | } |
| 353 | |
| 354 | /* decrypt in place in 'rr->input' */ |
| 355 | rr->data = rr->input; |
| 356 | |
David Benjamin | 1e52eca | 2015-01-22 15:33:51 -0500 | [diff] [blame] | 357 | if (!s->enc_method->enc(s, 0)) { |
David Benjamin | 5fa3eba | 2015-01-22 16:35:40 -0500 | [diff] [blame] | 358 | /* Bad packets are silently dropped in DTLS. Clear the error queue of any |
| 359 | * errors decryption may have added. */ |
| 360 | ERR_clear_error(); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 361 | rr->length = 0; |
| 362 | s->packet_length = 0; |
| 363 | goto err; |
| 364 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 365 | |
| 366 | if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 367 | al = SSL_AD_RECORD_OVERFLOW; |
| 368 | OPENSSL_PUT_ERROR(SSL, dtls1_process_record, SSL_R_DATA_LENGTH_TOO_LONG); |
| 369 | goto f_err; |
| 370 | } |
| 371 | |
| 372 | rr->off = 0; |
| 373 | /* So at this point the following is true |
| 374 | * ssl->s3->rrec.type is the type of record |
| 375 | * ssl->s3->rrec.length == number of bytes in record |
| 376 | * ssl->s3->rrec.off == offset to first valid byte |
| 377 | * ssl->s3->rrec.data == where to take bytes from, increment |
| 378 | * after use :-). */ |
| 379 | |
| 380 | /* we have pulled in a full packet so zero things */ |
| 381 | s->packet_length = 0; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 382 | return 1; |
| 383 | |
| 384 | f_err: |
| 385 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 386 | |
| 387 | err: |
| 388 | return 0; |
| 389 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 390 | |
| 391 | /* Call this to get a new input record. |
| 392 | * It will return <= 0 if more data is needed, normally due to an error |
| 393 | * or non-blocking IO. |
| 394 | * When it finishes, one packet has been decoded and can be found in |
| 395 | * ssl->s3->rrec.type - is the type of record |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 396 | * ssl->s3->rrec.data, - data |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 397 | * ssl->s3->rrec.length, - number of bytes |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 398 | * |
| 399 | * used only by dtls1_read_bytes */ |
| 400 | int dtls1_get_record(SSL *s) { |
| 401 | int ssl_major, ssl_minor; |
| 402 | int i, n; |
| 403 | SSL3_RECORD *rr; |
| 404 | unsigned char *p = NULL; |
| 405 | unsigned short version; |
| 406 | DTLS1_BITMAP *bitmap; |
| 407 | unsigned int is_next_epoch; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 408 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 409 | rr = &(s->s3->rrec); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 410 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 411 | /* The epoch may have changed. If so, process all the pending records. This |
| 412 | * is a non-blocking operation. */ |
Adam Langley | 44e2709 | 2015-01-08 12:02:28 -0800 | [diff] [blame] | 413 | if (dtls1_process_buffered_records(s) < 0) { |
| 414 | return -1; |
| 415 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 416 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 417 | /* If we're renegotiating, then there may be buffered records. */ |
| 418 | if (dtls1_get_processed_record(s)) { |
| 419 | return 1; |
| 420 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 421 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 422 | /* get something from the wire */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | again: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 424 | /* check if we have the header */ |
| 425 | if ((s->rstate != SSL_ST_READ_BODY) || |
| 426 | (s->packet_length < DTLS1_RT_HEADER_LENGTH)) { |
| 427 | n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0); |
| 428 | /* read timeout is handled by dtls1_read_bytes */ |
| 429 | if (n <= 0) { |
| 430 | return n; /* error or non-blocking */ |
| 431 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 433 | /* this packet contained a partial record, dump it */ |
| 434 | if (s->packet_length != DTLS1_RT_HEADER_LENGTH) { |
| 435 | s->packet_length = 0; |
| 436 | goto again; |
| 437 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 438 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 439 | s->rstate = SSL_ST_READ_BODY; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 440 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 441 | p = s->packet; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 442 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 443 | if (s->msg_callback) { |
| 444 | s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH, s, |
| 445 | s->msg_callback_arg); |
| 446 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 447 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 448 | /* Pull apart the header into the DTLS1_RECORD */ |
| 449 | rr->type = *(p++); |
| 450 | ssl_major = *(p++); |
| 451 | ssl_minor = *(p++); |
| 452 | version = (ssl_major << 8) | ssl_minor; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 453 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 454 | /* sequence number is 64 bits, with top 2 bytes = epoch */ |
| 455 | n2s(p, rr->epoch); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 456 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 457 | memcpy(&(s->s3->read_sequence[2]), p, 6); |
| 458 | p += 6; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 459 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 460 | n2s(p, rr->length); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 461 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 462 | /* Lets check version */ |
| 463 | if (s->s3->have_version) { |
| 464 | if (version != s->version) { |
| 465 | /* The record's version doesn't match, so silently drop it. |
| 466 | * |
| 467 | * TODO(davidben): This doesn't work. The DTLS record layer is not |
| 468 | * packet-based, so the remainder of the packet isn't dropped and we |
| 469 | * get a framing error. It's also unclear what it means to silently |
| 470 | * drop a record in a packet containing two records. */ |
| 471 | rr->length = 0; |
| 472 | s->packet_length = 0; |
| 473 | goto again; |
| 474 | } |
| 475 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 476 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 477 | if ((version & 0xff00) != (s->version & 0xff00)) { |
| 478 | /* wrong version, silently discard record */ |
| 479 | rr->length = 0; |
| 480 | s->packet_length = 0; |
| 481 | goto again; |
| 482 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 484 | if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) { |
| 485 | /* record too long, silently discard it */ |
| 486 | rr->length = 0; |
| 487 | s->packet_length = 0; |
| 488 | goto again; |
| 489 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 490 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 491 | /* now s->rstate == SSL_ST_READ_BODY */ |
| 492 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 493 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 494 | /* s->rstate == SSL_ST_READ_BODY, get and decode the data */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 496 | if (rr->length > s->packet_length - DTLS1_RT_HEADER_LENGTH) { |
| 497 | /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */ |
| 498 | i = rr->length; |
| 499 | n = ssl3_read_n(s, i, i, 1); |
| 500 | if (n <= 0) { |
| 501 | return n; /* error or non-blocking io */ |
| 502 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 504 | /* this packet contained a partial record, dump it */ |
| 505 | if (n != i) { |
| 506 | rr->length = 0; |
| 507 | s->packet_length = 0; |
| 508 | goto again; |
| 509 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 510 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 511 | /* now n == rr->length, |
| 512 | * and s->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length */ |
| 513 | } |
| 514 | s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 515 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 516 | /* match epochs. NULL means the packet is dropped on the floor */ |
| 517 | bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch); |
| 518 | if (bitmap == NULL) { |
| 519 | rr->length = 0; |
| 520 | s->packet_length = 0; /* dump this record */ |
| 521 | goto again; /* get another record */ |
| 522 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 523 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 524 | /* Check whether this is a repeat, or aged record. */ |
| 525 | if (!dtls1_record_replay_check(s, bitmap)) { |
| 526 | rr->length = 0; |
| 527 | s->packet_length = 0; /* dump this record */ |
| 528 | goto again; /* get another record */ |
| 529 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 530 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 531 | /* just read a 0 length packet */ |
| 532 | if (rr->length == 0) { |
| 533 | goto again; |
| 534 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 535 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 536 | /* If this record is from the next epoch (either HM or ALERT), |
| 537 | * and a handshake is currently in progress, buffer it since it |
| 538 | * cannot be processed at this time. |
| 539 | */ |
| 540 | if (is_next_epoch) { |
| 541 | if (SSL_in_init(s) || s->in_handshake) { |
Adam Langley | 44e2709 | 2015-01-08 12:02:28 -0800 | [diff] [blame] | 542 | if (dtls1_buffer_record(s, &(s->d1->unprocessed_rcds), rr->seq_num) < 0) { |
| 543 | return -1; |
| 544 | } |
| 545 | dtls1_record_bitmap_update(s, bitmap); /* Mark receipt of record. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 546 | } |
| 547 | rr->length = 0; |
| 548 | s->packet_length = 0; |
| 549 | goto again; |
| 550 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 551 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 552 | if (!dtls1_process_record(s)) { |
| 553 | rr->length = 0; |
| 554 | s->packet_length = 0; /* dump this record */ |
| 555 | goto again; /* get another record */ |
| 556 | } |
Adam Langley | 44e2709 | 2015-01-08 12:02:28 -0800 | [diff] [blame] | 557 | dtls1_record_bitmap_update(s, bitmap); /* Mark receipt of record. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 559 | return 1; |
| 560 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 561 | |
| 562 | /* Return up to 'len' payload bytes received in 'type' records. |
| 563 | * 'type' is one of the following: |
| 564 | * |
| 565 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) |
| 566 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) |
| 567 | * - 0 (during a shutdown, no data has to be returned) |
| 568 | * |
| 569 | * If we don't have stored data to work from, read a SSL/TLS record first |
| 570 | * (possibly multiple records if we still don't have anything to return). |
| 571 | * |
| 572 | * This function must handle any surprises the peer may have for us, such as |
| 573 | * Alert records (e.g. close_notify), ChangeCipherSpec records (not really |
| 574 | * a surprise, but handled as if it were), or renegotiation requests. |
| 575 | * Also if record payloads contain fragments too small to process, we store |
| 576 | * them until there is enough for the respective protocol (the record protocol |
| 577 | * may use arbitrary fragmentation and even interleaving): |
| 578 | * Change cipher spec protocol |
| 579 | * just 1 byte needed, no need for keeping anything stored |
| 580 | * Alert protocol |
| 581 | * 2 bytes needed (AlertLevel, AlertDescription) |
| 582 | * Handshake protocol |
| 583 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have |
| 584 | * to detect unexpected Client Hello and Hello Request messages |
| 585 | * here, anything else is handled by higher layers |
| 586 | * Application data protocol |
| 587 | * none of our business |
| 588 | */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 589 | int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) { |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 590 | int al, i, ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 591 | unsigned int n; |
| 592 | SSL3_RECORD *rr; |
| 593 | void (*cb)(const SSL *ssl, int type2, int val) = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 594 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 595 | if (s->s3->rbuf.buf == NULL && !ssl3_setup_buffers(s)) { |
| 596 | return -1; |
| 597 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 598 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 599 | /* XXX: check what the second '&& type' is about */ |
| 600 | if ((type && (type != SSL3_RT_APPLICATION_DATA) && |
| 601 | (type != SSL3_RT_HANDSHAKE) && type) || |
| 602 | (peek && (type != SSL3_RT_APPLICATION_DATA))) { |
| 603 | OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, ERR_R_INTERNAL_ERROR); |
| 604 | return -1; |
| 605 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 606 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 607 | if (!s->in_handshake && SSL_in_init(s)) { |
| 608 | /* type == SSL3_RT_APPLICATION_DATA */ |
| 609 | i = s->handshake_func(s); |
| 610 | if (i < 0) { |
| 611 | return i; |
| 612 | } |
| 613 | if (i == 0) { |
| 614 | OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 615 | return -1; |
| 616 | } |
| 617 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 618 | |
| 619 | start: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 620 | s->rwstate = SSL_NOTHING; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 621 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 622 | /* s->s3->rrec.type - is the type of record |
| 623 | * s->s3->rrec.data - data |
| 624 | * s->s3->rrec.off - offset into 'data' for next read |
| 625 | * s->s3->rrec.length - number of bytes. */ |
| 626 | rr = &s->s3->rrec; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 627 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 628 | /* We are not handshaking and have no data yet, |
| 629 | * so process data buffered during the last handshake |
| 630 | * in advance, if any. |
| 631 | */ |
| 632 | if (s->state == SSL_ST_OK && rr->length == 0) { |
| 633 | pitem *item; |
| 634 | item = pqueue_pop(s->d1->buffered_app_data.q); |
| 635 | if (item) { |
| 636 | dtls1_copy_record(s, item); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 637 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 638 | OPENSSL_free(item->data); |
| 639 | pitem_free(item); |
| 640 | } |
| 641 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 642 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 643 | /* Check for timeout */ |
| 644 | if (dtls1_handle_timeout(s) > 0) { |
| 645 | goto start; |
| 646 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 647 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 648 | /* get new packet if necessary */ |
| 649 | if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) { |
| 650 | ret = dtls1_get_record(s); |
| 651 | if (ret <= 0) { |
| 652 | ret = dtls1_read_failed(s, ret); |
| 653 | /* anything other than a timeout is an error */ |
| 654 | if (ret <= 0) { |
| 655 | return ret; |
| 656 | } else { |
| 657 | goto start; |
| 658 | } |
| 659 | } |
| 660 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 661 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 662 | /* we now have a packet which can be read and processed */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 663 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 664 | /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by |
| 665 | * ssl3_get_finished. */ |
| 666 | if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE) { |
| 667 | /* We now have application data between CCS and Finished. Most likely the |
| 668 | * packets were reordered on their way, so buffer the application data for |
| 669 | * later processing rather than dropping the connection. */ |
Adam Langley | 44e2709 | 2015-01-08 12:02:28 -0800 | [diff] [blame] | 670 | if (dtls1_buffer_record(s, &(s->d1->buffered_app_data), rr->seq_num) < 0) { |
| 671 | OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, ERR_R_INTERNAL_ERROR); |
| 672 | return -1; |
| 673 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 674 | rr->length = 0; |
| 675 | goto start; |
| 676 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 677 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 678 | /* If the other end has shut down, throw anything we read away (even in |
| 679 | * 'peek' mode) */ |
| 680 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { |
| 681 | rr->length = 0; |
| 682 | s->rwstate = SSL_NOTHING; |
| 683 | return 0; |
| 684 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 685 | |
| 686 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 687 | if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ |
| 688 | /* make sure that we are not getting application data when we |
| 689 | * are doing a handshake for the first time */ |
| 690 | if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 691 | (s->aead_read_ctx == NULL)) { |
| 692 | /* TODO(davidben): Is this check redundant with the handshake_func |
| 693 | * check? */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 694 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 695 | OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE); |
| 696 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 699 | if (len <= 0) { |
| 700 | return len; |
| 701 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 702 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 703 | if ((unsigned int)len > rr->length) { |
| 704 | n = rr->length; |
| 705 | } else { |
| 706 | n = (unsigned int)len; |
| 707 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 708 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 709 | memcpy(buf, &(rr->data[rr->off]), n); |
| 710 | if (!peek) { |
| 711 | rr->length -= n; |
| 712 | rr->off += n; |
| 713 | if (rr->length == 0) { |
| 714 | s->rstate = SSL_ST_READ_HEADER; |
| 715 | rr->off = 0; |
| 716 | } |
| 717 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 718 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 719 | return n; |
| 720 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 721 | |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 722 | /* If we get here, then type != rr->type. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 723 | |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 724 | /* If an alert record, process one alert out of the record. Note that we allow |
| 725 | * a single record to contain multiple alerts. */ |
| 726 | if (rr->type == SSL3_RT_ALERT) { |
| 727 | /* Alerts may not be fragmented. */ |
| 728 | if (rr->length < 2) { |
| 729 | al = SSL_AD_DECODE_ERROR; |
| 730 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT); |
| 731 | goto f_err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 732 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 733 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 734 | if (s->msg_callback) { |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 735 | s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 736 | s->msg_callback_arg); |
| 737 | } |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 738 | uint8_t alert_level = rr->data[rr->off++]; |
| 739 | uint8_t alert_descr = rr->data[rr->off++]; |
| 740 | rr->length -= 2; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 741 | |
| 742 | if (s->info_callback != NULL) { |
| 743 | cb = s->info_callback; |
| 744 | } else if (s->ctx->info_callback != NULL) { |
| 745 | cb = s->ctx->info_callback; |
| 746 | } |
| 747 | |
| 748 | if (cb != NULL) { |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 749 | uint16_t alert = (alert_level << 8) | alert_descr; |
| 750 | cb(s, SSL_CB_READ_ALERT, alert); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | if (alert_level == 1) { /* warning */ |
| 754 | s->s3->warn_alert = alert_descr; |
| 755 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { |
| 756 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 757 | return 0; |
| 758 | } |
| 759 | } else if (alert_level == 2) { /* fatal */ |
| 760 | char tmp[16]; |
| 761 | |
| 762 | s->rwstate = SSL_NOTHING; |
| 763 | s->s3->fatal_alert = alert_descr; |
| 764 | OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, |
| 765 | SSL_AD_REASON_OFFSET + alert_descr); |
| 766 | BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr); |
| 767 | ERR_add_error_data(2, "SSL alert number ", tmp); |
| 768 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 769 | SSL_CTX_remove_session(s->ctx, s->session); |
| 770 | return 0; |
| 771 | } else { |
| 772 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 773 | OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE); |
| 774 | goto f_err; |
| 775 | } |
| 776 | |
| 777 | goto start; |
| 778 | } |
| 779 | |
| 780 | if (s->shutdown & SSL_SENT_SHUTDOWN) { |
| 781 | /* but we have not received a shutdown */ |
| 782 | s->rwstate = SSL_NOTHING; |
| 783 | rr->length = 0; |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
| 788 | struct ccs_header_st ccs_hdr; |
| 789 | unsigned int ccs_hdr_len = DTLS1_CCS_HEADER_LENGTH; |
| 790 | |
| 791 | dtls1_get_ccs_header(rr->data, &ccs_hdr); |
| 792 | |
| 793 | /* 'Change Cipher Spec' is just a single byte, so we know |
| 794 | * exactly what the record payload has to look like */ |
| 795 | /* XDTLS: check that epoch is consistent */ |
| 796 | if ((rr->length != ccs_hdr_len) || (rr->off != 0) || |
| 797 | (rr->data[0] != SSL3_MT_CCS)) { |
| 798 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 799 | OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 800 | goto f_err; |
| 801 | } |
| 802 | |
| 803 | rr->length = 0; |
| 804 | |
| 805 | if (s->msg_callback) { |
| 806 | s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, |
| 807 | s->msg_callback_arg); |
| 808 | } |
| 809 | |
| 810 | /* We can't process a CCS now, because previous handshake |
| 811 | * messages are still missing, so just drop it. |
| 812 | */ |
| 813 | if (!s->d1->change_cipher_spec_ok) { |
| 814 | goto start; |
| 815 | } |
| 816 | |
| 817 | s->d1->change_cipher_spec_ok = 0; |
| 818 | |
| 819 | s->s3->change_cipher_spec = 1; |
| 820 | if (!ssl3_do_change_cipher_spec(s)) { |
| 821 | goto err; |
| 822 | } |
| 823 | |
| 824 | /* do this whenever CCS is processed */ |
| 825 | dtls1_reset_seq_numbers(s, SSL3_CC_READ); |
| 826 | |
| 827 | goto start; |
| 828 | } |
| 829 | |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 830 | /* Unexpected handshake message. It may be a retransmitted Finished (the only |
| 831 | * post-CCS message). Otherwise, it's a pre-CCS handshake message from an |
| 832 | * unsupported renegotiation attempt. */ |
| 833 | if (rr->type == SSL3_RT_HANDSHAKE && !s->in_handshake) { |
| 834 | if (rr->length < DTLS1_HM_HEADER_LENGTH) { |
| 835 | al = SSL_AD_DECODE_ERROR; |
| 836 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HANDSHAKE_RECORD); |
| 837 | goto f_err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 838 | } |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 839 | struct hm_header_st msg_hdr; |
| 840 | dtls1_get_message_header(&rr->data[rr->off], &msg_hdr); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 841 | |
David Benjamin | ac6900b | 2014-12-19 10:39:01 -0500 | [diff] [blame] | 842 | /* Ignore the Finished, but retransmit our last flight of messages. If the |
| 843 | * peer sends the second Finished, they may not have received ours. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 844 | if (msg_hdr.type == SSL3_MT_FINISHED) { |
| 845 | if (dtls1_check_timeout_num(s) < 0) { |
| 846 | return -1; |
| 847 | } |
| 848 | |
| 849 | dtls1_retransmit_buffered_messages(s); |
| 850 | rr->length = 0; |
| 851 | goto start; |
| 852 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 853 | } |
| 854 | |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame^] | 855 | /* We already handled these. */ |
| 856 | assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 857 | |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame^] | 858 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 859 | OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_UNEXPECTED_RECORD); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 860 | |
| 861 | f_err: |
| 862 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 863 | err: |
| 864 | return -1; |
| 865 | } |
| 866 | |
| 867 | int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len) { |
| 868 | int i; |
| 869 | |
| 870 | if (SSL_in_init(s) && !s->in_handshake) { |
| 871 | i = s->handshake_func(s); |
| 872 | if (i < 0) { |
| 873 | return i; |
| 874 | } |
| 875 | if (i == 0) { |
| 876 | OPENSSL_PUT_ERROR(SSL, dtls1_write_app_data_bytes, |
| 877 | SSL_R_SSL_HANDSHAKE_FAILURE); |
| 878 | return -1; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 883 | OPENSSL_PUT_ERROR(SSL, dtls1_write_app_data_bytes, |
| 884 | SSL_R_DTLS_MESSAGE_TOO_BIG); |
| 885 | return -1; |
| 886 | } |
| 887 | |
| 888 | i = dtls1_write_bytes(s, type, buf_, len); |
| 889 | return i; |
| 890 | } |
| 891 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 892 | /* Call this to write data in records of type 'type' It will return <= 0 if not |
| 893 | * all data has been sent or non-blocking IO. */ |
| 894 | int dtls1_write_bytes(SSL *s, int type, const void *buf, int len) { |
| 895 | int i; |
| 896 | |
| 897 | assert(len <= SSL3_RT_MAX_PLAIN_LENGTH); |
| 898 | s->rwstate = SSL_NOTHING; |
| 899 | i = do_dtls1_write(s, type, buf, len); |
| 900 | return i; |
| 901 | } |
| 902 | |
| 903 | static int do_dtls1_write(SSL *s, int type, const uint8_t *buf, |
| 904 | unsigned int len) { |
| 905 | uint8_t *p, *pseq; |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 906 | int i; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 907 | int prefix_len = 0; |
| 908 | int eivlen = 0; |
| 909 | SSL3_RECORD *wr; |
| 910 | SSL3_BUFFER *wb; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 911 | |
| 912 | /* first check if there is a SSL3_BUFFER still being written |
| 913 | * out. This will happen with non blocking IO */ |
| 914 | if (s->s3->wbuf.left != 0) { |
| 915 | assert(0); /* XDTLS: want to see if we ever get here */ |
| 916 | return ssl3_write_pending(s, type, buf, len); |
| 917 | } |
| 918 | |
| 919 | /* If we have an alert to send, lets send it */ |
| 920 | if (s->s3->alert_dispatch) { |
| 921 | i = s->method->ssl_dispatch_alert(s); |
| 922 | if (i <= 0) { |
| 923 | return i; |
| 924 | } |
| 925 | /* if it went, fall through and send more stuff */ |
| 926 | } |
| 927 | |
| 928 | if (len == 0) { |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | wr = &(s->s3->wrec); |
| 933 | wb = &(s->s3->wbuf); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 934 | |
| 935 | p = wb->buf + prefix_len; |
| 936 | |
| 937 | /* write the header */ |
| 938 | |
| 939 | *(p++) = type & 0xff; |
| 940 | wr->type = type; |
| 941 | /* Special case: for hello verify request, client version 1.0 and |
| 942 | * we haven't decided which version to use yet send back using |
| 943 | * version 1.0 header: otherwise some clients will ignore it. |
| 944 | */ |
| 945 | if (!s->s3->have_version) { |
| 946 | *(p++) = DTLS1_VERSION >> 8; |
| 947 | *(p++) = DTLS1_VERSION & 0xff; |
| 948 | } else { |
| 949 | *(p++) = s->version >> 8; |
| 950 | *(p++) = s->version & 0xff; |
| 951 | } |
| 952 | |
| 953 | /* field where we are to write out packet epoch, seq num and len */ |
| 954 | pseq = p; |
| 955 | p += 10; |
| 956 | |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 957 | /* Leave room for the variable nonce for AEADs which specify it explicitly. */ |
| 958 | if (s->aead_write_ctx != NULL && |
| 959 | s->aead_write_ctx->variable_nonce_included_in_record) { |
David Benjamin | e95d20d | 2014-12-23 11:16:01 -0500 | [diff] [blame] | 960 | eivlen = s->aead_write_ctx->variable_nonce_len; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* lets setup the record stuff. */ |
| 964 | wr->data = p + eivlen; /* make room for IV in case of CBC */ |
| 965 | wr->length = (int)len; |
| 966 | wr->input = (unsigned char *)buf; |
| 967 | |
| 968 | /* we now 'read' from wr->input, wr->length bytes into wr->data */ |
| 969 | memcpy(wr->data, wr->input, wr->length); |
| 970 | wr->input = wr->data; |
| 971 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 972 | /* this is true regardless of mac size */ |
| 973 | wr->input = p; |
| 974 | wr->data = p; |
| 975 | wr->length += eivlen; |
| 976 | |
David Benjamin | 1e52eca | 2015-01-22 15:33:51 -0500 | [diff] [blame] | 977 | if (!s->enc_method->enc(s, 1)) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 978 | goto err; |
| 979 | } |
| 980 | |
| 981 | /* there's only one epoch between handshake and app data */ |
| 982 | s2n(s->d1->w_epoch, pseq); |
| 983 | |
| 984 | memcpy(pseq, &(s->s3->write_sequence[2]), 6); |
| 985 | pseq += 6; |
| 986 | s2n(wr->length, pseq); |
| 987 | |
| 988 | if (s->msg_callback) { |
| 989 | s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH, |
| 990 | DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg); |
| 991 | } |
| 992 | |
| 993 | /* we should now have wr->data pointing to the encrypted data, which is |
| 994 | * wr->length long */ |
| 995 | wr->type = type; /* not needed but helps for debugging */ |
| 996 | wr->length += DTLS1_RT_HEADER_LENGTH; |
| 997 | |
| 998 | ssl3_record_sequence_update(&(s->s3->write_sequence[0])); |
| 999 | |
| 1000 | /* now let's set up wb */ |
| 1001 | wb->left = prefix_len + wr->length; |
| 1002 | wb->offset = 0; |
| 1003 | |
| 1004 | /* memorize arguments so that ssl3_write_pending can detect bad write retries |
| 1005 | * later */ |
| 1006 | s->s3->wpend_tot = len; |
| 1007 | s->s3->wpend_buf = buf; |
| 1008 | s->s3->wpend_type = type; |
| 1009 | s->s3->wpend_ret = len; |
| 1010 | |
| 1011 | /* we now just need to write the buffer */ |
| 1012 | return ssl3_write_pending(s, type, buf, len); |
| 1013 | |
| 1014 | err: |
| 1015 | return -1; |
| 1016 | } |
| 1017 | |
| 1018 | static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap) { |
| 1019 | int cmp; |
| 1020 | unsigned int shift; |
| 1021 | const uint8_t *seq = s->s3->read_sequence; |
| 1022 | |
| 1023 | cmp = satsub64be(seq, bitmap->max_seq_num); |
| 1024 | if (cmp > 0) { |
| 1025 | memcpy(s->s3->rrec.seq_num, seq, 8); |
| 1026 | return 1; /* this record in new */ |
| 1027 | } |
| 1028 | shift = -cmp; |
| 1029 | if (shift >= sizeof(bitmap->map) * 8) { |
| 1030 | return 0; /* stale, outside the window */ |
| 1031 | } else if (bitmap->map & (((uint64_t)1) << shift)) { |
| 1032 | return 0; /* record previously received */ |
| 1033 | } |
| 1034 | |
| 1035 | memcpy(s->s3->rrec.seq_num, seq, 8); |
| 1036 | return 1; |
| 1037 | } |
| 1038 | |
| 1039 | static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap) { |
| 1040 | int cmp; |
| 1041 | unsigned int shift; |
| 1042 | const uint8_t *seq = s->s3->read_sequence; |
| 1043 | |
| 1044 | cmp = satsub64be(seq, bitmap->max_seq_num); |
| 1045 | if (cmp > 0) { |
| 1046 | shift = cmp; |
| 1047 | if (shift < sizeof(bitmap->map) * 8) { |
| 1048 | bitmap->map <<= shift, bitmap->map |= 1UL; |
| 1049 | } else { |
| 1050 | bitmap->map = 1UL; |
| 1051 | } |
| 1052 | memcpy(bitmap->max_seq_num, seq, 8); |
| 1053 | } else { |
| 1054 | shift = -cmp; |
| 1055 | if (shift < sizeof(bitmap->map) * 8) { |
| 1056 | bitmap->map |= ((uint64_t)1) << shift; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | int dtls1_dispatch_alert(SSL *s) { |
| 1062 | int i, j; |
| 1063 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
| 1064 | uint8_t buf[DTLS1_AL_HEADER_LENGTH]; |
| 1065 | uint8_t *ptr = &buf[0]; |
| 1066 | |
| 1067 | s->s3->alert_dispatch = 0; |
| 1068 | |
| 1069 | memset(buf, 0x00, sizeof(buf)); |
| 1070 | *ptr++ = s->s3->send_alert[0]; |
| 1071 | *ptr++ = s->s3->send_alert[1]; |
| 1072 | |
| 1073 | i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf)); |
| 1074 | if (i <= 0) { |
| 1075 | s->s3->alert_dispatch = 1; |
| 1076 | } else { |
| 1077 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 1078 | (void)BIO_flush(s->wbio); |
| 1079 | } |
| 1080 | |
| 1081 | if (s->msg_callback) { |
| 1082 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, |
| 1083 | s->msg_callback_arg); |
| 1084 | } |
| 1085 | |
| 1086 | if (s->info_callback != NULL) { |
| 1087 | cb = s->info_callback; |
| 1088 | } else if (s->ctx->info_callback != NULL) { |
| 1089 | cb = s->ctx->info_callback; |
| 1090 | } |
| 1091 | |
| 1092 | if (cb != NULL) { |
| 1093 | j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; |
| 1094 | cb(s, SSL_CB_WRITE_ALERT, j); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | return i; |
| 1099 | } |
| 1100 | |
| 1101 | static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, |
| 1102 | unsigned int *is_next_epoch) { |
| 1103 | *is_next_epoch = 0; |
| 1104 | |
| 1105 | /* In current epoch, accept HM, CCS, DATA, & ALERT */ |
| 1106 | if (rr->epoch == s->d1->r_epoch) { |
| 1107 | return &s->d1->bitmap; |
| 1108 | } else if (rr->epoch == (unsigned long)(s->d1->r_epoch + 1) && |
| 1109 | (rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) { |
| 1110 | /* Only HM and ALERT messages can be from the next epoch */ |
| 1111 | *is_next_epoch = 1; |
| 1112 | return &s->d1->next_bitmap; |
| 1113 | } |
| 1114 | |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | |
| 1118 | void dtls1_reset_seq_numbers(SSL *s, int rw) { |
| 1119 | uint8_t *seq; |
| 1120 | unsigned int seq_bytes = sizeof(s->s3->read_sequence); |
| 1121 | |
| 1122 | if (rw & SSL3_CC_READ) { |
| 1123 | seq = s->s3->read_sequence; |
| 1124 | s->d1->r_epoch++; |
| 1125 | memcpy(&(s->d1->bitmap), &(s->d1->next_bitmap), sizeof(DTLS1_BITMAP)); |
| 1126 | memset(&(s->d1->next_bitmap), 0x00, sizeof(DTLS1_BITMAP)); |
| 1127 | } else { |
| 1128 | seq = s->s3->write_sequence; |
| 1129 | memcpy(s->d1->last_write_sequence, seq, sizeof(s->s3->write_sequence)); |
| 1130 | s->d1->w_epoch++; |
| 1131 | } |
| 1132 | |
| 1133 | memset(seq, 0x00, seq_bytes); |
| 1134 | } |