Blame view

lib/asn1_decoder.c 13.4 KB
42d5ec27f   David Howells   X.509: Add an ASN...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  /* Decoder for ASN.1 BER/DER/CER encoded bytestream
   *
   * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
   * Written by David Howells (dhowells@redhat.com)
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public Licence
   * as published by the Free Software Foundation; either version
   * 2 of the Licence, or (at your option) any later version.
   */
  
  #include <linux/export.h>
  #include <linux/kernel.h>
  #include <linux/errno.h>
ccab6058d   Tudor Ambarus   lib: asn1_decoder...
15
  #include <linux/module.h>
42d5ec27f   David Howells   X.509: Add an ASN...
16
17
18
19
20
21
22
23
24
25
26
27
  #include <linux/asn1_decoder.h>
  #include <linux/asn1_ber_bytecode.h>
  
  static const unsigned char asn1_op_lengths[ASN1_OP__NR] = {
  	/*					OPC TAG JMP ACT */
  	[ASN1_OP_MATCH]				= 1 + 1,
  	[ASN1_OP_MATCH_OR_SKIP]			= 1 + 1,
  	[ASN1_OP_MATCH_ACT]			= 1 + 1     + 1,
  	[ASN1_OP_MATCH_ACT_OR_SKIP]		= 1 + 1     + 1,
  	[ASN1_OP_MATCH_JUMP]			= 1 + 1 + 1,
  	[ASN1_OP_MATCH_JUMP_OR_SKIP]		= 1 + 1 + 1,
  	[ASN1_OP_MATCH_ANY]			= 1,
233ce79db   David Howells   ASN.1: Handle 'AN...
28
  	[ASN1_OP_MATCH_ANY_OR_SKIP]		= 1,
42d5ec27f   David Howells   X.509: Add an ASN...
29
  	[ASN1_OP_MATCH_ANY_ACT]			= 1         + 1,
233ce79db   David Howells   ASN.1: Handle 'AN...
30
  	[ASN1_OP_MATCH_ANY_ACT_OR_SKIP]		= 1         + 1,
42d5ec27f   David Howells   X.509: Add an ASN...
31
32
33
34
  	[ASN1_OP_COND_MATCH_OR_SKIP]		= 1 + 1,
  	[ASN1_OP_COND_MATCH_ACT_OR_SKIP]	= 1 + 1     + 1,
  	[ASN1_OP_COND_MATCH_JUMP_OR_SKIP]	= 1 + 1 + 1,
  	[ASN1_OP_COND_MATCH_ANY]		= 1,
233ce79db   David Howells   ASN.1: Handle 'AN...
35
  	[ASN1_OP_COND_MATCH_ANY_OR_SKIP]	= 1,
42d5ec27f   David Howells   X.509: Add an ASN...
36
  	[ASN1_OP_COND_MATCH_ANY_ACT]		= 1         + 1,
233ce79db   David Howells   ASN.1: Handle 'AN...
37
  	[ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP]	= 1         + 1,
42d5ec27f   David Howells   X.509: Add an ASN...
38
39
40
  	[ASN1_OP_COND_FAIL]			= 1,
  	[ASN1_OP_COMPLETE]			= 1,
  	[ASN1_OP_ACT]				= 1         + 1,
3f3af97d8   David Howells   ASN.1: Fix action...
41
  	[ASN1_OP_MAYBE_ACT]			= 1         + 1,
42d5ec27f   David Howells   X.509: Add an ASN...
42
43
44
45
46
47
48
49
50
51
52
53
54
  	[ASN1_OP_RETURN]			= 1,
  	[ASN1_OP_END_SEQ]			= 1,
  	[ASN1_OP_END_SEQ_OF]			= 1     + 1,
  	[ASN1_OP_END_SET]			= 1,
  	[ASN1_OP_END_SET_OF]			= 1     + 1,
  	[ASN1_OP_END_SEQ_ACT]			= 1         + 1,
  	[ASN1_OP_END_SEQ_OF_ACT]		= 1     + 1 + 1,
  	[ASN1_OP_END_SET_ACT]			= 1         + 1,
  	[ASN1_OP_END_SET_OF_ACT]		= 1     + 1 + 1,
  };
  
  /*
   * Find the length of an indefinite length object
dbadc1768   David Howells   X.509: Fix indefi...
55
56
57
58
59
   * @data: The data buffer
   * @datalen: The end of the innermost containing element in the buffer
   * @_dp: The data parse cursor (updated before returning)
   * @_len: Where to return the size of the element.
   * @_errmsg: Where to return a pointer to an error message on error
42d5ec27f   David Howells   X.509: Add an ASN...
60
61
   */
  static int asn1_find_indefinite_length(const unsigned char *data, size_t datalen,
dbadc1768   David Howells   X.509: Fix indefi...
62
63
  				       size_t *_dp, size_t *_len,
  				       const char **_errmsg)
