asn1_decoder.c 13 KB

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