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,
37 [ASN1_OP_END_SEQ
] = 1,
38 [ASN1_OP_END_SEQ_OF
] = 1 + 1,
39 [ASN1_OP_END_SET
] = 1,
40 [ASN1_OP_END_SET_OF
] = 1 + 1,
41 [ASN1_OP_END_SEQ_ACT
] = 1 + 1,
42 [ASN1_OP_END_SEQ_OF_ACT
] = 1 + 1 + 1,
43 [ASN1_OP_END_SET_ACT
] = 1 + 1,
44 [ASN1_OP_END_SET_OF_ACT
] = 1 + 1 + 1,
48 * Find the length of an indefinite length object
49 * @data: The data buffer
50 * @datalen: The end of the innermost containing element in the buffer
51 * @_dp: The data parse cursor (updated before returning)
52 * @_len: Where to return the size of the element.
53 * @_errmsg: Where to return a pointer to an error message on error
55 static int asn1_find_indefinite_length(const unsigned char *data
, size_t datalen
,
56 size_t *_dp
, size_t *_len
,
59 unsigned char tag
, tmp
;
60 size_t dp
= *_dp
, len
, n
;
64 if (unlikely(datalen
- dp
< 2)) {
67 goto data_overrun_error
;
70 /* Extract a tag from the data */
72 if (tag
== ASN1_EOC
) {
73 /* It appears to be an EOC. */
76 if (--indef_level
<= 0) {
84 if (unlikely((tag
& 0x1f) == ASN1_LONG_TAG
)) {
86 if (unlikely(datalen
- dp
< 2))
87 goto data_overrun_error
;
92 /* Extract the length */
97 if (unlikely(len
== ASN1_INDEFINITE_LENGTH
)) {
98 /* Indefinite length */
99 if (unlikely((tag
& ASN1_CONS_BIT
) == ASN1_PRIM
<< 5))
100 goto indefinite_len_primitive
;
106 if (unlikely(n
> sizeof(len
) - 1))
107 goto length_too_long
;
108 if (unlikely(n
> datalen
- dp
))
109 goto data_overrun_error
;
116 if (len
> datalen
- dp
)
117 goto data_overrun_error
;
122 *_errmsg
= "Unsupported length";
124 indefinite_len_primitive
:
125 *_errmsg
= "Indefinite len primitive not permitted";
128 *_errmsg
= "Invalid length EOC";
131 *_errmsg
= "Data overrun error";
134 *_errmsg
= "Missing EOC in indefinite len cons";
141 * asn1_ber_decoder - Decoder BER/DER/CER ASN.1 according to pattern
142 * @decoder: The decoder definition (produced by asn1_compiler)
143 * @context: The caller's context (to be passed to the action functions)
144 * @data: The encoded data
145 * @datalen: The size of the encoded data
147 * Decode BER/DER/CER encoded ASN.1 data according to a bytecode pattern
148 * produced by asn1_compiler. Action functions are called on marked tags to
149 * allow the caller to retrieve significant data.
153 * To keep down the amount of stack used by this function, the following limits
156 * (1) This won't handle datalen > 65535 without increasing the size of the
157 * cons stack elements and length_too_long checking.
159 * (2) The stack of constructed types is 10 deep. If the depth of non-leaf
160 * constructed types exceeds this, the decode will fail.
162 * (3) The SET type (not the SET OF type) isn't really supported as tracking
163 * what members of the set have been seen is a pain.
165 int asn1_ber_decoder(const struct asn1_decoder
*decoder
,
167 const unsigned char *data
,
170 const unsigned char *machine
= decoder
->machine
;
171 const asn1_action_t
*actions
= decoder
->actions
;
172 size_t machlen
= decoder
->machlen
;
174 unsigned char tag
= 0, csp
= 0, jsp
= 0, optag
= 0, hdr
= 0;
176 size_t pc
= 0, dp
= 0, tdp
= 0, len
= 0;
179 unsigned char flags
= 0;
180 #define FLAG_INDEFINITE_LENGTH 0x01
181 #define FLAG_MATCHED 0x02
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
&& flags
& FLAG_MATCHED
) ||
214 (op
& ASN1_OP_MATCH__SKIP
&& dp
== datalen
)) {
215 pc
+= asn1_op_lengths
[op
];
222 /* Extract a tag from the data */
223 if (unlikely(dp
>= datalen
- 1))
224 goto data_overrun_error
;
226 if (unlikely((tag
& 0x1f) == ASN1_LONG_TAG
))
227 goto long_tag_not_supported
;
229 if (op
& ASN1_OP_MATCH__ANY
) {
230 pr_debug("- any %02x\n", tag
);
232 /* Extract the tag from the machine
233 * - Either CONS or PRIM are permitted in the data if
234 * CONS is not set in the op stream, otherwise CONS
237 optag
= machine
[pc
+ 1];
238 flags
|= optag
& FLAG_CONS
;
240 /* Determine whether the tag matched */
242 tmp
&= ~(optag
& ASN1_CONS_BIT
);
243 pr_debug("- match? %02x %02x %02x\n", tag
, optag
, tmp
);
245 /* All odd-numbered tags are MATCH_OR_SKIP. */
246 if (op
& ASN1_OP_MATCH__SKIP
) {
247 pc
+= asn1_op_lengths
[op
];
254 flags
|= FLAG_MATCHED
;
258 if (unlikely(len
== ASN1_INDEFINITE_LENGTH
)) {
259 /* Indefinite length */
260 if (unlikely(!(tag
& ASN1_CONS_BIT
)))
261 goto indefinite_len_primitive
;
262 flags
|= FLAG_INDEFINITE_LENGTH
;
263 if (unlikely(2 > datalen
- dp
))
264 goto data_overrun_error
;
268 goto length_too_long
;
269 if (unlikely(dp
>= datalen
- n
))
270 goto data_overrun_error
;
272 for (len
= 0; n
> 0; n
--) {
276 if (unlikely(len
> datalen
- dp
))
277 goto data_overrun_error
;
281 if (flags
& FLAG_CONS
) {
282 /* For expected compound forms, we stack the positions
283 * of the start and end of the data.
285 if (unlikely(csp
>= NR_CONS_STACK
))
286 goto cons_stack_overflow
;
287 cons_dp_stack
[csp
] = dp
;
288 cons_hdrlen_stack
[csp
] = hdr
;
289 if (!(flags
& FLAG_INDEFINITE_LENGTH
)) {
290 cons_datalen_stack
[csp
] = datalen
;
293 cons_datalen_stack
[csp
] = 0;
298 pr_debug("- TAG: %02x %zu%s\n",
299 tag
, len
, flags
& FLAG_CONS
? " CONS" : "");
303 /* Decide how to handle the operation */
305 case ASN1_OP_MATCH_ANY_ACT
:
306 case ASN1_OP_COND_MATCH_ANY_ACT
:
307 ret
= actions
[machine
[pc
+ 1]](context
, hdr
, tag
, data
+ dp
, len
);
312 case ASN1_OP_MATCH_ACT
:
313 case ASN1_OP_MATCH_ACT_OR_SKIP
:
314 case ASN1_OP_COND_MATCH_ACT_OR_SKIP
:
315 ret
= actions
[machine
[pc
+ 2]](context
, hdr
, tag
, data
+ dp
, len
);
321 case ASN1_OP_MATCH_OR_SKIP
:
322 case ASN1_OP_MATCH_ANY
:
323 case ASN1_OP_COND_MATCH_OR_SKIP
:
324 case ASN1_OP_COND_MATCH_ANY
:
326 if (!(flags
& FLAG_CONS
)) {
327 if (flags
& FLAG_INDEFINITE_LENGTH
) {
328 ret
= asn1_find_indefinite_length(
329 data
, datalen
, &dp
, &len
, &errmsg
);
335 pr_debug("- LEAF: %zu\n", len
);
337 pc
+= asn1_op_lengths
[op
];
340 case ASN1_OP_MATCH_JUMP
:
341 case ASN1_OP_MATCH_JUMP_OR_SKIP
:
342 case ASN1_OP_COND_MATCH_JUMP_OR_SKIP
:
343 pr_debug("- MATCH_JUMP\n");
344 if (unlikely(jsp
== NR_JUMP_STACK
))
345 goto jump_stack_overflow
;
346 jump_stack
[jsp
++] = pc
+ asn1_op_lengths
[op
];
347 pc
= machine
[pc
+ 2];
350 case ASN1_OP_COND_FAIL
:
351 if (unlikely(!(flags
& FLAG_MATCHED
)))
353 pc
+= asn1_op_lengths
[op
];
356 case ASN1_OP_COMPLETE
:
357 if (unlikely(jsp
!= 0 || csp
!= 0)) {
358 pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
364 case ASN1_OP_END_SET
:
365 case ASN1_OP_END_SET_ACT
:
366 if (unlikely(!(flags
& FLAG_MATCHED
)))
368 case ASN1_OP_END_SEQ
:
369 case ASN1_OP_END_SET_OF
:
370 case ASN1_OP_END_SEQ_OF
:
371 case ASN1_OP_END_SEQ_ACT
:
372 case ASN1_OP_END_SET_OF_ACT
:
373 case ASN1_OP_END_SEQ_OF_ACT
:
374 if (unlikely(csp
<= 0))
375 goto cons_stack_underflow
;
377 tdp
= cons_dp_stack
[csp
];
378 hdr
= cons_hdrlen_stack
[csp
];
380 datalen
= cons_datalen_stack
[csp
];
381 pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
382 tdp
, dp
, len
, datalen
);
384 /* Indefinite length - check for the EOC. */
386 if (unlikely(datalen
- dp
< 2))
387 goto data_overrun_error
;
388 if (data
[dp
++] != 0) {
389 if (op
& ASN1_OP_END__OF
) {
392 pc
= machine
[pc
+ 1];
393 pr_debug("- continue\n");
402 if (dp
< len
&& (op
& ASN1_OP_END__OF
)) {
405 pc
= machine
[pc
+ 1];
406 pr_debug("- continue\n");
410 goto cons_length_error
;
412 pr_debug("- cons len l=%zu d=%zu\n", len
, dp
- tdp
);
415 if (op
& ASN1_OP_END__ACT
) {
417 if (op
& ASN1_OP_END__OF
)
418 act
= machine
[pc
+ 2];
420 act
= machine
[pc
+ 1];
421 ret
= actions
[act
](context
, hdr
, 0, data
+ tdp
, len
);
423 pc
+= asn1_op_lengths
[op
];
427 ret
= actions
[machine
[pc
+ 1]](context
, hdr
, tag
, data
+ tdp
, len
);
428 pc
+= asn1_op_lengths
[op
];
432 if (unlikely(jsp
<= 0))
433 goto jump_stack_underflow
;
434 pc
= jump_stack
[--jsp
];
441 /* Shouldn't reach here */
442 pr_err("ASN.1 decoder error: Found reserved opcode (%u)\n", op
);
446 errmsg
= "Data overrun error";
448 machine_overrun_error
:
449 errmsg
= "Machine overrun error";
451 jump_stack_underflow
:
452 errmsg
= "Jump stack underflow";
455 errmsg
= "Jump stack overflow";
457 cons_stack_underflow
:
458 errmsg
= "Cons stack underflow";
461 errmsg
= "Cons stack overflow";
464 errmsg
= "Cons length error";
467 errmsg
= "Missing EOC in indefinite len cons";
470 errmsg
= "Invalid length EOC";
473 errmsg
= "Unsupported length";
475 indefinite_len_primitive
:
476 errmsg
= "Indefinite len primitive not permitted";
479 errmsg
= "Unexpected tag";
481 long_tag_not_supported
:
482 errmsg
= "Long tag not supported";
484 pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
485 errmsg
, pc
, dp
, optag
, tag
, len
);
488 EXPORT_SYMBOL_GPL(asn1_ber_decoder
);