Mark msysGit as obsolete
[msysgit.git] / mingw / include / openssl / asn1t.h
blob0a868acebe86189c4d988a4167f232892342cac9
1 /* asn1t.h */
2 /*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4 * 2000.
5 */
6 /* ====================================================================
7 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 #ifndef HEADER_ASN1T_H
60 # define HEADER_ASN1T_H
62 # include <stddef.h>
63 # include <openssl/e_os2.h>
64 # include <openssl/asn1.h>
66 # ifdef OPENSSL_BUILD_SHLIBCRYPTO
67 # undef OPENSSL_EXTERN
68 # define OPENSSL_EXTERN OPENSSL_EXPORT
69 # endif
71 /* ASN1 template defines, structures and functions */
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
77 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
79 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
80 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
82 /* Macros for start and end of ASN1_ITEM definition */
84 # define ASN1_ITEM_start(itname) \
85 OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
87 # define ASN1_ITEM_end(itname) \
90 # else
92 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
93 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
95 /* Macros for start and end of ASN1_ITEM definition */
97 # define ASN1_ITEM_start(itname) \
98 const ASN1_ITEM * itname##_it(void) \
99 { \
100 static const ASN1_ITEM local_it = {
102 # define ASN1_ITEM_end(itname) \
103 }; \
104 return &local_it; \
107 # endif
109 /* Macros to aid ASN1 template writing */
111 # define ASN1_ITEM_TEMPLATE(tname) \
112 static const ASN1_TEMPLATE tname##_item_tt
114 # define ASN1_ITEM_TEMPLATE_END(tname) \
116 ASN1_ITEM_start(tname) \
117 ASN1_ITYPE_PRIMITIVE,\
118 -1,\
119 &tname##_item_tt,\
121 NULL,\
123 #tname \
124 ASN1_ITEM_end(tname)
126 /* This is a ASN1 type which just embeds a template */
129 * This pair helps declare a SEQUENCE. We can do:
131 * ASN1_SEQUENCE(stname) = {
132 * ... SEQUENCE components ...
133 * } ASN1_SEQUENCE_END(stname)
135 * This will produce an ASN1_ITEM called stname_it
136 * for a structure called stname.
138 * If you want the same structure but a different
139 * name then use:
141 * ASN1_SEQUENCE(itname) = {
142 * ... SEQUENCE components ...
143 * } ASN1_SEQUENCE_END_name(stname, itname)
145 * This will create an item called itname_it using
146 * a structure called stname.
149 # define ASN1_SEQUENCE(tname) \
150 static const ASN1_TEMPLATE tname##_seq_tt[]
152 # define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
154 # define ASN1_SEQUENCE_END_name(stname, tname) \
156 ASN1_ITEM_start(tname) \
157 ASN1_ITYPE_SEQUENCE,\
158 V_ASN1_SEQUENCE,\
159 tname##_seq_tt,\
160 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
161 NULL,\
162 sizeof(stname),\
163 #stname \
164 ASN1_ITEM_end(tname)
166 # define ASN1_NDEF_SEQUENCE(tname) \
167 ASN1_SEQUENCE(tname)
169 # define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
170 ASN1_SEQUENCE_cb(tname, cb)
172 # define ASN1_SEQUENCE_cb(tname, cb) \
173 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
174 ASN1_SEQUENCE(tname)
176 # define ASN1_BROKEN_SEQUENCE(tname) \
177 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
178 ASN1_SEQUENCE(tname)
180 # define ASN1_SEQUENCE_ref(tname, cb, lck) \
181 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
182 ASN1_SEQUENCE(tname)
184 # define ASN1_SEQUENCE_enc(tname, enc, cb) \
185 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
186 ASN1_SEQUENCE(tname)
188 # define ASN1_NDEF_SEQUENCE_END(tname) \
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)
200 # define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
202 # define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
204 # define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
206 # define ASN1_SEQUENCE_END_ref(stname, tname) \
208 ASN1_ITEM_start(tname) \
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 \
216 ASN1_ITEM_end(tname)
219 * This pair helps declare a CHOICE type. We can do:
221 * ASN1_CHOICE(chname) = {
222 * ... CHOICE options ...
223 * ASN1_CHOICE_END(chname)
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;
236 * the name of the selector must be 'type'.
237 * to use an alternative selector name use the
238 * ASN1_CHOICE_END_selector() version.
241 # define ASN1_CHOICE(tname) \
242 static const ASN1_TEMPLATE tname##_ch_tt[]
244 # define ASN1_CHOICE_cb(tname, cb) \
245 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
246 ASN1_CHOICE(tname)
248 # define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
250 # define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
252 # define ASN1_CHOICE_END_selector(stname, tname, selname) \
254 ASN1_ITEM_start(tname) \
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 \
262 ASN1_ITEM_end(tname)
264 # define ASN1_CHOICE_END_cb(stname, tname, selname) \
266 ASN1_ITEM_start(tname) \
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 \
274 ASN1_ITEM_end(tname)
276 /* This helps with the template wrapper form of ASN1_ITEM */
278 # define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
279 (flags), (tag), 0,\
280 #name, ASN1_ITEM_ref(type) }
282 /* These help with SEQUENCE or CHOICE components */
284 /* used to declare other types */
286 # define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
287 (flags), (tag), offsetof(stname, field),\
288 #field, ASN1_ITEM_ref(type) }
290 /* used when the structure is combined with the parent */
292 # define ASN1_EX_COMBINE(flags, tag, type) { \
293 (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
295 /* implicit and explicit helper macros */
297 # define ASN1_IMP_EX(stname, field, type, tag, ex) \
298 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
300 # define ASN1_EXP_EX(stname, field, type, tag, ex) \
301 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
303 /* Any defined by macros: the field used is in the table itself */
305 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
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
312 /* Plain simple type */
313 # define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
315 /* OPTIONAL simple type */
316 # define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
318 /* IMPLICIT tagged simple type */
319 # define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
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)
324 /* Same as above but EXPLICIT */
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)
329 /* SEQUENCE OF type */
330 # define ASN1_SEQUENCE_OF(stname, field, type) \
331 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
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)
337 /* Same as above but for SET OF */
339 # define ASN1_SET_OF(stname, field, type) \
340 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
342 # define ASN1_SET_OF_OPT(stname, field, type) \
343 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
345 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
347 # define ASN1_IMP_SET_OF(stname, field, type, tag) \
348 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
350 # define ASN1_EXP_SET_OF(stname, field, type, tag) \
351 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
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)
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)
359 # define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
360 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
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)
365 # define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
366 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
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)
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)
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)
379 /* Macros for the ASN1_ADB structure */
381 # define ASN1_ADB(name) \
382 static const ASN1_ADB_TABLE name##_adbtbl[]
384 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
386 # define ASN1_ADB_END(name, flags, field, app_table, def, none) \
388 static const ASN1_ADB name##_adb = {\
389 flags,\
390 offsetof(name, field),\
391 app_table,\
392 name##_adbtbl,\
393 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
394 def,\
395 none\
398 # else
400 # define ASN1_ADB_END(name, flags, field, app_table, def, none) \
402 static const ASN1_ITEM *name##_adb(void) \
404 static const ASN1_ADB internal_adb = \
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; \
416 void dummy_function(void)
418 # endif
420 # define ADB_ENTRY(val, template) {val, template}
422 # define ASN1_ADB_TEMPLATE(name) \
423 static const ASN1_TEMPLATE name##_tt
426 * This is the ASN1 template structure that defines a wrapper round the
427 * actual type. It determines the actual position of the field in the value
428 * structure, various flags such as OPTIONAL and the field name.
431 struct ASN1_TEMPLATE_st {
432 unsigned long flags; /* Various flags */
433 long tag; /* tag, not used if no tagging */
434 unsigned long offset; /* Offset of this field in structure */
435 # ifndef NO_ASN1_FIELD_NAMES
436 const char *field_name; /* Field name */
437 # endif
438 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
441 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
443 # define ASN1_TEMPLATE_item(t) (t->item_ptr)
444 # define ASN1_TEMPLATE_adb(t) (t->item_ptr)
446 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
447 typedef struct ASN1_ADB_st ASN1_ADB;
449 struct 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 */
459 struct 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 */
464 /* template flags */
466 /* Field is optional */
467 # define ASN1_TFLG_OPTIONAL (0x1)
469 /* Field is a SET OF */
470 # define ASN1_TFLG_SET_OF (0x1 << 1)
472 /* Field is a SEQUENCE OF */
473 # define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
476 * Special case: this refers to a SET OF that will be sorted into DER order
477 * when encoded *and* the corresponding STACK will be modified to match the
478 * new order.
480 # define ASN1_TFLG_SET_ORDER (0x3 << 1)
482 /* Mask for SET OF or SEQUENCE OF */
483 # define ASN1_TFLG_SK_MASK (0x3 << 1)
486 * These flags mean the tag should be taken from the tag field. If EXPLICIT
487 * then the underlying type is used for the inner tag.
490 /* IMPLICIT tagging */
491 # define ASN1_TFLG_IMPTAG (0x1 << 3)
493 /* EXPLICIT tagging, inner tag from underlying type */
494 # define ASN1_TFLG_EXPTAG (0x2 << 3)
496 # define ASN1_TFLG_TAG_MASK (0x3 << 3)
498 /* context specific IMPLICIT */
499 # define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
501 /* context specific EXPLICIT */
502 # define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
505 * If tagging is in force these determine the type of tag to use. Otherwise
506 * the tag is determined by the underlying type. These values reflect the
507 * actual octet format.
510 /* Universal tag */
511 # define ASN1_TFLG_UNIVERSAL (0x0<<6)
512 /* Application tag */
513 # define ASN1_TFLG_APPLICATION (0x1<<6)
514 /* Context specific tag */
515 # define ASN1_TFLG_CONTEXT (0x2<<6)
516 /* Private tag */
517 # define ASN1_TFLG_PRIVATE (0x3<<6)
519 # define ASN1_TFLG_TAG_CLASS (0x3<<6)
522 * These are for ANY DEFINED BY type. In this case the 'item' field points to
523 * an ASN1_ADB structure which contains a table of values to decode the
524 * relevant type
527 # define ASN1_TFLG_ADB_MASK (0x3<<8)
529 # define ASN1_TFLG_ADB_OID (0x1<<8)
531 # define ASN1_TFLG_ADB_INT (0x1<<9)
534 * This flag means a parent structure is passed instead of the field: this is
535 * useful is a SEQUENCE is being combined with a CHOICE for example. Since
536 * this means the structure and item name will differ we need to use the
537 * ASN1_CHOICE_END_name() macro for example.
540 # define ASN1_TFLG_COMBINE (0x1<<10)
543 * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes
544 * indefinite length constructed encoding to be used if required.
547 # define ASN1_TFLG_NDEF (0x1<<11)
549 /* This is the actual ASN1 item itself */
551 struct ASN1_ITEM_st {
552 char itype; /* The item type, primitive, SEQUENCE, CHOICE
553 * or extern */
554 long utype; /* underlying type */
555 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains
556 * the contents */
557 long tcount; /* Number of templates if SEQUENCE or CHOICE */
558 const void *funcs; /* functions that handle this type */
559 long size; /* Structure size (usually) */
560 # ifndef NO_ASN1_FIELD_NAMES
561 const char *sname; /* Structure name */
562 # endif
566 * These are values for the itype field and
567 * determine how the type is interpreted.
569 * For PRIMITIVE types the underlying type
570 * determines the behaviour if items is NULL.
572 * Otherwise templates must contain a single
573 * template and the type is treated in the
574 * same way as the type specified in the template.
576 * For SEQUENCE types the templates field points
577 * to the members, the size field is the
578 * structure size.
580 * For CHOICE types the templates field points
581 * to each possible member (typically a union)
582 * and the 'size' field is the offset of the
583 * selector.
585 * The 'funcs' field is used for application
586 * specific functions.
588 * For COMPAT types the funcs field gives a
589 * set of functions that handle this type, this
590 * supports the old d2i, i2d convention.
592 * The EXTERN type uses a new style d2i/i2d.
593 * The new style should be used where possible
594 * because it avoids things like the d2i IMPLICIT
595 * hack.
597 * MSTRING is a multiple string type, it is used
598 * for a CHOICE of character strings where the
599 * actual strings all occupy an ASN1_STRING
600 * structure. In this case the 'utype' field
601 * has a special meaning, it is used as a mask
602 * of acceptable types using the B_ASN1 constants.
604 * NDEF_SEQUENCE is the same as SEQUENCE except
605 * that it will use indefinite length constructed
606 * encoding if requested.
610 # define ASN1_ITYPE_PRIMITIVE 0x0
612 # define ASN1_ITYPE_SEQUENCE 0x1
614 # define ASN1_ITYPE_CHOICE 0x2
616 # define ASN1_ITYPE_COMPAT 0x3
618 # define ASN1_ITYPE_EXTERN 0x4
620 # define ASN1_ITYPE_MSTRING 0x5
622 # define ASN1_ITYPE_NDEF_SEQUENCE 0x6
625 * Cache for ASN1 tag and length, so we don't keep re-reading it for things
626 * like CHOICE
629 struct ASN1_TLC_st {
630 char valid; /* Values below are valid */
631 int ret; /* return value */
632 long plen; /* length */
633 int ptag; /* class value */
634 int pclass; /* class value */
635 int hdrlen; /* header length */
638 /* Typedefs for ASN1 function pointers */
640 typedef ASN1_VALUE *ASN1_new_func(void);
641 typedef void ASN1_free_func(ASN1_VALUE *a);
642 typedef ASN1_VALUE *ASN1_d2i_func(ASN1_VALUE **a, const unsigned char **in,
643 long length);
644 typedef int ASN1_i2d_func(ASN1_VALUE *a, unsigned char **in);
646 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
647 const ASN1_ITEM *it, int tag, int aclass, char opt,
648 ASN1_TLC *ctx);
650 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
651 const ASN1_ITEM *it, int tag, int aclass);
652 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
653 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
655 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont,
656 int *putype, const ASN1_ITEM *it);
657 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
658 int len, int utype, char *free_cont,
659 const ASN1_ITEM *it);
661 typedef struct ASN1_COMPAT_FUNCS_st {
662 ASN1_new_func *asn1_new;
663 ASN1_free_func *asn1_free;
664 ASN1_d2i_func *asn1_d2i;
665 ASN1_i2d_func *asn1_i2d;
666 } ASN1_COMPAT_FUNCS;
668 typedef struct ASN1_EXTERN_FUNCS_st {
669 void *app_data;
670 ASN1_ex_new_func *asn1_ex_new;
671 ASN1_ex_free_func *asn1_ex_free;
672 ASN1_ex_free_func *asn1_ex_clear;
673 ASN1_ex_d2i *asn1_ex_d2i;
674 ASN1_ex_i2d *asn1_ex_i2d;
675 } ASN1_EXTERN_FUNCS;
677 typedef struct ASN1_PRIMITIVE_FUNCS_st {
678 void *app_data;
679 unsigned long flags;
680 ASN1_ex_new_func *prim_new;
681 ASN1_ex_free_func *prim_free;
682 ASN1_ex_free_func *prim_clear;
683 ASN1_primitive_c2i *prim_c2i;
684 ASN1_primitive_i2c *prim_i2c;
685 } ASN1_PRIMITIVE_FUNCS;
688 * This is the ASN1_AUX structure: it handles various miscellaneous
689 * requirements. For example the use of reference counts and an informational
690 * callback. The "informational callback" is called at various points during
691 * the ASN1 encoding and decoding. It can be used to provide minor
692 * customisation of the structures used. This is most useful where the
693 * supplied routines *almost* do the right thing but need some extra help at
694 * a few points. If the callback returns zero then it is assumed a fatal
695 * error has occurred and the main operation should be abandoned. If major
696 * changes in the default behaviour are required then an external type is
697 * more appropriate.
700 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it);
702 typedef struct ASN1_AUX_st {
703 void *app_data;
704 int flags;
705 int ref_offset; /* Offset of reference value */
706 int ref_lock; /* Lock type to use */
707 ASN1_aux_cb *asn1_cb;
708 int enc_offset; /* Offset of ASN1_ENCODING structure */
709 } ASN1_AUX;
711 /* Flags in ASN1_AUX */
713 /* Use a reference count */
714 # define ASN1_AFLG_REFCOUNT 1
715 /* Save the encoding of structure (useful for signatures) */
716 # define ASN1_AFLG_ENCODING 2
717 /* The Sequence length is invalid */
718 # define ASN1_AFLG_BROKEN 4
720 /* operation values for asn1_cb */
722 # define ASN1_OP_NEW_PRE 0
723 # define ASN1_OP_NEW_POST 1
724 # define ASN1_OP_FREE_PRE 2
725 # define ASN1_OP_FREE_POST 3
726 # define ASN1_OP_D2I_PRE 4
727 # define ASN1_OP_D2I_POST 5
728 # define ASN1_OP_I2D_PRE 6
729 # define ASN1_OP_I2D_POST 7
731 /* Macro to implement a primitive type */
732 # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
733 # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
734 ASN1_ITEM_start(itname) \
735 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
736 ASN1_ITEM_end(itname)
738 /* Macro to implement a multi string type */
739 # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
740 ASN1_ITEM_start(itname) \
741 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
742 ASN1_ITEM_end(itname)
744 /* Macro to implement an ASN1_ITEM in terms of old style funcs */
746 # define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
748 # define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
749 static const ASN1_COMPAT_FUNCS sname##_ff = { \
750 (ASN1_new_func *)sname##_new, \
751 (ASN1_free_func *)sname##_free, \
752 (ASN1_d2i_func *)d2i_##sname, \
753 (ASN1_i2d_func *)i2d_##sname, \
754 }; \
755 ASN1_ITEM_start(sname) \
756 ASN1_ITYPE_COMPAT, \
757 tag, \
758 NULL, \
759 0, \
760 &sname##_ff, \
761 0, \
762 #sname \
763 ASN1_ITEM_end(sname)
765 # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
766 ASN1_ITEM_start(sname) \
767 ASN1_ITYPE_EXTERN, \
768 tag, \
769 NULL, \
770 0, \
771 &fptrs, \
772 0, \
773 #sname \
774 ASN1_ITEM_end(sname)
776 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
778 # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
780 # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
782 # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
783 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
785 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
786 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
788 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
789 stname *fname##_new(void) \
791 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
793 void fname##_free(stname *a) \
795 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
798 # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
799 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
800 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
802 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
803 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
805 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
807 int i2d_##fname(stname *a, unsigned char **out) \
809 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
812 # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
813 int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
815 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
819 * This includes evil casts to remove const: they will go away when full ASN1
820 * constification is done.
822 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
823 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
825 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
827 int i2d_##fname(const stname *a, unsigned char **out) \
829 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
832 # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
833 stname * stname##_dup(stname *x) \
835 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
838 # define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
839 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
841 # define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
842 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
843 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
845 /* external definitions for primitive types */
847 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
848 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
849 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
850 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
851 DECLARE_ASN1_ITEM(CBIGNUM)
852 DECLARE_ASN1_ITEM(BIGNUM)
853 DECLARE_ASN1_ITEM(LONG)
854 DECLARE_ASN1_ITEM(ZLONG)
856 DECLARE_STACK_OF(ASN1_VALUE)
858 /* Functions used internally by the ASN1 code */
860 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
861 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
862 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
863 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
865 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
866 int ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
867 const ASN1_TEMPLATE *tt);
868 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
869 const ASN1_ITEM *it, int tag, int aclass, char opt,
870 ASN1_TLC *ctx);
872 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
873 const ASN1_ITEM *it, int tag, int aclass);
874 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
875 const ASN1_TEMPLATE *tt);
876 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
878 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
879 const ASN1_ITEM *it);
880 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
881 int utype, char *free_cont, const ASN1_ITEM *it);
883 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
884 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
885 const ASN1_ITEM *it);
887 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
889 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
890 int nullerr);
892 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
894 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
895 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
896 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
897 const ASN1_ITEM *it);
898 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
899 const ASN1_ITEM *it);
901 #ifdef __cplusplus
903 #endif
904 #endif