/src/nss/lib/util/secasn1d.c
Line | Count | Source (jump to first uncovered line) |
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 DEcoding ASN.1 data based on BER/DER (Basic/Distinguished |
7 | | * Encoding Rules). |
8 | | */ |
9 | | |
10 | | /* #define DEBUG_ASN1D_STATES 1 */ |
11 | | |
12 | | #ifdef DEBUG_ASN1D_STATES |
13 | | #include <stdio.h> |
14 | | #define PR_Assert sec_asn1d_Assert |
15 | | #endif |
16 | | |
17 | | #include <limits.h> |
18 | | |
19 | | #include "secasn1.h" |
20 | | #include "secerr.h" |
21 | | |
22 | | typedef enum { |
23 | | beforeIdentifier, |
24 | | duringIdentifier, |
25 | | afterIdentifier, |
26 | | beforeLength, |
27 | | duringLength, |
28 | | afterLength, |
29 | | beforeBitString, |
30 | | duringBitString, |
31 | | duringConstructedString, |
32 | | duringGroup, |
33 | | duringLeaf, |
34 | | duringSaveEncoding, |
35 | | duringSequence, |
36 | | afterConstructedString, |
37 | | afterGroup, |
38 | | afterExplicit, |
39 | | afterImplicit, |
40 | | afterInline, |
41 | | afterPointer, |
42 | | afterSaveEncoding, |
43 | | beforeEndOfContents, |
44 | | duringEndOfContents, |
45 | | afterEndOfContents, |
46 | | beforeChoice, |
47 | | duringChoice, |
48 | | afterChoice, |
49 | | notInUse |
50 | | } sec_asn1d_parse_place; |
51 | | |
52 | | #ifdef DEBUG_ASN1D_STATES |
53 | | static const char *const place_names[] = { |
54 | | "beforeIdentifier", |
55 | | "duringIdentifier", |
56 | | "afterIdentifier", |
57 | | "beforeLength", |
58 | | "duringLength", |
59 | | "afterLength", |
60 | | "beforeBitString", |
61 | | "duringBitString", |
62 | | "duringConstructedString", |
63 | | "duringGroup", |
64 | | "duringLeaf", |
65 | | "duringSaveEncoding", |
66 | | "duringSequence", |
67 | | "afterConstructedString", |
68 | | "afterGroup", |
69 | | "afterExplicit", |
70 | | "afterImplicit", |
71 | | "afterInline", |
72 | | "afterPointer", |
73 | | "afterSaveEncoding", |
74 | | "beforeEndOfContents", |
75 | | "duringEndOfContents", |
76 | | "afterEndOfContents", |
77 | | "beforeChoice", |
78 | | "duringChoice", |
79 | | "afterChoice", |
80 | | "notInUse" |
81 | | }; |
82 | | |
83 | | static const char *const class_names[] = { |
84 | | "UNIVERSAL", |
85 | | "APPLICATION", |
86 | | "CONTEXT_SPECIFIC", |
87 | | "PRIVATE" |
88 | | }; |
89 | | |
90 | | static const char *const method_names[] = { "PRIMITIVE", "CONSTRUCTED" }; |
91 | | |
92 | | static const char *const type_names[] = { |
93 | | "END_OF_CONTENTS", |
94 | | "BOOLEAN", |
95 | | "INTEGER", |
96 | | "BIT_STRING", |
97 | | "OCTET_STRING", |
98 | | "NULL", |
99 | | "OBJECT_ID", |
100 | | "OBJECT_DESCRIPTOR", |
101 | | "(type 08)", |
102 | | "REAL", |
103 | | "ENUMERATED", |
104 | | "EMBEDDED", |
105 | | "UTF8_STRING", |
106 | | "(type 0d)", |
107 | | "(type 0e)", |
108 | | "(type 0f)", |
109 | | "SEQUENCE", |
110 | | "SET", |
111 | | "NUMERIC_STRING", |
112 | | "PRINTABLE_STRING", |
113 | | "T61_STRING", |
114 | | "VIDEOTEXT_STRING", |
115 | | "IA5_STRING", |
116 | | "UTC_TIME", |
117 | | "GENERALIZED_TIME", |
118 | | "GRAPHIC_STRING", |
119 | | "VISIBLE_STRING", |
120 | | "GENERAL_STRING", |
121 | | "UNIVERSAL_STRING", |
122 | | "(type 1d)", |
123 | | "BMP_STRING", |
124 | | "HIGH_TAG_VALUE" |
125 | | }; |
126 | | |
127 | | static const char *const flag_names[] = { |
128 | | /* flags, right to left */ |
129 | | "OPTIONAL", |
130 | | "EXPLICIT", |
131 | | "ANY", |
132 | | "INLINE", |
133 | | "POINTER", |
134 | | "GROUP", |
135 | | "DYNAMIC", |
136 | | "SKIP", |
137 | | "INNER", |
138 | | "SAVE", |
139 | | "", /* decoder ignores "MAY_STREAM", */ |
140 | | "SKIP_REST", |
141 | | "CHOICE", |
142 | | "NO_STREAM", |
143 | | "DEBUG_BREAK", |
144 | | "unknown 08", |
145 | | "unknown 10", |
146 | | "unknown 20", |
147 | | "unknown 40", |
148 | | "unknown 80" |
149 | | }; |
150 | | |
151 | | static int /* bool */ |
152 | | formatKind(unsigned long kind, char *buf, int space_in_buffer) |
153 | | { |
154 | | int i; |
155 | | unsigned long k = kind & SEC_ASN1_TAGNUM_MASK; |
156 | | unsigned long notag = kind & (SEC_ASN1_CHOICE | SEC_ASN1_POINTER | |
157 | | SEC_ASN1_INLINE | SEC_ASN1_ANY | SEC_ASN1_SAVE); |
158 | | |
159 | | buf[0] = 0; |
160 | | if ((kind & SEC_ASN1_CLASS_MASK) != SEC_ASN1_UNIVERSAL) { |
161 | | space_in_buffer -= snprintf(buf, space_in_buffer, " %s", class_names[(kind & SEC_ASN1_CLASS_MASK) >> 6]); |
162 | | buf += strlen(buf); |
163 | | } |
164 | | if (kind & SEC_ASN1_METHOD_MASK) { |
165 | | space_in_buffer -= snprintf(buf, space_in_buffer, " %s", method_names[1]); |
166 | | buf += strlen(buf); |
167 | | } |
168 | | if ((kind & SEC_ASN1_CLASS_MASK) == SEC_ASN1_UNIVERSAL) { |
169 | | if (k || !notag) { |
170 | | space_in_buffer -= snprintf(buf, space_in_buffer, " %s", type_names[k]); |
171 | | if ((k == SEC_ASN1_SET || k == SEC_ASN1_SEQUENCE) && |
172 | | (kind & SEC_ASN1_GROUP)) { |
173 | | buf += strlen(buf); |
174 | | space_in_buffer -= snprintf(buf, space_in_buffer, "_OF"); |
175 | | } |
176 | | } |
177 | | } else { |
178 | | space_in_buffer -= snprintf(buf, space_in_buffer, " [%lu]", k); |
179 | | } |
180 | | buf += strlen(buf); |
181 | | |
182 | | for (k = kind >> 8, i = 0; k; k >>= 1, ++i) { |
183 | | if (k & 1) { |
184 | | space_in_buffer -= snprintf(buf, space_in_buffer, " %s", flag_names[i]); |
185 | | buf += strlen(buf); |
186 | | } |
187 | | } |
188 | | return notag != 0; |
189 | | } |
190 | | |
191 | | #endif /* DEBUG_ASN1D_STATES */ |
192 | | |
193 | | typedef enum { |
194 | | allDone, |
195 | | decodeError, |
196 | | keepGoing, |
197 | | needBytes |
198 | | } sec_asn1d_parse_status; |
199 | | |
200 | | struct subitem { |
201 | | const void *data; |
202 | | unsigned long len; /* only used for substrings */ |
203 | | struct subitem *next; |
204 | | }; |
205 | | |
206 | | typedef struct sec_asn1d_state_struct { |
207 | | SEC_ASN1DecoderContext *top; |
208 | | const SEC_ASN1Template *theTemplate; |
209 | | void *dest; |
210 | | |
211 | | void *our_mark; /* free on completion */ |
212 | | |
213 | | struct sec_asn1d_state_struct *parent; /* aka prev */ |
214 | | struct sec_asn1d_state_struct *child; /* aka next */ |
215 | | |
216 | | sec_asn1d_parse_place place; |
217 | | |
218 | | /* |
219 | | * XXX explain the next fields as clearly as possible... |
220 | | */ |
221 | | unsigned char found_tag_modifiers; |
222 | | unsigned char expect_tag_modifiers; |
223 | | unsigned long check_tag_mask; |
224 | | unsigned long found_tag_number; |
225 | | unsigned long expect_tag_number; |
226 | | unsigned long underlying_kind; |
227 | | |
228 | | unsigned long contents_length; |
229 | | unsigned long pending; |
230 | | unsigned long consumed; |
231 | | |
232 | | int depth; |
233 | | |
234 | | /* |
235 | | * Bit strings have their length adjusted -- the first octet of the |
236 | | * contents contains a value between 0 and 7 which says how many bits |
237 | | * at the end of the octets are not actually part of the bit string; |
238 | | * when parsing bit strings we put that value here because we need it |
239 | | * later, for adjustment of the length (when the whole string is done). |
240 | | */ |
241 | | unsigned int bit_string_unused_bits; |
242 | | |
243 | | /* |
244 | | * The following are used for indefinite-length constructed strings. |
245 | | */ |
246 | | struct subitem *subitems_head; |
247 | | struct subitem *subitems_tail; |
248 | | |
249 | | PRPackedBool |
250 | | allocate, /* when true, need to allocate the destination */ |
251 | | endofcontents, /* this state ended up parsing its parent's end-of-contents octets */ |
252 | | explicit, /* we are handling an explicit header */ |
253 | | indefinite, /* the current item has indefinite-length encoding */ |
254 | | missing, /* an optional field that was not present */ |
255 | | optional, /* the template says this field may be omitted */ |
256 | | substring; /* this is a substring of a constructed string */ |
257 | | |
258 | | } sec_asn1d_state; |
259 | | |
260 | 48 | #define IS_HIGH_TAG_NUMBER(n) ((n) == SEC_ASN1_HIGH_TAG_NUMBER) |
261 | 0 | #define LAST_TAG_NUMBER_BYTE(b) (((b)&0x80) == 0) |
262 | 0 | #define TAG_NUMBER_BITS 7 |
263 | 0 | #define TAG_NUMBER_MASK 0x7f |
264 | | |
265 | 48 | #define LENGTH_IS_SHORT_FORM(b) (((b)&0x80) == 0) |
266 | 12 | #define LONG_FORM_LENGTH(b) ((b)&0x7f) |
267 | | |
268 | 20 | #define HIGH_BITS(field, cnt) ((field) >> ((sizeof(field) * 8) - (cnt))) |
269 | | |
270 | | /* |
271 | | * An "outsider" will have an opaque pointer to this, created by calling |
272 | | * SEC_ASN1DecoderStart(). It will be passed back in to all subsequent |
273 | | * calls to SEC_ASN1DecoderUpdate(), and when done it is passed to |
274 | | * SEC_ASN1DecoderFinish(). |
275 | | */ |
276 | | struct sec_DecoderContext_struct { |
277 | | PLArenaPool *our_pool; /* for our internal allocs */ |
278 | | PLArenaPool *their_pool; /* for destination structure allocs */ |
279 | | #ifdef SEC_ASN1D_FREE_ON_ERROR /* \ |
280 | | * XXX see comment below (by same \ |
281 | | * ifdef) that explains why this \ |
282 | | * does not work (need more smarts \ |
283 | | * in order to free back to mark) \ |
284 | | */ |
285 | | /* |
286 | | * XXX how to make their_mark work in the case where they do NOT |
287 | | * give us a pool pointer? |
288 | | */ |
289 | | void *their_mark; /* free on error */ |
290 | | #endif |
291 | | |
292 | | sec_asn1d_state *current; |
293 | | sec_asn1d_parse_status status; |
294 | | |
295 | | /* The maximum size the caller is willing to allow a single element |
296 | | * to be before returning an error. |
297 | | * |
298 | | * In the case of an indefinite length element, this is the sum total |
299 | | * of all child elements. |
300 | | * |
301 | | * In the case of a definite length element, this represents the maximum |
302 | | * size of the top-level element. |
303 | | */ |
304 | | unsigned long max_element_size; |
305 | | |
306 | | SEC_ASN1NotifyProc notify_proc; /* call before/after handling field */ |
307 | | void *notify_arg; /* argument to notify_proc */ |
308 | | PRBool during_notify; /* true during call to notify_proc */ |
309 | | |
310 | | SEC_ASN1WriteProc filter_proc; /* pass field bytes to this */ |
311 | | void *filter_arg; /* argument to that function */ |
312 | | PRBool filter_only; /* do not allocate/store fields */ |
313 | | }; |
314 | | |
315 | | /* |
316 | | * XXX this is a fairly generic function that may belong elsewhere |
317 | | */ |
318 | | static void * |
319 | | sec_asn1d_alloc(PLArenaPool *poolp, unsigned long len) |
320 | 96 | { |
321 | 96 | void *thing; |
322 | | |
323 | 96 | if (poolp != NULL) { |
324 | | /* |
325 | | * Allocate from the pool. |
326 | | */ |
327 | 96 | thing = PORT_ArenaAlloc(poolp, len); |
328 | 96 | } else { |
329 | | /* |
330 | | * Allocate generically. |
331 | | */ |
332 | 0 | thing = PORT_Alloc(len); |
333 | 0 | } |
334 | | |
335 | 96 | return thing; |
336 | 96 | } |
337 | | |
338 | | /* |
339 | | * XXX this is a fairly generic function that may belong elsewhere |
340 | | */ |
341 | | static void * |
342 | | sec_asn1d_zalloc(PLArenaPool *poolp, unsigned long len) |
343 | 80 | { |
344 | 80 | void *thing; |
345 | | |
346 | 80 | thing = sec_asn1d_alloc(poolp, len); |
347 | 80 | if (thing != NULL) |
348 | 80 | PORT_Memset(thing, 0, len); |
349 | 80 | return thing; |
350 | 80 | } |
351 | | |
352 | | static sec_asn1d_state * |
353 | | sec_asn1d_push_state(SEC_ASN1DecoderContext *cx, |
354 | | const SEC_ASN1Template *theTemplate, |
355 | | void *dest, PRBool new_depth) |
356 | 32 | { |
357 | 32 | sec_asn1d_state *state, *new_state; |
358 | | |
359 | 32 | state = cx->current; |
360 | | |
361 | 32 | PORT_Assert(state == NULL || state->child == NULL); |
362 | | |
363 | 32 | if (state != NULL) { |
364 | 24 | PORT_Assert(state->our_mark == NULL); |
365 | 24 | state->our_mark = PORT_ArenaMark(cx->our_pool); |
366 | 24 | } |
367 | | |
368 | 32 | if (theTemplate == NULL) { |
369 | 0 | PORT_SetError(SEC_ERROR_BAD_TEMPLATE); |
370 | 0 | goto loser; |
371 | 0 | } |
372 | | |
373 | 32 | new_state = (sec_asn1d_state *)sec_asn1d_zalloc(cx->our_pool, |
374 | 32 | sizeof(*new_state)); |
375 | 32 | if (new_state == NULL) { |
376 | 0 | goto loser; |
377 | 0 | } |
378 | | |
379 | 32 | new_state->top = cx; |
380 | 32 | new_state->parent = state; |
381 | 32 | new_state->theTemplate = theTemplate; |
382 | 32 | new_state->place = notInUse; |
383 | 32 | if (dest != NULL) |
384 | 32 | new_state->dest = (char *)dest + theTemplate->offset; |
385 | | |
386 | 32 | if (state != NULL) { |
387 | 24 | new_state->depth = state->depth; |
388 | 24 | if (new_depth) { |
389 | 16 | if (++new_state->depth > SEC_ASN1D_MAX_DEPTH) { |
390 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
391 | 0 | goto loser; |
392 | 0 | } |
393 | 16 | } |
394 | 24 | state->child = new_state; |
395 | 24 | } |
396 | | |
397 | 32 | cx->current = new_state; |
398 | 32 | return new_state; |
399 | | |
400 | 0 | loser: |
401 | 0 | cx->status = decodeError; |
402 | 0 | if (state != NULL) { |
403 | 0 | PORT_ArenaRelease(cx->our_pool, state->our_mark); |
404 | 0 | state->our_mark = NULL; |
405 | 0 | } |
406 | 0 | return NULL; |
407 | 32 | } |
408 | | |
409 | | static void |
410 | | sec_asn1d_scrub_state(sec_asn1d_state *state) |
411 | 56 | { |
412 | | /* |
413 | | * Some default "scrubbing". |
414 | | * XXX right set of initializations? |
415 | | */ |
416 | 56 | state->place = beforeIdentifier; |
417 | 56 | state->endofcontents = PR_FALSE; |
418 | 56 | state->indefinite = PR_FALSE; |
419 | 56 | state->missing = PR_FALSE; |
420 | 56 | PORT_Assert(state->consumed == 0); |
421 | 56 | } |
422 | | |
423 | | static void |
424 | | sec_asn1d_notify_before(SEC_ASN1DecoderContext *cx, void *dest, int depth) |
425 | 40 | { |
426 | 40 | if (cx->notify_proc == NULL) |
427 | 40 | return; |
428 | | |
429 | 0 | cx->during_notify = PR_TRUE; |
430 | 0 | (*cx->notify_proc)(cx->notify_arg, PR_TRUE, dest, depth); |
431 | 0 | cx->during_notify = PR_FALSE; |
432 | 0 | } |
433 | | |
434 | | static void |
435 | | sec_asn1d_notify_after(SEC_ASN1DecoderContext *cx, void *dest, int depth) |
436 | 40 | { |
437 | 40 | if (cx->notify_proc == NULL) |
438 | 40 | return; |
439 | | |
440 | 0 | cx->during_notify = PR_TRUE; |
441 | 0 | (*cx->notify_proc)(cx->notify_arg, PR_FALSE, dest, depth); |
442 | 0 | cx->during_notify = PR_FALSE; |
443 | 0 | } |
444 | | |
445 | | static sec_asn1d_state * |
446 | | sec_asn1d_init_state_based_on_template(sec_asn1d_state *state) |
447 | 56 | { |
448 | 56 | PRBool explicit, optional, universal; |
449 | 56 | unsigned char expect_tag_modifiers; |
450 | 56 | unsigned long encode_kind, under_kind; |
451 | 56 | unsigned long check_tag_mask, expect_tag_number; |
452 | | |
453 | | /* XXX Check that both of these tests are really needed/appropriate. */ |
454 | 56 | if (state == NULL || state->top->status == decodeError) |
455 | 0 | return state; |
456 | | |
457 | 56 | encode_kind = state->theTemplate->kind; |
458 | | |
459 | 56 | if (encode_kind & SEC_ASN1_SAVE) { |
460 | | /* |
461 | | * This is a "magic" field that saves away all bytes, allowing |
462 | | * the immediately following field to still be decoded from this |
463 | | * same spot -- sort of a fork. |
464 | | */ |
465 | | /* check that there are no extraneous bits */ |
466 | 0 | PORT_Assert(encode_kind == SEC_ASN1_SAVE); |
467 | 0 | if (state->top->filter_only) { |
468 | | /* |
469 | | * If we are not storing, then we do not do the SAVE field |
470 | | * at all. Just move ahead to the "real" field instead, |
471 | | * doing the appropriate notify calls before and after. |
472 | | */ |
473 | 0 | sec_asn1d_notify_after(state->top, state->dest, state->depth); |
474 | | /* |
475 | | * Since we are not storing, allow for our current dest value |
476 | | * to be NULL. (This might not actually occur, but right now I |
477 | | * cannot convince myself one way or the other.) If it is NULL, |
478 | | * assume that our parent dest can help us out. |
479 | | */ |
480 | 0 | if (state->dest == NULL) |
481 | 0 | state->dest = state->parent->dest; |
482 | 0 | else |
483 | 0 | state->dest = (char *)state->dest - state->theTemplate->offset; |
484 | 0 | state->theTemplate++; |
485 | 0 | if (state->dest != NULL) |
486 | 0 | state->dest = (char *)state->dest + state->theTemplate->offset; |
487 | 0 | sec_asn1d_notify_before(state->top, state->dest, state->depth); |
488 | 0 | encode_kind = state->theTemplate->kind; |
489 | 0 | PORT_Assert((encode_kind & SEC_ASN1_SAVE) == 0); |
490 | 0 | } else { |
491 | 0 | sec_asn1d_scrub_state(state); |
492 | 0 | state->place = duringSaveEncoding; |
493 | 0 | state = sec_asn1d_push_state(state->top, SEC_AnyTemplate, |
494 | 0 | state->dest, PR_FALSE); |
495 | 0 | if (state != NULL) |
496 | 0 | state = sec_asn1d_init_state_based_on_template(state); |
497 | 0 | return state; |
498 | 0 | } |
499 | 0 | } |
500 | | |
501 | 56 | universal = ((encode_kind & SEC_ASN1_CLASS_MASK) == SEC_ASN1_UNIVERSAL) |
502 | 56 | ? PR_TRUE |
503 | 56 | : PR_FALSE; |
504 | | |
505 | 56 | explicit = (encode_kind & SEC_ASN1_EXPLICIT) ? PR_TRUE : PR_FALSE; |
506 | 56 | encode_kind &= ~SEC_ASN1_EXPLICIT; |
507 | | |
508 | 56 | optional = (encode_kind & SEC_ASN1_OPTIONAL) ? PR_TRUE : PR_FALSE; |
509 | 56 | encode_kind &= ~SEC_ASN1_OPTIONAL; |
510 | | |
511 | 56 | PORT_Assert(!(explicit && universal)); /* bad templates */ |
512 | | |
513 | 56 | encode_kind &= ~SEC_ASN1_DYNAMIC; |
514 | 56 | encode_kind &= ~SEC_ASN1_MAY_STREAM; |
515 | | |
516 | 56 | if (encode_kind & SEC_ASN1_CHOICE) { |
517 | | #if 0 /* XXX remove? */ |
518 | | sec_asn1d_state *child = sec_asn1d_push_state(state->top, state->theTemplate, state->dest, PR_FALSE); |
519 | | if ((sec_asn1d_state *)NULL == child) { |
520 | | return (sec_asn1d_state *)NULL; |
521 | | } |
522 | | |
523 | | child->allocate = state->allocate; |
524 | | child->place = beforeChoice; |
525 | | return child; |
526 | | #else |
527 | 0 | state->place = beforeChoice; |
528 | 0 | return state; |
529 | 0 | #endif |
530 | 0 | } |
531 | | |
532 | 56 | if ((encode_kind & (SEC_ASN1_POINTER | SEC_ASN1_INLINE)) || (!universal && !explicit)) { |
533 | 8 | const SEC_ASN1Template *subt; |
534 | 8 | void *dest; |
535 | 8 | PRBool child_allocate; |
536 | | |
537 | 8 | PORT_Assert((encode_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP)) == 0); |
538 | | |
539 | 8 | sec_asn1d_scrub_state(state); |
540 | 8 | child_allocate = PR_FALSE; |
541 | | |
542 | 8 | if (encode_kind & SEC_ASN1_POINTER) { |
543 | | /* |
544 | | * A POINTER means we need to allocate the destination for |
545 | | * this field. But, since it may also be an optional field, |
546 | | * we defer the allocation until later; we just record that |
547 | | * it needs to be done. |
548 | | * |
549 | | * There are two possible scenarios here -- one is just a |
550 | | * plain POINTER (kind of like INLINE, except with allocation) |
551 | | * and the other is an implicitly-tagged POINTER. We don't |
552 | | * need to do anything special here for the two cases, but |
553 | | * since the template definition can be tricky, we do check |
554 | | * that there are no extraneous bits set in encode_kind. |
555 | | * |
556 | | * XXX The same conditions which assert should set an error. |
557 | | */ |
558 | 0 | if (universal) { |
559 | | /* |
560 | | * "universal" means this entry is a standalone POINTER; |
561 | | * there should be no other bits set in encode_kind. |
562 | | */ |
563 | 0 | PORT_Assert(encode_kind == SEC_ASN1_POINTER); |
564 | 0 | } else { |
565 | | /* |
566 | | * If we get here we have an implicitly-tagged field |
567 | | * that needs to be put into a POINTER. The subtemplate |
568 | | * will determine how to decode the field, but encode_kind |
569 | | * describes the (implicit) tag we are looking for. |
570 | | * The non-tag bits of encode_kind will be ignored by |
571 | | * the code below; none of them should be set, however, |
572 | | * except for the POINTER bit itself -- so check that. |
573 | | */ |
574 | 0 | PORT_Assert((encode_kind & ~SEC_ASN1_TAG_MASK) == SEC_ASN1_POINTER); |
575 | 0 | } |
576 | 0 | if (!state->top->filter_only) |
577 | 0 | child_allocate = PR_TRUE; |
578 | 0 | dest = NULL; |
579 | 0 | state->place = afterPointer; |
580 | 8 | } else { |
581 | 8 | dest = state->dest; |
582 | 8 | if (encode_kind & SEC_ASN1_INLINE) { |
583 | | /* check that there are no extraneous bits */ |
584 | 8 | PORT_Assert(encode_kind == SEC_ASN1_INLINE && !optional); |
585 | 8 | state->place = afterInline; |
586 | 8 | } else { |
587 | 0 | state->place = afterImplicit; |
588 | 0 | } |
589 | 8 | } |
590 | | |
591 | 8 | state->optional = optional; |
592 | 8 | subt = SEC_ASN1GetSubtemplate(state->theTemplate, state->dest, PR_FALSE); |
593 | 8 | state = sec_asn1d_push_state(state->top, subt, dest, PR_FALSE); |
594 | 8 | if (state == NULL) |
595 | 0 | return NULL; |
596 | | |
597 | 8 | state->allocate = child_allocate; |
598 | | |
599 | 8 | if (universal) { |
600 | 8 | state = sec_asn1d_init_state_based_on_template(state); |
601 | 8 | if (state != NULL) { |
602 | | /* |
603 | | * If this field is optional, we need to record that on |
604 | | * the pushed child so it won't fail if the field isn't |
605 | | * found. I can't think of a way that this new state |
606 | | * could already have optional set (which we would wipe |
607 | | * out below if our local optional is not set) -- but |
608 | | * just to be sure, assert that it isn't set. |
609 | | */ |
610 | 8 | PORT_Assert(!state->optional); |
611 | 8 | state->optional = optional; |
612 | 8 | } |
613 | 8 | return state; |
614 | 8 | } |
615 | | |
616 | 0 | under_kind = state->theTemplate->kind; |
617 | 0 | under_kind &= ~SEC_ASN1_MAY_STREAM; |
618 | 48 | } else if (explicit) { |
619 | | /* |
620 | | * For explicit, we only need to match the encoding tag next, |
621 | | * then we will push another state to handle the entire inner |
622 | | * part. In this case, there is no underlying kind which plays |
623 | | * any part in the determination of the outer, explicit tag. |
624 | | * So we just set under_kind to 0, which is not a valid tag, |
625 | | * and the rest of the tag matching stuff should be okay. |
626 | | */ |
627 | 0 | under_kind = 0; |
628 | 48 | } else { |
629 | | /* |
630 | | * Nothing special; the underlying kind and the given encoding |
631 | | * information are the same. |
632 | | */ |
633 | 48 | under_kind = encode_kind; |
634 | 48 | } |
635 | | |
636 | | /* XXX is this the right set of bits to test here? */ |
637 | 48 | PORT_Assert((under_kind & (SEC_ASN1_EXPLICIT | SEC_ASN1_OPTIONAL | SEC_ASN1_MAY_STREAM | SEC_ASN1_INLINE | SEC_ASN1_POINTER)) == 0); |
638 | | |
639 | 48 | if (encode_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP)) { |
640 | 8 | PORT_Assert(encode_kind == under_kind); |
641 | 8 | if (encode_kind & SEC_ASN1_SKIP) { |
642 | 0 | PORT_Assert(!optional); |
643 | 0 | PORT_Assert(encode_kind == SEC_ASN1_SKIP); |
644 | 0 | state->dest = NULL; |
645 | 0 | } |
646 | 8 | check_tag_mask = 0; |
647 | 8 | expect_tag_modifiers = 0; |
648 | 8 | expect_tag_number = 0; |
649 | 40 | } else { |
650 | 40 | check_tag_mask = SEC_ASN1_TAG_MASK; |
651 | 40 | expect_tag_modifiers = (unsigned char)encode_kind & SEC_ASN1_TAG_MASK & ~SEC_ASN1_TAGNUM_MASK; |
652 | | /* |
653 | | * XXX This assumes only single-octet identifiers. To handle |
654 | | * the HIGH TAG form we would need to do some more work, especially |
655 | | * in how to specify them in the template, because right now we |
656 | | * do not provide a way to specify more *tag* bits in encode_kind. |
657 | | */ |
658 | 40 | expect_tag_number = encode_kind & SEC_ASN1_TAGNUM_MASK; |
659 | | |
660 | 40 | switch (under_kind & SEC_ASN1_TAGNUM_MASK) { |
661 | 0 | case SEC_ASN1_SET: |
662 | | /* |
663 | | * XXX A plain old SET (as opposed to a SET OF) is not implemented. |
664 | | * If it ever is, remove this assert... |
665 | | */ |
666 | 0 | PORT_Assert((under_kind & SEC_ASN1_GROUP) != 0); |
667 | | /* fallthru */ |
668 | 16 | case SEC_ASN1_SEQUENCE: |
669 | 16 | expect_tag_modifiers |= SEC_ASN1_CONSTRUCTED; |
670 | 16 | break; |
671 | 0 | case SEC_ASN1_BIT_STRING: |
672 | 0 | case SEC_ASN1_BMP_STRING: |
673 | 0 | case SEC_ASN1_GENERALIZED_TIME: |
674 | 0 | case SEC_ASN1_IA5_STRING: |
675 | 8 | case SEC_ASN1_OCTET_STRING: |
676 | 8 | case SEC_ASN1_PRINTABLE_STRING: |
677 | 8 | case SEC_ASN1_T61_STRING: |
678 | 8 | case SEC_ASN1_UNIVERSAL_STRING: |
679 | 8 | case SEC_ASN1_UTC_TIME: |
680 | 8 | case SEC_ASN1_UTF8_STRING: |
681 | 8 | case SEC_ASN1_VISIBLE_STRING: |
682 | 8 | check_tag_mask &= ~SEC_ASN1_CONSTRUCTED; |
683 | 8 | break; |
684 | 40 | } |
685 | 40 | } |
686 | | |
687 | 48 | state->check_tag_mask = check_tag_mask; |
688 | 48 | state->expect_tag_modifiers = expect_tag_modifiers; |
689 | 48 | state->expect_tag_number = expect_tag_number; |
690 | 48 | state->underlying_kind = under_kind; |
691 | 48 | state->explicit = explicit; |
692 | 48 | state->optional = optional; |
693 | | |
694 | 48 | sec_asn1d_scrub_state(state); |
695 | | |
696 | 48 | return state; |
697 | 48 | } |
698 | | |
699 | | static sec_asn1d_state * |
700 | | sec_asn1d_get_enclosing_construct(sec_asn1d_state *state) |
701 | 88 | { |
702 | 104 | for (state = state->parent; state; state = state->parent) { |
703 | 96 | sec_asn1d_parse_place place = state->place; |
704 | 96 | if (place != afterImplicit && |
705 | 96 | place != afterPointer && |
706 | 96 | place != afterInline && |
707 | 96 | place != afterSaveEncoding && |
708 | 96 | place != duringSaveEncoding && |
709 | 96 | place != duringChoice) { |
710 | | |
711 | | /* we've walked up the stack to a state that represents |
712 | | ** the enclosing construct. |
713 | | */ |
714 | 80 | break; |
715 | 80 | } |
716 | 96 | } |
717 | 88 | return state; |
718 | 88 | } |
719 | | |
720 | | static PRBool |
721 | | sec_asn1d_parent_allows_EOC(sec_asn1d_state *state) |
722 | 0 | { |
723 | | /* get state of enclosing construct. */ |
724 | 0 | state = sec_asn1d_get_enclosing_construct(state); |
725 | 0 | if (state) { |
726 | 0 | sec_asn1d_parse_place place = state->place; |
727 | | /* Is it one of the types that permits an unexpected EOC? */ |
728 | 0 | int eoc_permitted = |
729 | 0 | (place == duringGroup || |
730 | 0 | place == duringConstructedString || |
731 | 0 | state->child->optional); |
732 | 0 | return (state->indefinite && eoc_permitted) ? PR_TRUE : PR_FALSE; |
733 | 0 | } |
734 | 0 | return PR_FALSE; |
735 | 0 | } |
736 | | |
737 | | static unsigned long |
738 | | sec_asn1d_parse_identifier(sec_asn1d_state *state, |
739 | | const char *buf, unsigned long len) |
740 | 48 | { |
741 | 48 | unsigned char byte; |
742 | 48 | unsigned char tag_number; |
743 | | |
744 | 48 | PORT_Assert(state->place == beforeIdentifier); |
745 | | |
746 | 48 | if (len == 0) { |
747 | 0 | state->top->status = needBytes; |
748 | 0 | return 0; |
749 | 0 | } |
750 | | |
751 | 48 | byte = (unsigned char)*buf; |
752 | | #ifdef DEBUG_ASN1D_STATES |
753 | | { |
754 | | int bufsize = 256; |
755 | | char kindBuf[bufsize]; |
756 | | formatKind(byte, kindBuf, bufsize); |
757 | | printf("Found tag %02x %s\n", byte, kindBuf); |
758 | | } |
759 | | #endif |
760 | 48 | tag_number = byte & SEC_ASN1_TAGNUM_MASK; |
761 | | |
762 | 48 | if (IS_HIGH_TAG_NUMBER(tag_number)) { |
763 | 0 | state->place = duringIdentifier; |
764 | 0 | state->found_tag_number = 0; |
765 | | /* |
766 | | * Actually, we have no idea how many bytes are pending, but we |
767 | | * do know that it is at least 1. That is all we know; we have |
768 | | * to look at each byte to know if there is another, etc. |
769 | | */ |
770 | 0 | state->pending = 1; |
771 | 48 | } else { |
772 | 48 | if (byte == 0 && sec_asn1d_parent_allows_EOC(state)) { |
773 | | /* |
774 | | * Our parent has indefinite-length encoding, and the |
775 | | * entire tag found is 0, so it seems that we have hit the |
776 | | * end-of-contents octets. To handle this, we just change |
777 | | * our state to that which expects to get the bytes of the |
778 | | * end-of-contents octets and let that code re-read this byte |
779 | | * so that our categorization of field types is correct. |
780 | | * After that, our parent will then deal with everything else. |
781 | | */ |
782 | 0 | state->place = duringEndOfContents; |
783 | 0 | state->pending = 2; |
784 | 0 | state->found_tag_number = 0; |
785 | 0 | state->found_tag_modifiers = 0; |
786 | | /* |
787 | | * We might be an optional field that is, as we now find out, |
788 | | * missing. Give our parent a clue that this happened. |
789 | | */ |
790 | 0 | if (state->optional) |
791 | 0 | state->missing = PR_TRUE; |
792 | 0 | return 0; |
793 | 0 | } |
794 | 48 | state->place = afterIdentifier; |
795 | 48 | state->found_tag_number = tag_number; |
796 | 48 | } |
797 | 48 | state->found_tag_modifiers = byte & ~SEC_ASN1_TAGNUM_MASK; |
798 | | |
799 | 48 | return 1; |
800 | 48 | } |
801 | | |
802 | | static unsigned long |
803 | | sec_asn1d_parse_more_identifier(sec_asn1d_state *state, |
804 | | const char *buf, unsigned long len) |
805 | 0 | { |
806 | 0 | unsigned char byte; |
807 | 0 | int count; |
808 | |
|
809 | 0 | PORT_Assert(state->pending == 1); |
810 | 0 | PORT_Assert(state->place == duringIdentifier); |
811 | |
|
812 | 0 | if (len == 0) { |
813 | 0 | state->top->status = needBytes; |
814 | 0 | return 0; |
815 | 0 | } |
816 | | |
817 | 0 | count = 0; |
818 | |
|
819 | 0 | while (len && state->pending) { |
820 | 0 | if (HIGH_BITS(state->found_tag_number, TAG_NUMBER_BITS) != 0) { |
821 | | /* |
822 | | * The given high tag number overflows our container; |
823 | | * just give up. This is not likely to *ever* happen. |
824 | | */ |
825 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
826 | 0 | state->top->status = decodeError; |
827 | 0 | return 0; |
828 | 0 | } |
829 | | |
830 | 0 | state->found_tag_number <<= TAG_NUMBER_BITS; |
831 | |
|
832 | 0 | byte = (unsigned char)buf[count++]; |
833 | 0 | state->found_tag_number |= (byte & TAG_NUMBER_MASK); |
834 | |
|
835 | 0 | len--; |
836 | 0 | if (LAST_TAG_NUMBER_BYTE(byte)) |
837 | 0 | state->pending = 0; |
838 | 0 | } |
839 | | |
840 | 0 | if (state->pending == 0) |
841 | 0 | state->place = afterIdentifier; |
842 | |
|
843 | 0 | return count; |
844 | 0 | } |
845 | | |
846 | | static void |
847 | | sec_asn1d_confirm_identifier(sec_asn1d_state *state) |
848 | 48 | { |
849 | 48 | PRBool match; |
850 | | |
851 | 48 | PORT_Assert(state->place == afterIdentifier); |
852 | | |
853 | 48 | match = (PRBool)(((state->found_tag_modifiers & state->check_tag_mask) == state->expect_tag_modifiers) && ((state->found_tag_number & state->check_tag_mask) == state->expect_tag_number)); |
854 | 48 | if (match) { |
855 | 48 | state->place = beforeLength; |
856 | 48 | } else { |
857 | 0 | if (state->optional) { |
858 | 0 | state->missing = PR_TRUE; |
859 | 0 | state->place = afterEndOfContents; |
860 | 0 | } else { |
861 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
862 | 0 | state->top->status = decodeError; |
863 | 0 | } |
864 | 0 | } |
865 | 48 | } |
866 | | |
867 | | static unsigned long |
868 | | sec_asn1d_parse_length(sec_asn1d_state *state, |
869 | | const char *buf, unsigned long len) |
870 | 48 | { |
871 | 48 | unsigned char byte; |
872 | | |
873 | 48 | PORT_Assert(state->place == beforeLength); |
874 | | |
875 | 48 | if (len == 0) { |
876 | 0 | state->top->status = needBytes; |
877 | 0 | return 0; |
878 | 0 | } |
879 | | |
880 | | /* |
881 | | * The default/likely outcome. It may get adjusted below. |
882 | | */ |
883 | 48 | state->place = afterLength; |
884 | | |
885 | 48 | byte = (unsigned char)*buf; |
886 | | |
887 | 48 | if (LENGTH_IS_SHORT_FORM(byte)) { |
888 | 36 | state->contents_length = byte; |
889 | 36 | } else { |
890 | 12 | state->contents_length = 0; |
891 | 12 | state->pending = LONG_FORM_LENGTH(byte); |
892 | 12 | if (state->pending == 0) { |
893 | 0 | state->indefinite = PR_TRUE; |
894 | 12 | } else { |
895 | 12 | state->place = duringLength; |
896 | 12 | } |
897 | 12 | } |
898 | | |
899 | | /* If we're parsing an ANY, SKIP, or SAVE template, and |
900 | | ** the object being saved is definite length encoded and constructed, |
901 | | ** there's no point in decoding that construct's members. |
902 | | ** So, just forget it's constructed and treat it as primitive. |
903 | | ** (SAVE appears as an ANY at this point) |
904 | | */ |
905 | 48 | if (!state->indefinite && |
906 | 48 | (state->underlying_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP))) { |
907 | 8 | state->found_tag_modifiers &= ~SEC_ASN1_CONSTRUCTED; |
908 | 8 | } |
909 | | |
910 | 48 | return 1; |
911 | 48 | } |
912 | | |
913 | | static unsigned long |
914 | | sec_asn1d_parse_more_length(sec_asn1d_state *state, |
915 | | const char *buf, unsigned long len) |
916 | 12 | { |
917 | 12 | int count; |
918 | | |
919 | 12 | PORT_Assert(state->pending > 0); |
920 | 12 | PORT_Assert(state->place == duringLength); |
921 | | |
922 | 12 | if (len == 0) { |
923 | 0 | state->top->status = needBytes; |
924 | 0 | return 0; |
925 | 0 | } |
926 | | |
927 | 12 | count = 0; |
928 | | |
929 | 32 | while (len && state->pending) { |
930 | 20 | if (HIGH_BITS(state->contents_length, 9) != 0) { |
931 | | /* |
932 | | * The given full content length overflows our container; |
933 | | * just give up. |
934 | | */ |
935 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
936 | 0 | state->top->status = decodeError; |
937 | 0 | return 0; |
938 | 0 | } |
939 | | |
940 | 20 | state->contents_length <<= 8; |
941 | 20 | state->contents_length |= (unsigned char)buf[count++]; |
942 | | |
943 | 20 | len--; |
944 | 20 | state->pending--; |
945 | 20 | } |
946 | | |
947 | 12 | if (state->pending == 0) |
948 | 12 | state->place = afterLength; |
949 | | |
950 | 12 | return count; |
951 | 12 | } |
952 | | |
953 | | /* |
954 | | * Helper function for sec_asn1d_prepare_for_contents. |
955 | | * Checks that a value representing a number of bytes consumed can be |
956 | | * subtracted from a remaining length. If so, returns PR_TRUE. |
957 | | * Otherwise, sets the error SEC_ERROR_BAD_DER, indicates that there was a |
958 | | * decoding error in the given SEC_ASN1DecoderContext, and returns PR_FALSE. |
959 | | */ |
960 | | static PRBool |
961 | | sec_asn1d_check_and_subtract_length(unsigned long *remaining, |
962 | | unsigned long consumed, |
963 | | SEC_ASN1DecoderContext *cx) |
964 | 80 | { |
965 | 80 | PORT_Assert(remaining); |
966 | 80 | PORT_Assert(cx); |
967 | 80 | if (!remaining || !cx) { |
968 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
969 | 0 | cx->status = decodeError; |
970 | 0 | return PR_FALSE; |
971 | 0 | } |
972 | 80 | if (*remaining < consumed) { |
973 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
974 | 0 | cx->status = decodeError; |
975 | 0 | return PR_FALSE; |
976 | 0 | } |
977 | 80 | *remaining -= consumed; |
978 | 80 | return PR_TRUE; |
979 | 80 | } |
980 | | |
981 | | static void |
982 | | sec_asn1d_prepare_for_contents(sec_asn1d_state *state) |
983 | 48 | { |
984 | 48 | SECItem *item; |
985 | 48 | PLArenaPool *poolp; |
986 | 48 | unsigned long alloc_len; |
987 | 48 | sec_asn1d_state *parent; |
988 | | |
989 | | #ifdef DEBUG_ASN1D_STATES |
990 | | { |
991 | | printf("Found Length %lu %s\n", state->contents_length, |
992 | | state->indefinite ? "indefinite" : ""); |
993 | | } |
994 | | #endif |
995 | | |
996 | | /** |
997 | | * The maximum length for a child element should be constrained to the |
998 | | * length remaining in the first definite length element in the ancestor |
999 | | * stack. If there is no definite length element in the ancestor stack, |
1000 | | * there's nothing to constrain the length of the child, so there's no |
1001 | | * further processing necessary. |
1002 | | * |
1003 | | * It's necessary to walk the ancestor stack, because it's possible to have |
1004 | | * definite length children that are part of an indefinite length element, |
1005 | | * which is itself part of an indefinite length element, and which is |
1006 | | * ultimately part of a definite length element. A simple example of this |
1007 | | * would be the handling of constructed OCTET STRINGs in BER encoding. |
1008 | | * |
1009 | | * This algorithm finds the first definite length element in the ancestor |
1010 | | * stack, if any, and if so, ensures that the length of the child element |
1011 | | * is consistent with the number of bytes remaining in the constraining |
1012 | | * ancestor element (that is, after accounting for any other sibling |
1013 | | * elements that may have been read). |
1014 | | * |
1015 | | * It's slightly complicated by the need to account both for integer |
1016 | | * underflow and overflow, as well as ensure that for indefinite length |
1017 | | * encodings, there's also enough space for the End-of-Contents (EOC) |
1018 | | * octets (Tag = 0x00, Length = 0x00, or two bytes). |
1019 | | */ |
1020 | | |
1021 | | /* Determine the maximum length available for this element by finding the |
1022 | | * first definite length ancestor, if any. */ |
1023 | 48 | parent = sec_asn1d_get_enclosing_construct(state); |
1024 | 48 | while (parent && parent->indefinite) { |
1025 | 0 | parent = sec_asn1d_get_enclosing_construct(parent); |
1026 | 0 | } |
1027 | | /* If parent is null, state is either the outermost state / at the top of |
1028 | | * the stack, or the outermost state uses indefinite length encoding. In |
1029 | | * these cases, there's nothing external to constrain this element, so |
1030 | | * there's nothing to check. */ |
1031 | 48 | if (parent) { |
1032 | 40 | unsigned long remaining = parent->pending; |
1033 | 40 | parent = state; |
1034 | 40 | do { |
1035 | 40 | if (!sec_asn1d_check_and_subtract_length( |
1036 | 40 | &remaining, parent->consumed, state->top) || |
1037 | | /* If parent->indefinite is true, parent->contents_length is |
1038 | | * zero and this is a no-op. */ |
1039 | 40 | !sec_asn1d_check_and_subtract_length( |
1040 | 40 | &remaining, parent->contents_length, state->top) || |
1041 | | /* If parent->indefinite is true, then ensure there is enough |
1042 | | * space for an EOC tag of 2 bytes. */ |
1043 | 40 | (parent->indefinite && !sec_asn1d_check_and_subtract_length(&remaining, 2, state->top))) { |
1044 | | /* This element is larger than its enclosing element, which is |
1045 | | * invalid. */ |
1046 | 0 | return; |
1047 | 0 | } |
1048 | 40 | } while ((parent = sec_asn1d_get_enclosing_construct(parent)) && |
1049 | 40 | parent->indefinite); |
1050 | 40 | } |
1051 | | |
1052 | | /* |
1053 | | * XXX I cannot decide if this allocation should exclude the case |
1054 | | * where state->endofcontents is true -- figure it out! |
1055 | | */ |
1056 | 48 | if (state->allocate) { |
1057 | 0 | void *dest; |
1058 | |
|
1059 | 0 | PORT_Assert(state->dest == NULL); |
1060 | | /* |
1061 | | * We are handling a POINTER or a member of a GROUP, and need to |
1062 | | * allocate for the data structure. |
1063 | | */ |
1064 | 0 | dest = sec_asn1d_zalloc(state->top->their_pool, |
1065 | 0 | state->theTemplate->size); |
1066 | 0 | if (dest == NULL) { |
1067 | 0 | state->top->status = decodeError; |
1068 | 0 | return; |
1069 | 0 | } |
1070 | 0 | state->dest = (char *)dest + state->theTemplate->offset; |
1071 | | |
1072 | | /* |
1073 | | * For a member of a GROUP, our parent will later put the |
1074 | | * pointer wherever it belongs. But for a POINTER, we need |
1075 | | * to record the destination now, in case notify or filter |
1076 | | * procs need access to it -- they cannot find it otherwise, |
1077 | | * until it is too late (for one-pass processing). |
1078 | | */ |
1079 | 0 | if (state->parent->place == afterPointer) { |
1080 | 0 | void **placep; |
1081 | |
|
1082 | 0 | placep = state->parent->dest; |
1083 | 0 | *placep = dest; |
1084 | 0 | } |
1085 | 0 | } |
1086 | | |
1087 | | /* |
1088 | | * Remember, length may be indefinite here! In that case, |
1089 | | * both contents_length and pending will be zero. |
1090 | | */ |
1091 | 48 | state->pending = state->contents_length; |
1092 | | |
1093 | | /* |
1094 | | * An EXPLICIT is nothing but an outer header, which we have |
1095 | | * already parsed and accepted. Now we need to do the inner |
1096 | | * header and its contents. |
1097 | | */ |
1098 | 48 | if (state->explicit) { |
1099 | 0 | state->place = afterExplicit; |
1100 | 0 | state = sec_asn1d_push_state(state->top, |
1101 | 0 | SEC_ASN1GetSubtemplate(state->theTemplate, |
1102 | 0 | state->dest, |
1103 | 0 | PR_FALSE), |
1104 | 0 | state->dest, PR_TRUE); |
1105 | 0 | if (state != NULL) { |
1106 | 0 | (void)sec_asn1d_init_state_based_on_template(state); |
1107 | 0 | } |
1108 | 0 | return; |
1109 | 0 | } |
1110 | | |
1111 | | /* |
1112 | | * For GROUP (SET OF, SEQUENCE OF), even if we know the length here |
1113 | | * we cannot tell how many items we will end up with ... so push a |
1114 | | * state that can keep track of "children" (the individual members |
1115 | | * of the group; we will allocate as we go and put them all together |
1116 | | * at the end. |
1117 | | */ |
1118 | 48 | if (state->underlying_kind & SEC_ASN1_GROUP) { |
1119 | | /* XXX If this assertion holds (should be able to confirm it via |
1120 | | * inspection, too) then move this code into the switch statement |
1121 | | * below under cases SET_OF and SEQUENCE_OF; it will be cleaner. |
1122 | | */ |
1123 | 0 | PORT_Assert(state->underlying_kind == SEC_ASN1_SET_OF || state->underlying_kind == SEC_ASN1_SEQUENCE_OF || state->underlying_kind == (SEC_ASN1_SET_OF | SEC_ASN1_DYNAMIC) || state->underlying_kind == (SEC_ASN1_SEQUENCE_OF | SEC_ASN1_DYNAMIC)); |
1124 | 0 | if (state->contents_length != 0 || state->indefinite) { |
1125 | 0 | const SEC_ASN1Template *subt; |
1126 | |
|
1127 | 0 | state->place = duringGroup; |
1128 | 0 | subt = SEC_ASN1GetSubtemplate(state->theTemplate, state->dest, |
1129 | 0 | PR_FALSE); |
1130 | 0 | state = sec_asn1d_push_state(state->top, subt, NULL, PR_TRUE); |
1131 | 0 | if (state != NULL) { |
1132 | 0 | if (!state->top->filter_only) |
1133 | 0 | state->allocate = PR_TRUE; /* XXX propogate this? */ |
1134 | | /* |
1135 | | * Do the "before" field notification for next in group. |
1136 | | */ |
1137 | 0 | sec_asn1d_notify_before(state->top, state->dest, state->depth); |
1138 | 0 | (void)sec_asn1d_init_state_based_on_template(state); |
1139 | 0 | } |
1140 | 0 | } else { |
1141 | | /* |
1142 | | * A group of zero; we are done. |
1143 | | * Set state to afterGroup and let that code plant the NULL. |
1144 | | */ |
1145 | 0 | state->place = afterGroup; |
1146 | 0 | } |
1147 | 0 | return; |
1148 | 0 | } |
1149 | | |
1150 | 48 | switch (state->underlying_kind) { |
1151 | 16 | case SEC_ASN1_SEQUENCE: |
1152 | | /* |
1153 | | * We need to push a child to handle the individual fields. |
1154 | | */ |
1155 | 16 | state->place = duringSequence; |
1156 | 16 | state = sec_asn1d_push_state(state->top, state->theTemplate + 1, |
1157 | 16 | state->dest, PR_TRUE); |
1158 | 16 | if (state != NULL) { |
1159 | | /* |
1160 | | * Do the "before" field notification. |
1161 | | */ |
1162 | 16 | sec_asn1d_notify_before(state->top, state->dest, state->depth); |
1163 | 16 | (void)sec_asn1d_init_state_based_on_template(state); |
1164 | 16 | } |
1165 | 16 | break; |
1166 | | |
1167 | 0 | case SEC_ASN1_SET: /* XXX SET is not really implemented */ |
1168 | | /* |
1169 | | * XXX A plain SET requires special handling; scanning of a |
1170 | | * template to see where a field should go (because by definition, |
1171 | | * they are not in any particular order, and you have to look at |
1172 | | * each tag to disambiguate what the field is). We may never |
1173 | | * implement this because in practice, it seems to be unused. |
1174 | | */ |
1175 | 0 | PORT_Assert(0); |
1176 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); /* XXX */ |
1177 | 0 | state->top->status = decodeError; |
1178 | 0 | break; |
1179 | | |
1180 | 0 | case SEC_ASN1_NULL: |
1181 | | /* |
1182 | | * The NULL type, by definition, is "nothing", content length of zero. |
1183 | | * An indefinite-length encoding is not alloweed. |
1184 | | */ |
1185 | 0 | if (state->contents_length || state->indefinite) { |
1186 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1187 | 0 | state->top->status = decodeError; |
1188 | 0 | break; |
1189 | 0 | } |
1190 | 0 | if (state->dest != NULL) { |
1191 | 0 | item = (SECItem *)(state->dest); |
1192 | 0 | item->data = NULL; |
1193 | 0 | item->len = 0; |
1194 | 0 | } |
1195 | 0 | state->place = afterEndOfContents; |
1196 | 0 | break; |
1197 | | |
1198 | 0 | case SEC_ASN1_BMP_STRING: |
1199 | | /* Error if length is not divisable by 2 */ |
1200 | 0 | if (state->contents_length % 2) { |
1201 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1202 | 0 | state->top->status = decodeError; |
1203 | 0 | break; |
1204 | 0 | } |
1205 | | /* otherwise, handle as other string types */ |
1206 | 0 | goto regular_string_type; |
1207 | | |
1208 | 0 | case SEC_ASN1_UNIVERSAL_STRING: |
1209 | | /* Error if length is not divisable by 4 */ |
1210 | 0 | if (state->contents_length % 4) { |
1211 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1212 | 0 | state->top->status = decodeError; |
1213 | 0 | break; |
1214 | 0 | } |
1215 | | /* otherwise, handle as other string types */ |
1216 | 0 | goto regular_string_type; |
1217 | | |
1218 | 0 | case SEC_ASN1_SKIP: |
1219 | 8 | case SEC_ASN1_ANY: |
1220 | 8 | case SEC_ASN1_ANY_CONTENTS: |
1221 | | /* |
1222 | | * These are not (necessarily) strings, but they need nearly |
1223 | | * identical handling (especially when we need to deal with |
1224 | | * constructed sub-pieces), so we pretend they are. |
1225 | | */ |
1226 | | /* fallthru */ |
1227 | 8 | regular_string_type: |
1228 | 8 | case SEC_ASN1_BIT_STRING: |
1229 | 8 | case SEC_ASN1_IA5_STRING: |
1230 | 16 | case SEC_ASN1_OCTET_STRING: |
1231 | 16 | case SEC_ASN1_PRINTABLE_STRING: |
1232 | 16 | case SEC_ASN1_T61_STRING: |
1233 | 16 | case SEC_ASN1_UTC_TIME: |
1234 | 16 | case SEC_ASN1_UTF8_STRING: |
1235 | 16 | case SEC_ASN1_VISIBLE_STRING: |
1236 | | /* |
1237 | | * We are allocating for a primitive or a constructed string. |
1238 | | * If it is a constructed string, it may also be indefinite-length. |
1239 | | * If it is primitive, the length can (legally) be zero. |
1240 | | * Our first order of business is to allocate the memory for |
1241 | | * the string, if we can (if we know the length). |
1242 | | */ |
1243 | 16 | item = (SECItem *)(state->dest); |
1244 | | |
1245 | | /* |
1246 | | * If the item is a definite-length constructed string, then |
1247 | | * the contents_length is actually larger than what we need |
1248 | | * (because it also counts each intermediate header which we |
1249 | | * will be throwing away as we go), but it is a perfectly good |
1250 | | * upper bound that we just allocate anyway, and then concat |
1251 | | * as we go; we end up wasting a few extra bytes but save a |
1252 | | * whole other copy. |
1253 | | */ |
1254 | 16 | alloc_len = state->contents_length; |
1255 | 16 | poolp = NULL; /* quiet compiler warnings about unused... */ |
1256 | | |
1257 | 16 | if (item == NULL || state->top->filter_only) { |
1258 | 0 | if (item != NULL) { |
1259 | 0 | item->data = NULL; |
1260 | 0 | item->len = 0; |
1261 | 0 | } |
1262 | 0 | alloc_len = 0; |
1263 | 16 | } else if (state->substring) { |
1264 | | /* |
1265 | | * If we are a substring of a constructed string, then we may |
1266 | | * not have to allocate anything (because our parent, the |
1267 | | * actual constructed string, did it for us). If we are a |
1268 | | * substring and we *do* have to allocate, that means our |
1269 | | * parent is an indefinite-length, so we allocate from our pool; |
1270 | | * later our parent will copy our string into the aggregated |
1271 | | * whole and free our pool allocation. |
1272 | | */ |
1273 | 0 | if (item->data == NULL) { |
1274 | 0 | PORT_Assert(item->len == 0); |
1275 | 0 | poolp = state->top->our_pool; |
1276 | 0 | } else { |
1277 | 0 | alloc_len = 0; |
1278 | 0 | } |
1279 | 16 | } else { |
1280 | 16 | item->len = 0; |
1281 | 16 | item->data = NULL; |
1282 | 16 | poolp = state->top->their_pool; |
1283 | 16 | } |
1284 | | |
1285 | 16 | if (alloc_len || ((!state->indefinite) && (state->subitems_head != NULL))) { |
1286 | 16 | struct subitem *subitem; |
1287 | 16 | int len; |
1288 | | |
1289 | 16 | PORT_Assert(item); |
1290 | 16 | if (!item) { |
1291 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1292 | 0 | state->top->status = decodeError; |
1293 | 0 | return; |
1294 | 0 | } |
1295 | 16 | PORT_Assert(item->len == 0 && item->data == NULL); |
1296 | | /* |
1297 | | * Check for and handle an ANY which has stashed aside the |
1298 | | * header (identifier and length) bytes for us to include |
1299 | | * in the saved contents. |
1300 | | */ |
1301 | 16 | if (state->subitems_head != NULL) { |
1302 | 8 | PORT_Assert(state->underlying_kind == SEC_ASN1_ANY); |
1303 | 8 | for (subitem = state->subitems_head; |
1304 | 24 | subitem != NULL; subitem = subitem->next) |
1305 | 16 | alloc_len += subitem->len; |
1306 | 8 | } |
1307 | | |
1308 | 16 | if (state->top->max_element_size > 0 && |
1309 | 16 | alloc_len > state->top->max_element_size) { |
1310 | 0 | PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
1311 | 0 | state->top->status = decodeError; |
1312 | 0 | return; |
1313 | 0 | } |
1314 | | |
1315 | 16 | item->data = (unsigned char *)sec_asn1d_zalloc(poolp, alloc_len); |
1316 | 16 | if (item->data == NULL) { |
1317 | 0 | state->top->status = decodeError; |
1318 | 0 | break; |
1319 | 0 | } |
1320 | | |
1321 | 16 | len = 0; |
1322 | 16 | for (subitem = state->subitems_head; |
1323 | 32 | subitem != NULL; subitem = subitem->next) { |
1324 | 16 | PORT_Memcpy(item->data + len, subitem->data, subitem->len); |
1325 | 16 | len += subitem->len; |
1326 | 16 | } |
1327 | 16 | item->len = len; |
1328 | | |
1329 | | /* |
1330 | | * Because we use arenas and have a mark set, we later free |
1331 | | * everything we have allocated, so this does *not* present |
1332 | | * a memory leak (it is just temporarily left dangling). |
1333 | | */ |
1334 | 16 | state->subitems_head = state->subitems_tail = NULL; |
1335 | 16 | } |
1336 | | |
1337 | 16 | if (state->contents_length == 0 && (!state->indefinite)) { |
1338 | | /* |
1339 | | * A zero-length simple or constructed string; we are done. |
1340 | | */ |
1341 | 4 | state->place = afterEndOfContents; |
1342 | 12 | } else if (state->found_tag_modifiers & SEC_ASN1_CONSTRUCTED) { |
1343 | 0 | const SEC_ASN1Template *sub; |
1344 | |
|
1345 | 0 | switch (state->underlying_kind) { |
1346 | 0 | case SEC_ASN1_ANY: |
1347 | 0 | case SEC_ASN1_ANY_CONTENTS: |
1348 | 0 | sub = SEC_AnyTemplate; |
1349 | 0 | break; |
1350 | 0 | case SEC_ASN1_BIT_STRING: |
1351 | 0 | sub = SEC_BitStringTemplate; |
1352 | 0 | break; |
1353 | 0 | case SEC_ASN1_BMP_STRING: |
1354 | 0 | sub = SEC_BMPStringTemplate; |
1355 | 0 | break; |
1356 | 0 | case SEC_ASN1_GENERALIZED_TIME: |
1357 | 0 | sub = SEC_GeneralizedTimeTemplate; |
1358 | 0 | break; |
1359 | 0 | case SEC_ASN1_IA5_STRING: |
1360 | 0 | sub = SEC_IA5StringTemplate; |
1361 | 0 | break; |
1362 | 0 | case SEC_ASN1_OCTET_STRING: |
1363 | 0 | sub = SEC_OctetStringTemplate; |
1364 | 0 | break; |
1365 | 0 | case SEC_ASN1_PRINTABLE_STRING: |
1366 | 0 | sub = SEC_PrintableStringTemplate; |
1367 | 0 | break; |
1368 | 0 | case SEC_ASN1_T61_STRING: |
1369 | 0 | sub = SEC_T61StringTemplate; |
1370 | 0 | break; |
1371 | 0 | case SEC_ASN1_UNIVERSAL_STRING: |
1372 | 0 | sub = SEC_UniversalStringTemplate; |
1373 | 0 | break; |
1374 | 0 | case SEC_ASN1_UTC_TIME: |
1375 | 0 | sub = SEC_UTCTimeTemplate; |
1376 | 0 | break; |
1377 | 0 | case SEC_ASN1_UTF8_STRING: |
1378 | 0 | sub = SEC_UTF8StringTemplate; |
1379 | 0 | break; |
1380 | 0 | case SEC_ASN1_VISIBLE_STRING: |
1381 | 0 | sub = SEC_VisibleStringTemplate; |
1382 | 0 | break; |
1383 | 0 | case SEC_ASN1_SKIP: |
1384 | 0 | sub = SEC_SkipTemplate; |
1385 | 0 | break; |
1386 | 0 | default: /* redundant given outer switch cases, but */ |
1387 | 0 | PORT_Assert(0); /* the compiler does not seem to know that, */ |
1388 | 0 | sub = NULL; /* so just do enough to quiet it. */ |
1389 | 0 | break; |
1390 | 0 | } |
1391 | | |
1392 | 0 | state->place = duringConstructedString; |
1393 | 0 | state = sec_asn1d_push_state(state->top, sub, item, PR_TRUE); |
1394 | 0 | if (state != NULL) { |
1395 | 0 | state->substring = PR_TRUE; /* XXX propogate? */ |
1396 | 0 | (void)sec_asn1d_init_state_based_on_template(state); |
1397 | 0 | } |
1398 | 12 | } else if (state->indefinite) { |
1399 | | /* |
1400 | | * An indefinite-length string *must* be constructed! |
1401 | | */ |
1402 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1403 | 0 | state->top->status = decodeError; |
1404 | 12 | } else { |
1405 | | /* |
1406 | | * A non-zero-length simple string. |
1407 | | */ |
1408 | 12 | if (state->underlying_kind == SEC_ASN1_BIT_STRING) |
1409 | 0 | state->place = beforeBitString; |
1410 | 12 | else |
1411 | 12 | state->place = duringLeaf; |
1412 | 12 | } |
1413 | 16 | break; |
1414 | | |
1415 | 16 | default: |
1416 | | /* |
1417 | | * We are allocating for a simple leaf item. |
1418 | | */ |
1419 | 16 | if (state->contents_length) { |
1420 | 16 | if (state->dest != NULL) { |
1421 | 16 | item = (SECItem *)(state->dest); |
1422 | 16 | item->len = 0; |
1423 | 16 | if (state->top->max_element_size > 0 && |
1424 | 16 | state->contents_length > state->top->max_element_size) { |
1425 | 0 | PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
1426 | 0 | state->top->status = decodeError; |
1427 | 0 | return; |
1428 | 0 | } |
1429 | | |
1430 | 16 | if (state->top->filter_only) { |
1431 | 0 | item->data = NULL; |
1432 | 16 | } else { |
1433 | 16 | item->data = (unsigned char *) |
1434 | 16 | sec_asn1d_zalloc(state->top->their_pool, |
1435 | 16 | state->contents_length); |
1436 | 16 | if (item->data == NULL) { |
1437 | 0 | state->top->status = decodeError; |
1438 | 0 | return; |
1439 | 0 | } |
1440 | 16 | } |
1441 | 16 | } |
1442 | 16 | state->place = duringLeaf; |
1443 | 16 | } else { |
1444 | | /* |
1445 | | * An indefinite-length or zero-length item is not allowed. |
1446 | | * (All legal cases of such were handled above.) |
1447 | | */ |
1448 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1449 | 0 | state->top->status = decodeError; |
1450 | 0 | } |
1451 | 48 | } |
1452 | 48 | } |
1453 | | |
1454 | | static void |
1455 | | sec_asn1d_free_child(sec_asn1d_state *state, PRBool error) |
1456 | 96 | { |
1457 | 96 | if (state->child != NULL) { |
1458 | 24 | PORT_Assert(error || state->child->consumed == 0); |
1459 | 24 | PORT_Assert(state->our_mark != NULL); |
1460 | 24 | PORT_ArenaZRelease(state->top->our_pool, state->our_mark); |
1461 | 24 | if (error && state->top->their_pool == NULL) { |
1462 | | /* |
1463 | | * XXX We need to free anything allocated. |
1464 | | * At this point, we failed in the middle of decoding. But we |
1465 | | * can't free the data we previously allocated with PR_Malloc |
1466 | | * unless we keep track of every pointer. So instead we have a |
1467 | | * memory leak when decoding fails half-way, unless an arena is |
1468 | | * used. See bug 95311 . |
1469 | | */ |
1470 | 0 | } |
1471 | 24 | state->child = NULL; |
1472 | 24 | state->our_mark = NULL; |
1473 | 72 | } else { |
1474 | | /* |
1475 | | * It is important that we do not leave a mark unreleased/unmarked. |
1476 | | * But I do not think we should ever have one set in this case, only |
1477 | | * if we had a child (handled above). So check for that. If this |
1478 | | * assertion should ever get hit, then we probably need to add code |
1479 | | * here to release back to our_mark (and then set our_mark to NULL). |
1480 | | */ |
1481 | 72 | PORT_Assert(state->our_mark == NULL); |
1482 | 72 | } |
1483 | 96 | state->place = beforeEndOfContents; |
1484 | 96 | } |
1485 | | |
1486 | | /* We have just saved an entire encoded ASN.1 object (type) for a SAVE |
1487 | | ** template, and now in the next template, we are going to decode that |
1488 | | ** saved data by calling SEC_ASN1DecoderUpdate recursively. |
1489 | | ** If that recursive call fails with needBytes, it is a fatal error, |
1490 | | ** because the encoded object should have been complete. |
1491 | | ** If that recursive call fails with decodeError, it will have already |
1492 | | ** cleaned up the state stack, so we must bail out quickly. |
1493 | | ** |
1494 | | ** These checks of the status returned by the recursive call are now |
1495 | | ** done in the caller of this function, immediately after it returns. |
1496 | | */ |
1497 | | static void |
1498 | | sec_asn1d_reuse_encoding(sec_asn1d_state *state) |
1499 | 0 | { |
1500 | 0 | sec_asn1d_state *child; |
1501 | 0 | unsigned long consumed; |
1502 | 0 | SECItem *item; |
1503 | 0 | void *dest; |
1504 | |
|
1505 | 0 | child = state->child; |
1506 | 0 | PORT_Assert(child != NULL); |
1507 | |
|
1508 | 0 | consumed = child->consumed; |
1509 | 0 | child->consumed = 0; |
1510 | |
|
1511 | 0 | item = (SECItem *)(state->dest); |
1512 | 0 | PORT_Assert(item != NULL); |
1513 | |
|
1514 | 0 | PORT_Assert(item->len == consumed); |
1515 | | |
1516 | | /* |
1517 | | * Free any grandchild. |
1518 | | */ |
1519 | 0 | sec_asn1d_free_child(child, PR_FALSE); |
1520 | | |
1521 | | /* |
1522 | | * Notify after the SAVE field. |
1523 | | */ |
1524 | 0 | sec_asn1d_notify_after(state->top, state->dest, state->depth); |
1525 | | |
1526 | | /* |
1527 | | * Adjust to get new dest and move forward. |
1528 | | */ |
1529 | 0 | dest = (char *)state->dest - state->theTemplate->offset; |
1530 | 0 | state->theTemplate++; |
1531 | 0 | child->dest = (char *)dest + state->theTemplate->offset; |
1532 | 0 | child->theTemplate = state->theTemplate; |
1533 | | |
1534 | | /* |
1535 | | * Notify before the "real" field. |
1536 | | */ |
1537 | 0 | PORT_Assert(state->depth == child->depth); |
1538 | 0 | sec_asn1d_notify_before(state->top, child->dest, child->depth); |
1539 | | |
1540 | | /* |
1541 | | * This will tell DecoderUpdate to return when it is done. |
1542 | | */ |
1543 | 0 | state->place = afterSaveEncoding; |
1544 | | |
1545 | | /* |
1546 | | * We already have a child; "push" it by making it current. |
1547 | | */ |
1548 | 0 | state->top->current = child; |
1549 | | |
1550 | | /* |
1551 | | * And initialize it so it is ready to parse. |
1552 | | */ |
1553 | 0 | (void)sec_asn1d_init_state_based_on_template(child); |
1554 | | |
1555 | | /* |
1556 | | * Now parse that out of our data. |
1557 | | */ |
1558 | 0 | if (SEC_ASN1DecoderUpdate(state->top, |
1559 | 0 | (char *)item->data, item->len) != SECSuccess) |
1560 | 0 | return; |
1561 | 0 | if (state->top->status == needBytes) { |
1562 | 0 | return; |
1563 | 0 | } |
1564 | | |
1565 | 0 | PORT_Assert(state->top->current == state); |
1566 | 0 | PORT_Assert(state->child == child); |
1567 | | |
1568 | | /* |
1569 | | * That should have consumed what we consumed before. |
1570 | | */ |
1571 | 0 | PORT_Assert(consumed == child->consumed); |
1572 | 0 | child->consumed = 0; |
1573 | | |
1574 | | /* |
1575 | | * Done. |
1576 | | */ |
1577 | 0 | state->consumed += consumed; |
1578 | 0 | child->place = notInUse; |
1579 | 0 | state->place = afterEndOfContents; |
1580 | 0 | } |
1581 | | |
1582 | | static unsigned long |
1583 | | sec_asn1d_parse_leaf(sec_asn1d_state *state, |
1584 | | const char *buf, unsigned long len) |
1585 | 28 | { |
1586 | 28 | SECItem *item; |
1587 | 28 | unsigned long bufLen; |
1588 | | |
1589 | 28 | if (len == 0) { |
1590 | 0 | state->top->status = needBytes; |
1591 | 0 | return 0; |
1592 | 0 | } |
1593 | | |
1594 | 28 | if (state->pending < len) |
1595 | 20 | len = state->pending; |
1596 | | |
1597 | 28 | bufLen = len; |
1598 | | |
1599 | 28 | item = (SECItem *)(state->dest); |
1600 | 28 | if (item != NULL && item->data != NULL) { |
1601 | 28 | unsigned long offset; |
1602 | | /* Strip leading zeroes when target is unsigned integer */ |
1603 | 28 | if (state->underlying_kind == SEC_ASN1_INTEGER && /* INTEGER */ |
1604 | 28 | item->len == 0 && /* MSB */ |
1605 | 28 | item->type == siUnsignedInteger) /* unsigned */ |
1606 | 0 | { |
1607 | 0 | while (len > 1 && buf[0] == 0) { /* leading 0 */ |
1608 | 0 | buf++; |
1609 | 0 | len--; |
1610 | 0 | } |
1611 | 0 | } |
1612 | 28 | offset = item->len; |
1613 | 28 | if (state->underlying_kind == SEC_ASN1_BIT_STRING) { |
1614 | | // The previous bit string must have no unused bits. |
1615 | 0 | if (item->len & 0x7) { |
1616 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1617 | 0 | state->top->status = decodeError; |
1618 | 0 | return 0; |
1619 | 0 | } |
1620 | | // If this is a bit string, the length is bits, not bytes. |
1621 | 0 | offset = item->len >> 3; |
1622 | 0 | } |
1623 | 28 | if (state->underlying_kind == SEC_ASN1_BIT_STRING) { |
1624 | 0 | unsigned long len_in_bits; |
1625 | | // Protect against overflow during the bytes-to-bits conversion. |
1626 | 0 | if (len >= (ULONG_MAX >> 3) + 1) { |
1627 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1628 | 0 | state->top->status = decodeError; |
1629 | 0 | return 0; |
1630 | 0 | } |
1631 | 0 | len_in_bits = (len << 3) - state->bit_string_unused_bits; |
1632 | | // Protect against overflow when computing the total length in bits. |
1633 | 0 | if (UINT_MAX - item->len < len_in_bits) { |
1634 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1635 | 0 | state->top->status = decodeError; |
1636 | 0 | return 0; |
1637 | 0 | } |
1638 | 0 | item->len += len_in_bits; |
1639 | 28 | } else { |
1640 | 28 | if (UINT_MAX - item->len < len) { |
1641 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1642 | 0 | state->top->status = decodeError; |
1643 | 0 | return 0; |
1644 | 0 | } |
1645 | 28 | item->len += len; |
1646 | 28 | } |
1647 | 28 | PORT_Memcpy(item->data + offset, buf, len); |
1648 | 28 | } |
1649 | 28 | state->pending -= bufLen; |
1650 | 28 | if (state->pending == 0) |
1651 | 28 | state->place = beforeEndOfContents; |
1652 | | |
1653 | 28 | return bufLen; |
1654 | 28 | } |
1655 | | |
1656 | | static unsigned long |
1657 | | sec_asn1d_parse_bit_string(sec_asn1d_state *state, |
1658 | | const char *buf, unsigned long len) |
1659 | 0 | { |
1660 | 0 | unsigned char byte; |
1661 | | |
1662 | | /*PORT_Assert (state->pending > 0); */ |
1663 | 0 | PORT_Assert(state->place == beforeBitString); |
1664 | |
|
1665 | 0 | if (state->pending == 0) { |
1666 | 0 | if (state->dest != NULL) { |
1667 | 0 | SECItem *item = (SECItem *)(state->dest); |
1668 | 0 | item->data = NULL; |
1669 | 0 | item->len = 0; |
1670 | 0 | state->place = beforeEndOfContents; |
1671 | 0 | return 0; |
1672 | 0 | } |
1673 | 0 | } |
1674 | | |
1675 | 0 | if (len == 0) { |
1676 | 0 | state->top->status = needBytes; |
1677 | 0 | return 0; |
1678 | 0 | } |
1679 | | |
1680 | 0 | byte = (unsigned char)*buf; |
1681 | 0 | if (byte > 7) { |
1682 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1683 | 0 | state->top->status = decodeError; |
1684 | 0 | return 0; |
1685 | 0 | } |
1686 | | |
1687 | 0 | state->bit_string_unused_bits = byte; |
1688 | 0 | state->place = duringBitString; |
1689 | 0 | state->pending -= 1; |
1690 | |
|
1691 | 0 | return 1; |
1692 | 0 | } |
1693 | | |
1694 | | static unsigned long |
1695 | | sec_asn1d_parse_more_bit_string(sec_asn1d_state *state, |
1696 | | const char *buf, unsigned long len) |
1697 | 0 | { |
1698 | 0 | PORT_Assert(state->place == duringBitString); |
1699 | 0 | if (state->pending == 0) { |
1700 | | /* An empty bit string with some unused bits is invalid. */ |
1701 | 0 | if (state->bit_string_unused_bits) { |
1702 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1703 | 0 | state->top->status = decodeError; |
1704 | 0 | } else { |
1705 | | /* An empty bit string with no unused bits is OK. */ |
1706 | 0 | state->place = beforeEndOfContents; |
1707 | 0 | } |
1708 | 0 | return 0; |
1709 | 0 | } |
1710 | | |
1711 | 0 | len = sec_asn1d_parse_leaf(state, buf, len); |
1712 | 0 | return len; |
1713 | 0 | } |
1714 | | |
1715 | | /* |
1716 | | * XXX All callers should be looking at return value to detect |
1717 | | * out-of-memory errors (and stop!). |
1718 | | */ |
1719 | | static struct subitem * |
1720 | | sec_asn1d_add_to_subitems(sec_asn1d_state *state, |
1721 | | const void *data, unsigned long len, |
1722 | | PRBool copy_data) |
1723 | 16 | { |
1724 | 16 | struct subitem *thing; |
1725 | | |
1726 | 16 | thing = (struct subitem *)sec_asn1d_zalloc(state->top->our_pool, |
1727 | 16 | sizeof(struct subitem)); |
1728 | 16 | if (thing == NULL) { |
1729 | 0 | state->top->status = decodeError; |
1730 | 0 | return NULL; |
1731 | 0 | } |
1732 | | |
1733 | 16 | if (copy_data) { |
1734 | 16 | void *copy; |
1735 | 16 | copy = sec_asn1d_alloc(state->top->our_pool, len); |
1736 | 16 | if (copy == NULL) { |
1737 | 0 | state->top->status = decodeError; |
1738 | 0 | if (!state->top->our_pool) |
1739 | 0 | PORT_Free(thing); |
1740 | 0 | return NULL; |
1741 | 0 | } |
1742 | 16 | PORT_Memcpy(copy, data, len); |
1743 | 16 | thing->data = copy; |
1744 | 16 | } else { |
1745 | 0 | thing->data = data; |
1746 | 0 | } |
1747 | 16 | thing->len = len; |
1748 | 16 | thing->next = NULL; |
1749 | | |
1750 | 16 | if (state->subitems_head == NULL) { |
1751 | 8 | PORT_Assert(state->subitems_tail == NULL); |
1752 | 8 | state->subitems_head = state->subitems_tail = thing; |
1753 | 8 | } else { |
1754 | 8 | state->subitems_tail->next = thing; |
1755 | 8 | state->subitems_tail = thing; |
1756 | 8 | } |
1757 | | |
1758 | 16 | return thing; |
1759 | 16 | } |
1760 | | |
1761 | | static void |
1762 | | sec_asn1d_record_any_header(sec_asn1d_state *state, |
1763 | | const char *buf, |
1764 | | unsigned long len) |
1765 | 16 | { |
1766 | 16 | SECItem *item; |
1767 | | |
1768 | 16 | item = (SECItem *)(state->dest); |
1769 | 16 | if (item != NULL && item->data != NULL) { |
1770 | 0 | PORT_Assert(state->substring); |
1771 | 0 | PORT_Memcpy(item->data + item->len, buf, len); |
1772 | 0 | item->len += len; |
1773 | 16 | } else { |
1774 | 16 | sec_asn1d_add_to_subitems(state, buf, len, PR_TRUE); |
1775 | 16 | } |
1776 | 16 | } |
1777 | | |
1778 | | /* |
1779 | | * We are moving along through the substrings of a constructed string, |
1780 | | * and have just finished parsing one -- we need to save our child data |
1781 | | * (if the child was not already writing directly into the destination) |
1782 | | * and then move forward by one. |
1783 | | * |
1784 | | * We also have to detect when we are done: |
1785 | | * - a definite-length encoding stops when our pending value hits 0 |
1786 | | * - an indefinite-length encoding stops when our child is empty |
1787 | | * (which means it was the end-of-contents octets) |
1788 | | */ |
1789 | | static void |
1790 | | sec_asn1d_next_substring(sec_asn1d_state *state) |
1791 | 0 | { |
1792 | 0 | sec_asn1d_state *child; |
1793 | 0 | SECItem *item; |
1794 | 0 | unsigned long child_consumed; |
1795 | 0 | PRBool done; |
1796 | |
|
1797 | 0 | PORT_Assert(state->place == duringConstructedString); |
1798 | 0 | PORT_Assert(state->child != NULL); |
1799 | |
|
1800 | 0 | child = state->child; |
1801 | |
|
1802 | 0 | child_consumed = child->consumed; |
1803 | 0 | child->consumed = 0; |
1804 | 0 | state->consumed += child_consumed; |
1805 | |
|
1806 | 0 | done = PR_FALSE; |
1807 | |
|
1808 | 0 | if (state->pending) { |
1809 | 0 | PORT_Assert(!state->indefinite); |
1810 | 0 | if (child_consumed > state->pending) { |
1811 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
1812 | 0 | state->top->status = decodeError; |
1813 | 0 | return; |
1814 | 0 | } |
1815 | | |
1816 | 0 | state->pending -= child_consumed; |
1817 | 0 | if (state->pending == 0) |
1818 | 0 | done = PR_TRUE; |
1819 | 0 | } else { |
1820 | 0 | PRBool preallocatedString; |
1821 | 0 | sec_asn1d_state *temp_state; |
1822 | 0 | PORT_Assert(state->indefinite); |
1823 | |
|
1824 | 0 | item = (SECItem *)(child->dest); |
1825 | | |
1826 | | /** |
1827 | | * At this point, there's three states at play: |
1828 | | * child: The element that was just parsed |
1829 | | * state: The currently processed element |
1830 | | * 'parent' (aka state->parent): The enclosing construct |
1831 | | * of state, or NULL if this is the top-most element. |
1832 | | * |
1833 | | * This state handles both substrings of a constructed string AND |
1834 | | * child elements of items whose template type was that of |
1835 | | * SEC_ASN1_ANY, SEC_ASN1_SAVE, SEC_ASN1_ANY_CONTENTS, SEC_ASN1_SKIP |
1836 | | * template, as described in sec_asn1d_prepare_for_contents. For |
1837 | | * brevity, these will be referred to as 'string' and 'any' types. |
1838 | | * |
1839 | | * This leads to the following possibilities: |
1840 | | * 1: This element is an indefinite length string, part of a |
1841 | | * definite length string. |
1842 | | * 2: This element is an indefinite length string, part of an |
1843 | | * indefinite length string. |
1844 | | * 3: This element is an indefinite length any, part of a |
1845 | | * definite length any. |
1846 | | * 4: This element is an indefinite length any, part of an |
1847 | | * indefinite length any. |
1848 | | * 5: This element is an indefinite length any and does not |
1849 | | * meet any of the above criteria. Note that this would include |
1850 | | * an indefinite length string type matching an indefinite |
1851 | | * length any template. |
1852 | | * |
1853 | | * In Cases #1 and #3, the definite length 'parent' element will |
1854 | | * have allocated state->dest based on the parent elements definite |
1855 | | * size. During the processing of 'child', sec_asn1d_parse_leaf will |
1856 | | * have copied the (string, any) data directly into the offset of |
1857 | | * dest, as appropriate, so there's no need for this class to still |
1858 | | * store the child - it's already been processed. |
1859 | | * |
1860 | | * In Cases #2 and #4, dest will be set to the parent element's dest, |
1861 | | * but dest->data will not have been allocated yet, due to the |
1862 | | * indefinite length encoding. In this situation, it's necessary to |
1863 | | * hold onto child (and all other children) until the EOC, at which |
1864 | | * point, it becomes possible to compute 'state's overall length. Once |
1865 | | * 'state' has a computed length, this can then be fed to 'parent' (via |
1866 | | * this state), and then 'parent' can similarly compute the length of |
1867 | | * all of its children up to the EOC, which will ultimately transit to |
1868 | | * sec_asn1d_concat_substrings, determine the overall size needed, |
1869 | | * allocate, and copy the contents (of all of parent's children, which |
1870 | | * would include 'state', just as 'state' will have copied all of its |
1871 | | * children via sec_asn1d_concat_substrings) |
1872 | | * |
1873 | | * The final case, Case #5, will manifest in that item->data and |
1874 | | * item->len will be NULL/0, respectively, since this element was |
1875 | | * indefinite-length encoded. In that case, both the tag and length will |
1876 | | * already exist in state's subitems, via sec_asn1d_record_any_header, |
1877 | | * and so the contents (aka 'child') should be added to that list of |
1878 | | * items to concatenate in sec_asn1d_concat_substrings once the EOC |
1879 | | * is encountered. |
1880 | | * |
1881 | | * To distinguish #2/#4 from #1/#3, it's sufficient to walk the ancestor |
1882 | | * tree. If the current type is a string type, then the enclosing |
1883 | | * construct will be that same type (#1/#2). If the current type is an |
1884 | | * any type, then the enclosing construct is either an any type (#3/#4) |
1885 | | * or some other type (#5). Since this is BER, this nesting relationship |
1886 | | * between 'state' and 'parent' may go through several levels of |
1887 | | * constructed encoding, so continue walking the ancestor chain until a |
1888 | | * clear determination can be made. |
1889 | | * |
1890 | | * The variable preallocatedString is used to indicate Case #1/#3, |
1891 | | * indicating an in-place copy has already occurred, and Cases #2, #4, |
1892 | | * and #5 all have the same behaviour of adding a new substring. |
1893 | | */ |
1894 | 0 | preallocatedString = PR_FALSE; |
1895 | 0 | temp_state = state; |
1896 | 0 | while (temp_state && item == temp_state->dest && temp_state->indefinite) { |
1897 | 0 | sec_asn1d_state *parent = sec_asn1d_get_enclosing_construct(temp_state); |
1898 | 0 | if (!parent || parent->underlying_kind != temp_state->underlying_kind) { |
1899 | | /* Case #5 - Either this is a top-level construct or it is part |
1900 | | * of some other element (e.g. a SEQUENCE), in which case, a |
1901 | | * new item should be allocated. */ |
1902 | 0 | break; |
1903 | 0 | } |
1904 | 0 | if (!parent->indefinite) { |
1905 | | /* Cases #1 / #3 - A definite length ancestor exists, for which |
1906 | | * this is a substring that has already copied into dest. */ |
1907 | 0 | preallocatedString = PR_TRUE; |
1908 | 0 | break; |
1909 | 0 | } |
1910 | 0 | if (!parent->substring) { |
1911 | | /* Cases #2 / #4 - If the parent is not a substring, but is |
1912 | | * indefinite, then there's nothing further up that may have |
1913 | | * preallocated dest, thus child will not have already |
1914 | | * been copied in place, therefore it's necessary to save child |
1915 | | * as a subitem. */ |
1916 | 0 | break; |
1917 | 0 | } |
1918 | 0 | temp_state = parent; |
1919 | 0 | } |
1920 | 0 | if (item != NULL && item->data != NULL && !preallocatedString) { |
1921 | | /* |
1922 | | * Save the string away for later concatenation. |
1923 | | */ |
1924 | 0 | PORT_Assert(item->data != NULL); |
1925 | 0 | sec_asn1d_add_to_subitems(state, item->data, item->len, PR_FALSE); |
1926 | | /* |
1927 | | * Clear the child item for the next round. |
1928 | | */ |
1929 | 0 | item->data = NULL; |
1930 | 0 | item->len = 0; |
1931 | 0 | } |
1932 | | |
1933 | | /* |
1934 | | * If our child was just our end-of-contents octets, we are done. |
1935 | | */ |
1936 | 0 | if (child->endofcontents) |
1937 | 0 | done = PR_TRUE; |
1938 | 0 | } |
1939 | | |
1940 | | /* |
1941 | | * Stop or do the next one. |
1942 | | */ |
1943 | 0 | if (done) { |
1944 | 0 | child->place = notInUse; |
1945 | 0 | state->place = afterConstructedString; |
1946 | 0 | } else { |
1947 | 0 | sec_asn1d_scrub_state(child); |
1948 | 0 | state->top->current = child; |
1949 | 0 | } |
1950 | 0 | } |
1951 | | |
1952 | | /* |
1953 | | * We are doing a SET OF or SEQUENCE OF, and have just finished an item. |
1954 | | */ |
1955 | | static void |
1956 | | sec_asn1d_next_in_group(sec_asn1d_state *state) |
1957 | 0 | { |
1958 | 0 | sec_asn1d_state *child; |
1959 | 0 | unsigned long child_consumed; |
1960 | |
|
1961 | 0 | PORT_Assert(state->place == duringGroup); |
1962 | 0 | PORT_Assert(state->child != NULL); |
1963 | |
|
1964 | 0 | child = state->child; |
1965 | |
|
1966 | 0 | child_consumed = child->consumed; |
1967 | 0 | child->consumed = 0; |
1968 | 0 | state->consumed += child_consumed; |
1969 | | |
1970 | | /* |
1971 | | * If our child was just our end-of-contents octets, we are done. |
1972 | | */ |
1973 | 0 | if (child->endofcontents) { |
1974 | | /* XXX I removed the PORT_Assert (child->dest == NULL) because there |
1975 | | * was a bug in that a template that was a sequence of which also had |
1976 | | * a child of a sequence of, in an indefinite group was not working |
1977 | | * properly. This fix seems to work, (added the if statement below), |
1978 | | * and nothing appears broken, but I am putting this note here just |
1979 | | * in case. */ |
1980 | | /* |
1981 | | * XXX No matter how many times I read that comment, |
1982 | | * I cannot figure out what case he was fixing. I believe what he |
1983 | | * did was deliberate, so I am loathe to touch it. I need to |
1984 | | * understand how it could ever be that child->dest != NULL but |
1985 | | * child->endofcontents is true, and why it is important to check |
1986 | | * that state->subitems_head is NULL. This really needs to be |
1987 | | * figured out, as I am not sure if the following code should be |
1988 | | * compensating for "offset", as is done a little farther below |
1989 | | * in the more normal case. |
1990 | | */ |
1991 | | /* |
1992 | | * XXX We used to assert our overall state was that we were decoding |
1993 | | * an indefinite-length object here (state->indefinite == TRUE and no |
1994 | | * pending bytes in the decoder), but those assertions aren't correct |
1995 | | * as it's legitimate to wrap indefinite sequences inside definite ones |
1996 | | * and this code handles that case. Additionally, when compiled in |
1997 | | * release mode these assertions aren't checked anyway, yet function |
1998 | | * safely. |
1999 | | */ |
2000 | 0 | if (child->dest && !state->subitems_head) { |
2001 | 0 | sec_asn1d_add_to_subitems(state, child->dest, 0, PR_FALSE); |
2002 | 0 | child->dest = NULL; |
2003 | 0 | } |
2004 | |
|
2005 | 0 | child->place = notInUse; |
2006 | 0 | state->place = afterGroup; |
2007 | 0 | return; |
2008 | 0 | } |
2009 | | |
2010 | | /* |
2011 | | * Do the "after" field notification for next in group. |
2012 | | */ |
2013 | 0 | sec_asn1d_notify_after(state->top, child->dest, child->depth); |
2014 | | |
2015 | | /* |
2016 | | * Save it away (unless we are not storing). |
2017 | | */ |
2018 | 0 | if (child->dest != NULL) { |
2019 | 0 | void *dest; |
2020 | |
|
2021 | 0 | dest = child->dest; |
2022 | 0 | dest = (char *)dest - child->theTemplate->offset; |
2023 | 0 | sec_asn1d_add_to_subitems(state, dest, 0, PR_FALSE); |
2024 | 0 | child->dest = NULL; |
2025 | 0 | } |
2026 | | |
2027 | | /* |
2028 | | * Account for those bytes; see if we are done. |
2029 | | */ |
2030 | 0 | if (state->pending) { |
2031 | 0 | PORT_Assert(!state->indefinite); |
2032 | 0 | if (child_consumed > state->pending) { |
2033 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2034 | 0 | state->top->status = decodeError; |
2035 | 0 | return; |
2036 | 0 | } |
2037 | | |
2038 | 0 | state->pending -= child_consumed; |
2039 | 0 | if (state->pending == 0) { |
2040 | 0 | child->place = notInUse; |
2041 | 0 | state->place = afterGroup; |
2042 | 0 | return; |
2043 | 0 | } |
2044 | 0 | } |
2045 | | |
2046 | | /* |
2047 | | * Do the "before" field notification for next item in group. |
2048 | | */ |
2049 | 0 | sec_asn1d_notify_before(state->top, child->dest, child->depth); |
2050 | | |
2051 | | /* |
2052 | | * Now we do the next one. |
2053 | | */ |
2054 | 0 | sec_asn1d_scrub_state(child); |
2055 | | |
2056 | | /* Initialize child state from the template */ |
2057 | 0 | sec_asn1d_init_state_based_on_template(child); |
2058 | |
|
2059 | 0 | state->top->current = child; |
2060 | 0 | } |
2061 | | |
2062 | | /* |
2063 | | * We are moving along through a sequence; move forward by one, |
2064 | | * (detecting end-of-sequence when it happens). |
2065 | | * XXX The handling of "missing" is ugly. Fix it. |
2066 | | */ |
2067 | | static void |
2068 | | sec_asn1d_next_in_sequence(sec_asn1d_state *state) |
2069 | 40 | { |
2070 | 40 | sec_asn1d_state *child; |
2071 | 40 | unsigned long child_consumed; |
2072 | 40 | PRBool child_missing; |
2073 | | |
2074 | 40 | PORT_Assert(state->place == duringSequence); |
2075 | 40 | PORT_Assert(state->child != NULL); |
2076 | | |
2077 | 40 | child = state->child; |
2078 | | |
2079 | | /* |
2080 | | * Do the "after" field notification. |
2081 | | */ |
2082 | 40 | sec_asn1d_notify_after(state->top, child->dest, child->depth); |
2083 | | |
2084 | 40 | child_missing = (PRBool)child->missing; |
2085 | 40 | child_consumed = child->consumed; |
2086 | 40 | child->consumed = 0; |
2087 | | |
2088 | | /* |
2089 | | * Take care of accounting. |
2090 | | */ |
2091 | 40 | if (child_missing) { |
2092 | 0 | PORT_Assert(child->optional); |
2093 | 40 | } else { |
2094 | 40 | state->consumed += child_consumed; |
2095 | | /* |
2096 | | * Free any grandchild. |
2097 | | */ |
2098 | 40 | sec_asn1d_free_child(child, PR_FALSE); |
2099 | 40 | if (state->pending) { |
2100 | 40 | PORT_Assert(!state->indefinite); |
2101 | 40 | if (child_consumed > state->pending) { |
2102 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2103 | 0 | state->top->status = decodeError; |
2104 | 0 | return; |
2105 | 0 | } |
2106 | 40 | state->pending -= child_consumed; |
2107 | 40 | if (state->pending == 0) { |
2108 | 16 | child->theTemplate++; |
2109 | 24 | while (child->theTemplate->kind != 0) { |
2110 | 8 | if ((child->theTemplate->kind & SEC_ASN1_OPTIONAL) == 0) { |
2111 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2112 | 0 | state->top->status = decodeError; |
2113 | 0 | return; |
2114 | 0 | } |
2115 | 8 | child->theTemplate++; |
2116 | 8 | } |
2117 | 16 | child->place = notInUse; |
2118 | 16 | state->place = afterEndOfContents; |
2119 | 16 | return; |
2120 | 16 | } |
2121 | 40 | } |
2122 | 40 | } |
2123 | | |
2124 | | /* |
2125 | | * Move forward. |
2126 | | */ |
2127 | 24 | child->theTemplate++; |
2128 | 24 | if (child->theTemplate->kind == 0) { |
2129 | | /* |
2130 | | * We are done with this sequence. |
2131 | | */ |
2132 | 0 | child->place = notInUse; |
2133 | 0 | if (state->pending) { |
2134 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2135 | 0 | state->top->status = decodeError; |
2136 | 0 | } else if (child_missing) { |
2137 | | /* |
2138 | | * We got to the end, but have a child that started parsing |
2139 | | * and ended up "missing". The only legitimate reason for |
2140 | | * this is that we had one or more optional fields at the |
2141 | | * end of our sequence, and we were encoded indefinite-length, |
2142 | | * so when we went looking for those optional fields we |
2143 | | * found our end-of-contents octets instead. |
2144 | | * (Yes, this is ugly; dunno a better way to handle it.) |
2145 | | * So, first confirm the situation, and then mark that we |
2146 | | * are done. |
2147 | | */ |
2148 | 0 | if (state->indefinite && child->endofcontents) { |
2149 | 0 | PORT_Assert(child_consumed == 2); |
2150 | 0 | if (child_consumed != 2) { |
2151 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2152 | 0 | state->top->status = decodeError; |
2153 | 0 | } else { |
2154 | 0 | state->consumed += child_consumed; |
2155 | 0 | state->place = afterEndOfContents; |
2156 | 0 | } |
2157 | 0 | } else { |
2158 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2159 | 0 | state->top->status = decodeError; |
2160 | 0 | } |
2161 | 0 | } else { |
2162 | | /* |
2163 | | * We have to finish out, maybe reading end-of-contents octets; |
2164 | | * let the normal logic do the right thing. |
2165 | | */ |
2166 | 0 | state->place = beforeEndOfContents; |
2167 | 0 | } |
2168 | 24 | } else { |
2169 | 24 | unsigned char child_found_tag_modifiers = 0; |
2170 | 24 | unsigned long child_found_tag_number = 0; |
2171 | | |
2172 | | /* |
2173 | | * Reset state and push. |
2174 | | */ |
2175 | 24 | if (state->dest != NULL) |
2176 | 24 | child->dest = (char *)state->dest + child->theTemplate->offset; |
2177 | | |
2178 | | /* |
2179 | | * Do the "before" field notification. |
2180 | | */ |
2181 | 24 | sec_asn1d_notify_before(state->top, child->dest, child->depth); |
2182 | | |
2183 | 24 | if (child_missing) { /* if previous child was missing, copy the tag data we already have */ |
2184 | 0 | child_found_tag_modifiers = child->found_tag_modifiers; |
2185 | 0 | child_found_tag_number = child->found_tag_number; |
2186 | 0 | } |
2187 | 24 | state->top->current = child; |
2188 | 24 | child = sec_asn1d_init_state_based_on_template(child); |
2189 | 24 | if (child_missing && child) { |
2190 | 0 | child->place = afterIdentifier; |
2191 | 0 | child->found_tag_modifiers = child_found_tag_modifiers; |
2192 | 0 | child->found_tag_number = child_found_tag_number; |
2193 | 0 | child->consumed = child_consumed; |
2194 | 0 | if (child->underlying_kind == SEC_ASN1_ANY && !child->top->filter_only) { |
2195 | | /* |
2196 | | * If the new field is an ANY, and we are storing, then |
2197 | | * we need to save the tag out. We would have done this |
2198 | | * already in the normal case, but since we were looking |
2199 | | * for an optional field, and we did not find it, we only |
2200 | | * now realize we need to save the tag. |
2201 | | */ |
2202 | 0 | unsigned char identifier; |
2203 | | |
2204 | | /* |
2205 | | * Check that we did not end up with a high tag; for that |
2206 | | * we need to re-encode the tag into multiple bytes in order |
2207 | | * to store it back to look like what we parsed originally. |
2208 | | * In practice this does not happen, but for completeness |
2209 | | * sake it should probably be made to work at some point. |
2210 | | */ |
2211 | 0 | if (child_found_tag_modifiers >= SEC_ASN1_HIGH_TAG_NUMBER) { |
2212 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
2213 | 0 | state->top->status = decodeError; |
2214 | 0 | } else { |
2215 | 0 | identifier = (unsigned char)(child_found_tag_modifiers | child_found_tag_number); |
2216 | 0 | sec_asn1d_record_any_header(child, (char *)&identifier, 1); |
2217 | 0 | } |
2218 | 0 | } |
2219 | 0 | } |
2220 | 24 | } |
2221 | 24 | } |
2222 | | |
2223 | | static void |
2224 | | sec_asn1d_concat_substrings(sec_asn1d_state *state) |
2225 | 0 | { |
2226 | 0 | PORT_Assert(state->place == afterConstructedString); |
2227 | |
|
2228 | 0 | if (state->subitems_head != NULL) { |
2229 | 0 | struct subitem *substring; |
2230 | 0 | unsigned long alloc_len, item_len; |
2231 | 0 | unsigned char *where; |
2232 | 0 | SECItem *item; |
2233 | 0 | PRBool is_bit_string; |
2234 | |
|
2235 | 0 | item_len = 0; |
2236 | 0 | is_bit_string = (state->underlying_kind == SEC_ASN1_BIT_STRING) |
2237 | 0 | ? PR_TRUE |
2238 | 0 | : PR_FALSE; |
2239 | |
|
2240 | 0 | substring = state->subitems_head; |
2241 | 0 | while (substring != NULL) { |
2242 | | /* |
2243 | | * All bit-string substrings except the last one should be |
2244 | | * a clean multiple of 8 bits. |
2245 | | */ |
2246 | 0 | if (is_bit_string && (substring->next != NULL) && (substring->len & 0x7)) { |
2247 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2248 | 0 | state->top->status = decodeError; |
2249 | 0 | return; |
2250 | 0 | } |
2251 | 0 | item_len += substring->len; |
2252 | 0 | substring = substring->next; |
2253 | 0 | } |
2254 | | |
2255 | 0 | if (is_bit_string) { |
2256 | 0 | alloc_len = ((item_len + 7) >> 3); |
2257 | 0 | } else { |
2258 | | /* |
2259 | | * Add 2 for the end-of-contents octets of an indefinite-length |
2260 | | * ANY that is *not* also an INNER. Because we zero-allocate |
2261 | | * below, all we need to do is increase the length here. |
2262 | | */ |
2263 | 0 | if (state->underlying_kind == SEC_ASN1_ANY && state->indefinite) |
2264 | 0 | item_len += 2; |
2265 | 0 | alloc_len = item_len; |
2266 | 0 | } |
2267 | |
|
2268 | 0 | if (state->top->max_element_size > 0 && |
2269 | 0 | alloc_len > state->top->max_element_size) { |
2270 | 0 | PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
2271 | 0 | state->top->status = decodeError; |
2272 | 0 | return; |
2273 | 0 | } |
2274 | | |
2275 | 0 | item = (SECItem *)(state->dest); |
2276 | 0 | PORT_Assert(item != NULL); |
2277 | 0 | PORT_Assert(item->data == NULL); |
2278 | 0 | item->data = (unsigned char *)sec_asn1d_zalloc(state->top->their_pool, |
2279 | 0 | alloc_len); |
2280 | 0 | if (item->data == NULL) { |
2281 | 0 | state->top->status = decodeError; |
2282 | 0 | return; |
2283 | 0 | } |
2284 | 0 | item->len = item_len; |
2285 | |
|
2286 | 0 | where = item->data; |
2287 | 0 | substring = state->subitems_head; |
2288 | 0 | while (substring != NULL) { |
2289 | 0 | if (is_bit_string) |
2290 | 0 | item_len = (substring->len + 7) >> 3; |
2291 | 0 | else |
2292 | 0 | item_len = substring->len; |
2293 | 0 | PORT_Memcpy(where, substring->data, item_len); |
2294 | 0 | where += item_len; |
2295 | 0 | substring = substring->next; |
2296 | 0 | } |
2297 | | |
2298 | | /* |
2299 | | * Because we use arenas and have a mark set, we later free |
2300 | | * everything we have allocated, so this does *not* present |
2301 | | * a memory leak (it is just temporarily left dangling). |
2302 | | */ |
2303 | 0 | state->subitems_head = state->subitems_tail = NULL; |
2304 | 0 | } |
2305 | | |
2306 | 0 | state->place = afterEndOfContents; |
2307 | 0 | } |
2308 | | |
2309 | | static void |
2310 | | sec_asn1d_concat_group(sec_asn1d_state *state) |
2311 | 0 | { |
2312 | 0 | const void ***placep; |
2313 | |
|
2314 | 0 | PORT_Assert(state->place == afterGroup); |
2315 | |
|
2316 | 0 | placep = (const void ***)state->dest; |
2317 | 0 | PORT_Assert(state->subitems_head == NULL || placep != NULL); |
2318 | 0 | if (placep != NULL) { |
2319 | 0 | struct subitem *item; |
2320 | 0 | const void **group; |
2321 | 0 | int count; |
2322 | |
|
2323 | 0 | count = 0; |
2324 | 0 | item = state->subitems_head; |
2325 | 0 | while (item != NULL) { |
2326 | 0 | PORT_Assert(item->next != NULL || item == state->subitems_tail); |
2327 | 0 | count++; |
2328 | 0 | item = item->next; |
2329 | 0 | } |
2330 | |
|
2331 | 0 | group = (const void **)sec_asn1d_zalloc(state->top->their_pool, |
2332 | 0 | (count + 1) * (sizeof(void *))); |
2333 | 0 | if (group == NULL) { |
2334 | 0 | state->top->status = decodeError; |
2335 | 0 | return; |
2336 | 0 | } |
2337 | | |
2338 | 0 | *placep = group; |
2339 | |
|
2340 | 0 | item = state->subitems_head; |
2341 | 0 | while (item != NULL) { |
2342 | 0 | *group++ = item->data; |
2343 | 0 | item = item->next; |
2344 | 0 | } |
2345 | 0 | *group = NULL; |
2346 | | |
2347 | | /* |
2348 | | * Because we use arenas and have a mark set, we later free |
2349 | | * everything we have allocated, so this does *not* present |
2350 | | * a memory leak (it is just temporarily left dangling). |
2351 | | */ |
2352 | 0 | state->subitems_head = state->subitems_tail = NULL; |
2353 | 0 | } |
2354 | | |
2355 | 0 | state->place = afterEndOfContents; |
2356 | 0 | } |
2357 | | |
2358 | | /* |
2359 | | * For those states that push a child to handle a subtemplate, |
2360 | | * "absorb" that child (transfer necessary information). |
2361 | | */ |
2362 | | static void |
2363 | | sec_asn1d_absorb_child(sec_asn1d_state *state) |
2364 | 8 | { |
2365 | | /* |
2366 | | * There is absolutely supposed to be a child there. |
2367 | | */ |
2368 | 8 | PORT_Assert(state->child != NULL); |
2369 | | |
2370 | | /* |
2371 | | * Inherit the missing status of our child, and do the ugly |
2372 | | * backing-up if necessary. |
2373 | | */ |
2374 | 8 | state->missing = state->child->missing; |
2375 | 8 | if (state->missing) { |
2376 | 0 | state->found_tag_number = state->child->found_tag_number; |
2377 | 0 | state->found_tag_modifiers = state->child->found_tag_modifiers; |
2378 | 0 | state->endofcontents = state->child->endofcontents; |
2379 | 0 | } |
2380 | | |
2381 | | /* |
2382 | | * Add in number of bytes consumed by child. |
2383 | | * (Only EXPLICIT should have already consumed bytes itself.) |
2384 | | */ |
2385 | 8 | PORT_Assert(state->place == afterExplicit || state->consumed == 0); |
2386 | 8 | state->consumed += state->child->consumed; |
2387 | | |
2388 | | /* |
2389 | | * Subtract from bytes pending; this only applies to a definite-length |
2390 | | * EXPLICIT field. |
2391 | | */ |
2392 | 8 | if (state->pending) { |
2393 | 0 | PORT_Assert(!state->indefinite); |
2394 | 0 | PORT_Assert(state->place == afterExplicit); |
2395 | | |
2396 | | /* |
2397 | | * If we had a definite-length explicit, then what the child |
2398 | | * consumed should be what was left pending. |
2399 | | */ |
2400 | 0 | if (state->pending != state->child->consumed) { |
2401 | 0 | if (state->pending < state->child->consumed) { |
2402 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2403 | 0 | state->top->status = decodeError; |
2404 | 0 | return; |
2405 | 0 | } |
2406 | | /* |
2407 | | * Okay, this is a hack. It *should* be an error whether |
2408 | | * pending is too big or too small, but it turns out that |
2409 | | * we had a bug in our *old* DER encoder that ended up |
2410 | | * counting an explicit header twice in the case where |
2411 | | * the underlying type was an ANY. So, because we cannot |
2412 | | * prevent receiving these (our own certificate server can |
2413 | | * send them to us), we need to be lenient and accept them. |
2414 | | * To do so, we need to pretend as if we read all of the |
2415 | | * bytes that the header said we would find, even though |
2416 | | * we actually came up short. |
2417 | | */ |
2418 | 0 | state->consumed += (state->pending - state->child->consumed); |
2419 | 0 | } |
2420 | 0 | state->pending = 0; |
2421 | 0 | } |
2422 | | |
2423 | | /* |
2424 | | * Indicate that we are done with child. |
2425 | | */ |
2426 | 8 | state->child->consumed = 0; |
2427 | | |
2428 | | /* |
2429 | | * And move on to final state. |
2430 | | * (Technically everybody could move to afterEndOfContents except |
2431 | | * for an indefinite-length EXPLICIT; for simplicity though we assert |
2432 | | * that but let the end-of-contents code do the real determination.) |
2433 | | */ |
2434 | 8 | PORT_Assert(state->place == afterExplicit || (!state->indefinite)); |
2435 | 8 | state->place = beforeEndOfContents; |
2436 | 8 | } |
2437 | | |
2438 | | static void |
2439 | | sec_asn1d_prepare_for_end_of_contents(sec_asn1d_state *state) |
2440 | 36 | { |
2441 | 36 | PORT_Assert(state->place == beforeEndOfContents); |
2442 | | |
2443 | 36 | if (state->indefinite) { |
2444 | 0 | state->place = duringEndOfContents; |
2445 | 0 | state->pending = 2; |
2446 | 36 | } else { |
2447 | 36 | state->place = afterEndOfContents; |
2448 | 36 | } |
2449 | 36 | } |
2450 | | |
2451 | | static unsigned long |
2452 | | sec_asn1d_parse_end_of_contents(sec_asn1d_state *state, |
2453 | | const char *buf, unsigned long len) |
2454 | 0 | { |
2455 | 0 | unsigned int i; |
2456 | |
|
2457 | 0 | PORT_Assert(state->pending <= 2); |
2458 | 0 | PORT_Assert(state->place == duringEndOfContents); |
2459 | |
|
2460 | 0 | if (len == 0) { |
2461 | 0 | state->top->status = needBytes; |
2462 | 0 | return 0; |
2463 | 0 | } |
2464 | | |
2465 | 0 | if (state->pending < len) |
2466 | 0 | len = state->pending; |
2467 | |
|
2468 | 0 | for (i = 0; i < len; i++) { |
2469 | 0 | if (buf[i] != 0) { |
2470 | | /* |
2471 | | * We expect to find only zeros; if not, just give up. |
2472 | | */ |
2473 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2474 | 0 | state->top->status = decodeError; |
2475 | 0 | return 0; |
2476 | 0 | } |
2477 | 0 | } |
2478 | | |
2479 | 0 | state->pending -= len; |
2480 | |
|
2481 | 0 | if (state->pending == 0) { |
2482 | 0 | state->place = afterEndOfContents; |
2483 | | /* These end-of-contents octets either terminate a SEQUENCE, a GROUP, |
2484 | | * or a constructed string. The SEQUENCE case is unique in that the |
2485 | | * state parses its own end-of-contents octets and therefore should not |
2486 | | * have its `endofcontents` flag set. We identify the SEQUENCE case by |
2487 | | * checking whether the child state's template is pointing at a |
2488 | | * template terminator (see `sec_asn1d_next_in_sequence`). |
2489 | | */ |
2490 | 0 | if (state->child && state->child->theTemplate->kind == 0) { |
2491 | 0 | state->endofcontents = PR_FALSE; |
2492 | 0 | } else { |
2493 | 0 | state->endofcontents = PR_TRUE; |
2494 | 0 | } |
2495 | 0 | } |
2496 | |
|
2497 | 0 | return len; |
2498 | 0 | } |
2499 | | |
2500 | | static void |
2501 | | sec_asn1d_pop_state(sec_asn1d_state *state) |
2502 | 56 | { |
2503 | | #if 0 /* XXX I think this should always be handled explicitly by parent? */ |
2504 | | /* |
2505 | | * Account for our child. |
2506 | | */ |
2507 | | if (state->child != NULL) { |
2508 | | state->consumed += state->child->consumed; |
2509 | | if (state->pending) { |
2510 | | PORT_Assert (!state->indefinite); |
2511 | | if (state->child->consumed > state->pending) { |
2512 | | PORT_SetError (SEC_ERROR_BAD_DER); |
2513 | | state->top->status = decodeError; |
2514 | | } else { |
2515 | | state->pending -= state->child->consumed; |
2516 | | } |
2517 | | } |
2518 | | state->child->consumed = 0; |
2519 | | } |
2520 | | #endif /* XXX */ |
2521 | | |
2522 | | /* |
2523 | | * Free our child. |
2524 | | */ |
2525 | 56 | sec_asn1d_free_child(state, PR_FALSE); |
2526 | | |
2527 | | /* |
2528 | | * Just make my parent be the current state. It will then clean |
2529 | | * up after me and free me (or reuse me). |
2530 | | */ |
2531 | 56 | state->top->current = state->parent; |
2532 | 56 | } |
2533 | | |
2534 | | static sec_asn1d_state * |
2535 | | sec_asn1d_before_choice(sec_asn1d_state *state) |
2536 | 0 | { |
2537 | 0 | sec_asn1d_state *child; |
2538 | |
|
2539 | 0 | if (state->allocate) { |
2540 | 0 | void *dest; |
2541 | |
|
2542 | 0 | dest = sec_asn1d_zalloc(state->top->their_pool, state->theTemplate->size); |
2543 | 0 | if ((void *)NULL == dest) { |
2544 | 0 | state->top->status = decodeError; |
2545 | 0 | return (sec_asn1d_state *)NULL; |
2546 | 0 | } |
2547 | | |
2548 | 0 | state->dest = (char *)dest + state->theTemplate->offset; |
2549 | 0 | } |
2550 | | |
2551 | 0 | child = sec_asn1d_push_state(state->top, state->theTemplate + 1, |
2552 | 0 | (char *)state->dest - state->theTemplate->offset, |
2553 | 0 | PR_FALSE); |
2554 | 0 | if ((sec_asn1d_state *)NULL == child) { |
2555 | 0 | return (sec_asn1d_state *)NULL; |
2556 | 0 | } |
2557 | | |
2558 | 0 | sec_asn1d_scrub_state(child); |
2559 | 0 | child = sec_asn1d_init_state_based_on_template(child); |
2560 | 0 | if ((sec_asn1d_state *)NULL == child) { |
2561 | 0 | return (sec_asn1d_state *)NULL; |
2562 | 0 | } |
2563 | | |
2564 | 0 | child->optional = PR_TRUE; |
2565 | |
|
2566 | 0 | state->place = duringChoice; |
2567 | |
|
2568 | 0 | return child; |
2569 | 0 | } |
2570 | | |
2571 | | static sec_asn1d_state * |
2572 | | sec_asn1d_during_choice(sec_asn1d_state *state) |
2573 | 0 | { |
2574 | 0 | sec_asn1d_state *child = state->child; |
2575 | |
|
2576 | 0 | PORT_Assert((sec_asn1d_state *)NULL != child); |
2577 | |
|
2578 | 0 | if (child->missing) { |
2579 | 0 | unsigned char child_found_tag_modifiers = 0; |
2580 | 0 | unsigned long child_found_tag_number = 0; |
2581 | 0 | void *dest; |
2582 | |
|
2583 | 0 | state->consumed += child->consumed; |
2584 | |
|
2585 | 0 | if (child->endofcontents) { |
2586 | | /* This choice is probably the first item in a GROUP |
2587 | | ** (e.g. SET_OF) that was indefinite-length encoded. |
2588 | | ** We're actually at the end of that GROUP. |
2589 | | ** We look up the stack to be sure that we find |
2590 | | ** a state with indefinite length encoding before we |
2591 | | ** find a state (like a SEQUENCE) that is definite. |
2592 | | */ |
2593 | 0 | child->place = notInUse; |
2594 | 0 | state->place = afterChoice; |
2595 | 0 | state->endofcontents = PR_TRUE; /* propagate this up */ |
2596 | 0 | if (sec_asn1d_parent_allows_EOC(state)) |
2597 | 0 | return state; |
2598 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2599 | 0 | state->top->status = decodeError; |
2600 | 0 | return NULL; |
2601 | 0 | } |
2602 | | |
2603 | 0 | dest = (char *)child->dest - child->theTemplate->offset; |
2604 | 0 | child->theTemplate++; |
2605 | |
|
2606 | 0 | if (0 == child->theTemplate->kind) { |
2607 | | /* Ran out of choices */ |
2608 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2609 | 0 | state->top->status = decodeError; |
2610 | 0 | return (sec_asn1d_state *)NULL; |
2611 | 0 | } |
2612 | 0 | child->dest = (char *)dest + child->theTemplate->offset; |
2613 | | |
2614 | | /* cargo'd from next_in_sequence innards */ |
2615 | 0 | if (state->pending) { |
2616 | 0 | PORT_Assert(!state->indefinite); |
2617 | 0 | if (child->consumed > state->pending) { |
2618 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2619 | 0 | state->top->status = decodeError; |
2620 | 0 | return NULL; |
2621 | 0 | } |
2622 | 0 | state->pending -= child->consumed; |
2623 | 0 | if (0 == state->pending) { |
2624 | | /* XXX uh.. not sure if I should have stopped this |
2625 | | * from happening before. */ |
2626 | 0 | PORT_Assert(0); |
2627 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2628 | 0 | state->top->status = decodeError; |
2629 | 0 | return (sec_asn1d_state *)NULL; |
2630 | 0 | } |
2631 | 0 | } |
2632 | | |
2633 | 0 | child->consumed = 0; |
2634 | 0 | sec_asn1d_scrub_state(child); |
2635 | | |
2636 | | /* move it on top again */ |
2637 | 0 | state->top->current = child; |
2638 | |
|
2639 | 0 | child_found_tag_modifiers = child->found_tag_modifiers; |
2640 | 0 | child_found_tag_number = child->found_tag_number; |
2641 | |
|
2642 | 0 | child = sec_asn1d_init_state_based_on_template(child); |
2643 | 0 | if ((sec_asn1d_state *)NULL == child) { |
2644 | 0 | return (sec_asn1d_state *)NULL; |
2645 | 0 | } |
2646 | | |
2647 | | /* copy our findings to the new top */ |
2648 | 0 | child->found_tag_modifiers = child_found_tag_modifiers; |
2649 | 0 | child->found_tag_number = child_found_tag_number; |
2650 | |
|
2651 | 0 | child->optional = PR_TRUE; |
2652 | 0 | child->place = afterIdentifier; |
2653 | |
|
2654 | 0 | return child; |
2655 | 0 | } |
2656 | 0 | if ((void *)NULL != state->dest) { |
2657 | | /* Store the enum */ |
2658 | 0 | int *which = (int *)state->dest; |
2659 | 0 | *which = (int)child->theTemplate->size; |
2660 | 0 | } |
2661 | |
|
2662 | 0 | child->place = notInUse; |
2663 | |
|
2664 | 0 | state->place = afterChoice; |
2665 | 0 | return state; |
2666 | 0 | } |
2667 | | |
2668 | | static void |
2669 | | sec_asn1d_after_choice(sec_asn1d_state *state) |
2670 | 0 | { |
2671 | 0 | state->consumed += state->child->consumed; |
2672 | 0 | state->child->consumed = 0; |
2673 | 0 | state->place = afterEndOfContents; |
2674 | 0 | sec_asn1d_pop_state(state); |
2675 | 0 | } |
2676 | | |
2677 | | unsigned long |
2678 | | sec_asn1d_uinteger(SECItem *src) |
2679 | 0 | { |
2680 | 0 | unsigned long value; |
2681 | 0 | int len; |
2682 | |
|
2683 | 0 | if (src->len > 5 || (src->len > 4 && src->data[0] == 0)) |
2684 | 0 | return 0; |
2685 | | |
2686 | 0 | value = 0; |
2687 | 0 | len = src->len; |
2688 | 0 | while (len) { |
2689 | 0 | value <<= 8; |
2690 | 0 | value |= src->data[--len]; |
2691 | 0 | } |
2692 | 0 | return value; |
2693 | 0 | } |
2694 | | |
2695 | | SECStatus |
2696 | | SEC_ASN1DecodeInteger(SECItem *src, unsigned long *value) |
2697 | 0 | { |
2698 | 0 | unsigned long v; |
2699 | 0 | unsigned int i; |
2700 | |
|
2701 | 0 | if (src == NULL) { |
2702 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
2703 | 0 | return SECFailure; |
2704 | 0 | } |
2705 | | |
2706 | 0 | if (src->len > sizeof(unsigned long)) { |
2707 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
2708 | 0 | return SECFailure; |
2709 | 0 | } |
2710 | | |
2711 | 0 | if (src->data == NULL) { |
2712 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
2713 | 0 | return SECFailure; |
2714 | 0 | } |
2715 | | |
2716 | 0 | if (src->data[0] & 0x80) |
2717 | 0 | v = -1; /* signed and negative - start with all 1's */ |
2718 | 0 | else |
2719 | 0 | v = 0; |
2720 | |
|
2721 | 0 | for (i = 0; i < src->len; i++) { |
2722 | | /* shift in next byte */ |
2723 | 0 | v <<= 8; |
2724 | 0 | v |= src->data[i]; |
2725 | 0 | } |
2726 | 0 | *value = v; |
2727 | 0 | return SECSuccess; |
2728 | 0 | } |
2729 | | |
2730 | | #ifdef DEBUG_ASN1D_STATES |
2731 | | static void |
2732 | | dump_states(SEC_ASN1DecoderContext *cx) |
2733 | | { |
2734 | | sec_asn1d_state *state; |
2735 | | int bufsize = 256; |
2736 | | char kindBuf[bufsize]; |
2737 | | |
2738 | | for (state = cx->current; state->parent; state = state->parent) { |
2739 | | ; |
2740 | | } |
2741 | | |
2742 | | for (; state; state = state->child) { |
2743 | | int i; |
2744 | | for (i = 0; i < state->depth; i++) { |
2745 | | printf(" "); |
2746 | | } |
2747 | | |
2748 | | i = formatKind(state->theTemplate->kind, kindBuf, bufsize); |
2749 | | printf("%s: tmpl kind %s", |
2750 | | (state == cx->current) ? "STATE" : "State", |
2751 | | kindBuf); |
2752 | | printf(" %s", (state->place >= 0 && state->place <= notInUse) ? place_names[state->place] : "(undefined)"); |
2753 | | if (!i) |
2754 | | printf(", expect 0x%02lx", |
2755 | | state->expect_tag_number | state->expect_tag_modifiers); |
2756 | | |
2757 | | printf("%s%s%s %lu\n", |
2758 | | state->indefinite ? ", indef" : "", |
2759 | | state->missing ? ", miss" : "", |
2760 | | state->endofcontents ? ", EOC" : "", |
2761 | | state->pending); |
2762 | | } |
2763 | | |
2764 | | return; |
2765 | | } |
2766 | | #endif /* DEBUG_ASN1D_STATES */ |
2767 | | |
2768 | | SECStatus |
2769 | | SEC_ASN1DecoderUpdate(SEC_ASN1DecoderContext *cx, |
2770 | | const char *buf, unsigned long len) |
2771 | 8 | { |
2772 | 8 | sec_asn1d_state *state = NULL; |
2773 | 8 | unsigned long consumed; |
2774 | 8 | SEC_ASN1EncodingPart what; |
2775 | | |
2776 | 8 | if (cx->status == needBytes) |
2777 | 8 | cx->status = keepGoing; |
2778 | | |
2779 | 372 | while (cx->status == keepGoing) { |
2780 | 372 | state = cx->current; |
2781 | 372 | what = SEC_ASN1_Contents; |
2782 | 372 | consumed = 0; |
2783 | | #ifdef DEBUG_ASN1D_STATES |
2784 | | printf("\nPLACE = %s, next byte = 0x%02x, %p[%lu]\n", |
2785 | | (state->place >= 0 && state->place <= notInUse) ? place_names[state->place] : "(undefined)", |
2786 | | len ? (unsigned int)((unsigned char *)buf)[consumed] : 0, |
2787 | | buf, consumed); |
2788 | | dump_states(cx); |
2789 | | #endif /* DEBUG_ASN1D_STATES */ |
2790 | 372 | switch (state->place) { |
2791 | 48 | case beforeIdentifier: |
2792 | 48 | consumed = sec_asn1d_parse_identifier(state, buf, len); |
2793 | 48 | what = SEC_ASN1_Identifier; |
2794 | 48 | break; |
2795 | 0 | case duringIdentifier: |
2796 | 0 | consumed = sec_asn1d_parse_more_identifier(state, buf, len); |
2797 | 0 | what = SEC_ASN1_Identifier; |
2798 | 0 | break; |
2799 | 48 | case afterIdentifier: |
2800 | 48 | sec_asn1d_confirm_identifier(state); |
2801 | 48 | break; |
2802 | 48 | case beforeLength: |
2803 | 48 | consumed = sec_asn1d_parse_length(state, buf, len); |
2804 | 48 | what = SEC_ASN1_Length; |
2805 | 48 | break; |
2806 | 12 | case duringLength: |
2807 | 12 | consumed = sec_asn1d_parse_more_length(state, buf, len); |
2808 | 12 | what = SEC_ASN1_Length; |
2809 | 12 | break; |
2810 | 48 | case afterLength: |
2811 | 48 | sec_asn1d_prepare_for_contents(state); |
2812 | 48 | break; |
2813 | 0 | case beforeBitString: |
2814 | 0 | consumed = sec_asn1d_parse_bit_string(state, buf, len); |
2815 | 0 | break; |
2816 | 0 | case duringBitString: |
2817 | 0 | consumed = sec_asn1d_parse_more_bit_string(state, buf, len); |
2818 | 0 | break; |
2819 | 0 | case duringConstructedString: |
2820 | 0 | sec_asn1d_next_substring(state); |
2821 | 0 | break; |
2822 | 0 | case duringGroup: |
2823 | 0 | sec_asn1d_next_in_group(state); |
2824 | 0 | break; |
2825 | 28 | case duringLeaf: |
2826 | 28 | consumed = sec_asn1d_parse_leaf(state, buf, len); |
2827 | 28 | break; |
2828 | 0 | case duringSaveEncoding: |
2829 | 0 | sec_asn1d_reuse_encoding(state); |
2830 | 0 | if (cx->status == decodeError) { |
2831 | | /* recursive call has already popped all states from stack. |
2832 | | ** Bail out quickly. |
2833 | | */ |
2834 | 0 | return SECFailure; |
2835 | 0 | } |
2836 | 0 | if (cx->status == needBytes) { |
2837 | | /* recursive call wanted more data. Fatal. Clean up below. */ |
2838 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2839 | 0 | cx->status = decodeError; |
2840 | 0 | } |
2841 | 0 | break; |
2842 | 40 | case duringSequence: |
2843 | 40 | sec_asn1d_next_in_sequence(state); |
2844 | 40 | break; |
2845 | 0 | case afterConstructedString: |
2846 | 0 | sec_asn1d_concat_substrings(state); |
2847 | 0 | break; |
2848 | 0 | case afterExplicit: |
2849 | 0 | case afterImplicit: |
2850 | 8 | case afterInline: |
2851 | 8 | case afterPointer: |
2852 | 8 | sec_asn1d_absorb_child(state); |
2853 | 8 | break; |
2854 | 0 | case afterGroup: |
2855 | 0 | sec_asn1d_concat_group(state); |
2856 | 0 | break; |
2857 | 0 | case afterSaveEncoding: |
2858 | | /* SEC_ASN1DecoderUpdate has called itself recursively to |
2859 | | ** decode SAVEd encoded data, and now is done decoding that. |
2860 | | ** Return to the calling copy of SEC_ASN1DecoderUpdate. |
2861 | | */ |
2862 | 0 | return SECSuccess; |
2863 | 36 | case beforeEndOfContents: |
2864 | 36 | sec_asn1d_prepare_for_end_of_contents(state); |
2865 | 36 | break; |
2866 | 0 | case duringEndOfContents: |
2867 | 0 | consumed = sec_asn1d_parse_end_of_contents(state, buf, len); |
2868 | 0 | what = SEC_ASN1_EndOfContents; |
2869 | 0 | break; |
2870 | 56 | case afterEndOfContents: |
2871 | 56 | sec_asn1d_pop_state(state); |
2872 | 56 | break; |
2873 | 0 | case beforeChoice: |
2874 | 0 | state = sec_asn1d_before_choice(state); |
2875 | 0 | break; |
2876 | 0 | case duringChoice: |
2877 | 0 | state = sec_asn1d_during_choice(state); |
2878 | 0 | break; |
2879 | 0 | case afterChoice: |
2880 | 0 | sec_asn1d_after_choice(state); |
2881 | 0 | break; |
2882 | 0 | case notInUse: |
2883 | 0 | default: |
2884 | | /* This is not an error, but rather a plain old BUG! */ |
2885 | 0 | PORT_Assert(0); |
2886 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2887 | 0 | cx->status = decodeError; |
2888 | 0 | break; |
2889 | 372 | } |
2890 | | |
2891 | 372 | if (cx->status == decodeError) |
2892 | 0 | break; |
2893 | | |
2894 | | /* We should not consume more than we have. */ |
2895 | 372 | PORT_Assert(consumed <= len); |
2896 | 372 | if (consumed > len) { |
2897 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
2898 | 0 | cx->status = decodeError; |
2899 | 0 | break; |
2900 | 0 | } |
2901 | | |
2902 | | /* It might have changed, so we have to update our local copy. */ |
2903 | 372 | state = cx->current; |
2904 | | |
2905 | | /* If it is NULL, we have popped all the way to the top. */ |
2906 | 372 | if (state == NULL) { |
2907 | 8 | PORT_Assert(consumed == 0); |
2908 | | #if 0 /* XXX I want this here, but it seems that we have situations (like \ |
2909 | | * downloading a pkcs7 cert chain from some issuers) that give us a \ |
2910 | | * length which is greater than the entire encoding. So, we cannot \ |
2911 | | * have this be an error. \ |
2912 | | */ |
2913 | | if (len > 0) { |
2914 | | PORT_SetError (SEC_ERROR_BAD_DER); |
2915 | | cx->status = decodeError; |
2916 | | } else |
2917 | | #endif |
2918 | 8 | cx->status = allDone; |
2919 | 8 | break; |
2920 | 364 | } else if (state->theTemplate->kind == SEC_ASN1_SKIP_REST) { |
2921 | 0 | cx->status = allDone; |
2922 | 0 | break; |
2923 | 0 | } |
2924 | | |
2925 | 364 | if (consumed == 0) |
2926 | 228 | continue; |
2927 | | |
2928 | | /* |
2929 | | * The following check is specifically looking for an ANY |
2930 | | * that is *not* also an INNER, because we need to save aside |
2931 | | * all bytes in that case -- the contents parts will get |
2932 | | * handled like all other contents, and the end-of-contents |
2933 | | * bytes are added by the concat code, but the outer header |
2934 | | * bytes need to get saved too, so we do them explicitly here. |
2935 | | */ |
2936 | 136 | if (state->underlying_kind == SEC_ASN1_ANY && !cx->filter_only && (what == SEC_ASN1_Identifier || what == SEC_ASN1_Length)) { |
2937 | 16 | sec_asn1d_record_any_header(state, buf, consumed); |
2938 | 16 | } |
2939 | | |
2940 | | /* |
2941 | | * We had some number of good, accepted bytes. If the caller |
2942 | | * has registered to see them, pass them along. |
2943 | | */ |
2944 | 136 | if (state->top->filter_proc != NULL) { |
2945 | 0 | int depth; |
2946 | |
|
2947 | 0 | depth = state->depth; |
2948 | 0 | if (what == SEC_ASN1_EndOfContents && !state->indefinite) { |
2949 | 0 | PORT_Assert(state->parent != NULL && state->parent->indefinite); |
2950 | 0 | depth--; |
2951 | 0 | PORT_Assert(depth == state->parent->depth); |
2952 | 0 | } |
2953 | 0 | (*state->top->filter_proc)(state->top->filter_arg, |
2954 | 0 | buf, consumed, depth, what); |
2955 | 0 | } |
2956 | | |
2957 | 136 | state->consumed += consumed; |
2958 | 136 | buf += consumed; |
2959 | 136 | len -= consumed; |
2960 | 136 | } |
2961 | | |
2962 | 8 | if (cx->status == decodeError) { |
2963 | 0 | while (state != NULL) { |
2964 | 0 | sec_asn1d_free_child(state, PR_TRUE); |
2965 | 0 | state = state->parent; |
2966 | 0 | } |
2967 | | #ifdef SEC_ASN1D_FREE_ON_ERROR /* \ |
2968 | | * XXX This does not work because we can \ |
2969 | | * end up leaving behind dangling pointers \ |
2970 | | * to stuff that was allocated. In order \ |
2971 | | * to make this really work (which would \ |
2972 | | * be a good thing, I think), we need to \ |
2973 | | * keep track of every place/pointer that \ |
2974 | | * was allocated and make sure to NULL it \ |
2975 | | * out before we then free back to the mark. \ |
2976 | | */ |
2977 | | if (cx->their_pool != NULL) { |
2978 | | PORT_Assert(cx->their_mark != NULL); |
2979 | | PORT_ArenaRelease(cx->their_pool, cx->their_mark); |
2980 | | cx->their_mark = NULL; |
2981 | | } |
2982 | | #endif |
2983 | 0 | return SECFailure; |
2984 | 0 | } |
2985 | | |
2986 | | #if 0 /* XXX This is what I want, but cannot have because it seems we \ |
2987 | | * have situations (like when downloading a pkcs7 cert chain from \ |
2988 | | * some issuers) that give us a total length which is greater than \ |
2989 | | * the entire encoding. So, we have to allow allDone to have a \ |
2990 | | * remaining length greater than zero. I wanted to catch internal \ |
2991 | | * bugs with this, noticing when we do not have the right length. \ |
2992 | | * Oh well. \ |
2993 | | */ |
2994 | | PORT_Assert (len == 0 |
2995 | | && (cx->status == needBytes || cx->status == allDone)); |
2996 | | #else |
2997 | 8 | PORT_Assert((len == 0 && cx->status == needBytes) || cx->status == allDone); |
2998 | 8 | #endif |
2999 | 8 | return SECSuccess; |
3000 | 8 | } |
3001 | | |
3002 | | SECStatus |
3003 | | SEC_ASN1DecoderFinish(SEC_ASN1DecoderContext *cx) |
3004 | 8 | { |
3005 | 8 | SECStatus rv; |
3006 | | |
3007 | 8 | if (!cx || cx->status == needBytes) { |
3008 | 0 | PORT_SetError(SEC_ERROR_BAD_DER); |
3009 | 0 | rv = SECFailure; |
3010 | 8 | } else { |
3011 | 8 | rv = SECSuccess; |
3012 | 8 | } |
3013 | | |
3014 | | /* |
3015 | | * XXX anything else that needs to be finished? |
3016 | | */ |
3017 | | |
3018 | 8 | if (cx) { |
3019 | 8 | PORT_FreeArena(cx->our_pool, PR_TRUE); |
3020 | 8 | } |
3021 | | |
3022 | 8 | return rv; |
3023 | 8 | } |
3024 | | |
3025 | | SEC_ASN1DecoderContext * |
3026 | | SEC_ASN1DecoderStart(PLArenaPool *their_pool, void *dest, |
3027 | | const SEC_ASN1Template *theTemplate) |
3028 | 8 | { |
3029 | 8 | PLArenaPool *our_pool; |
3030 | 8 | SEC_ASN1DecoderContext *cx; |
3031 | | |
3032 | 8 | our_pool = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE); |
3033 | 8 | if (our_pool == NULL) |
3034 | 0 | return NULL; |
3035 | | |
3036 | 8 | cx = (SEC_ASN1DecoderContext *)PORT_ArenaZAlloc(our_pool, sizeof(*cx)); |
3037 | 8 | if (cx == NULL) { |
3038 | 0 | PORT_FreeArena(our_pool, PR_FALSE); |
3039 | 0 | return NULL; |
3040 | 0 | } |
3041 | | |
3042 | 8 | cx->our_pool = our_pool; |
3043 | 8 | if (their_pool != NULL) { |
3044 | 8 | cx->their_pool = their_pool; |
3045 | | #ifdef SEC_ASN1D_FREE_ON_ERROR |
3046 | | cx->their_mark = PORT_ArenaMark(their_pool); |
3047 | | #endif |
3048 | 8 | } |
3049 | | |
3050 | 8 | cx->status = needBytes; |
3051 | | |
3052 | 8 | if (sec_asn1d_push_state(cx, theTemplate, dest, PR_FALSE) == NULL || sec_asn1d_init_state_based_on_template(cx->current) == NULL) { |
3053 | | /* |
3054 | | * Trouble initializing (probably due to failed allocations) |
3055 | | * requires that we just give up. |
3056 | | */ |
3057 | 0 | PORT_FreeArena(our_pool, PR_FALSE); |
3058 | 0 | return NULL; |
3059 | 0 | } |
3060 | | |
3061 | 8 | return cx; |
3062 | 8 | } |
3063 | | |
3064 | | void |
3065 | | SEC_ASN1DecoderSetFilterProc(SEC_ASN1DecoderContext *cx, |
3066 | | SEC_ASN1WriteProc fn, void *arg, |
3067 | | PRBool only) |
3068 | 0 | { |
3069 | | /* check that we are "between" fields here */ |
3070 | 0 | PORT_Assert(cx->during_notify); |
3071 | |
|
3072 | 0 | cx->filter_proc = fn; |
3073 | 0 | cx->filter_arg = arg; |
3074 | 0 | cx->filter_only = only; |
3075 | 0 | } |
3076 | | |
3077 | | void |
3078 | | SEC_ASN1DecoderClearFilterProc(SEC_ASN1DecoderContext *cx) |
3079 | 0 | { |
3080 | | /* check that we are "between" fields here */ |
3081 | 0 | PORT_Assert(cx->during_notify); |
3082 | |
|
3083 | 0 | cx->filter_proc = NULL; |
3084 | 0 | cx->filter_arg = NULL; |
3085 | 0 | cx->filter_only = PR_FALSE; |
3086 | 0 | } |
3087 | | |
3088 | | void |
3089 | | SEC_ASN1DecoderSetNotifyProc(SEC_ASN1DecoderContext *cx, |
3090 | | SEC_ASN1NotifyProc fn, void *arg) |
3091 | 0 | { |
3092 | 0 | cx->notify_proc = fn; |
3093 | 0 | cx->notify_arg = arg; |
3094 | 0 | } |
3095 | | |
3096 | | void |
3097 | | SEC_ASN1DecoderClearNotifyProc(SEC_ASN1DecoderContext *cx) |
3098 | 0 | { |
3099 | 0 | cx->notify_proc = NULL; |
3100 | 0 | cx->notify_arg = NULL; /* not necessary; just being clean */ |
3101 | 0 | } |
3102 | | |
3103 | | void |
3104 | | SEC_ASN1DecoderSetMaximumElementSize(SEC_ASN1DecoderContext *cx, |
3105 | | unsigned long max_size) |
3106 | 8 | { |
3107 | 8 | cx->max_element_size = max_size; |
3108 | 8 | } |
3109 | | |
3110 | | void |
3111 | | SEC_ASN1DecoderAbort(SEC_ASN1DecoderContext *cx, int error) |
3112 | 0 | { |
3113 | 0 | PORT_Assert(cx); |
3114 | 0 | PORT_SetError(error); |
3115 | 0 | cx->status = decodeError; |
3116 | 0 | } |
3117 | | |
3118 | | SECStatus |
3119 | | SEC_ASN1Decode(PLArenaPool *poolp, void *dest, |
3120 | | const SEC_ASN1Template *theTemplate, |
3121 | | const char *buf, long len) |
3122 | 8 | { |
3123 | 8 | SEC_ASN1DecoderContext *dcx; |
3124 | 8 | SECStatus urv, frv; |
3125 | | |
3126 | 8 | dcx = SEC_ASN1DecoderStart(poolp, dest, theTemplate); |
3127 | 8 | if (dcx == NULL) |
3128 | 0 | return SECFailure; |
3129 | | |
3130 | | /* In one-shot mode, there's no possibility of streaming data beyond the |
3131 | | * length of len */ |
3132 | 8 | SEC_ASN1DecoderSetMaximumElementSize(dcx, len); |
3133 | | |
3134 | 8 | urv = SEC_ASN1DecoderUpdate(dcx, buf, len); |
3135 | 8 | frv = SEC_ASN1DecoderFinish(dcx); |
3136 | | |
3137 | 8 | if (urv != SECSuccess) |
3138 | 0 | return urv; |
3139 | | |
3140 | 8 | return frv; |
3141 | 8 | } |
3142 | | |
3143 | | SECStatus |
3144 | | SEC_ASN1DecodeItem(PLArenaPool *poolp, void *dest, |
3145 | | const SEC_ASN1Template *theTemplate, |
3146 | | const SECItem *src) |
3147 | 8 | { |
3148 | 8 | return SEC_ASN1Decode(poolp, dest, theTemplate, |
3149 | 8 | (const char *)src->data, src->len); |
3150 | 8 | } |
3151 | | |
3152 | | #ifdef DEBUG_ASN1D_STATES |
3153 | | void |
3154 | | sec_asn1d_Assert(const char *s, const char *file, PRIntn ln) |
3155 | | { |
3156 | | printf("Assertion failed, \"%s\", file %s, line %d\n", s, file, ln); |
3157 | | fflush(stdout); |
3158 | | } |
3159 | | #endif |
3160 | | |
3161 | | /* |
3162 | | * Generic templates for individual/simple items and pointers to |
3163 | | * and sets of same. |
3164 | | * |
3165 | | * If you need to add a new one, please note the following: |
3166 | | * - For each new basic type you should add *four* templates: |
3167 | | * one plain, one PointerTo, one SequenceOf and one SetOf. |
3168 | | * - If the new type can be constructed (meaning, it is a |
3169 | | * *string* type according to BER/DER rules), then you should |
3170 | | * or-in SEC_ASN1_MAY_STREAM to the type in the basic template. |
3171 | | * See the definition of the OctetString template for an example. |
3172 | | * - It may not be obvious, but these are in *alphabetical* |
3173 | | * order based on the SEC_ASN1_XXX name; so put new ones in |
3174 | | * the appropriate place. |
3175 | | */ |
3176 | | |
3177 | | const SEC_ASN1Template SEC_SequenceOfAnyTemplate[] = { |
3178 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_AnyTemplate } |
3179 | | }; |
3180 | | |
3181 | | #if 0 |
3182 | | |
3183 | | const SEC_ASN1Template SEC_PointerToBitStringTemplate[] = { |
3184 | | { SEC_ASN1_POINTER, 0, SEC_BitStringTemplate } |
3185 | | }; |
3186 | | |
3187 | | const SEC_ASN1Template SEC_SequenceOfBitStringTemplate[] = { |
3188 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_BitStringTemplate } |
3189 | | }; |
3190 | | |
3191 | | const SEC_ASN1Template SEC_SetOfBitStringTemplate[] = { |
3192 | | { SEC_ASN1_SET_OF, 0, SEC_BitStringTemplate } |
3193 | | }; |
3194 | | |
3195 | | const SEC_ASN1Template SEC_PointerToBMPStringTemplate[] = { |
3196 | | { SEC_ASN1_POINTER, 0, SEC_BMPStringTemplate } |
3197 | | }; |
3198 | | |
3199 | | const SEC_ASN1Template SEC_SequenceOfBMPStringTemplate[] = { |
3200 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_BMPStringTemplate } |
3201 | | }; |
3202 | | |
3203 | | const SEC_ASN1Template SEC_SetOfBMPStringTemplate[] = { |
3204 | | { SEC_ASN1_SET_OF, 0, SEC_BMPStringTemplate } |
3205 | | }; |
3206 | | |
3207 | | const SEC_ASN1Template SEC_PointerToBooleanTemplate[] = { |
3208 | | { SEC_ASN1_POINTER, 0, SEC_BooleanTemplate } |
3209 | | }; |
3210 | | |
3211 | | const SEC_ASN1Template SEC_SequenceOfBooleanTemplate[] = { |
3212 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_BooleanTemplate } |
3213 | | }; |
3214 | | |
3215 | | const SEC_ASN1Template SEC_SetOfBooleanTemplate[] = { |
3216 | | { SEC_ASN1_SET_OF, 0, SEC_BooleanTemplate } |
3217 | | }; |
3218 | | |
3219 | | #endif |
3220 | | |
3221 | | const SEC_ASN1Template SEC_EnumeratedTemplate[] = { |
3222 | | { SEC_ASN1_ENUMERATED, 0, NULL, sizeof(SECItem) } |
3223 | | }; |
3224 | | |
3225 | | const SEC_ASN1Template SEC_PointerToEnumeratedTemplate[] = { |
3226 | | { SEC_ASN1_POINTER, 0, SEC_EnumeratedTemplate } |
3227 | | }; |
3228 | | |
3229 | | #if 0 |
3230 | | |
3231 | | const SEC_ASN1Template SEC_SequenceOfEnumeratedTemplate[] = { |
3232 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_EnumeratedTemplate } |
3233 | | }; |
3234 | | |
3235 | | #endif |
3236 | | |
3237 | | const SEC_ASN1Template SEC_SetOfEnumeratedTemplate[] = { |
3238 | | { SEC_ASN1_SET_OF, 0, SEC_EnumeratedTemplate } |
3239 | | }; |
3240 | | |
3241 | | const SEC_ASN1Template SEC_PointerToGeneralizedTimeTemplate[] = { |
3242 | | { SEC_ASN1_POINTER, 0, SEC_GeneralizedTimeTemplate } |
3243 | | }; |
3244 | | |
3245 | | #if 0 |
3246 | | |
3247 | | const SEC_ASN1Template SEC_SequenceOfGeneralizedTimeTemplate[] = { |
3248 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_GeneralizedTimeTemplate } |
3249 | | }; |
3250 | | |
3251 | | const SEC_ASN1Template SEC_SetOfGeneralizedTimeTemplate[] = { |
3252 | | { SEC_ASN1_SET_OF, 0, SEC_GeneralizedTimeTemplate } |
3253 | | }; |
3254 | | |
3255 | | const SEC_ASN1Template SEC_PointerToIA5StringTemplate[] = { |
3256 | | { SEC_ASN1_POINTER, 0, SEC_IA5StringTemplate } |
3257 | | }; |
3258 | | |
3259 | | const SEC_ASN1Template SEC_SequenceOfIA5StringTemplate[] = { |
3260 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_IA5StringTemplate } |
3261 | | }; |
3262 | | |
3263 | | const SEC_ASN1Template SEC_SetOfIA5StringTemplate[] = { |
3264 | | { SEC_ASN1_SET_OF, 0, SEC_IA5StringTemplate } |
3265 | | }; |
3266 | | |
3267 | | const SEC_ASN1Template SEC_PointerToIntegerTemplate[] = { |
3268 | | { SEC_ASN1_POINTER, 0, SEC_IntegerTemplate } |
3269 | | }; |
3270 | | |
3271 | | const SEC_ASN1Template SEC_SequenceOfIntegerTemplate[] = { |
3272 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_IntegerTemplate } |
3273 | | }; |
3274 | | |
3275 | | const SEC_ASN1Template SEC_SetOfIntegerTemplate[] = { |
3276 | | { SEC_ASN1_SET_OF, 0, SEC_IntegerTemplate } |
3277 | | }; |
3278 | | |
3279 | | const SEC_ASN1Template SEC_PointerToNullTemplate[] = { |
3280 | | { SEC_ASN1_POINTER, 0, SEC_NullTemplate } |
3281 | | }; |
3282 | | |
3283 | | const SEC_ASN1Template SEC_SequenceOfNullTemplate[] = { |
3284 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_NullTemplate } |
3285 | | }; |
3286 | | |
3287 | | const SEC_ASN1Template SEC_SetOfNullTemplate[] = { |
3288 | | { SEC_ASN1_SET_OF, 0, SEC_NullTemplate } |
3289 | | }; |
3290 | | |
3291 | | const SEC_ASN1Template SEC_PointerToObjectIDTemplate[] = { |
3292 | | { SEC_ASN1_POINTER, 0, SEC_ObjectIDTemplate } |
3293 | | }; |
3294 | | |
3295 | | #endif |
3296 | | |
3297 | | const SEC_ASN1Template SEC_SequenceOfObjectIDTemplate[] = { |
3298 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_ObjectIDTemplate } |
3299 | | }; |
3300 | | |
3301 | | #if 0 |
3302 | | |
3303 | | const SEC_ASN1Template SEC_SetOfObjectIDTemplate[] = { |
3304 | | { SEC_ASN1_SET_OF, 0, SEC_ObjectIDTemplate } |
3305 | | }; |
3306 | | |
3307 | | const SEC_ASN1Template SEC_SequenceOfOctetStringTemplate[] = { |
3308 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_OctetStringTemplate } |
3309 | | }; |
3310 | | |
3311 | | const SEC_ASN1Template SEC_SetOfOctetStringTemplate[] = { |
3312 | | { SEC_ASN1_SET_OF, 0, SEC_OctetStringTemplate } |
3313 | | }; |
3314 | | |
3315 | | #endif |
3316 | | |
3317 | | const SEC_ASN1Template SEC_PrintableStringTemplate[] = { |
3318 | | { SEC_ASN1_PRINTABLE_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem) } |
3319 | | }; |
3320 | | |
3321 | | #if 0 |
3322 | | |
3323 | | const SEC_ASN1Template SEC_PointerToPrintableStringTemplate[] = { |
3324 | | { SEC_ASN1_POINTER, 0, SEC_PrintableStringTemplate } |
3325 | | }; |
3326 | | |
3327 | | const SEC_ASN1Template SEC_SequenceOfPrintableStringTemplate[] = { |
3328 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_PrintableStringTemplate } |
3329 | | }; |
3330 | | |
3331 | | const SEC_ASN1Template SEC_SetOfPrintableStringTemplate[] = { |
3332 | | { SEC_ASN1_SET_OF, 0, SEC_PrintableStringTemplate } |
3333 | | }; |
3334 | | |
3335 | | #endif |
3336 | | |
3337 | | const SEC_ASN1Template SEC_T61StringTemplate[] = { |
3338 | | { SEC_ASN1_T61_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem) } |
3339 | | }; |
3340 | | |
3341 | | #if 0 |
3342 | | |
3343 | | const SEC_ASN1Template SEC_PointerToT61StringTemplate[] = { |
3344 | | { SEC_ASN1_POINTER, 0, SEC_T61StringTemplate } |
3345 | | }; |
3346 | | |
3347 | | const SEC_ASN1Template SEC_SequenceOfT61StringTemplate[] = { |
3348 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_T61StringTemplate } |
3349 | | }; |
3350 | | |
3351 | | const SEC_ASN1Template SEC_SetOfT61StringTemplate[] = { |
3352 | | { SEC_ASN1_SET_OF, 0, SEC_T61StringTemplate } |
3353 | | }; |
3354 | | |
3355 | | #endif |
3356 | | |
3357 | | const SEC_ASN1Template SEC_UniversalStringTemplate[] = { |
3358 | | { SEC_ASN1_UNIVERSAL_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem) } |
3359 | | }; |
3360 | | |
3361 | | #if 0 |
3362 | | |
3363 | | const SEC_ASN1Template SEC_PointerToUniversalStringTemplate[] = { |
3364 | | { SEC_ASN1_POINTER, 0, SEC_UniversalStringTemplate } |
3365 | | }; |
3366 | | |
3367 | | const SEC_ASN1Template SEC_SequenceOfUniversalStringTemplate[] = { |
3368 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_UniversalStringTemplate } |
3369 | | }; |
3370 | | |
3371 | | const SEC_ASN1Template SEC_SetOfUniversalStringTemplate[] = { |
3372 | | { SEC_ASN1_SET_OF, 0, SEC_UniversalStringTemplate } |
3373 | | }; |
3374 | | |
3375 | | const SEC_ASN1Template SEC_PointerToUTCTimeTemplate[] = { |
3376 | | { SEC_ASN1_POINTER, 0, SEC_UTCTimeTemplate } |
3377 | | }; |
3378 | | |
3379 | | const SEC_ASN1Template SEC_SequenceOfUTCTimeTemplate[] = { |
3380 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_UTCTimeTemplate } |
3381 | | }; |
3382 | | |
3383 | | const SEC_ASN1Template SEC_SetOfUTCTimeTemplate[] = { |
3384 | | { SEC_ASN1_SET_OF, 0, SEC_UTCTimeTemplate } |
3385 | | }; |
3386 | | |
3387 | | const SEC_ASN1Template SEC_PointerToUTF8StringTemplate[] = { |
3388 | | { SEC_ASN1_POINTER, 0, SEC_UTF8StringTemplate } |
3389 | | }; |
3390 | | |
3391 | | const SEC_ASN1Template SEC_SequenceOfUTF8StringTemplate[] = { |
3392 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_UTF8StringTemplate } |
3393 | | }; |
3394 | | |
3395 | | const SEC_ASN1Template SEC_SetOfUTF8StringTemplate[] = { |
3396 | | { SEC_ASN1_SET_OF, 0, SEC_UTF8StringTemplate } |
3397 | | }; |
3398 | | |
3399 | | #endif |
3400 | | |
3401 | | const SEC_ASN1Template SEC_VisibleStringTemplate[] = { |
3402 | | { SEC_ASN1_VISIBLE_STRING | SEC_ASN1_MAY_STREAM, 0, NULL, sizeof(SECItem) } |
3403 | | }; |
3404 | | |
3405 | | #if 0 |
3406 | | |
3407 | | const SEC_ASN1Template SEC_PointerToVisibleStringTemplate[] = { |
3408 | | { SEC_ASN1_POINTER, 0, SEC_VisibleStringTemplate } |
3409 | | }; |
3410 | | |
3411 | | const SEC_ASN1Template SEC_SequenceOfVisibleStringTemplate[] = { |
3412 | | { SEC_ASN1_SEQUENCE_OF, 0, SEC_VisibleStringTemplate } |
3413 | | }; |
3414 | | |
3415 | | const SEC_ASN1Template SEC_SetOfVisibleStringTemplate[] = { |
3416 | | { SEC_ASN1_SET_OF, 0, SEC_VisibleStringTemplate } |
3417 | | }; |
3418 | | |
3419 | | #endif |
3420 | | |
3421 | | /* |
3422 | | * Template for skipping a subitem. |
3423 | | * |
3424 | | * Note that it only makes sense to use this for decoding (when you want |
3425 | | * to decode something where you are only interested in one or two of |
3426 | | * the fields); you cannot encode a SKIP! |
3427 | | */ |
3428 | | const SEC_ASN1Template SEC_SkipTemplate[] = { |
3429 | | { SEC_ASN1_SKIP } |
3430 | | }; |
3431 | | |
3432 | | /* These functions simply return the address of the above-declared templates. |
3433 | | ** This is necessary for Windows DLLs. Sigh. |
3434 | | */ |
3435 | | SEC_ASN1_CHOOSER_IMPLEMENT(SEC_EnumeratedTemplate) |
3436 | | SEC_ASN1_CHOOSER_IMPLEMENT(SEC_PointerToEnumeratedTemplate) |
3437 | | SEC_ASN1_CHOOSER_IMPLEMENT(SEC_SequenceOfAnyTemplate) |
3438 | | SEC_ASN1_CHOOSER_IMPLEMENT(SEC_SequenceOfObjectIDTemplate) |
3439 | | SEC_ASN1_CHOOSER_IMPLEMENT(SEC_SkipTemplate) |
3440 | | SEC_ASN1_CHOOSER_IMPLEMENT(SEC_UniversalStringTemplate) |
3441 | | SEC_ASN1_CHOOSER_IMPLEMENT(SEC_PrintableStringTemplate) |
3442 | | SEC_ASN1_CHOOSER_IMPLEMENT(SEC_T61StringTemplate) |
3443 | | SEC_ASN1_CHOOSER_IMPLEMENT(SEC_PointerToGeneralizedTimeTemplate) |