2 * Copyright (c) 2016 Christos Zoulas
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
27 * DER (Distinguished Encoding Rules) Parser
30 * https://en.wikipedia.org/wiki/X.690
31 * http://fm4dd.com/openssl/certexamples.htm
32 * http://blog.engelke.com/2014/10/17/parsing-ber-and-der-encoded-asn-1-objects/
38 FILE_RCSID("@(#)$File: der.c,v 1.4 2016/03/21 23:04:40 christos Exp $")
42 #include <sys/types.h>
58 #define DER_BAD ((uint32_t)-1)
60 #define DER_CLASS_UNIVERSAL 0
61 #define DER_CLASS_APPLICATION 1
62 #define DER_CLASS_CONTEXT 2
63 #define DER_CLASS_PRIVATE 3
64 static const char der_class[] = "UACP";
66 #define DER_TYPE_PRIMITIVE 0
67 #define DER_TYPE_CONSTRUCTED 1
68 static const char der_type[] = "PC";
70 #define DER_TAG_EOC 0x00
71 #define DER_TAG_BOOLEAN 0x01
72 #define DER_TAG_INTEGER 0x02
73 #define DER_TAG_BIT STRING 0x03
74 #define DER_TAG_OCTET_STRING 0x04
75 #define DER_TAG_NULL 0x05
76 #define DER_TAG_OBJECT_IDENTIFIER 0x06
77 #define DER_TAG_OBJECT_DESCRIPTOR 0x07
78 #define DER_TAG_EXTERNAL 0x08
79 #define DER_TAG_REAL 0x09
80 #define DER_TAG_ENUMERATED 0x0a
81 #define DER_TAG_EMBEDDED_PDV 0x0b
82 #define DER_TAG_UTF8_STRING 0x0c
83 #define DER_TAG_RELATIVE_OID 0x0d
84 #define DER_TAG_RESERVED_1 0x0e
85 #define DER_TAG_RESERVED_2 0x0f
86 #define DER_TAG_SEQUENCE 0x10
87 #define DER_TAG_SET 0x11
88 #define DER_TAG_NUMERIC_STRING 0x12
89 #define DER_TAG_PRINTABLE_STRING 0x13
90 #define DER_TAG_T61_STRING 0x14
91 #define DER_TAG_VIDEOTEX_STRING 0x15
92 #define DER_TAG_IA5_STRING 0x16
93 #define DER_TAG_UTCTIME 0x17
94 #define DER_TAG_GENERALIZED_TIME 0x18
95 #define DER_TAG_GRAPHIC_STRING 0x19
96 #define DER_TAG_VISIBLE_STRING 0x1a
97 #define DER_TAG_GENERAL_STRING 0x1b
98 #define DER_TAG_UNIVERSAL_STRING 0x1c
99 #define DER_TAG_CHARACTER_STRING 0x1d
100 #define DER_TAG_BMP_STRING 0x1e
101 #define DER_TAG_LONG 0x1f
103 static const char *der__tag[] = {
104 "eoc", "bool", "int", "bit_str", "octet_str",
105 "null", "obj_id", "obj_desc", "ext", "real",
106 "enum", "embed", "utf8_str", "oid", "res1",
107 "res2", "seq", "set", "num_str", "prt_str",
108 "t61_str", "vid_str", "ia5_str", "utc_time",
109 "gen_time", "gr_str", "vis_str", "gen_str",
110 "char_str", "bmp_str", "long"
114 #define DPRINTF(a) printf a
134 gettag(const uint8_t *c, size_t *p, size_t l)
141 tag = c[(*p)++] & 0x1f;
149 while (c[*p] >= 0x80) {
150 tag = tag * 128 + c[(*p)++] - 0x80;
158 getlength(const uint8_t *c, size_t *p, size_t l)
168 if ((digits & 0x80) == 0)
174 if (*p + digits >= l)
177 for (i = 0; i < digits; i++)
178 len = (len << 8) | c[(*p)++];
183 der_tag(char *buf, size_t len, uint32_t tag)
185 if (tag < DER_TAG_LONG)
186 strlcpy(buf, der__tag[tag], len);
188 snprintf(buf, len, "%#x", tag);
194 der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len)
196 const uint8_t *d = q;
198 case DER_TAG_PRINTABLE_STRING:
199 case DER_TAG_UTF8_STRING:
200 case DER_TAG_IA5_STRING:
201 case DER_TAG_UTCTIME:
202 return snprintf(buf, blen, "%.*s", len, (const char *)q);
207 for (uint32_t i = 0; i < len; i++) {
210 snprintf(buf + z, blen - z, "%.2x", d[i]);
216 der_offs(struct magic_set *ms, struct magic *m, size_t nbytes)
218 const uint8_t *b = CAST(const void *, ms->search.s);
219 size_t offs = 0, len = ms->search.rm_len ? ms->search.rm_len : nbytes;
221 if (gettag(b, &offs, len) == DER_BAD)
223 DPRINTF(("%s1: %d %zu %u\n", __func__, ms->offset, offs, m->offset));
225 uint32_t tlen = getlength(b, &offs, len);
228 DPRINTF(("%s2: %d %zu %u\n", __func__, ms->offset, offs, tlen));
230 offs += ms->offset + m->offset;
231 DPRINTF(("cont_level = %d\n", m->cont_level));
233 for (size_t i = 0; i < m->cont_level; i++)
234 printf("cont_level[%zu] = %u\n", i, ms->c.li[i].off);
236 if (m->cont_level != 0) {
237 if (offs + tlen > nbytes)
239 ms->c.li[m->cont_level - 1].off = offs + tlen;
240 DPRINTF(("cont_level[%u] = %u\n", m->cont_level - 1,
241 ms->c.li[m->cont_level - 1].off));
247 der_cmp(struct magic_set *ms, struct magic *m)
249 const uint8_t *b = CAST(const void *, ms->search.s);
250 const char *s = m->value.s;
251 size_t offs = 0, len = ms->search.s_len;
255 tag = gettag(b, &offs, len);
259 tlen = getlength(b, &offs, len);
263 der_tag(buf, sizeof(buf), tag);
264 if ((ms->flags & MAGIC_DEBUG) != 0)
265 fprintf(stderr, "%s: tag %p got=%s exp=%s\n", __func__, b,
267 size_t slen = strlen(buf);
269 if (strncmp(buf, s, slen) != 0)
282 if (!isdigit((unsigned char)*s))
287 slen = slen * 10 + *s - '0';
288 while (isdigit((unsigned char)*++s));
289 if ((ms->flags & MAGIC_DEBUG) != 0)
290 fprintf(stderr, "%s: len %zu %u\n", __func__,
297 DPRINTF(("%s: before data %zu %u\n", __func__, offs, tlen));
298 der_data(buf, sizeof(buf), tag, b + offs, tlen);
299 if ((ms->flags & MAGIC_DEBUG) != 0)
300 fprintf(stderr, "%s: data %s %s\n", __func__, buf, s);
301 if (strcmp(buf, s) != 0 && strcmp("x", s) != 0)
303 strlcpy(ms->ms_value.s, buf, sizeof(ms->ms_value.s));
310 printtag(uint32_t tag, const void *q, uint32_t len)
312 const uint8_t *d = q;
314 case DER_TAG_PRINTABLE_STRING:
315 case DER_TAG_UTF8_STRING:
316 printf("%.*s\n", len, (const char *)q);
322 for (uint32_t i = 0; i < len; i++)
323 printf("%.2x", d[i]);
328 printdata(size_t level, const void *v, size_t x, size_t l)
330 const uint8_t *p = v, *ep = p + l;
336 uint8_t c = getclass(p[x]);
337 uint8_t t = gettype(p[x]);
340 printf("%.2x %.2x %.2x\n", p[x - 1], p[x], p[x + 1]);
341 uint32_t tag = gettag(p, &x, ep - p + x);
344 uint32_t len = getlength(p, &x, ep - p + x);
346 printf("%zu %zu-%zu %c,%c,%s,%u:", level, ox, x,
347 der_class[c], der_type[t],
348 der_tag(buf, sizeof(buf), tag), len);
351 errx(EXIT_FAILURE, "corrupt der");
352 printtag(tag, q, len);
353 if (t != DER_TYPE_PRIMITIVE)
354 printdata(level + 1, p, x, len + x);
360 main(int argc, char *argv[])
367 if ((fd = open(argv[1], O_RDONLY)) == -1)
368 err(EXIT_FAILURE, "open `%s'", argv[1]);
369 if (fstat(fd, &st) == -1)
370 err(EXIT_FAILURE, "stat `%s'", argv[1]);
371 l = (size_t)st.st_size;
372 if ((p = mmap(NULL, l, PROT_READ, MAP_FILE, fd, 0)) == MAP_FAILED)
373 err(EXIT_FAILURE, "mmap `%s'", argv[1]);
375 printdata(0, p, 0, l);