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