| 1 |
|
| #ifndef SRL_READER_CHECKSUM_H_ |
| 2 |
|
| #define SRL_READER_CHECKSUM_H_ |
| 3 |
|
| |
| 4 |
|
| /* Reading and verifying the protocol v6 keyed checksum envelope: |
| 5 |
|
| * |
| 6 |
|
| * <CHECKSUM-TYPE-U8> <CHECKSUM-LENGTH-VARINT> <CHECKSUM> |
| 7 |
|
| * <PAYLOAD-LENGTH-VARINT> <PAYLOAD> |
| 8 |
|
| * |
| 9 |
|
| * Shared by Sereal::Decoder and the tools that read Sereal documents |
| 10 |
|
| * (Sereal::Merger, Sereal::Splitter, Sereal::Path::Iterator). */ |
| 11 |
|
| |
| 12 |
|
| #include "srl_inline.h" |
| 13 |
|
| #include "srl_common.h" |
| 14 |
|
| #include "srl_reader_types.h" |
| 15 |
|
| #include "srl_reader.h" |
| 16 |
|
| #include "srl_reader_error.h" |
| 17 |
|
| #include "srl_reader_varint.h" |
| 18 |
|
| #include "srl_protocol.h" |
| 19 |
|
| #include "srl_siphash.h" |
| 20 |
|
| #include "srl_checksum_policy.h" |
| 21 |
|
| |
| 22 |
|
| typedef struct { |
| 23 |
|
| U8 checksum_type; |
| 24 |
|
| const U8 *tag; |
| 25 |
|
| size_t tag_len; |
| 26 |
|
| const U8 *payload_len_start; |
| 27 |
|
| size_t payload_len_varint_size; |
| 28 |
|
| const U8 *payload; |
| 29 |
|
| size_t payload_len; |
| 30 |
|
| } srl_checksum_envelope_t; |
| 31 |
|
| |
| 32 |
|
| SRL_STATIC_INLINE void |
| 33 |
0 |
| srl_write_u64_le(U8 *p, srl_u64 value) |
| 34 |
|
| { |
| 35 |
|
| U8 i; |
| 36 |
0 |
▶ | for (i = 0; i < 8; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) |
0 |
0 |
0 |
|
| 37 |
0 |
| p[i] = (U8)(value >> (8 * i)); |
| 38 |
0 |
| } |
| 39 |
|
| |
| 40 |
|
| SRL_STATIC_INLINE void |
| 41 |
0 |
| srl_read_checksum_envelope(pTHX_ srl_reader_buffer_t *buf, srl_checksum_envelope_t *envelope) |
| 42 |
|
| { |
| 43 |
|
| UV tag_len_uv, payload_len_uv; |
| 44 |
|
| size_t available; |
| 45 |
|
| int known_type; |
| 46 |
0 |
▶ | SRL_RDR_ASSERT_SPACE(buf, 1, " while reading the checksum type"); |
Branch: 1 branch
| Branch | True | False | Total |
| SRL_RDR_ASSERT_SPACE(buf, 1, " while reading the checksum type"); |
0 |
0 |
0 |
|
| 47 |
0 |
| envelope->checksum_type = *buf->pos++; |
| 48 |
0 |
| known_type = srl_checksum_tag_length(envelope->checksum_type) != 0; |
| 49 |
0 |
| tag_len_uv = srl_read_varint_uv(aTHX_ buf); |
| 50 |
0 |
| envelope->tag_len = (size_t)tag_len_uv; |
| 51 |
0 |
▶ | if (expect_false((UV)envelope->tag_len != tag_len_uv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false((UV)envelope->tag_len != tag_len_uv)) |
0 |
0 |
0 |
|
| 52 |
0 |
| SRL_RDR_ERROR(buf, "Sereal checksum length is too large for this platform"); |
| 53 |
0 |
| available = (size_t)(buf->end - buf->pos); |
| 54 |
0 |
▶ | if (expect_false(envelope->tag_len > available)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false(envelope->tag_len > available)) |
0 |
0 |
0 |
|
| 55 |
0 |
| SRL_RDR_ERROR(buf, "Sereal checksum length exceeds the available input"); |
| 56 |
|
| |
| 57 |
0 |
▶ | if (known_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (known_type) { |
0 |
0 |
0 |
|
| 58 |
0 |
| const size_t expected_len = srl_checksum_tag_length(envelope->checksum_type); |
| 59 |
0 |
▶ | if (expect_false(envelope->tag_len != expected_len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false(envelope->tag_len != expected_len)) |
0 |
0 |
0 |
|
| 60 |
0 |
| SRL_RDR_ERRORf2(buf, "Invalid checksum length %"UVuf" for checksum type %u", |
| 61 |
|
| tag_len_uv, envelope->checksum_type); |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
0 |
| envelope->tag = buf->pos; |
| 65 |
0 |
| envelope->payload_len_start = envelope->tag + envelope->tag_len; |
| 66 |
0 |
| buf->pos = (U8 *)envelope->payload_len_start; |
| 67 |
0 |
| payload_len_uv = srl_read_varint_uv(aTHX_ buf); |
| 68 |
0 |
| envelope->payload_len_varint_size = (size_t)(buf->pos - envelope->payload_len_start); |
| 69 |
0 |
| envelope->payload = buf->pos; |
| 70 |
0 |
| envelope->payload_len = (size_t)payload_len_uv; |
| 71 |
0 |
▶ | if (expect_false((UV)envelope->payload_len != payload_len_uv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false((UV)envelope->payload_len != payload_len_uv)) |
0 |
0 |
0 |
|
| 72 |
0 |
| SRL_RDR_ERROR(buf, "Checksummed Sereal payload length is too large for this platform"); |
| 73 |
0 |
▶ | if (expect_false(envelope->payload_len > (size_t)(buf->end - envelope->payload))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false(envelope->payload_len > (size_t)(buf->end - envelope->payload))) |
0 |
0 |
0 |
|
| 74 |
0 |
| SRL_RDR_ERROR(buf, "Checksummed Sereal payload length exceeds the available input"); |
| 75 |
|
| |
| 76 |
0 |
| } |
| 77 |
|
| |
| 78 |
|
| SRL_STATIC_INLINE int |
| 79 |
0 |
| srl_checksum_matches(pTHX_ srl_reader_buffer_t *buf, const srl_checksum_envelope_t *envelope, const U8 *key) |
| 80 |
|
| { |
| 81 |
|
| U8 actual[SRL_CHECKSUM_TAG_LENGTH_128]; |
| 82 |
|
| U8 expected[SRL_CHECKSUM_TAG_LENGTH_128]; |
| 83 |
0 |
| U8 mismatch = 0; |
| 84 |
|
| U8 i; |
| 85 |
|
| srl_siphash_ctx sip; |
| 86 |
|
| /* callers validate known types first; this keeps a future caller from overrunning the stack buffers */ |
| 87 |
0 |
▶ | if (expect_false(envelope->tag_len > sizeof(expected))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_false(envelope->tag_len > sizeof(expected))) |
0 |
0 |
0 |
|
| 88 |
0 |
| SRL_RDR_ERROR(buf, "Sereal checksum tag is too long to verify"); |
| 89 |
0 |
| Copy(envelope->tag, expected, envelope->tag_len, U8); |
| 90 |
0 |
| srl_siphash_init(&sip, key, envelope->checksum_type); |
| 91 |
0 |
| srl_siphash_update(&sip, (const U8 *)buf->start, (size_t)(envelope->tag - buf->start)); |
| 92 |
0 |
| srl_siphash_update(&sip, envelope->payload_len_start, envelope->payload_len_varint_size); |
| 93 |
0 |
| srl_siphash_update(&sip, envelope->payload, envelope->payload_len); |
| 94 |
0 |
▶ | if (envelope->tag_len == SRL_CHECKSUM_TAG_LENGTH_128) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (envelope->tag_len == SRL_CHECKSUM_TAG_LENGTH_128) { |
0 |
0 |
0 |
|
| 95 |
0 |
| srl_siphash_final128(&sip, actual); |
| 96 |
|
| } else { |
| 97 |
0 |
| srl_u64 digest = srl_siphash_final64(&sip); |
| 98 |
0 |
| srl_write_u64_le(actual, digest); |
| 99 |
|
| } |
| 100 |
0 |
▶ | for (i = 0; i < envelope->tag_len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < envelope->tag_len; i++) |
0 |
0 |
0 |
|
| 101 |
0 |
| mismatch |= (U8)(actual[i] ^ expected[i]); |
| 102 |
0 |
| return mismatch == 0; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
|
| /* Enforce a checksum policy on an envelope that srl_read_checksum_envelope() |
| 106 |
|
| * has just read: reject unknown types and missing keys under the strict |
| 107 |
|
| * policies, warn under the permissive ones, and verify the tag whenever a |
| 108 |
|
| * matching key is available. Croaks on failure. */ |
| 109 |
|
| SRL_STATIC_INLINE void |
| 110 |
0 |
| srl_checksum_apply_policy(pTHX_ srl_reader_buffer_t *buf, const srl_checksum_envelope_t *envelope, |
| 111 |
|
| const srl_checksum_config_t *cfg) |
| 112 |
|
| { |
| 113 |
0 |
| const U8 policy = cfg->policy; |
| 114 |
|
| int known_type, has_key, verify; |
| 115 |
0 |
| const int strict = srl_checksum_policy_is_strict(policy); |
| 116 |
0 |
| const U8 minimum = srl_checksum_required_mode(policy); |
| 117 |
0 |
| known_type = srl_checksum_tag_length(envelope->checksum_type) != 0; |
| 118 |
0 |
▶ | has_key = known_type && cfg->has_key; |
Branch: 2 branches
| Branch | True | False | Total |
| has_key = known_type && cfg->has_key; |
0 |
0 |
0 |
| has_key = known_type && cfg->has_key; |
0 |
0 |
0 |
|
| 119 |
0 |
▶ | if (strict && !known_type) |
Branch: 2 branches
| Branch | True | False | Total |
| if (strict && !known_type) |
0 |
0 |
0 |
| if (strict && !known_type) |
0 |
0 |
0 |
|
| 120 |
0 |
| SRL_RDR_ERRORf1(buf, "Unsupported Sereal checksum type %u", envelope->checksum_type); |
| 121 |
0 |
▶ | if (minimum && envelope->checksum_type + 1 < minimum) |
Branch: 2 branches
| Branch | True | False | Total |
| if (minimum && envelope->checksum_type + 1 < minimum) |
0 |
0 |
0 |
| if (minimum && envelope->checksum_type + 1 < minimum) |
0 |
0 |
0 |
|
| 122 |
0 |
| SRL_RDR_ERRORf2(buf, "checksum_policy '%s' requires checksum mode v%u or higher", |
| 123 |
|
| srl_checksum_policy_name(policy), (unsigned int)minimum); |
| 124 |
0 |
▶ | if (strict && !has_key) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (strict && !has_key) { |
0 |
0 |
0 |
| if (strict && !has_key) { |
0 |
0 |
0 |
|
| 125 |
0 |
| SRL_RDR_ERROR(buf, "Checksummed Sereal document requires a 'checksum_key'"); |
| 126 |
|
| } |
| 127 |
0 |
▶ | verify = has_key && (policy == SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE || strict); |
Branch: 3 branches
| Branch | True | False | Total |
| verify = has_key && (policy == SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE || strict); |
0 |
0 |
0 |
| verify = has_key && (policy == SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE || strict); |
0 |
0 |
0 |
| verify = has_key && (policy == SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE || strict); |
0 |
0 |
0 |
|
| 128 |
0 |
▶ | if (policy == SRL_CHECKSUM_POLICY_IGNORE_WARN) |
Branch: 1 branch
| Branch | True | False | Total |
| if (policy == SRL_CHECKSUM_POLICY_IGNORE_WARN) |
0 |
0 |
0 |
|
| 129 |
0 |
| warn("Ignoring Sereal checksum authentication by request"); |
| 130 |
0 |
▶ | else if (policy == SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE && !has_key) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (policy == SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE && !has_key) |
0 |
0 |
0 |
| else if (policy == SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE && !has_key) |
0 |
0 |
0 |
|
| 131 |
0 |
| warn("Decoding checksummed Sereal document without authentication: no matching checksum key is configured"); |
| 132 |
0 |
▶ | if (verify && !srl_checksum_matches(aTHX_ buf, envelope, cfg->key)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (verify && !srl_checksum_matches(aTHX_ buf, envelope, cfg->key)) |
0 |
0 |
0 |
| if (verify && !srl_checksum_matches(aTHX_ buf, envelope, cfg->key)) |
0 |
0 |
0 |
|
| 133 |
0 |
| SRL_RDR_ERROR(buf, "Sereal packet checksum verification failed"); |
| 134 |
0 |
| } |
| 135 |
|
| |
| 136 |
|
| /* Store one checksum key, padding or truncating it to 16 bytes with a |
| 137 |
|
| * warning. Policy-specific length checks have already been done. */ |
| 138 |
|
| SRL_STATIC_INLINE void |
| 139 |
44 |
| srl_checksum_store_key(pTHX_ U8 key_out[SRL_CHECKSUM_KEY_LENGTH], U8 *has_key, SV *val, |
| 140 |
|
| const char *option_name) |
| 141 |
|
| { |
| 142 |
44 |
▶ | if (val && SvOK(val)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (val && SvOK(val)) { |
44 |
0 |
44 |
| if (val && SvOK(val)) { |
0 |
44 |
44 |
|
| 143 |
|
| STRLEN key_len; |
| 144 |
0 |
| char *key = SvPVbyte(val, key_len); |
| 145 |
0 |
▶ | if (key_len != SRL_CHECKSUM_KEY_LENGTH) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != SRL_CHECKSUM_KEY_LENGTH) { |
0 |
0 |
0 |
|
| 146 |
0 |
▶ | if (key_len < SRL_CHECKSUM_KEY_LENGTH) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len < SRL_CHECKSUM_KEY_LENGTH) |
0 |
0 |
0 |
|
| 147 |
0 |
| warn("The '%s' key is shorter than 16 bytes; padding with nulls. Use Sereal::Decoder::prepare_checksum_key() to normalize it", option_name); |
| 148 |
|
| else |
| 149 |
0 |
| warn("The '%s' key is longer than 16 bytes; extra bytes are ignored and may contain unused entropy. Use Sereal::Decoder::prepare_checksum_key() to normalize it", option_name); |
| 150 |
|
| } |
| 151 |
0 |
| Zero(key_out, SRL_CHECKSUM_KEY_LENGTH, U8); |
| 152 |
0 |
| Copy(key, key_out, |
| 153 |
|
| key_len < SRL_CHECKSUM_KEY_LENGTH ? key_len : SRL_CHECKSUM_KEY_LENGTH, U8); |
| 154 |
0 |
| *has_key = 1; |
| 155 |
|
| } |
| 156 |
44 |
| } |
| 157 |
|
| |
| 158 |
|
| /* Load the same checksum options and defaults as the Decoder constructor. */ |
| 159 |
|
| SRL_STATIC_INLINE void |
| 160 |
|
| srl_checksum_config_from_hv(pTHX_ srl_checksum_config_t *cfg, HV *opt) |
| 161 |
|
| { |
| 162 |
|
| static const char * const key_options[3] = { |
| 163 |
|
| "checksum_key_v1", "checksum_key_v2", "checksum_key_v3" |
| 164 |
|
| }; |
| 165 |
|
| SV *checksum_key = NULL; |
| 166 |
|
| SV *policy = NULL; |
| 167 |
|
| U8 version; |
| 168 |
|
| SV **svp; |
| 169 |
|
| |
| 170 |
|
| if (opt != NULL) { |
| 171 |
|
| svp = hv_fetchs(opt, "checksum_policy", 0); |
| 172 |
|
| if (svp) |
| 173 |
|
| policy = *svp; |
| 174 |
|
| svp = hv_fetchs(opt, "checksum_key", 0); |
| 175 |
|
| if (svp) |
| 176 |
|
| srl_set_checksum_key(aTHX_ &checksum_key, *svp); |
| 177 |
|
| svp = hv_fetchs(opt, "checksum_mode", 0); |
| 178 |
|
| if (svp) |
| 179 |
|
| (void)srl_checksum_mode_from_sv(aTHX_ *svp, "checksum_mode"); |
| 180 |
|
| for (version = 0; version <= 2; version++) { |
| 181 |
|
| svp = hv_fetch(opt, key_options[version], (I32)strlen(key_options[version]), 0); |
| 182 |
|
| if (svp) { |
| 183 |
|
| srl_set_checksum_key(aTHX_ &checksum_key, *svp); |
| 184 |
|
| } |
| 185 |
|
| } |
| 186 |
|
| } |
| 187 |
|
| if (!checksum_key) |
| 188 |
|
| checksum_key = srl_get_default_checksum_key(aTHX_ "Sereal::Decoder::DefaultKey"); |
| 189 |
|
| |
| 190 |
|
| Zero(cfg, 1, srl_checksum_config_t); |
| 191 |
|
| cfg->policy = srl_get_checksum_policy(aTHX_ policy, "Sereal::Decoder::ChecksumPolicy"); |
| 192 |
|
| srl_validate_checksum_key(aTHX_ checksum_key, "checksum_key", cfg->policy); |
| 193 |
|
| srl_validate_checksum_required_key(aTHX_ checksum_key, cfg->policy); |
| 194 |
|
| srl_checksum_store_key(aTHX_ cfg->key, &cfg->has_key, checksum_key, "checksum_key"); |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
|
| #endif |