blob: c3a768ae1a40bd5d3e67ac4814e7a9e33d94f993 [file] [log] [blame]
David Howells42d5ec22012-09-24 17:11:16 +01001/* Decoder for ASN.1 BER/DER/CER encoded bytestream
2 *
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
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>
17
18static const unsigned char asn1_op_lengths[ASN1_OP__NR] = {
19 /* OPC TAG JMP ACT */
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_RETURN] = 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,
45};
46
47/*
48 * Find the length of an indefinite length object
David Howellsdbadc172012-10-04 14:21:23 +010049 * @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
David Howells42d5ec22012-09-24 17:11:16 +010054 */
55static int asn1_find_indefinite_length(const unsigned char *data, size_t datalen,
David Howellsdbadc172012-10-04 14:21:23 +010056 size_t *_dp, size_t *_len,
57 const char **_errmsg)
David Howells42d5ec22012-09-24 17:11:16 +010058{
59 unsigned char tag, tmp;
David Howellsdbadc172012-10-04 14:21:23 +010060 size_t dp = *_dp, len, n;
David Howells42d5ec22012-09-24 17:11:16 +010061 int indef_level = 1;
62
63next_tag:
64 if (unlikely(datalen - dp < 2)) {
65 if (datalen == dp)
66 goto missing_eoc;
67 goto data_overrun_error;
68 }
69
70 /* Extract a tag from the data */
71 tag = data[dp++];
David Howells7da78072016-02-23 11:03:12 +000072 if (tag == ASN1_EOC) {
David Howells42d5ec22012-09-24 17:11:16 +010073 /* It appears to be an EOC. */
74 if (data[dp++] != 0)
75 goto invalid_eoc;
David Howellsdbadc172012-10-04 14:21:23 +010076 if (--indef_level <= 0) {
77 *_len = dp - *_dp;
78 *_dp = dp;
79 return 0;
80 }
David Howells42d5ec22012-09-24 17:11:16 +010081 goto next_tag;
82 }
83
David Howells99cca912012-12-05 11:55:30 +103084 if (unlikely((tag & 0x1f) == ASN1_LONG_TAG)) {
David Howells42d5ec22012-09-24 17:11:16 +010085 do {
86 if (unlikely(datalen - dp < 2))
87 goto data_overrun_error;
88 tmp = data[dp++];
89 } while (tmp & 0x80);
90 }
91
92 /* Extract the length */
93 len = data[dp++];
David Howells7da78072016-02-23 11:03:12 +000094 if (len <= 0x7f)
95 goto check_length;
David Howells42d5ec22012-09-24 17:11:16 +010096
David Howells99cca912012-12-05 11:55:30 +103097 if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
David Howells42d5ec22012-09-24 17:11:16 +010098 /* Indefinite length */
99 if (unlikely((tag & ASN1_CONS_BIT) == ASN1_PRIM << 5))
100 goto indefinite_len_primitive;
101 indef_level++;
102 goto next_tag;
103 }
104
105 n = len - 0x80;
David Howells7da78072016-02-23 11:03:12 +0000106 if (unlikely(n > sizeof(len) - 1))
David Howells42d5ec22012-09-24 17:11:16 +0100107 goto length_too_long;
108 if (unlikely(n > datalen - dp))
109 goto data_overrun_error;
David Howells7da78072016-02-23 11:03:12 +0000110 len = 0;
111 for (; n > 0; n--) {
David Howells42d5ec22012-09-24 17:11:16 +0100112 len <<= 8;
113 len |= data[dp++];
114 }
David Howells7da78072016-02-23 11:03:12 +0000115check_length:
116 if (len > datalen - dp)
117 goto data_overrun_error;
David Howells42d5ec22012-09-24 17:11:16 +0100118 dp += len;
119 goto next_tag;
120
121length_too_long:
122 *_errmsg = "Unsupported length";
123 goto error;
124indefinite_len_primitive:
125 *_errmsg = "Indefinite len primitive not permitted";
126 goto error;
127invalid_eoc:
128 *_errmsg = "Invalid length EOC";
129 goto error;
130data_overrun_error:
131 *_errmsg = "Data overrun error";
132 goto error;
133missing_eoc:
134 *_errmsg = "Missing EOC in indefinite len cons";
135error:
David Howellsdbadc172012-10-04 14:21:23 +0100136 *_dp = dp;
David Howells42d5ec22012-09-24 17:11:16 +0100137 return -1;
138}
139
140/**
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
Fabian Frederick548bbff2014-06-04 16:12:01 -0700145 * @datalen: The size of the encoded data
David Howells42d5ec22012-09-24 17:11:16 +0100146 *
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.
150 *
151 * LIMITATIONS:
152 *
153 * To keep down the amount of stack used by this function, the following limits
154 * have been imposed:
155 *
156 * (1) This won't handle datalen > 65535 without increasing the size of the
157 * cons stack elements and length_too_long checking.
158 *
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.
161 *
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.
164 */
165int asn1_ber_decoder(const struct asn1_decoder *decoder,
166 void *context,
167 const unsigned char *data,
168 size_t datalen)
169{
170 const unsigned char *machine = decoder->machine;
171 const asn1_action_t *actions = decoder->actions;
172 size_t machlen = decoder->machlen;
173 enum asn1_opcode op;
174 unsigned char tag = 0, csp = 0, jsp = 0, optag = 0, hdr = 0;
175 const char *errmsg;
176 size_t pc = 0, dp = 0, tdp = 0, len = 0;
177 int ret;
178
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
184 * a compound type.
185 */
186
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];
193
194 if (datalen > 65535)
195 return -EMSGSIZE;
196
197next_op:
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;
202 op = machine[pc];
203 if (unlikely(pc + asn1_op_lengths[op] > machlen))
204 goto machine_overrun_error;
205
206 /* If this command is meant to match a tag, then do that before
207 * evaluating the command.
208 */
209 if (op <= ASN1_OP__MATCHES_TAG) {
210 unsigned char tmp;
211
212 /* Skip conditional matches if possible */
David Howellsc66d9b72015-08-05 12:54:46 +0100213 if ((op & ASN1_OP_MATCH__COND && flags & FLAG_MATCHED) ||
214 (op & ASN1_OP_MATCH__SKIP && dp == datalen)) {
David Howells42d5ec22012-09-24 17:11:16 +0100215 pc += asn1_op_lengths[op];
216 goto next_op;
217 }
218
219 flags = 0;
220 hdr = 2;
221
222 /* Extract a tag from the data */
Eric Biggers1f166fb2017-11-07 22:29:02 +0000223 if (unlikely(datalen - dp < 2))
David Howells42d5ec22012-09-24 17:11:16 +0100224 goto data_overrun_error;
225 tag = data[dp++];
David Howells99cca912012-12-05 11:55:30 +1030226 if (unlikely((tag & 0x1f) == ASN1_LONG_TAG))
David Howells42d5ec22012-09-24 17:11:16 +0100227 goto long_tag_not_supported;
228
229 if (op & ASN1_OP_MATCH__ANY) {
230 pr_debug("- any %02x\n", tag);
231 } else {
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
235 * is mandatory.
236 */
237 optag = machine[pc + 1];
238 flags |= optag & FLAG_CONS;
239
240 /* Determine whether the tag matched */
241 tmp = optag ^ tag;
242 tmp &= ~(optag & ASN1_CONS_BIT);
243 pr_debug("- match? %02x %02x %02x\n", tag, optag, tmp);
244 if (tmp != 0) {
245 /* All odd-numbered tags are MATCH_OR_SKIP. */
246 if (op & ASN1_OP_MATCH__SKIP) {
247 pc += asn1_op_lengths[op];
248 dp--;
249 goto next_op;
250 }
251 goto tag_mismatch;
252 }
253 }
254 flags |= FLAG_MATCHED;
255
256 len = data[dp++];
257 if (len > 0x7f) {
David Howells99cca912012-12-05 11:55:30 +1030258 if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
David Howells42d5ec22012-09-24 17:11:16 +0100259 /* 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;
265 } else {
266 int n = len - 0x80;
267 if (unlikely(n > 2))
268 goto length_too_long;
Eric Biggers1f166fb2017-11-07 22:29:02 +0000269 if (unlikely(n > datalen - dp))
David Howells42d5ec22012-09-24 17:11:16 +0100270 goto data_overrun_error;
271 hdr += n;
272 for (len = 0; n > 0; n--) {
273 len <<= 8;
274 len |= data[dp++];
275 }
276 if (unlikely(len > datalen - dp))
277 goto data_overrun_error;
278 }
Eric Biggersef2518b2017-11-02 00:47:19 +0000279 } else {
280 if (unlikely(len > datalen - dp))
281 goto data_overrun_error;
David Howells42d5ec22012-09-24 17:11:16 +0100282 }
283
284 if (flags & FLAG_CONS) {
285 /* For expected compound forms, we stack the positions
286 * of the start and end of the data.
287 */
288 if (unlikely(csp >= NR_CONS_STACK))
289 goto cons_stack_overflow;
290 cons_dp_stack[csp] = dp;
291 cons_hdrlen_stack[csp] = hdr;
292 if (!(flags & FLAG_INDEFINITE_LENGTH)) {
293 cons_datalen_stack[csp] = datalen;
294 datalen = dp + len;
295 } else {
296 cons_datalen_stack[csp] = 0;
297 }
298 csp++;
299 }
300
301 pr_debug("- TAG: %02x %zu%s\n",
302 tag, len, flags & FLAG_CONS ? " CONS" : "");
303 tdp = dp;
304 }
305
306 /* Decide how to handle the operation */
307 switch (op) {
David Howells42d5ec22012-09-24 17:11:16 +0100308 case ASN1_OP_MATCH:
309 case ASN1_OP_MATCH_OR_SKIP:
Eric Biggers8f4007a2018-02-26 10:43:09 -0800310 case ASN1_OP_MATCH_ACT:
311 case ASN1_OP_MATCH_ACT_OR_SKIP:
David Howells42d5ec22012-09-24 17:11:16 +0100312 case ASN1_OP_MATCH_ANY:
Eric Biggers8f4007a2018-02-26 10:43:09 -0800313 case ASN1_OP_MATCH_ANY_ACT:
David Howells42d5ec22012-09-24 17:11:16 +0100314 case ASN1_OP_COND_MATCH_OR_SKIP:
Eric Biggers8f4007a2018-02-26 10:43:09 -0800315 case ASN1_OP_COND_MATCH_ACT_OR_SKIP:
David Howells42d5ec22012-09-24 17:11:16 +0100316 case ASN1_OP_COND_MATCH_ANY:
Eric Biggers8f4007a2018-02-26 10:43:09 -0800317 case ASN1_OP_COND_MATCH_ANY_ACT:
318
David Howells42d5ec22012-09-24 17:11:16 +0100319 if (!(flags & FLAG_CONS)) {
320 if (flags & FLAG_INDEFINITE_LENGTH) {
Eric Biggers8f4007a2018-02-26 10:43:09 -0800321 size_t tmp = dp;
322
David Howellsdbadc172012-10-04 14:21:23 +0100323 ret = asn1_find_indefinite_length(
Eric Biggers8f4007a2018-02-26 10:43:09 -0800324 data, datalen, &tmp, &len, &errmsg);
David Howellsdbadc172012-10-04 14:21:23 +0100325 if (ret < 0)
David Howells42d5ec22012-09-24 17:11:16 +0100326 goto error;
327 }
328 pr_debug("- LEAF: %zu\n", len);
David Howells42d5ec22012-09-24 17:11:16 +0100329 }
Eric Biggers8f4007a2018-02-26 10:43:09 -0800330
331 if (op & ASN1_OP_MATCH__ACT) {
332 unsigned char act;
333
334 if (op & ASN1_OP_MATCH__ANY)
335 act = machine[pc + 1];
336 else
337 act = machine[pc + 2];
338 ret = actions[act](context, hdr, tag, data + dp, len);
339 if (ret < 0)
340 return ret;
341 }
342
343 if (!(flags & FLAG_CONS))
344 dp += len;
David Howells42d5ec22012-09-24 17:11:16 +0100345 pc += asn1_op_lengths[op];
346 goto next_op;
347
348 case ASN1_OP_MATCH_JUMP:
349 case ASN1_OP_MATCH_JUMP_OR_SKIP:
350 case ASN1_OP_COND_MATCH_JUMP_OR_SKIP:
351 pr_debug("- MATCH_JUMP\n");
352 if (unlikely(jsp == NR_JUMP_STACK))
353 goto jump_stack_overflow;
354 jump_stack[jsp++] = pc + asn1_op_lengths[op];
355 pc = machine[pc + 2];
356 goto next_op;
357
358 case ASN1_OP_COND_FAIL:
359 if (unlikely(!(flags & FLAG_MATCHED)))
360 goto tag_mismatch;
361 pc += asn1_op_lengths[op];
362 goto next_op;
363
364 case ASN1_OP_COMPLETE:
365 if (unlikely(jsp != 0 || csp != 0)) {
366 pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
367 jsp, csp);
368 return -EBADMSG;
369 }
370 return 0;
371
372 case ASN1_OP_END_SET:
373 case ASN1_OP_END_SET_ACT:
374 if (unlikely(!(flags & FLAG_MATCHED)))
375 goto tag_mismatch;
376 case ASN1_OP_END_SEQ:
377 case ASN1_OP_END_SET_OF:
378 case ASN1_OP_END_SEQ_OF:
379 case ASN1_OP_END_SEQ_ACT:
380 case ASN1_OP_END_SET_OF_ACT:
381 case ASN1_OP_END_SEQ_OF_ACT:
382 if (unlikely(csp <= 0))
383 goto cons_stack_underflow;
384 csp--;
385 tdp = cons_dp_stack[csp];
386 hdr = cons_hdrlen_stack[csp];
387 len = datalen;
388 datalen = cons_datalen_stack[csp];
389 pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
390 tdp, dp, len, datalen);
391 if (datalen == 0) {
392 /* Indefinite length - check for the EOC. */
393 datalen = len;
394 if (unlikely(datalen - dp < 2))
395 goto data_overrun_error;
396 if (data[dp++] != 0) {
397 if (op & ASN1_OP_END__OF) {
398 dp--;
399 csp++;
400 pc = machine[pc + 1];
401 pr_debug("- continue\n");
402 goto next_op;
403 }
404 goto missing_eoc;
405 }
406 if (data[dp++] != 0)
407 goto invalid_eoc;
408 len = dp - tdp - 2;
409 } else {
410 if (dp < len && (op & ASN1_OP_END__OF)) {
411 datalen = len;
412 csp++;
413 pc = machine[pc + 1];
414 pr_debug("- continue\n");
415 goto next_op;
416 }
417 if (dp != len)
418 goto cons_length_error;
419 len -= tdp;
420 pr_debug("- cons len l=%zu d=%zu\n", len, dp - tdp);
421 }
422
423 if (op & ASN1_OP_END__ACT) {
424 unsigned char act;
425 if (op & ASN1_OP_END__OF)
426 act = machine[pc + 2];
427 else
428 act = machine[pc + 1];
429 ret = actions[act](context, hdr, 0, data + tdp, len);
Eric Biggers602a1c72017-12-08 15:13:27 +0000430 if (ret < 0)
431 return ret;
David Howells42d5ec22012-09-24 17:11:16 +0100432 }
433 pc += asn1_op_lengths[op];
434 goto next_op;
435
436 case ASN1_OP_ACT:
437 ret = actions[machine[pc + 1]](context, hdr, tag, data + tdp, len);
438 pc += asn1_op_lengths[op];
439 goto next_op;
440
441 case ASN1_OP_RETURN:
442 if (unlikely(jsp <= 0))
443 goto jump_stack_underflow;
444 pc = jump_stack[--jsp];
445 goto next_op;
446
447 default:
448 break;
449 }
450
451 /* Shouldn't reach here */
452 pr_err("ASN.1 decoder error: Found reserved opcode (%u)\n", op);
453 return -EBADMSG;
454
455data_overrun_error:
456 errmsg = "Data overrun error";
457 goto error;
458machine_overrun_error:
459 errmsg = "Machine overrun error";
460 goto error;
461jump_stack_underflow:
462 errmsg = "Jump stack underflow";
463 goto error;
464jump_stack_overflow:
465 errmsg = "Jump stack overflow";
466 goto error;
467cons_stack_underflow:
468 errmsg = "Cons stack underflow";
469 goto error;
470cons_stack_overflow:
471 errmsg = "Cons stack overflow";
472 goto error;
473cons_length_error:
474 errmsg = "Cons length error";
475 goto error;
476missing_eoc:
477 errmsg = "Missing EOC in indefinite len cons";
478 goto error;
479invalid_eoc:
480 errmsg = "Invalid length EOC";
481 goto error;
482length_too_long:
483 errmsg = "Unsupported length";
484 goto error;
485indefinite_len_primitive:
486 errmsg = "Indefinite len primitive not permitted";
487 goto error;
488tag_mismatch:
489 errmsg = "Unexpected tag";
490 goto error;
491long_tag_not_supported:
492 errmsg = "Long tag not supported";
493error:
494 pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
495 errmsg, pc, dp, optag, tag, len);
496 return -EBADMSG;
497}
498EXPORT_SYMBOL_GPL(asn1_ber_decoder);