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