1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//! The decoded tree.
//!
//! Every `range` is a half-open `[start, end)` byte range into the buffer the
//! node or field was parsed from. A `Layer` node starts a new buffer (its
//! `decoded` bytes) for its child; a string's `decoded` node starts a new
//! buffer at that string's bytes.
use crate::Diag;
use crate::value::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Alphabet {
Std,
Url,
}
impl Alphabet {
pub fn name(self) -> &'static str {
match self {
Alphabet::Std => "std",
Alphabet::Url => "url",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Codec {
Hex,
/// A hex dump with an offset column (`style` is `xxd` or `hexdump`).
/// `start` is the decimal offset the first line starts at (nonzero for
/// `xxd -s` / `hexdump -s`); child ranges stay 0-based.
Dump {
style: &'static str,
start: usize,
},
Base64 {
alphabet: Alphabet,
padded: bool,
},
Pem {
label: String,
},
/// Two or more PEM blocks: a certificate chain or a key + cert file.
Bundle,
/// A JSON Web Token; `kind` is `jws` (3 parts) or `jwe` (5 parts).
Jwt {
kind: &'static str,
},
/// gzip; `name` is the original file name from the header, if any.
Gzip {
name: Option<String>,
},
Zlib,
Zstd,
Protobuf,
Der,
/// CBOR (RFC 8949): a sequence of one or more [`Value`]s.
Cbor,
/// MessagePack: a sequence of one or more [`Value`]s.
Msgpack,
Utf8,
Bytes,
}
impl Codec {
pub fn name(&self) -> &'static str {
match self {
Codec::Hex => "hex",
Codec::Dump { .. } => "dump",
Codec::Base64 { .. } => "base64",
Codec::Pem { .. } => "pem",
Codec::Bundle => "bundle",
Codec::Jwt { .. } => "jwt",
Codec::Gzip { .. } => "gzip",
Codec::Zlib => "zlib",
Codec::Zstd => "zstd",
Codec::Protobuf => "protobuf",
Codec::Der => "der",
Codec::Cbor => "cbor",
Codec::Msgpack => "msgpack",
Codec::Utf8 => "utf8",
Codec::Bytes => "bytes",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Node {
pub codec: Codec,
pub range: [usize; 2],
/// How confident the detector is in `codec` (0..=1).
pub confidence: f64,
pub detail: Detail,
pub warnings: Vec<Diag>,
}
/// Bytes kept in a [`Detail::Bytes`] leaf for its preview.
pub const BYTES_HEAD: usize = 64;
/// Characters kept in a [`Detail::Text`] leaf.
pub const TEXT_HEAD: usize = 1024;
#[derive(Debug, Clone, PartialEq)]
pub enum Detail {
/// A text encoding (hex/base64/PEM); `child` describes the decoded bytes.
Layer { decoded: Vec<u8>, child: Box<Node> },
/// A protobuf message.
Message { fields: Vec<Field> },
/// DER-encoded ASN.1: one or more top-level elements.
Der { items: Vec<Asn1> },
/// CBOR or MessagePack: normally one top-level value; a sequence may have
/// several when forced with `--as cbor` / `--as msgpack`.
Values { items: Vec<Value> },
/// A JWT or a PEM bundle: one node per part (base64 segments, or PEM
/// blocks). `times` is `Detail::Parts`' only JWT-specific field: the
/// top-level time claims of a JWS payload (`exp`, `nbf`, `iat`), in that
/// order, as `(claim, ISO-8601 UTC)`; it is empty for bundles.
Parts {
parts: Vec<Node>,
times: Vec<(&'static str, String)>,
},
/// Printable UTF-8 text. Only the first [`TEXT_HEAD`] characters are kept
/// (what the JSON shows): the full text is the parent's bytes, so a large
/// text is not stored twice.
Text {
/// The first `TEXT_HEAD` characters (all of a shorter text).
text: String,
/// Byte length of the whole text; `text` is all of it when equal.
len: usize,
},
/// Opaque bytes (nothing more specific was found, or the format is not
/// supported). Only the first [`BYTES_HEAD`] bytes are kept, for the
/// preview: the full bytes are the parent's (the input, a layer's
/// `decoded`, a field's or value's data), so a large leaf is not stored twice.
Bytes {
/// Length of the whole leaf.
len: usize,
/// Its first `min(len, BYTES_HEAD)` bytes.
head: Vec<u8>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct Field {
pub number: u32,
/// Range of the whole field, tag included.
pub range: [usize; 2],
pub value: FieldValue,
}
#[derive(Debug, Clone, PartialEq)]
pub enum FieldValue {
Varint {
raw: Vec<u8>,
value: u64,
},
I64 {
value: u64,
},
I32 {
value: u32,
},
Len {
data_range: [usize; 2],
data: Vec<u8>,
/// Interpretations, best first. Scores sum to ~1 before truncation.
alts: Vec<Alt>,
},
Group {
fields: Vec<Field>,
},
}
impl FieldValue {
pub fn wire_name(&self) -> &'static str {
match self {
FieldValue::Varint { .. } => "VARINT",
FieldValue::I64 { .. } => "I64",
FieldValue::I32 { .. } => "I32",
FieldValue::Len { .. } => "LEN",
FieldValue::Group { .. } => "GROUP",
}
}
}
impl Field {
/// The field's payload bytes (tag excluded). `None` for groups.
pub fn payload(&self) -> Option<Vec<u8>> {
match &self.value {
FieldValue::Varint { raw, .. } => Some(raw.clone()),
FieldValue::I64 { value } => Some(value.to_le_bytes().to_vec()),
FieldValue::I32 { value } => Some(value.to_le_bytes().to_vec()),
FieldValue::Len { data, .. } => Some(data.clone()),
FieldValue::Group { .. } => None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Alt {
pub kind: AltKind,
pub score: f64,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AltKind {
Message(Vec<Field>),
String {
text: String,
/// Set when the string itself decodes further (e.g. it is base64).
decoded: Option<Box<Node>>,
},
PackedVarint(Vec<u64>),
/// Little-endian 4-byte values (packed `fixed32` / `sfixed32` / `float`).
PackedFixed32(Vec<u32>),
/// Little-endian 8-byte values (packed `fixed64` / `sfixed64` / `double`).
PackedFixed64(Vec<u64>),
Bytes {
/// Set when the raw bytes are themselves another format (compressed,
/// DER, CBOR/MessagePack); see `docs/DESIGN.md` for the threshold.
decoded: Option<Box<Node>>,
},
Empty,
}
impl AltKind {
pub fn name(&self) -> &'static str {
match self {
AltKind::Message(_) => "message",
AltKind::String { .. } => "string",
AltKind::PackedVarint(_) => "packed_varint",
AltKind::PackedFixed32(_) => "packed_fixed32",
AltKind::PackedFixed64(_) => "packed_fixed64",
AltKind::Bytes { .. } => "bytes",
AltKind::Empty => "empty",
}
}
}
/// ASN.1 tag class.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TagClass {
Universal,
Application,
Context,
Private,
}
/// One DER element (tag, length, value).
#[derive(Debug, Clone, PartialEq)]
pub struct Asn1 {
pub class: TagClass,
pub number: u32,
pub constructed: bool,
/// Range of the whole element, header included.
pub range: [usize; 2],
/// Range of the content (header excluded).
pub content: [usize; 2],
/// Content bytes of a primitive element (empty when constructed).
pub raw: Vec<u8>,
pub value: Asn1Value,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Asn1Value {
/// A constructed element (SEQUENCE, SET, `[0]`, ...).
Constructed(Vec<Asn1>),
/// A BIT STRING or OCTET STRING whose content is itself DER.
/// `unused_bits` is `Some(0)` for a BIT STRING.
Encapsulated {
unused_bits: Option<u8>,
items: Vec<Asn1>,
},
Bool(bool),
/// INTEGER or ENUMERATED; the value is `raw` (big-endian two's complement).
Int,
Null,
/// OBJECT IDENTIFIER or RELATIVE-OID in dotted form.
Oid(String),
/// A character string, decoded per its type.
Text(String),
/// UTCTime or GeneralizedTime; `iso` is set when the value is well formed.
Time {
text: String,
iso: Option<String>,
},
/// A BIT STRING that is not itself DER; the bits are `raw[1..]`.
BitString {
unused_bits: u8,
},
/// Anything else; the content is `raw`.
Bytes,
}
impl Asn1 {
/// Human-readable tag, e.g. `SEQUENCE`, `[0]`, `[APPLICATION 3]`.
pub fn tag_name(&self) -> String {
match self.class {
TagClass::Universal => match crate::der::universal_name(self.number) {
Some(n) => n.to_string(),
None => format!("[UNIVERSAL {}]", self.number),
},
TagClass::Application => format!("[APPLICATION {}]", self.number),
TagClass::Context => format!("[{}]", self.number),
TagClass::Private => format!("[PRIVATE {}]", self.number),
}
}
/// The element's payload: content bytes, minus the unused-bits byte for a
/// BIT STRING. `None` for constructed elements.
pub fn payload(&self) -> Option<Vec<u8>> {
if self.constructed {
return None;
}
match self.value {
Asn1Value::BitString { .. }
| Asn1Value::Encapsulated {
unused_bits: Some(_),
..
} => Some(self.raw.get(1..).unwrap_or_default().to_vec()),
_ => Some(self.raw.clone()),
}
}
/// Child elements, for constructed or encapsulating elements.
pub fn children(&self) -> Option<&[Asn1]> {
match &self.value {
Asn1Value::Constructed(items) | Asn1Value::Encapsulated { items, .. } => Some(items),
_ => None,
}
}
}