grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / devs / diskimage / include / amigaos / openssl / asn1t.h
blob6f5efd602fedc2659e7995749376880fc6b5ba5b
1 /* asn1t.h */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3 * project 2000.
4 */
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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/)"
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.
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.
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/)"
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 * ====================================================================
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).
58 #ifndef PROTO_AMISSL_H
59 #include <proto/amissl.h>
60 #endif /* PROTO_AMISSL_H */
62 #ifndef HEADER_ASN1T_H
63 #define HEADER_ASN1T_H
65 #include <stddef.h>
66 #include <openssl/e_os2.h>
67 #include <openssl/asn1.h>
69 #ifdef OPENSSL_BUILD_SHLIBCRYPTO
70 # undef OPENSSL_EXTERN
71 # define OPENSSL_EXTERN OPENSSL_EXPORT
72 #endif
74 /* ASN1 template defines, structures and functions */
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
81 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
83 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
84 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
87 /* Macros for start and end of ASN1_ITEM definition */
89 #ifndef AMISSL
90 #define ASN1_ITEM_start(itname) \
91 OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
92 #else /* !AMISSL */
93 #error Logic error
94 #endif /* !AMISSL */
96 #define ASN1_ITEM_end(itname) \
99 #else
101 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
102 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
105 /* Macros for start and end of ASN1_ITEM definition */
107 #ifndef AMISSL
108 #define ASN1_ITEM_start(itname) \
109 const ASN1_ITEM * itname##_it(void) \
111 static const ASN1_ITEM local_it = { \
113 #else /* !AMISSL */
114 #define ASN1_ITEM_start(itname) \
115 const ASN1_ITEM * itname##_it(void) \
117 static const ASN1_ITEM local_it = {{0, 0, 0} /* amissl_pad */, \
119 #endif /* !AMISSL */
121 #define ASN1_ITEM_end(itname) \
122 }; \
123 return &local_it; \
126 #endif
129 /* Macros to aid ASN1 template writing */
131 #define ASN1_ITEM_TEMPLATE(tname) \
132 const static ASN1_TEMPLATE tname##_item_tt
134 #define ASN1_ITEM_TEMPLATE_END(tname) \
136 ASN1_ITEM_start(tname) \
137 ASN1_ITYPE_PRIMITIVE,\
138 -1,\
139 &tname##_item_tt,\
141 NULL,\
143 #tname \
144 ASN1_ITEM_end(tname)
147 /* This is a ASN1 type which just embeds a template */
149 /* This pair helps declare a SEQUENCE. We can do:
151 * ASN1_SEQUENCE(stname) = {
152 * ... SEQUENCE components ...
153 * } ASN1_SEQUENCE_END(stname)
155 * This will produce an ASN1_ITEM called stname_it
156 * for a structure called stname.
158 * If you want the same structure but a different
159 * name then use:
161 * ASN1_SEQUENCE(itname) = {
162 * ... SEQUENCE components ...
163 * } ASN1_SEQUENCE_END_name(stname, itname)
165 * This will create an item called itname_it using
166 * a structure called stname.
169 #define ASN1_SEQUENCE(tname) \
170 const static ASN1_TEMPLATE tname##_seq_tt[]
172 #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
174 #define ASN1_SEQUENCE_END_name(stname, tname) \
176 ASN1_ITEM_start(tname) \
177 ASN1_ITYPE_SEQUENCE,\
178 V_ASN1_SEQUENCE,\
179 tname##_seq_tt,\
180 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
181 NULL,\
182 sizeof(stname),\
183 #stname \
184 ASN1_ITEM_end(tname)
186 #define ASN1_SEQUENCE_cb(tname, cb) \
187 const static ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
188 ASN1_SEQUENCE(tname)
190 #define ASN1_BROKEN_SEQUENCE(tname) \
191 const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
192 ASN1_SEQUENCE(tname)
194 #define ASN1_SEQUENCE_ref(tname, cb, lck) \
195 const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
196 ASN1_SEQUENCE(tname)
198 #define ASN1_SEQUENCE_enc(tname, enc, cb) \
199 const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
200 ASN1_SEQUENCE(tname)
202 #define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
204 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
206 #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
208 #define ASN1_SEQUENCE_END_ref(stname, tname) \
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)
221 /* This pair helps declare a CHOICE type. We can do:
223 * ASN1_CHOICE(chname) = {
224 * ... CHOICE options ...
225 * ASN1_CHOICE_END(chname)
227 * This will produce an ASN1_ITEM called chname_it
228 * for a structure called chname. The structure
229 * definition must look like this:
230 * typedef struct {
231 * int type;
232 * union {
233 * ASN1_SOMETHING *opt1;
234 * ASN1_SOMEOTHER *opt2;
235 * } value;
236 * } chname;
238 * the name of the selector must be 'type'.
239 * to use an alternative selector name use the
240 * ASN1_CHOICE_END_selector() version.
243 #define ASN1_CHOICE(tname) \
244 const static ASN1_TEMPLATE tname##_ch_tt[]
246 #define ASN1_CHOICE_cb(tname, cb) \
247 const static ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
248 ASN1_CHOICE(tname)
250 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
252 #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
254 #define ASN1_CHOICE_END_selector(stname, tname, selname) \
256 ASN1_ITEM_start(tname) \
257 ASN1_ITYPE_CHOICE,\
258 offsetof(stname,selname) ,\
259 tname##_ch_tt,\
260 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
261 NULL,\
262 sizeof(stname),\
263 #stname \
264 ASN1_ITEM_end(tname)
266 #define ASN1_CHOICE_END_cb(stname, tname, selname) \
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 &tname##_aux,\
274 sizeof(stname),\
275 #stname \
276 ASN1_ITEM_end(tname)
278 /* This helps with the template wrapper form of ASN1_ITEM */
280 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
281 (flags), (tag), 0,\
282 #name, ASN1_ITEM_ref(type) }
284 /* These help with SEQUENCE or CHOICE components */
286 /* used to declare other types */
288 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
289 (flags), (tag), offsetof(stname, field),\
290 #field, ASN1_ITEM_ref(type) }
292 /* used when the structure is combined with the parent */
294 #define ASN1_EX_COMBINE(flags, tag, type) { \
295 (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
297 /* implicit and explicit helper macros */
299 #define ASN1_IMP_EX(stname, field, type, tag, ex) \
300 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
302 #define ASN1_EXP_EX(stname, field, type, tag, ex) \
303 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
305 /* Any defined by macros: the field used is in the table itself */
307 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
308 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
309 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
310 #else
311 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
312 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
313 #endif
314 /* Plain simple type */
315 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
317 /* OPTIONAL simple type */
318 #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
320 /* IMPLICIT tagged simple type */
321 #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
323 /* IMPLICIT tagged OPTIONAL simple type */
324 #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
326 /* Same as above but EXPLICIT */
328 #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
329 #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
331 /* SEQUENCE OF type */
332 #define ASN1_SEQUENCE_OF(stname, field, type) \
333 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
335 /* OPTIONAL SEQUENCE OF */
336 #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
337 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
339 /* Same as above but for SET OF */
341 #define ASN1_SET_OF(stname, field, type) \
342 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
344 #define ASN1_SET_OF_OPT(stname, field, type) \
345 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
347 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
349 #define ASN1_IMP_SET_OF(stname, field, type, tag) \
350 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
352 #define ASN1_EXP_SET_OF(stname, field, type, tag) \
353 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
355 #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
356 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
358 #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
359 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
361 #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
362 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
364 #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
365 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
367 #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
368 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
370 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
371 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
373 /* Macros for the ASN1_ADB structure */
375 #define ASN1_ADB(name) \
376 const static ASN1_ADB_TABLE name##_adbtbl[]
378 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
380 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
382 const static ASN1_ADB name##_adb = {\
383 flags,\
384 offsetof(name, field),\
385 app_table,\
386 name##_adbtbl,\
387 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
388 def,\
389 none\
392 #else
394 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
396 const static ASN1_ITEM *name##_adb(void) \
398 const static ASN1_ADB internal_adb = \
400 flags,\
401 offsetof(name, field),\
402 app_table,\
403 name##_adbtbl,\
404 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
405 def,\
406 none\
407 }; \
408 return (const ASN1_ITEM *) &internal_adb; \
410 void dummy_function(void)
412 #endif
414 #define ADB_ENTRY(val, template) {val, template}
416 #define ASN1_ADB_TEMPLATE(name) \
417 const static ASN1_TEMPLATE name##_tt
419 /* This is the ASN1 template structure that defines
420 * a wrapper round the actual type. It determines the
421 * actual position of the field in the value structure,
422 * various flags such as OPTIONAL and the field name.
425 struct ASN1_TEMPLATE_st {
426 unsigned long flags; /* Various flags */
427 long tag; /* tag, not used if no tagging */
428 unsigned long offset; /* Offset of this field in structure */
429 #ifndef NO_ASN1_FIELD_NAMES
430 char *field_name; /* Field name */
431 #endif
432 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
435 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
437 #define ASN1_TEMPLATE_item(t) (t->item_ptr)
438 #define ASN1_TEMPLATE_adb(t) (t->item_ptr)
440 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
441 typedef struct ASN1_ADB_st ASN1_ADB;
443 struct ASN1_ADB_st {
444 unsigned long flags; /* Various flags */
445 unsigned long offset; /* Offset of selector field */
446 STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
447 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
448 long tblcount; /* Number of entries in tbl */
449 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
450 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
453 struct ASN1_ADB_TABLE_st {
454 long value; /* NID for an object or value for an int */
455 const ASN1_TEMPLATE tt; /* item for this value */
458 /* template flags */
460 /* Field is optional */
461 #define ASN1_TFLG_OPTIONAL (0x1)
463 /* Field is a SET OF */
464 #define ASN1_TFLG_SET_OF (0x1 << 1)
466 /* Field is a SEQUENCE OF */
467 #define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
469 /* Special case: this refers to a SET OF that
470 * will be sorted into DER order when encoded *and*
471 * the corresponding STACK will be modified to match
472 * the new order.
474 #define ASN1_TFLG_SET_ORDER (0x3 << 1)
476 /* Mask for SET OF or SEQUENCE OF */
477 #define ASN1_TFLG_SK_MASK (0x3 << 1)
479 /* These flags mean the tag should be taken from the
480 * tag field. If EXPLICIT then the underlying type
481 * is used for the inner tag.
484 /* IMPLICIT tagging */
485 #define ASN1_TFLG_IMPTAG (0x1 << 3)
488 /* EXPLICIT tagging, inner tag from underlying type */
489 #define ASN1_TFLG_EXPTAG (0x2 << 3)
491 #define ASN1_TFLG_TAG_MASK (0x3 << 3)
493 /* context specific IMPLICIT */
494 #define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
496 /* context specific EXPLICIT */
497 #define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
499 /* If tagging is in force these determine the
500 * type of tag to use. Otherwise the tag is
501 * determined by the underlying type. These
502 * values reflect the actual octet format.
505 /* Universal tag */
506 #define ASN1_TFLG_UNIVERSAL (0x0<<6)
507 /* Application tag */
508 #define ASN1_TFLG_APPLICATION (0x1<<6)
509 /* Context specific tag */
510 #define ASN1_TFLG_CONTEXT (0x2<<6)
511 /* Private tag */
512 #define ASN1_TFLG_PRIVATE (0x3<<6)
514 #define ASN1_TFLG_TAG_CLASS (0x3<<6)
516 /* These are for ANY DEFINED BY type. In this case
517 * the 'item' field points to an ASN1_ADB structure
518 * which contains a table of values to decode the
519 * relevant type
522 #define ASN1_TFLG_ADB_MASK (0x3<<8)
524 #define ASN1_TFLG_ADB_OID (0x1<<8)
526 #define ASN1_TFLG_ADB_INT (0x1<<9)
528 /* This flag means a parent structure is passed
529 * instead of the field: this is useful is a
530 * SEQUENCE is being combined with a CHOICE for
531 * example. Since this means the structure and
532 * item name will differ we need to use the
533 * ASN1_CHOICE_END_name() macro for example.
536 #define ASN1_TFLG_COMBINE (0x1<<10)
538 /* This is the actual ASN1 item itself */
540 struct ASN1_ITEM_st {
541 #ifdef AMISSL
542 char amissl_pad[3];
543 #endif /* AMISSL */
544 char itype; /* The item type, primitive, SEQUENCE, CHOICE or extern */
545 long utype; /* underlying type */
546 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */
547 long tcount; /* Number of templates if SEQUENCE or CHOICE */
548 const void *funcs; /* functions that handle this type */
549 long size; /* Structure size (usually)*/
550 #ifndef NO_ASN1_FIELD_NAMES
551 const char *sname; /* Structure name */
552 #endif
555 /* These are values for the itype field and
556 * determine how the type is interpreted.
558 * For PRIMITIVE types the underlying type
559 * determines the behaviour if items is NULL.
561 * Otherwise templates must contain a single
562 * template and the type is treated in the
563 * same way as the type specified in the template.
565 * For SEQUENCE types the templates field points
566 * to the members, the size field is the
567 * structure size.
569 * For CHOICE types the templates field points
570 * to each possible member (typically a union)
571 * and the 'size' field is the offset of the
572 * selector.
574 * The 'funcs' field is used for application
575 * specific functions.
577 * For COMPAT types the funcs field gives a
578 * set of functions that handle this type, this
579 * supports the old d2i, i2d convention.
581 * The EXTERN type uses a new style d2i/i2d.
582 * The new style should be used where possible
583 * because it avoids things like the d2i IMPLICIT
584 * hack.
586 * MSTRING is a multiple string type, it is used
587 * for a CHOICE of character strings where the
588 * actual strings all occupy an ASN1_STRING
589 * structure. In this case the 'utype' field
590 * has a special meaning, it is used as a mask
591 * of acceptable types using the B_ASN1 constants.
595 #define ASN1_ITYPE_PRIMITIVE 0x0
597 #define ASN1_ITYPE_SEQUENCE 0x1
599 #define ASN1_ITYPE_CHOICE 0x2
601 #define ASN1_ITYPE_COMPAT 0x3
603 #define ASN1_ITYPE_EXTERN 0x4
605 #define ASN1_ITYPE_MSTRING 0x5
607 /* Cache for ASN1 tag and length, so we
608 * don't keep re-reading it for things
609 * like CHOICE
612 struct ASN1_TLC_st{
613 char valid; /* Values below are valid */
614 #ifdef AMISSL
615 char amissl_pad[3];
616 #endif /* AMISSL */
617 int ret; /* return value */
618 long plen; /* length */
619 int ptag; /* class value */
620 int pclass; /* class value */
621 int hdrlen; /* header length */
624 /* Typedefs for ASN1 function pointers */
626 typedef ASN1_VALUE * ASN1_new_func(void);
627 typedef void ASN1_free_func(ASN1_VALUE *a);
628 typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, unsigned char ** in, long length);
629 typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
631 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it,
632 int tag, int aclass, char opt, ASN1_TLC *ctx);
634 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
635 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
636 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
638 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
639 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
641 typedef struct ASN1_COMPAT_FUNCS_st {
642 ASN1_new_func *asn1_new;
643 ASN1_free_func *asn1_free;
644 ASN1_d2i_func *asn1_d2i;
645 ASN1_i2d_func *asn1_i2d;
646 } ASN1_COMPAT_FUNCS;
648 typedef struct ASN1_EXTERN_FUNCS_st {
649 void *app_data;
650 ASN1_ex_new_func *asn1_ex_new;
651 ASN1_ex_free_func *asn1_ex_free;
652 ASN1_ex_free_func *asn1_ex_clear;
653 ASN1_ex_d2i *asn1_ex_d2i;
654 ASN1_ex_i2d *asn1_ex_i2d;
655 } ASN1_EXTERN_FUNCS;
657 typedef struct ASN1_PRIMITIVE_FUNCS_st {
658 void *app_data;
659 unsigned long flags;
660 ASN1_ex_new_func *prim_new;
661 ASN1_ex_free_func *prim_free;
662 ASN1_ex_free_func *prim_clear;
663 ASN1_primitive_c2i *prim_c2i;
664 ASN1_primitive_i2c *prim_i2c;
665 } ASN1_PRIMITIVE_FUNCS;
667 /* This is the ASN1_AUX structure: it handles various
668 * miscellaneous requirements. For example the use of
669 * reference counts and an informational callback.
671 * The "informational callback" is called at various
672 * points during the ASN1 encoding and decoding. It can
673 * be used to provide minor customisation of the structures
674 * used. This is most useful where the supplied routines
675 * *almost* do the right thing but need some extra help
676 * at a few points. If the callback returns zero then
677 * it is assumed a fatal error has occurred and the
678 * main operation should be abandoned.
680 * If major changes in the default behaviour are required
681 * then an external type is more appropriate.
684 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it);
686 typedef struct ASN1_AUX_st {
687 void *app_data;
688 int flags;
689 int ref_offset; /* Offset of reference value */
690 int ref_lock; /* Lock type to use */
691 ASN1_aux_cb *asn1_cb;
692 int enc_offset; /* Offset of ASN1_ENCODING structure */
693 } ASN1_AUX;
695 /* Flags in ASN1_AUX */
697 /* Use a reference count */
698 #define ASN1_AFLG_REFCOUNT 1
699 /* Save the encoding of structure (useful for signatures) */
700 #define ASN1_AFLG_ENCODING 2
701 /* The Sequence length is invalid */
702 #define ASN1_AFLG_BROKEN 4
704 /* operation values for asn1_cb */
706 #define ASN1_OP_NEW_PRE 0
707 #define ASN1_OP_NEW_POST 1
708 #define ASN1_OP_FREE_PRE 2
709 #define ASN1_OP_FREE_POST 3
710 #define ASN1_OP_D2I_PRE 4
711 #define ASN1_OP_D2I_POST 5
712 #define ASN1_OP_I2D_PRE 6
713 #define ASN1_OP_I2D_POST 7
715 /* Macro to implement a primitive type */
716 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
717 #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
718 ASN1_ITEM_start(itname) \
719 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
720 ASN1_ITEM_end(itname)
722 /* Macro to implement a multi string type */
723 #define IMPLEMENT_ASN1_MSTRING(itname, mask) \
724 ASN1_ITEM_start(itname) \
725 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
726 ASN1_ITEM_end(itname)
728 /* Macro to implement an ASN1_ITEM in terms of old style funcs */
730 #define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
732 #define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
733 static const ASN1_COMPAT_FUNCS sname##_ff = { \
734 (ASN1_new_func *)sname##_new, \
735 (ASN1_free_func *)sname##_free, \
736 (ASN1_d2i_func *)d2i_##sname, \
737 (ASN1_i2d_func *)i2d_##sname, \
738 }; \
739 ASN1_ITEM_start(sname) \
740 ASN1_ITYPE_COMPAT, \
741 tag, \
742 NULL, \
743 0, \
744 &sname##_ff, \
745 0, \
746 #sname \
747 ASN1_ITEM_end(sname)
749 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
750 ASN1_ITEM_start(sname) \
751 ASN1_ITYPE_EXTERN, \
752 tag, \
753 NULL, \
754 0, \
755 &fptrs, \
756 0, \
757 #sname \
758 ASN1_ITEM_end(sname)
760 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
762 #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
764 #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
766 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
767 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
769 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
770 stname *fname##_new(void) \
772 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
774 void fname##_free(stname *a) \
776 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
779 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
780 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
781 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
783 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
784 stname *d2i_##fname(stname **a, unsigned char **in, long len) \
786 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
788 int i2d_##fname(stname *a, unsigned char **out) \
790 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
793 /* This includes evil casts to remove const: they will go away when full
794 * ASN1 constification is done.
796 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
797 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
799 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, (unsigned char **)in, len, ASN1_ITEM_rptr(itname));\
801 int i2d_##fname(const stname *a, unsigned char **out) \
803 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
806 #define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
807 stname * stname##_dup(stname *x) \
809 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
812 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
813 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
815 #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
816 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
817 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
819 /* external definitions for primitive types */
821 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
822 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
823 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
824 DECLARE_ASN1_ITEM(ASN1_ANY)
825 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
826 DECLARE_ASN1_ITEM(CBIGNUM)
827 DECLARE_ASN1_ITEM(BIGNUM)
828 DECLARE_ASN1_ITEM(LONG)
829 DECLARE_ASN1_ITEM(ZLONG)
831 DECLARE_STACK_OF(ASN1_VALUE)
833 /* Functions used internally by the ASN1 code */
835 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
836 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
837 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
838 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
840 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
841 int ASN1_template_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt);
842 int ASN1_item_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it,
843 int tag, int aclass, char opt, ASN1_TLC *ctx);
845 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
846 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt);
847 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
849 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
850 int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
852 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
853 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
855 ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
857 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
859 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
861 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
862 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
863 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
864 int asn1_enc_save(ASN1_VALUE **pval, unsigned char *in, int inlen, const ASN1_ITEM *it);
866 #ifdef __cplusplus
868 #endif
869 #endif