asn1_decoder.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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(dp >= datalen - 1))
  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(dp >= datalen - n))
  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. }
  266. if (flags & FLAG_CONS) {
  267. /* For expected compound forms, we stack the positions
  268. * of the start and end of the data.
  269. */
  270. if (unlikely(csp >= NR_CONS_STACK))
  271. goto cons_stack_overflow;
  272. cons_dp_stack[csp] = dp;
  273. cons_hdrlen_stack[csp] = hdr;
  274. if (!(flags & FLAG_INDEFINITE_LENGTH)) {
  275. cons_datalen_stack[csp] = datalen;
  276. datalen = dp + len;
  277. } else {
  278. cons_datalen_stack[csp] = 0;
  279. }
  280. csp++;
  281. }
  282. pr_debug("- TAG: %02x %zu%s\n",
  283. tag, len, flags & FLAG_CONS ? " CONS" : "");
  284. tdp = dp;
  285. }
  286. /* Decide how to handle the operation */
  287. switch (op) {
  288. case ASN1_OP_MATCH_ANY_ACT:
  289. case ASN1_OP_MATCH_ANY_ACT_OR_SKIP:
  290. case ASN1_OP_COND_MATCH_ANY_ACT:
  291. case ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP:
  292. ret = actions[machine[pc + 1]](context, hdr, tag, data + dp, len);
  293. if (ret < 0)
  294. return ret;
  295. goto skip_data;
  296. case ASN1_OP_MATCH_ACT:
  297. case ASN1_OP_MATCH_ACT_OR_SKIP:
  298. case ASN1_OP_COND_MATCH_ACT_OR_SKIP:
  299. ret = actions[machine[pc + 2]](context, hdr, tag, data + dp, len);
  300. if (ret < 0)
  301. return ret;
  302. goto skip_data;
  303. case ASN1_OP_MATCH:
  304. case ASN1_OP_MATCH_OR_SKIP:
  305. case ASN1_OP_MATCH_ANY:
  306. case ASN1_OP_MATCH_ANY_OR_SKIP:
  307. case ASN1_OP_COND_MATCH_OR_SKIP:
  308. case ASN1_OP_COND_MATCH_ANY:
  309. case ASN1_OP_COND_MATCH_ANY_OR_SKIP:
  310. skip_data:
  311. if (!(flags & FLAG_CONS)) {
  312. if (flags & FLAG_INDEFINITE_LENGTH) {
  313. ret = asn1_find_indefinite_length(
  314. data, datalen, &dp, &len, &errmsg);
  315. if (ret < 0)
  316. goto error;
  317. } else {
  318. dp += len;
  319. }
  320. pr_debug("- LEAF: %zu\n", len);
  321. }
  322. pc += asn1_op_lengths[op];
  323. goto next_op;
  324. case ASN1_OP_MATCH_JUMP:
  325. case ASN1_OP_MATCH_JUMP_OR_SKIP:
  326. case ASN1_OP_COND_MATCH_JUMP_OR_SKIP:
  327. pr_debug("- MATCH_JUMP\n");
  328. if (unlikely(jsp == NR_JUMP_STACK))
  329. goto jump_stack_overflow;
  330. jump_stack[jsp++] = pc + asn1_op_lengths[op];
  331. pc = machine[pc + 2];
  332. goto next_op;
  333. case ASN1_OP_COND_FAIL:
  334. if (unlikely(!(flags & FLAG_MATCHED)))
  335. goto tag_mismatch;
  336. pc += asn1_op_lengths[op];
  337. goto next_op;
  338. case ASN1_OP_COMPLETE:
  339. if (unlikely(jsp != 0 || csp != 0)) {
  340. pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
  341. jsp, csp);
  342. return -EBADMSG;
  343. }
  344. return 0;
  345. case ASN1_OP_END_SET:
  346. case ASN1_OP_END_SET_ACT:
  347. if (unlikely(!(flags & FLAG_MATCHED)))
  348. goto tag_mismatch;
  349. case ASN1_OP_END_SEQ:
  350. case ASN1_OP_END_SET_OF:
  351. case ASN1_OP_END_SEQ_OF:
  352. case ASN1_OP_END_SEQ_ACT:
  353. case ASN1_OP_END_SET_OF_ACT:
  354. case ASN1_OP_END_SEQ_OF_ACT:
  355. if (unlikely(csp <= 0))
  356. goto cons_stack_underflow;
  357. csp--;
  358. tdp = cons_dp_stack[csp];
  359. hdr = cons_hdrlen_stack[csp];
  360. len = datalen;
  361. datalen = cons_datalen_stack[csp];
  362. pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
  363. tdp, dp, len, datalen);
  364. if (datalen == 0) {
  365. /* Indefinite length - check for the EOC. */
  366. datalen = len;
  367. if (unlikely(datalen - dp < 2))
  368. goto data_overrun_error;
  369. if (data[dp++] != 0) {
  370. if (op & ASN1_OP_END__OF) {
  371. dp--;
  372. csp++;
  373. pc = machine[pc + 1];
  374. pr_debug("- continue\n");
  375. goto next_op;
  376. }
  377. goto missing_eoc;
  378. }
  379. if (data[dp++] != 0)
  380. goto invalid_eoc;
  381. len = dp - tdp - 2;
  382. } else {
  383. if (dp < len && (op & ASN1_OP_END__OF)) {
  384. datalen = len;
  385. csp++;
  386. pc = machine[pc + 1];
  387. pr_debug("- continue\n");
  388. goto next_op;
  389. }
  390. if (dp != len)
  391. goto cons_length_error;
  392. len -= tdp;
  393. pr_debug("- cons len l=%zu d=%zu\n", len, dp - tdp);
  394. }
  395. if (op & ASN1_OP_END__ACT) {
  396. unsigned char act;
  397. if (op & ASN1_OP_END__OF)
  398. act = machine[pc + 2];
  399. else
  400. act = machine[pc + 1];
  401. ret = actions[act](context, hdr, 0, data + tdp, len);
  402. }
  403. pc += asn1_op_lengths[op];
  404. goto next_op;
  405. case ASN1_OP_MAYBE_ACT:
  406. if (!(flags & FLAG_LAST_MATCHED)) {
  407. pc += asn1_op_lengths[op];
  408. goto next_op;
  409. }
  410. case ASN1_OP_ACT:
  411. ret = actions[machine[pc + 1]](context, hdr, tag, data + tdp, len);
  412. if (ret < 0)
  413. return ret;
  414. pc += asn1_op_lengths[op];
  415. goto next_op;
  416. case ASN1_OP_RETURN:
  417. if (unlikely(jsp <= 0))
  418. goto jump_stack_underflow;
  419. pc = jump_stack[--jsp];
  420. flags |= FLAG_MATCHED | FLAG_LAST_MATCHED;
  421. goto next_op;
  422. default:
  423. break;
  424. }
  425. /* Shouldn't reach here */
  426. pr_err("ASN.1 decoder error: Found reserved opcode (%u) pc=%zu\n",
  427. op, pc);
  428. return -EBADMSG;
  429. data_overrun_error:
  430. errmsg = "Data overrun error";
  431. goto error;
  432. machine_overrun_error:
  433. errmsg = "Machine overrun error";
  434. goto error;
  435. jump_stack_underflow:
  436. errmsg = "Jump stack underflow";
  437. goto error;
  438. jump_stack_overflow:
  439. errmsg = "Jump stack overflow";
  440. goto error;
  441. cons_stack_underflow:
  442. errmsg = "Cons stack underflow";
  443. goto error;
  444. cons_stack_overflow:
  445. errmsg = "Cons stack overflow";
  446. goto error;
  447. cons_length_error:
  448. errmsg = "Cons length error";
  449. goto error;
  450. missing_eoc:
  451. errmsg = "Missing EOC in indefinite len cons";
  452. goto error;
  453. invalid_eoc:
  454. errmsg = "Invalid length EOC";
  455. goto error;
  456. length_too_long:
  457. errmsg = "Unsupported length";
  458. goto error;
  459. indefinite_len_primitive:
  460. errmsg = "Indefinite len primitive not permitted";
  461. goto error;
  462. tag_mismatch:
  463. errmsg = "Unexpected tag";
  464. goto error;
  465. long_tag_not_supported:
  466. errmsg = "Long tag not supported";
  467. error:
  468. pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
  469. errmsg, pc, dp, optag, tag, len);
  470. return -EBADMSG;
  471. }
  472. EXPORT_SYMBOL_GPL(asn1_ber_decoder);
  473. MODULE_LICENSE("GPL");