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/module.h>
16 #include <linux/asn1_decoder.h>
17 #include <linux/asn1_ber_bytecode.h>
19 static const unsigned char asn1_op_lengths
[ASN1_OP__NR
] = {
21 [ASN1_OP_MATCH
] = 1 + 1,
22 [ASN1_OP_MATCH_OR_SKIP
] = 1 + 1,
23 [ASN1_OP_MATCH_ACT
] = 1 + 1 + 1,
24 [ASN1_OP_MATCH_ACT_OR_SKIP
] = 1 + 1 + 1,
25 [ASN1_OP_MATCH_JUMP
] = 1 + 1 + 1,
26 [ASN1_OP_MATCH_JUMP_OR_SKIP
] = 1 + 1 + 1,
27 [ASN1_OP_MATCH_ANY
] = 1,
28 [ASN1_OP_MATCH_ANY_OR_SKIP
] = 1,
29 [ASN1_OP_MATCH_ANY_ACT
] = 1 + 1,
30 [ASN1_OP_MATCH_ANY_ACT_OR_SKIP
] = 1 + 1,
31 [ASN1_OP_COND_MATCH_OR_SKIP
] = 1 + 1,
32 [ASN1_OP_COND_MATCH_ACT_OR_SKIP
] = 1 + 1 + 1,
33 [ASN1_OP_COND_MATCH_JUMP_OR_SKIP
] = 1 + 1 + 1,
34 [ASN1_OP_COND_MATCH_ANY
] = 1,
35 [ASN1_OP_COND_MATCH_ANY_OR_SKIP
] = 1,
36 [ASN1_OP_COND_MATCH_ANY_ACT
] = 1 + 1,
37 [ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP
] = 1 + 1,
38 [ASN1_OP_COND_FAIL
] = 1,
39 [ASN1_OP_COMPLETE
] = 1,
40 [ASN1_OP_ACT
] = 1 + 1,
41 [ASN1_OP_MAYBE_ACT
] = 1 + 1,
43 [ASN1_OP_END_SEQ
] = 1,
44 [ASN1_OP_END_SEQ_OF
] = 1 + 1,
45 [ASN1_OP_END_SET
] = 1,
46 [ASN1_OP_END_SET_OF
] = 1 + 1,
47 [ASN1_OP_END_SEQ_ACT
] = 1 + 1,
48 [ASN1_OP_END_SEQ_OF_ACT
] = 1 + 1 + 1,
49 [ASN1_OP_END_SET_ACT
] = 1 + 1,
50 [ASN1_OP_END_SET_OF_ACT
] = 1 + 1 + 1,
54 * Find the length of an indefinite length object
55 * @data: The data buffer
56 * @datalen: The end of the innermost containing element in the buffer
57 * @_dp: The data parse cursor (updated before returning)
58 * @_len: Where to return the size of the element.
59 * @_errmsg: Where to return a pointer to an error message on error
61 static int asn1_find_indefinite_length(const unsigned char *data
, size_t datalen
,
62 size_t *_dp
, size_t *_len
,
65 unsigned char tag
, tmp
;
66 size_t dp
= *_dp
, len
, n
;
70 if (unlikely(datalen
- dp
< 2)) {
73 goto data_overrun_error
;
76 /* Extract a tag from the data */
78 if (tag
== ASN1_EOC
) {
79 /* It appears to be an EOC. */
82 if (--indef_level
<= 0) {
90 if (unlikely((tag
& 0x1f) == ASN1_LONG_TAG
)) {
92 if (unlikely(datalen
- dp
< 2))
93 goto data_overrun_error
;
98 /* Extract the length */
103 if (unlikely(len
== ASN1_INDEFINITE_LENGTH
)) {
104 /* Indefinite length */
105 if (unlikely((tag
& ASN1_CONS_BIT
) == ASN1_PRIM
<< 5))
106 goto indefinite_len_primitive
;
112 if (unlikely(n
> sizeof(len
) - 1))
113 goto length_too_long
;
114 if (unlikely(n
> datalen
- dp
))
115 goto data_overrun_error
;
122 if (len
> datalen
- dp
)
123 goto data_overrun_error
;
128 *_errmsg
= "Unsupported length";
130 indefinite_len_primitive
:
131 *_errmsg
= "Indefinite len primitive not permitted";
134 *_errmsg
= "Invalid length EOC";
137 *_errmsg
= "Data overrun error";
140 *_errmsg
= "Missing EOC in indefinite len cons";
147 * asn1_ber_decoder - Decoder BER/DER/CER ASN.1 according to pattern
148 * @decoder: The decoder definition (produced by asn1_compiler)
149 * @context: The caller's context (to be passed to the action functions)
150 * @data: The encoded data
151 * @datalen: The size of the encoded data
153 * Decode BER/DER/CER encoded ASN.1 data according to a bytecode pattern
154 * produced by asn1_compiler. Action functions are called on marked tags to
155 * allow the caller to retrieve significant data.
159 * To keep down the amount of stack used by this function, the following limits
162 * (1) This won't handle datalen > 65535 without increasing the size of the
163 * cons stack elements and length_too_long checking.
165 * (2) The stack of constructed types is 10 deep. If the depth of non-leaf
166 * constructed types exceeds this, the decode will fail.
168 * (3) The SET type (not the SET OF type) isn't really supported as tracking
169 * what members of the set have been seen is a pain.
171 int asn1_ber_decoder(const struct asn1_decoder
*decoder
,
173 const unsigned char *data
,
176 const unsigned char *machine
= decoder
->machine
;
177 const asn1_action_t
*actions
= decoder
->actions
;
178 size_t machlen
= decoder
->machlen
;
180 unsigned char tag
= 0, csp
= 0, jsp
= 0, optag
= 0, hdr
= 0;
182 size_t pc
= 0, dp
= 0, tdp
= 0, len
= 0;
185 unsigned char flags
= 0;
186 #define FLAG_INDEFINITE_LENGTH 0x01
187 #define FLAG_MATCHED 0x02
188 #define FLAG_LAST_MATCHED 0x04 /* Last tag matched */
189 #define FLAG_CONS 0x20 /* Corresponds to CONS bit in the opcode tag
190 * - ie. whether or not we are going to parse
194 #define NR_CONS_STACK 10
195 unsigned short cons_dp_stack
[NR_CONS_STACK
];
196 unsigned short cons_datalen_stack
[NR_CONS_STACK
];
197 unsigned char cons_hdrlen_stack
[NR_CONS_STACK
];
198 #define NR_JUMP_STACK 10
199 unsigned char jump_stack
[NR_JUMP_STACK
];
205 pr_debug("next_op: pc=\e[32m%zu\e[m/%zu dp=\e[33m%zu\e[m/%zu C=%d J=%d\n",
206 pc
, machlen
, dp
, datalen
, csp
, jsp
);
207 if (unlikely(pc
>= machlen
))
208 goto machine_overrun_error
;
210 if (unlikely(pc
+ asn1_op_lengths
[op
] > machlen
))
211 goto machine_overrun_error
;
213 /* If this command is meant to match a tag, then do that before
214 * evaluating the command.
216 if (op
<= ASN1_OP__MATCHES_TAG
) {
219 /* Skip conditional matches if possible */
220 if ((op
& ASN1_OP_MATCH__COND
&& flags
& FLAG_MATCHED
) ||
221 (op
& ASN1_OP_MATCH__SKIP
&& dp
== datalen
)) {
222 flags
&= ~FLAG_LAST_MATCHED
;
223 pc
+= asn1_op_lengths
[op
];
230 /* Extract a tag from the data */
231 if (unlikely(datalen
- dp
< 2))
232 goto data_overrun_error
;
234 if (unlikely((tag
& 0x1f) == ASN1_LONG_TAG
))
235 goto long_tag_not_supported
;
237 if (op
& ASN1_OP_MATCH__ANY
) {
238 pr_debug("- any %02x\n", tag
);
240 /* Extract the tag from the machine
241 * - Either CONS or PRIM are permitted in the data if
242 * CONS is not set in the op stream, otherwise CONS
245 optag
= machine
[pc
+ 1];
246 flags
|= optag
& FLAG_CONS
;
248 /* Determine whether the tag matched */
250 tmp
&= ~(optag
& ASN1_CONS_BIT
);
251 pr_debug("- match? %02x %02x %02x\n", tag
, optag
, tmp
);
253 /* All odd-numbered tags are MATCH_OR_SKIP. */
254 if (op
& ASN1_OP_MATCH__SKIP
) {
255 pc
+= asn1_op_lengths
[op
];
262 flags
|= FLAG_MATCHED
;
266 if (unlikely(len
== ASN1_INDEFINITE_LENGTH
)) {
267 /* Indefinite length */
268 if (unlikely(!(tag
& ASN1_CONS_BIT
)))
269 goto indefinite_len_primitive
;
270 flags
|= FLAG_INDEFINITE_LENGTH
;
271 if (unlikely(2 > datalen
- dp
))
272 goto data_overrun_error
;
276 goto length_too_long
;
277 if (unlikely(n
> datalen
- dp
))
278 goto data_overrun_error
;
280 for (len
= 0; n
> 0; n
--) {
284 if (unlikely(len
> datalen
- dp
))
285 goto data_overrun_error
;
288 if (unlikely(len
> datalen
- dp
))
289 goto data_overrun_error
;
292 if (flags
& FLAG_CONS
) {
293 /* For expected compound forms, we stack the positions
294 * of the start and end of the data.
296 if (unlikely(csp
>= NR_CONS_STACK
))
297 goto cons_stack_overflow
;
298 cons_dp_stack
[csp
] = dp
;
299 cons_hdrlen_stack
[csp
] = hdr
;
300 if (!(flags
& FLAG_INDEFINITE_LENGTH
)) {
301 cons_datalen_stack
[csp
] = datalen
;
304 cons_datalen_stack
[csp
] = 0;
309 pr_debug("- TAG: %02x %zu%s\n",
310 tag
, len
, flags
& FLAG_CONS
? " CONS" : "");
314 /* Decide how to handle the operation */
317 case ASN1_OP_MATCH_OR_SKIP
:
318 case ASN1_OP_MATCH_ACT
:
319 case ASN1_OP_MATCH_ACT_OR_SKIP
:
320 case ASN1_OP_MATCH_ANY
:
321 case ASN1_OP_MATCH_ANY_OR_SKIP
:
322 case ASN1_OP_MATCH_ANY_ACT
:
323 case ASN1_OP_MATCH_ANY_ACT_OR_SKIP
:
324 case ASN1_OP_COND_MATCH_OR_SKIP
:
325 case ASN1_OP_COND_MATCH_ACT_OR_SKIP
:
326 case ASN1_OP_COND_MATCH_ANY
:
327 case ASN1_OP_COND_MATCH_ANY_OR_SKIP
:
328 case ASN1_OP_COND_MATCH_ANY_ACT
:
329 case ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP
:
331 if (!(flags
& FLAG_CONS
)) {
332 if (flags
& FLAG_INDEFINITE_LENGTH
) {
335 ret
= asn1_find_indefinite_length(
336 data
, datalen
, &tmp
, &len
, &errmsg
);
340 pr_debug("- LEAF: %zu\n", len
);
343 if (op
& ASN1_OP_MATCH__ACT
) {
346 if (op
& ASN1_OP_MATCH__ANY
)
347 act
= machine
[pc
+ 1];
349 act
= machine
[pc
+ 2];
350 ret
= actions
[act
](context
, hdr
, tag
, data
+ dp
, len
);
355 if (!(flags
& FLAG_CONS
))
357 pc
+= asn1_op_lengths
[op
];
360 case ASN1_OP_MATCH_JUMP
:
361 case ASN1_OP_MATCH_JUMP_OR_SKIP
:
362 case ASN1_OP_COND_MATCH_JUMP_OR_SKIP
:
363 pr_debug("- MATCH_JUMP\n");
364 if (unlikely(jsp
== NR_JUMP_STACK
))
365 goto jump_stack_overflow
;
366 jump_stack
[jsp
++] = pc
+ asn1_op_lengths
[op
];
367 pc
= machine
[pc
+ 2];
370 case ASN1_OP_COND_FAIL
:
371 if (unlikely(!(flags
& FLAG_MATCHED
)))
373 pc
+= asn1_op_lengths
[op
];
376 case ASN1_OP_COMPLETE
:
377 if (unlikely(jsp
!= 0 || csp
!= 0)) {
378 pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
384 case ASN1_OP_END_SET
:
385 case ASN1_OP_END_SET_ACT
:
386 if (unlikely(!(flags
& FLAG_MATCHED
)))
388 case ASN1_OP_END_SEQ
:
389 case ASN1_OP_END_SET_OF
:
390 case ASN1_OP_END_SEQ_OF
:
391 case ASN1_OP_END_SEQ_ACT
:
392 case ASN1_OP_END_SET_OF_ACT
:
393 case ASN1_OP_END_SEQ_OF_ACT
:
394 if (unlikely(csp
<= 0))
395 goto cons_stack_underflow
;
397 tdp
= cons_dp_stack
[csp
];
398 hdr
= cons_hdrlen_stack
[csp
];
400 datalen
= cons_datalen_stack
[csp
];
401 pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
402 tdp
, dp
, len
, datalen
);
404 /* Indefinite length - check for the EOC. */
406 if (unlikely(datalen
- dp
< 2))
407 goto data_overrun_error
;
408 if (data
[dp
++] != 0) {
409 if (op
& ASN1_OP_END__OF
) {
412 pc
= machine
[pc
+ 1];
413 pr_debug("- continue\n");
422 if (dp
< len
&& (op
& ASN1_OP_END__OF
)) {
425 pc
= machine
[pc
+ 1];
426 pr_debug("- continue\n");
430 goto cons_length_error
;
432 pr_debug("- cons len l=%zu d=%zu\n", len
, dp
- tdp
);
435 if (op
& ASN1_OP_END__ACT
) {
437 if (op
& ASN1_OP_END__OF
)
438 act
= machine
[pc
+ 2];
440 act
= machine
[pc
+ 1];
441 ret
= actions
[act
](context
, hdr
, 0, data
+ tdp
, len
);
445 pc
+= asn1_op_lengths
[op
];
448 case ASN1_OP_MAYBE_ACT
:
449 if (!(flags
& FLAG_LAST_MATCHED
)) {
450 pc
+= asn1_op_lengths
[op
];
454 ret
= actions
[machine
[pc
+ 1]](context
, hdr
, tag
, data
+ tdp
, len
);
457 pc
+= asn1_op_lengths
[op
];
461 if (unlikely(jsp
<= 0))
462 goto jump_stack_underflow
;
463 pc
= jump_stack
[--jsp
];
464 flags
|= FLAG_MATCHED
| FLAG_LAST_MATCHED
;
471 /* Shouldn't reach here */
472 pr_err("ASN.1 decoder error: Found reserved opcode (%u) pc=%zu\n",
477 errmsg
= "Data overrun error";
479 machine_overrun_error
:
480 errmsg
= "Machine overrun error";
482 jump_stack_underflow
:
483 errmsg
= "Jump stack underflow";
486 errmsg
= "Jump stack overflow";
488 cons_stack_underflow
:
489 errmsg
= "Cons stack underflow";
492 errmsg
= "Cons stack overflow";
495 errmsg
= "Cons length error";
498 errmsg
= "Missing EOC in indefinite len cons";
501 errmsg
= "Invalid length EOC";
504 errmsg
= "Unsupported length";
506 indefinite_len_primitive
:
507 errmsg
= "Indefinite len primitive not permitted";
510 errmsg
= "Unexpected tag";
512 long_tag_not_supported
:
513 errmsg
= "Long tag not supported";
515 pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
516 errmsg
, pc
, dp
, optag
, tag
, len
);
519 EXPORT_SYMBOL_GPL(asn1_ber_decoder
);
521 MODULE_LICENSE("GPL");