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