asn1_decoder.c 13 KB

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