1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef _SECDERT_H_
6 #define _SECDERT_H_
7 /*
8  * secdert.h - public data structures for the DER encoding and
9  *             decoding utilities library
10  */
11 
12 #include "utilrename.h"
13 #include "seccomon.h"
14 
15 typedef struct DERTemplateStr DERTemplate;
16 
17 /*
18 ** An array of these structures defines an encoding for an object using DER.
19 ** The array usually starts with a dummy entry whose kind is DER_SEQUENCE;
20 ** such an array is terminated with an entry where kind == 0.  (An array
21 ** which consists of a single component does not require a second dummy
22 ** entry -- the array is only searched as long as previous component(s)
23 ** instruct it.)
24 */
25 struct DERTemplateStr {
26     /*
27     ** Kind of item being decoded/encoded, including tags and modifiers.
28     */
29     unsigned long kind;
30 
31     /*
32     ** Offset from base of structure to field that holds the value
33     ** being decoded/encoded.
34     */
35     unsigned int offset;
36 
37     /*
38     ** When kind suggests it (DER_POINTER, DER_INDEFINITE, DER_INLINE),
39     ** this points to a sub-template for nested encoding/decoding.
40     */
41     DERTemplate *sub;
42 
43     /*
44     ** Argument value, dependent on "kind" and/or template placement
45     ** within an array of templates:
46     **  - In the first element of a template array, the value is the
47     **    size of the structure to allocate when this template is being
48     **    referenced by another template via DER_POINTER or DER_INDEFINITE.
49     **  - In a component of a DER_SET or DER_SEQUENCE which is *not* a
50     **    DER_UNIVERSAL type (that is, it has a class tag for either
51     **    DER_APPLICATION, DER_CONTEXT_SPECIFIC, or DER_PRIVATE), the
52     **    value is the underlying type of item being decoded/encoded.
53     */
54     unsigned long arg;
55 };
56 
57 /************************************************************************/
58 
59 /* default chunksize for arenas used for DER stuff */
60 #define DER_DEFAULT_CHUNKSIZE (2048)
61 
62 /*
63 ** BER/DER values for ASN.1 identifier octets.
64 */
65 #define DER_TAG_MASK 0xff
66 
67 /*
68  * BER/DER universal type tag numbers.
69  * The values are defined by the X.208 standard; do not change them!
70  * NOTE: if you add anything to this list, you must add code to derdec.c
71  * to accept the tag, and probably also to derenc.c to encode it.
72  */
73 #define DER_TAGNUM_MASK 0x1f
74 #define DER_BOOLEAN 0x01
75 #define DER_INTEGER 0x02
76 #define DER_BIT_STRING 0x03
77 #define DER_OCTET_STRING 0x04
78 #define DER_NULL 0x05
79 #define DER_OBJECT_ID 0x06
80 #define DER_SEQUENCE 0x10
81 #define DER_SET 0x11
82 #define DER_PRINTABLE_STRING 0x13
83 #define DER_T61_STRING 0x14
84 #define DER_IA5_STRING 0x16
85 #define DER_UTC_TIME 0x17
86 #define DER_VISIBLE_STRING 0x1a
87 #define DER_HIGH_TAG_NUMBER 0x1f
88 
89 /*
90 ** Modifiers to type tags.  These are also specified by a/the
91 ** standard, and must not be changed.
92 */
93 
94 #define DER_METHOD_MASK 0x20
95 #define DER_PRIMITIVE 0x00
96 #define DER_CONSTRUCTED 0x20
97 
98 #define DER_CLASS_MASK 0xc0
99 #define DER_UNIVERSAL 0x00
100 #define DER_APPLICATION 0x40
101 #define DER_CONTEXT_SPECIFIC 0x80
102 #define DER_PRIVATE 0xc0
103 
104 /*
105 ** Our additions, used for templates.
106 ** These are not defined by any standard; the values are used internally only.
107 ** Just be careful to keep them out of the low 8 bits.
108 */
109 #define DER_OPTIONAL 0x00100
110 #define DER_EXPLICIT 0x00200
111 #define DER_ANY 0x00400
112 #define DER_INLINE 0x00800
113 #define DER_POINTER 0x01000
114 #define DER_INDEFINITE 0x02000
115 #define DER_DERPTR 0x04000
116 #define DER_SKIP 0x08000
117 #define DER_FORCE 0x10000
118 #define DER_OUTER 0x40000 /* for DER_DERPTR */
119 
120 /*
121 ** Macro to convert der decoded bit string into a decoded octet
122 ** string. All it needs to do is fiddle with the length code.
123 */
124 #define DER_ConvertBitString(item)            \
125     {                                         \
126         (item)->len = ((item)->len + 7) >> 3; \
127     }
128 
129 #endif /* _SECDERT_H_ */
130