42d5ec27f   David Howells   X.509: Add an ASN...
64
65
  {
  	unsigned char tag, tmp;
dbadc1768   David Howells   X.509: Fix indefi...
66
  	size_t dp = *_dp, len, n;
42d5ec27f   David Howells   X.509: Add an ASN...
67
68
69
70
71
72
73
74
75
76
77
  	int indef_level = 1;
  
  next_tag:
  	if (unlikely(datalen - dp < 2)) {
  		if (datalen == dp)
  			goto missing_eoc;
  		goto data_overrun_error;
  	}
  
  	/* Extract a tag from the data */
  	tag = data[dp++];
23c8a812d   David Howells   KEYS: Fix ASN.1 i...
78
  	if (tag == ASN1_EOC) {
42d5ec27f   David Howells   X.509: Add an ASN...
79
80
81
  		/* It appears to be an EOC. */
  		if (data[dp++] != 0)
  			goto invalid_eoc;
dbadc1768   David Howells   X.509: Fix indefi...
82
83
84
85
86
  		if (--indef_level <= 0) {
  			*_len = dp - *_dp;
  			*_dp = dp;
  			return 0;
  		}
42d5ec27f   David Howells   X.509: Add an ASN...
87
88
  		goto next_tag;
  	}
99cca91e3   David Howells   ASN.1: Use the AS...
89
  	if (unlikely((tag & 0x1f) == ASN1_LONG_TAG)) {
42d5ec27f   David Howells   X.509: Add an ASN...
90
91
92
93
94
95
96
97
98
  		do {
  			if (unlikely(datalen - dp < 2))
  				goto data_overrun_error;
  			tmp = data[dp++];
  		} while (tmp & 0x80);
  	}
  
  	/* Extract the length */
  	len = data[dp++];
23c8a812d   David Howells   KEYS: Fix ASN.1 i...
99
100
  	if (len <= 0x7f)
  		goto check_length;
42d5ec27f   David Howells   X.509: Add an ASN...
101

99cca91e3   David Howells   ASN.1: Use the AS...
102
  	if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
42d5ec27f   David Howells   X.509: Add an ASN...
103
104
105
106
107
108
109
110
  		/* Indefinite length */
  		if (unlikely((tag & ASN1_CONS_BIT) == ASN1_PRIM << 5))
  			goto indefinite_len_primitive;
  		indef_level++;
  		goto next_tag;
  	}
  
  	n = len - 0x80;
23c8a812d   David Howells   KEYS: Fix ASN.1 i...
111
  	if (unlikely(n > sizeof(len) - 1))
42d5ec27f   David Howells   X.509: Add an ASN...
112
113
114
  		goto length_too_long;
  	if (unlikely(n > datalen - dp))
  		goto data_overrun_error;
23c8a812d   David Howells   KEYS: Fix ASN.1 i...
115
116
  	len = 0;
  	for (; n > 0; n--) {
42d5ec27f   David Howells   X.509: Add an ASN...
117
118
119
  		len <<= 8;
  		len |= data[dp++];
  	}
23c8a812d   David Howells   KEYS: Fix ASN.1 i...
120
121
122
  check_length:
  	if (len > datalen - dp)
  		goto data_overrun_error;
42d5ec27f   David Howells   X.509: Add an ASN...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  	dp += len;
  	goto next_tag;
  
  length_too_long:
  	*_errmsg = "Unsupported length";
  	goto error;
  indefinite_len_primitive:
  	*_errmsg = "Indefinite len primitive not permitted";
  	goto error;
  invalid_eoc:
  	*_errmsg = "Invalid length EOC";
  	goto error;
  data_overrun_error:
  	*_errmsg = "Data overrun error";
  	goto error;
  missing_eoc:
  	*_errmsg = "Missing EOC in indefinite len cons";
  error:
dbadc1768   David Howells   X.509: Fix indefi...
141
  	*_dp = dp;
