1/*
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is the Netscape security libraries.
13 *
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation.  Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU General Public License Version 2 or later (the
23 * "GPL"), in which case the provisions of the GPL are applicable
24 * instead of those above.  If you wish to allow use of your
25 * version of this file only under the terms of the GPL and not to
26 * allow others to use your version of this file under the MPL,
27 * indicate your decision by deleting the provisions above and
28 * replace them with the notice and other provisions required by
29 * the GPL.  If you do not delete the provisions above, a recipient
30 * may use your version of this file under either the MPL or the
31 * GPL.
32 */
33
34/*
35 * Types for encoding/decoding of ASN.1 using BER/DER (Basic/Distinguished
36 * Encoding Rules).
37 */
38
39#ifndef _SEC_ASN1_TYPES_H_
40#define _SEC_ASN1_TYPES_H_
41
42#include <CoreFoundation/CFBase.h>		/* Boolean */
43#include <sys/types.h>
44#include <stdint.h>
45
46#include <TargetConditionals.h>
47#if TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR
48/* @@@ We need something that tells us which platform we are building
49   for that let's us distinguish if we are doing an emulator build. */
50
51typedef struct {
52    size_t Length;
53    uint8_t *Data;
54} SecAsn1Item, SecAsn1Oid;
55
56typedef struct {
57    SecAsn1Oid algorithm;
58    SecAsn1Item parameters;
59} SecAsn1AlgId;
60
61typedef struct {
62    SecAsn1AlgId algorithm;
63    SecAsn1Item subjectPublicKey;
64} SecAsn1PubKeyInfo;
65
66#else
67#include <Security/cssmtype.h>
68#include <Security/x509defs.h>
69
70typedef CSSM_DATA SecAsn1Item;
71typedef CSSM_OID SecAsn1Oid;
72typedef CSSM_X509_ALGORITHM_IDENTIFIER SecAsn1AlgId;
73typedef CSSM_X509_SUBJECT_PUBLIC_KEY_INFO SecAsn1PubKeyInfo;
74
75#endif
76
77/*
78 * An array of these structures defines a BER/DER encoding for an object.
79 *
80 * The array usually starts with a dummy entry whose kind is SEC_ASN1_SEQUENCE;
81 * such an array is terminated with an entry where kind == 0.  (An array
82 * which consists of a single component does not require a second dummy
83 * entry -- the array is only searched as long as previous component(s)
84 * instruct it.)
85 */
86typedef struct SecAsn1Template_struct {
87    /*
88     * Kind of item being decoded/encoded, including tags and modifiers.
89     */
90    uint32_t kind;
91
92    /*
93     * This value is the offset from the base of the structure (i.e., the
94	 * (void *) passed as 'src' to SecAsn1EncodeItem, or the 'dst' argument
95	 * passed to SecAsn1CoderRef()) to the field that holds the value being
96	 * decoded/encoded.
97     */
98    uint32_t offset;
99
100    /*
101     * When kind suggests it (e.g., SEC_ASN1_POINTER, SEC_ASN1_GROUP,
102	 * SEC_ASN1_INLINE, or a component that is *not* a SEC_ASN1_UNIVERSAL),
103	 * this points to a sub-template for nested encoding/decoding.
104     * OR, iff SEC_ASN1_DYNAMIC is set, then this is a pointer to a pointer
105     * to a function which will return the appropriate template when called
106     * at runtime.  NOTE! that explicit level of indirection, which is
107     * necessary because ANSI does not allow you to store a function
108     * pointer directly as a "void *" so we must store it separately and
109     * dereference it to get at the function pointer itself.
110     */
111    const void *sub;
112
113    /*
114     * In the first element of a template array, the value is the size
115     * of the structure to allocate when this template is being referenced
116     * by another template via SEC_ASN1_POINTER or SEC_ASN1_GROUP.
117     * In all other cases, the value is ignored.
118     */
119    uint32_t size;
120} SecAsn1Template;
121
122
123/*
124 * BER/DER values for ASN.1 identifier octets.
125 */
126#define SEC_ASN1_TAG_MASK		0xff
127
128/*
129 * BER/DER universal type tag numbers.
130 */
131#define SEC_ASN1_TAGNUM_MASK		0x1f
132#define SEC_ASN1_BOOLEAN			0x01
133#define SEC_ASN1_INTEGER			0x02
134#define SEC_ASN1_BIT_STRING			0x03
135#define SEC_ASN1_OCTET_STRING		0x04
136#define SEC_ASN1_NULL				0x05
137#define SEC_ASN1_OBJECT_ID			0x06
138#define SEC_ASN1_OBJECT_DESCRIPTOR  0x07
139/* External type and instance-of type   0x08 */
140#define SEC_ASN1_REAL               0x09
141#define SEC_ASN1_ENUMERATED			0x0a
142#define SEC_ASN1_EMBEDDED_PDV       0x0b
143#define SEC_ASN1_UTF8_STRING		0x0c
144/* not used                         0x0d */
145/* not used                         0x0e */
146/* not used                         0x0f */
147#define SEC_ASN1_SEQUENCE			0x10
148#define SEC_ASN1_SET				0x11
149#define SEC_ASN1_NUMERIC_STRING     0x12
150#define SEC_ASN1_PRINTABLE_STRING	0x13
151#define SEC_ASN1_T61_STRING			0x14
152#define SEC_ASN1_VIDEOTEX_STRING	0x15
153#define SEC_ASN1_IA5_STRING			0x16
154#define SEC_ASN1_UTC_TIME			0x17
155#define SEC_ASN1_GENERALIZED_TIME	0x18
156#define SEC_ASN1_GRAPHIC_STRING		0x19
157#define SEC_ASN1_VISIBLE_STRING		0x1a
158#define SEC_ASN1_GENERAL_STRING		0x1b
159#define SEC_ASN1_UNIVERSAL_STRING	0x1c
160/* not used							0x1d */
161#define SEC_ASN1_BMP_STRING			0x1e
162#define SEC_ASN1_HIGH_TAG_NUMBER	0x1f
163#define SEC_ASN1_TELETEX_STRING SEC_ASN1_T61_STRING
164
165/*
166 * Modifiers to type tags.  These are also specified by a/the
167 * standard, and must not be changed.
168 */
169#define SEC_ASN1_METHOD_MASK		0x20
170#define SEC_ASN1_PRIMITIVE			0x00
171#define SEC_ASN1_CONSTRUCTED		0x20
172
173#define SEC_ASN1_CLASS_MASK			0xc0
174#define SEC_ASN1_UNIVERSAL			0x00
175#define SEC_ASN1_APPLICATION		0x40
176#define SEC_ASN1_CONTEXT_SPECIFIC	0x80
177#define SEC_ASN1_PRIVATE			0xc0
178
179/*
180 * Our additions, used for templates.
181 * These are not defined by any standard; the values are used internally only.
182 * Just be careful to keep them out of the low 8 bits.
183 */
184#define SEC_ASN1_OPTIONAL	0x00100
185#define SEC_ASN1_EXPLICIT	0x00200
186#define SEC_ASN1_ANY		0x00400
187#define SEC_ASN1_INLINE		0x00800
188#define SEC_ASN1_POINTER	0x01000
189#define SEC_ASN1_GROUP		0x02000	/* with SET or SEQUENCE means
190									 * SET OF or SEQUENCE OF */
191#define SEC_ASN1_DYNAMIC	0x04000 /* subtemplate is found by calling
192									 * a function at runtime */
193#define SEC_ASN1_SKIP		0x08000 /* skip a field; only for decoding */
194#define SEC_ASN1_INNER		0x10000	/* with ANY means capture the
195									 * contents only (not the id, len,
196									 * or eoc); only for decoding */
197#define SEC_ASN1_SAVE		0x20000 /* stash away the encoded bytes first;
198									 * only for decoding */
199#define SEC_ASN1_SKIP_REST	0x80000	/* skip all following fields;
200									 * only for decoding */
201#define SEC_ASN1_CHOICE     0x100000 /* pick one from a template */
202
203/*
204 * Indicate that a type SEC_ASN1_INTEGER is actually signed.
205 * The default is unsigned, which causes a leading zero to be
206 * encoded if the MS bit of the source data is 1.
207 */
208#define SEC_ASN1_SIGNED_INT	0X800000
209
210/* Shorthand/Aliases */
211#define SEC_ASN1_SEQUENCE_OF	(SEC_ASN1_GROUP | SEC_ASN1_SEQUENCE)
212#define SEC_ASN1_SET_OF			(SEC_ASN1_GROUP | SEC_ASN1_SET)
213#define SEC_ASN1_ANY_CONTENTS	(SEC_ASN1_ANY | SEC_ASN1_INNER)
214
215/*
216 * Function used for SEC_ASN1_DYNAMIC.
217 * "arg"  is a pointer to the top-level structure being encoded or
218 *        decoded.
219 *
220 * "enc"  when true, means that we are encoding (false means decoding)
221 *
222 * "buf"  For decode only; points to the start of the decoded data for
223 *        the current template. Callee can use the tag at this location
224 *        to infer the returned template. Not used on encode.
225 *
226 * "Dest" points to the template-specific item being decoded to
227 *        or encoded from. (This is as opposed to arg, which
228 *        points to the start of the struct associated with the
229 *        current array of templates).
230 */
231
232typedef const SecAsn1Template * SecAsn1TemplateChooser(
233	void *arg,
234	Boolean enc,
235	const char *buf,
236	void *dest);
237
238typedef SecAsn1TemplateChooser * SecAsn1TemplateChooserPtr;
239
240
241#endif /* _SEC_ASN1_TYPES_H_ */
242