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.20 2020/06/07 19:10:37 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 break; 251 default: 252 break; 253 } 254 255 for (uint32_t i = 0; i < len; i++) { 256 uint32_t z = i << 1; 257 if (z < blen - 2) 258 snprintf(buf + z, blen - z, "%.2x", d[i]); 259 } 260 return len * 2; 261 } 262 263 int32_t 264 der_offs(struct magic_set *ms, struct magic *m, size_t nbytes) 265 { 266 const uint8_t *b = RCAST(const uint8_t *, ms->search.s); 267 size_t offs = 0, len = ms->search.s_len ? ms->search.s_len : nbytes; 268 269 if (gettag(b, &offs, len) == DER_BAD) { 270 DPRINTF(("%s: bad tag 1\n", __func__)); 271 return -1; 272 } 273 DPRINTF(("%s1: %d %" SIZE_T_FORMAT "u %u\n", __func__, ms->offset, 274 offs, m->offset)); 275 276 uint32_t tlen = getlength(b, &offs, len); 277 if (tlen == DER_BAD) { 278 DPRINTF(("%s: bad tag 2\n", __func__)); 279 return -1; 280 } 281 DPRINTF(("%s2: %d %" SIZE_T_FORMAT "u %u\n", __func__, ms->offset, 282 offs, tlen)); 283 284 offs += ms->offset + m->offset; 285 DPRINTF(("cont_level = %d\n", m->cont_level)); 286 #ifdef DEBUG_DER 287 for (size_t i = 0; i < m->cont_level; i++) 288 printf("cont_level[%" SIZE_T_FORMAT "u] = %u\n", i, 289 ms->c.li[i].off); 290 #endif 291 if (m->cont_level != 0) { 292 if (offs + tlen > nbytes) 293 return -1; 294 ms->c.li[m->cont_level - 1].off = CAST(int, offs + tlen); 295 DPRINTF(("cont_level[%u] = %u\n", m->cont_level - 1, 296 ms->c.li[m->cont_level - 1].off)); 297 } 298 return CAST(int32_t, offs); 299 } 300 301 int 302 der_cmp(struct magic_set *ms, struct magic *m) 303 { 304 const uint8_t *b = RCAST(const uint8_t *, ms->search.s); 305 const char *s = m->value.s; 306 size_t offs = 0, len = ms->search.s_len; 307 uint32_t tag, tlen; 308 char buf[128]; 309 310 DPRINTF(("%s: compare %zu bytes\n", __func__, len)); 311 312 tag = gettag(b, &offs, len); 313 if (tag == DER_BAD) { 314 DPRINTF(("%s: bad tag 1\n", __func__)); 315 return -1; 316 } 317 318 DPRINTF(("%s1: %d %" SIZE_T_FORMAT "u %u\n", __func__, ms->offset, 319 offs, m->offset)); 320 321 tlen = getlength(b, &offs, len); 322 if (tlen == DER_BAD) { 323 DPRINTF(("%s: bad tag 2\n", __func__)); 324 return -1; 325 } 326 327 der_tag(buf, sizeof(buf), tag); 328 if ((ms->flags & MAGIC_DEBUG) != 0) 329 fprintf(stderr, "%s: tag %p got=%s exp=%s\n", __func__, b, 330 buf, s); 331 size_t slen = strlen(buf); 332 333 if (strncmp(buf, s, slen) != 0) 334 return 0; 335 336 s += slen; 337 338 again: 339 switch (*s) { 340 case '\0': 341 return 1; 342 case '=': 343 s++; 344 goto val; 345 default: 346 if (!isdigit(CAST(unsigned char, *s))) 347 return 0; 348 349 slen = 0; 350 do 351 slen = slen * 10 + *s - '0'; 352 while (isdigit(CAST(unsigned char, *++s))); 353 if ((ms->flags & MAGIC_DEBUG) != 0) 354 fprintf(stderr, "%s: len %" SIZE_T_FORMAT "u %u\n", 355 __func__, slen, tlen); 356 if (tlen != slen) 357 return 0; 358 goto again; 359 } 360 val: 361 DPRINTF(("%s: before data %" SIZE_T_FORMAT "u %u\n", __func__, offs, 362 tlen)); 363 der_data(buf, sizeof(buf), tag, b + offs, tlen); 364 if ((ms->flags & MAGIC_DEBUG) != 0) 365 fprintf(stderr, "%s: data %s %s\n", __func__, buf, s); 366 if (strcmp(buf, s) != 0 && strcmp("x", s) != 0) 367 return 0; 368 strlcpy(ms->ms_value.s, buf, sizeof(ms->ms_value.s)); 369 return 1; 370 } 371 #endif 372 373 #ifdef TEST_DER 374 static void 375 printtag(uint32_t tag, const void *q, uint32_t len) 376 { 377 const uint8_t *d = q; 378 switch (tag) { 379 case DER_TAG_PRINTABLE_STRING: 380 case DER_TAG_UTF8_STRING: 381 case DER_TAG_IA5_STRING: 382 case DER_TAG_UTCTIME: 383 printf("%.*s\n", len, (const char *)q); 384 return; 385 default: 386 break; 387 } 388 389 for (uint32_t i = 0; i < len; i++) 390 printf("%.2x", d[i]); 391 printf("\n"); 392 } 393 394 static void 395 printdata(size_t level, const void *v, size_t x, size_t l) 396 { 397 const uint8_t *p = v, *ep = p + l; 398 size_t ox; 399 char buf[128]; 400 401 while (p + x < ep) { 402 const uint8_t *q; 403 uint8_t c = getclass(p[x]); 404 uint8_t t = gettype(p[x]); 405 ox = x; 406 // if (x != 0) 407 // printf("%.2x %.2x %.2x\n", p[x - 1], p[x], p[x + 1]); 408 uint32_t tag = gettag(p, &x, ep - p + x); 409 if (p + x >= ep) 410 break; 411 uint32_t len = getlength(p, &x, ep - p + x); 412 413 printf("%" SIZE_T_FORMAT "u %" SIZE_T_FORMAT "u-%" 414 SIZE_T_FORMAT "u %c,%c,%s,%u:", level, ox, x, 415 der_class[c], der_type[t], 416 der_tag(buf, sizeof(buf), tag), len); 417 q = p + x; 418 if (p + len > ep) 419 errx(EXIT_FAILURE, "corrupt der"); 420 printtag(tag, q, len); 421 if (t != DER_TYPE_PRIMITIVE) 422 printdata(level + 1, p, x, len + x); 423 x += len; 424 } 425 } 426 427 int 428 main(int argc, char *argv[]) 429 { 430 int fd; 431 struct stat st; 432 size_t l; 433 void *p; 434 435 if ((fd = open(argv[1], O_RDONLY)) == -1) 436 err(EXIT_FAILURE, "open `%s'", argv[1]); 437 if (fstat(fd, &st) == -1) 438 err(EXIT_FAILURE, "stat `%s'", argv[1]); 439 l = (size_t)st.st_size; 440 if ((p = mmap(NULL, l, PROT_READ, MAP_FILE, fd, 0)) == MAP_FAILED) 441 err(EXIT_FAILURE, "mmap `%s'", argv[1]); 442 443 printdata(0, p, 0, l); 444 munmap(p, l); 445 return 0; 446 } 447 #endif 448