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 */
73 /* It appears to be an EOC. */
76 if (--indef_level
<= 0) {
84 if (unlikely((tag
& 0x1f) == 0x1f)) {
86 if (unlikely(datalen
- dp
< 2))
87 goto data_overrun_error
;
92 /* Extract the length */
99 if (unlikely(len
== 0x80)) {
100 /* Indefinite length */
101 if (unlikely((tag
& ASN1_CONS_BIT
) == ASN1_PRIM
<< 5))
102 goto indefinite_len_primitive
;
108 if (unlikely(n
> sizeof(size_t) - 1))
109 goto length_too_long
;
110 if (unlikely(n
> datalen
- dp
))
111 goto data_overrun_error
;
112 for (len
= 0; n
> 0; n
--) {
120 *_errmsg
= "Unsupported length";
122 indefinite_len_primitive
:
123 *_errmsg
= "Indefinite len primitive not permitted";
126 *_errmsg
= "Invalid length EOC";
129 *_errmsg
= "Data overrun error";
132 *_errmsg
= "Missing EOC in indefinite len cons";
139 * asn1_ber_decoder - Decoder BER/DER/CER ASN.1 according to pattern
140 * @decoder: The decoder definition (produced by asn1_compiler)
141 * @context: The caller's context (to be passed to the action functions)
142 * @data: The encoded data
143 * @datasize: The size of the encoded data
145 * Decode BER/DER/CER encoded ASN.1 data according to a bytecode pattern
146 * produced by asn1_compiler. Action functions are called on marked tags to
147 * allow the caller to retrieve significant data.
151 * To keep down the amount of stack used by this function, the following limits
154 * (1) This won't handle datalen > 65535 without increasing the size of the
155 * cons stack elements and length_too_long checking.
157 * (2) The stack of constructed types is 10 deep. If the depth of non-leaf
158 * constructed types exceeds this, the decode will fail.
160 * (3) The SET type (not the SET OF type) isn't really supported as tracking
161 * what members of the set have been seen is a pain.
163 int asn1_ber_decoder(const struct asn1_decoder
*decoder
,
165 const unsigned char *data
,
168 const unsigned char *machine
= decoder
->machine
;
169 const asn1_action_t
*actions
= decoder
->actions
;
170 size_t machlen
= decoder
->machlen
;
172 unsigned char tag
= 0, csp
= 0, jsp
= 0, optag
= 0, hdr
= 0;
174 size_t pc
= 0, dp
= 0, tdp
= 0, len
= 0;
177 unsigned char flags
= 0;
178 #define FLAG_INDEFINITE_LENGTH 0x01
179 #define FLAG_MATCHED 0x02
180 #define FLAG_CONS 0x20 /* Corresponds to CONS bit in the opcode tag
181 * - ie. whether or not we are going to parse
185 #define NR_CONS_STACK 10
186 unsigned short cons_dp_stack
[NR_CONS_STACK
];
187 unsigned short cons_datalen_stack
[NR_CONS_STACK
];
188 unsigned char cons_hdrlen_stack
[NR_CONS_STACK
];
189 #define NR_JUMP_STACK 10
190 unsigned char jump_stack
[NR_JUMP_STACK
];
196 pr_debug("next_op: pc=\e[32m%zu\e[m/%zu dp=\e[33m%zu\e[m/%zu C=%d J=%d\n",
197 pc
, machlen
, dp
, datalen
, csp
, jsp
);
198 if (unlikely(pc
>= machlen
))
199 goto machine_overrun_error
;
201 if (unlikely(pc
+ asn1_op_lengths
[op
] > machlen
))
202 goto machine_overrun_error
;
204 /* If this command is meant to match a tag, then do that before
205 * evaluating the command.
207 if (op
<= ASN1_OP__MATCHES_TAG
) {
210 /* Skip conditional matches if possible */
211 if ((op
& ASN1_OP_MATCH__COND
&&
212 flags
& FLAG_MATCHED
) ||
214 pc
+= asn1_op_lengths
[op
];
221 /* Extract a tag from the data */
222 if (unlikely(dp
>= datalen
- 1))
223 goto data_overrun_error
;
225 if (unlikely((tag
& 0x1f) == 0x1f))
226 goto long_tag_not_supported
;
228 if (op
& ASN1_OP_MATCH__ANY
) {
229 pr_debug("- any %02x\n", tag
);
231 /* Extract the tag from the machine
232 * - Either CONS or PRIM are permitted in the data if
233 * CONS is not set in the op stream, otherwise CONS
236 optag
= machine
[pc
+ 1];
237 flags
|= optag
& FLAG_CONS
;
239 /* Determine whether the tag matched */
241 tmp
&= ~(optag
& ASN1_CONS_BIT
);
242 pr_debug("- match? %02x %02x %02x\n", tag
, optag
, tmp
);
244 /* All odd-numbered tags are MATCH_OR_SKIP. */
245 if (op
& ASN1_OP_MATCH__SKIP
) {
246 pc
+= asn1_op_lengths
[op
];
253 flags
|= FLAG_MATCHED
;
257 if (unlikely(len
== 0x80)) {
258 /* Indefinite length */
259 if (unlikely(!(tag
& ASN1_CONS_BIT
)))
260 goto indefinite_len_primitive
;
261 flags
|= FLAG_INDEFINITE_LENGTH
;
262 if (unlikely(2 > datalen
- dp
))
263 goto data_overrun_error
;
267 goto length_too_long
;
268 if (unlikely(dp
>= datalen
- n
))
269 goto data_overrun_error
;
271 for (len
= 0; n
> 0; n
--) {
275 if (unlikely(len
> datalen
- dp
))
276 goto data_overrun_error
;
280 if (flags
& FLAG_CONS
) {
281 /* For expected compound forms, we stack the positions
282 * of the start and end of the data.
284 if (unlikely(csp
>= NR_CONS_STACK
))
285 goto cons_stack_overflow
;
286 cons_dp_stack
[csp
] = dp
;
287 cons_hdrlen_stack
[csp
] = hdr
;
288 if (!(flags
& FLAG_INDEFINITE_LENGTH
)) {
289 cons_datalen_stack
[csp
] = datalen
;
292 cons_datalen_stack
[csp
] = 0;
297 pr_debug("- TAG: %02x %zu%s\n",
298 tag
, len
, flags
& FLAG_CONS
? " CONS" : "");
302 /* Decide how to handle the operation */
304 case ASN1_OP_MATCH_ANY_ACT
:
305 case ASN1_OP_COND_MATCH_ANY_ACT
:
306 ret
= actions
[machine
[pc
+ 1]](context
, hdr
, tag
, data
+ dp
, len
);
311 case ASN1_OP_MATCH_ACT
:
312 case ASN1_OP_MATCH_ACT_OR_SKIP
:
313 case ASN1_OP_COND_MATCH_ACT_OR_SKIP
:
314 ret
= actions
[machine
[pc
+ 2]](context
, hdr
, tag
, data
+ dp
, len
);
320 case ASN1_OP_MATCH_OR_SKIP
:
321 case ASN1_OP_MATCH_ANY
:
322 case ASN1_OP_COND_MATCH_OR_SKIP
:
323 case ASN1_OP_COND_MATCH_ANY
:
325 if (!(flags
& FLAG_CONS
)) {
326 if (flags
& FLAG_INDEFINITE_LENGTH
) {
327 ret
= asn1_find_indefinite_length(
328 data
, datalen
, &dp
, &len
, &errmsg
);
334 pr_debug("- LEAF: %zu\n", len
);
336 pc
+= asn1_op_lengths
[op
];
339 case ASN1_OP_MATCH_JUMP
:
340 case ASN1_OP_MATCH_JUMP_OR_SKIP
:
341 case ASN1_OP_COND_MATCH_JUMP_OR_SKIP
:
342 pr_debug("- MATCH_JUMP\n");
343 if (unlikely(jsp
== NR_JUMP_STACK
))
344 goto jump_stack_overflow
;
345 jump_stack
[jsp
++] = pc
+ asn1_op_lengths
[op
];
346 pc
= machine
[pc
+ 2];
349 case ASN1_OP_COND_FAIL
:
350 if (unlikely(!(flags
& FLAG_MATCHED
)))
352 pc
+= asn1_op_lengths
[op
];
355 case ASN1_OP_COMPLETE
:
356 if (unlikely(jsp
!= 0 || csp
!= 0)) {
357 pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
363 case ASN1_OP_END_SET
:
364 case ASN1_OP_END_SET_ACT
:
365 if (unlikely(!(flags
& FLAG_MATCHED
)))
367 case ASN1_OP_END_SEQ
:
368 case ASN1_OP_END_SET_OF
:
369 case ASN1_OP_END_SEQ_OF
:
370 case ASN1_OP_END_SEQ_ACT
:
371 case ASN1_OP_END_SET_OF_ACT
:
372 case ASN1_OP_END_SEQ_OF_ACT
:
373 if (unlikely(csp
<= 0))
374 goto cons_stack_underflow
;
376 tdp
= cons_dp_stack
[csp
];
377 hdr
= cons_hdrlen_stack
[csp
];
379 datalen
= cons_datalen_stack
[csp
];
380 pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
381 tdp
, dp
, len
, datalen
);
383 /* Indefinite length - check for the EOC. */
385 if (unlikely(datalen
- dp
< 2))
386 goto data_overrun_error
;
387 if (data
[dp
++] != 0) {
388 if (op
& ASN1_OP_END__OF
) {
391 pc
= machine
[pc
+ 1];
392 pr_debug("- continue\n");
401 if (dp
< len
&& (op
& ASN1_OP_END__OF
)) {
404 pc
= machine
[pc
+ 1];
405 pr_debug("- continue\n");
409 goto cons_length_error
;
411 pr_debug("- cons len l=%zu d=%zu\n", len
, dp
- tdp
);
414 if (op
& ASN1_OP_END__ACT
) {
416 if (op
& ASN1_OP_END__OF
)
417 act
= machine
[pc
+ 2];
419 act
= machine
[pc
+ 1];
420 ret
= actions
[act
](context
, hdr
, 0, data
+ tdp
, len
);
422 pc
+= asn1_op_lengths
[op
];
426 ret
= actions
[machine
[pc
+ 1]](context
, hdr
, tag
, data
+ tdp
, len
);
427 pc
+= asn1_op_lengths
[op
];
431 if (unlikely(jsp
<= 0))
432 goto jump_stack_underflow
;
433 pc
= jump_stack
[--jsp
];
440 /* Shouldn't reach here */
441 pr_err("ASN.1 decoder error: Found reserved opcode (%u)\n", op
);
445 errmsg
= "Data overrun error";
447 machine_overrun_error
:
448 errmsg
= "Machine overrun error";
450 jump_stack_underflow
:
451 errmsg
= "Jump stack underflow";
454 errmsg
= "Jump stack overflow";
456 cons_stack_underflow
:
457 errmsg
= "Cons stack underflow";
460 errmsg
= "Cons stack overflow";
463 errmsg
= "Cons length error";
466 errmsg
= "Missing EOC in indefinite len cons";
469 errmsg
= "Invalid length EOC";
472 errmsg
= "Unsupported length";
474 indefinite_len_primitive
:
475 errmsg
= "Indefinite len primitive not permitted";
478 errmsg
= "Unexpected tag";
480 long_tag_not_supported
:
481 errmsg
= "Long tag not supported";
483 pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
484 errmsg
, pc
, dp
, optag
, tag
, len
);
487 EXPORT_SYMBOL_GPL(asn1_ber_decoder
);