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