xref: /freebsd/contrib/file/src/der.c (revision 898496ee09ed2b7d25f6807edc4515628196ec0a)
13e41d09dSXin LI /*-
23e41d09dSXin LI  * Copyright (c) 2016 Christos Zoulas
33e41d09dSXin LI  * All rights reserved.
43e41d09dSXin LI  *
53e41d09dSXin LI  * Redistribution and use in source and binary forms, with or without
63e41d09dSXin LI  * modification, are permitted provided that the following conditions
73e41d09dSXin LI  * are met:
83e41d09dSXin LI  * 1. Redistributions of source code must retain the above copyright
93e41d09dSXin LI  *    notice, this list of conditions and the following disclaimer.
103e41d09dSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
113e41d09dSXin LI  *    notice, this list of conditions and the following disclaimer in the
123e41d09dSXin LI  *    documentation and/or other materials provided with the distribution.
133e41d09dSXin LI  *
143e41d09dSXin LI  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
153e41d09dSXin LI  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
163e41d09dSXin LI  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
173e41d09dSXin LI  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
183e41d09dSXin LI  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
193e41d09dSXin LI  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
203e41d09dSXin LI  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
213e41d09dSXin LI  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
223e41d09dSXin LI  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
233e41d09dSXin LI  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
243e41d09dSXin LI  * POSSIBILITY OF SUCH DAMAGE.
253e41d09dSXin LI  */
263e41d09dSXin LI /*
273e41d09dSXin LI  * DER (Distinguished Encoding Rules) Parser
283e41d09dSXin LI  *
293e41d09dSXin LI  * Sources:
303e41d09dSXin LI  * https://en.wikipedia.org/wiki/X.690
313e41d09dSXin LI  * http://fm4dd.com/openssl/certexamples.htm
323e41d09dSXin LI  * http://blog.engelke.com/2014/10/17/parsing-ber-and-der-encoded-asn-1-objects/
333e41d09dSXin LI  */
343e41d09dSXin LI #ifndef TEST_DER
353e41d09dSXin LI #include "file.h"
363e41d09dSXin LI 
373e41d09dSXin LI #ifndef lint
38*898496eeSXin LI FILE_RCSID("@(#)$File: der.c,v 1.27 2022/09/24 20:30:13 christos Exp $")
393e41d09dSXin LI #endif
402726a701SXin LI #else
412726a701SXin LI #define SIZE_T_FORMAT "z"
422726a701SXin LI #define CAST(a, b) ((a)(b))
433e41d09dSXin LI #endif
443e41d09dSXin LI 
453e41d09dSXin LI #include <sys/types.h>
463e41d09dSXin LI 
473e41d09dSXin LI #include <stdio.h>
483e41d09dSXin LI #include <fcntl.h>
493e41d09dSXin LI #include <stdlib.h>
503e41d09dSXin LI #include <string.h>
513e41d09dSXin LI #include <ctype.h>
523e41d09dSXin LI 
533e41d09dSXin LI #ifndef TEST_DER
543e41d09dSXin LI #include "magic.h"
553e41d09dSXin LI #include "der.h"
5620f8619dSXin LI #else
57a5d223e6SXin LI #include <sys/mman.h>
58a5d223e6SXin LI #include <sys/stat.h>
5920f8619dSXin LI #include <err.h>
603e41d09dSXin LI #endif
613e41d09dSXin LI 
6248c779cdSXin LI #define DER_BAD	CAST(uint32_t, -1)
633e41d09dSXin LI 
643e41d09dSXin LI #define DER_CLASS_UNIVERSAL	0
653e41d09dSXin LI #define	DER_CLASS_APPLICATION	1
663e41d09dSXin LI #define	DER_CLASS_CONTEXT	2
673e41d09dSXin LI #define	DER_CLASS_PRIVATE	3
682726a701SXin LI #if defined(DEBUG_DER) || defined(TEST_DER)
693e41d09dSXin LI static const char der_class[] = "UACP";
7020f8619dSXin LI #endif
713e41d09dSXin LI 
723e41d09dSXin LI #define DER_TYPE_PRIMITIVE	0
733e41d09dSXin LI #define DER_TYPE_CONSTRUCTED	1
742726a701SXin LI #if defined(DEBUG_DER) || defined(TEST_DER)
753e41d09dSXin LI static const char der_type[] = "PC";
7620f8619dSXin LI #endif
773e41d09dSXin LI 
783e41d09dSXin LI #define	DER_TAG_EOC			0x00
793e41d09dSXin LI #define	DER_TAG_BOOLEAN			0x01
803e41d09dSXin LI #define	DER_TAG_INTEGER			0x02
813e41d09dSXin LI #define	DER_TAG_BIT STRING		0x03
823e41d09dSXin LI #define	DER_TAG_OCTET_STRING		0x04
833e41d09dSXin LI #define	DER_TAG_NULL			0x05
843e41d09dSXin LI #define	DER_TAG_OBJECT_IDENTIFIER	0x06
853e41d09dSXin LI #define	DER_TAG_OBJECT_DESCRIPTOR	0x07
863e41d09dSXin LI #define	DER_TAG_EXTERNAL		0x08
873e41d09dSXin LI #define	DER_TAG_REAL			0x09
883e41d09dSXin LI #define	DER_TAG_ENUMERATED		0x0a
893e41d09dSXin LI #define	DER_TAG_EMBEDDED_PDV		0x0b
903e41d09dSXin LI #define	DER_TAG_UTF8_STRING		0x0c
913e41d09dSXin LI #define	DER_TAG_RELATIVE_OID		0x0d
922726a701SXin LI #define DER_TAG_TIME			0x0e
933e41d09dSXin LI #define DER_TAG_RESERVED_2		0x0f
943e41d09dSXin LI #define	DER_TAG_SEQUENCE		0x10
953e41d09dSXin LI #define	DER_TAG_SET			0x11
963e41d09dSXin LI #define	DER_TAG_NUMERIC_STRING		0x12
973e41d09dSXin LI #define	DER_TAG_PRINTABLE_STRING	0x13
983e41d09dSXin LI #define	DER_TAG_T61_STRING		0x14
993e41d09dSXin LI #define	DER_TAG_VIDEOTEX_STRING		0x15
1003e41d09dSXin LI #define	DER_TAG_IA5_STRING		0x16
1013e41d09dSXin LI #define	DER_TAG_UTCTIME			0x17
1023e41d09dSXin LI #define	DER_TAG_GENERALIZED_TIME	0x18
1033e41d09dSXin LI #define	DER_TAG_GRAPHIC_STRING		0x19
1043e41d09dSXin LI #define	DER_TAG_VISIBLE_STRING		0x1a
1053e41d09dSXin LI #define	DER_TAG_GENERAL_STRING		0x1b
1063e41d09dSXin LI #define	DER_TAG_UNIVERSAL_STRING	0x1c
1073e41d09dSXin LI #define	DER_TAG_CHARACTER_STRING	0x1d
1083e41d09dSXin LI #define	DER_TAG_BMP_STRING		0x1e
1092726a701SXin LI #define	DER_TAG_DATE			0x1f
1102726a701SXin LI #define	DER_TAG_TIME_OF_DAY		0x20
1112726a701SXin LI #define	DER_TAG_DATE_TIME		0x21
1122726a701SXin LI #define	DER_TAG_DURATION		0x22
1132726a701SXin LI #define	DER_TAG_OID_IRI			0x23
1142726a701SXin LI #define	DER_TAG_RELATIVE_OID_IRI	0x24
1152726a701SXin LI #define	DER_TAG_LAST			0x25
1163e41d09dSXin LI 
1173e41d09dSXin LI static const char *der__tag[] = {
1183e41d09dSXin LI 	"eoc", "bool", "int", "bit_str", "octet_str",
1193e41d09dSXin LI 	"null", "obj_id", "obj_desc", "ext", "real",
1202726a701SXin LI 	"enum", "embed", "utf8_str", "rel_oid", "time",
1213e41d09dSXin LI 	"res2", "seq", "set", "num_str", "prt_str",
1222726a701SXin LI 	"t61_str", "vid_str", "ia5_str", "utc_time", "gen_time",
1232726a701SXin LI 	"gr_str", "vis_str", "gen_str", "univ_str", "char_str",
1242726a701SXin LI 	"bmp_str", "date", "tod", "datetime", "duration",
1252726a701SXin LI 	"oid-iri", "rel-oid-iri",
1263e41d09dSXin LI };
1273e41d09dSXin LI 
1283e41d09dSXin LI #ifdef DEBUG_DER
1293e41d09dSXin LI #define DPRINTF(a) printf a
1303e41d09dSXin LI #else
1313e41d09dSXin LI #define DPRINTF(a)
1323e41d09dSXin LI #endif
1333e41d09dSXin LI 
1343e41d09dSXin LI #ifdef TEST_DER
1353e41d09dSXin LI static uint8_t
getclass(uint8_t c)1363e41d09dSXin LI getclass(uint8_t c)
1373e41d09dSXin LI {
1383e41d09dSXin LI 	return c >> 6;
1393e41d09dSXin LI }
1403e41d09dSXin LI 
1413e41d09dSXin LI static uint8_t
gettype(uint8_t c)1423e41d09dSXin LI gettype(uint8_t c)
1433e41d09dSXin LI {
1443e41d09dSXin LI 	return (c >> 5) & 1;
1453e41d09dSXin LI }
1463e41d09dSXin LI #endif
1473e41d09dSXin LI 
1483e41d09dSXin LI static uint32_t
gettag(const uint8_t * c,size_t * p,size_t l)1493e41d09dSXin LI gettag(const uint8_t *c, size_t *p, size_t l)
1503e41d09dSXin LI {
1513e41d09dSXin LI 	uint32_t tag;
1523e41d09dSXin LI 
1533e41d09dSXin LI 	if (*p >= l)
1543e41d09dSXin LI 		return DER_BAD;
1553e41d09dSXin LI 
1563e41d09dSXin LI 	tag = c[(*p)++] & 0x1f;
1573e41d09dSXin LI 
1583e41d09dSXin LI 	if (tag != 0x1f)
1593e41d09dSXin LI 		return tag;
1603e41d09dSXin LI 
1613e41d09dSXin LI 	if (*p >= l)
1623e41d09dSXin LI 		return DER_BAD;
1633e41d09dSXin LI 
1643e41d09dSXin LI 	while (c[*p] >= 0x80) {
1653e41d09dSXin LI 		tag = tag * 128 + c[(*p)++] - 0x80;
1663e41d09dSXin LI 		if (*p >= l)
1673e41d09dSXin LI 			return DER_BAD;
1683e41d09dSXin LI 	}
1693e41d09dSXin LI 	return tag;
1703e41d09dSXin LI }
1713e41d09dSXin LI 
17240427ccaSGordon Tetlow /*
17340427ccaSGordon Tetlow  * Read the length of a DER tag from the input.
17440427ccaSGordon Tetlow  *
17540427ccaSGordon Tetlow  * `c` is the input, `p` is an output parameter that specifies how much of the
17640427ccaSGordon Tetlow  * input we consumed, and `l` is the maximum input length.
17740427ccaSGordon Tetlow  *
17840427ccaSGordon Tetlow  * Returns the length, or DER_BAD if the end of the input is reached or the
17940427ccaSGordon Tetlow  * length exceeds the remaining input.
18040427ccaSGordon Tetlow  */
1813e41d09dSXin LI static uint32_t
getlength(const uint8_t * c,size_t * p,size_t l)1823e41d09dSXin LI getlength(const uint8_t *c, size_t *p, size_t l)
1833e41d09dSXin LI {
1843e41d09dSXin LI 	uint8_t digits, i;
1853e41d09dSXin LI 	size_t len;
18640427ccaSGordon Tetlow 	int is_onebyte_result;
1873e41d09dSXin LI 
1882726a701SXin LI 	if (*p >= l) {
1892726a701SXin LI 		DPRINTF(("%s:[1] %zu >= %zu\n", __func__, *p, l));
1903e41d09dSXin LI 		return DER_BAD;
1912726a701SXin LI 	}
1923e41d09dSXin LI 
19340427ccaSGordon Tetlow 	/*
19440427ccaSGordon Tetlow 	 * Digits can either be 0b0 followed by the result, or 0b1
19540427ccaSGordon Tetlow 	 * followed by the number of digits of the result. In either case,
19640427ccaSGordon Tetlow 	 * we verify that we can read so many bytes from the input.
19740427ccaSGordon Tetlow 	 */
19840427ccaSGordon Tetlow 	is_onebyte_result = (c[*p] & 0x80) == 0;
19940427ccaSGordon Tetlow 	digits = c[(*p)++] & 0x7f;
2002726a701SXin LI 	if (*p + digits >= l) {
2012726a701SXin LI 		DPRINTF(("%s:[2] %zu + %u >= %zu\n", __func__, *p, digits, l));
2023e41d09dSXin LI 		return DER_BAD;
2032726a701SXin LI 	}
2043e41d09dSXin LI 
20540427ccaSGordon Tetlow 	if (is_onebyte_result)
20640427ccaSGordon Tetlow 		return digits;
20740427ccaSGordon Tetlow 
20840427ccaSGordon Tetlow 	/*
20940427ccaSGordon Tetlow 	 * Decode len. We've already verified that we're allowed to read
21040427ccaSGordon Tetlow 	 * `digits` bytes.
21140427ccaSGordon Tetlow 	 */
21240427ccaSGordon Tetlow 	len = 0;
2133e41d09dSXin LI 	for (i = 0; i < digits; i++)
2143e41d09dSXin LI 		len = (len << 8) | c[(*p)++];
21540427ccaSGordon Tetlow 
2162726a701SXin LI 	if (len > UINT32_MAX - *p || *p + len > l) {
2172726a701SXin LI 		DPRINTF(("%s:[3] bad len %zu + %zu >= %zu\n",
2182726a701SXin LI 		    __func__, *p, len, l));
219a5d223e6SXin LI 		return DER_BAD;
2202726a701SXin LI 	}
22140427ccaSGordon Tetlow 	return CAST(uint32_t, len);
2223e41d09dSXin LI }
2233e41d09dSXin LI 
2243e41d09dSXin LI static const char *
der_tag(char * buf,size_t len,uint32_t tag)2253e41d09dSXin LI der_tag(char *buf, size_t len, uint32_t tag)
2263e41d09dSXin LI {
2272726a701SXin LI 	if (tag < DER_TAG_LAST)
2283e41d09dSXin LI 		strlcpy(buf, der__tag[tag], len);
2293e41d09dSXin LI 	else
2303e41d09dSXin LI 		snprintf(buf, len, "%#x", tag);
2313e41d09dSXin LI 	return buf;
2323e41d09dSXin LI }
2333e41d09dSXin LI 
2343e41d09dSXin LI #ifndef TEST_DER
2353e41d09dSXin LI static int
der_data(char * buf,size_t blen,uint32_t tag,const void * q,uint32_t len)2363e41d09dSXin LI der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len)
2373e41d09dSXin LI {
238a4d6d3b8SXin LI 	uint32_t i;
239a5d223e6SXin LI 	const uint8_t *d = CAST(const uint8_t *, q);
2403e41d09dSXin LI 	switch (tag) {
2413e41d09dSXin LI 	case DER_TAG_PRINTABLE_STRING:
2423e41d09dSXin LI 	case DER_TAG_UTF8_STRING:
2433e41d09dSXin LI 	case DER_TAG_IA5_STRING:
24448c779cdSXin LI 		return snprintf(buf, blen, "%.*s", len, RCAST(const char *, q));
2452726a701SXin LI 	case DER_TAG_UTCTIME:
2462726a701SXin LI 		if (len < 12)
2472726a701SXin LI 			break;
2482726a701SXin LI 		return snprintf(buf, blen,
2492726a701SXin LI 		    "20%c%c-%c%c-%c%c %c%c:%c%c:%c%c GMT", d[0], d[1], d[2],
2502726a701SXin LI 		    d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10], d[11]);
2513e41d09dSXin LI 	default:
2523e41d09dSXin LI 		break;
2533e41d09dSXin LI 	}
2543e41d09dSXin LI 
255a4d6d3b8SXin LI 	for (i = 0; i < len; i++) {
2563e41d09dSXin LI 		uint32_t z = i << 1;
2573e41d09dSXin LI 		if (z < blen - 2)
2583e41d09dSXin LI 			snprintf(buf + z, blen - z, "%.2x", d[i]);
2593e41d09dSXin LI 	}
2603e41d09dSXin LI 	return len * 2;
2613e41d09dSXin LI }
2623e41d09dSXin LI 
2633e41d09dSXin LI int32_t
der_offs(struct magic_set * ms,struct magic * m,size_t nbytes)2643e41d09dSXin LI der_offs(struct magic_set *ms, struct magic *m, size_t nbytes)
2653e41d09dSXin LI {
266a5d223e6SXin LI 	const uint8_t *b = RCAST(const uint8_t *, ms->search.s);
267282e23f0SXin LI 	size_t offs = 0, len = ms->search.s_len ? ms->search.s_len : nbytes;
2683e41d09dSXin LI 
2692726a701SXin LI 	if (gettag(b, &offs, len) == DER_BAD) {
2702726a701SXin LI 		DPRINTF(("%s: bad tag 1\n", __func__));
2713e41d09dSXin LI 		return -1;
2722726a701SXin LI 	}
273*898496eeSXin LI 	DPRINTF(("%s1: %u %" SIZE_T_FORMAT "u %d\n", __func__, ms->offset,
27448c779cdSXin LI 	    offs, m->offset));
2753e41d09dSXin LI 
2763e41d09dSXin LI 	uint32_t tlen = getlength(b, &offs, len);
2772726a701SXin LI 	if (tlen == DER_BAD) {
2782726a701SXin LI 		DPRINTF(("%s: bad tag 2\n", __func__));
2793e41d09dSXin LI 		return -1;
2802726a701SXin LI 	}
281*898496eeSXin LI 	DPRINTF(("%s2: %u %" SIZE_T_FORMAT "u %u\n", __func__, ms->offset,
28248c779cdSXin LI 	    offs, tlen));
2833e41d09dSXin LI 
2843e41d09dSXin LI 	offs += ms->offset + m->offset;
2853e41d09dSXin LI 	DPRINTF(("cont_level = %d\n", m->cont_level));
2863e41d09dSXin LI #ifdef DEBUG_DER
287a4d6d3b8SXin LI 	size_t i;
288a4d6d3b8SXin LI 	for (i = 0; i < m->cont_level; i++)
289*898496eeSXin LI 		printf("cont_level[%" SIZE_T_FORMAT "u] = %d\n", i,
29048c779cdSXin LI 		    ms->c.li[i].off);
2913e41d09dSXin LI #endif
2923e41d09dSXin LI 	if (m->cont_level != 0) {
2933e41d09dSXin LI 		if (offs + tlen > nbytes)
29440427ccaSGordon Tetlow 			return -1;
29540427ccaSGordon Tetlow 		ms->c.li[m->cont_level - 1].off = CAST(int, offs + tlen);
296*898496eeSXin LI 		DPRINTF(("cont_level[%u] = %d\n", m->cont_level - 1,
2973e41d09dSXin LI 		    ms->c.li[m->cont_level - 1].off));
2983e41d09dSXin LI 	}
29940427ccaSGordon Tetlow 	return CAST(int32_t, offs);
3003e41d09dSXin LI }
3013e41d09dSXin LI 
3023e41d09dSXin LI int
der_cmp(struct magic_set * ms,struct magic * m)3033e41d09dSXin LI der_cmp(struct magic_set *ms, struct magic *m)
3043e41d09dSXin LI {
305a5d223e6SXin LI 	const uint8_t *b = RCAST(const uint8_t *, ms->search.s);
3063e41d09dSXin LI 	const char *s = m->value.s;
3073e41d09dSXin LI 	size_t offs = 0, len = ms->search.s_len;
3083e41d09dSXin LI 	uint32_t tag, tlen;
3093e41d09dSXin LI 	char buf[128];
3103e41d09dSXin LI 
3112726a701SXin LI 	DPRINTF(("%s: compare %zu bytes\n", __func__, len));
3122726a701SXin LI 
3133e41d09dSXin LI 	tag = gettag(b, &offs, len);
3142726a701SXin LI 	if (tag == DER_BAD) {
3152726a701SXin LI 		DPRINTF(("%s: bad tag 1\n", __func__));
3163e41d09dSXin LI 		return -1;
3172726a701SXin LI 	}
3182726a701SXin LI 
319*898496eeSXin LI 	DPRINTF(("%s1: %d %" SIZE_T_FORMAT "u %d\n", __func__, ms->offset,
3202726a701SXin LI 	    offs, m->offset));
3213e41d09dSXin LI 
3223e41d09dSXin LI 	tlen = getlength(b, &offs, len);
3232726a701SXin LI 	if (tlen == DER_BAD) {
3242726a701SXin LI 		DPRINTF(("%s: bad tag 2\n", __func__));
3253e41d09dSXin LI 		return -1;
3262726a701SXin LI 	}
3273e41d09dSXin LI 
3283e41d09dSXin LI 	der_tag(buf, sizeof(buf), tag);
3293e41d09dSXin LI 	if ((ms->flags & MAGIC_DEBUG) != 0)
3303e41d09dSXin LI 		fprintf(stderr, "%s: tag %p got=%s exp=%s\n", __func__, b,
3313e41d09dSXin LI 		    buf, s);
3323e41d09dSXin LI 	size_t slen = strlen(buf);
3333e41d09dSXin LI 
334a2dfb722SXin LI 	if (strncmp(buf, s, slen) != 0) {
335a2dfb722SXin LI 		DPRINTF(("%s: no string match %s != %s\n", __func__, buf, s));
3363e41d09dSXin LI 		return 0;
337a2dfb722SXin LI 	}
3383e41d09dSXin LI 
3393e41d09dSXin LI 	s += slen;
3403e41d09dSXin LI 
3413e41d09dSXin LI again:
3423e41d09dSXin LI 	switch (*s) {
3433e41d09dSXin LI 	case '\0':
344a2dfb722SXin LI 		DPRINTF(("%s: EOF match\n", __func__));
3453e41d09dSXin LI 		return 1;
3463e41d09dSXin LI 	case '=':
3473e41d09dSXin LI 		s++;
3483e41d09dSXin LI 		goto val;
3493e41d09dSXin LI 	default:
350a2dfb722SXin LI 		if (!isdigit(CAST(unsigned char, *s))) {
351a2dfb722SXin LI 			DPRINTF(("%s: no digit %c\n", __func__, *s));
3523e41d09dSXin LI 			return 0;
353a2dfb722SXin LI 		}
3543e41d09dSXin LI 
3553e41d09dSXin LI 		slen = 0;
3563e41d09dSXin LI 		do
3573e41d09dSXin LI 			slen = slen * 10 + *s - '0';
35848c779cdSXin LI 		while (isdigit(CAST(unsigned char, *++s)));
3593e41d09dSXin LI 		if ((ms->flags & MAGIC_DEBUG) != 0)
36048c779cdSXin LI 			fprintf(stderr, "%s: len %" SIZE_T_FORMAT "u %u\n",
36148c779cdSXin LI 			    __func__, slen, tlen);
362a2dfb722SXin LI 		if (tlen != slen) {
363a2dfb722SXin LI 			DPRINTF(("%s: len %u != %zu\n", __func__, tlen, slen));
3643e41d09dSXin LI 			return 0;
365a2dfb722SXin LI 		}
3663e41d09dSXin LI 		goto again;
3673e41d09dSXin LI 	}
3683e41d09dSXin LI val:
36948c779cdSXin LI 	DPRINTF(("%s: before data %" SIZE_T_FORMAT "u %u\n", __func__, offs,
37048c779cdSXin LI 	    tlen));
3713e41d09dSXin LI 	der_data(buf, sizeof(buf), tag, b + offs, tlen);
3723e41d09dSXin LI 	if ((ms->flags & MAGIC_DEBUG) != 0)
3733e41d09dSXin LI 		fprintf(stderr, "%s: data %s %s\n", __func__, buf, s);
374a2dfb722SXin LI 	if (strcmp(buf, s) != 0 && strcmp("x", s) != 0) {
375a2dfb722SXin LI 		DPRINTF(("%s: no string match %s != %s\n", __func__, buf, s));
3763e41d09dSXin LI 		return 0;
377a2dfb722SXin LI 	}
3783e41d09dSXin LI 	strlcpy(ms->ms_value.s, buf, sizeof(ms->ms_value.s));
379a2dfb722SXin LI 	DPRINTF(("%s: complete match\n", __func__));
3803e41d09dSXin LI 	return 1;
3813e41d09dSXin LI }
3823e41d09dSXin LI #endif
3833e41d09dSXin LI 
3843e41d09dSXin LI #ifdef TEST_DER
3853e41d09dSXin LI static void
printtag(uint32_t tag,const void * q,uint32_t len)3863e41d09dSXin LI printtag(uint32_t tag, const void *q, uint32_t len)
3873e41d09dSXin LI {
3883e41d09dSXin LI 	const uint8_t *d = q;
3893e41d09dSXin LI 	switch (tag) {
3903e41d09dSXin LI 	case DER_TAG_PRINTABLE_STRING:
3913e41d09dSXin LI 	case DER_TAG_UTF8_STRING:
3922726a701SXin LI 	case DER_TAG_IA5_STRING:
3932726a701SXin LI 	case DER_TAG_UTCTIME:
3943e41d09dSXin LI 		printf("%.*s\n", len, (const char *)q);
3953e41d09dSXin LI 		return;
3963e41d09dSXin LI 	default:
3973e41d09dSXin LI 		break;
3983e41d09dSXin LI 	}
3993e41d09dSXin LI 
4003e41d09dSXin LI 	for (uint32_t i = 0; i < len; i++)
4013e41d09dSXin LI 		printf("%.2x", d[i]);
4023e41d09dSXin LI 	printf("\n");
4033e41d09dSXin LI }
4043e41d09dSXin LI 
4053e41d09dSXin LI static void
printdata(size_t level,const void * v,size_t x,size_t l)4063e41d09dSXin LI printdata(size_t level, const void *v, size_t x, size_t l)
4073e41d09dSXin LI {
4083e41d09dSXin LI 	const uint8_t *p = v, *ep = p + l;
4093e41d09dSXin LI 	size_t ox;
4103e41d09dSXin LI 	char buf[128];
4113e41d09dSXin LI 
4123e41d09dSXin LI 	while (p + x < ep) {
4133e41d09dSXin LI 		const uint8_t *q;
4143e41d09dSXin LI 		uint8_t c = getclass(p[x]);
4153e41d09dSXin LI 		uint8_t t = gettype(p[x]);
4163e41d09dSXin LI 		ox = x;
4172726a701SXin LI //		if (x != 0)
4182726a701SXin LI //		printf("%.2x %.2x %.2x\n", p[x - 1], p[x], p[x + 1]);
4193e41d09dSXin LI 		uint32_t tag = gettag(p, &x, ep - p + x);
4203e41d09dSXin LI 		if (p + x >= ep)
4213e41d09dSXin LI 			break;
4223e41d09dSXin LI 		uint32_t len = getlength(p, &x, ep - p + x);
4233e41d09dSXin LI 
42448c779cdSXin LI 		printf("%" SIZE_T_FORMAT "u %" SIZE_T_FORMAT "u-%"
42548c779cdSXin LI 		    SIZE_T_FORMAT "u %c,%c,%s,%u:", level, ox, x,
4263e41d09dSXin LI 		    der_class[c], der_type[t],
4273e41d09dSXin LI 		    der_tag(buf, sizeof(buf), tag), len);
4283e41d09dSXin LI 		q = p + x;
4293e41d09dSXin LI 		if (p + len > ep)
4303e41d09dSXin LI 			errx(EXIT_FAILURE, "corrupt der");
4313e41d09dSXin LI 		printtag(tag, q, len);
4323e41d09dSXin LI 		if (t != DER_TYPE_PRIMITIVE)
4333e41d09dSXin LI 			printdata(level + 1, p, x, len + x);
4343e41d09dSXin LI 		x += len;
4353e41d09dSXin LI 	}
4363e41d09dSXin LI }
4373e41d09dSXin LI 
4383e41d09dSXin LI int
main(int argc,char * argv[])4393e41d09dSXin LI main(int argc, char *argv[])
4403e41d09dSXin LI {
4413e41d09dSXin LI 	int fd;
4423e41d09dSXin LI 	struct stat st;
4433e41d09dSXin LI 	size_t l;
4443e41d09dSXin LI 	void *p;
4453e41d09dSXin LI 
4463e41d09dSXin LI 	if ((fd = open(argv[1], O_RDONLY)) == -1)
4473e41d09dSXin LI 		err(EXIT_FAILURE, "open `%s'", argv[1]);
4483e41d09dSXin LI 	if (fstat(fd, &st) == -1)
4493e41d09dSXin LI 		err(EXIT_FAILURE, "stat `%s'", argv[1]);
4503e41d09dSXin LI 	l = (size_t)st.st_size;
4513e41d09dSXin LI 	if ((p = mmap(NULL, l, PROT_READ, MAP_FILE, fd, 0)) == MAP_FAILED)
4523e41d09dSXin LI 		err(EXIT_FAILURE, "mmap `%s'", argv[1]);
4533e41d09dSXin LI 
4543e41d09dSXin LI 	printdata(0, p, 0, l);
4553e41d09dSXin LI 	munmap(p, l);
4563e41d09dSXin LI 	return 0;
4573e41d09dSXin LI }
4583e41d09dSXin LI #endif
459