asn1_decoder.c 13 KB

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