Skip to content

Commit 4520c6a

Browse files
dhowellsrustyrussell
authored andcommitted
X.509: Add simple ASN.1 grammar compiler
Add a simple ASN.1 grammar compiler. This produces a bytecode output that can be fed to a decoder to inform the decoder how to interpret the ASN.1 stream it is trying to parse. Action functions can be specified in the grammar by interpolating: ({ foo }) after a type, for example: SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING ({ do_key_data }) } The decoder is expected to call these after matching this type and parsing the contents if it is a constructed type. The grammar compiler does not currently support the SET type (though it does support SET OF) as I can't see a good way of tracking which members have been encountered yet without using up extra stack space. Currently, the grammar compiler will fail if more than 256 bytes of bytecode would be produced or more than 256 actions have been specified as it uses 8-bit jump values and action indices to keep space usage down. Signed-off-by: David Howells <[email protected]> Signed-off-by: Rusty Russell <[email protected]>
1 parent 4f73175 commit 4520c6a

File tree

7 files changed

+1721
-0
lines changed

7 files changed

+1721
-0
lines changed

include/linux/asn1.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* ASN.1 BER/DER/CER encoding definitions
2+
*
3+
* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4+
* Written by David Howells ([email protected])
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+
#ifndef _LINUX_ASN1_H
13+
#define _LINUX_ASN1_H
14+
15+
/* Class */
16+
enum asn1_class {
17+
ASN1_UNIV = 0, /* Universal */
18+
ASN1_APPL = 1, /* Application */
19+
ASN1_CONT = 2, /* Context */
20+
ASN1_PRIV = 3 /* Private */
21+
};
22+
#define ASN1_CLASS_BITS 0xc0
23+
24+
25+
enum asn1_method {
26+
ASN1_PRIM = 0, /* Primitive */
27+
ASN1_CONS = 1 /* Constructed */
28+
};
29+
#define ASN1_CONS_BIT 0x20
30+
31+
/* Tag */
32+
enum asn1_tag {
33+
ASN1_EOC = 0, /* End Of Contents or N/A */
34+
ASN1_BOOL = 1, /* Boolean */
35+
ASN1_INT = 2, /* Integer */
36+
ASN1_BTS = 3, /* Bit String */
37+
ASN1_OTS = 4, /* Octet String */
38+
ASN1_NULL = 5, /* Null */
39+
ASN1_OID = 6, /* Object Identifier */
40+
ASN1_ODE = 7, /* Object Description */
41+
ASN1_EXT = 8, /* External */
42+
ASN1_REAL = 9, /* Real float */
43+
ASN1_ENUM = 10, /* Enumerated */
44+
ASN1_EPDV = 11, /* Embedded PDV */
45+
ASN1_UTF8STR = 12, /* UTF8 String */
46+
ASN1_RELOID = 13, /* Relative OID */
47+
/* 14 - Reserved */
48+
/* 15 - Reserved */
49+
ASN1_SEQ = 16, /* Sequence and Sequence of */
50+
ASN1_SET = 17, /* Set and Set of */
51+
ASN1_NUMSTR = 18, /* Numerical String */
52+
ASN1_PRNSTR = 19, /* Printable String */
53+
ASN1_TEXSTR = 20, /* T61 String / Teletext String */
54+
ASN1_VIDSTR = 21, /* Videotex String */
55+
ASN1_IA5STR = 22, /* IA5 String */
56+
ASN1_UNITIM = 23, /* Universal Time */
57+
ASN1_GENTIM = 24, /* General Time */
58+
ASN1_GRASTR = 25, /* Graphic String */
59+
ASN1_VISSTR = 26, /* Visible String */
60+
ASN1_GENSTR = 27, /* General String */
61+
ASN1_UNISTR = 28, /* Universal String */
62+
ASN1_CHRSTR = 29, /* Character String */
63+
ASN1_BMPSTR = 30, /* BMP String */
64+
ASN1_LONG_TAG = 31 /* Long form tag */
65+
};
66+
67+
#endif /* _LINUX_ASN1_H */

