This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
genname.c
1862 lines (1707 loc) · 53.1 KB
/
genname.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/. */
#include "plarena.h"
#include "seccomon.h"
#include "secitem.h"
#include "secoidt.h"
#include "secasn1.h"
#include "secder.h"
#include "certt.h"
#include "cert.h"
#include "certi.h"
#include "xconst.h"
#include "secerr.h"
#include "secoid.h"
#include "prprf.h"
#include "genname.h"
SEC_ASN1_MKSUB(SEC_AnyTemplate)
SEC_ASN1_MKSUB(SEC_IntegerTemplate)
SEC_ASN1_MKSUB(SEC_IA5StringTemplate)
SEC_ASN1_MKSUB(SEC_ObjectIDTemplate)
SEC_ASN1_MKSUB(SEC_OctetStringTemplate)
static const SEC_ASN1Template CERTNameConstraintTemplate[] = {
{ SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CERTNameConstraint) },
{ SEC_ASN1_ANY, offsetof(CERTNameConstraint, DERName) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
offsetof(CERTNameConstraint, min),
SEC_ASN1_SUB(SEC_IntegerTemplate) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 1,
offsetof(CERTNameConstraint, max),
SEC_ASN1_SUB(SEC_IntegerTemplate) },
{ 0, }
};
const SEC_ASN1Template CERT_NameConstraintSubtreeSubTemplate[] = {
{ SEC_ASN1_SEQUENCE_OF | SEC_ASN1_XTRN, 0, SEC_ASN1_SUB(SEC_AnyTemplate) }
};
static const SEC_ASN1Template CERTNameConstraintsTemplate[] = {
{ SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CERTNameConstraints) },
{ SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
offsetof(CERTNameConstraints, DERPermited),
CERT_NameConstraintSubtreeSubTemplate},
{ SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
offsetof(CERTNameConstraints, DERExcluded),
CERT_NameConstraintSubtreeSubTemplate},
{ 0, }
};
static const SEC_ASN1Template CERTOthNameTemplate[] = {
{ SEC_ASN1_SEQUENCE, 0, NULL, sizeof(OtherName) },
{ SEC_ASN1_OBJECT_ID,
offsetof(OtherName, oid) },
{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT |
SEC_ASN1_XTRN | 0, offsetof(OtherName, name),
SEC_ASN1_SUB(SEC_AnyTemplate) },
{ 0, }
};
static const SEC_ASN1Template CERTOtherNameTemplate[] = {
{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0 ,
offsetof(CERTGeneralName, name.OthName), CERTOthNameTemplate,
sizeof(CERTGeneralName) }
};
static const SEC_ASN1Template CERTOtherName2Template[] = {
{ SEC_ASN1_SEQUENCE | SEC_ASN1_CONTEXT_SPECIFIC | 0 ,
0, NULL, sizeof(CERTGeneralName) },
{ SEC_ASN1_OBJECT_ID,
offsetof(CERTGeneralName, name.OthName) + offsetof(OtherName, oid) },
{ SEC_ASN1_ANY,
offsetof(CERTGeneralName, name.OthName) + offsetof(OtherName, name) },
{ 0, }
};
static const SEC_ASN1Template CERT_RFC822NameTemplate[] = {
{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 1 ,
offsetof(CERTGeneralName, name.other),
SEC_ASN1_SUB(SEC_IA5StringTemplate),
sizeof (CERTGeneralName)}
};
static const SEC_ASN1Template CERT_DNSNameTemplate[] = {
{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 2 ,
offsetof(CERTGeneralName, name.other),
SEC_ASN1_SUB(SEC_IA5StringTemplate),
sizeof (CERTGeneralName)}
};
static const SEC_ASN1Template CERT_X400AddressTemplate[] = {
{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | SEC_ASN1_XTRN | 3,
offsetof(CERTGeneralName, name.other), SEC_ASN1_SUB(SEC_AnyTemplate),
sizeof (CERTGeneralName)}
};
static const SEC_ASN1Template CERT_DirectoryNameTemplate[] = {
{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT |
SEC_ASN1_XTRN | 4, offsetof(CERTGeneralName, derDirectoryName),
SEC_ASN1_SUB(SEC_AnyTemplate), sizeof (CERTGeneralName)}
};
static const SEC_ASN1Template CERT_EDIPartyNameTemplate[] = {
{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | SEC_ASN1_XTRN | 5,
offsetof(CERTGeneralName, name.other), SEC_ASN1_SUB(SEC_AnyTemplate),
sizeof (CERTGeneralName)}
};
static const SEC_ASN1Template CERT_URITemplate[] = {
{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 6 ,
offsetof(CERTGeneralName, name.other),
SEC_ASN1_SUB(SEC_IA5StringTemplate),
sizeof (CERTGeneralName)}
};
static const SEC_ASN1Template CERT_IPAddressTemplate[] = {
{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 7 ,
offsetof(CERTGeneralName, name.other),
SEC_ASN1_SUB(SEC_OctetStringTemplate),
sizeof (CERTGeneralName)}
};
static const SEC_ASN1Template CERT_RegisteredIDTemplate[] = {
{ SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 8 ,
offsetof(CERTGeneralName, name.other),
SEC_ASN1_SUB(SEC_ObjectIDTemplate),
sizeof (CERTGeneralName)}
};
const SEC_ASN1Template CERT_GeneralNamesTemplate[] = {
{ SEC_ASN1_SEQUENCE_OF | SEC_ASN1_XTRN , 0, SEC_ASN1_SUB(SEC_AnyTemplate) }
};
CERTGeneralName *
CERT_NewGeneralName(PLArenaPool *arena, CERTGeneralNameType type)
{
CERTGeneralName *name = arena
? PORT_ArenaZNew(arena, CERTGeneralName)
: PORT_ZNew(CERTGeneralName);
if (name) {
name->type = type;
name->l.prev = name->l.next = &name->l;
}
return name;
}
/* Copy content of one General Name to another.
** Caller has allocated destination general name.
** This function does not change the destinate's GeneralName's list linkage.
*/
SECStatus
cert_CopyOneGeneralName(PLArenaPool *arena,
CERTGeneralName *dest,
CERTGeneralName *src)
{
SECStatus rv;
void *mark = NULL;
PORT_Assert(dest != NULL);
dest->type = src->type;
mark = PORT_ArenaMark(arena);
switch (src->type) {
case certDirectoryName:
rv = SECITEM_CopyItem(arena, &dest->derDirectoryName,
&src->derDirectoryName);
if (rv == SECSuccess)
rv = CERT_CopyName(arena, &dest->name.directoryName,
&src->name.directoryName);
break;
case certOtherName:
rv = SECITEM_CopyItem(arena, &dest->name.OthName.name,
&src->name.OthName.name);
if (rv == SECSuccess)
rv = SECITEM_CopyItem(arena, &dest->name.OthName.oid,
&src->name.OthName.oid);
break;
default:
rv = SECITEM_CopyItem(arena, &dest->name.other,
&src->name.other);
break;
}
if (rv != SECSuccess) {
PORT_ArenaRelease(arena, mark);
} else {
PORT_ArenaUnmark(arena, mark);
}
return rv;
}
void
CERT_DestroyGeneralNameList(CERTGeneralNameList *list)
{
PZLock *lock;
if (list != NULL) {
lock = list->lock;
PZ_Lock(lock);
if (--list->refCount <= 0 && list->arena != NULL) {
PORT_FreeArena(list->arena, PR_FALSE);
PZ_Unlock(lock);
PZ_DestroyLock(lock);
} else {
PZ_Unlock(lock);
}
}
return;
}
CERTGeneralNameList *
CERT_CreateGeneralNameList(CERTGeneralName *name) {
PLArenaPool *arena;
CERTGeneralNameList *list = NULL;
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (arena == NULL) {
goto done;
}
list = PORT_ArenaZNew(arena, CERTGeneralNameList);
if (!list)
goto loser;
if (name != NULL) {
SECStatus rv;
list->name = CERT_NewGeneralName(arena, (CERTGeneralNameType)0);
if (!list->name)
goto loser;
rv = CERT_CopyGeneralName(arena, list->name, name);
if (rv != SECSuccess)
goto loser;
}
list->lock = PZ_NewLock(nssILockList);
if (!list->lock)
goto loser;
list->arena = arena;
list->refCount = 1;
done:
return list;
loser:
PORT_FreeArena(arena, PR_FALSE);
return NULL;
}
CERTGeneralName *
CERT_GetNextGeneralName(CERTGeneralName *current)
{
PRCList *next;
next = current->l.next;
return (CERTGeneralName *) (((char *) next) - offsetof(CERTGeneralName, l));
}
CERTGeneralName *
CERT_GetPrevGeneralName(CERTGeneralName *current)
{
PRCList *prev;
prev = current->l.prev;
return (CERTGeneralName *) (((char *) prev) - offsetof(CERTGeneralName, l));
}
CERTNameConstraint *
CERT_GetNextNameConstraint(CERTNameConstraint *current)
{
PRCList *next;
next = current->l.next;
return (CERTNameConstraint *) (((char *) next) - offsetof(CERTNameConstraint, l));
}
CERTNameConstraint *
CERT_GetPrevNameConstraint(CERTNameConstraint *current)
{
PRCList *prev;
prev = current->l.prev;
return (CERTNameConstraint *) (((char *) prev) - offsetof(CERTNameConstraint, l));
}
SECItem *
CERT_EncodeGeneralName(CERTGeneralName *genName, SECItem *dest, PLArenaPool *arena)
{
const SEC_ASN1Template * template;
PORT_Assert(arena);
if (arena == NULL) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return NULL;
}
/* TODO: mark arena */
if (dest == NULL) {
dest = PORT_ArenaZNew(arena, SECItem);
if (!dest)
goto loser;
}
if (genName->type == certDirectoryName) {
if (genName->derDirectoryName.data == NULL) {
/* The field hasn't been encoded yet. */
SECItem * pre_dest =
SEC_ASN1EncodeItem (arena, &(genName->derDirectoryName),
&(genName->name.directoryName),
CERT_NameTemplate);
if (!pre_dest)
goto loser;
}
if (genName->derDirectoryName.data == NULL) {
goto loser;
}
}
switch (genName->type) {
case certURI: template = CERT_URITemplate; break;
case certRFC822Name: template = CERT_RFC822NameTemplate; break;
case certDNSName: template = CERT_DNSNameTemplate; break;
case certIPAddress: template = CERT_IPAddressTemplate; break;
case certOtherName: template = CERTOtherNameTemplate; break;
case certRegisterID: template = CERT_RegisteredIDTemplate; break;
/* for this type, we expect the value is already encoded */
case certEDIPartyName: template = CERT_EDIPartyNameTemplate; break;
/* for this type, we expect the value is already encoded */
case certX400Address: template = CERT_X400AddressTemplate; break;
case certDirectoryName: template = CERT_DirectoryNameTemplate; break;
default:
PORT_Assert(0); goto loser;
}
dest = SEC_ASN1EncodeItem(arena, dest, genName, template);
if (!dest) {
goto loser;
}
/* TODO: unmark arena */
return dest;
loser:
/* TODO: release arena back to mark */
return NULL;
}
SECItem **
cert_EncodeGeneralNames(PLArenaPool *arena, CERTGeneralName *names)
{
CERTGeneralName *current_name;
SECItem **items = NULL;
int count = 0;
int i;
PRCList *head;
PORT_Assert(arena);
/* TODO: mark arena */
current_name = names;
if (names != NULL) {
count = 1;
}
head = &(names->l);
while (current_name->l.next != head) {
current_name = CERT_GetNextGeneralName(current_name);
++count;
}
current_name = CERT_GetNextGeneralName(current_name);
items = PORT_ArenaNewArray(arena, SECItem *, count + 1);
if (items == NULL) {
goto loser;
}
for (i = 0; i < count; i++) {
items[i] = CERT_EncodeGeneralName(current_name, (SECItem *)NULL, arena);
if (items[i] == NULL) {
goto loser;
}
current_name = CERT_GetNextGeneralName(current_name);
}
items[i] = NULL;
/* TODO: unmark arena */
return items;
loser:
/* TODO: release arena to mark */
return NULL;
}
CERTGeneralName *
CERT_DecodeGeneralName(PLArenaPool *reqArena,
SECItem *encodedName,
CERTGeneralName *genName)
{
const SEC_ASN1Template * template;
CERTGeneralNameType genNameType;
SECStatus rv = SECSuccess;
SECItem* newEncodedName;
if (!reqArena) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return NULL;
}
/* make a copy for decoding so the data decoded with QuickDER doesn't
point to temporary memory */
newEncodedName = SECITEM_ArenaDupItem(reqArena, encodedName);
if (!newEncodedName) {
return NULL;
}
/* TODO: mark arena */
genNameType = (CERTGeneralNameType)((*(newEncodedName->data) & 0x0f) + 1);
if (genName == NULL) {
genName = CERT_NewGeneralName(reqArena, genNameType);
if (!genName)
goto loser;
} else {
genName->type = genNameType;
genName->l.prev = genName->l.next = &genName->l;
}
switch (genNameType) {
case certURI: template = CERT_URITemplate; break;
case certRFC822Name: template = CERT_RFC822NameTemplate; break;
case certDNSName: template = CERT_DNSNameTemplate; break;
case certIPAddress: template = CERT_IPAddressTemplate; break;
case certOtherName: template = CERTOtherNameTemplate; break;
case certRegisterID: template = CERT_RegisteredIDTemplate; break;
case certEDIPartyName: template = CERT_EDIPartyNameTemplate; break;
case certX400Address: template = CERT_X400AddressTemplate; break;
case certDirectoryName: template = CERT_DirectoryNameTemplate; break;
default:
goto loser;
}
rv = SEC_QuickDERDecodeItem(reqArena, genName, template, newEncodedName);
if (rv != SECSuccess)
goto loser;
if (genNameType == certDirectoryName) {
rv = SEC_QuickDERDecodeItem(reqArena, &(genName->name.directoryName),
CERT_NameTemplate,
&(genName->derDirectoryName));
if (rv != SECSuccess)
goto loser;
}
/* TODO: unmark arena */
return genName;
loser:
/* TODO: release arena to mark */
return NULL;
}
CERTGeneralName *
cert_DecodeGeneralNames (PLArenaPool *arena,
SECItem **encodedGenName)
{
PRCList *head = NULL;
PRCList *tail = NULL;
CERTGeneralName *currentName = NULL;
PORT_Assert(arena);
if (!encodedGenName || !arena) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return NULL;
}
/* TODO: mark arena */
while (*encodedGenName != NULL) {
currentName = CERT_DecodeGeneralName(arena, *encodedGenName, NULL);
if (currentName == NULL)
break;
if (head == NULL) {
head = &(currentName->l);
tail = head;
}
currentName->l.next = head;
currentName->l.prev = tail;
tail = head->prev = tail->next = &(currentName->l);
encodedGenName++;
}
if (currentName) {
/* TODO: unmark arena */
return CERT_GetNextGeneralName(currentName);
}
/* TODO: release arena to mark */
return NULL;
}
void
CERT_DestroyGeneralName(CERTGeneralName *name)
{
cert_DestroyGeneralNames(name);
}
SECStatus
cert_DestroyGeneralNames(CERTGeneralName *name)
{
CERTGeneralName *first;
CERTGeneralName *next = NULL;
first = name;
do {
next = CERT_GetNextGeneralName(name);
PORT_Free(name);
name = next;
} while (name != first);
return SECSuccess;
}
static SECItem *
cert_EncodeNameConstraint(CERTNameConstraint *constraint,
SECItem *dest,
PLArenaPool *arena)
{
PORT_Assert(arena);
if (dest == NULL) {
dest = PORT_ArenaZNew(arena, SECItem);
if (dest == NULL) {
return NULL;
}
}
CERT_EncodeGeneralName(&(constraint->name), &(constraint->DERName), arena);
dest = SEC_ASN1EncodeItem (arena, dest, constraint,
CERTNameConstraintTemplate);
return dest;
}
SECStatus
cert_EncodeNameConstraintSubTree(CERTNameConstraint *constraints,
PLArenaPool *arena,
SECItem ***dest,
PRBool permited)
{
CERTNameConstraint *current_constraint = constraints;
SECItem **items = NULL;
int count = 0;
int i;
PRCList *head;
PORT_Assert(arena);
/* TODO: mark arena */
if (constraints != NULL) {
count = 1;
}
head = &constraints->l;
while (current_constraint->l.next != head) {
current_constraint = CERT_GetNextNameConstraint(current_constraint);
++count;
}
current_constraint = CERT_GetNextNameConstraint(current_constraint);
items = PORT_ArenaZNewArray(arena, SECItem *, count + 1);
if (items == NULL) {
goto loser;
}
for (i = 0; i < count; i++) {
items[i] = cert_EncodeNameConstraint(current_constraint,
(SECItem *) NULL, arena);
if (items[i] == NULL) {
goto loser;
}
current_constraint = CERT_GetNextNameConstraint(current_constraint);
}
*dest = items;
if (*dest == NULL) {
goto loser;
}
/* TODO: unmark arena */
return SECSuccess;
loser:
/* TODO: release arena to mark */
return SECFailure;
}
SECStatus
cert_EncodeNameConstraints(CERTNameConstraints *constraints,
PLArenaPool *arena,
SECItem *dest)
{
SECStatus rv = SECSuccess;
PORT_Assert(arena);
/* TODO: mark arena */
if (constraints->permited != NULL) {
rv = cert_EncodeNameConstraintSubTree(constraints->permited, arena,
&constraints->DERPermited,
PR_TRUE);
if (rv == SECFailure) {
goto loser;
}
}
if (constraints->excluded != NULL) {
rv = cert_EncodeNameConstraintSubTree(constraints->excluded, arena,
&constraints->DERExcluded,
PR_FALSE);
if (rv == SECFailure) {
goto loser;
}
}
dest = SEC_ASN1EncodeItem(arena, dest, constraints,
CERTNameConstraintsTemplate);
if (dest == NULL) {
goto loser;
}
/* TODO: unmark arena */
return SECSuccess;
loser:
/* TODO: release arena to mark */
return SECFailure;
}
CERTNameConstraint *
cert_DecodeNameConstraint(PLArenaPool *reqArena,
SECItem *encodedConstraint)
{
CERTNameConstraint *constraint;
SECStatus rv = SECSuccess;
CERTGeneralName *temp;
SECItem* newEncodedConstraint;
if (!reqArena) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return NULL;
}
newEncodedConstraint = SECITEM_ArenaDupItem(reqArena, encodedConstraint);
if (!newEncodedConstraint) {
return NULL;
}
/* TODO: mark arena */
constraint = PORT_ArenaZNew(reqArena, CERTNameConstraint);
if (!constraint)
goto loser;
rv = SEC_QuickDERDecodeItem(reqArena, constraint,
CERTNameConstraintTemplate,
newEncodedConstraint);
if (rv != SECSuccess) {
goto loser;
}
temp = CERT_DecodeGeneralName(reqArena, &(constraint->DERName),
&(constraint->name));
if (temp != &(constraint->name)) {
goto loser;
}
/* ### sjlee: since the name constraint contains only one
* CERTGeneralName, the list within CERTGeneralName shouldn't
* point anywhere else. Otherwise, bad things will happen.
*/
constraint->name.l.prev = constraint->name.l.next = &(constraint->name.l);
/* TODO: unmark arena */
return constraint;
loser:
/* TODO: release arena back to mark */
return NULL;
}
CERTNameConstraint *
cert_DecodeNameConstraintSubTree(PLArenaPool *arena,
SECItem **subTree,
PRBool permited)
{
CERTNameConstraint *current = NULL;
CERTNameConstraint *first = NULL;
CERTNameConstraint *last = NULL;
int i = 0;
PORT_Assert(arena);
/* TODO: mark arena */
while (subTree[i] != NULL) {
current = cert_DecodeNameConstraint(arena, subTree[i]);
if (current == NULL) {
goto loser;
}
if (last == NULL) {
first = last = current;
}
current->l.prev = &(last->l);
current->l.next = last->l.next;
last->l.next = &(current->l);
i++;
}
first->l.prev = &(current->l);
/* TODO: unmark arena */
return first;
loser:
/* TODO: release arena back to mark */
return NULL;
}
CERTNameConstraints *
cert_DecodeNameConstraints(PLArenaPool *reqArena,
const SECItem *encodedConstraints)
{
CERTNameConstraints *constraints;
SECStatus rv;
SECItem* newEncodedConstraints;
if (!reqArena) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return NULL;
}
PORT_Assert(encodedConstraints);
newEncodedConstraints = SECITEM_ArenaDupItem(reqArena, encodedConstraints);
/* TODO: mark arena */
constraints = PORT_ArenaZNew(reqArena, CERTNameConstraints);
if (constraints == NULL) {
goto loser;
}
rv = SEC_QuickDERDecodeItem(reqArena, constraints,
CERTNameConstraintsTemplate,
newEncodedConstraints);
if (rv != SECSuccess) {
goto loser;
}
if (constraints->DERPermited != NULL &&
constraints->DERPermited[0] != NULL) {
constraints->permited =
cert_DecodeNameConstraintSubTree(reqArena,
constraints->DERPermited,
PR_TRUE);
if (constraints->permited == NULL) {
goto loser;
}
}
if (constraints->DERExcluded != NULL &&
constraints->DERExcluded[0] != NULL) {
constraints->excluded =
cert_DecodeNameConstraintSubTree(reqArena,
constraints->DERExcluded,
PR_FALSE);
if (constraints->excluded == NULL) {
goto loser;
}
}
/* TODO: unmark arena */
return constraints;
loser:
/* TODO: release arena back to mark */
return NULL;
}
/* Copy a chain of one or more general names to a destination chain.
** Caller has allocated at least the first destination GeneralName struct.
** Both source and destination chains are circular doubly-linked lists.
** The first source struct is copied to the first destination struct.
** If the source chain has more than one member, and the destination chain
** has only one member, then this function allocates new structs for all but
** the first copy from the arena and links them into the destination list.
** If the destination struct is part of a list with more than one member,
** then this function traverses both the source and destination lists,
** copying each source struct to the corresponding dest struct.
** In that case, the destination list MUST contain at least as many
** structs as the source list or some dest entries will be overwritten.
*/
SECStatus
CERT_CopyGeneralName(PLArenaPool *arena,
CERTGeneralName *dest,
CERTGeneralName *src)
{
SECStatus rv;
CERTGeneralName *destHead = dest;
CERTGeneralName *srcHead = src;
PORT_Assert(dest != NULL);
if (!dest) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
/* TODO: mark arena */
do {
rv = cert_CopyOneGeneralName(arena, dest, src);
if (rv != SECSuccess)
goto loser;
src = CERT_GetNextGeneralName(src);
/* if there is only one general name, we shouldn't do this */
if (src != srcHead) {
if (dest->l.next == &destHead->l) {
CERTGeneralName *temp;
temp = CERT_NewGeneralName(arena, (CERTGeneralNameType)0);
if (!temp)
goto loser;
temp->l.next = &destHead->l;
temp->l.prev = &dest->l;
destHead->l.prev = &temp->l;
dest->l.next = &temp->l;
dest = temp;
} else {
dest = CERT_GetNextGeneralName(dest);
}
}
} while (src != srcHead && rv == SECSuccess);
/* TODO: unmark arena */
return rv;
loser:
/* TODO: release back to mark */
return SECFailure;
}
CERTGeneralNameList *
CERT_DupGeneralNameList(CERTGeneralNameList *list)
{
if (list != NULL) {
PZ_Lock(list->lock);
list->refCount++;
PZ_Unlock(list->lock);
}
return list;
}
/* Allocate space and copy CERTNameConstraint from src to dest */
CERTNameConstraint *
CERT_CopyNameConstraint(PLArenaPool *arena,
CERTNameConstraint *dest,
CERTNameConstraint *src)
{
SECStatus rv;
/* TODO: mark arena */
if (dest == NULL) {
dest = PORT_ArenaZNew(arena, CERTNameConstraint);
if (!dest)
goto loser;
/* mark that it is not linked */
dest->name.l.prev = dest->name.l.next = &(dest->name.l);
}
rv = CERT_CopyGeneralName(arena, &dest->name, &src->name);
if (rv != SECSuccess) {
goto loser;
}
rv = SECITEM_CopyItem(arena, &dest->DERName, &src->DERName);
if (rv != SECSuccess) {
goto loser;
}
rv = SECITEM_CopyItem(arena, &dest->min, &src->min);
if (rv != SECSuccess) {
goto loser;
}
rv = SECITEM_CopyItem(arena, &dest->max, &src->max);
if (rv != SECSuccess) {
goto loser;
}
dest->l.prev = dest->l.next = &dest->l;
/* TODO: unmark arena */
return dest;
loser:
/* TODO: release arena to mark */
return NULL;
}
CERTGeneralName *
cert_CombineNamesLists(CERTGeneralName *list1, CERTGeneralName *list2)
{
PRCList *begin1;
PRCList *begin2;
PRCList *end1;
PRCList *end2;
if (list1 == NULL){
return list2;
} else if (list2 == NULL) {
return list1;
} else {
begin1 = &list1->l;
begin2 = &list2->l;
end1 = list1->l.prev;
end2 = list2->l.prev;
end1->next = begin2;
end2->next = begin1;
begin1->prev = end2;
begin2->prev = end1;
return list1;
}
}
CERTNameConstraint *
cert_CombineConstraintsLists(CERTNameConstraint *list1, CERTNameConstraint *list2)
{
PRCList *begin1;
PRCList *begin2;
PRCList *end1;
PRCList *end2;
if (list1 == NULL){
return list2;
} else if (list2 == NULL) {
return list1;
} else {
begin1 = &list1->l;
begin2 = &list2->l;
end1 = list1->l.prev;
end2 = list2->l.prev;
end1->next = begin2;
end2->next = begin1;
begin1->prev = end2;
begin2->prev = end1;
return list1;
}
}
/* Add a CERTNameConstraint to the CERTNameConstraint list */
CERTNameConstraint *
CERT_AddNameConstraint(CERTNameConstraint *list,
CERTNameConstraint *constraint)
{
PORT_Assert(constraint != NULL);
constraint->l.next = constraint->l.prev = &constraint->l;
list = cert_CombineConstraintsLists(list, constraint);
return list;
}
SECStatus
CERT_GetNameConstraintByType (CERTNameConstraint *constraints,
CERTGeneralNameType type,
CERTNameConstraint **returnList,
PLArenaPool *arena)
{
CERTNameConstraint *current = NULL;
void *mark = NULL;
*returnList = NULL;
if (!constraints)
return SECSuccess;
mark = PORT_ArenaMark(arena);
current = constraints;
do {
PORT_Assert(current->name.type);
if (current->name.type == type) {
CERTNameConstraint *temp;
temp = CERT_CopyNameConstraint(arena, NULL, current);
if (temp == NULL)
goto loser;
*returnList = CERT_AddNameConstraint(*returnList, temp);
}
current = CERT_GetNextNameConstraint(current);
} while (current != constraints);
PORT_ArenaUnmark(arena, mark);
return SECSuccess;
loser:
PORT_ArenaRelease(arena, mark);
return SECFailure;
}
void *
CERT_GetGeneralNameByType (CERTGeneralName *genNames,
CERTGeneralNameType type, PRBool derFormat)
{
CERTGeneralName *current;
if (!genNames)
return NULL;
current = genNames;
do {
if (current->type == type) {
switch (type) {
case certDNSName:
case certEDIPartyName:
case certIPAddress:
case certRegisterID:
case certRFC822Name:
case certX400Address:
case certURI:
return (void *)¤t->name.other; /* SECItem * */
case certOtherName:
return (void *)¤t->name.OthName; /* OthName * */
case certDirectoryName:
return derFormat
? (void *)¤t->derDirectoryName /* SECItem * */
: (void *)¤t->name.directoryName; /* CERTName * */
}
PORT_Assert(0);
return NULL;
}
current = CERT_GetNextGeneralName(current);
} while (current != genNames);
return NULL;
}
int
CERT_GetNamesLength(CERTGeneralName *names)
{
int length = 0;
CERTGeneralName *first;
first = names;
if (names != NULL) {
do {
length++;
names = CERT_GetNextGeneralName(names);
} while (names != first);
}
return length;