asn1_decoder.c 13 KB

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