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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
|
/* 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) }
};
static struct {
CERTGeneralNameType type;
char *name;
} typesArray[] = {
{ certOtherName, "other" },
{ certRFC822Name, "email" },
{ certRFC822Name, "rfc822" },
{ certDNSName, "dns" },
{ certX400Address, "x400" },
{ certX400Address, "x400addr" },
{ certDirectoryName, "directory" },
{ certDirectoryName, "dn" },
{ certEDIPartyName, "edi" },
{ certEDIPartyName, "ediparty" },
{ certURI, "uri" },
{ certIPAddress, "ip" },
{ certIPAddress, "ipaddr" },
{ certRegisterID, "registerid" }
};
CERTGeneralNameType
CERT_GetGeneralNameTypeFromString(const char *string)
{
int types_count = sizeof(typesArray)/sizeof(typesArray[0]);
int i;
for (i=0; i < types_count; i++) {
if (PORT_Strcasecmp(string, typesArray[i].name) == 0) {
return typesArray[i].type;
}
}
return 0;
}
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;
}
/* Creates new GeneralNames for any email addresses found in the
** input DN, and links them onto the list for the DN.
*/
SECStatus
cert_ExtractDNEmailAddrs(CERTGeneralName *name, PLArenaPool *arena)
{
CERTGeneralName *nameList = NULL;
const CERTRDN **nRDNs = (const CERTRDN **)(name->name.directoryName.rdns);
SECStatus rv = SECSuccess;
PORT_Assert(name->type == certDirectoryName);
if (name->type != certDirectoryName) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
/* TODO: mark arena */
while (nRDNs && *nRDNs) { /* loop over RDNs */
const CERTRDN *nRDN = *nRDNs++;
CERTAVA **nAVAs = nRDN->avas;
while (nAVAs && *nAVAs) { /* loop over AVAs */
int tag;
CERTAVA *nAVA = *nAVAs++;
tag = CERT_GetAVATag(nAVA);
if ( tag == SEC_OID_PKCS9_EMAIL_ADDRESS ||
tag == SEC_OID_RFC1274_MAIL) { /* email AVA */
CERTGeneralName *newName = NULL;
SECItem *avaValue = CERT_DecodeAVAValue(&nAVA->value);
if (!avaValue)
goto loser;
rv = SECFailure;
newName = CERT_NewGeneralName(arena, certRFC822Name);
if (newName) {
rv = SECITEM_CopyItem(arena, &newName->name.other, avaValue);
}
SECITEM_FreeItem(avaValue, PR_TRUE);
if (rv != SECSuccess)
goto loser;
nameList = cert_CombineNamesLists(nameList, newName);
} /* handle one email AVA */
} /* loop over AVAs */
} /* loop over RDNs */
/* combine new names with old one. */
name = cert_CombineNamesLists(name, nameList);
/* TODO: unmark arena */
return SECSuccess;
loser:
/* TODO: release arena back to mark */
return SECFailure;
}
/* Extract all names except Subject Common Name from a cert
** in preparation for a name constraints test.
*/
CERTGeneralName *
CERT_GetCertificateNames(CERTCertificate *cert, PLArenaPool *arena)
{
return CERT_GetConstrainedCertificateNames(cert, arena, PR_FALSE);
}
/* This function is called by CERT_VerifyCertChain to extract all
** names from a cert in preparation for a name constraints test.
*/
CERTGeneralName *
CERT_GetConstrainedCertificateNames(const CERTCertificate *cert,
PLArenaPool *arena,
PRBool includeSubjectCommonName)
{
CERTGeneralName *DN;
CERTGeneralName *SAN;
PRUint32 numDNSNames = 0;
SECStatus rv;
if (!arena) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return NULL;
}
/* TODO: mark arena */
DN = CERT_NewGeneralName(arena, certDirectoryName);
if (DN == NULL) {
goto loser;
}
rv = CERT_CopyName(arena, &DN->name.directoryName, &cert->subject);
if (rv != SECSuccess) {
goto loser;
}
rv = SECITEM_CopyItem(arena, &DN->derDirectoryName, &cert->derSubject);
if (rv != SECSuccess) {
goto loser;
}
/* Extract email addresses from DN, construct CERTGeneralName structs
** for them, add them to the name list
*/
rv = cert_ExtractDNEmailAddrs(DN, arena);
if (rv != SECSuccess)
goto loser;
/* Now extract any GeneralNames from the subject name names extension. */
SAN = cert_GetSubjectAltNameList(cert, arena);
if (SAN) {
numDNSNames = cert_CountDNSPatterns(SAN);
DN = cert_CombineNamesLists(DN, SAN);
}
if (!numDNSNames && includeSubjectCommonName) {
char *cn = CERT_GetCommonName(&cert->subject);
if (cn) {
CERTGeneralName *CN = CERT_NewGeneralName(arena, certDNSName);
if (CN) {
SECItem cnItem = {siBuffer, NULL, 0};
cnItem.data = (unsigned char *)cn;
cnItem.len = strlen(cn);
rv = SECITEM_CopyItem(arena, &CN->name.other, &cnItem);
if (rv == SECSuccess) {
DN = cert_CombineNamesLists(DN, CN);
}
}
PORT_Free(cn);
}
}
if (rv == SECSuccess) {
/* TODO: unmark arena */
return DN;
}
loser:
/* TODO: release arena to mark */
return NULL;
}
/* Returns SECSuccess if name matches constraint per RFC 3280 rules for
** URI name constraints. SECFailure otherwise.
** If the constraint begins with a dot, it is a domain name, otherwise
** It is a host name. Examples:
** Constraint Name Result
** ------------ --------------- --------
** foo.bar.com foo.bar.com matches
** foo.bar.com FoO.bAr.CoM matches
** foo.bar.com www.foo.bar.com no match
** foo.bar.com nofoo.bar.com no match
** .foo.bar.com www.foo.bar.com matches
** .foo.bar.com nofoo.bar.com no match
** .foo.bar.com foo.bar.com no match
** .foo.bar.com www..foo.bar.com no match
*/
static SECStatus
compareURIN2C(const SECItem *name, const SECItem *constraint)
{
int offset;
/* The spec is silent on intepreting zero-length constraints.
** We interpret them as matching no URI names.
*/
if (!constraint->len)
return SECFailure;
if (constraint->data[0] != '.') {
/* constraint is a host name. */
if (name->len != constraint->len ||
PL_strncasecmp((char *)name->data,
(char *)constraint->data, constraint->len))
return SECFailure;
return SECSuccess;
}
/* constraint is a domain name. */
if (name->len < constraint->len)
return SECFailure;
offset = name->len - constraint->len;
if (PL_strncasecmp((char *)(name->data + offset),
(char *)constraint->data, constraint->len))
return SECFailure;
if (!offset ||
(name->data[offset - 1] == '.') + (constraint->data[0] == '.') == 1)
return SECSuccess;
return SECFailure;
}
/* for DNSname constraints, RFC 3280 says, (section 4.2.1.11, page 38)
**
** DNS name restrictions are expressed as foo.bar.com. Any DNS name
** that can be constructed by simply adding to the left hand side of the
** name satisfies the name constraint. For example, www.foo.bar.com
** would satisfy the constraint but foo1.bar.com would not.
**
** But NIST's PKITS test suite requires that the constraint be treated
** as a domain name, and requires that any name added to the left hand
** side end in a dot ".". Sensible, but not strictly following the RFC.
**
** Constraint Name RFC 3280 NIST PKITS
** ------------ --------------- -------- ----------
** foo.bar.com foo.bar.com matches matches
** foo.bar.com FoO.bAr.CoM matches matches
** foo.bar.com www.foo.bar.com matches matches
** foo.bar.com nofoo.bar.com MATCHES NO MATCH
** .foo.bar.com www.foo.bar.com matches matches? disallowed?
** .foo.bar.com foo.bar.com no match no match
** .foo.bar.com www..foo.bar.com matches probably not
**
** We will try to conform to NIST's PKITS tests, and the unstated
** rules they imply.
*/
static SECStatus
compareDNSN2C(const SECItem *name, const SECItem *constraint)
{
int offset;
/* The spec is silent on intepreting zero-length constraints.
** We interpret them as matching all DNSnames.
*/
if (!constraint->len)
return SECSuccess;
if (name->len < constraint->len)
return SECFailure;
offset = name->len - constraint->len;
if (PL_strncasecmp((char *)(name->data + offset),
(char *)constraint->data, constraint->len))
return SECFailure;
if (!offset ||
(name->data[offset - 1] == '.') + (constraint->data[0] == '.') == 1)
return SECSuccess;
return SECFailure;
}
/* Returns SECSuccess if name matches constraint per RFC 3280 rules for
** internet email addresses. SECFailure otherwise.
** If constraint contains a '@' then the two strings much match exactly.
** Else if constraint starts with a '.'. then it must match the right-most
** substring of the name,
** else constraint string must match entire name after the name's '@'.
** Empty constraint string matches all names. All comparisons case insensitive.
*/
static SECStatus
compareRFC822N2C(const SECItem *name, const SECItem *constraint)
{
int offset;
if (!constraint->len)
return SECSuccess;
if (name->len < constraint->len)
return SECFailure;
if (constraint->len == 1 && constraint->data[0] == '.')
return SECSuccess;
for (offset = constraint->len - 1; offset >= 0; --offset) {
if (constraint->data[offset] == '@') {
return (name->len == constraint->len &&
!PL_strncasecmp((char *)name->data,
(char *)constraint->data, constraint->len))
? SECSuccess : SECFailure;
}
}
offset = name->len - constraint->len;
if (PL_strncasecmp((char *)(name->data + offset),
(char *)constraint->data, constraint->len))
return SECFailure;
if (constraint->data[0] == '.')
return SECSuccess;
if (offset > 0 && name->data[offset - 1] == '@')
return SECSuccess;
return SECFailure;
}
/* name contains either a 4 byte IPv4 address or a 16 byte IPv6 address.
** constraint contains an address of the same length, and a subnet mask
** of the same length. Compare name's address to the constraint's
** address, subject to the mask.
** Return SECSuccess if they match, SECFailure if they don't.
*/
static SECStatus
compareIPaddrN2C(const SECItem *name, const SECItem *constraint)
{
int i;
if (name->len == 4 && constraint->len == 8) { /* ipv4 addr */
for (i = 0; i < 4; i++) {
if ((name->data[i] ^ constraint->data[i]) & constraint->data[i+4])
goto loser;
}
return SECSuccess;
}
if (name->len == 16 && constraint->len == 32) { /* ipv6 addr */
for (i = 0; i < 16; i++) {
if ((name->data[i] ^ constraint->data[i]) & constraint->data[i+16])
goto loser;
}
return SECSuccess;
}
loser:
return SECFailure;
}
/* start with a SECItem that points to a URI. Parse it lookingg for
** a hostname. Modify item->data and item->len to define the hostname,
** but do not modify and data at item->data.
** If anything goes wrong, the contents of *item are undefined.
*/
static SECStatus
parseUriHostname(SECItem * item)
{
int i;
PRBool found = PR_FALSE;
for (i = 0; (unsigned)(i+2) < item->len; ++i) {
if (item->data[i ] == ':' &&
item->data[i+1] == '/' &&
item->data[i+2] == '/') {
i += 3;
item->data += i;
item->len -= i;
found = PR_TRUE;
break;
}
}
if (!found)
return SECFailure;
/* now look for a '/', which is an upper bound in the end of the name */
for (i = 0; (unsigned)i < item->len; ++i) {
if (item->data[i] == '/') {
item->len = i;
break;
}
}
/* now look for a ':', which marks the end of the name */
for (i = item->len; --i >= 0; ) {
if (item->data[i] == ':') {
item->len = i;
break;
}
}
/* now look for an '@', which marks the beginning of the hostname */
for (i = 0; (unsigned)i < item->len; ++i) {
if (item->data[i] == '@') {
++i;
item->data += i;
item->len -= i;
break;
}
}
return item->len ? SECSuccess : SECFailure;
}
/* This function takes one name, and a list of constraints.
** It searches the constraints looking for a match.
** It returns SECSuccess if the name satisfies the constraints, i.e.,
** if excluded, then the name does not match any constraint,
** if permitted, then the name matches at least one constraint.
** It returns SECFailure if the name fails to satisfy the constraints,
** or if some code fails (e.g. out of memory, or invalid constraint)
*/
SECStatus
cert_CompareNameWithConstraints(const CERTGeneralName *name,
const CERTNameConstraint *constraints,
PRBool excluded)
{
SECStatus rv = SECSuccess;
SECStatus matched = SECFailure;
const CERTNameConstraint *current;
PORT_Assert(constraints); /* caller should not call with NULL */
if (!constraints) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
current = constraints;
do {
rv = SECSuccess;
matched = SECFailure;
PORT_Assert(name->type == current->name.type);
switch (name->type) {
case certDNSName:
matched = compareDNSN2C(&name->name.other,
¤t->name.name.other);
break;
case certRFC822Name:
matched = compareRFC822N2C(&name->name.other,
¤t->name.name.other);
break;
case certURI:
{
/* make a modifiable copy of the URI SECItem. */
SECItem uri = name->name.other;
/* find the hostname in the URI */
rv = parseUriHostname(&uri);
if (rv == SECSuccess) {
/* does our hostname meet the constraint? */
matched = compareURIN2C(&uri, ¤t->name.name.other);
}
}
break;
case certDirectoryName:
/* Determine if the constraint directory name is a "prefix"
** for the directory name being tested.
*/
{
/* status defaults to SECEqual, so that a constraint with
** no AVAs will be a wildcard, matching all directory names.
*/
SECComparison status = SECEqual;
const CERTRDN **cRDNs =
(const CERTRDN **)current->name.name.directoryName.rdns;
const CERTRDN **nRDNs =
(const CERTRDN **)name->name.directoryName.rdns;
while (cRDNs && *cRDNs && nRDNs && *nRDNs) {
/* loop over name RDNs and constraint RDNs in lock step */
const CERTRDN *cRDN = *cRDNs++;
const CERTRDN *nRDN = *nRDNs++;
CERTAVA **cAVAs = cRDN->avas;
while (cAVAs && *cAVAs) { /* loop over constraint AVAs */
CERTAVA *cAVA = *cAVAs++;
CERTAVA **nAVAs = nRDN->avas;
while (nAVAs && *nAVAs) { /* loop over name AVAs */
CERTAVA *nAVA = *nAVAs++;
status = CERT_CompareAVA(cAVA, nAVA);
if (status == SECEqual)
break;
} /* loop over name AVAs */
if (status != SECEqual)
break;
} /* loop over constraint AVAs */
if (status != SECEqual)
break;
} /* loop over name RDNs and constraint RDNs */
matched = (status == SECEqual) ? SECSuccess : SECFailure;
break;
}
case certIPAddress: /* type 8 */
matched = compareIPaddrN2C(&name->name.other,
¤t->name.name.other);
break;
/* NSS does not know how to compare these "Other" type names with
** their respective constraints. But it does know how to tell
** if the constraint applies to the type of name (by comparing
** the constraint OID to the name OID). NSS makes no use of "Other"
** type names at all, so NSS errs on the side of leniency for these
** types, provided that their OIDs match. So, when an "Other"
** name constraint appears in an excluded subtree, it never causes
** a name to fail. When an "Other" name constraint appears in a
** permitted subtree, AND the constraint's OID matches the name's
** OID, then name is treated as if it matches the constraint.
*/
case certOtherName: /* type 1 */
matched = (!excluded &&
name->type == current->name.type &&
SECITEM_ItemsAreEqual(&name->name.OthName.oid,
¤t->name.name.OthName.oid))
? SECSuccess : SECFailure;
break;
/* NSS does not know how to compare these types of names with their
** respective constraints. But NSS makes no use of these types of
** names at all, so it errs on the side of leniency for these types.
** Constraints for these types of names never cause the name to
** fail the constraints test. NSS behaves as if the name matched
** for permitted constraints, and did not match for excluded ones.
*/
case certX400Address: /* type 4 */
case certEDIPartyName: /* type 6 */
case certRegisterID: /* type 9 */
matched = excluded ? SECFailure : SECSuccess;
break;
default: /* non-standard types are not supported */
rv = SECFailure;
break;
}
if (matched == SECSuccess || rv != SECSuccess)
break;
current = CERT_GetNextNameConstraint((CERTNameConstraint*)current);
} while (current != constraints);
if (rv == SECSuccess) {
if (matched == SECSuccess)
rv = excluded ? SECFailure : SECSuccess;
else
rv = excluded ? SECSuccess : SECFailure;
return rv;
}
return SECFailure;
}
/* Add and link a CERTGeneralName to a CERTNameConstraint list. Most
** likely the CERTNameConstraint passed in is either the permitted
** list or the excluded list of a CERTNameConstraints.
*/
SECStatus
CERT_AddNameConstraintByGeneralName(PLArenaPool *arena,
CERTNameConstraint **constraints,
CERTGeneralName *name)
{
SECStatus rv;
CERTNameConstraint *current = NULL;
CERTNameConstraint *first = *constraints;
void *mark = NULL;
mark = PORT_ArenaMark(arena);
current = PORT_ArenaZNew(arena, CERTNameConstraint);
if (current == NULL) {
rv = SECFailure;
goto done;
}
rv = cert_CopyOneGeneralName(arena, ¤t->name, name);
if (rv != SECSuccess) {
goto done;
}
current->name.l.prev = current->name.l.next = &(current->name.l);
if (first == NULL) {
*constraints = current;
PR_INIT_CLIST(¤t->l);
} else {
PR_INSERT_BEFORE(¤t->l, &first->l);
}
done:
if (rv == SECFailure) {
PORT_ArenaRelease(arena, mark);
} else {
PORT_ArenaUnmark(arena, mark);
}
return rv;
}
/* Add name constraints to certain certs that do not include name constraints
* This is the core of the implementation for bug 952572.
*/
static SECStatus
getNameExtensionsBuiltIn(CERTCertificate *cert,
SECItem *extensions)
{
const char constraintFranceGov[] = "\x30\x5D" /* sequence len = 93*/
"\xA0\x5B" /* element len =91 */
"\x30\x05" /* sequence len 5 */
"\x82\x03" /* entry len 3 */
".fr"
"\x30\x05\x82\x03" /* sequence len5, entry len 3 */
".gp"
"\x30\x05\x82\x03"
".gf"
"\x30\x05\x82\x03"
".mq"
"\x30\x05\x82\x03"
".re"
"\x30\x05\x82\x03"
".yt"
"\x30\x05\x82\x03"
".pm"
"\x30\x05\x82\x03"
".bl"
"\x30\x05\x82\x03"
".mf"
"\x30\x05\x82\x03"
".wf"
"\x30\x05\x82\x03"
".pf"
"\x30\x05\x82\x03"
".nc"
"\x30\x05\x82\x03"
".tf";
/* The stringified value for the subject is:
E=igca@sgdn.pm.gouv.fr,CN=IGC/A,OU=DCSSI,O=PM/SGDN,L=Paris,ST=France,C=FR
*/
const char rawANSSISubject[] = "\x30\x81\x85\x31\x0B\x30\x09\x06\x03\x55\x04"
"\x06\x13\x02\x46\x52\x31\x0F\x30\x0D\x06\x03"
"\x55\x04\x08\x13\x06\x46\x72\x61\x6E\x63\x65"
"\x31\x0E\x30\x0C\x06\x03\x55\x04\x07\x13\x05"
"\x50\x61\x72\x69\x73\x31\x10\x30\x0E\x06\x03"
"\x55\x04\x0A\x13\x07\x50\x4D\x2F\x53\x47\x44"
"\x4E\x31\x0E\x30\x0C\x06\x03\x55\x04\x0B\x13"
"\x05\x44\x43\x53\x53\x49\x31\x0E\x30\x0C\x06"
"\x03\x55\x04\x03\x13\x05\x49\x47\x43\x2F\x41"
"\x31\x23\x30\x21\x06\x09\x2A\x86\x48\x86\xF7"
"\x0D\x01\x09\x01\x16\x14\x69\x67\x63\x61\x40"
"\x73\x67\x64\x6E\x2E\x70\x6D\x2E\x67\x6F\x75"
"\x76\x2E\x66\x72";
const SECItem anssi_subject = {0, (unsigned char *) rawANSSISubject,
sizeof(rawANSSISubject)-1};
const SECItem permitFranceGovNC = {0, (unsigned char *) constraintFranceGov,
sizeof(constraintFranceGov)-1};
if (SECITEM_ItemsAreEqual(&cert->derSubject, &anssi_subject)) {
SECStatus rv;
rv = SECITEM_CopyItem(NULL, extensions, &permitFranceGovNC);
return rv;
}
PORT_SetError(SEC_ERROR_EXTENSION_NOT_FOUND);
return SECFailure;
}
/* Extract the name constraints extension from the CA cert. */
SECStatus
CERT_FindNameConstraintsExten(PLArenaPool *arena,
CERTCertificate *cert,
CERTNameConstraints **constraints)
{
SECStatus rv = SECSuccess;
SECItem constraintsExtension;
void *mark = NULL;
*constraints = NULL;
rv = CERT_FindCertExtension(cert, SEC_OID_X509_NAME_CONSTRAINTS,
&constraintsExtension);
if (rv != SECSuccess) {
if (PORT_GetError() != SEC_ERROR_EXTENSION_NOT_FOUND) {
return rv;
}
rv = getNameExtensionsBuiltIn(cert, &constraintsExtension);
if (rv != SECSuccess) {
if (PORT_GetError() == SEC_ERROR_EXTENSION_NOT_FOUND) {
return SECSuccess;
}
return rv;
}
}
mark = PORT_ArenaMark(arena);
*constraints = cert_DecodeNameConstraints(arena, &constraintsExtension);
if (*constraints == NULL) { /* decode failed */
rv = SECFailure;
}
PORT_Free (constraintsExtension.data);
if (rv == SECFailure) {
PORT_ArenaRelease(arena, mark);
} else {
PORT_ArenaUnmark(arena, mark);
}
return rv;
}
/* Verify name against all the constraints relevant to that type of
** the name.
*/
SECStatus
CERT_CheckNameSpace(PLArenaPool *arena,
const CERTNameConstraints *constraints,
const CERTGeneralName *currentName)
{
CERTNameConstraint *matchingConstraints;
SECStatus rv = SECSuccess;
if (constraints->excluded != NULL) {
rv = CERT_GetNameConstraintByType(constraints->excluded,
currentName->type,
&matchingConstraints, arena);
if (rv == SECSuccess && matchingConstraints != NULL) {
rv = cert_CompareNameWithConstraints(currentName,
matchingConstraints,
PR_TRUE);
}
if (rv != SECSuccess) {
return(rv);
}
}
if (constraints->permited != NULL) {
rv = CERT_GetNameConstraintByType(constraints->permited,
currentName->type,
&matchingConstraints, arena);
if (rv == SECSuccess && matchingConstraints != NULL) {
rv = cert_CompareNameWithConstraints(currentName,
matchingConstraints,
PR_FALSE);
}
if (rv != SECSuccess) {
return(rv);
}
}
return(SECSuccess);
}
/* Extract the name constraints extension from the CA cert.
** Test each and every name in namesList against all the constraints
** relevant to that type of name.
** Returns NULL in pBadCert for success, if all names are acceptable.
** If some name is not acceptable, returns a pointer to the cert that
** contained that name.
*/
SECStatus
CERT_CompareNameSpace(CERTCertificate *cert,
CERTGeneralName *namesList,
CERTCertificate **certsList,
PLArenaPool *reqArena,
CERTCertificate **pBadCert)
{
SECStatus rv = SECSuccess;
CERTNameConstraints *constraints;
CERTGeneralName *currentName;
int count = 0;
CERTCertificate *badCert = NULL;
/* If no names to check, then no names can be bad. */
if (!namesList)
goto done;
rv = CERT_FindNameConstraintsExten(reqArena, cert, &constraints);
if (rv != SECSuccess) {
count = -1;
goto done;
}
currentName = namesList;
do {
if (constraints){
rv = CERT_CheckNameSpace(reqArena, constraints, currentName);
if (rv != SECSuccess) {
break;
}
}
currentName = CERT_GetNextGeneralName(currentName);
count ++;
} while (currentName != namesList);
done:
if (rv != SECSuccess) {
badCert = (count >= 0) ? certsList[count] : cert;
}
if (pBadCert)
*pBadCert = badCert;
return rv;
}
#if 0
/* not exported from shared libs, not used. Turn on if we ever need it. */
SECStatus
CERT_CompareGeneralName(CERTGeneralName *a, CERTGeneralName *b)
{
CERTGeneralName *currentA;
CERTGeneralName *currentB;
PRBool found;
currentA = a;
currentB = b;
if (a != NULL) {
do {
if (currentB == NULL) {
return SECFailure;
}
currentB = CERT_GetNextGeneralName(currentB);
currentA = CERT_GetNextGeneralName(currentA);
} while (currentA != a);
}
if (currentB != b) {
return SECFailure;
}
currentA = a;
do {
currentB = b;
found = PR_FALSE;
do {
if (currentB->type == currentA->type) {
switch (currentB->type) {
case certDNSName:
case certEDIPartyName:
case certIPAddress:
case certRegisterID:
case certRFC822Name:
case certX400Address:
case certURI:
if (SECITEM_CompareItem(¤tA->name.other,
¤tB->name.other)
== SECEqual) {
found = PR_TRUE;
}
break;
case certOtherName:
if (SECITEM_CompareItem(¤tA->name.OthName.oid,
¤tB->name.OthName.oid)
== SECEqual &&
SECITEM_CompareItem(¤tA->name.OthName.name,
¤tB->name.OthName.name)
== SECEqual) {
found = PR_TRUE;
}
break;
case certDirectoryName:
if (CERT_CompareName(¤tA->name.directoryName,
¤tB->name.directoryName)
== SECEqual) {
found = PR_TRUE;
}
}
}
currentB = CERT_GetNextGeneralName(currentB);
} while (currentB != b && found != PR_TRUE);
if (found != PR_TRUE) {
return SECFailure;
}
currentA = CERT_GetNextGeneralName(currentA);
} while (currentA != a);
return SECSuccess;
}
SECStatus
CERT_CompareGeneralNameLists(CERTGeneralNameList *a, CERTGeneralNameList *b)
{
SECStatus rv;
if (a == b) {
return SECSuccess;
}
if (a != NULL && b != NULL) {
PZ_Lock(a->lock);
PZ_Lock(b->lock);
rv = CERT_CompareGeneralName(a->name, b->name);
PZ_Unlock(a->lock);
PZ_Unlock(b->lock);
} else {
rv = SECFailure;
}
return rv;
}
#endif
#if 0
/* This function is not exported from NSS shared libraries, and is not
** used inside of NSS.
** XXX it doesn't check for failed allocations. :-(
*/
void *
CERT_GetGeneralNameFromListByType(CERTGeneralNameList *list,
CERTGeneralNameType type,
PLArenaPool *arena)
{
CERTName *name = NULL;
SECItem *item = NULL;
OtherName *other = NULL;
OtherName *tmpOther = NULL;
void *data;
PZ_Lock(list->lock);
data = CERT_GetGeneralNameByType(list->name, type, PR_FALSE);
if (data != NULL) {
switch (type) {
case certDNSName:
case certEDIPartyName:
case certIPAddress:
case certRegisterID:
case certRFC822Name:
case certX400Address:
case certURI:
if (arena != NULL) {
item = PORT_ArenaNew(arena, SECItem);
if (item != NULL) {
XXX SECITEM_CopyItem(arena, item, (SECItem *) data);
}
} else {
item = SECITEM_DupItem((SECItem *) data);
}
PZ_Unlock(list->lock);
return item;
case certOtherName:
other = (OtherName *) data;
if (arena != NULL) {
tmpOther = PORT_ArenaNew(arena, OtherName);
} else {
tmpOther = PORT_New(OtherName);
}
if (tmpOther != NULL) {
XXX SECITEM_CopyItem(arena, &tmpOther->oid, &other->oid);
XXX SECITEM_CopyItem(arena, &tmpOther->name, &other->name);
}
PZ_Unlock(list->lock);
return tmpOther;
case certDirectoryName:
if (arena) {
name = PORT_ArenaZNew(list->arena, CERTName);
if (name) {
XXX CERT_CopyName(arena, name, (CERTName *) data);
}
}
PZ_Unlock(list->lock);
return name;
}
}
PZ_Unlock(list->lock);
return NULL;
}
#endif
#if 0
/* This function is not exported from NSS shared libraries, and is not
** used inside of NSS.
** XXX it should NOT be a void function, since it does allocations
** that can fail.
*/
void
CERT_AddGeneralNameToList(CERTGeneralNameList *list,
CERTGeneralNameType type,
void *data, SECItem *oid)
{
CERTGeneralName *name;
if (list != NULL && data != NULL) {
PZ_Lock(list->lock);
name = CERT_NewGeneralName(list->arena, type);
if (!name)
goto done;
switch (type) {
case certDNSName:
case certEDIPartyName:
case certIPAddress:
case certRegisterID:
case certRFC822Name:
case certX400Address:
case certURI:
XXX SECITEM_CopyItem(list->arena, &name->name.other, (SECItem *)data);
break;
case certOtherName:
XXX SECITEM_CopyItem(list->arena, &name->name.OthName.name,
(SECItem *) data);
XXX SECITEM_CopyItem(list->arena, &name->name.OthName.oid,
oid);
break;
case certDirectoryName:
XXX CERT_CopyName(list->arena, &name->name.directoryName,
(CERTName *) data);
break;
}
list->name = cert_CombineNamesLists(list->name, name);
list->len++;
done:
PZ_Unlock(list->lock);
}
return;
}
#endif
|