]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_asn1/lib/SecAsn1Types.h
9cb362b9183cc68260446d062519866bcff32f67
[apple/security.git] / OSX / libsecurity_asn1 / lib / SecAsn1Types.h
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
48 #pragma clang diagnostic push
49 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
50
51 #if TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR
52 /* @@@ We need something that tells us which platform we are building
53 for that let's us distinguish if we are doing an emulator build. */
54
55 typedef struct {
56 size_t Length;
57 uint8_t * __nullable Data;
58 } SecAsn1Item, SecAsn1Oid;
59
60 typedef struct {
61 SecAsn1Oid algorithm;
62 SecAsn1Item parameters;
63 } SecAsn1AlgId;
64
65 typedef struct {
66 SecAsn1AlgId algorithm;
67 SecAsn1Item subjectPublicKey;
68 } SecAsn1PubKeyInfo;
69
70 #else
71 #include <Security/cssmtype.h>
72 #include <Security/x509defs.h>
73
74 typedef CSSM_DATA SecAsn1Item;
75 typedef CSSM_OID SecAsn1Oid;
76 typedef CSSM_X509_ALGORITHM_IDENTIFIER SecAsn1AlgId;
77 typedef CSSM_X509_SUBJECT_PUBLIC_KEY_INFO SecAsn1PubKeyInfo;
78
79 #endif
80
81 CF_ASSUME_NONNULL_BEGIN
82
83 /*
84 * An array of these structures defines a BER/DER encoding for an object.
85 *
86 * The array usually starts with a dummy entry whose kind is SEC_ASN1_SEQUENCE;
87 * such an array is terminated with an entry where kind == 0. (An array
88 * which consists of a single component does not require a second dummy
89 * entry -- the array is only searched as long as previous component(s)
90 * instruct it.)
91 */
92 typedef struct SecAsn1Template_struct {
93 /*
94 * Kind of item being decoded/encoded, including tags and modifiers.
95 */
96 uint32_t kind;
97
98 /*
99 * This value is the offset from the base of the structure (i.e., the
100 * (void *) passed as 'src' to SecAsn1EncodeItem, or the 'dst' argument
101 * passed to SecAsn1CoderRef()) to the field that holds the value being
102 * decoded/encoded.
103 */
104 uint32_t offset;
105
106 /*
107 * When kind suggests it (e.g., SEC_ASN1_POINTER, SEC_ASN1_GROUP,
108 * SEC_ASN1_INLINE, or a component that is *not* a SEC_ASN1_UNIVERSAL),
109 * this points to a sub-template for nested encoding/decoding.
110 * OR, iff SEC_ASN1_DYNAMIC is set, then this is a pointer to a pointer
111 * to a function which will return the appropriate template when called
112 * at runtime. NOTE! that explicit level of indirection, which is
113 * necessary because ANSI does not allow you to store a function
114 * pointer directly as a "void *" so we must store it separately and
115 * dereference it to get at the function pointer itself.
116 */
117 const void *sub;
118
119 /*
120 * In the first element of a template array, the value is the size
121 * of the structure to allocate when this template is being referenced
122 * by another template via SEC_ASN1_POINTER or SEC_ASN1_GROUP.
123 * In all other cases, the value is ignored.
124 */
125 uint32_t size;
126 } SecAsn1Template;
127
128
129 /*
130 * BER/DER values for ASN.1 identifier octets.
131 */
132 #define SEC_ASN1_TAG_MASK 0xff
133
134 /*
135 * BER/DER universal type tag numbers.
136 */
137 #define SEC_ASN1_TAGNUM_MASK 0x1f
138 #define SEC_ASN1_BOOLEAN 0x01
139 #define SEC_ASN1_INTEGER 0x02
140 #define SEC_ASN1_BIT_STRING 0x03
141 #define SEC_ASN1_OCTET_STRING 0x04
142 #define SEC_ASN1_NULL 0x05
143 #define SEC_ASN1_OBJECT_ID 0x06
144 #define SEC_ASN1_OBJECT_DESCRIPTOR 0x07
145 /* External type and instance-of type 0x08 */
146 #define SEC_ASN1_REAL 0x09
147 #define SEC_ASN1_ENUMERATED 0x0a
148 #define SEC_ASN1_EMBEDDED_PDV 0x0b
149 #define SEC_ASN1_UTF8_STRING 0x0c
150 /* not used 0x0d */
151 /* not used 0x0e */
152 /* not used 0x0f */
153 #define SEC_ASN1_SEQUENCE 0x10
154 #define SEC_ASN1_SET 0x11
155 #define SEC_ASN1_NUMERIC_STRING 0x12
156 #define SEC_ASN1_PRINTABLE_STRING 0x13
157 #define SEC_ASN1_T61_STRING 0x14
158 #define SEC_ASN1_VIDEOTEX_STRING 0x15
159 #define SEC_ASN1_IA5_STRING 0x16
160 #define SEC_ASN1_UTC_TIME 0x17
161 #define SEC_ASN1_GENERALIZED_TIME 0x18
162 #define SEC_ASN1_GRAPHIC_STRING 0x19
163 #define SEC_ASN1_VISIBLE_STRING 0x1a
164 #define SEC_ASN1_GENERAL_STRING 0x1b
165 #define SEC_ASN1_UNIVERSAL_STRING 0x1c
166 /* not used 0x1d */
167 #define SEC_ASN1_BMP_STRING 0x1e
168 #define SEC_ASN1_HIGH_TAG_NUMBER 0x1f
169 #define SEC_ASN1_TELETEX_STRING SEC_ASN1_T61_STRING
170
171 /*
172 * Modifiers to type tags. These are also specified by a/the
173 * standard, and must not be changed.
174 */
175 #define SEC_ASN1_METHOD_MASK 0x20
176 #define SEC_ASN1_PRIMITIVE 0x00
177 #define SEC_ASN1_CONSTRUCTED 0x20
178
179 #define SEC_ASN1_CLASS_MASK 0xc0
180 #define SEC_ASN1_UNIVERSAL 0x00
181 #define SEC_ASN1_APPLICATION 0x40
182 #define SEC_ASN1_CONTEXT_SPECIFIC 0x80
183 #define SEC_ASN1_PRIVATE 0xc0
184
185 /*
186 * Our additions, used for templates.
187 * These are not defined by any standard; the values are used internally only.
188 * Just be careful to keep them out of the low 8 bits.
189 */
190 #define SEC_ASN1_OPTIONAL 0x00100
191 #define SEC_ASN1_EXPLICIT 0x00200
192 #define SEC_ASN1_ANY 0x00400
193 #define SEC_ASN1_INLINE 0x00800
194 #define SEC_ASN1_POINTER 0x01000
195 #define SEC_ASN1_GROUP 0x02000 /* with SET or SEQUENCE means
196 * SET OF or SEQUENCE OF */
197 #define SEC_ASN1_DYNAMIC 0x04000 /* subtemplate is found by calling
198 * a function at runtime */
199 #define SEC_ASN1_SKIP 0x08000 /* skip a field; only for decoding */
200 #define SEC_ASN1_INNER 0x10000 /* with ANY means capture the
201 * contents only (not the id, len,
202 * or eoc); only for decoding */
203 #define SEC_ASN1_SAVE 0x20000 /* stash away the encoded bytes first;
204 * only for decoding */
205 #define SEC_ASN1_SKIP_REST 0x80000 /* skip all following fields;
206 * only for decoding */
207 #define SEC_ASN1_CHOICE 0x100000 /* pick one from a template */
208
209 /*
210 * Indicate that a type SEC_ASN1_INTEGER is actually signed.
211 * The default is unsigned, which causes a leading zero to be
212 * encoded if the MS bit of the source data is 1.
213 */
214 #define SEC_ASN1_SIGNED_INT 0X800000
215
216 /* Shorthand/Aliases */
217 #define SEC_ASN1_SEQUENCE_OF (SEC_ASN1_GROUP | SEC_ASN1_SEQUENCE)
218 #define SEC_ASN1_SET_OF (SEC_ASN1_GROUP | SEC_ASN1_SET)
219 #define SEC_ASN1_ANY_CONTENTS (SEC_ASN1_ANY | SEC_ASN1_INNER)
220
221 /*
222 * Function used for SEC_ASN1_DYNAMIC.
223 * "arg" is a pointer to the top-level structure being encoded or
224 * decoded.
225 *
226 * "enc" when true, means that we are encoding (false means decoding)
227 *
228 * "buf" For decode only; points to the start of the decoded data for
229 * the current template. Callee can use the tag at this location
230 * to infer the returned template. Not used on encode.
231 *
232 * "len" For decode only; the length of buf.
233 *
234 * "Dest" points to the template-specific item being decoded to
235 * or encoded from. (This is as opposed to arg, which
236 * points to the start of the struct associated with the
237 * current array of templates).
238 */
239
240 typedef const SecAsn1Template * SecAsn1TemplateChooser(
241 void *arg,
242 Boolean enc,
243 const char *buf,
244 size_t len,
245 void *dest);
246
247 typedef SecAsn1TemplateChooser * SecAsn1TemplateChooserPtr;
248
249 CF_ASSUME_NONNULL_END
250
251 #pragma clang diagnostic pop
252
253 #endif /* _SEC_ASN1_TYPES_H_ */