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