1/*	$NetBSD: asn1-template.h,v 1.2 2017/01/28 21:31:45 christos Exp $	*/
2
3/*
4 * Copyright (c) 1997 - 2006 Kungliga Tekniska H�gskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * 3. Neither the name of the Institute nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38/* asn1 templates */
39
40#ifndef __TEMPLATE_H__
41#define __TEMPLATE_H__
42
43/* tag:
44 *  0..20 tag
45 * 21     type
46 * 22..23 class
47 * 24..27 flags
48 * 28..31 op
49 */
50
51/* parse:
52 *  0..11 type
53 * 12..23 unused
54 * 24..27 flags
55 * 28..31 op
56 */
57
58#define A1_OP_MASK		(0xf0000000)
59#define A1_OP_TYPE		(0x10000000)
60#define A1_OP_TYPE_EXTERN	(0x20000000)
61#define A1_OP_TAG		(0x30000000)
62#define A1_OP_PARSE		(0x40000000)
63#define A1_OP_SEQOF		(0x50000000)
64#define A1_OP_SETOF		(0x60000000)
65#define A1_OP_BMEMBER		(0x70000000)
66#define A1_OP_CHOICE		(0x80000000)
67
68#define A1_FLAG_MASK		(0x0f000000)
69#define A1_FLAG_OPTIONAL	(0x01000000)
70#define A1_FLAG_IMPLICIT	(0x02000000)
71
72#define A1_TAG_T(CLASS,TYPE,TAG)	((A1_OP_TAG) | (((CLASS) << 22) | ((TYPE) << 21) | (TAG)))
73#define A1_TAG_CLASS(x)		(((x) >> 22) & 0x3)
74#define A1_TAG_TYPE(x)		(((x) >> 21) & 0x1)
75#define A1_TAG_TAG(x)		((x) & 0x1fffff)
76
77#define A1_TAG_LEN(t)		((uintptr_t)(t)->ptr)
78#define A1_HEADER_LEN(t)	((uintptr_t)(t)->ptr)
79
80#define A1_PARSE_T(type)	((A1_OP_PARSE) | (type))
81#define A1_PARSE_TYPE_MASK	0xfff
82#define A1_PARSE_TYPE(x)	(A1_PARSE_TYPE_MASK & (x))
83
84#define A1_PF_INDEFINTE		0x1
85#define A1_PF_ALLOW_BER		0x2
86
87#define A1_HF_PRESERVE		0x1
88#define A1_HF_ELLIPSIS		0x2
89
90#define A1_HBF_RFC1510		0x1
91
92
93struct asn1_template {
94    uint32_t tt;
95    uint32_t offset;
96    const void *ptr;
97};
98
99typedef int (*asn1_type_decode)(const unsigned char *, size_t, void *, size_t *);
100typedef int (*asn1_type_encode)(unsigned char *, size_t, const void *, size_t *);
101typedef size_t (*asn1_type_length)(const void *);
102typedef void (*asn1_type_release)(void *);
103typedef int (*asn1_type_copy)(const void *, void *);
104
105struct asn1_type_func {
106    asn1_type_encode encode;
107    asn1_type_decode decode;
108    asn1_type_length length;
109    asn1_type_copy copy;
110    asn1_type_release release;
111    size_t size;
112};
113
114struct template_of {
115    unsigned int len;
116    void *val;
117};
118
119enum template_types {
120    A1T_IMEMBER = 0,
121    A1T_HEIM_INTEGER,
122    A1T_INTEGER,
123    A1T_INTEGER64,
124    A1T_UNSIGNED,
125    A1T_UNSIGNED64,
126    A1T_GENERAL_STRING,
127    A1T_OCTET_STRING,
128    A1T_OCTET_STRING_BER,
129    A1T_IA5_STRING,
130    A1T_BMP_STRING,
131    A1T_UNIVERSAL_STRING,
132    A1T_PRINTABLE_STRING,
133    A1T_VISIBLE_STRING,
134    A1T_UTF8_STRING,
135    A1T_GENERALIZED_TIME,
136    A1T_UTC_TIME,
137    A1T_HEIM_BIT_STRING,
138    A1T_BOOLEAN,
139    A1T_OID,
140    A1T_TELETEX_STRING,
141    A1T_NUM_ENTRY
142};
143
144extern struct asn1_type_func asn1_template_prim[A1T_NUM_ENTRY];
145
146#define ABORT_ON_ERROR() abort()
147
148#define DPOC(data,offset) ((const void *)(((const unsigned char *)data)  + offset))
149#define DPO(data,offset) ((void *)(((unsigned char *)data)  + offset))
150
151/*
152 * These functions are needed by the generated template stubs and are
153 * really internal functions. Since they are part of der-private.h
154 * that contains extra prototypes that really a private we included a
155 * copy here.
156 */
157
158int
159_asn1_copy_top (
160	const struct asn1_template * /*t*/,
161	const void * /*from*/,
162	void * /*to*/);
163
164void
165_asn1_free_top(const struct asn1_template *t,
166	       void *data);
167
168int
169_asn1_decode_top (
170	const struct asn1_template * /*t*/,
171	unsigned /*flags*/,
172	const unsigned char * /*p*/,
173	size_t /*len*/,
174	void * /*data*/,
175	size_t * /*size*/);
176
177int
178_asn1_encode (
179	const struct asn1_template * /*t*/,
180	unsigned char * /*p*/,
181	size_t /*len*/,
182	const void * /*data*/,
183	size_t * /*size*/);
184
185int
186_asn1_encode_fuzzer (
187	const struct asn1_template * /*t*/,
188	unsigned char * /*p*/,
189	size_t /*len*/,
190	const void * /*data*/,
191	size_t * /*size*/);
192
193void
194_asn1_free (
195	const struct asn1_template * /*t*/,
196	void * /*data*/);
197
198size_t
199_asn1_length (
200	const struct asn1_template * /*t*/,
201	const void * /*data*/);
202
203size_t
204_asn1_length_fuzzer (
205	const struct asn1_template * /*t*/,
206	const void * /*data*/);
207
208
209#endif
210