blob: d60ce8a536503616166f240a543b2d96a9642f45 [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++];
72 if (tag == 0) {
73 /* 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 Howellsf3537f92012-10-22 15:05:55 +010094 if (len <= 0x7f) {
David Howells42d5ec22012-09-24 17:11:16 +010095 dp += len;
96 goto next_tag;
97 }
98
David Howells99cca912012-12-05 11:55:30 +103099 if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
David Howells42d5ec22012-09-24 17:11:16 +0100100 /* Indefinite length */
101 if (unlikely((tag & ASN1_CONS_BIT) == ASN1_PRIM << 5))
102 goto indefinite_len_primitive;
103 indef_level++;
104 goto next_tag;
105 }
106
107 n = len - 0x80;
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--) {
113 len <<= 8;
114 len |= data[dp++];
115 }
116 dp += len;
117 goto next_tag;
118
119length_too_long:
120 *_errmsg = "Unsupported length";
121 goto error;
122indefinite_len_primitive:
123 *_errmsg = "Indefinite len primitive not permitted";
124 goto error;
125invalid_eoc:
126 *_errmsg = "Invalid length EOC";
127 goto error;
128data_overrun_error:
129 *_errmsg = "Data overrun error";
130 goto error;
131missing_eoc:
132 *_errmsg = "Missing EOC in indefinite len cons";
133error:
David Howellsdbadc172012-10-04 14:21:23 +0100134 *_dp = dp;
David Howells42d5ec22012-09-24 17:11:16 +0100135 return -1;
136}
137
138/**
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
Fabian Frederick548bbff2014-06-04 16:12:01 -0700143 * @datalen: The size of the encoded data
David Howells42d5ec22012-09-24 17:11:16 +0100144 *
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.
148 *
149 * LIMITATIONS:
150 *
151 * To keep down the amount of stack used by this function, the following limits
152 * have been imposed:
153 *
154 * (1) This won't handle datalen > 65535 without increasing the size of the
155 * cons stack elements and length_too_long checking.
156 *
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.
159 *
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.
162 */
163int asn1_ber_decoder(const struct asn1_decoder *decoder,
164 void *context,
165 const unsigned char *data,
166 size_t datalen)
167{
168 const unsigned char *machine = decoder->machine;
169 const asn1_action_t *actions = decoder->actions;
170 size_t machlen = decoder->machlen;
171 enum asn1_opcode op;
172 unsigned char tag = 0, csp = 0, jsp = 0, optag = 0, hdr = 0;
173 const char *errmsg;
174 size_t pc = 0, dp = 0, tdp = 0, len = 0;
175 int ret;
176
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
182 * a compound type.
183 */
184
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];
191
192 if (datalen > 65535)
193 return -EMSGSIZE;
194
195next_op:
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;
200 op = machine[pc];
201 if (unlikely(pc + asn1_op_lengths[op] > machlen))
202 goto machine_overrun_error;
203
204 /* If this command is meant to match a tag, then do that before
205 * evaluating the command.
206 */
207 if (op <= ASN1_OP__MATCHES_TAG) {
208 unsigned char tmp;
209
210 /* Skip conditional matches if possible */
David Howells906458b2016-07-11 14:21:56 -0700211 if ((op & ASN1_OP_MATCH__COND && flags & FLAG_MATCHED) ||
212 (op & ASN1_OP_MATCH__SKIP && dp == datalen)) {
David Howells42d5ec22012-09-24 17:11:16 +0100213 pc += asn1_op_lengths[op];
214 goto next_op;
215 }
216
217 flags = 0;
218 hdr = 2;
219
220 /* Extract a tag from the data */
221 if (unlikely(dp >= datalen - 1))
222 goto data_overrun_error;
223 tag = data[dp++];
David Howells99cca912012-12-05 11:55:30 +1030224 if (unlikely((tag & 0x1f) == ASN1_LONG_TAG))
David Howells42d5ec22012-09-24 17:11:16 +0100225 goto long_tag_not_supported;
226
227 if (op & ASN1_OP_MATCH__ANY) {
228 pr_debug("- any %02x\n", tag);
229 } else {
230 /* Extract the tag from the machine
231 * - Either CONS or PRIM are permitted in the data if
232 * CONS is not set in the op stream, otherwise CONS
233 * is mandatory.
234 */
235 optag = machine[pc + 1];
236 flags |= optag & FLAG_CONS;
237
238 /* Determine whether the tag matched */
239 tmp = optag ^ tag;
240 tmp &= ~(optag & ASN1_CONS_BIT);
241 pr_debug("- match? %02x %02x %02x\n", tag, optag, tmp);
242 if (tmp != 0) {
243 /* All odd-numbered tags are MATCH_OR_SKIP. */
244 if (op & ASN1_OP_MATCH__SKIP) {
245 pc += asn1_op_lengths[op];
246 dp--;
247 goto next_op;
248 }
249 goto tag_mismatch;
250 }
251 }
252 flags |= FLAG_MATCHED;
253
254 len = data[dp++];
255 if (len > 0x7f) {
David Howells99cca912012-12-05 11:55:30 +1030256 if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
David Howells42d5ec22012-09-24 17:11:16 +0100257 /* Indefinite length */
258 if (unlikely(!(tag & ASN1_CONS_BIT)))
259 goto indefinite_len_primitive;
260 flags |= FLAG_INDEFINITE_LENGTH;
261 if (unlikely(2 > datalen - dp))
262 goto data_overrun_error;
263 } else {
264 int n = len - 0x80;
265 if (unlikely(n > 2))
266 goto length_too_long;
267 if (unlikely(dp >= datalen - n))
268 goto data_overrun_error;
269 hdr += n;
270 for (len = 0; n > 0; n--) {
271 len <<= 8;
272 len |= data[dp++];
273 }
274 if (unlikely(len > datalen - dp))
275 goto data_overrun_error;
276 }
277 }
278
279 if (flags & FLAG_CONS) {
280 /* For expected compound forms, we stack the positions
281 * of the start and end of the data.
282 */
283 if (unlikely(csp >= NR_CONS_STACK))
284 goto cons_stack_overflow;
285 cons_dp_stack[csp] = dp;
286 cons_hdrlen_stack[csp] = hdr;
287 if (!(flags & FLAG_INDEFINITE_LENGTH)) {
288 cons_datalen_stack[csp] = datalen;
289 datalen = dp + len;
290 } else {
291 cons_datalen_stack[csp] = 0;
292 }
293 csp++;
294 }
295
296 pr_debug("- TAG: %02x %zu%s\n",
297 tag, len, flags & FLAG_CONS ? " CONS" : "");
298 tdp = dp;
299 }
300
301 /* Decide how to handle the operation */
302 switch (op) {
303 case ASN1_OP_MATCH_ANY_ACT:
304 case ASN1_OP_COND_MATCH_ANY_ACT:
305 ret = actions[machine[pc + 1]](context, hdr, tag, data + dp, len);
306 if (ret < 0)
307 return ret;
308 goto skip_data;
309
310 case ASN1_OP_MATCH_ACT:
311 case ASN1_OP_MATCH_ACT_OR_SKIP:
312 case ASN1_OP_COND_MATCH_ACT_OR_SKIP:
313 ret = actions[machine[pc + 2]](context, hdr, tag, data + dp, len);
314 if (ret < 0)
315 return ret;
316 goto skip_data;
317
318 case ASN1_OP_MATCH:
319 case ASN1_OP_MATCH_OR_SKIP:
320 case ASN1_OP_MATCH_ANY:
321 case ASN1_OP_COND_MATCH_OR_SKIP:
322 case ASN1_OP_COND_MATCH_ANY:
323 skip_data:
324 if (!(flags & FLAG_CONS)) {
325 if (flags & FLAG_INDEFINITE_LENGTH) {
David Howellsdbadc172012-10-04 14:21:23 +0100326 ret = asn1_find_indefinite_length(
327 data, datalen, &dp, &len, &errmsg);
328 if (ret < 0)
David Howells42d5ec22012-09-24 17:11:16 +0100329 goto error;
David Howellsdbadc172012-10-04 14:21:23 +0100330 } else {
331 dp += len;
David Howells42d5ec22012-09-24 17:11:16 +0100332 }
333 pr_debug("- LEAF: %zu\n", len);
David Howells42d5ec22012-09-24 17:11:16 +0100334 }
335 pc += asn1_op_lengths[op];
336 goto next_op;
337
338 case ASN1_OP_MATCH_JUMP:
339 case ASN1_OP_MATCH_JUMP_OR_SKIP:
340 case ASN1_OP_COND_MATCH_JUMP_OR_SKIP:
341 pr_debug("- MATCH_JUMP\n");
342 if (unlikely(jsp == NR_JUMP_STACK))
343 goto jump_stack_overflow;
344 jump_stack[jsp++] = pc + asn1_op_lengths[op];
345 pc = machine[pc + 2];
346 goto next_op;
347
348 case ASN1_OP_COND_FAIL:
349 if (unlikely(!(flags & FLAG_MATCHED)))
350 goto tag_mismatch;
351 pc += asn1_op_lengths[op];
352 goto next_op;
353
354 case ASN1_OP_COMPLETE:
355 if (unlikely(jsp != 0 || csp != 0)) {
356 pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
357 jsp, csp);
358 return -EBADMSG;
359 }
360 return 0;
361
362 case ASN1_OP_END_SET:
363 case ASN1_OP_END_SET_ACT:
364 if (unlikely(!(flags & FLAG_MATCHED)))
365 goto tag_mismatch;
366 case ASN1_OP_END_SEQ:
367 case ASN1_OP_END_SET_OF:
368 case ASN1_OP_END_SEQ_OF:
369 case ASN1_OP_END_SEQ_ACT:
370 case ASN1_OP_END_SET_OF_ACT:
371 case ASN1_OP_END_SEQ_OF_ACT:
372 if (unlikely(csp <= 0))
373 goto cons_stack_underflow;
374 csp--;
375 tdp = cons_dp_stack[csp];
376 hdr = cons_hdrlen_stack[csp];
377 len = datalen;
378 datalen = cons_datalen_stack[csp];
379 pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
380 tdp, dp, len, datalen);
381 if (datalen == 0) {
382 /* Indefinite length - check for the EOC. */
383 datalen = len;
384 if (unlikely(datalen - dp < 2))
385 goto data_overrun_error;
386 if (data[dp++] != 0) {
387 if (op & ASN1_OP_END__OF) {
388 dp--;
389 csp++;
390 pc = machine[pc + 1];
391 pr_debug("- continue\n");
392 goto next_op;
393 }
394 goto missing_eoc;
395 }
396 if (data[dp++] != 0)
397 goto invalid_eoc;
398 len = dp - tdp - 2;
399 } else {
400 if (dp < len && (op & ASN1_OP_END__OF)) {
401 datalen = len;
402 csp++;
403 pc = machine[pc + 1];
404 pr_debug("- continue\n");
405 goto next_op;
406 }
407 if (dp != len)
408 goto cons_length_error;
409 len -= tdp;
410 pr_debug("- cons len l=%zu d=%zu\n", len, dp - tdp);
411 }
412
413 if (op & ASN1_OP_END__ACT) {
414 unsigned char act;
415 if (op & ASN1_OP_END__OF)
416 act = machine[pc + 2];
417 else
418 act = machine[pc + 1];
419 ret = actions[act](context, hdr, 0, data + tdp, len);
420 }
421 pc += asn1_op_lengths[op];
422 goto next_op;
423
424 case ASN1_OP_ACT:
425 ret = actions[machine[pc + 1]](context, hdr, tag, data + tdp, len);
426 pc += asn1_op_lengths[op];
427 goto next_op;
428
429 case ASN1_OP_RETURN:
430 if (unlikely(jsp <= 0))
431 goto jump_stack_underflow;
432 pc = jump_stack[--jsp];
433 goto next_op;
434
435 default:
436 break;
437 }
438
439 /* Shouldn't reach here */
440 pr_err("ASN.1 decoder error: Found reserved opcode (%u)\n", op);
441 return -EBADMSG;
442
443data_overrun_error:
444 errmsg = "Data overrun error";
445 goto error;
446machine_overrun_error:
447 errmsg = "Machine overrun error";
448 goto error;
449jump_stack_underflow:
450 errmsg = "Jump stack underflow";
451 goto error;
452jump_stack_overflow:
453 errmsg = "Jump stack overflow";
454 goto error;
455cons_stack_underflow:
456 errmsg = "Cons stack underflow";
457 goto error;
458cons_stack_overflow:
459 errmsg = "Cons stack overflow";
460 goto error;
461cons_length_error:
462 errmsg = "Cons length error";
463 goto error;
464missing_eoc:
465 errmsg = "Missing EOC in indefinite len cons";
466 goto error;
467invalid_eoc:
468 errmsg = "Invalid length EOC";
469 goto error;
470length_too_long:
471 errmsg = "Unsupported length";
472 goto error;
473indefinite_len_primitive:
474 errmsg = "Indefinite len primitive not permitted";
475 goto error;
476tag_mismatch:
477 errmsg = "Unexpected tag";
478 goto error;
479long_tag_not_supported:
480 errmsg = "Long tag not supported";
481error:
482 pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
483 errmsg, pc, dp, optag, tag, len);
484 return -EBADMSG;
485}
486EXPORT_SYMBOL_GPL(asn1_ber_decoder);