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