1226031Sstas/*
2226031Sstas * Copyright (c) 1997 - 2006 Kungliga Tekniska H�gskolan
3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4226031Sstas * All rights reserved.
5226031Sstas *
6226031Sstas * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7226031Sstas *
8226031Sstas * Redistribution and use in source and binary forms, with or without
9226031Sstas * modification, are permitted provided that the following conditions
10226031Sstas * are met:
11226031Sstas *
12226031Sstas * 1. Redistributions of source code must retain the above copyright
13226031Sstas *    notice, this list of conditions and the following disclaimer.
14226031Sstas *
15226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
16226031Sstas *    notice, this list of conditions and the following disclaimer in the
17226031Sstas *    documentation and/or other materials provided with the distribution.
18226031Sstas *
19226031Sstas * 3. Neither the name of the Institute nor the names of its contributors
20226031Sstas *    may be used to endorse or promote products derived from this software
21226031Sstas *    without specific prior written permission.
22226031Sstas *
23226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33226031Sstas * SUCH DAMAGE.
34226031Sstas */
35226031Sstas
36226031Sstas/* asn1 templates */
37226031Sstas
38226031Sstas#ifndef __TEMPLATE_H__
39226031Sstas#define __TEMPLATE_H__
40226031Sstas
41226031Sstas/* tag:
42226031Sstas *  0..20 tag
43226031Sstas * 21     type
44226031Sstas * 22..23 class
45226031Sstas * 24..27 flags
46226031Sstas * 28..31 op
47226031Sstas */
48226031Sstas
49226031Sstas/* parse:
50226031Sstas *  0..11 type
51226031Sstas * 12..23 unused
52226031Sstas * 24..27 flags
53226031Sstas * 28..31 op
54226031Sstas */
55226031Sstas
56226031Sstas#define A1_OP_MASK		(0xf0000000)
57226031Sstas#define A1_OP_TYPE		(0x10000000)
58226031Sstas#define A1_OP_TYPE_EXTERN	(0x20000000)
59226031Sstas#define A1_OP_TAG		(0x30000000)
60226031Sstas#define A1_OP_PARSE		(0x40000000)
61226031Sstas#define A1_OP_SEQOF		(0x50000000)
62226031Sstas#define A1_OP_SETOF		(0x60000000)
63226031Sstas#define A1_OP_BMEMBER		(0x70000000)
64226031Sstas#define A1_OP_CHOICE		(0x80000000)
65226031Sstas
66226031Sstas#define A1_FLAG_MASK		(0x0f000000)
67226031Sstas#define A1_FLAG_OPTIONAL	(0x01000000)
68226031Sstas#define A1_FLAG_IMPLICIT	(0x02000000)
69226031Sstas
70226031Sstas#define A1_TAG_T(CLASS,TYPE,TAG)	((A1_OP_TAG) | (((CLASS) << 22) | ((TYPE) << 21) | (TAG)))
71226031Sstas#define A1_TAG_CLASS(x)		(((x) >> 22) & 0x3)
72226031Sstas#define A1_TAG_TYPE(x)		(((x) >> 21) & 0x1)
73226031Sstas#define A1_TAG_TAG(x)		((x) & 0x1fffff)
74226031Sstas
75226031Sstas#define A1_TAG_LEN(t)		((uintptr_t)(t)->ptr)
76226031Sstas#define A1_HEADER_LEN(t)	((uintptr_t)(t)->ptr)
77226031Sstas
78226031Sstas#define A1_PARSE_T(type)	((A1_OP_PARSE) | (type))
79226031Sstas#define A1_PARSE_TYPE_MASK	0xfff
80226031Sstas#define A1_PARSE_TYPE(x)	(A1_PARSE_TYPE_MASK & (x))
81226031Sstas
82226031Sstas#define A1_PF_INDEFINTE		0x1
83226031Sstas#define A1_PF_ALLOW_BER		0x2
84226031Sstas
85226031Sstas#define A1_HF_PRESERVE		0x1
86226031Sstas#define A1_HF_ELLIPSIS		0x2
87226031Sstas
88226031Sstas#define A1_HBF_RFC1510		0x1
89226031Sstas
90226031Sstas
91226031Sstasstruct asn1_template {
92226031Sstas    uint32_t tt;
93226031Sstas    size_t offset;
94226031Sstas    const void *ptr;
95226031Sstas};
96226031Sstas
97226031Sstastypedef int (*asn1_type_decode)(const unsigned char *, size_t, void *, size_t *);
98226031Sstastypedef int (*asn1_type_encode)(unsigned char *, size_t, const void *, size_t *);
99226031Sstastypedef size_t (*asn1_type_length)(const void *);
100226031Sstastypedef void (*asn1_type_release)(void *);
101226031Sstastypedef int (*asn1_type_copy)(const void *, void *);
102226031Sstas
103226031Sstasstruct asn1_type_func {
104226031Sstas    asn1_type_encode encode;
105226031Sstas    asn1_type_decode decode;
106226031Sstas    asn1_type_length length;
107226031Sstas    asn1_type_copy copy;
108226031Sstas    asn1_type_release release;
109226031Sstas    size_t size;
110226031Sstas};
111226031Sstas
112226031Sstasstruct template_of {
113226031Sstas    unsigned int len;
114226031Sstas    void *val;
115226031Sstas};
116226031Sstas
117226031Sstasenum template_types {
118226031Sstas    A1T_IMEMBER = 0,
119226031Sstas    A1T_HEIM_INTEGER,
120226031Sstas    A1T_INTEGER,
121226031Sstas    A1T_UNSIGNED,
122226031Sstas    A1T_GENERAL_STRING,
123226031Sstas    A1T_OCTET_STRING,
124226031Sstas    A1T_OCTET_STRING_BER,
125226031Sstas    A1T_IA5_STRING,
126226031Sstas    A1T_BMP_STRING,
127226031Sstas    A1T_UNIVERSAL_STRING,
128226031Sstas    A1T_PRINTABLE_STRING,
129226031Sstas    A1T_VISIBLE_STRING,
130226031Sstas    A1T_UTF8_STRING,
131226031Sstas    A1T_GENERALIZED_TIME,
132226031Sstas    A1T_UTC_TIME,
133226031Sstas    A1T_HEIM_BIT_STRING,
134226031Sstas    A1T_BOOLEAN,
135226031Sstas    A1T_OID,
136226031Sstas    A1T_TELETEX_STRING,
137226031Sstas    A1T_NULL
138226031Sstas};
139226031Sstas
140226031Sstas
141226031Sstas#endif
142