1 /* Decoder for ASN.1 BER/DER/CER encoded bytestream
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
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.
12 #include <linux/export.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/asn1_decoder.h>
16 #include <linux/asn1_ber_bytecode.h>
18 static const unsigned char asn1_op_lengths[ASN1_OP__NR] = {
20 [ASN1_OP_MATCH] = 1 + 1,
21 [ASN1_OP_MATCH_OR_SKIP] = 1 + 1,
22 [ASN1_OP_MATCH_ACT] = 1 + 1 + 1,
23 [ASN1_OP_MATCH_ACT_OR_SKIP] = 1 + 1 + 1,
24 [ASN1_OP_MATCH_JUMP] = 1 + 1 + 1,
25 [ASN1_OP_MATCH_JUMP_OR_SKIP] = 1 + 1 + 1,
26 [ASN1_OP_MATCH_ANY] = 1,
27 [ASN1_OP_MATCH_ANY_OR_SKIP] = 1,
28 [ASN1_OP_MATCH_ANY_ACT] = 1 + 1,
29 [ASN1_OP_MATCH_ANY_ACT_OR_SKIP] = 1 + 1,
30 [ASN1_OP_COND_MATCH_OR_SKIP] = 1 + 1,
31 [ASN1_OP_COND_MATCH_ACT_OR_SKIP] = 1 + 1 + 1,
32 [ASN1_OP_COND_MATCH_JUMP_OR_SKIP] = 1 + 1 + 1,
33 [ASN1_OP_COND_MATCH_ANY] = 1,
34 [ASN1_OP_COND_MATCH_ANY_OR_SKIP] = 1,
35 [ASN1_OP_COND_MATCH_ANY_ACT] = 1 + 1,
36 [ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP] = 1 + 1,
37 [ASN1_OP_COND_FAIL] = 1,
38 [ASN1_OP_COMPLETE] = 1,
39 [ASN1_OP_ACT] = 1 + 1,
40 [ASN1_OP_MAYBE_ACT] = 1 + 1,
42 [ASN1_OP_END_SEQ] = 1,
43 [ASN1_OP_END_SEQ_OF] = 1 + 1,
44 [ASN1_OP_END_SET] = 1,
45 [ASN1_OP_END_SET_OF] = 1 + 1,
46 [ASN1_OP_END_SEQ_ACT] = 1 + 1,
47 [ASN1_OP_END_SEQ_OF_ACT] = 1 + 1 + 1,
48 [ASN1_OP_END_SET_ACT] = 1 + 1,
49 [ASN1_OP_END_SET_OF_ACT] = 1 + 1 + 1,
53 * Find the length of an indefinite length object
54 * @data: The data buffer
55 * @datalen: The end of the innermost containing element in the buffer
56 * @_dp: The data parse cursor (updated before returning)
57 * @_len: Where to return the size of the element.
58 * @_errmsg: Where to return a pointer to an error message on error
60 static int asn1_find_indefinite_length(const unsigned char *data, size_t datalen,
61 size_t *_dp, size_t *_len,
64 unsigned char tag, tmp;
65 size_t dp = *_dp, len, n;
69 if (unlikely(datalen - dp < 2)) {
72 goto data_overrun_error;
75 /* Extract a tag from the data */
78 /* It appears to be an EOC. */
81 if (--indef_level <= 0) {
89 if (unlikely((tag & 0x1f) == ASN1_LONG_TAG)) {
91 if (unlikely(datalen - dp < 2))
92 goto data_overrun_error;
97 /* Extract the length */
104 if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
105 /* Indefinite length */
106 if (unlikely((tag & ASN1_CONS_BIT) == ASN1_PRIM << 5))
107 goto indefinite_len_primitive;
113 if (unlikely(n > sizeof(size_t) - 1))
114 goto length_too_long;
115 if (unlikely(n > datalen - dp))
116 goto data_overrun_error;
117 for (len = 0; n > 0; n--) {
125 *_errmsg = "Unsupported length";
127 indefinite_len_primitive:
128 *_errmsg = "Indefinite len primitive not permitted";
131 *_errmsg = "Invalid length EOC";
134 *_errmsg = "Data overrun error";
137 *_errmsg = "Missing EOC in indefinite len cons";
144 * asn1_ber_decoder - Decoder BER/DER/CER ASN.1 according to pattern
145 * @decoder: The decoder definition (produced by asn1_compiler)
146 * @context: The caller's context (to be passed to the action functions)
147 * @data: The encoded data
148 * @datalen: The size of the encoded data
150 * Decode BER/DER/CER encoded ASN.1 data according to a bytecode pattern
151 * produced by asn1_compiler. Action functions are called on marked tags to
152 * allow the caller to retrieve significant data.
156 * To keep down the amount of stack used by this function, the following limits
159 * (1) This won't handle datalen > 65535 without increasing the size of the
160 * cons stack elements and length_too_long checking.
162 * (2) The stack of constructed types is 10 deep. If the depth of non-leaf
163 * constructed types exceeds this, the decode will fail.
165 * (3) The SET type (not the SET OF type) isn't really supported as tracking
166 * what members of the set have been seen is a pain.
168 int asn1_ber_decoder(const struct asn1_decoder *decoder,
170 const unsigned char *data,
173 const unsigned char *machine = decoder->machine;
174 const asn1_action_t *actions = decoder->actions;
175 size_t machlen = decoder->machlen;
177 unsigned char tag = 0, csp = 0, jsp = 0, optag = 0, hdr = 0;
179 size_t pc = 0, dp = 0, tdp = 0, len = 0;
182 unsigned char flags = 0;
183 #define FLAG_INDEFINITE_LENGTH 0x01
184 #define FLAG_MATCHED 0x02
185 #define FLAG_LAST_MATCHED 0x04 /* Last tag matched */
186 #define FLAG_CONS 0x20 /* Corresponds to CONS bit in the opcode tag
187 * - ie. whether or not we are going to parse
191 #define NR_CONS_STACK 10
192 unsigned short cons_dp_stack[NR_CONS_STACK];
193 unsigned short cons_datalen_stack[NR_CONS_STACK];
194 unsigned char cons_hdrlen_stack[NR_CONS_STACK];
195 #define NR_JUMP_STACK 10
196 unsigned char jump_stack[NR_JUMP_STACK];
202 pr_debug("next_op: pc=\e[32m%zu\e[m/%zu dp=\e[33m%zu\e[m/%zu C=%d J=%d\n",
203 pc, machlen, dp, datalen, csp, jsp);
204 if (unlikely(pc >= machlen))
205 goto machine_overrun_error;
207 if (unlikely(pc + asn1_op_lengths[op] > machlen))
208 goto machine_overrun_error;
210 /* If this command is meant to match a tag, then do that before
211 * evaluating the command.
213 if (op <= ASN1_OP__MATCHES_TAG) {
216 /* Skip conditional matches if possible */
217 if ((op & ASN1_OP_MATCH__COND && flags & FLAG_MATCHED) ||
218 (op & ASN1_OP_MATCH__SKIP && dp == datalen)) {
219 flags &= ~FLAG_LAST_MATCHED;
220 pc += asn1_op_lengths[op];
227 /* Extract a tag from the data */
228 if (unlikely(dp >= datalen - 1))
229 goto data_overrun_error;
231 if (unlikely((tag & 0x1f) == ASN1_LONG_TAG))
232 goto long_tag_not_supported;
234 if (op & ASN1_OP_MATCH__ANY) {
235 pr_debug("- any %02x\n", tag);
237 /* Extract the tag from the machine
238 * - Either CONS or PRIM are permitted in the data if
239 * CONS is not set in the op stream, otherwise CONS
242 optag = machine[pc + 1];
243 flags |= optag & FLAG_CONS;
245 /* Determine whether the tag matched */
247 tmp &= ~(optag & ASN1_CONS_BIT);
248 pr_debug("- match? %02x %02x %02x\n", tag, optag, tmp);
250 /* All odd-numbered tags are MATCH_OR_SKIP. */
251 if (op & ASN1_OP_MATCH__SKIP) {
252 pc += asn1_op_lengths[op];
259 flags |= FLAG_MATCHED;
263 if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
264 /* Indefinite length */
265 if (unlikely(!(tag & ASN1_CONS_BIT)))
266 goto indefinite_len_primitive;
267 flags |= FLAG_INDEFINITE_LENGTH;
268 if (unlikely(2 > datalen - dp))
269 goto data_overrun_error;
273 goto length_too_long;
274 if (unlikely(dp >= datalen - n))
275 goto data_overrun_error;
277 for (len = 0; n > 0; n--) {
281 if (unlikely(len > datalen - dp))
282 goto data_overrun_error;
286 if (flags & FLAG_CONS) {
287 /* For expected compound forms, we stack the positions
288 * of the start and end of the data.
290 if (unlikely(csp >= NR_CONS_STACK))
291 goto cons_stack_overflow;
292 cons_dp_stack[csp] = dp;
293 cons_hdrlen_stack[csp] = hdr;
294 if (!(flags & FLAG_INDEFINITE_LENGTH)) {
295 cons_datalen_stack[csp] = datalen;
298 cons_datalen_stack[csp] = 0;
303 pr_debug("- TAG: %02x %zu%s\n",
304 tag, len, flags & FLAG_CONS ? " CONS" : "");
308 /* Decide how to handle the operation */
310 case ASN1_OP_MATCH_ANY_ACT:
311 case ASN1_OP_MATCH_ANY_ACT_OR_SKIP:
312 case ASN1_OP_COND_MATCH_ANY_ACT:
313 case ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP:
314 ret = actions[machine[pc + 1]](context, hdr, tag, data + dp, len);
319 case ASN1_OP_MATCH_ACT:
320 case ASN1_OP_MATCH_ACT_OR_SKIP:
321 case ASN1_OP_COND_MATCH_ACT_OR_SKIP:
322 ret = actions[machine[pc + 2]](context, hdr, tag, data + dp, len);
328 case ASN1_OP_MATCH_OR_SKIP:
329 case ASN1_OP_MATCH_ANY:
330 case ASN1_OP_MATCH_ANY_OR_SKIP:
331 case ASN1_OP_COND_MATCH_OR_SKIP:
332 case ASN1_OP_COND_MATCH_ANY:
333 case ASN1_OP_COND_MATCH_ANY_OR_SKIP:
335 if (!(flags & FLAG_CONS)) {
336 if (flags & FLAG_INDEFINITE_LENGTH) {
337 ret = asn1_find_indefinite_length(
338 data, datalen, &dp, &len, &errmsg);
344 pr_debug("- LEAF: %zu\n", len);
346 pc += asn1_op_lengths[op];
349 case ASN1_OP_MATCH_JUMP:
350 case ASN1_OP_MATCH_JUMP_OR_SKIP:
351 case ASN1_OP_COND_MATCH_JUMP_OR_SKIP:
352 pr_debug("- MATCH_JUMP\n");
353 if (unlikely(jsp == NR_JUMP_STACK))
354 goto jump_stack_overflow;
355 jump_stack[jsp++] = pc + asn1_op_lengths[op];
356 pc = machine[pc + 2];
359 case ASN1_OP_COND_FAIL:
360 if (unlikely(!(flags & FLAG_MATCHED)))
362 pc += asn1_op_lengths[op];
365 case ASN1_OP_COMPLETE:
366 if (unlikely(jsp != 0 || csp != 0)) {
367 pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
373 case ASN1_OP_END_SET:
374 case ASN1_OP_END_SET_ACT:
375 if (unlikely(!(flags & FLAG_MATCHED)))
377 case ASN1_OP_END_SEQ:
378 case ASN1_OP_END_SET_OF:
379 case ASN1_OP_END_SEQ_OF:
380 case ASN1_OP_END_SEQ_ACT:
381 case ASN1_OP_END_SET_OF_ACT:
382 case ASN1_OP_END_SEQ_OF_ACT:
383 if (unlikely(csp <= 0))
384 goto cons_stack_underflow;
386 tdp = cons_dp_stack[csp];
387 hdr = cons_hdrlen_stack[csp];
389 datalen = cons_datalen_stack[csp];
390 pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
391 tdp, dp, len, datalen);
393 /* Indefinite length - check for the EOC. */
395 if (unlikely(datalen - dp < 2))
396 goto data_overrun_error;
397 if (data[dp++] != 0) {
398 if (op & ASN1_OP_END__OF) {
401 pc = machine[pc + 1];
402 pr_debug("- continue\n");
411 if (dp < len && (op & ASN1_OP_END__OF)) {
414 pc = machine[pc + 1];
415 pr_debug("- continue\n");
419 goto cons_length_error;
421 pr_debug("- cons len l=%zu d=%zu\n", len, dp - tdp);
424 if (op & ASN1_OP_END__ACT) {
426 if (op & ASN1_OP_END__OF)
427 act = machine[pc + 2];
429 act = machine[pc + 1];
430 ret = actions[act](context, hdr, 0, data + tdp, len);
432 pc += asn1_op_lengths[op];
435 case ASN1_OP_MAYBE_ACT:
436 if (!(flags & FLAG_LAST_MATCHED)) {
437 pc += asn1_op_lengths[op];
441 ret = actions[machine[pc + 1]](context, hdr, tag, data + tdp, len);
444 pc += asn1_op_lengths[op];
448 if (unlikely(jsp <= 0))
449 goto jump_stack_underflow;
450 pc = jump_stack[--jsp];
451 flags |= FLAG_MATCHED | FLAG_LAST_MATCHED;
458 /* Shouldn't reach here */
459 pr_err("ASN.1 decoder error: Found reserved opcode (%u) pc=%zu\n",
464 errmsg = "Data overrun error";
466 machine_overrun_error:
467 errmsg = "Machine overrun error";
469 jump_stack_underflow:
470 errmsg = "Jump stack underflow";
473 errmsg = "Jump stack overflow";
475 cons_stack_underflow:
476 errmsg = "Cons stack underflow";
479 errmsg = "Cons stack overflow";
482 errmsg = "Cons length error";
485 errmsg = "Missing EOC in indefinite len cons";
488 errmsg = "Invalid length EOC";
491 errmsg = "Unsupported length";
493 indefinite_len_primitive:
494 errmsg = "Indefinite len primitive not permitted";
497 errmsg = "Unexpected tag";
499 long_tag_not_supported:
500 errmsg = "Long tag not supported";
502 pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
503 errmsg, pc, dp, optag, tag, len);
506 EXPORT_SYMBOL_GPL(asn1_ber_decoder);