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