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