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