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_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 */
77 if (tag
== ASN1_EOC
) {
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 */
102 if (unlikely(len
== ASN1_INDEFINITE_LENGTH
)) {
103 /* Indefinite length */
104 if (unlikely((tag
& ASN1_CONS_BIT
) == ASN1_PRIM
<< 5))
105 goto indefinite_len_primitive
;
111 if (unlikely(n
> sizeof(len
) - 1))
112 goto length_too_long
;
113 if (unlikely(n
> datalen
- dp
))
114 goto data_overrun_error
;
121 if (len
> datalen
- dp
)
122 goto data_overrun_error
;
127 *_errmsg
= "Unsupported length";
129 indefinite_len_primitive
:
130 *_errmsg
= "Indefinite len primitive not permitted";
133 *_errmsg
= "Invalid length EOC";
136 *_errmsg
= "Data overrun error";
139 *_errmsg
= "Missing EOC in indefinite len cons";
146 * asn1_ber_decoder - Decoder BER/DER/CER ASN.1 according to pattern
147 * @decoder: The decoder definition (produced by asn1_compiler)
148 * @context: The caller's context (to be passed to the action functions)
149 * @data: The encoded data
150 * @datalen: The size of the encoded data
152 * Decode BER/DER/CER encoded ASN.1 data according to a bytecode pattern
153 * produced by asn1_compiler. Action functions are called on marked tags to
154 * allow the caller to retrieve significant data.
158 * To keep down the amount of stack used by this function, the following limits
161 * (1) This won't handle datalen > 65535 without increasing the size of the
162 * cons stack elements and length_too_long checking.
164 * (2) The stack of constructed types is 10 deep. If the depth of non-leaf
165 * constructed types exceeds this, the decode will fail.
167 * (3) The SET type (not the SET OF type) isn't really supported as tracking
168 * what members of the set have been seen is a pain.
170 int asn1_ber_decoder(const struct asn1_decoder
*decoder
,
172 const unsigned char *data
,
175 const unsigned char *machine
= decoder
->machine
;
176 const asn1_action_t
*actions
= decoder
->actions
;
177 size_t machlen
= decoder
->machlen
;
179 unsigned char tag
= 0, csp
= 0, jsp
= 0, optag
= 0, hdr
= 0;
181 size_t pc
= 0, dp
= 0, tdp
= 0, len
= 0;
184 unsigned char flags
= 0;
185 #define FLAG_INDEFINITE_LENGTH 0x01
186 #define FLAG_MATCHED 0x02
187 #define FLAG_LAST_MATCHED 0x04 /* Last tag matched */
188 #define FLAG_CONS 0x20 /* Corresponds to CONS bit in the opcode tag
189 * - ie. whether or not we are going to parse
193 #define NR_CONS_STACK 10
194 unsigned short cons_dp_stack
[NR_CONS_STACK
];
195 unsigned short cons_datalen_stack
[NR_CONS_STACK
];
196 unsigned char cons_hdrlen_stack
[NR_CONS_STACK
];
197 #define NR_JUMP_STACK 10
198 unsigned char jump_stack
[NR_JUMP_STACK
];
204 pr_debug("next_op: pc=\e[32m%zu\e[m/%zu dp=\e[33m%zu\e[m/%zu C=%d J=%d\n",
205 pc
, machlen
, dp
, datalen
, csp
, jsp
);
206 if (unlikely(pc
>= machlen
))
207 goto machine_overrun_error
;
209 if (unlikely(pc
+ asn1_op_lengths
[op
] > machlen
))
210 goto machine_overrun_error
;
212 /* If this command is meant to match a tag, then do that before
213 * evaluating the command.
215 if (op
<= ASN1_OP__MATCHES_TAG
) {
218 /* Skip conditional matches if possible */
219 if ((op
& ASN1_OP_MATCH__COND
&& flags
& FLAG_MATCHED
) ||
220 (op
& ASN1_OP_MATCH__SKIP
&& dp
== datalen
)) {
221 flags
&= ~FLAG_LAST_MATCHED
;
222 pc
+= asn1_op_lengths
[op
];
229 /* Extract a tag from the data */
230 if (unlikely(dp
>= datalen
- 1))
231 goto data_overrun_error
;
233 if (unlikely((tag
& 0x1f) == ASN1_LONG_TAG
))
234 goto long_tag_not_supported
;
236 if (op
& ASN1_OP_MATCH__ANY
) {
237 pr_debug("- any %02x\n", tag
);
239 /* Extract the tag from the machine
240 * - Either CONS or PRIM are permitted in the data if
241 * CONS is not set in the op stream, otherwise CONS
244 optag
= machine
[pc
+ 1];
245 flags
|= optag
& FLAG_CONS
;
247 /* Determine whether the tag matched */
249 tmp
&= ~(optag
& ASN1_CONS_BIT
);
250 pr_debug("- match? %02x %02x %02x\n", tag
, optag
, tmp
);
252 /* All odd-numbered tags are MATCH_OR_SKIP. */
253 if (op
& ASN1_OP_MATCH__SKIP
) {
254 pc
+= asn1_op_lengths
[op
];
261 flags
|= FLAG_MATCHED
;
265 if (unlikely(len
== ASN1_INDEFINITE_LENGTH
)) {
266 /* Indefinite length */
267 if (unlikely(!(tag
& ASN1_CONS_BIT
)))
268 goto indefinite_len_primitive
;
269 flags
|= FLAG_INDEFINITE_LENGTH
;
270 if (unlikely(2 > datalen
- dp
))
271 goto data_overrun_error
;
275 goto length_too_long
;
276 if (unlikely(dp
>= datalen
- n
))
277 goto data_overrun_error
;
279 for (len
= 0; n
> 0; n
--) {
283 if (unlikely(len
> datalen
- dp
))
284 goto data_overrun_error
;
288 if (flags
& FLAG_CONS
) {
289 /* For expected compound forms, we stack the positions
290 * of the start and end of the data.
292 if (unlikely(csp
>= NR_CONS_STACK
))
293 goto cons_stack_overflow
;
294 cons_dp_stack
[csp
] = dp
;
295 cons_hdrlen_stack
[csp
] = hdr
;
296 if (!(flags
& FLAG_INDEFINITE_LENGTH
)) {
297 cons_datalen_stack
[csp
] = datalen
;
300 cons_datalen_stack
[csp
] = 0;
305 pr_debug("- TAG: %02x %zu%s\n",
306 tag
, len
, flags
& FLAG_CONS
? " CONS" : "");
310 /* Decide how to handle the operation */
312 case ASN1_OP_MATCH_ANY_ACT
:
313 case ASN1_OP_MATCH_ANY_ACT_OR_SKIP
:
314 case ASN1_OP_COND_MATCH_ANY_ACT
:
315 case ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP
:
316 ret
= actions
[machine
[pc
+ 1]](context
, hdr
, tag
, data
+ dp
, len
);
321 case ASN1_OP_MATCH_ACT
:
322 case ASN1_OP_MATCH_ACT_OR_SKIP
:
323 case ASN1_OP_COND_MATCH_ACT_OR_SKIP
:
324 ret
= actions
[machine
[pc
+ 2]](context
, hdr
, tag
, data
+ dp
, len
);
330 case ASN1_OP_MATCH_OR_SKIP
:
331 case ASN1_OP_MATCH_ANY
:
332 case ASN1_OP_MATCH_ANY_OR_SKIP
:
333 case ASN1_OP_COND_MATCH_OR_SKIP
:
334 case ASN1_OP_COND_MATCH_ANY
:
335 case ASN1_OP_COND_MATCH_ANY_OR_SKIP
:
337 if (!(flags
& FLAG_CONS
)) {
338 if (flags
& FLAG_INDEFINITE_LENGTH
) {
339 ret
= asn1_find_indefinite_length(
340 data
, datalen
, &dp
, &len
, &errmsg
);
346 pr_debug("- LEAF: %zu\n", len
);
348 pc
+= asn1_op_lengths
[op
];
351 case ASN1_OP_MATCH_JUMP
:
352 case ASN1_OP_MATCH_JUMP_OR_SKIP
:
353 case ASN1_OP_COND_MATCH_JUMP_OR_SKIP
:
354 pr_debug("- MATCH_JUMP\n");
355 if (unlikely(jsp
== NR_JUMP_STACK
))
356 goto jump_stack_overflow
;
357 jump_stack
[jsp
++] = pc
+ asn1_op_lengths
[op
];
358 pc
= machine
[pc
+ 2];
361 case ASN1_OP_COND_FAIL
:
362 if (unlikely(!(flags
& FLAG_MATCHED
)))
364 pc
+= asn1_op_lengths
[op
];
367 case ASN1_OP_COMPLETE
:
368 if (unlikely(jsp
!= 0 || csp
!= 0)) {
369 pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
375 case ASN1_OP_END_SET
:
376 case ASN1_OP_END_SET_ACT
:
377 if (unlikely(!(flags
& FLAG_MATCHED
)))
379 case ASN1_OP_END_SEQ
:
380 case ASN1_OP_END_SET_OF
:
381 case ASN1_OP_END_SEQ_OF
:
382 case ASN1_OP_END_SEQ_ACT
:
383 case ASN1_OP_END_SET_OF_ACT
:
384 case ASN1_OP_END_SEQ_OF_ACT
:
385 if (unlikely(csp
<= 0))
386 goto cons_stack_underflow
;
388 tdp
= cons_dp_stack
[csp
];
389 hdr
= cons_hdrlen_stack
[csp
];
391 datalen
= cons_datalen_stack
[csp
];
392 pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
393 tdp
, dp
, len
, datalen
);
395 /* Indefinite length - check for the EOC. */
397 if (unlikely(datalen
- dp
< 2))
398 goto data_overrun_error
;
399 if (data
[dp
++] != 0) {
400 if (op
& ASN1_OP_END__OF
) {
403 pc
= machine
[pc
+ 1];
404 pr_debug("- continue\n");
413 if (dp
< len
&& (op
& ASN1_OP_END__OF
)) {
416 pc
= machine
[pc
+ 1];
417 pr_debug("- continue\n");
421 goto cons_length_error
;
423 pr_debug("- cons len l=%zu d=%zu\n", len
, dp
- tdp
);
426 if (op
& ASN1_OP_END__ACT
) {
428 if (op
& ASN1_OP_END__OF
)
429 act
= machine
[pc
+ 2];
431 act
= machine
[pc
+ 1];
432 ret
= actions
[act
](context
, hdr
, 0, data
+ tdp
, len
);
434 pc
+= asn1_op_lengths
[op
];
437 case ASN1_OP_MAYBE_ACT
:
438 if (!(flags
& FLAG_LAST_MATCHED
)) {
439 pc
+= asn1_op_lengths
[op
];
443 ret
= actions
[machine
[pc
+ 1]](context
, hdr
, tag
, data
+ tdp
, len
);
446 pc
+= asn1_op_lengths
[op
];
450 if (unlikely(jsp
<= 0))
451 goto jump_stack_underflow
;
452 pc
= jump_stack
[--jsp
];
453 flags
|= FLAG_MATCHED
| FLAG_LAST_MATCHED
;
460 /* Shouldn't reach here */
461 pr_err("ASN.1 decoder error: Found reserved opcode (%u) pc=%zu\n",
466 errmsg
= "Data overrun error";
468 machine_overrun_error
:
469 errmsg
= "Machine overrun error";
471 jump_stack_underflow
:
472 errmsg
= "Jump stack underflow";
475 errmsg
= "Jump stack overflow";
477 cons_stack_underflow
:
478 errmsg
= "Cons stack underflow";
481 errmsg
= "Cons stack overflow";
484 errmsg
= "Cons length error";
487 errmsg
= "Missing EOC in indefinite len cons";
490 errmsg
= "Invalid length EOC";
493 errmsg
= "Unsupported length";
495 indefinite_len_primitive
:
496 errmsg
= "Indefinite len primitive not permitted";
499 errmsg
= "Unexpected tag";
501 long_tag_not_supported
:
502 errmsg
= "Long tag not supported";
504 pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
505 errmsg
, pc
, dp
, optag
, tag
, len
);
508 EXPORT_SYMBOL_GPL(asn1_ber_decoder
);