|
| 1 | +use ciborium::Value; |
| 2 | +use serde::Serialize; |
| 3 | + |
| 4 | +fn parse_argument(additional_info: u8, value: &[u8]) -> (u64, usize) { |
| 5 | + match additional_info { |
| 6 | + 0..24 => (additional_info.into(), 0), |
| 7 | + 24 => (value[0].into(), 1), |
| 8 | + 25 => { |
| 9 | + let (argument, _) = value.split_first_chunk().unwrap(); |
| 10 | + (u16::from_be_bytes(*argument).into(), argument.len()) |
| 11 | + } |
| 12 | + 26 => { |
| 13 | + let (argument, _) = value.split_first_chunk().unwrap(); |
| 14 | + (u32::from_be_bytes(*argument).into(), argument.len()) |
| 15 | + } |
| 16 | + 27 => { |
| 17 | + let (argument, _) = value.split_first_chunk().unwrap(); |
| 18 | + (u64::from_be_bytes(*argument), argument.len()) |
| 19 | + } |
| 20 | + 28..=30 => panic!("reserved additional info: {additional_info}"), |
| 21 | + 31 => panic!("indefinite length items are not allowed in canonical CBOR"), |
| 22 | + 32.. => panic!("illegal additional info: {additional_info}"), |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +fn parse_value<'a>(data: &'a [u8], path: &str) -> &'a [u8] { |
| 27 | + println!("Checking value {path}"); |
| 28 | + |
| 29 | + assert!(data.len() > 0, "CBOR value must not be empty"); |
| 30 | + let mut offset = 1; |
| 31 | + let major_type = (data[0] & 0b11100000) >> 5; |
| 32 | + let additional_info = data[0] & 0b00011111; |
| 33 | + let (argument, n) = parse_argument(additional_info, &data[offset..]); |
| 34 | + offset += n; |
| 35 | + |
| 36 | + // if the argument encodes an integer, it must be encoded as short as possible |
| 37 | + if major_type <= 5 { |
| 38 | + let expected = if let Ok(argument) = u8::try_from(argument) { |
| 39 | + if argument <= 23 { |
| 40 | + argument |
| 41 | + } else { |
| 42 | + 24 |
| 43 | + } |
| 44 | + } else if argument <= u16::MAX.into() { |
| 45 | + 25 |
| 46 | + } else if argument <= u32::MAX.into() { |
| 47 | + 26 |
| 48 | + } else { |
| 49 | + 27 |
| 50 | + }; |
| 51 | + assert_eq!( |
| 52 | + additional_info, expected, |
| 53 | + "integer value {argument} must use additional info {expected}" |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + let argument = usize::try_from(argument).unwrap(); |
| 58 | + match major_type { |
| 59 | + 0 | 1 | 7 => { |
| 60 | + // no additional restrictions, no additional data |
| 61 | + } |
| 62 | + 2 => { |
| 63 | + // byte strings: no additional restrictions, but additional data |
| 64 | + offset += argument; |
| 65 | + } |
| 66 | + 3 => { |
| 67 | + // text strings: must be valid UTF-8 |
| 68 | + let s = &data[offset..][..argument]; |
| 69 | + assert!( |
| 70 | + str::from_utf8(s).is_ok(), |
| 71 | + "text must be valid UTF-8: {}", |
| 72 | + String::from_utf8_lossy(s) |
| 73 | + ); |
| 74 | + offset += argument; |
| 75 | + } |
| 76 | + 4 => { |
| 77 | + // arrays: must have valid items |
| 78 | + for i in 0..argument { |
| 79 | + let item = parse_value(&data[offset..], &format!("{path}[{i}]")); |
| 80 | + offset += item.len(); |
| 81 | + } |
| 82 | + } |
| 83 | + 5 => { |
| 84 | + // maps: must have valid items and be sorted |
| 85 | + let mut last_key = None; |
| 86 | + for i in 0..argument { |
| 87 | + let key = parse_value(&data[offset..], &format!("{path}[{i}].key")); |
| 88 | + offset += key.len(); |
| 89 | + |
| 90 | + let parsed_key: Value = ciborium::from_reader(key).unwrap(); |
| 91 | + println!("{path}[{i}].key = {parsed_key:?}"); |
| 92 | + |
| 93 | + let value = parse_value(&data[offset..], &format!("{path}[{i}].value")); |
| 94 | + offset += value.len(); |
| 95 | + |
| 96 | + if let Some(last_key) = last_key { |
| 97 | + assert!( |
| 98 | + last_key < key, |
| 99 | + "map keys must be in lexicographical order: keys[{}] = {}, keys[{i}] = {}", |
| 100 | + i - 1, |
| 101 | + hex::encode(last_key), |
| 102 | + hex::encode(key) |
| 103 | + ); |
| 104 | + } |
| 105 | + last_key = Some(key); |
| 106 | + } |
| 107 | + } |
| 108 | + 6 => { |
| 109 | + panic!("tags are not allowed in canonical CBOR"); |
| 110 | + } |
| 111 | + 8.. => panic!("illegal major type: {major_type}"), |
| 112 | + }; |
| 113 | + |
| 114 | + &data[..offset] |
| 115 | +} |
| 116 | + |
| 117 | +pub fn assert_canonical_cbor<T: Serialize>(object: &T) { |
| 118 | + let mut buffer = [0; 1024]; |
| 119 | + let serialized = cbor_smol::cbor_serialize(&object, &mut buffer).unwrap(); |
| 120 | + let value = parse_value(&serialized, "root"); |
| 121 | + assert_eq!( |
| 122 | + value.len(), |
| 123 | + serialized.len(), |
| 124 | + "CBOR data must not contain trailing bytes" |
| 125 | + ); |
| 126 | +} |
0 commit comments