Coverage Report

Created: 2026-11-15 06:55

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