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