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