include/linux/asn1_ber_bytecode.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* ASN.1 BER/DER/CER parsing state machine internal definitions
2+
*
3+
* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4+
* Written by David Howells ([email protected])
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+
#ifndef _LINUX_ASN1_BER_BYTECODE_H
13+
#define _LINUX_ASN1_BER_BYTECODE_H
14+
15+
#ifdef __KERNEL__
16+
#include <linux/types.h>
17+
#endif
18+
#include <linux/asn1.h>
19+
20+
typedef int (*asn1_action_t)(void *context,
21+
size_t hdrlen, /* In case of ANY type */
22+
unsigned char tag, /* In case of ANY type */
23+
const void *value, size_t vlen);
24+
25+
struct asn1_decoder {
26+
const unsigned char *machine;
27+
size_t machlen;
28+
const asn1_action_t *actions;
29+
};
30+
31+
enum asn1_opcode {
32+
/* The tag-matching ops come first and the odd-numbered slots
33+
* are for OR_SKIP ops.
34+
*/
35+
#define ASN1_OP_MATCH__SKIP 0x01
36+
#define ASN1_OP_MATCH__ACT 0x02
37+
#define ASN1_OP_MATCH__JUMP 0x04
38+
#define ASN1_OP_MATCH__ANY 0x08
39+
#define ASN1_OP_MATCH__COND 0x10
40+
41+
ASN1_OP_MATCH = 0x00,
42+
ASN1_OP_MATCH_OR_SKIP = 0x01,
43+
ASN1_OP_MATCH_ACT = 0x02,
44+
ASN1_OP_MATCH_ACT_OR_SKIP = 0x03,
45+
ASN1_OP_MATCH_JUMP = 0x04,
46+
ASN1_OP_MATCH_JUMP_OR_SKIP = 0x05,
47+
ASN1_OP_MATCH_ANY = 0x08,
48+
ASN1_OP_MATCH_ANY_ACT = 0x0a,
49+
/* Everything before here matches unconditionally */
50+
51+
ASN1_OP_COND_MATCH_OR_SKIP = 0x11,
52+
ASN1_OP_COND_MATCH_ACT_OR_SKIP = 0x13,
53+
ASN1_OP_COND_MATCH_JUMP_OR_SKIP = 0x15,
54+
ASN1_OP_COND_MATCH_ANY = 0x18,
55+
ASN1_OP_COND_MATCH_ANY_ACT = 0x1a,
56+
57+
/* Everything before here will want a tag from the data */
58+
#define ASN1_OP__MATCHES_TAG ASN1_OP_COND_MATCH_ANY_ACT
59+
60+
/* These are here to help fill up space */
61+
ASN1_OP_COND_FAIL = 0x1b,
62+
ASN1_OP_COMPLETE = 0x1c,
63+
ASN1_OP_ACT = 0x1d,
64+
ASN1_OP_RETURN = 0x1e,
65+
66+
/* The following eight have bit 0 -> SET, 1 -> OF, 2 -> ACT */
67+
ASN1_OP_END_SEQ = 0x20,
68+
ASN1_OP_END_SET = 0x21,
69+
ASN1_OP_END_SEQ_OF = 0x22,
70+
ASN1_OP_END_SET_OF = 0x23,
71+
ASN1_OP_END_SEQ_ACT = 0x24,
72+
ASN1_OP_END_SET_ACT = 0x25,
73+
ASN1_OP_END_SEQ_OF_ACT = 0x26,
74+
ASN1_OP_END_SET_OF_ACT = 0x27,
75+
#define ASN1_OP_END__SET 0x01
76+
#define ASN1_OP_END__OF 0x02
77+
#define ASN1_OP_END__ACT 0x04
78+
79+
ASN1_OP__NR
80+
};
81+
82+
#define _tag(CLASS, CP, TAG) ((ASN1_##CLASS << 6) | (ASN1_##CP << 5) | ASN1_##TAG)
83+
#define _tagn(CLASS, CP, TAG) ((ASN1_##CLASS << 6) | (ASN1_##CP << 5) | TAG)
84+
#define _jump_target(N) (N)
85+
#define _action(N) (N)
86+
87+
#endif /* _LINUX_ASN1_BER_BYTECODE_H */

init/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,4 +1612,12 @@ config PADATA
16121612
depends on SMP
16131613
bool
16141614

1615+
config ASN1
1616+
tristate
1617+
help
1618+
Build a simple ASN.1 grammar compiler that produces a bytecode output
1619+
that can be interpreted by the ASN.1 stream decoder and used to
1620+
inform it as to what tags are to be expected in a stream and what
1621+
functions to call on what tags.
1622+
16151623
source "kernel/Kconfig.locks"

scripts/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ ihex2fw
1010
recordmcount
1111
docproc
1212
sortextable
13+
asn1_compiler

scripts/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ hostprogs-$(CONFIG_VT) += conmakehash
1616
hostprogs-$(CONFIG_IKCONFIG) += bin2c
1717
hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount
1818
hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable
19+
hostprogs-$(CONFIG_ASN1) += asn1_compiler
1920

2021
HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include
22+
HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
2123

2224
always := $(hostprogs-y) $(hostprogs-m)
2325

scripts/Makefile.build

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,17 @@ quiet_cmd_cpp_lds_S = LDS $@
354354
$(obj)/%.lds: $(src)/%.lds.S FORCE
355355
$(call if_changed_dep,cpp_lds_S)
356356

357+
# ASN.1 grammar
358+
# ---------------------------------------------------------------------------
359+
quiet_cmd_asn1_compiler = ASN.1 $@
360+
cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
361+
$(subst .h,.c,$@) $(subst .c,.h,$@)
362+
363+
.PRECIOUS: $(objtree)/$(obj)/%-asn1.c $(objtree)/$(obj)/%-asn1.h
364+
365+
$(obj)/%-asn1.c $(obj)/%-asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
366+
$(call cmd,asn1_compiler)
367+
357368
# Build the compiled-in targets
358369
# ---------------------------------------------------------------------------
359370

0 commit comments

Comments
 (0)