1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/* ASN.1 BER/DER/CER parsing state machine internal definitions
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#ifndef _LINUX_ASN1_BER_BYTECODE_H
9#define _LINUX_ASN1_BER_BYTECODE_H
10
11#ifdef __KERNEL__
12#include <linux/types.h>
13#endif
14#include <linux/asn1.h>
15
16typedef int (*asn1_action_t)(void *context,
17			     size_t hdrlen, /* In case of ANY type */
18			     unsigned char tag, /* In case of ANY type */
19			     const void *value, size_t vlen);
20
21struct asn1_decoder {
22	const unsigned char *machine;
23	size_t machlen;
24	const asn1_action_t *actions;
25};
26
27enum asn1_opcode {
28	/* The tag-matching ops come first and the odd-numbered slots
29	 * are for OR_SKIP ops.
30	 */
31#define ASN1_OP_MATCH__SKIP		  0x01
32#define ASN1_OP_MATCH__ACT		  0x02
33#define ASN1_OP_MATCH__JUMP		  0x04
34#define ASN1_OP_MATCH__ANY		  0x08
35#define ASN1_OP_MATCH__COND		  0x10
36
37	ASN1_OP_MATCH			= 0x00,
38	ASN1_OP_MATCH_OR_SKIP		= 0x01,
39	ASN1_OP_MATCH_ACT		= 0x02,
40	ASN1_OP_MATCH_ACT_OR_SKIP	= 0x03,
41	ASN1_OP_MATCH_JUMP		= 0x04,
42	ASN1_OP_MATCH_JUMP_OR_SKIP	= 0x05,
43	ASN1_OP_MATCH_ANY		= 0x08,
44	ASN1_OP_MATCH_ANY_OR_SKIP	= 0x09,
45	ASN1_OP_MATCH_ANY_ACT		= 0x0a,
46	ASN1_OP_MATCH_ANY_ACT_OR_SKIP	= 0x0b,
47	/* Everything before here matches unconditionally */
48
49	ASN1_OP_COND_MATCH_OR_SKIP	= 0x11,
50	ASN1_OP_COND_MATCH_ACT_OR_SKIP	= 0x13,
51	ASN1_OP_COND_MATCH_JUMP_OR_SKIP	= 0x15,
52	ASN1_OP_COND_MATCH_ANY		= 0x18,
53	ASN1_OP_COND_MATCH_ANY_OR_SKIP	= 0x19,
54	ASN1_OP_COND_MATCH_ANY_ACT	= 0x1a,
55	ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP = 0x1b,
56
57	/* Everything before here will want a tag from the data */
58#define ASN1_OP__MATCHES_TAG ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP
59
60	/* These are here to help fill up space */
61	ASN1_OP_COND_FAIL		= 0x1c,
62	ASN1_OP_COMPLETE		= 0x1d,
63	ASN1_OP_ACT			= 0x1e,
64	ASN1_OP_MAYBE_ACT		= 0x1f,
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_RETURN			= 0x28,
80
81	ASN1_OP__NR
82};
83
84#define _tag(CLASS, CP, TAG) ((ASN1_##CLASS << 6) | (ASN1_##CP << 5) | ASN1_##TAG)
85#define _tagn(CLASS, CP, TAG) ((ASN1_##CLASS << 6) | (ASN1_##CP << 5) | TAG)
86#define _jump_target(N) (N)
87#define _action(N) (N)
88
89#endif /* _LINUX_ASN1_BER_BYTECODE_H */
90