• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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 
18 static 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_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,
41 	[ASN1_OP_RETURN]			= 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,
50 };
51 
52 /*
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
59  */
asn1_find_indefinite_length(const unsigned char * data,size_t datalen,size_t * _dp,size_t * _len,const char ** _errmsg)60 static int asn1_find_indefinite_length(const unsigned char *data, size_t datalen,
61 				       size_t *_dp, size_t *_len,
62 				       const char **_errmsg)
63 {
64 	unsigned char tag, tmp;
65 	size_t dp = *_dp, len, n;
66 	int indef_level = 1;
67 
68 next_tag:
69 	if (unlikely(datalen - dp < 2)) {
70 		if (datalen == dp)
71 			goto missing_eoc;
72 		goto data_overrun_error;
73 	}
74 
75 	/* Extract a tag from the data */
76 	tag = data[dp++];
77 	if (tag == ASN1_EOC) {
78 		/* It appears to be an EOC. */
79 		if (data[dp++] != 0)
80 			goto invalid_eoc;
81 		if (--indef_level <= 0) {
82 			*_len = dp - *_dp;
83 			*_dp = dp;
84 			return 0;
85 		}
86 		goto next_tag;
87 	}
88 
89 	if (unlikely((tag & 0x1f) == ASN1_LONG_TAG)) {
90 		do {
91 			if (unlikely(datalen - dp < 2))
92 				goto data_overrun_error;
93 			tmp = data[dp++];
94 		} while (tmp & 0x80);
95 	}
96 
97 	/* Extract the length */
98 	len = data[dp++];
99 	if (len <= 0x7f)
100 		goto check_length;
101 
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;
106 		indef_level++;
107 		goto next_tag;
108 	}
109 
110 	n = len - 0x80;
111 	if (unlikely(n > sizeof(len) - 1))
112 		goto length_too_long;
113 	if (unlikely(n > datalen - dp))
114 		goto data_overrun_error;
115 	len = 0;
116 	for (; n > 0; n--) {
117 		len <<= 8;
118 		len |= data[dp++];
119 	}
120 check_length:
121 	if (len > datalen - dp)
122 		goto data_overrun_error;
123 	dp += len;
124 	goto next_tag;
125 
126 length_too_long:
127 	*_errmsg = "Unsupported length";
128 	goto error;
129 indefinite_len_primitive:
130 	*_errmsg = "Indefinite len primitive not permitted";
131 	goto error;
132 invalid_eoc:
133 	*_errmsg = "Invalid length EOC";
134 	goto error;
135 data_overrun_error:
136 	*_errmsg = "Data overrun error";
137 	goto error;
138 missing_eoc:
139 	*_errmsg = "Missing EOC in indefinite len cons";
140 error:
141 	*_dp = dp;
142 	return -1;
143 }
144 
145 /**
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
151  *
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.
155  *
156  * LIMITATIONS:
157  *
158  * To keep down the amount of stack used by this function, the following limits
159  * have been imposed:
160  *
161  *  (1) This won't handle datalen > 65535 without increasing the size of the
162  *	cons stack elements and length_too_long checking.
163  *
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.
166  *
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.
169  */
asn1_ber_decoder(const struct asn1_decoder * decoder,void * context,const unsigned char * data,size_t datalen)170 int asn1_ber_decoder(const struct asn1_decoder *decoder,
171 		     void *context,
172 		     const unsigned char *data,
173 		     size_t datalen)
174 {
175 	const unsigned char *machine = decoder->machine;
176 	const asn1_action_t *actions = decoder->actions;
177 	size_t machlen = decoder->machlen;
178 	enum asn1_opcode op;
179 	unsigned char tag = 0, csp = 0, jsp = 0, optag = 0, hdr = 0;
180 	const char *errmsg;
181 	size_t pc = 0, dp = 0, tdp = 0, len = 0;
182 	int ret;
183 
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
190 				      *   a compound type.
191 				      */
192 
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];
199 
200 	if (datalen > 65535)
201 		return -EMSGSIZE;
202 
203 next_op:
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;
208 	op = machine[pc];
209 	if (unlikely(pc + asn1_op_lengths[op] > machlen))
210 		goto machine_overrun_error;
211 
212 	/* If this command is meant to match a tag, then do that before
213 	 * evaluating the command.
214 	 */
215 	if (op <= ASN1_OP__MATCHES_TAG) {
216 		unsigned char tmp;
217 
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];
223 			goto next_op;
224 		}
225 
226 		flags = 0;
227 		hdr = 2;
228 
229 		/* Extract a tag from the data */
230 		if (unlikely(datalen - dp < 2))
231 			goto data_overrun_error;
232 		tag = data[dp++];
233 		if (unlikely((tag & 0x1f) == ASN1_LONG_TAG))
234 			goto long_tag_not_supported;
235 
236 		if (op & ASN1_OP_MATCH__ANY) {
237 			pr_debug("- any %02x\n", tag);
238 		} else {
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
242 			 *   is mandatory.
243 			 */
244 			optag = machine[pc + 1];
245 			flags |= optag & FLAG_CONS;
246 
247 			/* Determine whether the tag matched */
248 			tmp = optag ^ tag;
249 			tmp &= ~(optag & ASN1_CONS_BIT);
250 			pr_debug("- match? %02x %02x %02x\n", tag, optag, tmp);
251 			if (tmp != 0) {
252 				/* All odd-numbered tags are MATCH_OR_SKIP. */
253 				if (op & ASN1_OP_MATCH__SKIP) {
254 					pc += asn1_op_lengths[op];
255 					dp--;
256 					goto next_op;
257 				}
258 				goto tag_mismatch;
259 			}
260 		}
261 		flags |= FLAG_MATCHED;
262 
263 		len = data[dp++];
264 		if (len > 0x7f) {
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;
272 			} else {
273 				int n = len - 0x80;
274 				if (unlikely(n > 2))
275 					goto length_too_long;
276 				if (unlikely(n > datalen - dp))
277 					goto data_overrun_error;
278 				hdr += n;
279 				for (len = 0; n > 0; n--) {
280 					len <<= 8;
281 					len |= data[dp++];
282 				}
283 				if (unlikely(len > datalen - dp))
284 					goto data_overrun_error;
285 			}
286 		} else {
287 			if (unlikely(len > datalen - dp))
288 				goto data_overrun_error;
289 		}
290 
291 		if (flags & FLAG_CONS) {
292 			/* For expected compound forms, we stack the positions
293 			 * of the start and end of the data.
294 			 */
295 			if (unlikely(csp >= NR_CONS_STACK))
296 				goto cons_stack_overflow;
297 			cons_dp_stack[csp] = dp;
298 			cons_hdrlen_stack[csp] = hdr;
299 			if (!(flags & FLAG_INDEFINITE_LENGTH)) {
300 				cons_datalen_stack[csp] = datalen;
301 				datalen = dp + len;
302 			} else {
303 				cons_datalen_stack[csp] = 0;
304 			}
305 			csp++;
306 		}
307 
308 		pr_debug("- TAG: %02x %zu%s\n",
309 			 tag, len, flags & FLAG_CONS ? " CONS" : "");
310 		tdp = dp;
311 	}
312 
313 	/* Decide how to handle the operation */
314 	switch (op) {
315 	case ASN1_OP_MATCH:
316 	case ASN1_OP_MATCH_OR_SKIP:
317 	case ASN1_OP_MATCH_ACT:
318 	case ASN1_OP_MATCH_ACT_OR_SKIP:
319 	case ASN1_OP_MATCH_ANY:
320 	case ASN1_OP_MATCH_ANY_OR_SKIP:
321 	case ASN1_OP_MATCH_ANY_ACT:
322 	case ASN1_OP_MATCH_ANY_ACT_OR_SKIP:
323 	case ASN1_OP_COND_MATCH_OR_SKIP:
324 	case ASN1_OP_COND_MATCH_ACT_OR_SKIP:
325 	case ASN1_OP_COND_MATCH_ANY:
326 	case ASN1_OP_COND_MATCH_ANY_OR_SKIP:
327 	case ASN1_OP_COND_MATCH_ANY_ACT:
328 	case ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP:
329 
330 		if (!(flags & FLAG_CONS)) {
331 			if (flags & FLAG_INDEFINITE_LENGTH) {
332 				size_t tmp = dp;
333 
334 				ret = asn1_find_indefinite_length(
335 					data, datalen, &tmp, &len, &errmsg);
336 				if (ret < 0)
337 					goto error;
338 			}
339 			pr_debug("- LEAF: %zu\n", len);
340 		}
341 
342 		if (op & ASN1_OP_MATCH__ACT) {
343 			unsigned char act;
344 
345 			if (op & ASN1_OP_MATCH__ANY)
346 				act = machine[pc + 1];
347 			else
348 				act = machine[pc + 2];
349 			ret = actions[act](context, hdr, tag, data + dp, len);
350 			if (ret < 0)
351 				return ret;
352 		}
353 
354 		if (!(flags & FLAG_CONS))
355 			dp += len;
356 		pc += asn1_op_lengths[op];
357 		goto next_op;
358 
359 	case ASN1_OP_MATCH_JUMP:
360 	case ASN1_OP_MATCH_JUMP_OR_SKIP:
361 	case ASN1_OP_COND_MATCH_JUMP_OR_SKIP:
362 		pr_debug("- MATCH_JUMP\n");
363 		if (unlikely(jsp == NR_JUMP_STACK))
364 			goto jump_stack_overflow;
365 		jump_stack[jsp++] = pc + asn1_op_lengths[op];
366 		pc = machine[pc + 2];
367 		goto next_op;
368 
369 	case ASN1_OP_COND_FAIL:
370 		if (unlikely(!(flags & FLAG_MATCHED)))
371 			goto tag_mismatch;
372 		pc += asn1_op_lengths[op];
373 		goto next_op;
374 
375 	case ASN1_OP_COMPLETE:
376 		if (unlikely(jsp != 0 || csp != 0)) {
377 			pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)\n",
378 			       jsp, csp);
379 			return -EBADMSG;
380 		}
381 		return 0;
382 
383 	case ASN1_OP_END_SET:
384 	case ASN1_OP_END_SET_ACT:
385 		if (unlikely(!(flags & FLAG_MATCHED)))
386 			goto tag_mismatch;
387 	case ASN1_OP_END_SEQ:
388 	case ASN1_OP_END_SET_OF:
389 	case ASN1_OP_END_SEQ_OF:
390 	case ASN1_OP_END_SEQ_ACT:
391 	case ASN1_OP_END_SET_OF_ACT:
392 	case ASN1_OP_END_SEQ_OF_ACT:
393 		if (unlikely(csp <= 0))
394 			goto cons_stack_underflow;
395 		csp--;
396 		tdp = cons_dp_stack[csp];
397 		hdr = cons_hdrlen_stack[csp];
398 		len = datalen;
399 		datalen = cons_datalen_stack[csp];
400 		pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu\n",
401 			 tdp, dp, len, datalen);
402 		if (datalen == 0) {
403 			/* Indefinite length - check for the EOC. */
404 			datalen = len;
405 			if (unlikely(datalen - dp < 2))
406 				goto data_overrun_error;
407 			if (data[dp++] != 0) {
408 				if (op & ASN1_OP_END__OF) {
409 					dp--;
410 					csp++;
411 					pc = machine[pc + 1];
412 					pr_debug("- continue\n");
413 					goto next_op;
414 				}
415 				goto missing_eoc;
416 			}
417 			if (data[dp++] != 0)
418 				goto invalid_eoc;
419 			len = dp - tdp - 2;
420 		} else {
421 			if (dp < len && (op & ASN1_OP_END__OF)) {
422 				datalen = len;
423 				csp++;
424 				pc = machine[pc + 1];
425 				pr_debug("- continue\n");
426 				goto next_op;
427 			}
428 			if (dp != len)
429 				goto cons_length_error;
430 			len -= tdp;
431 			pr_debug("- cons len l=%zu d=%zu\n", len, dp - tdp);
432 		}
433 
434 		if (op & ASN1_OP_END__ACT) {
435 			unsigned char act;
436 			if (op & ASN1_OP_END__OF)
437 				act = machine[pc + 2];
438 			else
439 				act = machine[pc + 1];
440 			ret = actions[act](context, hdr, 0, data + tdp, len);
441 			if (ret < 0)
442 				return ret;
443 		}
444 		pc += asn1_op_lengths[op];
445 		goto next_op;
446 
447 	case ASN1_OP_MAYBE_ACT:
448 		if (!(flags & FLAG_LAST_MATCHED)) {
449 			pc += asn1_op_lengths[op];
450 			goto next_op;
451 		}
452 	case ASN1_OP_ACT:
453 		ret = actions[machine[pc + 1]](context, hdr, tag, data + tdp, len);
454 		if (ret < 0)
455 			return ret;
456 		pc += asn1_op_lengths[op];
457 		goto next_op;
458 
459 	case ASN1_OP_RETURN:
460 		if (unlikely(jsp <= 0))
461 			goto jump_stack_underflow;
462 		pc = jump_stack[--jsp];
463 		flags |= FLAG_MATCHED | FLAG_LAST_MATCHED;
464 		goto next_op;
465 
466 	default:
467 		break;
468 	}
469 
470 	/* Shouldn't reach here */
471 	pr_err("ASN.1 decoder error: Found reserved opcode (%u) pc=%zu\n",
472 	       op, pc);
473 	return -EBADMSG;
474 
475 data_overrun_error:
476 	errmsg = "Data overrun error";
477 	goto error;
478 machine_overrun_error:
479 	errmsg = "Machine overrun error";
480 	goto error;
481 jump_stack_underflow:
482 	errmsg = "Jump stack underflow";
483 	goto error;
484 jump_stack_overflow:
485 	errmsg = "Jump stack overflow";
486 	goto error;
487 cons_stack_underflow:
488 	errmsg = "Cons stack underflow";
489 	goto error;
490 cons_stack_overflow:
491 	errmsg = "Cons stack overflow";
492 	goto error;
493 cons_length_error:
494 	errmsg = "Cons length error";
495 	goto error;
496 missing_eoc:
497 	errmsg = "Missing EOC in indefinite len cons";
498 	goto error;
499 invalid_eoc:
500 	errmsg = "Invalid length EOC";
501 	goto error;
502 length_too_long:
503 	errmsg = "Unsupported length";
504 	goto error;
505 indefinite_len_primitive:
506 	errmsg = "Indefinite len primitive not permitted";
507 	goto error;
508 tag_mismatch:
509 	errmsg = "Unexpected tag";
510 	goto error;
511 long_tag_not_supported:
512 	errmsg = "Long tag not supported";
513 error:
514 	pr_debug("\nASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]\n",
515 		 errmsg, pc, dp, optag, tag, len);
516 	return -EBADMSG;
517 }
518 EXPORT_SYMBOL_GPL(asn1_ber_decoder);
519