]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/asn1t.h
Update year.
[thirdparty/openssl.git] / crypto / asn1 / asn1t.h
CommitLineData
9d6b1ce6
DSH
1/* asn1t.h */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
24484759 6 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
9d6b1ce6
DSH
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58#ifndef HEADER_ASN1T_H
59#define HEADER_ASN1T_H
60
61#include <stddef.h>
26da3e65 62#include <openssl/e_os2.h>
9d6b1ce6
DSH
63#include <openssl/asn1.h>
64
26da3e65
RL
65#ifdef OPENSSL_BUILD_SHLIBCRYPTO
66# undef OPENSSL_EXTERN
67# define OPENSSL_EXTERN OPENSSL_EXPORT
68#endif
69
9d6b1ce6
DSH
70/* ASN1 template defines, structures and functions */
71
72#ifdef __cplusplus
73extern "C" {
74#endif
75
bb5ea36b 76
62dc5aad 77#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
bb5ea36b
DSH
78
79/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
80#define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
81
82
83/* Macros for start and end of ASN1_ITEM definition */
84
85#define ASN1_ITEM_start(itname) \
86 OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
87
88#define ASN1_ITEM_end(itname) \
d339187b 89 };
bb5ea36b
DSH
90
91#else
92
93/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
94#define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
95
96
97/* Macros for start and end of ASN1_ITEM definition */
98
99#define ASN1_ITEM_start(itname) \
100 const ASN1_ITEM * itname##_it(void) \
101 { \
34f0a193 102 static const ASN1_ITEM local_it = {
bb5ea36b
DSH
103
104#define ASN1_ITEM_end(itname) \
105 }; \
106 return &local_it; \
d339187b 107 }
bb5ea36b
DSH
108
109#endif
110
111
9d6b1ce6
DSH
112/* Macros to aid ASN1 template writing */
113
114#define ASN1_ITEM_TEMPLATE(tname) \
ffd418f2 115 static const ASN1_TEMPLATE tname##_item_tt
9d6b1ce6
DSH
116
117#define ASN1_ITEM_TEMPLATE_END(tname) \
118 ;\
bb5ea36b 119 ASN1_ITEM_start(tname) \
9d6b1ce6
DSH
120 ASN1_ITYPE_PRIMITIVE,\
121 -1,\
122 &tname##_item_tt,\
123 0,\
124 NULL,\
125 0,\
126 #tname \
bb5ea36b 127 ASN1_ITEM_end(tname)
9d6b1ce6
DSH
128
129
130/* This is a ASN1 type which just embeds a template */
131
132/* This pair helps declare a SEQUENCE. We can do:
133 *
134 * ASN1_SEQUENCE(stname) = {
135 * ... SEQUENCE components ...
d339187b 136 * } ASN1_SEQUENCE_END(stname)
9d6b1ce6
DSH
137 *
138 * This will produce an ASN1_ITEM called stname_it
139 * for a structure called stname.
140 *
141 * If you want the same structure but a different
142 * name then use:
143 *
144 * ASN1_SEQUENCE(itname) = {
145 * ... SEQUENCE components ...
d339187b 146 * } ASN1_SEQUENCE_END_name(stname, itname)
9d6b1ce6
DSH
147 *
148 * This will create an item called itname_it using
149 * a structure called stname.
150 */
151
152#define ASN1_SEQUENCE(tname) \
ffd418f2 153 static const ASN1_TEMPLATE tname##_seq_tt[]
9d6b1ce6
DSH
154
155#define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
156
157#define ASN1_SEQUENCE_END_name(stname, tname) \
158 ;\
bb5ea36b 159 ASN1_ITEM_start(tname) \
9d6b1ce6
DSH
160 ASN1_ITYPE_SEQUENCE,\
161 V_ASN1_SEQUENCE,\
162 tname##_seq_tt,\
163 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
164 NULL,\
165 sizeof(stname),\
166 #stname \
bb5ea36b 167 ASN1_ITEM_end(tname)
9d6b1ce6 168
230fd6b7
DSH
169#define ASN1_NDEF_SEQUENCE(tname) \
170 ASN1_SEQUENCE(tname)
171
9d6b1ce6 172#define ASN1_SEQUENCE_cb(tname, cb) \
ffd418f2 173 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
9d6b1ce6
DSH
174 ASN1_SEQUENCE(tname)
175
176#define ASN1_BROKEN_SEQUENCE(tname) \
ffd418f2 177 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
9d6b1ce6
DSH
178 ASN1_SEQUENCE(tname)
179
180#define ASN1_SEQUENCE_ref(tname, cb, lck) \
ffd418f2 181 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
9d6b1ce6
DSH
182 ASN1_SEQUENCE(tname)
183
184#define ASN1_SEQUENCE_enc(tname, enc, cb) \
ffd418f2 185 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
9d6b1ce6
DSH
186 ASN1_SEQUENCE(tname)
187
230fd6b7
DSH
188#define ASN1_NDEF_SEQUENCE_END(tname) \
189 ;\
190 ASN1_ITEM_start(tname) \
191 ASN1_ITYPE_NDEF_SEQUENCE,\
192 V_ASN1_SEQUENCE,\
193 tname##_seq_tt,\
194 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
195 NULL,\
196 sizeof(tname),\
197 #tname \
198 ASN1_ITEM_end(tname)
199
9d6b1ce6
DSH
200#define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
201
202#define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
203
204#define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
205
206#define ASN1_SEQUENCE_END_ref(stname, tname) \
207 ;\
bb5ea36b 208 ASN1_ITEM_start(tname) \
9d6b1ce6
DSH
209 ASN1_ITYPE_SEQUENCE,\
210 V_ASN1_SEQUENCE,\
211 tname##_seq_tt,\
212 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
213 &tname##_aux,\
214 sizeof(stname),\
215 #stname \
bb5ea36b 216 ASN1_ITEM_end(tname)
9d6b1ce6
DSH
217
218
219/* This pair helps declare a CHOICE type. We can do:
220 *
221 * ASN1_CHOICE(chname) = {
222 * ... CHOICE options ...
d339187b 223 * ASN1_CHOICE_END(chname)
9d6b1ce6
DSH
224 *
225 * This will produce an ASN1_ITEM called chname_it
226 * for a structure called chname. The structure
227 * definition must look like this:
228 * typedef struct {
229 * int type;
230 * union {
231 * ASN1_SOMETHING *opt1;
232 * ASN1_SOMEOTHER *opt2;
233 * } value;
234 * } chname;
235 *
236 * the name of the selector must be 'type'.
237 * to use an alternative selector name use the
238 * ASN1_CHOICE_END_selector() version.
239 */
240
241#define ASN1_CHOICE(tname) \
ffd418f2 242 static const ASN1_TEMPLATE tname##_ch_tt[]
9d6b1ce6
DSH
243
244#define ASN1_CHOICE_cb(tname, cb) \
ffd418f2 245 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
9d6b1ce6
DSH
246 ASN1_CHOICE(tname)
247
248#define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
249
250#define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
251
252#define ASN1_CHOICE_END_selector(stname, tname, selname) \
253 ;\
bb5ea36b 254 ASN1_ITEM_start(tname) \
9d6b1ce6
DSH
255 ASN1_ITYPE_CHOICE,\
256 offsetof(stname,selname) ,\
257 tname##_ch_tt,\
258 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
259 NULL,\
260 sizeof(stname),\
261 #stname \
bb5ea36b 262 ASN1_ITEM_end(tname)
9d6b1ce6
DSH
263
264#define ASN1_CHOICE_END_cb(stname, tname, selname) \
265 ;\
bb5ea36b 266 ASN1_ITEM_start(tname) \
9d6b1ce6
DSH
267 ASN1_ITYPE_CHOICE,\
268 offsetof(stname,selname) ,\
269 tname##_ch_tt,\
270 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
271 &tname##_aux,\
272 sizeof(stname),\
273 #stname \
bb5ea36b 274 ASN1_ITEM_end(tname)
9d6b1ce6
DSH
275
276/* This helps with the template wrapper form of ASN1_ITEM */
277
278#define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
279 (flags), (tag), 0,\
bb5ea36b 280 #name, ASN1_ITEM_ref(type) }
9d6b1ce6
DSH
281
282/* These help with SEQUENCE or CHOICE components */
283
284/* used to declare other types */
285
286#define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
287 (flags), (tag), offsetof(stname, field),\
bb5ea36b 288 #field, ASN1_ITEM_ref(type) }
9d6b1ce6
DSH
289
290/* used when the structure is combined with the parent */
291
292#define ASN1_EX_COMBINE(flags, tag, type) { \
bb5ea36b 293 (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
9d6b1ce6
DSH
294
295/* implicit and explicit helper macros */
296
297#define ASN1_IMP_EX(stname, field, type, tag, ex) \
298 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
299
300#define ASN1_EXP_EX(stname, field, type, tag, ex) \
301 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
302
303/* Any defined by macros: the field used is in the table itself */
304
62dc5aad 305#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
bb5ea36b
DSH
306#define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
307#define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
308#else
309#define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
310#define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
311#endif
9d6b1ce6
DSH
312/* Plain simple type */
313#define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
314
315/* OPTIONAL simple type */
316#define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
317
318/* IMPLICIT tagged simple type */
319#define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
320
321/* IMPLICIT tagged OPTIONAL simple type */
322#define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
323
324/* Same as above but EXPLICIT */
325
326#define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
327#define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
328
329/* SEQUENCE OF type */
330#define ASN1_SEQUENCE_OF(stname, field, type) \
331 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
332
333/* OPTIONAL SEQUENCE OF */
334#define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
335 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
336
337/* Same as above but for SET OF */
338
339#define ASN1_SET_OF(stname, field, type) \
340 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
341
342#define ASN1_SET_OF_OPT(stname, field, type) \
343 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
344
345/* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
346
347#define ASN1_IMP_SET_OF(stname, field, type, tag) \
348 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
349
350#define ASN1_EXP_SET_OF(stname, field, type, tag) \
351 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
352
353#define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
354 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
355
356#define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
357 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
358
359#define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
360 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
361
362#define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
363 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
364
365#define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
366 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
367
368#define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
369 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
370
f8492ffe
DSH
371/* EXPLICIT using indefinite length constructed form */
372#define ASN1_NDEF_EXP(stname, field, type, tag) \
373 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
374
230fd6b7
DSH
375/* EXPLICIT OPTIONAL using indefinite length constructed form */
376#define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
377 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
378
9d6b1ce6
DSH
379/* Macros for the ASN1_ADB structure */
380
381#define ASN1_ADB(name) \
ffd418f2 382 static const ASN1_ADB_TABLE name##_adbtbl[]
9d6b1ce6 383
62dc5aad 384#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
bb5ea36b 385
9d6b1ce6
DSH
386#define ASN1_ADB_END(name, flags, field, app_table, def, none) \
387 ;\
ffd418f2 388 static const ASN1_ADB name##_adb = {\
9d6b1ce6
DSH
389 flags,\
390 offsetof(name, field),\
391 app_table,\
392 name##_adbtbl,\
393 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
394 def,\
395 none\
396 }
397
bb5ea36b
DSH
398#else
399
400#define ASN1_ADB_END(name, flags, field, app_table, def, none) \
401 ;\
ffd418f2 402 static const ASN1_ITEM *name##_adb(void) \
bb5ea36b 403 { \
ffd418f2 404 static const ASN1_ADB internal_adb = \
bb5ea36b
DSH
405 {\
406 flags,\
407 offsetof(name, field),\
408 app_table,\
409 name##_adbtbl,\
410 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
411 def,\
412 none\
413 }; \
414 return (const ASN1_ITEM *) &internal_adb; \
415 } \
416 void dummy_function(void)
417
418#endif
419
9d6b1ce6
DSH
420#define ADB_ENTRY(val, template) {val, template}
421
422#define ASN1_ADB_TEMPLATE(name) \
ffd418f2 423 static const ASN1_TEMPLATE name##_tt
9d6b1ce6
DSH
424
425/* This is the ASN1 template structure that defines
426 * a wrapper round the actual type. It determines the
427 * actual position of the field in the value structure,
428 * various flags such as OPTIONAL and the field name.
429 */
430
431struct ASN1_TEMPLATE_st {
432unsigned long flags; /* Various flags */
433long tag; /* tag, not used if no tagging */
434unsigned long offset; /* Offset of this field in structure */
435#ifndef NO_ASN1_FIELD_NAMES
9edf4e81 436const char *field_name; /* Field name */
9d6b1ce6 437#endif
bb5ea36b 438ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
9d6b1ce6
DSH
439};
440
bb5ea36b
DSH
441/* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
442
443#define ASN1_TEMPLATE_item(t) (t->item_ptr)
444#define ASN1_TEMPLATE_adb(t) (t->item_ptr)
9d6b1ce6
DSH
445
446typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
447typedef struct ASN1_ADB_st ASN1_ADB;
448
449struct ASN1_ADB_st {
450 unsigned long flags; /* Various flags */
451 unsigned long offset; /* Offset of selector field */
452 STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
453 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
454 long tblcount; /* Number of entries in tbl */
455 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
456 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
457};
458
459struct ASN1_ADB_TABLE_st {
460 long value; /* NID for an object or value for an int */
461 const ASN1_TEMPLATE tt; /* item for this value */
462};
463
464/* template flags */
465
466/* Field is optional */
467#define ASN1_TFLG_OPTIONAL (0x1)
468
469/* Field is a SET OF */
470#define ASN1_TFLG_SET_OF (0x1 << 1)
471
472/* Field is a SEQUENCE OF */
473#define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
474
06db4253
DSH
475/* Special case: this refers to a SET OF that
476 * will be sorted into DER order when encoded *and*
477 * the corresponding STACK will be modified to match
478 * the new order.
479 */
480#define ASN1_TFLG_SET_ORDER (0x3 << 1)
481
482/* Mask for SET OF or SEQUENCE OF */
9d6b1ce6
DSH
483#define ASN1_TFLG_SK_MASK (0x3 << 1)
484
485/* These flags mean the tag should be taken from the
486 * tag field. If EXPLICIT then the underlying type
487 * is used for the inner tag.
488 */
489
490/* IMPLICIT tagging */
491#define ASN1_TFLG_IMPTAG (0x1 << 3)
492
493
494/* EXPLICIT tagging, inner tag from underlying type */
495#define ASN1_TFLG_EXPTAG (0x2 << 3)
496
497#define ASN1_TFLG_TAG_MASK (0x3 << 3)
498
499/* context specific IMPLICIT */
500#define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
501
502/* context specific EXPLICIT */
503#define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
504
505/* If tagging is in force these determine the
506 * type of tag to use. Otherwise the tag is
507 * determined by the underlying type. These
508 * values reflect the actual octet format.
509 */
510
511/* Universal tag */
512#define ASN1_TFLG_UNIVERSAL (0x0<<6)
513/* Application tag */
514#define ASN1_TFLG_APPLICATION (0x1<<6)
515/* Context specific tag */
516#define ASN1_TFLG_CONTEXT (0x2<<6)
517/* Private tag */
518#define ASN1_TFLG_PRIVATE (0x3<<6)
519
520#define ASN1_TFLG_TAG_CLASS (0x3<<6)
521
522/* These are for ANY DEFINED BY type. In this case
523 * the 'item' field points to an ASN1_ADB structure
524 * which contains a table of values to decode the
525 * relevant type
526 */
527
528#define ASN1_TFLG_ADB_MASK (0x3<<8)
529
530#define ASN1_TFLG_ADB_OID (0x1<<8)
531
532#define ASN1_TFLG_ADB_INT (0x1<<9)
533
534/* This flag means a parent structure is passed
535 * instead of the field: this is useful is a
536 * SEQUENCE is being combined with a CHOICE for
537 * example. Since this means the structure and
538 * item name will differ we need to use the
539 * ASN1_CHOICE_END_name() macro for example.
540 */
541
542#define ASN1_TFLG_COMBINE (0x1<<10)
543
230fd6b7
DSH
544/* This flag when present in a SEQUENCE OF, SET OF
545 * or EXPLICIT causes indefinite length constructed
546 * encoding to be used if required.
547 */
548
549#define ASN1_TFLG_NDEF (0x1<<11)
550
9d6b1ce6
DSH
551/* This is the actual ASN1 item itself */
552
553struct ASN1_ITEM_st {
554char itype; /* The item type, primitive, SEQUENCE, CHOICE or extern */
555long utype; /* underlying type */
556const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */
557long tcount; /* Number of templates if SEQUENCE or CHOICE */
558const void *funcs; /* functions that handle this type */
559long size; /* Structure size (usually)*/
560#ifndef NO_ASN1_FIELD_NAMES
561const char *sname; /* Structure name */
562#endif
563};
564
565/* These are values for the itype field and
566 * determine how the type is interpreted.
567 *
568 * For PRIMITIVE types the underlying type
569 * determines the behaviour if items is NULL.
570 *
571 * Otherwise templates must contain a single
572 * template and the type is treated in the
573 * same way as the type specified in the template.
574 *
575 * For SEQUENCE types the templates field points
576 * to the members, the size field is the
577 * structure size.
578 *
579 * For CHOICE types the templates field points
580 * to each possible member (typically a union)
581 * and the 'size' field is the offset of the
582 * selector.
583 *
584 * The 'funcs' field is used for application
585 * specific functions.
586 *
587 * For COMPAT types the funcs field gives a
588 * set of functions that handle this type, this
589 * supports the old d2i, i2d convention.
590 *
591 * The EXTERN type uses a new style d2i/i2d.
592 * The new style should be used where possible
593 * because it avoids things like the d2i IMPLICIT
594 * hack.
595 *
596 * MSTRING is a multiple string type, it is used
597 * for a CHOICE of character strings where the
598 * actual strings all occupy an ASN1_STRING
599 * structure. In this case the 'utype' field
600 * has a special meaning, it is used as a mask
601 * of acceptable types using the B_ASN1 constants.
602 *
230fd6b7
DSH
603 * NDEF_SEQUENCE is the same as SEQUENCE except
604 * that it will use indefinite length constructed
605 * encoding if requested.
606 *
9d6b1ce6
DSH
607 */
608
230fd6b7
DSH
609#define ASN1_ITYPE_PRIMITIVE 0x0
610
611#define ASN1_ITYPE_SEQUENCE 0x1
9d6b1ce6 612
230fd6b7 613#define ASN1_ITYPE_CHOICE 0x2
9d6b1ce6 614
230fd6b7 615#define ASN1_ITYPE_COMPAT 0x3
9d6b1ce6 616
230fd6b7 617#define ASN1_ITYPE_EXTERN 0x4
9d6b1ce6 618
230fd6b7 619#define ASN1_ITYPE_MSTRING 0x5
9d6b1ce6 620
230fd6b7 621#define ASN1_ITYPE_NDEF_SEQUENCE 0x6
9d6b1ce6
DSH
622
623/* Cache for ASN1 tag and length, so we
624 * don't keep re-reading it for things
625 * like CHOICE
626 */
627
628struct ASN1_TLC_st{
629 char valid; /* Values below are valid */
630 int ret; /* return value */
631 long plen; /* length */
632 int ptag; /* class value */
633 int pclass; /* class value */
634 int hdrlen; /* header length */
635};
636
637/* Typedefs for ASN1 function pointers */
638
639typedef ASN1_VALUE * ASN1_new_func(void);
640typedef void ASN1_free_func(ASN1_VALUE *a);
875a644a 641typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length);
9d6b1ce6
DSH
642typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
643
875a644a 644typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
9d6b1ce6
DSH
645 int tag, int aclass, char opt, ASN1_TLC *ctx);
646
647typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
648typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
649typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
650
1ef7acfe
DSH
651typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
652 int indent, const char *fname,
653 const ASN1_PCTX *pctx);
654
9d6b1ce6 655typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
875a644a 656typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
9e5df8e4 657typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent, const ASN1_PCTX *pctx);
9d6b1ce6
DSH
658
659typedef struct ASN1_COMPAT_FUNCS_st {
660 ASN1_new_func *asn1_new;
661 ASN1_free_func *asn1_free;
662 ASN1_d2i_func *asn1_d2i;
663 ASN1_i2d_func *asn1_i2d;
664} ASN1_COMPAT_FUNCS;
665
666typedef struct ASN1_EXTERN_FUNCS_st {
667 void *app_data;
668 ASN1_ex_new_func *asn1_ex_new;
669 ASN1_ex_free_func *asn1_ex_free;
670 ASN1_ex_free_func *asn1_ex_clear;
671 ASN1_ex_d2i *asn1_ex_d2i;
672 ASN1_ex_i2d *asn1_ex_i2d;
1ef7acfe 673 ASN1_ex_print_func *asn1_ex_print;
9d6b1ce6
DSH
674} ASN1_EXTERN_FUNCS;
675
676typedef struct ASN1_PRIMITIVE_FUNCS_st {
677 void *app_data;
678 unsigned long flags;
679 ASN1_ex_new_func *prim_new;
680 ASN1_ex_free_func *prim_free;
681 ASN1_ex_free_func *prim_clear;
682 ASN1_primitive_c2i *prim_c2i;
683 ASN1_primitive_i2c *prim_i2c;
9e5df8e4 684 ASN1_primitive_print *prim_print;
9d6b1ce6
DSH
685} ASN1_PRIMITIVE_FUNCS;
686
687/* This is the ASN1_AUX structure: it handles various
688 * miscellaneous requirements. For example the use of
689 * reference counts and an informational callback.
690 *
691 * The "informational callback" is called at various
692 * points during the ASN1 encoding and decoding. It can
693 * be used to provide minor customisation of the structures
694 * used. This is most useful where the supplied routines
695 * *almost* do the right thing but need some extra help
696 * at a few points. If the callback returns zero then
697 * it is assumed a fatal error has occurred and the
698 * main operation should be abandoned.
699 *
700 * If major changes in the default behaviour are required
701 * then an external type is more appropriate.
702 */
703
24484759
DSH
704typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
705 void *exarg);
9d6b1ce6
DSH
706
707typedef struct ASN1_AUX_st {
708 void *app_data;
709 int flags;
710 int ref_offset; /* Offset of reference value */
711 int ref_lock; /* Lock type to use */
712 ASN1_aux_cb *asn1_cb;
713 int enc_offset; /* Offset of ASN1_ENCODING structure */
714} ASN1_AUX;
715
24484759
DSH
716/* For print related callbacks exarg points to this structure */
717typedef struct ASN1_PRINT_ARG_st {
718 BIO *out;
719 int indent;
0c072a0b 720 const ASN1_PCTX *pctx;
24484759
DSH
721} ASN1_PRINT_ARG;
722
9d6b1ce6
DSH
723/* Flags in ASN1_AUX */
724
725/* Use a reference count */
726#define ASN1_AFLG_REFCOUNT 1
727/* Save the encoding of structure (useful for signatures) */
728#define ASN1_AFLG_ENCODING 2
729/* The Sequence length is invalid */
730#define ASN1_AFLG_BROKEN 4
731
732/* operation values for asn1_cb */
733
734#define ASN1_OP_NEW_PRE 0
735#define ASN1_OP_NEW_POST 1
736#define ASN1_OP_FREE_PRE 2
737#define ASN1_OP_FREE_POST 3
738#define ASN1_OP_D2I_PRE 4
739#define ASN1_OP_D2I_POST 5
740#define ASN1_OP_I2D_PRE 6
741#define ASN1_OP_I2D_POST 7
24484759
DSH
742#define ASN1_OP_PRINT_PRE 8
743#define ASN1_OP_PRINT_POST 9
9d6b1ce6
DSH
744
745/* Macro to implement a primitive type */
746#define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
bb5ea36b
DSH
747#define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
748 ASN1_ITEM_start(itname) \
749 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
d339187b 750 ASN1_ITEM_end(itname)
9d6b1ce6
DSH
751
752/* Macro to implement a multi string type */
bb5ea36b
DSH
753#define IMPLEMENT_ASN1_MSTRING(itname, mask) \
754 ASN1_ITEM_start(itname) \
755 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
d339187b 756 ASN1_ITEM_end(itname)
9d6b1ce6
DSH
757
758/* Macro to implement an ASN1_ITEM in terms of old style funcs */
759
760#define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
761
762#define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
763 static const ASN1_COMPAT_FUNCS sname##_ff = { \
764 (ASN1_new_func *)sname##_new, \
765 (ASN1_free_func *)sname##_free, \
766 (ASN1_d2i_func *)d2i_##sname, \
767 (ASN1_i2d_func *)i2d_##sname, \
768 }; \
bb5ea36b 769 ASN1_ITEM_start(sname) \
9d6b1ce6
DSH
770 ASN1_ITYPE_COMPAT, \
771 tag, \
772 NULL, \
773 0, \
774 &sname##_ff, \
775 0, \
776 #sname \
d339187b 777 ASN1_ITEM_end(sname)
9d6b1ce6
DSH
778
779#define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
bb5ea36b 780 ASN1_ITEM_start(sname) \
9d6b1ce6
DSH
781 ASN1_ITYPE_EXTERN, \
782 tag, \
783 NULL, \
784 0, \
785 &fptrs, \
786 0, \
787 #sname \
d339187b 788 ASN1_ITEM_end(sname)
9d6b1ce6
DSH
789
790/* Macro to implement standard functions in terms of ASN1_ITEM structures */
791
792#define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
793
794#define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
795
796#define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
797 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
798
f8492ffe
DSH
799#define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
800 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
801
ea3675b5
DSH
802#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
803 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
804
f8492ffe
DSH
805#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
806 pre stname *fname##_new(void) \
9d6b1ce6 807 { \
bb5ea36b 808 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
9d6b1ce6 809 } \
f8492ffe 810 pre void fname##_free(stname *a) \
9d6b1ce6 811 { \
bb5ea36b 812 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
9d6b1ce6
DSH
813 }
814
f8492ffe 815#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
54b5fd53
DSH
816 stname *fname##_new(void) \
817 { \
818 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
819 } \
820 void fname##_free(stname *a) \
821 { \
822 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
823 }
f8492ffe 824
9d6b1ce6
DSH
825#define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
826 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
827 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
828
829#define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
875a644a 830 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
9d6b1ce6 831 { \
bb5ea36b 832 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
9d6b1ce6
DSH
833 } \
834 int i2d_##fname(stname *a, unsigned char **out) \
835 { \
bb5ea36b 836 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
9d6b1ce6
DSH
837 }
838
230fd6b7
DSH
839#define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
840 int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
841 { \
842 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
843 }
844
9d6b1ce6
DSH
845/* This includes evil casts to remove const: they will go away when full
846 * ASN1 constification is done.
847 */
848#define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
849 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
850 { \
875a644a 851 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
9d6b1ce6
DSH
852 } \
853 int i2d_##fname(const stname *a, unsigned char **out) \
854 { \
bb5ea36b 855 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
9d6b1ce6
DSH
856 }
857
1241126a
DSH
858#define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
859 stname * stname##_dup(stname *x) \
860 { \
861 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
862 }
863
9194296d
DSH
864#define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
865 IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
866
867#define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
2a45408c 868 int fname##_print_ctx(BIO *out, stname *x, int indent, \
9194296d
DSH
869 const ASN1_PCTX *pctx) \
870 { \
871 return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \
872 ASN1_ITEM_rptr(itname), pctx); \
873 }
874
9d6b1ce6
DSH
875#define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
876 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
877
878#define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
879 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
880 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
881
882/* external definitions for primitive types */
883
bb5ea36b
DSH
884DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
885DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
886DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
bb5ea36b
DSH
887DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
888DECLARE_ASN1_ITEM(CBIGNUM)
889DECLARE_ASN1_ITEM(BIGNUM)
890DECLARE_ASN1_ITEM(LONG)
891DECLARE_ASN1_ITEM(ZLONG)
9d6b1ce6
DSH
892
893DECLARE_STACK_OF(ASN1_VALUE)
894
895/* Functions used internally by the ASN1 code */
896
897int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
898void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
899int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
900int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
901
902void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
875a644a
RL
903int ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_TEMPLATE *tt);
904int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
9d6b1ce6
DSH
905 int tag, int aclass, char opt, ASN1_TLC *ctx);
906
907int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
908int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt);
909void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
910
911int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
875a644a 912int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
9d6b1ce6
DSH
913
914int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
915int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
916
917ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
918
919const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
920
921int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
922
923void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
924void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
925int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
875a644a 926int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const ASN1_ITEM *it);
9d6b1ce6
DSH
927
928#ifdef __cplusplus
929}
930#endif
931#endif