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
//! ASN.1 Parser Utility
//!
//! This utility reads PEM or DER files and outputs an ASN.1 tree structure
//! similar to `openssl asn1parse`. It's useful for debugging and validating
//! ASN.1 encoded data.
//!
//! Usage:
//! cargo run --example asn1parse <file>
//! cargo run --example asn1parse <file> --offset <bytes>
//!
//! The output format matches `openssl asn1parse`:
//! offset:d=depth hl=header_len l=content_len cons/prim: TYPE [value]
//!
//! Examples:
//! cargo run --example asn1parse certificate.pem
//! cargo run --example asn1parse certificate.der
//! cargo run --example asn1parse data.der --offset 4
use std::env;
use std::fs;
use std::io::{self, Read};
use synta::{Decoder, Encoding, Tag, TagClass};
/// Parse DER or PEM file
fn read_asn1_file(path: &str) -> io::Result<Vec<u8>> {
let mut file = fs::File::open(path)?;
let mut contents = Vec::new();
file.read_to_end(&mut contents)?;
// Try to interpret as PEM first
if contents.windows(11).any(|w| w == b"-----BEGIN ") {
if let Some(der_data) = synta_certificate::pem_to_der(&contents).into_iter().next() {
return Ok(der_data);
}
}
// Otherwise assume it's DER
Ok(contents)
}
/// Format tag name
fn tag_name(tag: &Tag) -> String {
match tag.class() {
TagClass::Universal => match tag.number() {
1 => "BOOLEAN".to_string(),
2 => "INTEGER".to_string(),
3 => "BIT STRING".to_string(),
4 => "OCTET STRING".to_string(),
5 => "NULL".to_string(),
6 => "OBJECT".to_string(),
12 => "UTF8STRING".to_string(),
16 => "SEQUENCE".to_string(),
17 => "SET".to_string(),
19 => "PrintableString".to_string(),
22 => "IA5String".to_string(),
23 => "UTCTIME".to_string(),
24 => "GeneralizedTime".to_string(),
n => format!("UNIVERSAL[{}]", n),
},
TagClass::Application => format!("appl [ {} ]", tag.number()),
TagClass::ContextSpecific => format!("cont [ {} ]", tag.number()),
TagClass::Private => format!("priv [ {} ]", tag.number()),
}
}
/// Calculate tag encoding length
fn tag_length(tag: &Tag) -> usize {
if tag.number() < 31 {
1
} else {
// High-tag-number form: 1 byte + variable bytes
let mut num = tag.number();
let mut len = 1;
while num > 0 {
len += 1;
num >>= 7;
}
len
}
}
/// Calculate length encoding size
fn length_encoding_size(len: usize) -> usize {
if len < 128 {
1
} else if len < 256 {
2
} else if len < 65536 {
3
} else if len < 16777216 {
4
} else {
5
}
}
/// Parse and print ASN.1 tree
fn parse_tree(data: &[u8], depth: usize, base_offset: usize) -> synta::Result<usize> {
let mut decoder = Decoder::new(data, Encoding::Der);
let start_pos = decoder.position();
// Read tag
let tag = decoder.read_tag()?;
let tag_pos = start_pos;
let tag_len = tag_length(&tag);
// Read length
let length = decoder.read_length()?;
let content_len = length.definite()?;
let length_len = length_encoding_size(content_len);
let header_len = tag_len + length_len;
let cons_prim = if tag.is_constructed() { "cons" } else { "prim" };
let tag_name_str = tag_name(&tag);
// Read content for primitive types
let value_str = if !tag.is_constructed() && content_len > 0 {
let content = decoder.read_bytes(content_len)?;
// Format value based on type
match tag.class() {
TagClass::Universal => match tag.number() {
2 => {
// INTEGER - show hex if small
if content.len() <= 8 {
format!(
":{}",
content
.iter()
.map(|b| format!("{:02X}", b))
.collect::<String>()
)
} else {
String::new()
}
}
3 | 4 => {
// BIT STRING or OCTET STRING
if content.len() <= 8 {
format!(
":{}",
content
.iter()
.map(|b| format!("{:02X}", b))
.collect::<String>()
)
} else {
String::new()
}
}
6 => {
// OBJECT IDENTIFIER - decode it
if let Ok(oid_str) = decode_oid(content) {
format!(":{}", oid_str)
} else {
String::new()
}
}
12 | 19 | 22 => {
// String types
if let Ok(s) = std::str::from_utf8(content) {
format!(":{}", s)
} else {
String::new()
}
}
23 | 24 => {
// Time types
if let Ok(s) = std::str::from_utf8(content) {
format!(":{}", s)
} else {
String::new()
}
}
_ => String::new(),
},
_ => String::new(),
}
} else {
String::new()
};
println!(
"{:5}:d={:2} hl={} l={:4} {}: {}{}",
base_offset + tag_pos,
depth,
header_len,
content_len,
cons_prim,
tag_name_str,
value_str
);
// Recursively parse constructed types
if tag.is_constructed() && content_len > 0 {
let content_start = decoder.position();
let content_data = &data[content_start..content_start + content_len];
let mut parsed = 0;
while parsed < content_len {
let child_len = parse_tree(
&content_data[parsed..],
depth + 1,
base_offset + content_start + parsed,
)?;
parsed += child_len;
}
}
Ok(header_len + content_len)
}
/// Decode OID from bytes
fn decode_oid(data: &[u8]) -> Result<String, ()> {
if data.is_empty() {
return Err(());
}
let mut components = Vec::new();
// First byte encodes first two components
let first = data[0];
components.push((first / 40) as u32);
components.push((first % 40) as u32);
// Remaining bytes encode subsequent components
let mut i = 1;
while i < data.len() {
let mut value = 0u32;
loop {
if i >= data.len() {
break;
}
let byte = data[i];
i += 1;
value = (value << 7) | (byte & 0x7F) as u32;
if byte & 0x80 == 0 {
break;
}
}
components.push(value);
}
Ok(components
.iter()
.map(|c| c.to_string())
.collect::<Vec<_>>()
.join("."))
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("Usage: {} <file> [--offset <bytes>]", args[0]);
eprintln!();
eprintln!("Parse ASN.1 DER or PEM file and display structure.");
eprintln!();
eprintln!("Examples:");
eprintln!(" {} certificate.pem", args[0]);
eprintln!(" {} certificate.der", args[0]);
eprintln!(" {} data.der --offset 4", args[0]);
std::process::exit(1);
}
let file_path = &args[1];
// Parse optional offset
let start_offset = if args.len() >= 4 && args[2] == "--offset" {
args[3].parse::<usize>().unwrap_or(0)
} else {
0
};
// Read file
let data = match read_asn1_file(file_path) {
Ok(data) => data,
Err(e) => {
eprintln!("Error reading file: {}", e);
std::process::exit(1);
}
};
// Apply offset if specified
let parse_data = if start_offset > 0 {
if start_offset >= data.len() {
eprintln!("Offset {} exceeds file size {}", start_offset, data.len());
std::process::exit(1);
}
&data[start_offset..]
} else {
&data
};
// Parse ASN.1 tree
match parse_tree(parse_data, 0, start_offset) {
Ok(_) => {}
Err(e) => {
eprintln!("Error parsing ASN.1: {:?}", e);
std::process::exit(1);
}
}
}