1 /* Decoder for ASN.1 BER/DER/CER encoded bytestream
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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_ACT] = 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_ACT] = 1 + 1,
33 [ASN1_OP_COND_FAIL] = 1,
34 [ASN1_OP_COMPLETE] = 1,
35 [ASN1_OP_ACT] = 1 + 1,
36 [ASN1_OP_MAYBE_ACT] = 1 + 1,
38 [ASN1_OP_END_SEQ] = 1,
39 [ASN1_OP_END_SEQ_OF] = 1 + 1,
40 [ASN1_OP_END_SET] = 1,
41 [ASN1_OP_END_SET_OF] = 1 + 1,
42 [ASN1_OP_END_SEQ_ACT] = 1 + 1,
43 [ASN1_OP_END_SEQ_OF_ACT] = 1 + 1 + 1,
44 [ASN1_OP_END_SET_ACT] = 1 + 1,
45 [ASN1_OP_END_SET_OF_ACT] = 1 + 1 + 1,
49 * Find the length of an indefinite length object
50 * @data: The data buffer
51 * @datalen: The end of the innermost containing element in the buffer
52 * @_dp: The data parse cursor (updated before returning)
53 * @_len: Where to return the size of the element.
54 * @_errmsg: Where to return a pointer to an error message on error
56 static int asn1_find_indefinite_length(const unsigned char *data, size_t datalen,
57 size_t *_dp, size_t *_len,
60 unsigned char tag, tmp;
61 size_t dp = *_dp, len, n;
65 if (unlikely(datalen - dp < 2)) {
68 goto data_overrun_error;
71 /* Extract a tag from the data */
74 /* It appears to be an EOC. */
77 if (--indef_level <= 0) {
85 if (unlikely((tag & 0x1f) == ASN1_LONG_TAG)) {
87 if (unlikely(datalen - dp < 2))
88 goto data_overrun_error;
93 /* Extract the length */
100 if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
101 /* Indefinite length */
102 if (unlikely((tag & ASN1_CONS_BIT) == ASN1_PRIM << 5))
103 goto indefinite_len_primitive;
109 if (unlikely(n > sizeof(size_t) - 1))
110 goto length_too_long;
111 if (unlikely(n > datalen - dp))
112 goto data_overrun_error;
113 for (len = 0; n > 0; n--) {
121 *_errmsg = "Unsupported length";
123 indefinite_len_primitive:
124 *_errmsg = "Indefinite len primitive not permitted";
127 *_errmsg = "Invalid length EOC";
130 *_errmsg = "Data overrun error";
133 *_errmsg = "Missing EOC in indefinite len cons";
140 * asn1_ber_decoder - Decoder BER/DER/CER ASN.1 according to pattern
141 * @decoder: The decoder definition (produced by asn1_compiler)
142 * @context: The caller's context (to be passed to the action functions)
143 * @data: The encoded data
144 * @datalen: The size of the encoded data
146 * Decode BER/DER/CER encoded ASN.1 data according to a bytecode pattern
147 * produced by asn1_compiler. Action functions are called on marked tags to
148 * allow the caller to retrieve significant data.
152 * To keep down the amount of stack used by this function, the following limits
155 * (1) This won't handle datalen > 65535 without increasing the size of the
156 * cons stack elements and length_too_long checking.
158 * (2) The stack of constructed types is 10 deep. If the depth of non-leaf
159 * constructed types exceeds this, the decode will fail.
161 * (3) The SET type (not the SET OF type) isn't really supported as tracking
162 * what members of the set have been seen is a pain.
164 int asn1_ber_decoder(const struct asn1_decoder *decoder,
166 const unsigned char *data,
169 const unsigned char *machine = decoder->machine;
170 const asn1_action_t *actions = decoder->actions;
171 size_t machlen = decoder->machlen;
173 unsigned char tag = 0, csp = 0, jsp = 0, optag = 0, hdr = 0;
175 size_t pc = 0, dp = 0, tdp = 0, len = 0;
178 unsigned char flags = 0;
179 #define FLAG_INDEFINITE_LENGTH 0x01
180 #define FLAG_MATCHED 0x02
181 #define FLAG_LAST_MATCHED 0x04 /* Last tag matched */
182 #define FLAG_CONS 0x20 /* Corresponds to CONS bit in the opcode tag
183 * - ie. whether or not we are going to parse
187 #define NR_CONS_STACK 10
188 unsigned short cons_dp_stack[NR_CONS_STACK];
189 unsigned short cons_datalen_stack[NR_CONS_STACK];
190 unsigned char cons_hdrlen_stack[NR_CONS_STACK];
191 #define NR_JUMP_STACK 10
192 unsigned char jump_stack[NR_JUMP_STACK];
198 pr_debug("next_op: pc=\e[32m%zu\e[m/%zu dp=\e[33m%zu\e[m/%zu C=%d J=%d\n",
199 pc, machlen, dp, datalen, csp, jsp);
200 if (unlikely(pc >= machlen))
201 goto machine_overrun_error;
203 if (unlikely(pc + asn1_op_lengths[op] > machlen))
204 goto machine_overrun_error;
206 /* If this command is meant to match a tag, then do that before
207 * evaluating the command.
209 if (op <= ASN1_OP__MATCHES_TAG) {
212 /* Skip conditional matches if possible */
213 if ((op & ASN1_OP_MATCH__COND &&
214 flags & FLAG_MATCHED) ||
216 flags &= ~FLAG_LAST_MATCHED;
217 pc += asn1_op_lengths[op];
224 /* Extract a tag from the data */
225 if (unlikely(dp >= datalen - 1))
226 goto data_overrun_error;
228 if (unlikely((tag & 0x1f) == ASN1_LONG_TAG))
229 goto long_tag_not_supported;
231 if (op & ASN1_OP_MATCH__ANY) {
232 pr_debug("- any %02x\n", tag);
234 /* Extract the tag from the machine
235 * - Either CONS or PRIM are permitted in the data if
236 * CONS is not set in the op stream, otherwise CONS
239 optag = machine[pc + 1];
240 flags |= optag & FLAG_CONS;
242 /* Determine whether the tag matched */
244 tmp &= ~(optag & ASN1_CONS_BIT);
245 pr_debug("- match? %02x %02x %02x\n", tag, optag, tmp);
247 /* All odd-numbered tags are MATCH_OR_SKIP. */
248 if (op & ASN1_OP_MATCH__SKIP) {
249 pc += asn1_op_lengths[op];
256 flags |= FLAG_MATCHED;
260 if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
261 /* Indefinite length */
262 if (unlikely(!(tag & ASN1_CONS_BIT)))
263 goto indefinite_len_primitive;
264 flags |= FLAG_INDEFINITE_LENGTH;
265 if (unlikely(2 > datalen - dp))
266 goto data_overrun_error;
270 goto length_too_long;
271 if (unlikely(dp >= datalen - n))
272 goto data_overrun_error;
274 for (len = 0; n > 0; n--) {
278 if (unlikely(len > datalen - dp))
279 goto data_overrun_error;
283 if (flags & FLAG_CONS) {
284 /* For expected compound forms, we stack the positions
285 * of the start and end of the data.
287 if (unlikely(csp >= NR_CONS_STACK))
288 goto cons_stack_overflow;
289 cons_dp_stack[csp] = dp;
290 cons_hdrlen_stack[csp] = hdr;
291 if (!(flags & FLAG_INDEFINITE_LENGTH)) {
292 cons_datalen_stack[csp] = datalen;
295 cons_datalen_stack[csp] = 0;
300 pr_debug("- TAG: %02x %zu%s\n",
301 tag, len, flags & FLAG_CONS ? " CONS" : "");
305 /* Decide how to handle the operation */
307 case ASN1_OP_MATCH_ANY_ACT:
308 case ASN1_OP_COND_MATCH_ANY_ACT:
309 ret = actions[machine[pc + 1]](context, hdr, tag, data + dp, len);
314 case ASN1_OP_MATCH_ACT:
315 case ASN1_OP_MATCH_ACT_OR_SKIP:
316 case ASN1_OP_COND_MATCH_ACT_OR_SKIP:
317 ret = actions[machine[pc + 2]](context, hdr, tag, data + dp, len);
323 case ASN1_OP_MATCH_OR_SKIP:
324 case ASN1_OP_MATCH_ANY:
325 case ASN1_OP_COND_MATCH_OR_SKIP:
326 case ASN1_OP_COND_MATCH_ANY:
328 if (!(flags & FLAG_CONS)) {
329 if (flags & FLAG_INDEFINITE_LENGTH) {
330 ret = asn1_find_indefinite_length(
331 data, datalen, &dp, &len, &errmsg);
337 pr_debug("- LEAF: %zu\n", len);
339 pc += asn1_op_lengths[op];
342 case ASN1_OP_MATCH_JUMP:
343 case ASN1_OP_MATCH_JUMP_OR_SKIP:
344 case ASN1_OP_COND_MATCH_JUMP_OR_SKIP:
345 pr_debug("- MATCH_JUMP\n");
346 if (unlikely(jsp == NR_JUMP_STACK))
347 goto jump_stack_overflow;
348 jump_stack[jsp++] = pc + asn1_op_lengths[op];
349 pc = machine[pc + 2];
352 case ASN1_OP_COND_FAIL:
353 if (unlikely(!(flags & FLAG_MATCHED)))
355 pc += asn1_op_lengths[op];
358 case ASN1_OP_COMPLETE:
359 if (unlikely(jsp != 0 || csp != 0)) {
360 pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
366 case ASN1_OP_END_SET:
367 case ASN1_OP_END_SET_ACT:
368 if (unlikely(!(flags & FLAG_MATCHED)))
370 case ASN1_OP_END_SEQ:
371 case ASN1_OP_END_SET_OF:
372 case ASN1_OP_END_SEQ_OF:
373 case ASN1_OP_END_SEQ_ACT:
374 case ASN1_OP_END_SET_OF_ACT:
375 case ASN1_OP_END_SEQ_OF_ACT:
376 if (unlikely(csp <= 0))
377 goto cons_stack_underflow;
379 tdp = cons_dp_stack[csp];
380 hdr = cons_hdrlen_stack[csp];
382 datalen = cons_datalen_stack[csp];
383 pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
384 tdp, dp, len, datalen);
386 /* Indefinite length - check for the EOC. */
388 if (unlikely(datalen - dp < 2))
389 goto data_overrun_error;
390 if (data[dp++] != 0) {
391 if (op & ASN1_OP_END__OF) {
394 pc = machine[pc + 1];
395 pr_debug("- continue\n");
404 if (dp < len && (op & ASN1_OP_END__OF)) {
407 pc = machine[pc + 1];
408 pr_debug("- continue\n");
412 goto cons_length_error;
414 pr_debug("- cons len l=%zu d=%zu\n", len, dp - tdp);
417 if (op & ASN1_OP_END__ACT) {
419 if (op & ASN1_OP_END__OF)
420 act = machine[pc + 2];
422 act = machine[pc + 1];
423 ret = actions[act](context, hdr, 0, data + tdp, len);
425 pc += asn1_op_lengths[op];
428 case ASN1_OP_MAYBE_ACT:
429 if (!(flags & FLAG_LAST_MATCHED)) {
430 pc += asn1_op_lengths[op];
434 ret = actions[machine[pc + 1]](context, hdr, tag, data + tdp, len);
437 pc += asn1_op_lengths[op];
441 if (unlikely(jsp <= 0))
442 goto jump_stack_underflow;
443 pc = jump_stack[--jsp];
444 flags |= FLAG_MATCHED | FLAG_LAST_MATCHED;
451 /* Shouldn't reach here */
452 pr_err("ASN.1 decoder error: Found reserved opcode (%u) pc=%zu\n",
457 errmsg = "Data overrun error";
459 machine_overrun_error:
460 errmsg = "Machine overrun error";
462 jump_stack_underflow:
463 errmsg = "Jump stack underflow";
466 errmsg = "Jump stack overflow";
468 cons_stack_underflow:
469 errmsg = "Cons stack underflow";
472 errmsg = "Cons stack overflow";
475 errmsg = "Cons length error";
478 errmsg = "Missing EOC in indefinite len cons";
481 errmsg = "Invalid length EOC";
484 errmsg = "Unsupported length";
486 indefinite_len_primitive:
487 errmsg = "Indefinite len primitive not permitted";
490 errmsg = "Unexpected tag";
492 long_tag_not_supported:
493 errmsg = "Long tag not supported";
495 pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
496 errmsg, pc, dp, optag, tag, len);
499 EXPORT_SYMBOL_GPL(asn1_ber_decoder);