1
8
9
15
16
17
18
19
20
21
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
45
46
47
48
49
54
55
56
57
58
59
60
61
62
63
67
78
79
80
84
85
86
87
88
89
90
91
92
93
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
194
195
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
229
230
231
232
233
234
239
240
241
242
243
244
245
246
247
248
249
250
251
256
257
258
259
270
271
272
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
310
311
312
313
314
315
316
317
318
319
320
321
327
328
329
330
331
332
338
339
340
341
342
346
347
348
354
355
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
379
383
387
391
395
399
400
401
402
403
404
405
406
407
408
412
413
414
415
416
417
418
419
423
424
425
426
427
428
429
430
431
432
433
438
439
440
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
467
469
471
473
475
477
479
481
482
483
484
485
486
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
519
520
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
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
598
599
604
605
606
607
608
609
610
624
628
629
630
631
636
637
638
639
644
645
646
647
652
653
654
655
660
661
662
663
668
669
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
713
714
715
716
717
718
719
720
721
722
723
724
729
730
731
732
733
734
735
736
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
815
816
821
822
823
824
825
826
827
828
833
834
850
851
856
857
858
859
860
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
1012
1013
1014
1015
1016
1017
1021
1025
1029
1033
1037
1038
1039
1040
1041
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
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
1140
1141
1142
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
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
1286
1287
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1340
1341
1343
1345
1347
1349
1351
1353
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1380
1381
1382
1383
1388
1389
1390
1395
1396
1397
1398
1399
1405
1406
1407
1408
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1445
1446
1447
1448
1449
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1476
1477
1478
1479
1483
1484
1485
1486
1487
1488
1489
1494
1499
1500
1501
1502
1507
1508
1509
1510
1511
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1530
1531
1532
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
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1617
1618
1619
1625
1626
1627
1628
1634
1635
1636
1637
1638
1639
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1665
1666
1667
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
1706
1707
1708
1713
1714
1715
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
1756
1757
1758
1762
1763
1769
1770
1771
1772
1776
1777
1778
1779
1780
1784
1785
1786
1787
1793
1797
1798
1799
1800
1801
1802
1806
1807
1808
1809
1810
1811
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1864
1865
1866
1867
1868
1869
1870
1871
1872
1876
1877
1878
1879
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1928
1929
1930
1931
1932
1937
1938
1939
1940
1945
1946
1947
1948
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2027
2028
2029
2030
2031
2032
2033
2034
2039
2040
2041
2042
2043
2049
2055
2061
2067
2073
2074
2079
2080
2081
2082
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2115
2116
2117
2122
2123
2124
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2225
2226
2227
2228
2229
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2283
2284
2285
2286
2287
2292
/* ... */
#include "includes.h"
#include "common.h"
#include "crypto/crypto.h"
#include "asn1.h"
#include "x509v3.h"5 includes
void x509_free_name(struct x509_name *name)
{
size_t i;
for (i = 0; i < name->num_attr; i++) {
os_free(name->attr[i].value);
name->attr[i].value = NULL;
name->attr[i].type = X509_NAME_ATTR_NOT_USED;
}{...}
name->num_attr = 0;
os_free(name->email);
name->email = NULL;
os_free(name->alt_email);
os_free(name->dns);
os_free(name->uri);
os_free(name->ip);
name->alt_email = name->dns = name->uri = NULL;
name->ip = NULL;
name->ip_len = 0;
os_memset(&name->rid, 0, sizeof(name->rid));
}{ ... }
/* ... */
void x509_certificate_free(struct x509_certificate *cert)
{
if (cert == NULL)
return;
if (cert->next) {
wpa_printf(MSG_DEBUG, "X509: x509_certificate_free: cer=%p "
"was still on a list (next=%p)\n",
cert, cert->next);
}{...}
x509_free_name(&cert->issuer);
x509_free_name(&cert->subject);
os_free(cert->public_key);
os_free(cert->sign_value);
os_free(cert->subject_dn);
os_free(cert);
}{ ... }
/* ... */
void x509_certificate_chain_free(struct x509_certificate *cert)
{
struct x509_certificate *next;
while (cert) {
next = cert->next;
cert->next = NULL;
x509_certificate_free(cert);
cert = next;
}{...}
}{ ... }
static int x509_whitespace(char c)
{
return c == ' ' || c == '\t';
}{ ... }
static void x509_str_strip_whitespace(char *a)
{
char *ipos, *opos;
int remove_whitespace = 1;
ipos = opos = a;
while (*ipos) {
if (remove_whitespace && x509_whitespace(*ipos))
ipos++;
else {
remove_whitespace = x509_whitespace(*ipos);
*opos++ = *ipos++;
}{...}
}{...}
*opos-- = '\0';
if (opos > a && x509_whitespace(*opos))
*opos = '\0';
}{ ... }
static int x509_str_compare(const char *a, const char *b)
{
char *aa, *bb;
int ret;
if (!a && b)
return -1;
if (a && !b)
return 1;
if (!a && !b)
return 0;
aa = os_strdup(a);
bb = os_strdup(b);
if (aa == NULL || bb == NULL) {
os_free(aa);
os_free(bb);
return os_strcasecmp(a, b);
}{...}
x509_str_strip_whitespace(aa);
x509_str_strip_whitespace(bb);
ret = os_strcasecmp(aa, bb);
os_free(aa);
os_free(bb);
return ret;
}{ ... }
/* ... */
int x509_name_compare(struct x509_name *a, struct x509_name *b)
{
int res;
size_t i;
if (!a && b)
return -1;
if (a && !b)
return 1;
if (!a && !b)
return 0;
if (a->num_attr < b->num_attr)
return -1;
if (a->num_attr > b->num_attr)
return 1;
for (i = 0; i < a->num_attr; i++) {
if (a->attr[i].type < b->attr[i].type)
return -1;
if (a->attr[i].type > b->attr[i].type)
return -1;
res = x509_str_compare(a->attr[i].value, b->attr[i].value);
if (res)
return res;
}{...}
res = x509_str_compare(a->email, b->email);
if (res)
return res;
return 0;
}{ ... }
int x509_parse_algorithm_identifier(const u8 *buf, size_t len,
struct x509_algorithm_identifier *id,
const u8 **next)
{
struct asn1_hdr hdr;
const u8 *pos, *end;
/* ... */
if (asn1_get_next(buf, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE (AlgorithmIdentifier)");
return -1;
}{...}
if (hdr.length > buf + len - hdr.payload)
return -1;
pos = hdr.payload;
end = pos + hdr.length;
*next = end;
if (asn1_get_oid(pos, end - pos, &id->oid, &pos))
return -1;
return 0;
}{ ... }
static int x509_parse_public_key(const u8 *buf, size_t len,
struct x509_certificate *cert,
const u8 **next)
{
struct asn1_hdr hdr;
const u8 *pos, *end;
/* ... */
pos = buf;
end = buf + len;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE (SubjectPublicKeyInfo)");
return -1;
}{...}
pos = hdr.payload;
if (hdr.length > end - pos)
return -1;
end = pos + hdr.length;
*next = end;
if (x509_parse_algorithm_identifier(pos, end - pos,
&cert->public_key_alg, &pos))
return -1;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_bitstring(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected BITSTRING (subjectPublicKey)");
return -1;
}{...}
if (hdr.length < 1)
return -1;
pos = hdr.payload;
if (*pos) {
wpa_printf(MSG_DEBUG,
"X509: BITSTRING (subjectPublicKey) - %d unused bits",
*pos);
/* ... */
}{...}
os_free(cert->public_key);
cert->public_key = os_memdup(pos + 1, hdr.length - 1);
if (cert->public_key == NULL) {
wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
"public key");
return -1;
}{...}
cert->public_key_len = hdr.length - 1;
wpa_hexdump(MSG_MSGDUMP, "X509: subjectPublicKey",
cert->public_key, cert->public_key_len);
return 0;
}{ ... }
int x509_parse_name(const u8 *buf, size_t len, struct x509_name *name,
const u8 **next)
{
struct asn1_hdr hdr;
const u8 *pos, *end, *set_pos, *set_end, *seq_pos, *seq_end;
struct asn1_oid oid;
char *val;
/* ... */
if (asn1_get_next(buf, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE (Name / RDNSequencer)");
return -1;
}{...}
pos = hdr.payload;
if (hdr.length > buf + len - pos)
return -1;
end = *next = pos + hdr.length;
while (pos < end) {
enum x509_name_attr_type type;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_set(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SET (RelativeDistinguishedName)");
x509_free_name(name);
return -1;
}{...}
set_pos = hdr.payload;
pos = set_end = hdr.payload + hdr.length;
if (asn1_get_next(set_pos, set_end - set_pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE (AttributeTypeAndValue)");
x509_free_name(name);
return -1;
}{...}
seq_pos = hdr.payload;
seq_end = hdr.payload + hdr.length;
if (asn1_get_oid(seq_pos, seq_end - seq_pos, &oid, &seq_pos)) {
x509_free_name(name);
return -1;
}{...}
if (asn1_get_next(seq_pos, seq_end - seq_pos, &hdr) < 0 ||
hdr.class != ASN1_CLASS_UNIVERSAL) {
wpa_printf(MSG_DEBUG, "X509: Failed to parse "
"AttributeValue");
x509_free_name(name);
return -1;
}{...}
if (!asn1_is_string_type(&hdr)) {
wpa_printf(MSG_DEBUG,
"X509: Ignore non-string type attribute (tag 0x%x)",
hdr.tag);
continue;
}{...}
/* ... */
type = X509_NAME_ATTR_NOT_USED;
if (oid.len == 4 &&
oid.oid[0] == 2 && oid.oid[1] == 5 && oid.oid[2] == 4) {
switch (oid.oid[3]) {
case 3:
type = X509_NAME_ATTR_CN;
break;...
case 6:
type = X509_NAME_ATTR_C;
break;...
case 7:
type = X509_NAME_ATTR_L;
break;...
case 8:
type = X509_NAME_ATTR_ST;
break;...
case 10:
type = X509_NAME_ATTR_O;
break;...
case 11:
type = X509_NAME_ATTR_OU;
break;...
}{...}
}{...} else if (oid.len == 7 &&
oid.oid[0] == 1 && oid.oid[1] == 2 &&
oid.oid[2] == 840 && oid.oid[3] == 113549 &&
oid.oid[4] == 1 && oid.oid[5] == 9 &&
oid.oid[6] == 1) {
os_free(name->email);
name->email = os_malloc(hdr.length + 1);
if (name->email == NULL) {
x509_free_name(name);
return -1;
}{...}
os_memcpy(name->email, hdr.payload, hdr.length);
name->email[hdr.length] = '\0';
continue;
}{...} else if (oid.len == 7 &&
oid.oid[0] == 0 && oid.oid[1] == 9 &&
oid.oid[2] == 2342 && oid.oid[3] == 19200300 &&
oid.oid[4] == 100 && oid.oid[5] == 1 &&
oid.oid[6] == 25) {
type = X509_NAME_ATTR_DC;
}{...}
if (type == X509_NAME_ATTR_NOT_USED) {
wpa_hexdump(MSG_DEBUG, "X509: Unrecognized OID",
(u8 *) oid.oid,
oid.len * sizeof(oid.oid[0]));
wpa_hexdump_ascii(MSG_MSGDUMP, "X509: Attribute Data",
hdr.payload, hdr.length);
continue;
}{...}
if (name->num_attr == X509_MAX_NAME_ATTRIBUTES) {
wpa_printf(MSG_INFO, "X509: Too many Name attributes");
x509_free_name(name);
return -1;
}{...}
val = dup_binstr(hdr.payload, hdr.length);
if (val == NULL) {
x509_free_name(name);
return -1;
}{...}
if (os_strlen(val) != hdr.length) {
wpa_printf(MSG_INFO, "X509: Reject certificate with "
"embedded NUL byte in a string (%s[NUL])",
val);
os_free(val);
x509_free_name(name);
return -1;
}{...}
name->attr[name->num_attr].type = type;
name->attr[name->num_attr].value = val;
name->num_attr++;
}{...}
return 0;
}{ ... }
static char * x509_name_attr_str(enum x509_name_attr_type type)
{
switch (type) {
case X509_NAME_ATTR_NOT_USED:
return "[N/A]";...
case X509_NAME_ATTR_DC:
return "DC";...
case X509_NAME_ATTR_CN:
return "CN";...
case X509_NAME_ATTR_C:
return "C";...
case X509_NAME_ATTR_L:
return "L";...
case X509_NAME_ATTR_ST:
return "ST";...
case X509_NAME_ATTR_O:
return "O";...
case X509_NAME_ATTR_OU:
return "OU";...
}{...}
return "?";
}{ ... }
/* ... */
void x509_name_string(struct x509_name *name, char *buf, size_t len)
{
char *pos, *end;
int ret;
size_t i;
if (len == 0)
return;
pos = buf;
end = buf + len;
for (i = 0; i < name->num_attr; i++) {
ret = os_snprintf(pos, end - pos, "%s=%s, ",
x509_name_attr_str(name->attr[i].type),
name->attr[i].value);
if (os_snprintf_error(end - pos, ret))
goto done;
pos += ret;
}{...}
if (pos > buf + 1 && pos[-1] == ' ' && pos[-2] == ',') {
pos--;
*pos = '\0';
pos--;
*pos = '\0';
}{...}
if (name->email) {
ret = os_snprintf(pos, end - pos, "/emailAddress=%s",
name->email);
if (os_snprintf_error(end - pos, ret))
goto done;
pos += ret;
}{...}
done:
if (pos < end)
*pos = '\0';
end[-1] = '\0';
}{ ... }
static int parse_uint2(const char *pos, size_t len)
{
char buf[2] = {0};
int ret = 0;
int i;
if (len < 2)
return -1;
for (i = 0; i < 2; i++) {
buf[0] = pos[i];
ret *= 10;
ret += atoi(buf);
}{...}
return ret;
}{ ... }
static int parse_uint4(const char *pos, size_t len)
{
char buf[2] = {0};
int ret = 0;
int i;
if (len < 4)
return -1;
for (i = 0; i < 4; i++) {
buf[0] = pos[i];
ret *= 10;
ret += atoi(buf);
}{...}
return ret;
}{ ... }
int x509_parse_time(const u8 *buf, size_t len, u8 asn1_tag, os_time_t *val)
{
const char *pos, *end;
int year, month, day, hour, min, sec;
/* ... */
pos = (const char *) buf;
end = pos + len;
switch (asn1_tag) {
case ASN1_TAG_UTCTIME:
if (len != 13 || buf[12] != 'Z') {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
"UTCTime format", buf, len);
return -1;
}{...}
year = parse_uint2(pos, end - pos);
if (year < 0) {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
"UTCTime year", buf, len);
return -1;
}{...}
if (year < 50)
year += 2000;
else
year += 1900;
pos += 2;
break;...
case ASN1_TAG_GENERALIZEDTIME:
if (len != 15 || buf[14] != 'Z') {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
"GeneralizedTime format", buf, len);
return -1;
}{...}
year = parse_uint4(pos, end - pos);
if (year < 0) {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
"GeneralizedTime year", buf, len);
return -1;
}{...}
pos += 4;
break;...
default:
wpa_printf(MSG_DEBUG, "X509: Expected UTCTime or "
"GeneralizedTime - found tag 0x%x", asn1_tag);
return -1;...
}{...}
month = parse_uint2(pos, end - pos);
if (month < 0) {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
"(month)", buf, len);
return -1;
}{...}
pos += 2;
day = parse_uint2(pos, end - pos);
if (day < 0) {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
"(day)", buf, len);
return -1;
}{...}
pos += 2;
hour = parse_uint2(pos, end - pos);
if (hour < 0) {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
"(hour)", buf, len);
return -1;
}{...}
pos += 2;
min = parse_uint2(pos, end - pos);
if (min < 0) {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
"(min)", buf, len);
return -1;
}{...}
pos += 2;
sec = parse_uint2(pos, end - pos);
if (sec < 0) {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
"(sec)", buf, len);
return -1;
}{...}
if (os_mktime(year, month, day, hour, min, sec, val) < 0) {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to convert Time",
buf, len);
if (year < 1970) {
/* ... */
wpa_printf(MSG_DEBUG, "X509: Year=%d before epoch - "
"assume epoch as the time", year);
*val = 0;
return 0;
}{...}
return -1;
}{...}
return 0;
}{ ... }
static int x509_parse_validity(const u8 *buf, size_t len,
struct x509_certificate *cert, const u8 **next)
{
struct asn1_hdr hdr;
const u8 *pos;
size_t plen;
/* ... */
if (asn1_get_next(buf, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr, "X509: Expected SEQUENCE (Validity)");
return -1;
}{...}
pos = hdr.payload;
plen = hdr.length;
if (plen > (size_t) (buf + len - pos))
return -1;
*next = pos + plen;
if (asn1_get_next(pos, plen, &hdr) < 0 ||
(!asn1_is_utctime(&hdr) && !asn1_is_generalizedtime(&hdr)) ||
x509_parse_time(hdr.payload, hdr.length, hdr.tag,
&cert->not_before) < 0) {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notBefore "
"Time", hdr.payload, hdr.length);
return -1;
}{...}
pos = hdr.payload + hdr.length;
plen = *next - pos;
if (asn1_get_next(pos, plen, &hdr) < 0 ||
(!asn1_is_utctime(&hdr) && !asn1_is_generalizedtime(&hdr)) ||
x509_parse_time(hdr.payload, hdr.length, hdr.tag,
&cert->not_after) < 0) {
wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notAfter "
"Time", hdr.payload, hdr.length);
return -1;
}{...}
wpa_printf(MSG_MSGDUMP, "X509: Validity: notBefore: %lu notAfter: %lu",
(unsigned long) cert->not_before,
(unsigned long) cert->not_after);
return 0;
}{ ... }
static int x509_id_ce_oid(struct asn1_oid *oid)
{
return oid->len >= 4 &&
oid->oid[0] == 2 &&
oid->oid[1] == 5 &&
oid->oid[2] == 29 ;
}{ ... }
static int x509_any_ext_key_usage_oid(struct asn1_oid *oid)
{
return oid->len == 6 &&
x509_id_ce_oid(oid) &&
oid->oid[3] == 37 &&
oid->oid[4] == 0 ;
}{ ... }
static int x509_parse_ext_key_usage(struct x509_certificate *cert,
const u8 *pos, size_t len)
{
struct asn1_hdr hdr;
/* ... */
if (asn1_get_next(pos, len, &hdr) < 0 || !asn1_is_bitstring(&hdr) ||
hdr.length < 1) {
asn1_unexpected(&hdr, "X509: Expected BIT STRING in KeyUsage");
return -1;
}{...}
cert->extensions_present |= X509_EXT_KEY_USAGE;
cert->key_usage = asn1_bit_string_to_long(hdr.payload, hdr.length);
wpa_printf(MSG_DEBUG, "X509: KeyUsage 0x%lx", cert->key_usage);
return 0;
}{ ... }
static int x509_parse_ext_basic_constraints(struct x509_certificate *cert,
const u8 *pos, size_t len)
{
struct asn1_hdr hdr;
unsigned long value;
size_t left;
const u8 *end_seq;
/* ... */
if (asn1_get_next(pos, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE in BasicConstraints");
return -1;
}{...}
cert->extensions_present |= X509_EXT_BASIC_CONSTRAINTS;
if (hdr.length == 0)
return 0;
end_seq = hdr.payload + hdr.length;
if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0) {
wpa_printf(MSG_DEBUG, "X509: Failed to parse "
"BasicConstraints");
return -1;
}{...}
if (asn1_is_boolean(&hdr)) {
cert->ca = hdr.payload[0];
pos = hdr.payload + hdr.length;
if (pos >= end_seq) {
wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d",
cert->ca);
return 0;
}{...}
if (asn1_get_next(pos, end_seq - pos, &hdr) < 0) {
wpa_printf(MSG_DEBUG, "X509: Failed to parse "
"BasicConstraints");
return -1;
}{...}
}{...}
if (!asn1_is_integer(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected INTEGER in BasicConstraints");
return -1;
}{...}
pos = hdr.payload;
left = hdr.length;
value = 0;
while (left) {
value <<= 8;
value |= *pos++;
left--;
}{...}
cert->path_len_constraint = value;
cert->extensions_present |= X509_EXT_PATH_LEN_CONSTRAINT;
wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d "
"pathLenConstraint=%lu",
cert->ca, cert->path_len_constraint);
return 0;
}{ ... }
static int x509_parse_alt_name_rfc8222(struct x509_name *name,
const u8 *pos, size_t len)
{
wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - rfc822Name", pos, len);
os_free(name->alt_email);
name->alt_email = os_zalloc(len + 1);
if (name->alt_email == NULL)
return -1;
os_memcpy(name->alt_email, pos, len);
if (os_strlen(name->alt_email) != len) {
wpa_printf(MSG_INFO, "X509: Reject certificate with "
"embedded NUL byte in rfc822Name (%s[NUL])",
name->alt_email);
os_free(name->alt_email);
name->alt_email = NULL;
return -1;
}{...}
return 0;
}{ ... }
static int x509_parse_alt_name_dns(struct x509_name *name,
const u8 *pos, size_t len)
{
wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - dNSName", pos, len);
os_free(name->dns);
name->dns = os_zalloc(len + 1);
if (name->dns == NULL)
return -1;
os_memcpy(name->dns, pos, len);
if (os_strlen(name->dns) != len) {
wpa_printf(MSG_INFO, "X509: Reject certificate with "
"embedded NUL byte in dNSName (%s[NUL])",
name->dns);
os_free(name->dns);
name->dns = NULL;
return -1;
}{...}
return 0;
}{ ... }
static int x509_parse_alt_name_uri(struct x509_name *name,
const u8 *pos, size_t len)
{
wpa_hexdump_ascii(MSG_MSGDUMP,
"X509: altName - uniformResourceIdentifier",
pos, len);
os_free(name->uri);
name->uri = os_zalloc(len + 1);
if (name->uri == NULL)
return -1;
os_memcpy(name->uri, pos, len);
if (os_strlen(name->uri) != len) {
wpa_printf(MSG_INFO, "X509: Reject certificate with "
"embedded NUL byte in uniformResourceIdentifier "
"(%s[NUL])", name->uri);
os_free(name->uri);
name->uri = NULL;
return -1;
}{...}
return 0;
}{ ... }
static int x509_parse_alt_name_ip(struct x509_name *name,
const u8 *pos, size_t len)
{
wpa_hexdump(MSG_MSGDUMP, "X509: altName - iPAddress", pos, len);
os_free(name->ip);
name->ip = os_memdup(pos, len);
if (name->ip == NULL)
return -1;
name->ip_len = len;
return 0;
}{ ... }
static int x509_parse_alt_name_rid(struct x509_name *name,
const u8 *pos, size_t len)
{
char buf[80];
if (asn1_parse_oid(pos, len, &name->rid) < 0)
return -1;
asn1_oid_to_str(&name->rid, buf, sizeof(buf));
wpa_printf(MSG_MSGDUMP, "X509: altName - registeredID: %s", buf);
return 0;
}{ ... }
static int x509_parse_ext_alt_name(struct x509_name *name,
const u8 *pos, size_t len)
{
struct asn1_hdr hdr;
const u8 *p, *end;
/* ... */
for (p = pos, end = pos + len; p < end; p = hdr.payload + hdr.length) {
int res;
if (asn1_get_next(p, end - p, &hdr) < 0) {
wpa_printf(MSG_DEBUG, "X509: Failed to parse "
"SubjectAltName item");
return -1;
}{...}
if (hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC)
continue;
switch (hdr.tag) {
case 1:
res = x509_parse_alt_name_rfc8222(name, hdr.payload,
hdr.length);
break;...
case 2:
res = x509_parse_alt_name_dns(name, hdr.payload,
hdr.length);
break;...
case 6:
res = x509_parse_alt_name_uri(name, hdr.payload,
hdr.length);
break;...
case 7:
res = x509_parse_alt_name_ip(name, hdr.payload,
hdr.length);
break;...
case 8:
res = x509_parse_alt_name_rid(name, hdr.payload,
hdr.length);
break;...
case 0:
case 3:
case 4:
case 5:
default:
res = 0;
break;...
}{...}
if (res < 0)
return res;
}{...}
return 0;
}{ ... }
static int x509_parse_ext_subject_alt_name(struct x509_certificate *cert,
const u8 *pos, size_t len)
{
struct asn1_hdr hdr;
if (asn1_get_next(pos, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE in SubjectAltName");
return -1;
}{...}
wpa_printf(MSG_DEBUG, "X509: SubjectAltName");
cert->extensions_present |= X509_EXT_SUBJECT_ALT_NAME;
if (hdr.length == 0)
return 0;
return x509_parse_ext_alt_name(&cert->subject, hdr.payload,
hdr.length);
}{ ... }
static int x509_parse_ext_issuer_alt_name(struct x509_certificate *cert,
const u8 *pos, size_t len)
{
struct asn1_hdr hdr;
if (asn1_get_next(pos, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE in IssuerAltName");
return -1;
}{...}
wpa_printf(MSG_DEBUG, "X509: IssuerAltName");
cert->extensions_present |= X509_EXT_ISSUER_ALT_NAME;
if (hdr.length == 0)
return 0;
return x509_parse_ext_alt_name(&cert->issuer, hdr.payload,
hdr.length);
}{ ... }
static int x509_id_cert_policy_any_oid(struct asn1_oid *oid)
{
return oid->len == 5 &&
oid->oid[0] == 2 &&
oid->oid[1] == 5 &&
oid->oid[2] == 29 &&
oid->oid[3] == 32 &&
oid->oid[4] == 0 ;
}{ ... }
static int x509_id_wfa_oid(struct asn1_oid *oid)
{
return oid->len >= 7 &&
oid->oid[0] == 1 &&
oid->oid[1] == 3 &&
oid->oid[2] == 6 &&
oid->oid[3] == 1 &&
oid->oid[4] == 4 &&
oid->oid[5] == 1 &&
oid->oid[6] == 40808 ;
}{ ... }
static int x509_id_wfa_tod_oid(struct asn1_oid *oid)
{
return oid->len >= 9 &&
x509_id_wfa_oid(oid) &&
oid->oid[7] == 1 &&
oid->oid[8] == 3;
}{ ... }
static int x509_id_wfa_tod_strict_oid(struct asn1_oid *oid)
{
return oid->len == 10 &&
x509_id_wfa_tod_oid(oid) &&
oid->oid[9] == 1;
}{ ... }
static int x509_id_wfa_tod_tofu_oid(struct asn1_oid *oid)
{
return oid->len == 10 &&
x509_id_wfa_tod_oid(oid) &&
oid->oid[9] == 2;
}{ ... }
static int x509_parse_ext_certificate_policies(struct x509_certificate *cert,
const u8 *pos, size_t len)
{
struct asn1_hdr hdr;
const u8 *end;
/* ... */
if (asn1_get_next(pos, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE (certificatePolicies)");
return -1;
}{...}
if (hdr.length > pos + len - hdr.payload)
return -1;
pos = hdr.payload;
end = pos + hdr.length;
wpa_hexdump(MSG_MSGDUMP, "X509: certificatePolicies", pos, end - pos);
while (pos < end) {
const u8 *pol_end;
struct asn1_oid oid;
char buf[80];
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE (PolicyInformation)");
return -1;
}{...}
if (hdr.length > end - hdr.payload)
return -1;
pos = hdr.payload;
pol_end = pos + hdr.length;
wpa_hexdump(MSG_MSGDUMP, "X509: PolicyInformation",
pos, pol_end - pos);
if (asn1_get_oid(pos, pol_end - pos, &oid, &pos))
return -1;
if (x509_id_cert_policy_any_oid(&oid)) {
os_strlcpy(buf, "anyPolicy-STRICT", sizeof(buf));
cert->certificate_policy |=
X509_EXT_CERT_POLICY_ANY;
}{...} else if (x509_id_wfa_tod_strict_oid(&oid)) {
os_strlcpy(buf, "TOD-STRICT", sizeof(buf));
cert->certificate_policy |=
X509_EXT_CERT_POLICY_TOD_STRICT;
}{...} else if (x509_id_wfa_tod_tofu_oid(&oid)) {
os_strlcpy(buf, "TOD-TOFU", sizeof(buf));
cert->certificate_policy |=
X509_EXT_CERT_POLICY_TOD_TOFU;
}{...} else {
asn1_oid_to_str(&oid, buf, sizeof(buf));
}{...}
wpa_printf(MSG_DEBUG, "policyIdentifier: %s", buf);
pos = pol_end;
}{...}
cert->extensions_present |= X509_EXT_CERTIFICATE_POLICY;
return 0;
}{ ... }
static int x509_id_pkix_oid(struct asn1_oid *oid)
{
return oid->len >= 7 &&
oid->oid[0] == 1 &&
oid->oid[1] == 3 &&
oid->oid[2] == 6 &&
oid->oid[3] == 1 &&
oid->oid[4] == 5 &&
oid->oid[5] == 5 &&
oid->oid[6] == 7 ;
}{ ... }
static int x509_id_kp_oid(struct asn1_oid *oid)
{
return oid->len >= 8 &&
x509_id_pkix_oid(oid) &&
oid->oid[7] == 3 ;
}{ ... }
static int x509_id_kp_server_auth_oid(struct asn1_oid *oid)
{
return oid->len == 9 &&
x509_id_kp_oid(oid) &&
oid->oid[8] == 1 ;
}{ ... }
static int x509_id_kp_client_auth_oid(struct asn1_oid *oid)
{
return oid->len == 9 &&
x509_id_kp_oid(oid) &&
oid->oid[8] == 2 ;
}{ ... }
static int x509_id_kp_ocsp_oid(struct asn1_oid *oid)
{
return oid->len == 9 &&
x509_id_kp_oid(oid) &&
oid->oid[8] == 9 ;
}{ ... }
static int x509_parse_ext_ext_key_usage(struct x509_certificate *cert,
const u8 *pos, size_t len)
{
struct asn1_hdr hdr;
const u8 *end;
struct asn1_oid oid;
/* ... */
if (asn1_get_next(pos, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE (ExtKeyUsageSyntax)");
return -1;
}{...}
if (hdr.length > pos + len - hdr.payload)
return -1;
pos = hdr.payload;
end = pos + hdr.length;
wpa_hexdump(MSG_MSGDUMP, "X509: ExtKeyUsageSyntax", pos, end - pos);
while (pos < end) {
char buf[80];
if (asn1_get_oid(pos, end - pos, &oid, &pos))
return -1;
if (x509_any_ext_key_usage_oid(&oid)) {
os_strlcpy(buf, "anyExtendedKeyUsage", sizeof(buf));
cert->ext_key_usage |= X509_EXT_KEY_USAGE_ANY;
}{...} else if (x509_id_kp_server_auth_oid(&oid)) {
os_strlcpy(buf, "id-kp-serverAuth", sizeof(buf));
cert->ext_key_usage |= X509_EXT_KEY_USAGE_SERVER_AUTH;
}{...} else if (x509_id_kp_client_auth_oid(&oid)) {
os_strlcpy(buf, "id-kp-clientAuth", sizeof(buf));
cert->ext_key_usage |= X509_EXT_KEY_USAGE_CLIENT_AUTH;
}{...} else if (x509_id_kp_ocsp_oid(&oid)) {
os_strlcpy(buf, "id-kp-OCSPSigning", sizeof(buf));
cert->ext_key_usage |= X509_EXT_KEY_USAGE_OCSP;
}{...} else {
asn1_oid_to_str(&oid, buf, sizeof(buf));
}{...}
wpa_printf(MSG_DEBUG, "ExtKeyUsage KeyPurposeId: %s", buf);
}{...}
cert->extensions_present |= X509_EXT_EXT_KEY_USAGE;
return 0;
}{ ... }
static int x509_parse_extension_data(struct x509_certificate *cert,
struct asn1_oid *oid,
const u8 *pos, size_t len)
{
if (!x509_id_ce_oid(oid))
return 1;
/* ... */
switch (oid->oid[3]) {
case 15:
return x509_parse_ext_key_usage(cert, pos, len);...
case 17:
return x509_parse_ext_subject_alt_name(cert, pos, len);...
case 18:
return x509_parse_ext_issuer_alt_name(cert, pos, len);...
case 19:
return x509_parse_ext_basic_constraints(cert, pos, len);...
case 32:
return x509_parse_ext_certificate_policies(cert, pos, len);...
case 37:
return x509_parse_ext_ext_key_usage(cert, pos, len);...
default:
return 1;...
}{...}
}{ ... }
static int x509_parse_extension(struct x509_certificate *cert,
const u8 *pos, size_t len, const u8 **next)
{
const u8 *end;
struct asn1_hdr hdr;
struct asn1_oid oid;
int critical_ext = 0, res;
char buf[80];
/* ... */
if (asn1_get_next(pos, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr, "X509: Expected SEQUENCE in Extensions");
return -1;
}{...}
pos = hdr.payload;
*next = end = pos + hdr.length;
if (asn1_get_oid(pos, end - pos, &oid, &pos) < 0) {
wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data for "
"Extension (expected OID)");
return -1;
}{...}
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
(!asn1_is_boolean(&hdr) && !asn1_is_octetstring(&hdr))) {
asn1_unexpected(&hdr,
"X509: Expected BOOLEAN or OCTETSTRING in Extensions");
return -1;
}{...}
if (hdr.tag == ASN1_TAG_BOOLEAN) {
critical_ext = hdr.payload[0];
pos = hdr.payload;
/* ... */
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
(!asn1_is_octetstring(&hdr) &&
!(hdr.class == ASN1_CLASS_PRIVATE &&
hdr.tag == ASN1_TAG_OCTETSTRING))) {
asn1_unexpected(&hdr,
"X509: Expected OCTETSTRING in Extensions");
return -1;
}{...}
}{...}
asn1_oid_to_str(&oid, buf, sizeof(buf));
wpa_printf(MSG_DEBUG, "X509: Extension: extnID=%s critical=%d",
buf, critical_ext);
wpa_hexdump(MSG_MSGDUMP, "X509: extnValue", hdr.payload, hdr.length);
res = x509_parse_extension_data(cert, &oid, hdr.payload, hdr.length);
if (res < 0)
return res;
if (res == 1 && critical_ext) {
wpa_printf(MSG_INFO, "X509: Unknown critical extension %s",
buf);
return -1;
}{...}
return 0;
}{ ... }
static int x509_parse_extensions(struct x509_certificate *cert,
const u8 *pos, size_t len)
{
const u8 *end;
struct asn1_hdr hdr;
if (asn1_get_next(pos, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr, "X509: Expected SEQUENCE for Extensions");
return -1;
}{...}
pos = hdr.payload;
end = pos + hdr.length;
while (pos < end) {
if (x509_parse_extension(cert, pos, end - pos, &pos)
< 0)
return -1;
}{...}
return 0;
}{ ... }
static int x509_parse_tbs_certificate(const u8 *buf, size_t len,
struct x509_certificate *cert,
const u8 **next)
{
struct asn1_hdr hdr;
const u8 *pos, *end;
size_t left;
char sbuf[128];
unsigned long value;
const u8 *subject_dn;
if (asn1_get_next(buf, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: tbsCertificate did not start with a valid SEQUENCE");
return -1;
}{...}
pos = hdr.payload;
end = *next = pos + hdr.length;
/* ... */
if (asn1_get_next(pos, end - pos, &hdr) < 0)
return -1;
pos = hdr.payload;
if (asn1_is_cs_tag(&hdr, 0) && hdr.constructed) {
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_integer(&hdr)) {
asn1_unexpected(&hdr,
"X509: No INTEGER tag found for version field");
return -1;
}{...}
if (hdr.length != 1) {
wpa_printf(MSG_DEBUG, "X509: Unexpected version field "
"length %u (expected 1)", hdr.length);
return -1;
}{...}
pos = hdr.payload;
left = hdr.length;
value = 0;
while (left) {
value <<= 8;
value |= *pos++;
left--;
}{...}
cert->version = value;
if (cert->version != X509_CERT_V1 &&
cert->version != X509_CERT_V2 &&
cert->version != X509_CERT_V3) {
wpa_printf(MSG_DEBUG, "X509: Unsupported version %d",
cert->version + 1);
return -1;
}{...}
if (asn1_get_next(pos, end - pos, &hdr) < 0)
return -1;
}{...} else
cert->version = X509_CERT_V1;
wpa_printf(MSG_MSGDUMP, "X509: Version X.509v%d", cert->version + 1);
if (!asn1_is_integer(&hdr) ||
hdr.length < 1 || hdr.length > X509_MAX_SERIAL_NUM_LEN) {
asn1_unexpected(&hdr,
"X509: No INTEGER tag found for serialNumber");
return -1;
}{...}
pos = hdr.payload + hdr.length;
while (hdr.length > 0 && hdr.payload[0] == 0) {
hdr.payload++;
hdr.length--;
}{...}
os_memcpy(cert->serial_number, hdr.payload, hdr.length);
cert->serial_number_len = hdr.length;
wpa_hexdump(MSG_MSGDUMP, "X509: serialNumber", cert->serial_number,
cert->serial_number_len);
if (x509_parse_algorithm_identifier(pos, end - pos, &cert->signature,
&pos))
return -1;
if (x509_parse_name(pos, end - pos, &cert->issuer, &pos))
return -1;
x509_name_string(&cert->issuer, sbuf, sizeof(sbuf));
wpa_printf(MSG_MSGDUMP, "X509: issuer %s", sbuf);
if (x509_parse_validity(pos, end - pos, cert, &pos))
return -1;
subject_dn = pos;
if (x509_parse_name(pos, end - pos, &cert->subject, &pos))
return -1;
cert->subject_dn = os_malloc(pos - subject_dn);
if (!cert->subject_dn)
return -1;
cert->subject_dn_len = pos - subject_dn;
os_memcpy(cert->subject_dn, subject_dn, cert->subject_dn_len);
x509_name_string(&cert->subject, sbuf, sizeof(sbuf));
wpa_printf(MSG_MSGDUMP, "X509: subject %s", sbuf);
if (x509_parse_public_key(pos, end - pos, cert, &pos))
return -1;
if (pos == end)
return 0;
if (cert->version == X509_CERT_V1)
return 0;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
asn1_unexpected(&hdr,
"X509: Expected Context-Specific tag to parse optional tbsCertificate field(s)");
return -1;
}{...}
if (hdr.tag == 1) {
wpa_printf(MSG_DEBUG, "X509: issuerUniqueID");
pos = hdr.payload + hdr.length;
if (pos == end)
return 0;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
asn1_unexpected(&hdr,
"X509: Expected Context-Specific tag to parse optional tbsCertificate field(s)");
return -1;
}{...}
}{...}
if (hdr.tag == 2) {
wpa_printf(MSG_DEBUG, "X509: subjectUniqueID");
pos = hdr.payload + hdr.length;
if (pos == end)
return 0;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
asn1_unexpected(&hdr,
"X509: Expected Context-Specific tag to parse optional tbsCertificate field(s)");
return -1;
}{...}
}{...}
if (hdr.tag != 3) {
wpa_printf(MSG_DEBUG,
"X509: Ignored unexpected Context-Specific constructed %d tag %d in optional tbsCertificate fields",
hdr.constructed, hdr.tag);
return 0;
}{...}
if (cert->version != X509_CERT_V3) {
wpa_printf(MSG_DEBUG, "X509: X.509%d certificate and "
"Extensions data which are only allowed for "
"version 3", cert->version + 1);
return -1;
}{...}
if (x509_parse_extensions(cert, hdr.payload, hdr.length) < 0)
return -1;
pos = hdr.payload + hdr.length;
if (pos < end) {
wpa_hexdump(MSG_DEBUG,
"X509: Ignored extra tbsCertificate data",
pos, end - pos);
}{...}
return 0;
}{ ... }
static int x509_rsadsi_oid(struct asn1_oid *oid)
{
return oid->len >= 4 &&
oid->oid[0] == 1 &&
oid->oid[1] == 2 &&
oid->oid[2] == 840 &&
oid->oid[3] == 113549 ;
}{ ... }
static int x509_pkcs_oid(struct asn1_oid *oid)
{
return oid->len >= 5 &&
x509_rsadsi_oid(oid) &&
oid->oid[4] == 1 ;
}{ ... }
static int x509_digest_oid(struct asn1_oid *oid)
{
return oid->len >= 5 &&
x509_rsadsi_oid(oid) &&
oid->oid[4] == 2 ;
}{ ... }
int x509_sha1_oid(struct asn1_oid *oid)
{
return oid->len == 6 &&
oid->oid[0] == 1 &&
oid->oid[1] == 3 &&
oid->oid[2] == 14 &&
oid->oid[3] == 3 &&
oid->oid[4] == 2 &&
oid->oid[5] == 26 ;
}{ ... }
static int x509_sha2_oid(struct asn1_oid *oid)
{
return oid->len == 9 &&
oid->oid[0] == 2 &&
oid->oid[1] == 16 &&
oid->oid[2] == 840 &&
oid->oid[3] == 1 &&
oid->oid[4] == 101 &&
oid->oid[5] == 3 &&
oid->oid[6] == 4 &&
oid->oid[7] == 2 ;
}{ ... }
int x509_sha256_oid(struct asn1_oid *oid)
{
return x509_sha2_oid(oid) &&
oid->oid[8] == 1 ;
}{ ... }
int x509_sha384_oid(struct asn1_oid *oid)
{
return x509_sha2_oid(oid) &&
oid->oid[8] == 2 ;
}{ ... }
int x509_sha512_oid(struct asn1_oid *oid)
{
return x509_sha2_oid(oid) &&
oid->oid[8] == 3 ;
}{ ... }
/* ... */
struct x509_certificate * x509_certificate_parse(const u8 *buf, size_t len)
{
struct asn1_hdr hdr;
const u8 *pos, *end, *hash_start;
struct x509_certificate *cert;
cert = os_zalloc(sizeof(*cert) + len);
if (cert == NULL)
return NULL;
os_memcpy(cert + 1, buf, len);
cert->cert_start = (u8 *) (cert + 1);
cert->cert_len = len;
pos = buf;
end = buf + len;
if (asn1_get_next(pos, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Certificate did not start with a valid SEQUENCE");
x509_certificate_free(cert);
return NULL;
}{...}
pos = hdr.payload;
if (hdr.length > end - pos) {
x509_certificate_free(cert);
return NULL;
}{...}
if (hdr.length < end - pos) {
wpa_hexdump(MSG_MSGDUMP, "X509: Ignoring extra data after DER "
"encoded certificate",
pos + hdr.length, end - (pos + hdr.length));
end = pos + hdr.length;
}{...}
hash_start = pos;
cert->tbs_cert_start = cert->cert_start + (hash_start - buf);
if (x509_parse_tbs_certificate(pos, end - pos, cert, &pos)) {
x509_certificate_free(cert);
return NULL;
}{...}
cert->tbs_cert_len = pos - hash_start;
if (x509_parse_algorithm_identifier(pos, end - pos,
&cert->signature_alg, &pos)) {
x509_certificate_free(cert);
return NULL;
}{...}
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_bitstring(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected BITSTRING (signatureValue)");
x509_certificate_free(cert);
return NULL;
}{...}
if (hdr.length < 1) {
x509_certificate_free(cert);
return NULL;
}{...}
pos = hdr.payload;
if (*pos) {
wpa_printf(MSG_DEBUG,
"X509: BITSTRING (signatureValue) - %d unused bits",
*pos);
/* ... */
x509_certificate_free(cert);
return NULL;
}{...}
os_free(cert->sign_value);
cert->sign_value = os_memdup(pos + 1, hdr.length - 1);
if (cert->sign_value == NULL) {
wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
"signatureValue");
x509_certificate_free(cert);
return NULL;
}{...}
cert->sign_value_len = hdr.length - 1;
wpa_hexdump(MSG_MSGDUMP, "X509: signature",
cert->sign_value, cert->sign_value_len);
return cert;
}{ ... }
/* ... */
int x509_certificate_check_signature(struct x509_certificate *issuer,
struct x509_certificate *cert)
{
return x509_check_signature(issuer, &cert->signature,
cert->sign_value, cert->sign_value_len,
cert->tbs_cert_start, cert->tbs_cert_len);
}{ ... }
int x509_check_signature(struct x509_certificate *issuer,
struct x509_algorithm_identifier *signature,
const u8 *sign_value, size_t sign_value_len,
const u8 *signed_data, size_t signed_data_len)
{
struct crypto_public_key *pk;
u8 *data;
const u8 *pos, *end, *next, *da_end;
size_t data_len;
struct asn1_hdr hdr;
struct asn1_oid oid;
u8 hash[64];
size_t hash_len;
const u8 *addr[1] = { signed_data };
size_t len[1] = { signed_data_len };
if (!x509_pkcs_oid(&signature->oid) ||
signature->oid.len != 7 ||
signature->oid.oid[5] != 1 ) {
wpa_printf(MSG_DEBUG, "X509: Unrecognized signature "
"algorithm");
return -1;
}{...}
pk = crypto_public_key_import(issuer->public_key,
issuer->public_key_len);
if (pk == NULL)
return -1;
data_len = sign_value_len;
data = os_malloc(data_len);
if (data == NULL) {
crypto_public_key_free(pk);
return -1;
}{...}
if (crypto_public_key_decrypt_pkcs1(pk, sign_value,
sign_value_len, data,
&data_len) < 0) {
wpa_printf(MSG_DEBUG, "X509: Failed to decrypt signature");
crypto_public_key_free(pk);
os_free(data);
return -1;
}{...}
crypto_public_key_free(pk);
wpa_hexdump(MSG_MSGDUMP, "X509: Signature data D", data, data_len);
/* ... */
if (asn1_get_next(data, data_len, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr, "X509: Expected SEQUENCE (DigestInfo)");
os_free(data);
return -1;
}{...}
wpa_hexdump(MSG_MSGDUMP, "X509: DigestInfo", hdr.payload, hdr.length);
pos = hdr.payload;
end = pos + hdr.length;
/* ... */
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"X509: Expected SEQUENCE (AlgorithmIdentifier)");
os_free(data);
return -1;
}{...}
wpa_hexdump(MSG_MSGDUMP, "X509: DigestAlgorithmIdentifier",
hdr.payload, hdr.length);
da_end = hdr.payload + hdr.length;
if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
wpa_printf(MSG_DEBUG, "X509: Failed to parse digestAlgorithm");
os_free(data);
return -1;
}{...}
wpa_hexdump(MSG_MSGDUMP, "X509: Digest algorithm parameters",
next, da_end - next);
/* ... */
if (da_end > next &&
(asn1_get_next(next, da_end - next, &hdr) < 0 ||
!asn1_is_null(&hdr) ||
hdr.payload + hdr.length != da_end)) {
wpa_printf(MSG_DEBUG,
"X509: Unexpected digest algorithm parameters");
os_free(data);
return -1;
}{...}
if (x509_sha1_oid(&oid)) {
if (signature->oid.oid[6] != 5 ) {
wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA1 "
"does not match with certificate "
"signatureAlgorithm (%lu)",
signature->oid.oid[6]);
os_free(data);
return -1;
}{...}
goto skip_digest_oid;
}{...}
if (x509_sha256_oid(&oid)) {
if (signature->oid.oid[6] !=
11 ) {
wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA256 "
"does not match with certificate "
"signatureAlgorithm (%lu)",
signature->oid.oid[6]);
os_free(data);
return -1;
}{...}
goto skip_digest_oid;
}{...}
if (x509_sha384_oid(&oid)) {
if (signature->oid.oid[6] != 12 ) {
wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA384 "
"does not match with certificate "
"signatureAlgorithm (%lu)",
signature->oid.oid[6]);
os_free(data);
return -1;
}{...}
goto skip_digest_oid;
}{...}
if (x509_sha512_oid(&oid)) {
if (signature->oid.oid[6] != 13 ) {
wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA512 "
"does not match with certificate "
"signatureAlgorithm (%lu)",
signature->oid.oid[6]);
os_free(data);
return -1;
}{...}
goto skip_digest_oid;
}{...}
if (!x509_digest_oid(&oid)) {
wpa_printf(MSG_DEBUG, "X509: Unrecognized digestAlgorithm");
os_free(data);
return -1;
}{...}
switch (oid.oid[5]) {
case 5:
if (signature->oid.oid[6] != 4 ) {
wpa_printf(MSG_DEBUG, "X509: digestAlgorithm MD5 does "
"not match with certificate "
"signatureAlgorithm (%lu)",
signature->oid.oid[6]);
os_free(data);
return -1;
}{...}
break;...
case 2:
case 4:
default:
wpa_printf(MSG_DEBUG, "X509: Unsupported digestAlgorithm "
"(%lu)", oid.oid[5]);
os_free(data);
return -1;...
}{...}
skip_digest_oid:
pos = da_end;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_octetstring(&hdr)) {
asn1_unexpected(&hdr, "X509: Expected OCTETSTRING (Digest)");
os_free(data);
return -1;
}{...}
wpa_hexdump(MSG_MSGDUMP, "X509: Decrypted Digest",
hdr.payload, hdr.length);
switch (signature->oid.oid[6]) {
case 4:
md5_vector(1, addr, len, hash);
hash_len = 16;
wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (MD5)",
hash, hash_len);
break;...
case 5:
sha1_vector(1, addr, len, hash);
hash_len = 20;
wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA1)",
hash, hash_len);
break;...
case 11:
sha256_vector(1, addr, len, hash);
hash_len = 32;
wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA256)",
hash, hash_len);
break;...
case 12:
sha384_vector(1, addr, len, hash);
hash_len = 48;
wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA384)",
hash, hash_len);
break;...
case 13:
sha512_vector(1, addr, len, hash);
hash_len = 64;
wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA512)",
hash, hash_len);
break;...
case 2:
default:
wpa_printf(MSG_INFO, "X509: Unsupported certificate signature "
"algorithm (%lu)", signature->oid.oid[6]);
os_free(data);
return -1;...
}{...}
if (hdr.length != hash_len ||
os_memcmp_const(hdr.payload, hash, hdr.length) != 0) {
wpa_printf(MSG_INFO, "X509: Certificate Digest does not match "
"with calculated tbsCertificate hash");
os_free(data);
return -1;
}{...}
if (hdr.payload + hdr.length < data + data_len) {
wpa_hexdump(MSG_INFO,
"X509: Extra data after certificate signature hash",
hdr.payload + hdr.length,
data + data_len - hdr.payload - hdr.length);
os_free(data);
return -1;
}{...}
os_free(data);
wpa_printf(MSG_DEBUG, "X509: Certificate Digest matches with "
"calculated tbsCertificate hash");
return 0;
}{ ... }
static int x509_valid_issuer(const struct x509_certificate *cert)
{
if ((cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS) &&
!cert->ca) {
wpa_printf(MSG_DEBUG, "X509: Non-CA certificate used as an "
"issuer");
return -1;
}{...}
if (cert->version == X509_CERT_V3 &&
!(cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS)) {
wpa_printf(MSG_DEBUG, "X509: v3 CA certificate did not "
"include BasicConstraints extension");
return -1;
}{...}
if ((cert->extensions_present & X509_EXT_KEY_USAGE) &&
!(cert->key_usage & X509_KEY_USAGE_KEY_CERT_SIGN)) {
wpa_printf(MSG_DEBUG, "X509: Issuer certificate did not have "
"keyCertSign bit in Key Usage");
return -1;
}{...}
return 0;
}{ ... }
/* ... */
int x509_certificate_chain_validate(struct x509_certificate *trusted,
struct x509_certificate *chain,
int *reason, int disable_time_checks)
{
long unsigned idx;
int chain_trusted = 0;
struct x509_certificate *cert, *trust;
char buf[128];
struct os_time now;
*reason = X509_VALIDATE_OK;
wpa_printf(MSG_DEBUG, "X509: Validate certificate chain");
os_get_time(&now);
for (cert = chain, idx = 0; cert; cert = cert->next, idx++) {
cert->issuer_trusted = 0;
x509_name_string(&cert->subject, buf, sizeof(buf));
wpa_printf(MSG_DEBUG, "X509: %lu: %s", idx, buf);
if (chain_trusted)
continue;
if (!disable_time_checks &&
((unsigned long) now.sec <
(unsigned long) cert->not_before ||
(unsigned long) now.sec >
(unsigned long) cert->not_after)) {
wpa_printf(MSG_INFO, "X509: Certificate not valid "
"(now=%lu not_before=%lu not_after=%lu)",
now.sec, cert->not_before, cert->not_after);
*reason = X509_VALIDATE_CERTIFICATE_EXPIRED;
return -1;
}{...}
if (cert->next) {
if (x509_name_compare(&cert->issuer,
&cert->next->subject) != 0) {
wpa_printf(MSG_DEBUG, "X509: Certificate "
"chain issuer name mismatch");
x509_name_string(&cert->issuer, buf,
sizeof(buf));
wpa_printf(MSG_DEBUG, "X509: cert issuer: %s",
buf);
x509_name_string(&cert->next->subject, buf,
sizeof(buf));
wpa_printf(MSG_DEBUG, "X509: next cert "
"subject: %s", buf);
*reason = X509_VALIDATE_CERTIFICATE_UNKNOWN;
return -1;
}{...}
if (x509_valid_issuer(cert->next) < 0) {
*reason = X509_VALIDATE_BAD_CERTIFICATE;
return -1;
}{...}
if ((cert->next->extensions_present &
X509_EXT_PATH_LEN_CONSTRAINT) &&
idx > cert->next->path_len_constraint) {
wpa_printf(MSG_DEBUG, "X509: pathLenConstraint"
" not met (idx=%lu issuer "
"pathLenConstraint=%lu)", idx,
cert->next->path_len_constraint);
*reason = X509_VALIDATE_BAD_CERTIFICATE;
return -1;
}{...}
if (x509_certificate_check_signature(cert->next, cert)
< 0) {
wpa_printf(MSG_DEBUG, "X509: Invalid "
"certificate signature within "
"chain");
*reason = X509_VALIDATE_BAD_CERTIFICATE;
return -1;
}{...}
}{...}
for (trust = trusted; trust; trust = trust->next) {
if (x509_name_compare(&cert->issuer, &trust->subject)
== 0)
break;
}{...}
if (trust) {
wpa_printf(MSG_DEBUG, "X509: Found issuer from the "
"list of trusted certificates");
if (x509_valid_issuer(trust) < 0) {
*reason = X509_VALIDATE_BAD_CERTIFICATE;
return -1;
}{...}
if (x509_certificate_check_signature(trust, cert) < 0)
{
wpa_printf(MSG_DEBUG, "X509: Invalid "
"certificate signature");
*reason = X509_VALIDATE_BAD_CERTIFICATE;
return -1;
}{...}
wpa_printf(MSG_DEBUG, "X509: Trusted certificate "
"found to complete the chain");
cert->issuer_trusted = 1;
chain_trusted = 1;
}{...}
}{...}
if (!chain_trusted) {
wpa_printf(MSG_DEBUG, "X509: Did not find any of the issuers "
"from the list of trusted certificates");
if (trusted) {
*reason = X509_VALIDATE_UNKNOWN_CA;
return -1;
}{...}
wpa_printf(MSG_DEBUG, "X509: Certificate chain validation "
"disabled - ignore unknown CA issue");
}{...}
wpa_printf(MSG_DEBUG, "X509: Certificate chain valid");
return 0;
}{ ... }
/* ... */
struct x509_certificate *
x509_certificate_get_subject(struct x509_certificate *chain,
struct x509_name *name)
{
struct x509_certificate *cert;
for (cert = chain; cert; cert = cert->next) {
if (x509_name_compare(&cert->subject, name) == 0)
return cert;
}{...}
return NULL;
}{ ... }
/* ... */
int x509_certificate_self_signed(struct x509_certificate *cert)
{
return x509_name_compare(&cert->issuer, &cert->subject) == 0;
}{ ... }