42d5ec27f   David Howells   X.509: Add an ASN...
142
143
144
145
146
147
148
149
  	return -1;
  }
  
  /**
   * asn1_ber_decoder - Decoder BER/DER/CER ASN.1 according to pattern
   * @decoder: The decoder definition (produced by asn1_compiler)
   * @context: The caller's context (to be passed to the action functions)
   * @data: The encoded data
548bbff98   Fabian Frederick   lib/asn1_decoder....
150
   * @datalen: The size of the encoded data
42d5ec27f   David Howells   X.509: Add an ASN...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
   *
   * Decode BER/DER/CER encoded ASN.1 data according to a bytecode pattern
   * produced by asn1_compiler.  Action functions are called on marked tags to
   * allow the caller to retrieve significant data.
   *
   * LIMITATIONS:
   *
   * To keep down the amount of stack used by this function, the following limits
   * have been imposed:
   *
   *  (1) This won't handle datalen > 65535 without increasing the size of the
   *	cons stack elements and length_too_long checking.
   *
   *  (2) The stack of constructed types is 10 deep.  If the depth of non-leaf
   *	constructed types exceeds this, the decode will fail.
   *
   *  (3) The SET type (not the SET OF type) isn't really supported as tracking
   *	what members of the set have been seen is a pain.
   */
  int asn1_ber_decoder(const struct asn1_decoder *decoder,
  		     void *context,
  		     const unsigned char *data,
  		     size_t datalen)
  {
  	const unsigned char *machine = decoder->machine;
  	const asn1_action_t *actions = decoder->actions;
  	size_t machlen = decoder->machlen;
  	enum asn1_opcode op;
  	unsigned char tag = 0, csp = 0, jsp = 0, optag = 0, hdr = 0;
  	const char *errmsg;
  	size_t pc = 0, dp = 0, tdp = 0, len = 0;
  	int ret;
  
  	unsigned char flags = 0;
  #define FLAG_INDEFINITE_LENGTH	0x01
  #define FLAG_MATCHED		0x02
3f3af97d8   David Howells   ASN.1: Fix action...
187
  #define FLAG_LAST_MATCHED	0x04 /* Last tag matched */
42d5ec27f   David Howells   X.509: Add an ASN...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  #define FLAG_CONS		0x20 /* Corresponds to CONS bit in the opcode tag
  				      * - ie. whether or not we are going to parse
  				      *   a compound type.
  				      */
  
  #define NR_CONS_STACK 10
  	unsigned short cons_dp_stack[NR_CONS_STACK];
  	unsigned short cons_datalen_stack[NR_CONS_STACK];
  	unsigned char cons_hdrlen_stack[NR_CONS_STACK];
  #define NR_JUMP_STACK 10
  	unsigned char jump_stack[NR_JUMP_STACK];
  
  	if (datalen > 65535)
  		return -EMSGSIZE;
  
  next_op:
  	pr_debug("next_op: pc=\e[32m%zu\e[m/%zu dp=\e[33m%zu\e[m/%zu C=%d J=%d
  ",
  		 pc, machlen, dp, datalen, csp, jsp);
  	if (unlikely(pc >= machlen))
  		goto machine_overrun_error;
  	op = machine[pc];
  	if (unlikely(pc + asn1_op_lengths[op] > machlen))
  		goto machine_overrun_error;
  
  	/* If this command is meant to match a tag, then do that before
  	 * evaluating the command.
  	 */
  	if (op <= ASN1_OP__MATCHES_TAG) {
  		unsigned char tmp;
  
  		/* Skip conditional matches if possible */
0d62e9dd6   David Howells   ASN.1: Fix non-ma...
220
221
  		if ((op & ASN1_OP_MATCH__COND && flags & FLAG_MATCHED) ||
  		    (op & ASN1_OP_MATCH__SKIP && dp == datalen)) {
3f3af97d8   David Howells   ASN.1: Fix action...
222
  			flags &= ~FLAG_LAST_MATCHED;
42d5ec27f   David Howells   X.509: Add an ASN...
223
224
225
226
227
228
229
230
  			pc += asn1_op_lengths[op];
  			goto next_op;
  		}
  
  		flags = 0;
  		hdr = 2;
  
  		/* Extract a tag from the data */
624f5ab87   Eric Biggers   KEYS: fix NULL po...
231
  		if (unlikely(datalen - dp < 2))
42d5ec27f   David Howells   X.509: Add an ASN...
232
233
  			goto data_overrun_error;
  		tag = data[dp++];
99cca91e3   David Howells   ASN.1: Use the AS...
234
  		if (unlikely((tag & 0x1f) == ASN1_LONG_TAG))
42d5ec27f   David Howells   X.509: Add an ASN...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  			goto long_tag_not_supported;
  
  		if (op & ASN1_OP_MATCH__ANY) {
  			pr_debug("- any %02x
  ", tag);
  		} else {
  			/* Extract the tag from the machine
  			 * - Either CONS or PRIM are permitted in the data if
  			 *   CONS is not set in the op stream, otherwise CONS
  			 *   is mandatory.
  			 */
  			optag = machine[pc + 1];
  			flags |= optag & FLAG_CONS;
  
  			/* Determine whether the tag matched */
  			tmp = optag ^ tag;
  			tmp &= ~(optag & ASN1_CONS_BIT);
  			pr_debug("- match? %02x %02x %02x
  ", tag, optag, tmp);
  			if (tmp != 0) {
  				/* All odd-numbered tags are MATCH_OR_SKIP. */
  				if (op & ASN1_OP_MATCH__SKIP) {
  					pc += asn1_op_lengths[op];
  					dp--;
  					goto next_op;
  				}
  				goto tag_mismatch;
  			}
  		}
  		flags |= FLAG_MATCHED;
  
  		len = data[dp++];
  		if (len > 0x7f) {
99cca91e3   David Howells   ASN.1: Use the AS...
268
  			if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
42d5ec27f   David Howells   X.509: Add an ASN...
269
270
271
272
273
274
275
276
277
278
  				/* Indefinite length */
  				if (unlikely(!(tag & ASN1_CONS_BIT)))
  					goto indefinite_len_primitive;
  				flags |= FLAG_INDEFINITE_LENGTH;
  				if (unlikely(2 > datalen - dp))
  					goto data_overrun_error;
  			} else {
  				int n = len - 0x80;
  				if (unlikely(n > 2))
  					goto length_too_long;
624f5ab87   Eric Biggers   KEYS: fix NULL po...
279
  				if (unlikely(n > datalen - dp))
42d5ec27f   David Howells   X.509: Add an ASN...
280
281
282
283
284
285
286
287
288
  					goto data_overrun_error;
  				hdr += n;
  				for (len = 0; n > 0; n--) {
  					len <<= 8;
  					len |= data[dp++];
  				}
  				if (unlikely(len > datalen - dp))
  					goto data_overrun_error;
  			}
2eb9eabf1   Eric Biggers   KEYS: fix out-of-...
289
290
291
  		} else {
  			if (unlikely(len > datalen - dp))
  				goto data_overrun_error;
42d5ec27f   David Howells   X.509: Add an ASN...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
  		}
  
  		if (flags & FLAG_CONS) {
  			/* For expected compound forms, we stack the positions
  			 * of the start and end of the data.
  			 */
  			if (unlikely(csp >= NR_CONS_STACK))
  				goto cons_stack_overflow;
  			cons_dp_stack[csp] = dp;
  			cons_hdrlen_stack[csp] = hdr;
  			if (!(flags & FLAG_INDEFINITE_LENGTH)) {
  				cons_datalen_stack[csp] = datalen;
  				datalen = dp + len;
  			} else {
  				cons_datalen_stack[csp] = 0;
  			}
  			csp++;
  		}
  
  		pr_debug("- TAG: %02x %zu%s
  ",
  			 tag, len, flags & FLAG_CONS ? " CONS" : "");
  		tdp = dp;
  	}
  
  	/* Decide how to handle the operation */
  	switch (op) {
42d5ec27f   David Howells   X.509: Add an ASN...
319
320
  	case ASN1_OP_MATCH:
  	case ASN1_OP_MATCH_OR_SKIP:
2c4c01d13   Eric Biggers   ASN.1: fix out-of...
321
322
  	case ASN1_OP_MATCH_ACT:
  	case ASN1_OP_MATCH_ACT_OR_SKIP:
42d5ec27f   David Howells   X.509: Add an ASN...
323
  	case ASN1_OP_MATCH_ANY:
233ce79db   David Howells   ASN.1: Handle 'AN...
324
  	case ASN1_OP_MATCH_ANY_OR_SKIP:
2c4c01d13   Eric Biggers   ASN.1: fix out-of...
325
326
  	case ASN1_OP_MATCH_ANY_ACT:
  	case ASN1_OP_MATCH_ANY_ACT_OR_SKIP:
42d5ec27f   David Howells   X.509: Add an ASN...
327
  	case ASN1_OP_COND_MATCH_OR_SKIP:
2c4c01d13   Eric Biggers   ASN.1: fix out-of...
328
  	case ASN1_OP_COND_MATCH_ACT_OR_SKIP:
42d5ec27f   David Howells   X.509: Add an ASN...
329
  	case ASN1_OP_COND_MATCH_ANY:
233ce79db   David Howells   ASN.1: Handle 'AN...
330
  	case ASN1_OP_COND_MATCH_ANY_OR_SKIP:
2c4c01d13   Eric Biggers   ASN.1: fix out-of...
331
332
  	case ASN1_OP_COND_MATCH_ANY_ACT:
  	case ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP:
42d5ec27f   David Howells   X.509: Add an ASN...
333
334
  		if (!(flags & FLAG_CONS)) {
  			if (flags & FLAG_INDEFINITE_LENGTH) {
2c4c01d13   Eric Biggers   ASN.1: fix out-of...
335
  				size_t tmp = dp;
dbadc1768   David Howells   X.509: Fix indefi...
336
  				ret = asn1_find_indefinite_length(
2c4c01d13   Eric Biggers   ASN.1: fix out-of...
337
  					data, datalen, &tmp, &len, &errmsg);
dbadc1768   David Howells   X.509: Fix indefi...
338
  				if (ret < 0)
42d5ec27f   David Howells   X.509: Add an ASN...
339
340
341
342
  					goto error;
  			}
  			pr_debug("- LEAF: %zu
  ", len);
42d5ec27f   David Howells   X.509: Add an ASN...
343
  		}
2c4c01d13   Eric Biggers   ASN.1: fix out-of...
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  
  		if (op & ASN1_OP_MATCH__ACT) {
  			unsigned char act;
  
  			if (op & ASN1_OP_MATCH__ANY)
  				act = machine[pc + 1];
  			else
  				act = machine[pc + 2];
  			ret = actions[act](context, hdr, tag, data + dp, len);
  			if (ret < 0)
  				return ret;
  		}
  
  		if (!(flags & FLAG_CONS))
  			dp += len;
42d5ec27f   David Howells   X.509: Add an ASN...
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
  		pc += asn1_op_lengths[op];
  		goto next_op;
  
  	case ASN1_OP_MATCH_JUMP:
  	case ASN1_OP_MATCH_JUMP_OR_SKIP:
  	case ASN1_OP_COND_MATCH_JUMP_OR_SKIP:
  		pr_debug("- MATCH_JUMP
  ");
  		if (unlikely(jsp == NR_JUMP_STACK))
  			goto jump_stack_overflow;
  		jump_stack[jsp++] = pc + asn1_op_lengths[op];
  		pc = machine[pc + 2];
  		goto next_op;
  
  	case ASN1_OP_COND_FAIL:
  		if (unlikely(!(flags & FLAG_MATCHED)))
  			goto tag_mismatch;
  		pc += asn1_op_lengths[op];
  		goto next_op;
  
  	case ASN1_OP_COMPLETE:
  		if (unlikely(jsp != 0 || csp != 0)) {
  			pr_err("ASN.1 decoder error: Stacks not empty at completion (%u, %u)
  ",
  			       jsp, csp);
  			return -EBADMSG;
  		}
  		return 0;
  
  	case ASN1_OP_END_SET:
  	case ASN1_OP_END_SET_ACT:
  		if (unlikely(!(flags & FLAG_MATCHED)))
  			goto tag_mismatch;
  	case ASN1_OP_END_SEQ:
  	case ASN1_OP_END_SET_OF:
  	case ASN1_OP_END_SEQ_OF:
  	case ASN1_OP_END_SEQ_ACT:
  	case ASN1_OP_END_SET_OF_ACT:
  	case ASN1_OP_END_SEQ_OF_ACT:
  		if (unlikely(csp <= 0))
  			goto cons_stack_underflow;
  		csp--;
  		tdp = cons_dp_stack[csp];
  		hdr = cons_hdrlen_stack[csp];
  		len = datalen;
  		datalen = cons_datalen_stack[csp];
  		pr_debug("- end cons t=%zu dp=%zu l=%zu/%zu
  ",
  			 tdp, dp, len, datalen);
  		if (datalen == 0) {
  			/* Indefinite length - check for the EOC. */
  			datalen = len;
  			if (unlikely(datalen - dp < 2))
  				goto data_overrun_error;
  			if (data[dp++] != 0) {
  				if (op & ASN1_OP_END__OF) {
  					dp--;
  					csp++;
  					pc = machine[pc + 1];
  					pr_debug("- continue
  ");
  					goto next_op;
  				}
  				goto missing_eoc;
  			}
  			if (data[dp++] != 0)
  				goto invalid_eoc;
  			len = dp - tdp - 2;
  		} else {
  			if (dp < len && (op & ASN1_OP_END__OF)) {
  				datalen = len;
  				csp++;
  				pc = machine[pc + 1];
  				pr_debug("- continue
  ");
  				goto next_op;
  			}
  			if (dp != len)
  				goto cons_length_error;
  			len -= tdp;
  			pr_debug("- cons len l=%zu d=%zu
  ", len, dp - tdp);
  		}
  
  		if (op & ASN1_OP_END__ACT) {
  			unsigned char act;
  			if (op & ASN1_OP_END__OF)
  				act = machine[pc + 2];
  			else
  				act = machine[pc + 1];
  			ret = actions[act](context, hdr, 0, data + tdp, len);
4c69b3405   Eric Biggers   ASN.1: check for ...
450
451
  			if (ret < 0)
  				return ret;
42d5ec27f   David Howells   X.509: Add an ASN...
452
453
454
  		}
  		pc += asn1_op_lengths[op];
  		goto next_op;
3f3af97d8   David Howells   ASN.1: Fix action...
455
456
457
458
459
  	case ASN1_OP_MAYBE_ACT:
  		if (!(flags & FLAG_LAST_MATCHED)) {
  			pc += asn1_op_lengths[op];
  			goto next_op;
  		}
42d5ec27f   David Howells   X.509: Add an ASN...
460
461
  	case ASN1_OP_ACT:
  		ret = actions[machine[pc + 1]](context, hdr, tag, data + tdp, len);
3f3af97d8   David Howells   ASN.1: Fix action...
462
463
  		if (ret < 0)
  			return ret;
42d5ec27f   David Howells   X.509: Add an ASN...
464
465
466
467
468
469
470
  		pc += asn1_op_lengths[op];
  		goto next_op;
  
  	case ASN1_OP_RETURN:
  		if (unlikely(jsp <= 0))
  			goto jump_stack_underflow;
  		pc = jump_stack[--jsp];
3f3af97d8   David Howells   ASN.1: Fix action...
471
  		flags |= FLAG_MATCHED | FLAG_LAST_MATCHED;
42d5ec27f   David Howells   X.509: Add an ASN...
472
473
474
475
476
477
478
  		goto next_op;
  
  	default:
  		break;
  	}
  
  	/* Shouldn't reach here */
3f3af97d8   David Howells   ASN.1: Fix action...
479
480
481
  	pr_err("ASN.1 decoder error: Found reserved opcode (%u) pc=%zu
  ",
  	       op, pc);
42d5ec27f   David Howells   X.509: Add an ASN...
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
  	return -EBADMSG;
  
  data_overrun_error:
  	errmsg = "Data overrun error";
  	goto error;
  machine_overrun_error:
  	errmsg = "Machine overrun error";
  	goto error;
  jump_stack_underflow:
  	errmsg = "Jump stack underflow";
  	goto error;
  jump_stack_overflow:
  	errmsg = "Jump stack overflow";
  	goto error;
  cons_stack_underflow:
  	errmsg = "Cons stack underflow";
  	goto error;
  cons_stack_overflow:
  	errmsg = "Cons stack overflow";
  	goto error;
  cons_length_error:
  	errmsg = "Cons length error";
  	goto error;
  missing_eoc:
  	errmsg = "Missing EOC in indefinite len cons";
  	goto error;
  invalid_eoc:
  	errmsg = "Invalid length EOC";
  	goto error;
  length_too_long:
  	errmsg = "Unsupported length";
  	goto error;
  indefinite_len_primitive:
  	errmsg = "Indefinite len primitive not permitted";
  	goto error;
  tag_mismatch:
  	errmsg = "Unexpected tag";
  	goto error;
  long_tag_not_supported:
  	errmsg = "Long tag not supported";
  error:
  	pr_debug("
  ASN1: %s [m=%zu d=%zu ot=%02x t=%02x l=%zu]
  ",
  		 errmsg, pc, dp, optag, tag, len);
  	return -EBADMSG;
  }
  EXPORT_SYMBOL_GPL(asn1_ber_decoder);
ccab6058d   Tudor Ambarus   lib: asn1_decoder...
530
531
  
  MODULE_LICENSE("GPL");