asn1_decoder.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Decoder for ASN.1 BER/DER/CER encoded bytestream
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #ifdef __UBOOT__
  8. #include <log.h>
  9. #include <linux/compat.h>
  10. #else
  11. #include <linux/export.h>
  12. #endif
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #ifndef __UBOOT__
  16. #include <linux/module.h>
  17. #endif
  18. #include <linux/asn1_decoder.h>
  19. #include <linux/asn1_ber_bytecode.h>
  20. static const unsigned char asn1_op_lengths[ASN1_OP__NR] = {
  21. /* OPC TAG JMP ACT */
  22. [ASN1_OP_MATCH] = 1 + 1,
  23. [ASN1_OP_MATCH_OR_SKIP] = 1 + 1,
  24. [ASN1_OP_MATCH_ACT] = 1 + 1 + 1,
  25. [ASN1_OP_MATCH_ACT_OR_SKIP] = 1 + 1 + 1,
  26. [ASN1_OP_MATCH_JUMP] = 1 + 1 + 1,
  27. [ASN1_OP_MATCH_JUMP_OR_SKIP] = 1 + 1 + 1,
  28. [ASN1_OP_MATCH_ANY] = 1,
  29. [ASN1_OP_MATCH_ANY_OR_SKIP] = 1,
  30. [ASN1_OP_MATCH_ANY_ACT] = 1 + 1,
  31. [ASN1_OP_MATCH_ANY_ACT_OR_SKIP] = 1 + 1,
  32. [ASN1_OP_COND_MATCH_OR_SKIP] = 1 + 1,
  33. [ASN1_OP_COND_MATCH_ACT_OR_SKIP] = 1 + 1 + 1,
  34. [ASN1_OP_COND_MATCH_JUMP_OR_SKIP] = 1 + 1 + 1,
  35. [ASN1_OP_COND_MATCH_ANY] = 1,
  36. [ASN1_OP_COND_MATCH_ANY_OR_SKIP] = 1,
  37. [ASN1_OP_COND_MATCH_ANY_ACT] = 1 + 1,
  38. [ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP] = 1 + 1,
  39. [ASN1_OP_COND_FAIL] = 1,
  40. [ASN1_OP_COMPLETE] = 1,
  41. [ASN1_OP_ACT] = 1 + 1,
  42. [ASN1_OP_MAYBE_ACT] = 1 + 1,
  43. [ASN1_OP_RETURN] = 1,
  44. [ASN1_OP_END_SEQ] = 1,
  45. [ASN1_OP_END_SEQ_OF] = 1 + 1,
  46. [ASN1_OP_END_SET] = 1,
  47. [ASN1_OP_END_SET_OF] = 1 + 1,
  48. [ASN1_OP_END_SEQ_ACT] = 1 + 1,
  49. [ASN1_OP_END_SEQ_OF_ACT] = 1 + 1 + 1,
  50. [ASN1_OP_END_SET_ACT] = 1 + 1,
  51. [ASN1_OP_END_SET_OF_ACT] = 1 + 1 + 1,
  52. };
  53. /*
  54. * Find the length of an indefinite length object
  55. * @data: The data buffer
  56. * @datalen: The end of the innermost containing element in the buffer
  57. * @_dp: The data parse cursor (updated before returning)
  58. * @_len: Where to return the size of the element.
  59. * @_errmsg: Where to return a pointer to an error message on error
  60. */
  61. static int asn1_find_indefinite_length(const unsigned char *data, size_t datalen,
  62. size_t *_dp, size_t *_len,
  63. const char **_errmsg)
  64. {
  65. unsigned char tag, tmp;
  66. size_t dp = *_dp, len, n;
  67. int indef_level = 1;
  68. next_tag:
  69. if (unlikely(datalen - dp < 2)) {
  70. if (datalen == dp)
  71. goto missing_eoc;
  72. goto data_overrun_error;
  73. }
  74. /* Extract a tag from the data */
  75. tag = data[dp++];
  76. if (tag == ASN1_EOC) {
  77. /* It appears to be an EOC. */
  78. if (data[dp++] != 0)
  79. goto invalid_eoc;
  80. if (--indef_level <= 0) {
  81. *_len = dp - *_dp;
  82. *_dp = dp;
  83. return 0;
  84. }
  85. goto next_tag;
  86. }
  87. if (unlikely((tag & 0x1f) == ASN1_LONG_TAG)) {
  88. do {
  89. if (unlikely(datalen - dp < 2))
  90. goto data_overrun_error;
  91. tmp = data[dp++];
  92. } while (tmp & 0x80);
  93. }
  94. /* Extract the length */
  95. len = data[dp++];
  96. if (len <= 0x7f)
  97. goto check_length;
  98. if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
  99. /* Indefinite length */
  100. if (unlikely((tag & ASN1_CONS_BIT) == ASN1_PRIM << 5))
  101. goto indefinite_len_primitive;
  102. indef_level++;
  103. goto next_tag;
  104. }
  105. n = len - 0x80;
  106. if (unlikely(n > sizeof(len) - 1))
  107. goto length_too_long;
  108. if (unlikely(n > datalen - dp))
  109. goto data_overrun_error;
  110. len = 0;
  111. for (; n > 0; n--) {
  112. len <<= 8;
  113. len |= data[dp++];
  114. }
  115. check_length:
  116. if (len > datalen - dp)
  117. goto data_overrun_error;
  118. dp += len;
  119. goto next_tag;
  120. length_too_long:
  121. *_errmsg = "Unsupported length";
  122. goto error;
  123. indefinite_len_primitive:
  124. *_errmsg = "Indefinite len primitive not permitted";
  125. goto error;
  126. invalid_eoc:
  127. *_errmsg = "Invalid length EOC";
  128. goto error;
  129. data_overrun_error:
  130. *_errmsg = "Data overrun error";
  131. goto error;
  132. missing_eoc:
  133. *_errmsg = "Missing EOC in indefinite len cons";
  134. error:
  135. *_dp = dp;
  136. return -1;
  137. }
  138. /**
  139. * asn1_ber_decoder - Decoder BER/DER/CER ASN.1 according to pattern
  140. * @decoder: The decoder definition (produced by asn1_compiler)
  141. * @context: The caller's context (to be passed to the action functions)
  142. * @data: The encoded data
  143. * @datalen: The size of the encoded data
  144. *
  145. * Decode BER/DER/CER encoded ASN.1 data according to a bytecode pattern
  146. * produced by asn1_compiler. Action functions are called on marked tags to
  147. * allow the caller to retrieve significant data.
  148. *
  149. * LIMITATIONS:
  150. *
  151. * To keep down the amount of stack used by this function, the following limits
  152. * have been imposed:
  153. *
  154. * (1) This won't handle datalen > 65535 without increasing the size of the
  155. * cons stack elements and length_too_long checking.
  156. *
  157. * (2) The stack of constructed types is 10 deep. If the depth of non-leaf
  158. * constructed types exceeds this, the decode will fail.
  159. *
  160. * (3) The SET type (not the SET OF type) isn't really supported as tracking
  161. * what members of the set have been seen is a pain.
  162. */
  163. int asn1_ber_decoder(const struct asn1_decoder *decoder,
  164. void *context,
  165. const unsigned char *data,
  166. size_t datalen)
  167. {
  168. const unsigned char *machine = decoder->machine;
  169. const asn1_action_t *actions = decoder->actions;
  170. size_t machlen = decoder->machlen;
  171. enum asn1_opcode op;
  172. unsigned char tag = 0, csp = 0, jsp = 0, optag = 0, hdr = 0;
  173. const char *errmsg;
  174. size_t pc = 0, dp = 0, tdp = 0, len = 0;
  175. int ret;
  176. unsigned char flags = 0;
  177. #define FLAG_INDEFINITE_LENGTH 0x01
  178. #define FLAG_MATCHED 0x02
  179. #define FLAG_LAST_MATCHED 0x04 /* Last tag matched */
  180. #define FLAG_CONS 0x20 /* Corresponds to CONS bit in the opcode tag
  181. * - ie. whether or not we are going to parse
  182. * a compound type.
  183. */
  184. #define NR_CONS_STACK 10
  185. unsigned short cons_dp_stack[NR_CONS_STACK];
  186. unsigned short cons_datalen_stack[NR_CONS_STACK];
  187. unsigned char cons_hdrlen_stack[NR_CONS_STACK];
  188. #define NR_JUMP_STACK 10
  189. unsigned char jump_stack[NR_JUMP_STACK];
  190. if (datalen > 65535)
  191. return -EMSGSIZE;
  192. next_op:
  193. pr_debug("next_op: pc=\e[32m%zu\e[m/%zu dp=\e[33m%zu\e[m/%zu C=%d J=%d\n",
  194. pc, machlen, dp, datalen, csp, jsp);
  195. if (unlikely(pc >= machlen))
  196. goto machine_overrun_error;
  197. op = machine[pc];
  198. if (unlikely(pc + asn1_op_lengths[op] > machlen))
  199. goto machine_overrun_error;
  200. /* If this command is meant to match a tag, then do that before
  201. * evaluating the command.
  202. */
  203. if (op <= ASN1_OP__MATCHES_TAG) {
  204. unsigned char tmp;
  205. /* Skip conditional matches if possible */
  206. if ((op & ASN1_OP_MATCH__COND && flags & FLAG_MATCHED) ||
  207. (op & ASN1_OP_MATCH__SKIP && dp == datalen)) {
  208. flags &= ~FLAG_LAST_MATCHED;
  209. pc += asn1_op_lengths[op];
  210. goto next_op;
  211. }
  212. flags = 0;
  213. hdr = 2;
  214. /* Extract a tag from the data */
  215. if (unlikely(datalen - dp < 2))
  216. goto data_overrun_error;
  217. tag = data[dp++];
  218. if (unlikely((tag & 0x1f) == ASN1_LONG_TAG))
  219. goto long_tag_not_supported;
  220. if (op & ASN1_OP_MATCH__ANY) {
  221. pr_debug("- any %02x\n", tag);
  222. } else {
  223. /* Extract the tag from the machine
  224. * - Either CONS or PRIM are permitted in the data if
  225. * CONS is not set in the op stream, otherwise CONS
  226. * is mandatory.
  227. */
  228. optag = machine[pc + 1];
  229. flags |= optag & FLAG_CONS;
  230. /* Determine whether the tag matched */
  231. tmp = optag ^ tag;
  232. tmp &= ~(optag & ASN1_CONS_BIT);
  233. pr_debug("- match? %02x %02x %02x\n", tag, optag, tmp);
  234. if (tmp != 0) {
  235. /* All odd-numbered tags are MATCH_OR_SKIP. */
  236. if (op & ASN1_OP_MATCH__SKIP) {
  237. pc += asn1_op_lengths[op];
  238. dp--;
  239. goto next_op;
  240. }
  241. goto tag_mismatch;
  242. }
  243. }
  244. flags |= FLAG_MATCHED;
  245. len = data[dp++];
  246. if (len > 0x7f) {
  247. if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
  248. /* Indefinite length */
  249. if (unlikely(!(tag & ASN1_CONS_BIT)))
  250. goto indefinite_len_primitive;
  251. flags |= FLAG_INDEFINITE_LENGTH;
  252. if (unlikely(2 > datalen - dp))
  253. goto data_overrun_error;
  254. } else {
  255. int n = len - 0x80;
  256. if (unlikely(n > 2))
  257. goto length_too_long;
  258. if (unlikely(n > datalen - dp))
  259. goto data_overrun_error;
  260. hdr += n;
  261. for (len = 0; n > 0; n--) {
  262. len <<= 8;
  263. len |= data[dp++];
  264. }
  265. if (unlikely(len > datalen - dp))
  266. goto data_overrun_error;
  267. }
  268. } else {
  269. if (unlikely(len > datalen - dp))
  270. goto data_overrun_error;
  271. }
  272. if (flags & FLAG_CONS) {
  273. /* For expected compound forms, we stack the positions
  274. * of the start and end of the data.
  275. */
  276. if (unlikely(csp >= NR_CONS_STACK))
  277. goto cons_stack_overflow;
  278. cons_dp_stack[csp] = dp;
  279. cons_hdrlen_stack[csp] = hdr;
  280. if (!(flags & FLAG_INDEFINITE_LENGTH)) {
  281. cons_datalen_stack[csp] = datalen;
  282. datalen = dp + len;
  283. } else {
  284. cons_datalen_stack[csp] = 0;
  285. }
  286. csp++;
  287. }
  288. pr_debug("- TAG: %02x %zu%s\n",
  289. tag, len, flags & FLAG_CONS ? " CONS" : "");
  290. tdp = dp;
  291. }
  292. /* Decide how to handle the operation */
  293. switch (op) {
  294. case ASN1_OP_MATCH:
  295. case ASN1_OP_MATCH_OR_SKIP:
  296. case ASN1_OP_MATCH_ACT:
  297. case ASN1_OP_MATCH_ACT_OR_SKIP:
  298. case ASN1_OP_MATCH_ANY:
  299. case ASN1_OP_MATCH_ANY_OR_SKIP:
  300. case ASN1_OP_MATCH_ANY_ACT:
  301. case ASN1_OP_MATCH_ANY_ACT_OR_SKIP:
  302. case ASN1_OP_COND_MATCH_OR_SKIP:
  303. case ASN1_OP_COND_MATCH_ACT_OR_SKIP:
  304. case ASN1_OP_COND_MATCH_ANY:
  305. case ASN1_OP_COND_MATCH_ANY_OR_SKIP:
  306. case ASN1_OP_COND_MATCH_ANY_ACT:
  307. case ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP:
  308. if (!(flags & FLAG_CONS)) {
  309. if (flags & FLAG_INDEFINITE_LENGTH) {
  310. size_t tmp = dp;
  311. ret = asn1_find_indefinite_length(
  312. data, datalen, &tmp, &len, &errmsg);
  313. if (ret < 0)
  314. goto error;
  315. }
  316. pr_debug("- LEAF: %zu\n", len);
  317. }
  318. if (op & ASN1_OP_MATCH__ACT) {
  319. unsigned char act;
  320. if (op & ASN1_OP_MATCH__ANY)
  321. act = machine[pc + 1];
  322. else
  323. act = machine[pc + 2];
  324. ret = actions[act](context, hdr, tag, data + dp, len);
  325. if (ret < 0)
  326. return ret;
  327. }
  328. if (!(flags & FLAG_CONS))
  329. dp += len;
  330. pc += asn1_op_lengths[op];
  331. goto next_op;
  332. case ASN1_OP_MATCH_JUMP:
  333. case ASN1_OP_MATCH_JUMP_OR_SKIP:
  334. case ASN1_OP_COND_MATCH_JUMP_OR_SKIP:
  335. pr_debug("- MATCH_JUMP\n");
  336. if (unlikely(jsp == NR_JUMP_STACK))
  337. goto jump_stack_overflow;
  338. jump_stack[jsp++] = pc + asn1_op_lengths[op];
  339. pc = machine[pc + 2];
  340. goto next_op;
  341. case ASN1_OP_COND_FAIL:
  342. if (unlikely(!(flags & FLAG_MATCHED)))
  343. goto tag_mismatch;
  344. pc += asn1_op_lengths[op];
  345. goto next_op;
  346. case ASN1_OP_COMPLETE:
  347. if (unlikely(jsp != 0 || csp != 0)) {
  348. pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
  349. jsp, csp);
  350. return -EBADMSG;
  351. }
  352. return 0;
  353. case ASN1_OP_END_SET:
  354. case ASN1_OP_END_SET_ACT:
  355. if (unlikely(!(flags & FLAG_MATCHED)))
  356. goto tag_mismatch;
  357. /* fall through */
  358. case ASN1_OP_END_SEQ:
  359. case ASN1_OP_END_SET_OF:
  360. case ASN1_OP_END_SEQ_OF:
  361. case ASN1_OP_END_SEQ_ACT:
  362. case ASN1_OP_END_SET_OF_ACT:
  363. case ASN1_OP_END_SEQ_OF_ACT:
  364. if (unlikely(csp <= 0))
  365. goto cons_stack_underflow;
  366. csp--;
  367. tdp = cons_dp_stack[csp];
  368. hdr = cons_hdrlen_stack[csp];
  369. len = datalen;
  370. datalen = cons_datalen_stack[csp];
  371. pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
  372. tdp, dp, len, datalen);
  373. if (datalen == 0) {
  374. /* Indefinite length - check for the EOC. */
  375. datalen = len;
  376. if (unlikely(datalen - dp < 2))
  377. goto data_overrun_error;
  378. if (data[dp++] != 0) {
  379. if (op & ASN1_OP_END__OF) {
  380. dp--;
  381. csp++;
  382. pc = machine[pc + 1];
  383. pr_debug("- continue\n");
  384. goto next_op;
  385. }
  386. goto missing_eoc;
  387. }
  388. if (data[dp++] != 0)
  389. goto invalid_eoc;
  390. len = dp - tdp - 2;
  391. } else {
  392. if (dp < len && (op & ASN1_OP_END__OF)) {
  393. datalen = len;
  394. csp++;
  395. pc = machine[pc + 1];
  396. pr_debug("- continue\n");
  397. goto next_op;
  398. }
  399. if (dp != len)
  400. goto cons_length_error;
  401. len -= tdp;
  402. pr_debug("- cons len l=%zu d=%zu\n", len, dp - tdp);
  403. }
  404. if (op & ASN1_OP_END__ACT) {
  405. unsigned char act;
  406. if (op & ASN1_OP_END__OF)
  407. act = machine[pc + 2];
  408. else
  409. act = machine[pc + 1];
  410. ret = actions[act](context, hdr, 0, data + tdp, len);
  411. if (ret < 0)
  412. return ret;
  413. }
  414. pc += asn1_op_lengths[op];
  415. goto next_op;
  416. case ASN1_OP_MAYBE_ACT:
  417. if (!(flags & FLAG_LAST_MATCHED)) {
  418. pc += asn1_op_lengths[op];
  419. goto next_op;
  420. }
  421. /* fall through */
  422. case ASN1_OP_ACT:
  423. ret = actions[machine[pc + 1]](context, hdr, tag, data + tdp, len);
  424. if (ret < 0)
  425. return ret;
  426. pc += asn1_op_lengths[op];
  427. goto next_op;
  428. case ASN1_OP_RETURN:
  429. if (unlikely(jsp <= 0))
  430. goto jump_stack_underflow;
  431. pc = jump_stack[--jsp];
  432. flags |= FLAG_MATCHED | FLAG_LAST_MATCHED;
  433. goto next_op;
  434. default:
  435. break;
  436. }
  437. /* Shouldn't reach here */
  438. pr_err("ASN.1 decoder error: Found reserved opcode (%u) pc=%zu\n",
  439. op, pc);
  440. return -EBADMSG;
  441. data_overrun_error:
  442. errmsg = "Data overrun error";
  443. goto error;
  444. machine_overrun_error:
  445. errmsg = "Machine overrun error";
  446. goto error;
  447. jump_stack_underflow:
  448. errmsg = "Jump stack underflow";
  449. goto error;
  450. jump_stack_overflow:
  451. errmsg = "Jump stack overflow";
  452. goto error;
  453. cons_stack_underflow:
  454. errmsg = "Cons stack underflow";
  455. goto error;
  456. cons_stack_overflow:
  457. errmsg = "Cons stack overflow";
  458. goto error;
  459. cons_length_error:
  460. errmsg = "Cons length error";
  461. goto error;
  462. missing_eoc:
  463. errmsg = "Missing EOC in indefinite len cons";
  464. goto error;
  465. invalid_eoc:
  466. errmsg = "Invalid length EOC";
  467. goto error;
  468. length_too_long:
  469. errmsg = "Unsupported length";
  470. goto error;
  471. indefinite_len_primitive:
  472. errmsg = "Indefinite len primitive not permitted";
  473. goto error;
  474. tag_mismatch:
  475. errmsg = "Unexpected tag";
  476. goto error;
  477. long_tag_not_supported:
  478. errmsg = "Long tag not supported";
  479. error:
  480. pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
  481. errmsg, pc, dp, optag, tag, len);
  482. return -EBADMSG;
  483. }
  484. EXPORT_SYMBOL_GPL(asn1_ber_decoder);
  485. MODULE_LICENSE("GPL");