OLD | NEW |
| (Empty) |
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 /* | |
6 * Support for encoding/decoding of ASN.1 using BER/DER (Basic/Distinguished | |
7 * Encoding Rules). The routines are found in and used extensively by the | |
8 * security library, but exported for other use. | |
9 * | |
10 * $Id: secasn1.h,v 1.18 2012/04/25 14:50:16 gerv%gerv.net Exp $ | |
11 */ | |
12 | |
13 #ifndef _SECASN1_H_ | |
14 #define _SECASN1_H_ | |
15 | |
16 #include "utilrename.h" | |
17 #include "plarena.h" | |
18 | |
19 #include "seccomon.h" | |
20 #include "secasn1t.h" | |
21 | |
22 | |
23 /************************************************************************/ | |
24 SEC_BEGIN_PROTOS | |
25 | |
26 /* | |
27 * XXX These function prototypes need full, explanatory comments. | |
28 */ | |
29 | |
30 /* | |
31 ** Decoding. | |
32 */ | |
33 | |
34 extern SEC_ASN1DecoderContext *SEC_ASN1DecoderStart(PLArenaPool *pool, | |
35 void *dest, | |
36 const SEC_ASN1Template *t); | |
37 | |
38 /* XXX char or unsigned char? */ | |
39 extern SECStatus SEC_ASN1DecoderUpdate(SEC_ASN1DecoderContext *cx, | |
40 const char *buf, | |
41 unsigned long len); | |
42 | |
43 extern SECStatus SEC_ASN1DecoderFinish(SEC_ASN1DecoderContext *cx); | |
44 | |
45 /* Higher level code detected an error, abort the rest of the processing */ | |
46 extern void SEC_ASN1DecoderAbort(SEC_ASN1DecoderContext *cx, int error); | |
47 | |
48 extern void SEC_ASN1DecoderSetFilterProc(SEC_ASN1DecoderContext *cx, | |
49 SEC_ASN1WriteProc fn, | |
50 void *arg, PRBool no_store); | |
51 | |
52 extern void SEC_ASN1DecoderClearFilterProc(SEC_ASN1DecoderContext *cx); | |
53 | |
54 extern void SEC_ASN1DecoderSetNotifyProc(SEC_ASN1DecoderContext *cx, | |
55 SEC_ASN1NotifyProc fn, | |
56 void *arg); | |
57 | |
58 extern void SEC_ASN1DecoderClearNotifyProc(SEC_ASN1DecoderContext *cx); | |
59 | |
60 extern SECStatus SEC_ASN1Decode(PLArenaPool *pool, void *dest, | |
61 const SEC_ASN1Template *t, | |
62 const char *buf, long len); | |
63 | |
64 /* Both classic ASN.1 and QuickDER have a feature that removes leading zeroes | |
65 out of SEC_ASN1_INTEGER if the caller sets siUnsignedInteger in the type | |
66 field of the target SECItem prior to calling the decoder. Otherwise, the | |
67 type field is ignored and untouched. For SECItem that are dynamically | |
68 allocated (from POINTER, SET OF, SEQUENCE OF) the decoder sets the type | |
69 field to siBuffer. */ | |
70 | |
71 extern SECStatus SEC_ASN1DecodeItem(PLArenaPool *pool, void *dest, | |
72 const SEC_ASN1Template *t, | |
73 const SECItem *src); | |
74 | |
75 extern SECStatus SEC_QuickDERDecodeItem(PLArenaPool* arena, void* dest, | |
76 const SEC_ASN1Template* templateEntry, | |
77 const SECItem* src); | |
78 | |
79 /* | |
80 ** Encoding. | |
81 */ | |
82 | |
83 extern SEC_ASN1EncoderContext *SEC_ASN1EncoderStart(const void *src, | |
84 const SEC_ASN1Template *t, | |
85 SEC_ASN1WriteProc fn, | |
86 void *output_arg); | |
87 | |
88 /* XXX char or unsigned char? */ | |
89 extern SECStatus SEC_ASN1EncoderUpdate(SEC_ASN1EncoderContext *cx, | |
90 const char *buf, | |
91 unsigned long len); | |
92 | |
93 extern void SEC_ASN1EncoderFinish(SEC_ASN1EncoderContext *cx); | |
94 | |
95 /* Higher level code detected an error, abort the rest of the processing */ | |
96 extern void SEC_ASN1EncoderAbort(SEC_ASN1EncoderContext *cx, int error); | |
97 | |
98 extern void SEC_ASN1EncoderSetNotifyProc(SEC_ASN1EncoderContext *cx, | |
99 SEC_ASN1NotifyProc fn, | |
100 void *arg); | |
101 | |
102 extern void SEC_ASN1EncoderClearNotifyProc(SEC_ASN1EncoderContext *cx); | |
103 | |
104 extern void SEC_ASN1EncoderSetStreaming(SEC_ASN1EncoderContext *cx); | |
105 | |
106 extern void SEC_ASN1EncoderClearStreaming(SEC_ASN1EncoderContext *cx); | |
107 | |
108 extern void sec_ASN1EncoderSetDER(SEC_ASN1EncoderContext *cx); | |
109 | |
110 extern void sec_ASN1EncoderClearDER(SEC_ASN1EncoderContext *cx); | |
111 | |
112 extern void SEC_ASN1EncoderSetTakeFromBuf(SEC_ASN1EncoderContext *cx); | |
113 | |
114 extern void SEC_ASN1EncoderClearTakeFromBuf(SEC_ASN1EncoderContext *cx); | |
115 | |
116 extern SECStatus SEC_ASN1Encode(const void *src, const SEC_ASN1Template *t, | |
117 SEC_ASN1WriteProc output_proc, | |
118 void *output_arg); | |
119 | |
120 /* | |
121 * If both pool and dest are NULL, the caller should free the returned SECItem | |
122 * with a SECITEM_FreeItem(..., PR_TRUE) call. If pool is NULL but dest is | |
123 * not NULL, the caller should free the data buffer pointed to by dest with a | |
124 * SECITEM_FreeItem(dest, PR_FALSE) or PORT_Free(dest->data) call. | |
125 */ | |
126 extern SECItem * SEC_ASN1EncodeItem(PLArenaPool *pool, SECItem *dest, | |
127 const void *src, const SEC_ASN1Template *t); | |
128 | |
129 extern SECItem * SEC_ASN1EncodeInteger(PLArenaPool *pool, | |
130 SECItem *dest, long value); | |
131 | |
132 extern SECItem * SEC_ASN1EncodeUnsignedInteger(PLArenaPool *pool, | |
133 SECItem *dest, | |
134 unsigned long value); | |
135 | |
136 extern SECStatus SEC_ASN1DecodeInteger(SECItem *src, | |
137 unsigned long *value); | |
138 | |
139 /* | |
140 ** Utilities. | |
141 */ | |
142 | |
143 /* | |
144 * We have a length that needs to be encoded; how many bytes will the | |
145 * encoding take? | |
146 */ | |
147 extern int SEC_ASN1LengthLength (unsigned long len); | |
148 | |
149 /* encode the length and return the number of bytes we encoded. Buffer | |
150 * must be pre allocated */ | |
151 extern int SEC_ASN1EncodeLength(unsigned char *buf,int value); | |
152 | |
153 /* | |
154 * Find the appropriate subtemplate for the given template. | |
155 * This may involve calling a "chooser" function, or it may just | |
156 * be right there. In either case, it is expected to *have* a | |
157 * subtemplate; this is asserted in debug builds (in non-debug | |
158 * builds, NULL will be returned). | |
159 * | |
160 * "thing" is a pointer to the structure being encoded/decoded | |
161 * "encoding", when true, means that we are in the process of encoding | |
162 * (as opposed to in the process of decoding) | |
163 */ | |
164 extern const SEC_ASN1Template * | |
165 SEC_ASN1GetSubtemplate (const SEC_ASN1Template *inTemplate, void *thing, | |
166 PRBool encoding); | |
167 | |
168 /* whether the template is for a primitive type or a choice of | |
169 * primitive types | |
170 */ | |
171 extern PRBool SEC_ASN1IsTemplateSimple(const SEC_ASN1Template *theTemplate); | |
172 | |
173 /************************************************************************/ | |
174 | |
175 /* | |
176 * Generic Templates | |
177 * One for each of the simple types, plus a special one for ANY, plus: | |
178 * - a pointer to each one of those | |
179 * - a set of each one of those | |
180 * - a sequence of each one of those | |
181 * | |
182 * Note that these are alphabetical (case insensitive); please add new | |
183 * ones in the appropriate place. | |
184 */ | |
185 | |
186 extern const SEC_ASN1Template SEC_AnyTemplate[]; | |
187 extern const SEC_ASN1Template SEC_BitStringTemplate[]; | |
188 extern const SEC_ASN1Template SEC_BMPStringTemplate[]; | |
189 extern const SEC_ASN1Template SEC_BooleanTemplate[]; | |
190 extern const SEC_ASN1Template SEC_EnumeratedTemplate[]; | |
191 extern const SEC_ASN1Template SEC_GeneralizedTimeTemplate[]; | |
192 extern const SEC_ASN1Template SEC_IA5StringTemplate[]; | |
193 extern const SEC_ASN1Template SEC_IntegerTemplate[]; | |
194 extern const SEC_ASN1Template SEC_NullTemplate[]; | |
195 extern const SEC_ASN1Template SEC_ObjectIDTemplate[]; | |
196 extern const SEC_ASN1Template SEC_OctetStringTemplate[]; | |
197 extern const SEC_ASN1Template SEC_PrintableStringTemplate[]; | |
198 extern const SEC_ASN1Template SEC_T61StringTemplate[]; | |
199 extern const SEC_ASN1Template SEC_UniversalStringTemplate[]; | |
200 extern const SEC_ASN1Template SEC_UTCTimeTemplate[]; | |
201 extern const SEC_ASN1Template SEC_UTF8StringTemplate[]; | |
202 extern const SEC_ASN1Template SEC_VisibleStringTemplate[]; | |
203 | |
204 extern const SEC_ASN1Template SEC_PointerToAnyTemplate[]; | |
205 extern const SEC_ASN1Template SEC_PointerToBitStringTemplate[]; | |
206 extern const SEC_ASN1Template SEC_PointerToBMPStringTemplate[]; | |
207 extern const SEC_ASN1Template SEC_PointerToBooleanTemplate[]; | |
208 extern const SEC_ASN1Template SEC_PointerToEnumeratedTemplate[]; | |
209 extern const SEC_ASN1Template SEC_PointerToGeneralizedTimeTemplate[]; | |
210 extern const SEC_ASN1Template SEC_PointerToIA5StringTemplate[]; | |
211 extern const SEC_ASN1Template SEC_PointerToIntegerTemplate[]; | |
212 extern const SEC_ASN1Template SEC_PointerToNullTemplate[]; | |
213 extern const SEC_ASN1Template SEC_PointerToObjectIDTemplate[]; | |
214 extern const SEC_ASN1Template SEC_PointerToOctetStringTemplate[]; | |
215 extern const SEC_ASN1Template SEC_PointerToPrintableStringTemplate[]; | |
216 extern const SEC_ASN1Template SEC_PointerToT61StringTemplate[]; | |
217 extern const SEC_ASN1Template SEC_PointerToUniversalStringTemplate[]; | |
218 extern const SEC_ASN1Template SEC_PointerToUTCTimeTemplate[]; | |
219 extern const SEC_ASN1Template SEC_PointerToUTF8StringTemplate[]; | |
220 extern const SEC_ASN1Template SEC_PointerToVisibleStringTemplate[]; | |
221 | |
222 extern const SEC_ASN1Template SEC_SequenceOfAnyTemplate[]; | |
223 extern const SEC_ASN1Template SEC_SequenceOfBitStringTemplate[]; | |
224 extern const SEC_ASN1Template SEC_SequenceOfBMPStringTemplate[]; | |
225 extern const SEC_ASN1Template SEC_SequenceOfBooleanTemplate[]; | |
226 extern const SEC_ASN1Template SEC_SequenceOfEnumeratedTemplate[]; | |
227 extern const SEC_ASN1Template SEC_SequenceOfGeneralizedTimeTemplate[]; | |
228 extern const SEC_ASN1Template SEC_SequenceOfIA5StringTemplate[]; | |
229 extern const SEC_ASN1Template SEC_SequenceOfIntegerTemplate[]; | |
230 extern const SEC_ASN1Template SEC_SequenceOfNullTemplate[]; | |
231 extern const SEC_ASN1Template SEC_SequenceOfObjectIDTemplate[]; | |
232 extern const SEC_ASN1Template SEC_SequenceOfOctetStringTemplate[]; | |
233 extern const SEC_ASN1Template SEC_SequenceOfPrintableStringTemplate[]; | |
234 extern const SEC_ASN1Template SEC_SequenceOfT61StringTemplate[]; | |
235 extern const SEC_ASN1Template SEC_SequenceOfUniversalStringTemplate[]; | |
236 extern const SEC_ASN1Template SEC_SequenceOfUTCTimeTemplate[]; | |
237 extern const SEC_ASN1Template SEC_SequenceOfUTF8StringTemplate[]; | |
238 extern const SEC_ASN1Template SEC_SequenceOfVisibleStringTemplate[]; | |
239 | |
240 extern const SEC_ASN1Template SEC_SetOfAnyTemplate[]; | |
241 extern const SEC_ASN1Template SEC_SetOfBitStringTemplate[]; | |
242 extern const SEC_ASN1Template SEC_SetOfBMPStringTemplate[]; | |
243 extern const SEC_ASN1Template SEC_SetOfBooleanTemplate[]; | |
244 extern const SEC_ASN1Template SEC_SetOfEnumeratedTemplate[]; | |
245 extern const SEC_ASN1Template SEC_SetOfGeneralizedTimeTemplate[]; | |
246 extern const SEC_ASN1Template SEC_SetOfIA5StringTemplate[]; | |
247 extern const SEC_ASN1Template SEC_SetOfIntegerTemplate[]; | |
248 extern const SEC_ASN1Template SEC_SetOfNullTemplate[]; | |
249 extern const SEC_ASN1Template SEC_SetOfObjectIDTemplate[]; | |
250 extern const SEC_ASN1Template SEC_SetOfOctetStringTemplate[]; | |
251 extern const SEC_ASN1Template SEC_SetOfPrintableStringTemplate[]; | |
252 extern const SEC_ASN1Template SEC_SetOfT61StringTemplate[]; | |
253 extern const SEC_ASN1Template SEC_SetOfUniversalStringTemplate[]; | |
254 extern const SEC_ASN1Template SEC_SetOfUTCTimeTemplate[]; | |
255 extern const SEC_ASN1Template SEC_SetOfUTF8StringTemplate[]; | |
256 extern const SEC_ASN1Template SEC_SetOfVisibleStringTemplate[]; | |
257 | |
258 /* | |
259 * Template for skipping a subitem; this only makes sense when decoding. | |
260 */ | |
261 extern const SEC_ASN1Template SEC_SkipTemplate[]; | |
262 | |
263 /* These functions simply return the address of the above-declared templates. | |
264 ** This is necessary for Windows DLLs. Sigh. | |
265 */ | |
266 SEC_ASN1_CHOOSER_DECLARE(SEC_AnyTemplate) | |
267 SEC_ASN1_CHOOSER_DECLARE(SEC_BMPStringTemplate) | |
268 SEC_ASN1_CHOOSER_DECLARE(SEC_BooleanTemplate) | |
269 SEC_ASN1_CHOOSER_DECLARE(SEC_BitStringTemplate) | |
270 SEC_ASN1_CHOOSER_DECLARE(SEC_GeneralizedTimeTemplate) | |
271 SEC_ASN1_CHOOSER_DECLARE(SEC_IA5StringTemplate) | |
272 SEC_ASN1_CHOOSER_DECLARE(SEC_IntegerTemplate) | |
273 SEC_ASN1_CHOOSER_DECLARE(SEC_NullTemplate) | |
274 SEC_ASN1_CHOOSER_DECLARE(SEC_ObjectIDTemplate) | |
275 SEC_ASN1_CHOOSER_DECLARE(SEC_OctetStringTemplate) | |
276 SEC_ASN1_CHOOSER_DECLARE(SEC_UTCTimeTemplate) | |
277 SEC_ASN1_CHOOSER_DECLARE(SEC_UTF8StringTemplate) | |
278 | |
279 SEC_ASN1_CHOOSER_DECLARE(SEC_PointerToAnyTemplate) | |
280 SEC_ASN1_CHOOSER_DECLARE(SEC_PointerToOctetStringTemplate) | |
281 | |
282 SEC_ASN1_CHOOSER_DECLARE(SEC_SetOfAnyTemplate) | |
283 | |
284 SEC_ASN1_CHOOSER_DECLARE(SEC_EnumeratedTemplate) | |
285 SEC_ASN1_CHOOSER_DECLARE(SEC_PointerToEnumeratedTemplate) | |
286 SEC_ASN1_CHOOSER_DECLARE(SEC_SequenceOfAnyTemplate) | |
287 SEC_ASN1_CHOOSER_DECLARE(SEC_SequenceOfObjectIDTemplate) | |
288 SEC_ASN1_CHOOSER_DECLARE(SEC_SkipTemplate) | |
289 SEC_ASN1_CHOOSER_DECLARE(SEC_UniversalStringTemplate) | |
290 SEC_ASN1_CHOOSER_DECLARE(SEC_PrintableStringTemplate) | |
291 SEC_ASN1_CHOOSER_DECLARE(SEC_T61StringTemplate) | |
292 SEC_ASN1_CHOOSER_DECLARE(SEC_PointerToGeneralizedTimeTemplate) | |
293 SEC_END_PROTOS | |
294 #endif /* _SECASN1_H_ */ | |
OLD | NEW |