]>
Commit | Line | Data |
---|---|---|
91f93f69 | 1 | /* |
846e33c7 | 2 | * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved. |
91f93f69 | 3 | * |
2c18d164 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
91f93f69 MC |
8 | */ |
9 | ||
706457b7 DMSP |
10 | #include "../ssl_local.h" |
11 | #include "record_local.h" | |
91f93f69 MC |
12 | |
13 | /* mod 128 saturating subtract of two 64-bit values in big-endian order */ | |
14 | static int satsub64be(const unsigned char *v1, const unsigned char *v2) | |
15 | { | |
31c34a3e DW |
16 | int64_t ret; |
17 | uint64_t l1, l2; | |
91f93f69 | 18 | |
31c34a3e DW |
19 | n2l8(v1, l1); |
20 | n2l8(v2, l2); | |
91f93f69 | 21 | |
31c34a3e | 22 | ret = l1 - l2; |
91f93f69 | 23 | |
31c34a3e DW |
24 | /* We do not permit wrap-around */ |
25 | if (l1 > l2 && ret < 0) | |
26 | return 128; | |
27 | else if (l2 > l1 && ret > 0) | |
28 | return -128; | |
2e94723c DW |
29 | |
30 | if (ret > 128) | |
31 | return 128; | |
32 | else if (ret < -128) | |
33 | return -128; | |
91f93f69 | 34 | else |
31c34a3e | 35 | return (int)ret; |
91f93f69 MC |
36 | } |
37 | ||
38b051a1 | 38 | int dtls1_record_replay_check(SSL_CONNECTION *s, DTLS1_BITMAP *bitmap) |
91f93f69 MC |
39 | { |
40 | int cmp; | |
41 | unsigned int shift; | |
42 | const unsigned char *seq = s->rlayer.read_sequence; | |
43 | ||
44 | cmp = satsub64be(seq, bitmap->max_seq_num); | |
45 | if (cmp > 0) { | |
46 | SSL3_RECORD_set_seq_num(RECORD_LAYER_get_rrec(&s->rlayer), seq); | |
47 | return 1; /* this record in new */ | |
48 | } | |
49 | shift = -cmp; | |
50 | if (shift >= sizeof(bitmap->map) * 8) | |
51 | return 0; /* stale, outside the window */ | |
52 | else if (bitmap->map & (1UL << shift)) | |
53 | return 0; /* record previously received */ | |
54 | ||
55 | SSL3_RECORD_set_seq_num(RECORD_LAYER_get_rrec(&s->rlayer), seq); | |
56 | return 1; | |
57 | } | |
58 | ||
38b051a1 | 59 | void dtls1_record_bitmap_update(SSL_CONNECTION *s, DTLS1_BITMAP *bitmap) |
91f93f69 MC |
60 | { |
61 | int cmp; | |
62 | unsigned int shift; | |
63 | const unsigned char *seq = RECORD_LAYER_get_read_sequence(&s->rlayer); | |
64 | ||
65 | cmp = satsub64be(seq, bitmap->max_seq_num); | |
66 | if (cmp > 0) { | |
67 | shift = cmp; | |
68 | if (shift < sizeof(bitmap->map) * 8) | |
69 | bitmap->map <<= shift, bitmap->map |= 1UL; | |
70 | else | |
71 | bitmap->map = 1UL; | |
e5bf62f7 | 72 | memcpy(bitmap->max_seq_num, seq, SEQ_NUM_SIZE); |
91f93f69 MC |
73 | } else { |
74 | shift = -cmp; | |
75 | if (shift < sizeof(bitmap->map) * 8) | |
76 | bitmap->map |= 1UL << shift; | |
77 | } | |
78 | } |