3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
\r
6 /* ====================================================================
\r
7 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
\r
9 * Redistribution and use in source and binary forms, with or without
\r
10 * modification, are permitted provided that the following conditions
\r
13 * 1. Redistributions of source code must retain the above copyright
\r
14 * notice, this list of conditions and the following disclaimer.
\r
16 * 2. Redistributions in binary form must reproduce the above copyright
\r
17 * notice, this list of conditions and the following disclaimer in
\r
18 * the documentation and/or other materials provided with the
\r
21 * 3. All advertising materials mentioning features or use of this
\r
22 * software must display the following acknowledgment:
\r
23 * "This product includes software developed by the OpenSSL Project
\r
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
\r
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
\r
27 * endorse or promote products derived from this software without
\r
28 * prior written permission. For written permission, please contact
\r
29 * licensing@OpenSSL.org.
\r
31 * 5. Products derived from this software may not be called "OpenSSL"
\r
32 * nor may "OpenSSL" appear in their names without prior written
\r
33 * permission of the OpenSSL Project.
\r
35 * 6. Redistributions of any form whatsoever must retain the following
\r
37 * "This product includes software developed by the OpenSSL Project
\r
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
\r
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
\r
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
\r
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
\r
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
\r
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
\r
52 * ====================================================================
\r
54 * This product includes cryptographic software written by Eric Young
\r
55 * (eay@cryptsoft.com). This product includes software written by Tim
\r
56 * Hudson (tjh@cryptsoft.com).
\r
59 #ifndef HEADER_ASN1T_H
\r
60 # define HEADER_ASN1T_H
\r
62 # include <stddef.h>
\r
63 # include <openssl/e_os2.h>
\r
64 # include <openssl/asn1.h>
\r
66 # ifdef OPENSSL_BUILD_SHLIBCRYPTO
\r
67 # undef OPENSSL_EXTERN
\r
68 # define OPENSSL_EXTERN OPENSSL_EXPORT
\r
71 /* ASN1 template defines, structures and functions */
\r
77 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
\r
79 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
\r
80 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
\r
82 /* Macros for start and end of ASN1_ITEM definition */
\r
84 # define ASN1_ITEM_start(itname) \
\r
85 OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
\r
87 # define ASN1_ITEM_end(itname) \
\r
92 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
\r
93 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
\r
95 /* Macros for start and end of ASN1_ITEM definition */
\r
97 # define ASN1_ITEM_start(itname) \
\r
98 const ASN1_ITEM * itname##_it(void) \
\r
100 static const ASN1_ITEM local_it = {
\r
102 # define ASN1_ITEM_end(itname) \
\r
104 return &local_it; \
\r
109 /* Macros to aid ASN1 template writing */
\r
111 # define ASN1_ITEM_TEMPLATE(tname) \
\r
112 static const ASN1_TEMPLATE tname##_item_tt
\r
114 # define ASN1_ITEM_TEMPLATE_END(tname) \
\r
116 ASN1_ITEM_start(tname) \
\r
117 ASN1_ITYPE_PRIMITIVE,\
\r
124 ASN1_ITEM_end(tname)
\r
126 /* This is a ASN1 type which just embeds a template */
\r
129 * This pair helps declare a SEQUENCE. We can do:
\r
131 * ASN1_SEQUENCE(stname) = {
\r
132 * ... SEQUENCE components ...
\r
133 * } ASN1_SEQUENCE_END(stname)
\r
135 * This will produce an ASN1_ITEM called stname_it
\r
136 * for a structure called stname.
\r
138 * If you want the same structure but a different
\r
141 * ASN1_SEQUENCE(itname) = {
\r
142 * ... SEQUENCE components ...
\r
143 * } ASN1_SEQUENCE_END_name(stname, itname)
\r
145 * This will create an item called itname_it using
\r
146 * a structure called stname.
\r
149 # define ASN1_SEQUENCE(tname) \
\r
150 static const ASN1_TEMPLATE tname##_seq_tt[]
\r
152 # define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
\r
154 # define ASN1_SEQUENCE_END_name(stname, tname) \
\r
156 ASN1_ITEM_start(tname) \
\r
157 ASN1_ITYPE_SEQUENCE,\
\r
160 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
\r
164 ASN1_ITEM_end(tname)
\r
166 # define ASN1_NDEF_SEQUENCE(tname) \
\r
167 ASN1_SEQUENCE(tname)
\r
169 # define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
\r
170 ASN1_SEQUENCE_cb(tname, cb)
\r
172 # define ASN1_SEQUENCE_cb(tname, cb) \
\r
173 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
\r
174 ASN1_SEQUENCE(tname)
\r
176 # define ASN1_BROKEN_SEQUENCE(tname) \
\r
177 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
\r
178 ASN1_SEQUENCE(tname)
\r
180 # define ASN1_SEQUENCE_ref(tname, cb, lck) \
\r
181 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
\r
182 ASN1_SEQUENCE(tname)
\r
184 # define ASN1_SEQUENCE_enc(tname, enc, cb) \
\r
185 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
\r
186 ASN1_SEQUENCE(tname)
\r
188 # define ASN1_NDEF_SEQUENCE_END(tname) \
\r
190 ASN1_ITEM_start(tname) \
\r
191 ASN1_ITYPE_NDEF_SEQUENCE,\
\r
194 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
\r
198 ASN1_ITEM_end(tname)
\r
200 # define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
\r
202 # define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
\r
204 # define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
\r
206 # define ASN1_SEQUENCE_END_ref(stname, tname) \
\r
208 ASN1_ITEM_start(tname) \
\r
209 ASN1_ITYPE_SEQUENCE,\
\r
212 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
\r
216 ASN1_ITEM_end(tname)
\r
218 # define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
\r
220 ASN1_ITEM_start(tname) \
\r
221 ASN1_ITYPE_NDEF_SEQUENCE,\
\r
224 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
\r
228 ASN1_ITEM_end(tname)
\r
231 * This pair helps declare a CHOICE type. We can do:
\r
233 * ASN1_CHOICE(chname) = {
\r
234 * ... CHOICE options ...
\r
235 * ASN1_CHOICE_END(chname)
\r
237 * This will produce an ASN1_ITEM called chname_it
\r
238 * for a structure called chname. The structure
\r
239 * definition must look like this:
\r
243 * ASN1_SOMETHING *opt1;
\r
244 * ASN1_SOMEOTHER *opt2;
\r
248 * the name of the selector must be 'type'.
\r
249 * to use an alternative selector name use the
\r
250 * ASN1_CHOICE_END_selector() version.
\r
253 # define ASN1_CHOICE(tname) \
\r
254 static const ASN1_TEMPLATE tname##_ch_tt[]
\r
256 # define ASN1_CHOICE_cb(tname, cb) \
\r
257 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
\r
260 # define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
\r
262 # define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
\r
264 # define ASN1_CHOICE_END_selector(stname, tname, selname) \
\r
266 ASN1_ITEM_start(tname) \
\r
267 ASN1_ITYPE_CHOICE,\
\r
268 offsetof(stname,selname) ,\
\r
270 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
\r
274 ASN1_ITEM_end(tname)
\r
276 # define ASN1_CHOICE_END_cb(stname, tname, selname) \
\r
278 ASN1_ITEM_start(tname) \
\r
279 ASN1_ITYPE_CHOICE,\
\r
280 offsetof(stname,selname) ,\
\r
282 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
\r
286 ASN1_ITEM_end(tname)
\r
288 /* This helps with the template wrapper form of ASN1_ITEM */
\r
290 # define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
\r
291 (flags), (tag), 0,\
\r
292 #name, ASN1_ITEM_ref(type) }
\r
294 /* These help with SEQUENCE or CHOICE components */
\r
296 /* used to declare other types */
\r
298 # define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
\r
299 (flags), (tag), offsetof(stname, field),\
\r
300 #field, ASN1_ITEM_ref(type) }
\r
302 /* used when the structure is combined with the parent */
\r
304 # define ASN1_EX_COMBINE(flags, tag, type) { \
\r
305 (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
\r
307 /* implicit and explicit helper macros */
\r
309 # define ASN1_IMP_EX(stname, field, type, tag, ex) \
\r
310 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
\r
312 # define ASN1_EXP_EX(stname, field, type, tag, ex) \
\r
313 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
\r
315 /* Any defined by macros: the field used is in the table itself */
\r
317 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
\r
318 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
\r
319 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
\r
321 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
\r
322 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
\r
324 /* Plain simple type */
\r
325 # define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
\r
327 /* OPTIONAL simple type */
\r
328 # define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
\r
330 /* IMPLICIT tagged simple type */
\r
331 # define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
\r
333 /* IMPLICIT tagged OPTIONAL simple type */
\r
334 # define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
\r
336 /* Same as above but EXPLICIT */
\r
338 # define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
\r
339 # define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
\r
341 /* SEQUENCE OF type */
\r
342 # define ASN1_SEQUENCE_OF(stname, field, type) \
\r
343 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
\r
345 /* OPTIONAL SEQUENCE OF */
\r
346 # define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
\r
347 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
\r
349 /* Same as above but for SET OF */
\r
351 # define ASN1_SET_OF(stname, field, type) \
\r
352 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
\r
354 # define ASN1_SET_OF_OPT(stname, field, type) \
\r
355 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
\r
357 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
\r
359 # define ASN1_IMP_SET_OF(stname, field, type, tag) \
\r
360 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
\r
362 # define ASN1_EXP_SET_OF(stname, field, type, tag) \
\r
363 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
\r
365 # define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
\r
366 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
\r
368 # define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
\r
369 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
\r
371 # define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
\r
372 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
\r
374 # define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
\r
375 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
\r
377 # define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
\r
378 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
\r
380 # define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
\r
381 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
\r
383 /* EXPLICIT using indefinite length constructed form */
\r
384 # define ASN1_NDEF_EXP(stname, field, type, tag) \
\r
385 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
\r
387 /* EXPLICIT OPTIONAL using indefinite length constructed form */
\r
388 # define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
\r
389 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
\r
391 /* Macros for the ASN1_ADB structure */
\r
393 # define ASN1_ADB(name) \
\r
394 static const ASN1_ADB_TABLE name##_adbtbl[]
\r
396 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
\r
398 # define ASN1_ADB_END(name, flags, field, app_table, def, none) \
\r
400 static const ASN1_ADB name##_adb = {\
\r
402 offsetof(name, field),\
\r
405 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
\r
412 # define ASN1_ADB_END(name, flags, field, app_table, def, none) \
\r
414 static const ASN1_ITEM *name##_adb(void) \
\r
416 static const ASN1_ADB internal_adb = \
\r
419 offsetof(name, field),\
\r
422 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
\r
426 return (const ASN1_ITEM *) &internal_adb; \
\r
428 void dummy_function(void)
\r
432 # define ADB_ENTRY(val, template) {val, template}
\r
434 # define ASN1_ADB_TEMPLATE(name) \
\r
435 static const ASN1_TEMPLATE name##_tt
\r
438 * This is the ASN1 template structure that defines a wrapper round the
\r
439 * actual type. It determines the actual position of the field in the value
\r
440 * structure, various flags such as OPTIONAL and the field name.
\r
443 struct ASN1_TEMPLATE_st {
\r
444 unsigned long flags; /* Various flags */
\r
445 long tag; /* tag, not used if no tagging */
\r
446 unsigned long offset; /* Offset of this field in structure */
\r
447 # ifndef NO_ASN1_FIELD_NAMES
\r
448 const char *field_name; /* Field name */
\r
450 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
\r
453 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
\r
455 # define ASN1_TEMPLATE_item(t) (t->item_ptr)
\r
456 # define ASN1_TEMPLATE_adb(t) (t->item_ptr)
\r
458 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
\r
459 typedef struct ASN1_ADB_st ASN1_ADB;
\r
461 struct ASN1_ADB_st {
\r
462 unsigned long flags; /* Various flags */
\r
463 unsigned long offset; /* Offset of selector field */
\r
464 STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
\r
465 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
\r
466 long tblcount; /* Number of entries in tbl */
\r
467 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
\r
468 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
\r
471 struct ASN1_ADB_TABLE_st {
\r
472 long value; /* NID for an object or value for an int */
\r
473 const ASN1_TEMPLATE tt; /* item for this value */
\r
476 /* template flags */
\r
478 /* Field is optional */
\r
479 # define ASN1_TFLG_OPTIONAL (0x1)
\r
481 /* Field is a SET OF */
\r
482 # define ASN1_TFLG_SET_OF (0x1 << 1)
\r
484 /* Field is a SEQUENCE OF */
\r
485 # define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
\r
488 * Special case: this refers to a SET OF that will be sorted into DER order
\r
489 * when encoded *and* the corresponding STACK will be modified to match the
\r
492 # define ASN1_TFLG_SET_ORDER (0x3 << 1)
\r
494 /* Mask for SET OF or SEQUENCE OF */
\r
495 # define ASN1_TFLG_SK_MASK (0x3 << 1)
\r
498 * These flags mean the tag should be taken from the tag field. If EXPLICIT
\r
499 * then the underlying type is used for the inner tag.
\r
502 /* IMPLICIT tagging */
\r
503 # define ASN1_TFLG_IMPTAG (0x1 << 3)
\r
505 /* EXPLICIT tagging, inner tag from underlying type */
\r
506 # define ASN1_TFLG_EXPTAG (0x2 << 3)
\r
508 # define ASN1_TFLG_TAG_MASK (0x3 << 3)
\r
510 /* context specific IMPLICIT */
\r
511 # define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
\r
513 /* context specific EXPLICIT */
\r
514 # define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
\r
517 * If tagging is in force these determine the type of tag to use. Otherwise
\r
518 * the tag is determined by the underlying type. These values reflect the
\r
519 * actual octet format.
\r
522 /* Universal tag */
\r
523 # define ASN1_TFLG_UNIVERSAL (0x0<<6)
\r
524 /* Application tag */
\r
525 # define ASN1_TFLG_APPLICATION (0x1<<6)
\r
526 /* Context specific tag */
\r
527 # define ASN1_TFLG_CONTEXT (0x2<<6)
\r
529 # define ASN1_TFLG_PRIVATE (0x3<<6)
\r
531 # define ASN1_TFLG_TAG_CLASS (0x3<<6)
\r
534 * These are for ANY DEFINED BY type. In this case the 'item' field points to
\r
535 * an ASN1_ADB structure which contains a table of values to decode the
\r
539 # define ASN1_TFLG_ADB_MASK (0x3<<8)
\r
541 # define ASN1_TFLG_ADB_OID (0x1<<8)
\r
543 # define ASN1_TFLG_ADB_INT (0x1<<9)
\r
546 * This flag means a parent structure is passed instead of the field: this is
\r
547 * useful is a SEQUENCE is being combined with a CHOICE for example. Since
\r
548 * this means the structure and item name will differ we need to use the
\r
549 * ASN1_CHOICE_END_name() macro for example.
\r
552 # define ASN1_TFLG_COMBINE (0x1<<10)
\r
555 * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes
\r
556 * indefinite length constructed encoding to be used if required.
\r
559 # define ASN1_TFLG_NDEF (0x1<<11)
\r
561 /* This is the actual ASN1 item itself */
\r
563 struct ASN1_ITEM_st {
\r
564 char itype; /* The item type, primitive, SEQUENCE, CHOICE
\r
566 long utype; /* underlying type */
\r
567 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains
\r
569 long tcount; /* Number of templates if SEQUENCE or CHOICE */
\r
570 const void *funcs; /* functions that handle this type */
\r
571 long size; /* Structure size (usually) */
\r
572 # ifndef NO_ASN1_FIELD_NAMES
\r
573 const char *sname; /* Structure name */
\r
578 * These are values for the itype field and
\r
579 * determine how the type is interpreted.
\r
581 * For PRIMITIVE types the underlying type
\r
582 * determines the behaviour if items is NULL.
\r
584 * Otherwise templates must contain a single
\r
585 * template and the type is treated in the
\r
586 * same way as the type specified in the template.
\r
588 * For SEQUENCE types the templates field points
\r
589 * to the members, the size field is the
\r
592 * For CHOICE types the templates field points
\r
593 * to each possible member (typically a union)
\r
594 * and the 'size' field is the offset of the
\r
597 * The 'funcs' field is used for application
\r
598 * specific functions.
\r
600 * For COMPAT types the funcs field gives a
\r
601 * set of functions that handle this type, this
\r
602 * supports the old d2i, i2d convention.
\r
604 * The EXTERN type uses a new style d2i/i2d.
\r
605 * The new style should be used where possible
\r
606 * because it avoids things like the d2i IMPLICIT
\r
609 * MSTRING is a multiple string type, it is used
\r
610 * for a CHOICE of character strings where the
\r
611 * actual strings all occupy an ASN1_STRING
\r
612 * structure. In this case the 'utype' field
\r
613 * has a special meaning, it is used as a mask
\r
614 * of acceptable types using the B_ASN1 constants.
\r
616 * NDEF_SEQUENCE is the same as SEQUENCE except
\r
617 * that it will use indefinite length constructed
\r
618 * encoding if requested.
\r
622 # define ASN1_ITYPE_PRIMITIVE 0x0
\r
624 # define ASN1_ITYPE_SEQUENCE 0x1
\r
626 # define ASN1_ITYPE_CHOICE 0x2
\r
628 # define ASN1_ITYPE_COMPAT 0x3
\r
630 # define ASN1_ITYPE_EXTERN 0x4
\r
632 # define ASN1_ITYPE_MSTRING 0x5
\r
634 # define ASN1_ITYPE_NDEF_SEQUENCE 0x6
\r
637 * Cache for ASN1 tag and length, so we don't keep re-reading it for things
\r
641 struct ASN1_TLC_st {
\r
642 char valid; /* Values below are valid */
\r
643 int ret; /* return value */
\r
644 long plen; /* length */
\r
645 int ptag; /* class value */
\r
646 int pclass; /* class value */
\r
647 int hdrlen; /* header length */
\r
650 /* Typedefs for ASN1 function pointers */
\r
652 typedef ASN1_VALUE *ASN1_new_func(void);
\r
653 typedef void ASN1_free_func(ASN1_VALUE *a);
\r
654 typedef ASN1_VALUE *ASN1_d2i_func(ASN1_VALUE **a, const unsigned char **in,
\r
656 typedef int ASN1_i2d_func(ASN1_VALUE *a, unsigned char **in);
\r
658 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
\r
659 const ASN1_ITEM *it, int tag, int aclass, char opt,
\r
662 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
\r
663 const ASN1_ITEM *it, int tag, int aclass);
\r
664 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
665 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
667 typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
\r
668 int indent, const char *fname,
\r
669 const ASN1_PCTX *pctx);
\r
671 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont,
\r
672 int *putype, const ASN1_ITEM *it);
\r
673 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
\r
674 int len, int utype, char *free_cont,
\r
675 const ASN1_ITEM *it);
\r
676 typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval,
\r
677 const ASN1_ITEM *it, int indent,
\r
678 const ASN1_PCTX *pctx);
\r
680 typedef struct ASN1_COMPAT_FUNCS_st {
\r
681 ASN1_new_func *asn1_new;
\r
682 ASN1_free_func *asn1_free;
\r
683 ASN1_d2i_func *asn1_d2i;
\r
684 ASN1_i2d_func *asn1_i2d;
\r
685 } ASN1_COMPAT_FUNCS;
\r
687 typedef struct ASN1_EXTERN_FUNCS_st {
\r
689 ASN1_ex_new_func *asn1_ex_new;
\r
690 ASN1_ex_free_func *asn1_ex_free;
\r
691 ASN1_ex_free_func *asn1_ex_clear;
\r
692 ASN1_ex_d2i *asn1_ex_d2i;
\r
693 ASN1_ex_i2d *asn1_ex_i2d;
\r
694 ASN1_ex_print_func *asn1_ex_print;
\r
695 } ASN1_EXTERN_FUNCS;
\r
697 typedef struct ASN1_PRIMITIVE_FUNCS_st {
\r
699 unsigned long flags;
\r
700 ASN1_ex_new_func *prim_new;
\r
701 ASN1_ex_free_func *prim_free;
\r
702 ASN1_ex_free_func *prim_clear;
\r
703 ASN1_primitive_c2i *prim_c2i;
\r
704 ASN1_primitive_i2c *prim_i2c;
\r
705 ASN1_primitive_print *prim_print;
\r
706 } ASN1_PRIMITIVE_FUNCS;
\r
709 * This is the ASN1_AUX structure: it handles various miscellaneous
\r
710 * requirements. For example the use of reference counts and an informational
\r
711 * callback. The "informational callback" is called at various points during
\r
712 * the ASN1 encoding and decoding. It can be used to provide minor
\r
713 * customisation of the structures used. This is most useful where the
\r
714 * supplied routines *almost* do the right thing but need some extra help at
\r
715 * a few points. If the callback returns zero then it is assumed a fatal
\r
716 * error has occurred and the main operation should be abandoned. If major
\r
717 * changes in the default behaviour are required then an external type is
\r
718 * more appropriate.
\r
721 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
\r
724 typedef struct ASN1_AUX_st {
\r
727 int ref_offset; /* Offset of reference value */
\r
728 int ref_lock; /* Lock type to use */
\r
729 ASN1_aux_cb *asn1_cb;
\r
730 int enc_offset; /* Offset of ASN1_ENCODING structure */
\r
733 /* For print related callbacks exarg points to this structure */
\r
734 typedef struct ASN1_PRINT_ARG_st {
\r
737 const ASN1_PCTX *pctx;
\r
740 /* For streaming related callbacks exarg points to this structure */
\r
741 typedef struct ASN1_STREAM_ARG_st {
\r
742 /* BIO to stream through */
\r
744 /* BIO with filters appended */
\r
746 /* Streaming I/O boundary */
\r
747 unsigned char **boundary;
\r
750 /* Flags in ASN1_AUX */
\r
752 /* Use a reference count */
\r
753 # define ASN1_AFLG_REFCOUNT 1
\r
754 /* Save the encoding of structure (useful for signatures) */
\r
755 # define ASN1_AFLG_ENCODING 2
\r
756 /* The Sequence length is invalid */
\r
757 # define ASN1_AFLG_BROKEN 4
\r
759 /* operation values for asn1_cb */
\r
761 # define ASN1_OP_NEW_PRE 0
\r
762 # define ASN1_OP_NEW_POST 1
\r
763 # define ASN1_OP_FREE_PRE 2
\r
764 # define ASN1_OP_FREE_POST 3
\r
765 # define ASN1_OP_D2I_PRE 4
\r
766 # define ASN1_OP_D2I_POST 5
\r
767 # define ASN1_OP_I2D_PRE 6
\r
768 # define ASN1_OP_I2D_POST 7
\r
769 # define ASN1_OP_PRINT_PRE 8
\r
770 # define ASN1_OP_PRINT_POST 9
\r
771 # define ASN1_OP_STREAM_PRE 10
\r
772 # define ASN1_OP_STREAM_POST 11
\r
773 # define ASN1_OP_DETACHED_PRE 12
\r
774 # define ASN1_OP_DETACHED_POST 13
\r
776 /* Macro to implement a primitive type */
\r
777 # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
\r
778 # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
\r
779 ASN1_ITEM_start(itname) \
\r
780 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
\r
781 ASN1_ITEM_end(itname)
\r
783 /* Macro to implement a multi string type */
\r
784 # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
\r
785 ASN1_ITEM_start(itname) \
\r
786 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
\r
787 ASN1_ITEM_end(itname)
\r
789 /* Macro to implement an ASN1_ITEM in terms of old style funcs */
\r
791 # define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
\r
793 # define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
\r
794 static const ASN1_COMPAT_FUNCS sname##_ff = { \
\r
795 (ASN1_new_func *)sname##_new, \
\r
796 (ASN1_free_func *)sname##_free, \
\r
797 (ASN1_d2i_func *)d2i_##sname, \
\r
798 (ASN1_i2d_func *)i2d_##sname, \
\r
800 ASN1_ITEM_start(sname) \
\r
801 ASN1_ITYPE_COMPAT, \
\r
808 ASN1_ITEM_end(sname)
\r
810 # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
\r
811 ASN1_ITEM_start(sname) \
\r
812 ASN1_ITYPE_EXTERN, \
\r
819 ASN1_ITEM_end(sname)
\r
821 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
\r
823 # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
\r
825 # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
\r
827 # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
\r
828 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
\r
830 # define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
\r
831 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
\r
833 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
\r
834 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
\r
836 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
\r
837 pre stname *fname##_new(void) \
\r
839 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
\r
841 pre void fname##_free(stname *a) \
\r
843 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
\r
846 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
\r
847 stname *fname##_new(void) \
\r
849 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
\r
851 void fname##_free(stname *a) \
\r
853 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
\r
856 # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
\r
857 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
\r
858 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
\r
860 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
\r
861 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
\r
863 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
\r
865 int i2d_##fname(stname *a, unsigned char **out) \
\r
867 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
\r
870 # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
\r
871 int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
\r
873 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
\r
877 * This includes evil casts to remove const: they will go away when full ASN1
\r
878 * constification is done.
\r
880 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
\r
881 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
\r
883 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
\r
885 int i2d_##fname(const stname *a, unsigned char **out) \
\r
887 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
\r
890 # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
\r
891 stname * stname##_dup(stname *x) \
\r
893 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
\r
896 # define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
\r
897 IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
\r
899 # define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
\r
900 int fname##_print_ctx(BIO *out, stname *x, int indent, \
\r
901 const ASN1_PCTX *pctx) \
\r
903 return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \
\r
904 ASN1_ITEM_rptr(itname), pctx); \
\r
907 # define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
\r
908 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
\r
910 # define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
\r
911 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
\r
912 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
\r
914 /* external definitions for primitive types */
\r
916 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
\r
917 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
\r
918 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
\r
919 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
\r
920 DECLARE_ASN1_ITEM(CBIGNUM)
\r
921 DECLARE_ASN1_ITEM(BIGNUM)
\r
922 DECLARE_ASN1_ITEM(LONG)
\r
923 DECLARE_ASN1_ITEM(ZLONG)
\r
925 DECLARE_STACK_OF(ASN1_VALUE)
\r
927 /* Functions used internally by the ASN1 code */
\r
929 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
930 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
931 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
\r
932 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
934 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
\r
935 int ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
\r
936 const ASN1_TEMPLATE *tt);
\r
937 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
\r
938 const ASN1_ITEM *it, int tag, int aclass, char opt,
\r
941 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
\r
942 const ASN1_ITEM *it, int tag, int aclass);
\r
943 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
\r
944 const ASN1_TEMPLATE *tt);
\r
945 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
947 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
\r
948 const ASN1_ITEM *it);
\r
949 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
\r
950 int utype, char *free_cont, const ASN1_ITEM *it);
\r
952 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
953 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
\r
954 const ASN1_ITEM *it);
\r
956 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
\r
958 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
\r
961 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
\r
963 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
964 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
965 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
\r
966 const ASN1_ITEM *it);
\r
967 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
\r
968 const ASN1_ITEM *it);
\r