asn1_decoder.c 13 KB

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