/
secasn1d.c
3413 lines (2991 loc) · 110 KB
/
secasn1d.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Support for DEcoding ASN.1 data based on BER/DER (Basic/Distinguished
* Encoding Rules).
*/
/* #define DEBUG_ASN1D_STATES 1 */
#ifdef DEBUG_ASN1D_STATES
#include <stdio.h>
#define PR_Assert sec_asn1d_Assert
#endif
#include <limits.h>
#include "secasn1.h"
#include "secerr.h"
typedef enum {
beforeIdentifier,
duringIdentifier,
afterIdentifier,
beforeLength,
duringLength,
afterLength,
beforeBitString,
duringBitString,
duringConstructedString,
duringGroup,
duringLeaf,
duringSaveEncoding,
duringSequence,
afterConstructedString,
afterGroup,
afterExplicit,
afterImplicit,
afterInline,
afterPointer,
afterSaveEncoding,
beforeEndOfContents,
duringEndOfContents,
afterEndOfContents,
beforeChoice,
duringChoice,
afterChoice,
notInUse
} sec_asn1d_parse_place;
#ifdef DEBUG_ASN1D_STATES
static const char *const place_names[] = {
"beforeIdentifier",
"duringIdentifier",
"afterIdentifier",
"beforeLength",
"duringLength",
"afterLength",
"beforeBitString",
"duringBitString",
"duringConstructedString",
"duringGroup",
"duringLeaf",
"duringSaveEncoding",
"duringSequence",
"afterConstructedString",
"afterGroup",
"afterExplicit",
"afterImplicit",
"afterInline",
"afterPointer",
"afterSaveEncoding",
"beforeEndOfContents",
"duringEndOfContents",
"afterEndOfContents",
"beforeChoice",
"duringChoice",
"afterChoice",
"notInUse"
};
static const char *const class_names[] = {
"UNIVERSAL",
"APPLICATION",
"CONTEXT_SPECIFIC",
"PRIVATE"
};
static const char *const method_names[] = { "PRIMITIVE", "CONSTRUCTED" };
static const char *const type_names[] = {
"END_OF_CONTENTS",
"BOOLEAN",
"INTEGER",
"BIT_STRING",
"OCTET_STRING",
"NULL",
"OBJECT_ID",
"OBJECT_DESCRIPTOR",
"(type 08)",
"REAL",
"ENUMERATED",
"EMBEDDED",
"UTF8_STRING",
"(type 0d)",
"(type 0e)",
"(type 0f)",
"SEQUENCE",
"SET",
"NUMERIC_STRING",
"PRINTABLE_STRING",
"T61_STRING",
"VIDEOTEXT_STRING",
"IA5_STRING",
"UTC_TIME",
"GENERALIZED_TIME",
"GRAPHIC_STRING",
"VISIBLE_STRING",
"GENERAL_STRING",
"UNIVERSAL_STRING",
"(type 1d)",
"BMP_STRING",
"HIGH_TAG_VALUE"
};
static const char *const flag_names[] = {
/* flags, right to left */
"OPTIONAL",
"EXPLICIT",
"ANY",
"INLINE",
"POINTER",
"GROUP",
"DYNAMIC",
"SKIP",
"INNER",
"SAVE",
"", /* decoder ignores "MAY_STREAM", */
"SKIP_REST",
"CHOICE",
"NO_STREAM",
"DEBUG_BREAK",
"unknown 08",
"unknown 10",
"unknown 20",
"unknown 40",
"unknown 80"
};
static int /* bool */
formatKind(unsigned long kind, char *buf)
{
int i;
unsigned long k = kind & SEC_ASN1_TAGNUM_MASK;
unsigned long notag = kind & (SEC_ASN1_CHOICE | SEC_ASN1_POINTER |
SEC_ASN1_INLINE | SEC_ASN1_ANY | SEC_ASN1_SAVE);
buf[0] = 0;
if ((kind & SEC_ASN1_CLASS_MASK) != SEC_ASN1_UNIVERSAL) {
sprintf(buf, " %s", class_names[(kind & SEC_ASN1_CLASS_MASK) >> 6]);
buf += strlen(buf);
}
if (kind & SEC_ASN1_METHOD_MASK) {
sprintf(buf, " %s", method_names[1]);
buf += strlen(buf);
}
if ((kind & SEC_ASN1_CLASS_MASK) == SEC_ASN1_UNIVERSAL) {
if (k || !notag) {
sprintf(buf, " %s", type_names[k]);
if ((k == SEC_ASN1_SET || k == SEC_ASN1_SEQUENCE) &&
(kind & SEC_ASN1_GROUP)) {
buf += strlen(buf);
sprintf(buf, "_OF");
}
}
} else {
sprintf(buf, " [%lu]", k);
}
buf += strlen(buf);
for (k = kind >> 8, i = 0; k; k >>= 1, ++i) {
if (k & 1) {
sprintf(buf, " %s", flag_names[i]);
buf += strlen(buf);
}
}
return notag != 0;
}
#endif /* DEBUG_ASN1D_STATES */
typedef enum {
allDone,
decodeError,
keepGoing,
needBytes
} sec_asn1d_parse_status;
struct subitem {
const void *data;
unsigned long len; /* only used for substrings */
struct subitem *next;
};
typedef struct sec_asn1d_state_struct {
SEC_ASN1DecoderContext *top;
const SEC_ASN1Template *theTemplate;
void *dest;
void *our_mark; /* free on completion */
struct sec_asn1d_state_struct *parent; /* aka prev */
struct sec_asn1d_state_struct *child; /* aka next */
sec_asn1d_parse_place place;
/*
* XXX explain the next fields as clearly as possible...
*/
unsigned char found_tag_modifiers;
unsigned char expect_tag_modifiers;
unsigned long check_tag_mask;
unsigned long found_tag_number;
unsigned long expect_tag_number;
unsigned long underlying_kind;
unsigned long contents_length;
unsigned long pending;
unsigned long consumed;
int depth;
/*
* Bit strings have their length adjusted -- the first octet of the
* contents contains a value between 0 and 7 which says how many bits
* at the end of the octets are not actually part of the bit string;
* when parsing bit strings we put that value here because we need it
* later, for adjustment of the length (when the whole string is done).
*/
unsigned int bit_string_unused_bits;
/*
* The following are used for indefinite-length constructed strings.
*/
struct subitem *subitems_head;
struct subitem *subitems_tail;
PRPackedBool
allocate, /* when true, need to allocate the destination */
endofcontents, /* this state ended up parsing end-of-contents octets */
explicit, /* we are handling an explicit header */
indefinite, /* the current item has indefinite-length encoding */
missing, /* an optional field that was not present */
optional, /* the template says this field may be omitted */
substring; /* this is a substring of a constructed string */
} sec_asn1d_state;
#define IS_HIGH_TAG_NUMBER(n) ((n) == SEC_ASN1_HIGH_TAG_NUMBER)
#define LAST_TAG_NUMBER_BYTE(b) (((b)&0x80) == 0)
#define TAG_NUMBER_BITS 7
#define TAG_NUMBER_MASK 0x7f
#define LENGTH_IS_SHORT_FORM(b) (((b)&0x80) == 0)
#define LONG_FORM_LENGTH(b) ((b)&0x7f)
#define HIGH_BITS(field, cnt) ((field) >> ((sizeof(field) * 8) - (cnt)))
/*
* An "outsider" will have an opaque pointer to this, created by calling
* SEC_ASN1DecoderStart(). It will be passed back in to all subsequent
* calls to SEC_ASN1DecoderUpdate(), and when done it is passed to
* SEC_ASN1DecoderFinish().
*/
struct sec_DecoderContext_struct {
PLArenaPool *our_pool; /* for our internal allocs */
PLArenaPool *their_pool; /* for destination structure allocs */
#ifdef SEC_ASN1D_FREE_ON_ERROR /* \
* XXX see comment below (by same \
* ifdef) that explains why this \
* does not work (need more smarts \
* in order to free back to mark) \
*/
/*
* XXX how to make their_mark work in the case where they do NOT
* give us a pool pointer?
*/
void *their_mark; /* free on error */
#endif
sec_asn1d_state *current;
sec_asn1d_parse_status status;
/* The maximum size the caller is willing to allow a single element
* to be before returning an error.
*
* In the case of an indefinite length element, this is the sum total
* of all child elements.
*
* In the case of a definite length element, this represents the maximum
* size of the top-level element.
*/
unsigned long max_element_size;
SEC_ASN1NotifyProc notify_proc; /* call before/after handling field */
void *notify_arg; /* argument to notify_proc */
PRBool during_notify; /* true during call to notify_proc */
SEC_ASN1WriteProc filter_proc; /* pass field bytes to this */
void *filter_arg; /* argument to that function */
PRBool filter_only; /* do not allocate/store fields */
};
/*
* XXX this is a fairly generic function that may belong elsewhere
*/
static void *
sec_asn1d_alloc(PLArenaPool *poolp, unsigned long len)
{
void *thing;
if (poolp != NULL) {
/*
* Allocate from the pool.
*/
thing = PORT_ArenaAlloc(poolp, len);
} else {
/*
* Allocate generically.
*/
thing = PORT_Alloc(len);
}
return thing;
}
/*
* XXX this is a fairly generic function that may belong elsewhere
*/
static void *
sec_asn1d_zalloc(PLArenaPool *poolp, unsigned long len)
{
void *thing;
thing = sec_asn1d_alloc(poolp, len);
if (thing != NULL)
PORT_Memset(thing, 0, len);
return thing;
}
static sec_asn1d_state *
sec_asn1d_push_state(SEC_ASN1DecoderContext *cx,
const SEC_ASN1Template *theTemplate,
void *dest, PRBool new_depth)
{
sec_asn1d_state *state, *new_state;
state = cx->current;
PORT_Assert(state == NULL || state->child == NULL);
if (state != NULL) {
PORT_Assert(state->our_mark == NULL);
state->our_mark = PORT_ArenaMark(cx->our_pool);
}
new_state = (sec_asn1d_state *)sec_asn1d_zalloc(cx->our_pool,
sizeof(*new_state));
if (new_state == NULL) {
goto loser;
}
new_state->top = cx;
new_state->parent = state;
new_state->theTemplate = theTemplate;
new_state->place = notInUse;
if (dest != NULL)
new_state->dest = (char *)dest + theTemplate->offset;
if (state != NULL) {
new_state->depth = state->depth;
if (new_depth) {
if (++new_state->depth > SEC_ASN1D_MAX_DEPTH) {
PORT_SetError(SEC_ERROR_BAD_DER);
goto loser;
}
}
state->child = new_state;
}
cx->current = new_state;
return new_state;
loser:
cx->status = decodeError;
if (state != NULL) {
PORT_ArenaRelease(cx->our_pool, state->our_mark);
state->our_mark = NULL;
}
return NULL;
}
static void
sec_asn1d_scrub_state(sec_asn1d_state *state)
{
/*
* Some default "scrubbing".
* XXX right set of initializations?
*/
state->place = beforeIdentifier;
state->endofcontents = PR_FALSE;
state->indefinite = PR_FALSE;
state->missing = PR_FALSE;
PORT_Assert(state->consumed == 0);
}
static void
sec_asn1d_notify_before(SEC_ASN1DecoderContext *cx, void *dest, int depth)
{
if (cx->notify_proc == NULL)
return;
cx->during_notify = PR_TRUE;
(*cx->notify_proc)(cx->notify_arg, PR_TRUE, dest, depth);
cx->during_notify = PR_FALSE;
}
static void
sec_asn1d_notify_after(SEC_ASN1DecoderContext *cx, void *dest, int depth)
{
if (cx->notify_proc == NULL)
return;
cx->during_notify = PR_TRUE;
(*cx->notify_proc)(cx->notify_arg, PR_FALSE, dest, depth);
cx->during_notify = PR_FALSE;
}
static sec_asn1d_state *
sec_asn1d_init_state_based_on_template(sec_asn1d_state *state)
{
PRBool explicit, optional, universal;
unsigned char expect_tag_modifiers;
unsigned long encode_kind, under_kind;
unsigned long check_tag_mask, expect_tag_number;
/* XXX Check that both of these tests are really needed/appropriate. */
if (state == NULL || state->top->status == decodeError)
return state;
encode_kind = state->theTemplate->kind;
if (encode_kind & SEC_ASN1_SAVE) {
/*
* This is a "magic" field that saves away all bytes, allowing
* the immediately following field to still be decoded from this
* same spot -- sort of a fork.
*/
/* check that there are no extraneous bits */
PORT_Assert(encode_kind == SEC_ASN1_SAVE);
if (state->top->filter_only) {
/*
* If we are not storing, then we do not do the SAVE field
* at all. Just move ahead to the "real" field instead,
* doing the appropriate notify calls before and after.
*/
sec_asn1d_notify_after(state->top, state->dest, state->depth);
/*
* Since we are not storing, allow for our current dest value
* to be NULL. (This might not actually occur, but right now I
* cannot convince myself one way or the other.) If it is NULL,
* assume that our parent dest can help us out.
*/
if (state->dest == NULL)
state->dest = state->parent->dest;
else
state->dest = (char *)state->dest - state->theTemplate->offset;
state->theTemplate++;
if (state->dest != NULL)
state->dest = (char *)state->dest + state->theTemplate->offset;
sec_asn1d_notify_before(state->top, state->dest, state->depth);
encode_kind = state->theTemplate->kind;
PORT_Assert((encode_kind & SEC_ASN1_SAVE) == 0);
} else {
sec_asn1d_scrub_state(state);
state->place = duringSaveEncoding;
state = sec_asn1d_push_state(state->top, SEC_AnyTemplate,
state->dest, PR_FALSE);
if (state != NULL)
state = sec_asn1d_init_state_based_on_template(state);
return state;
}
}
universal = ((encode_kind & SEC_ASN1_CLASS_MASK) == SEC_ASN1_UNIVERSAL)
? PR_TRUE
: PR_FALSE;
explicit = (encode_kind & SEC_ASN1_EXPLICIT) ? PR_TRUE : PR_FALSE;
encode_kind &= ~SEC_ASN1_EXPLICIT;
optional = (encode_kind & SEC_ASN1_OPTIONAL) ? PR_TRUE : PR_FALSE;
encode_kind &= ~SEC_ASN1_OPTIONAL;
PORT_Assert(!(explicit && universal)); /* bad templates */
encode_kind &= ~SEC_ASN1_DYNAMIC;
encode_kind &= ~SEC_ASN1_MAY_STREAM;
if (encode_kind & SEC_ASN1_CHOICE) {
#if 0 /* XXX remove? */
sec_asn1d_state *child = sec_asn1d_push_state(state->top, state->theTemplate, state->dest, PR_FALSE);
if ((sec_asn1d_state *)NULL == child) {
return (sec_asn1d_state *)NULL;
}
child->allocate = state->allocate;
child->place = beforeChoice;
return child;
#else
state->place = beforeChoice;
return state;
#endif
}
if ((encode_kind & (SEC_ASN1_POINTER | SEC_ASN1_INLINE)) || (!universal && !explicit)) {
const SEC_ASN1Template *subt;
void *dest;
PRBool child_allocate;
PORT_Assert((encode_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP)) == 0);
sec_asn1d_scrub_state(state);
child_allocate = PR_FALSE;
if (encode_kind & SEC_ASN1_POINTER) {
/*
* A POINTER means we need to allocate the destination for
* this field. But, since it may also be an optional field,
* we defer the allocation until later; we just record that
* it needs to be done.
*
* There are two possible scenarios here -- one is just a
* plain POINTER (kind of like INLINE, except with allocation)
* and the other is an implicitly-tagged POINTER. We don't
* need to do anything special here for the two cases, but
* since the template definition can be tricky, we do check
* that there are no extraneous bits set in encode_kind.
*
* XXX The same conditions which assert should set an error.
*/
if (universal) {
/*
* "universal" means this entry is a standalone POINTER;
* there should be no other bits set in encode_kind.
*/
PORT_Assert(encode_kind == SEC_ASN1_POINTER);
} else {
/*
* If we get here we have an implicitly-tagged field
* that needs to be put into a POINTER. The subtemplate
* will determine how to decode the field, but encode_kind
* describes the (implicit) tag we are looking for.
* The non-tag bits of encode_kind will be ignored by
* the code below; none of them should be set, however,
* except for the POINTER bit itself -- so check that.
*/
PORT_Assert((encode_kind & ~SEC_ASN1_TAG_MASK) == SEC_ASN1_POINTER);
}
if (!state->top->filter_only)
child_allocate = PR_TRUE;
dest = NULL;
state->place = afterPointer;
} else {
dest = state->dest;
if (encode_kind & SEC_ASN1_INLINE) {
/* check that there are no extraneous bits */
PORT_Assert(encode_kind == SEC_ASN1_INLINE && !optional);
state->place = afterInline;
} else {
state->place = afterImplicit;
}
}
state->optional = optional;
subt = SEC_ASN1GetSubtemplate(state->theTemplate, state->dest, PR_FALSE);
state = sec_asn1d_push_state(state->top, subt, dest, PR_FALSE);
if (state == NULL)
return NULL;
state->allocate = child_allocate;
if (universal) {
state = sec_asn1d_init_state_based_on_template(state);
if (state != NULL) {
/*
* If this field is optional, we need to record that on
* the pushed child so it won't fail if the field isn't
* found. I can't think of a way that this new state
* could already have optional set (which we would wipe
* out below if our local optional is not set) -- but
* just to be sure, assert that it isn't set.
*/
PORT_Assert(!state->optional);
state->optional = optional;
}
return state;
}
under_kind = state->theTemplate->kind;
under_kind &= ~SEC_ASN1_MAY_STREAM;
} else if (explicit) {
/*
* For explicit, we only need to match the encoding tag next,
* then we will push another state to handle the entire inner
* part. In this case, there is no underlying kind which plays
* any part in the determination of the outer, explicit tag.
* So we just set under_kind to 0, which is not a valid tag,
* and the rest of the tag matching stuff should be okay.
*/
under_kind = 0;
} else {
/*
* Nothing special; the underlying kind and the given encoding
* information are the same.
*/
under_kind = encode_kind;
}
/* XXX is this the right set of bits to test here? */
PORT_Assert((under_kind & (SEC_ASN1_EXPLICIT | SEC_ASN1_OPTIONAL | SEC_ASN1_MAY_STREAM | SEC_ASN1_INLINE | SEC_ASN1_POINTER)) == 0);
if (encode_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP)) {
PORT_Assert(encode_kind == under_kind);
if (encode_kind & SEC_ASN1_SKIP) {
PORT_Assert(!optional);
PORT_Assert(encode_kind == SEC_ASN1_SKIP);
state->dest = NULL;
}
check_tag_mask = 0;
expect_tag_modifiers = 0;
expect_tag_number = 0;
} else {
check_tag_mask = SEC_ASN1_TAG_MASK;
expect_tag_modifiers = (unsigned char)encode_kind & SEC_ASN1_TAG_MASK & ~SEC_ASN1_TAGNUM_MASK;
/*
* XXX This assumes only single-octet identifiers. To handle
* the HIGH TAG form we would need to do some more work, especially
* in how to specify them in the template, because right now we
* do not provide a way to specify more *tag* bits in encode_kind.
*/
expect_tag_number = encode_kind & SEC_ASN1_TAGNUM_MASK;
switch (under_kind & SEC_ASN1_TAGNUM_MASK) {
case SEC_ASN1_SET:
/*
* XXX A plain old SET (as opposed to a SET OF) is not implemented.
* If it ever is, remove this assert...
*/
PORT_Assert((under_kind & SEC_ASN1_GROUP) != 0);
/* fallthru */
case SEC_ASN1_SEQUENCE:
expect_tag_modifiers |= SEC_ASN1_CONSTRUCTED;
break;
case SEC_ASN1_BIT_STRING:
case SEC_ASN1_BMP_STRING:
case SEC_ASN1_GENERALIZED_TIME:
case SEC_ASN1_IA5_STRING:
case SEC_ASN1_OCTET_STRING:
case SEC_ASN1_PRINTABLE_STRING:
case SEC_ASN1_T61_STRING:
case SEC_ASN1_UNIVERSAL_STRING:
case SEC_ASN1_UTC_TIME:
case SEC_ASN1_UTF8_STRING:
case SEC_ASN1_VISIBLE_STRING:
check_tag_mask &= ~SEC_ASN1_CONSTRUCTED;
break;
}
}
state->check_tag_mask = check_tag_mask;
state->expect_tag_modifiers = expect_tag_modifiers;
state->expect_tag_number = expect_tag_number;
state->underlying_kind = under_kind;
state->explicit = explicit;
state->optional = optional;
sec_asn1d_scrub_state(state);
return state;
}
static sec_asn1d_state *
sec_asn1d_get_enclosing_construct(sec_asn1d_state *state)
{
for (state = state->parent; state; state = state->parent) {
sec_asn1d_parse_place place = state->place;
if (place != afterImplicit &&
place != afterPointer &&
place != afterInline &&
place != afterSaveEncoding &&
place != duringSaveEncoding &&
place != duringChoice) {
/* we've walked up the stack to a state that represents
** the enclosing construct.
*/
break;
}
}
return state;
}
static PRBool
sec_asn1d_parent_allows_EOC(sec_asn1d_state *state)
{
/* get state of enclosing construct. */
state = sec_asn1d_get_enclosing_construct(state);
if (state) {
sec_asn1d_parse_place place = state->place;
/* Is it one of the types that permits an unexpected EOC? */
int eoc_permitted =
(place == duringGroup ||
place == duringConstructedString ||
state->child->optional);
return (state->indefinite && eoc_permitted) ? PR_TRUE : PR_FALSE;
}
return PR_FALSE;
}
static unsigned long
sec_asn1d_parse_identifier(sec_asn1d_state *state,
const char *buf, unsigned long len)
{
unsigned char byte;
unsigned char tag_number;
PORT_Assert(state->place == beforeIdentifier);
if (len == 0) {
state->top->status = needBytes;
return 0;
}
byte = (unsigned char)*buf;
#ifdef DEBUG_ASN1D_STATES
{
char kindBuf[256];
formatKind(byte, kindBuf);
printf("Found tag %02x %s\n", byte, kindBuf);
}
#endif
tag_number = byte & SEC_ASN1_TAGNUM_MASK;
if (IS_HIGH_TAG_NUMBER(tag_number)) {
state->place = duringIdentifier;
state->found_tag_number = 0;
/*
* Actually, we have no idea how many bytes are pending, but we
* do know that it is at least 1. That is all we know; we have
* to look at each byte to know if there is another, etc.
*/
state->pending = 1;
} else {
if (byte == 0 && sec_asn1d_parent_allows_EOC(state)) {
/*
* Our parent has indefinite-length encoding, and the
* entire tag found is 0, so it seems that we have hit the
* end-of-contents octets. To handle this, we just change
* our state to that which expects to get the bytes of the
* end-of-contents octets and let that code re-read this byte
* so that our categorization of field types is correct.
* After that, our parent will then deal with everything else.
*/
state->place = duringEndOfContents;
state->pending = 2;
state->found_tag_number = 0;
state->found_tag_modifiers = 0;
/*
* We might be an optional field that is, as we now find out,
* missing. Give our parent a clue that this happened.
*/
if (state->optional)
state->missing = PR_TRUE;
return 0;
}
state->place = afterIdentifier;
state->found_tag_number = tag_number;
}
state->found_tag_modifiers = byte & ~SEC_ASN1_TAGNUM_MASK;
return 1;
}
static unsigned long
sec_asn1d_parse_more_identifier(sec_asn1d_state *state,
const char *buf, unsigned long len)
{
unsigned char byte;
int count;
PORT_Assert(state->pending == 1);
PORT_Assert(state->place == duringIdentifier);
if (len == 0) {
state->top->status = needBytes;
return 0;
}
count = 0;
while (len && state->pending) {
if (HIGH_BITS(state->found_tag_number, TAG_NUMBER_BITS) != 0) {
/*
* The given high tag number overflows our container;
* just give up. This is not likely to *ever* happen.
*/
PORT_SetError(SEC_ERROR_BAD_DER);
state->top->status = decodeError;
return 0;
}
state->found_tag_number <<= TAG_NUMBER_BITS;
byte = (unsigned char)buf[count++];
state->found_tag_number |= (byte & TAG_NUMBER_MASK);
len--;
if (LAST_TAG_NUMBER_BYTE(byte))
state->pending = 0;
}
if (state->pending == 0)
state->place = afterIdentifier;
return count;
}
static void
sec_asn1d_confirm_identifier(sec_asn1d_state *state)
{
PRBool match;
PORT_Assert(state->place == afterIdentifier);
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));
if (match) {
state->place = beforeLength;
} else {
if (state->optional) {
state->missing = PR_TRUE;
state->place = afterEndOfContents;
} else {
PORT_SetError(SEC_ERROR_BAD_DER);
state->top->status = decodeError;
}
}
}
static unsigned long
sec_asn1d_parse_length(sec_asn1d_state *state,
const char *buf, unsigned long len)
{
unsigned char byte;
PORT_Assert(state->place == beforeLength);
if (len == 0) {
state->top->status = needBytes;
return 0;
}
/*
* The default/likely outcome. It may get adjusted below.
*/
state->place = afterLength;
byte = (unsigned char)*buf;
if (LENGTH_IS_SHORT_FORM(byte)) {
state->contents_length = byte;
} else {
state->contents_length = 0;
state->pending = LONG_FORM_LENGTH(byte);
if (state->pending == 0) {
state->indefinite = PR_TRUE;
} else {
state->place = duringLength;
}
}
/* If we're parsing an ANY, SKIP, or SAVE template, and
** the object being saved is definite length encoded and constructed,
** there's no point in decoding that construct's members.
** So, just forget it's constructed and treat it as primitive.
** (SAVE appears as an ANY at this point)
*/
if (!state->indefinite &&
(state->underlying_kind & (SEC_ASN1_ANY | SEC_ASN1_SKIP))) {
state->found_tag_modifiers &= ~SEC_ASN1_CONSTRUCTED;
}
return 1;
}
static unsigned long
sec_asn1d_parse_more_length(sec_asn1d_state *state,
const char *buf, unsigned long len)
{
int count;
PORT_Assert(state->pending > 0);
PORT_Assert(state->place == duringLength);
if (len == 0) {
state->top->status = needBytes;
return 0;
}
count = 0;
while (len && state->pending) {
if (HIGH_BITS(state->contents_length, 9) != 0) {
/*
* The given full content length overflows our container;
* just give up.
*/
PORT_SetError(SEC_ERROR_BAD_DER);
state->top->status = decodeError;
return 0;
}
state->contents_length <<= 8;
state->contents_length |= (unsigned char)buf[count++];
len--;
state->pending--;
}
if (state->pending == 0)
state->place = afterLength;
return count;
}
/*
* Helper function for sec_asn1d_prepare_for_contents.
* Checks that a value representing a number of bytes consumed can be
* subtracted from a remaining length. If so, returns PR_TRUE.
* Otherwise, sets the error SEC_ERROR_BAD_DER, indicates that there was a
* decoding error in the given SEC_ASN1DecoderContext, and returns PR_FALSE.
*/
static PRBool
sec_asn1d_check_and_subtract_length(unsigned long *remaining,
unsigned long consumed,
SEC_ASN1DecoderContext *cx)
{
PORT_Assert(remaining);
PORT_Assert(cx);
if (!remaining || !cx) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
cx->status = decodeError;
return PR_FALSE;
}
if (*remaining < consumed) {
PORT_SetError(SEC_ERROR_BAD_DER);
cx->status = decodeError;
return PR_FALSE;
}
*remaining -= consumed;
return PR_TRUE;
}
static void
sec_asn1d_prepare_for_contents(sec_asn1d_state *state)
{
SECItem *item;
PLArenaPool *poolp;
unsigned long alloc_len;
sec_asn1d_state *parent;
#ifdef DEBUG_ASN1D_STATES
{
printf("Found Length %lu %s\n", state->contents_length,
state->indefinite ? "indefinite" : "");
}
#endif
/**
* The maximum length for a child element should be constrained to the
* length remaining in the first definite length element in the ancestor
* stack. If there is no definite length element in the ancestor stack,
* there's nothing to constrain the length of the child, so there's no
* further processing necessary.
*
* It's necessary to walk the ancestor stack, because it's possible to have
* definite length children that are part of an indefinite length element,
* which is itself part of an indefinite length element, and which is
* ultimately part of a definite length element. A simple example of this