1 /*- 2 * Copyright (c) 2016 Christos Zoulas 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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. 25 */ 26 /* 27 * DER (Distinguished Encoding Rules) Parser 28 * 29 * Sources: 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/ 33 */ 34 #ifndef TEST_DER 35 #include "file.h" 36 37 #ifndef lint 38 FILE_RCSID("@(#)$File: der.c,v 1.21 2020/06/15 00:58:10 christos Exp $") 39 #endif 40 #else 41 #define SIZE_T_FORMAT "z" 42 #define CAST(a, b) ((a)(b)) 43 #endif 44 45 #include <sys/types.h> 46 47 #include <stdio.h> 48 #include <fcntl.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <ctype.h> 52 53 #ifndef TEST_DER 54 #include "magic.h" 55 #include "der.h" 56 #else 57 #include <sys/mman.h> 58 #include <sys/stat.h> 59 #include <err.h> 60 #endif 61 62 #define DER_BAD CAST(uint32_t, -1) 63 64 #define DER_CLASS_UNIVERSAL 0 65 #define DER_CLASS_APPLICATION 1 66 #define DER_CLASS_CONTEXT 2 67 #define DER_CLASS_PRIVATE 3 68 #if defined(DEBUG_DER) || defined(TEST_DER) 69 static const char der_class[] = "UACP"; 70 #endif 71 72 #define DER_TYPE_PRIMITIVE 0 73 #define DER_TYPE_CONSTRUCTED 1 74 #if defined(DEBUG_DER) || defined(TEST_DER) 75 static const char der_type[] = "PC"; 76 #endif 77 78 #define DER_TAG_EOC 0x00 79 #define DER_TAG_BOOLEAN 0x01 80 #define DER_TAG_INTEGER 0x02 81 #define DER_TAG_BIT STRING 0x03 82 #define DER_TAG_OCTET_STRING 0x04 83 #define DER_TAG_NULL 0x05 84 #define DER_TAG_OBJECT_IDENTIFIER 0x06 85 #define DER_TAG_OBJECT_DESCRIPTOR 0x07 86 #define DER_TAG_EXTERNAL 0x08 87 #define DER_TAG_REAL 0x09 88 #define DER_TAG_ENUMERATED 0x0a 89 #define DER_TAG_EMBEDDED_PDV 0x0b 90 #define DER_TAG_UTF8_STRING 0x0c 91 #define DER_TAG_RELATIVE_OID 0x0d 92 #define DER_TAG_TIME 0x0e 93 #define DER_TAG_RESERVED_2 0x0f 94 #define DER_TAG_SEQUENCE 0x10 95 #define DER_TAG_SET 0x11 96 #define DER_TAG_NUMERIC_STRING 0x12 97 #define DER_TAG_PRINTABLE_STRING 0x13 98 #define DER_TAG_T61_STRING 0x14 99 #define DER_TAG_VIDEOTEX_STRING 0x15 100 #define DER_TAG_IA5_STRING 0x16 101 #define DER_TAG_UTCTIME 0x17 102 #define DER_TAG_GENERALIZED_TIME 0x18 103 #define DER_TAG_GRAPHIC_STRING 0x19 104 #define DER_TAG_VISIBLE_STRING 0x1a 105 #define DER_TAG_GENERAL_STRING 0x1b 106 #define DER_TAG_UNIVERSAL_STRING 0x1c 107 #define DER_TAG_CHARACTER_STRING 0x1d 108 #define DER_TAG_BMP_STRING 0x1e 109 #define DER_TAG_DATE 0x1f 110 #define DER_TAG_TIME_OF_DAY 0x20 111 #define DER_TAG_DATE_TIME 0x21 112 #define DER_TAG_DURATION 0x22 113 #define DER_TAG_OID_IRI 0x23 114 #define DER_TAG_RELATIVE_OID_IRI 0x24 115 #define DER_TAG_LAST 0x25 116 117 static const char *der__tag[] = { 118 "eoc", "bool", "int", "bit_str", "octet_str", 119 "null", "obj_id", "obj_desc", "ext", "real", 120 "enum", "embed", "utf8_str", "rel_oid", "time", 121 "res2", "seq", "set", "num_str", "prt_str", 122 "t61_str", "vid_str", "ia5_str", "utc_time", "gen_time", 123 "gr_str", "vis_str", "gen_str", "univ_str", "char_str", 124 "bmp_str", "date", "tod", "datetime", "duration", 125 "oid-iri", "rel-oid-iri", 126 }; 127 128 #ifdef DEBUG_DER 129 #define DPRINTF(a) printf a 130 #else 131 #define DPRINTF(a) 132 #endif 133 134 #ifdef TEST_DER 135 static uint8_t 136 getclass(uint8_t c) 137 { 138 return c >> 6; 139 } 140 141 static uint8_t 142 gettype(uint8_t c) 143 { 144 return (c >> 5) & 1; 145 } 146 #endif 147 148 static uint32_t 149 gettag(const uint8_t *c, size_t *p, size_t l) 150 { 151 uint32_t tag; 152 153 if (*p >= l) 154 return DER_BAD; 155 156 tag = c[(*p)++] & 0x1f; 157 158 if (tag != 0x1f) 159 return tag; 160 161 if (*p >= l) 162 return DER_BAD; 163 164 while (c[*p] >= 0x80) { 165 tag = tag * 128 + c[(*p)++] - 0x80; 166 if (*p >= l) 167 return DER_BAD; 168 } 169 return tag; 170 } 171 172 /* 173 * Read the length of a DER tag from the input. 174 * 175 * `c` is the input, `p` is an output parameter that specifies how much of the 176 * input we consumed, and `l` is the maximum input length. 177 * 178 * Returns the length, or DER_BAD if the end of the input is reached or the 179 * length exceeds the remaining input. 180 */ 181 static uint32_t 182 getlength(const uint8_t *c, size_t *p, size_t l) 183 { 184 uint8_t digits, i; 185 size_t len; 186 int is_onebyte_result; 187 188 if (*p >= l) { 189 DPRINTF(("%s:[1] %zu >= %zu\n", __func__, *p, l)); 190 return DER_BAD; 191 } 192 193 /* 194 * Digits can either be 0b0 followed by the result, or 0b1 195 * followed by the number of digits of the result. In either case, 196 * we verify that we can read so many bytes from the input. 197 */ 198 is_onebyte_result = (c[*p] & 0x80) == 0; 199 digits = c[(*p)++] & 0x7f; 200 if (*p + digits >= l) { 201 DPRINTF(("%s:[2] %zu + %u >= %zu\n", __func__, *p, digits, l)); 202 return DER_BAD; 203 } 204 205 if (is_onebyte_result) 206 return digits; 207 208 /* 209 * Decode len. We've already verified that we're allowed to read 210 * `digits` bytes. 211 */ 212 len = 0; 213 for (i = 0; i < digits; i++) 214 len = (len << 8) | c[(*p)++]; 215 216 if (len > UINT32_MAX - *p || *p + len > l) { 217 DPRINTF(("%s:[3] bad len %zu + %zu >= %zu\n", 218 __func__, *p, len, l)); 219 return DER_BAD; 220 } 221 return CAST(uint32_t, len); 222 } 223 224 static const char * 225 der_tag(char *buf, size_t len, uint32_t tag) 226 { 227 if (tag < DER_TAG_LAST) 228 strlcpy(buf, der__tag[tag], len); 229 else 230 snprintf(buf, len, "%#x", tag); 231 return buf; 232 } 233 234 #ifndef TEST_DER 235 static int 236 der_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len) 237 { 238 const uint8_t *d = CAST(const uint8_t *, q); 239 switch (tag) { 240 case DER_TAG_PRINTABLE_STRING: 241 case DER_TAG_UTF8_STRING: 242 case DER_TAG_IA5_STRING: 243 return snprintf(buf, blen, "%.*s", len, RCAST(const char *, q)); 244 case DER_TAG_UTCTIME: 245 if (len < 12) 246 break; 247 return snprintf(buf, blen, 248 "20%c%c-%c%c-%c%c %c%c:%c%c:%c%c GMT", d[0], d[1], d[2], 249 d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10], d[11]); 250 default: 251 break; 252 } 253 254 for (uint32_t i = 0; i < len; i++) { 255 uint32_t z = i << 1; 256 if (z < blen - 2) 257 snprintf(buf + z, blen - z, "%.2x", d[i]); 258 } 259 return len * 2; 260 } 261 262 int32_t 263 der_offs(struct magic_set *ms, struct magic *m, size_t nbytes) 264 { 265 const uint8_t *b = RCAST(const uint8_t *, ms->search.s); 266 size_t offs = 0, len = ms->search.s_len ? ms->search.s_len : nbytes; 267 268 if (gettag(b, &offs, len) == DER_BAD) { 269 DPRINTF(("%s: bad tag 1\n", __func__)); 270 return -1; 271 } 272 DPRINTF(("%s1: %d %" SIZE_T_FORMAT "u %u\n", __func__, ms->offset, 273 offs, m->offset)); 274 275 uint32_t tlen = getlength(b, &offs, len); 276 if (tlen == DER_BAD) { 277 DPRINTF(("%s: bad tag 2\n", __func__)); 278 return -1; 279 } 280 DPRINTF(("%s2: %d %" SIZE_T_FORMAT "u %u\n", __func__, ms->offset, 281 offs, tlen)); 282 283 offs += ms->offset + m->offset; 284 DPRINTF(("cont_level = %d\n", m->cont_level)); 285 #ifdef DEBUG_DER 286 for (size_t i = 0; i < m->cont_level; i++) 287 printf("cont_level[%" SIZE_T_FORMAT "u] = %u\n", i, 288 ms->c.li[i].off); 289 #endif 290 if (m->cont_level != 0) { 291 if (offs + tlen > nbytes) 292 return -1; 293 ms->c.li[m->cont_level - 1].off = CAST(int, offs + tlen); 294 DPRINTF(("cont_level[%u] = %u\n", m->cont_level - 1, 295 ms->c.li[m->cont_level - 1].off)); 296 } 297 return CAST(int32_t, offs); 298 } 299 300 int 301 der_cmp(struct magic_set *ms, struct magic *m) 302 { 303 const uint8_t *b = RCAST(const uint8_t *, ms->search.s); 304 const char *s = m->value.s; 305 size_t offs = 0, len = ms->search.s_len; 306 uint32_t tag, tlen; 307 char buf[128]; 308 309 DPRINTF(("%s: compare %zu bytes\n", __func__, len)); 310 311 tag = gettag(b, &offs, len); 312 if (tag == DER_BAD) { 313 DPRINTF(("%s: bad tag 1\n", __func__)); 314 return -1; 315 } 316 317 DPRINTF(("%s1: %d %" SIZE_T_FORMAT "u %u\n", __func__, ms->offset, 318 offs, m->offset)); 319 320 tlen = getlength(b, &offs, len); 321 if (tlen == DER_BAD) { 322 DPRINTF(("%s: bad tag 2\n", __func__)); 323 return -1; 324 } 325 326 der_tag(buf, sizeof(buf), tag); 327 if ((ms->flags & MAGIC_DEBUG) != 0) 328 fprintf(stderr, "%s: tag %p got=%s exp=%s\n", __func__, b, 329 buf, s); 330 size_t slen = strlen(buf); 331 332 if (strncmp(buf, s, slen) != 0) 333 return 0; 334 335 s += slen; 336 337 again: 338 switch (*s) { 339 case '\0': 340 return 1; 341 case '=': 342 s++; 343 goto val; 344 default: 345 if (!isdigit(CAST(unsigned char, *s))) 346 return 0; 347 348 slen = 0; 349 do 350 slen = slen * 10 + *s - '0'; 351 while (isdigit(CAST(unsigned char, *++s))); 352 if ((ms->flags & MAGIC_DEBUG) != 0) 353 fprintf(stderr, "%s: len %" SIZE_T_FORMAT "u %u\n", 354 __func__, slen, tlen); 355 if (tlen != slen) 356 return 0; 357 goto again; 358 } 359 val: 360 DPRINTF(("%s: before data %" SIZE_T_FORMAT "u %u\n", __func__, offs, 361 tlen)); 362 der_data(buf, sizeof(buf), tag, b + offs, tlen); 363 if ((ms->flags & MAGIC_DEBUG) != 0) 364 fprintf(stderr, "%s: data %s %s\n", __func__, buf, s); 365 if (strcmp(buf, s) != 0 && strcmp("x", s) != 0) 366 return 0; 367 strlcpy(ms->ms_value.s, buf, sizeof(ms->ms_value.s)); 368 return 1; 369 } 370 #endif 371 372 #ifdef TEST_DER 373 static void 374 printtag(uint32_t tag, const void *q, uint32_t len) 375 { 376 const uint8_t *d = q; 377 switch (tag) { 378 case DER_TAG_PRINTABLE_STRING: 379 case DER_TAG_UTF8_STRING: 380 case DER_TAG_IA5_STRING: 381 case DER_TAG_UTCTIME: 382 printf("%.*s\n", len, (const char *)q); 383 return; 384 default: 385 break; 386 } 387 388 for (uint32_t i = 0; i < len; i++) 389 printf("%.2x", d[i]); 390 printf("\n"); 391 } 392 393 static void 394 printdata(size_t level, const void *v, size_t x, size_t l) 395 { 396 const uint8_t *p = v, *ep = p + l; 397 size_t ox; 398 char buf[128]; 399 400 while (p + x < ep) { 401 const uint8_t *q; 402 uint8_t c = getclass(p[x]); 403 uint8_t t = gettype(p[x]); 404 ox = x; 405 // if (x != 0) 406 // printf("%.2x %.2x %.2x\n", p[x - 1], p[x], p[x + 1]); 407 uint32_t tag = gettag(p, &x, ep - p + x); 408 if (p + x >= ep) 409 break; 410 uint32_t len = getlength(p, &x, ep - p + x); 411 412 printf("%" SIZE_T_FORMAT "u %" SIZE_T_FORMAT "u-%" 413 SIZE_T_FORMAT "u %c,%c,%s,%u:", level, ox, x, 414 der_class[c], der_type[t], 415 der_tag(buf, sizeof(buf), tag), len); 416 q = p + x; 417 if (p + len > ep) 418 errx(EXIT_FAILURE, "corrupt der"); 419 printtag(tag, q, len); 420 if (t != DER_TYPE_PRIMITIVE) 421 printdata(level + 1, p, x, len + x); 422 x += len; 423 } 424 } 425 426 int 427 main(int argc, char *argv[]) 428 { 429 int fd; 430 struct stat st; 431 size_t l; 432 void *p; 433 434 if ((fd = open(argv[1], O_RDONLY)) == -1) 435 err(EXIT_FAILURE, "open `%s'", argv[1]); 436 if (fstat(fd, &st) == -1) 437 err(EXIT_FAILURE, "stat `%s'", argv[1]); 438 l = (size_t)st.st_size; 439 if ((p = mmap(NULL, l, PROT_READ, MAP_FILE, fd, 0)) == MAP_FAILED) 440 err(EXIT_FAILURE, "mmap `%s'", argv[1]); 441 442 printdata(0, p, 0, l); 443 munmap(p, l); 444 return 0; 445 } 446 #endif 447