asn1_decoder.c 13 KB

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