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
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
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
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
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
|
/*
* Copyright (C) 2011-2012 Free Software Foundation, Inc.
* Author: Simon Josefsson
*
* This file is part of GnuTLS.
*
* The GnuTLS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
/* Online Certificate Status Protocol - RFC 2560
*/
#include "gnutls_int.h"
#include <global.h>
#include "errors.h"
#include <libtasn1.h>
#include <pk.h>
#include "common.h"
#include "verify-high.h"
#include <gnutls/ocsp.h>
#include <auth/cert.h>
typedef struct gnutls_ocsp_req_int {
ASN1_TYPE req;
unsigned init;
} gnutls_ocsp_req_int;
typedef struct gnutls_ocsp_resp_int {
ASN1_TYPE resp;
gnutls_datum_t response_type_oid;
ASN1_TYPE basicresp;
gnutls_datum_t der;
unsigned init;
} gnutls_ocsp_resp_int;
#define MAX_TIME 64
/**
* gnutls_ocsp_req_init:
* @req: A pointer to the type to be initialized
*
* This function will initialize an OCSP request structure.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error value.
**/
int gnutls_ocsp_req_init(gnutls_ocsp_req_t * req)
{
gnutls_ocsp_req_t tmp =
gnutls_calloc(1, sizeof(gnutls_ocsp_req_int));
int ret;
if (!tmp)
return GNUTLS_E_MEMORY_ERROR;
ret = asn1_create_element(_gnutls_get_pkix(), "PKIX1.OCSPRequest",
&tmp->req);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
gnutls_free(tmp);
return _gnutls_asn2err(ret);
}
*req = tmp;
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_req_deinit:
* @req: The data to be deinitialized
*
* This function will deinitialize a OCSP request structure.
**/
void gnutls_ocsp_req_deinit(gnutls_ocsp_req_t req)
{
if (!req)
return;
if (req->req)
asn1_delete_structure(&req->req);
req->req = NULL;
gnutls_free(req);
}
/**
* gnutls_ocsp_resp_init:
* @resp: A pointer to the type to be initialized
*
* This function will initialize an OCSP response structure.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error value.
**/
int gnutls_ocsp_resp_init(gnutls_ocsp_resp_t * resp)
{
gnutls_ocsp_resp_t tmp =
gnutls_calloc(1, sizeof(gnutls_ocsp_resp_int));
int ret;
if (!tmp)
return GNUTLS_E_MEMORY_ERROR;
ret = asn1_create_element(_gnutls_get_pkix(),
"PKIX1.OCSPResponse", &tmp->resp);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
gnutls_free(tmp);
return _gnutls_asn2err(ret);
}
ret = asn1_create_element(_gnutls_get_pkix(),
"PKIX1.BasicOCSPResponse",
&tmp->basicresp);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
asn1_delete_structure(&tmp->resp);
gnutls_free(tmp);
return _gnutls_asn2err(ret);
}
*resp = tmp;
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_resp_deinit:
* @resp: The data to be deinitialized
*
* This function will deinitialize a OCSP response structure.
**/
void gnutls_ocsp_resp_deinit(gnutls_ocsp_resp_t resp)
{
if (!resp)
return;
if (resp->resp)
asn1_delete_structure(&resp->resp);
gnutls_free(resp->response_type_oid.data);
if (resp->basicresp)
asn1_delete_structure(&resp->basicresp);
resp->resp = NULL;
resp->response_type_oid.data = NULL;
resp->basicresp = NULL;
gnutls_free(resp->der.data);
gnutls_free(resp);
}
/**
* gnutls_ocsp_req_import:
* @req: The data to store the parsed request.
* @data: DER encoded OCSP request.
*
* This function will convert the given DER encoded OCSP request to
* the native #gnutls_ocsp_req_t format. The output will be stored in
* @req.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error value.
**/
int
gnutls_ocsp_req_import(gnutls_ocsp_req_t req, const gnutls_datum_t * data)
{
int ret = 0;
if (req == NULL || data == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
if (req->init) {
/* Any earlier _asn1_strict_der_decode will modify the ASN.1
structure, so we need to replace it with a fresh
structure. */
asn1_delete_structure(&req->req);
ret = asn1_create_element(_gnutls_get_pkix(),
"PKIX1.OCSPRequest", &req->req);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(ret);
}
}
req->init = 1;
ret = _asn1_strict_der_decode(&req->req, data->data, data->size, NULL);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(ret);
}
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_resp_import:
* @resp: The data to store the parsed response.
* @data: DER encoded OCSP response.
*
* This function will convert the given DER encoded OCSP response to
* the native #gnutls_ocsp_resp_t format. It also decodes the Basic
* OCSP Response part, if any. The output will be stored in @resp.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error value.
**/
int
gnutls_ocsp_resp_import(gnutls_ocsp_resp_t resp,
const gnutls_datum_t * data)
{
int ret = 0;
if (resp == NULL || data == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
if (resp->init != 0) {
/* Any earlier _asn1_strict_der_decode will modify the ASN.1
structure, so we need to replace it with a fresh
structure. */
asn1_delete_structure(&resp->resp);
if (resp->basicresp)
asn1_delete_structure(&resp->basicresp);
ret = asn1_create_element(_gnutls_get_pkix(),
"PKIX1.OCSPResponse",
&resp->resp);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(ret);
}
ret = asn1_create_element(_gnutls_get_pkix(),
"PKIX1.BasicOCSPResponse",
&resp->basicresp);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(ret);
}
gnutls_free(resp->der.data);
resp->der.data = NULL;
}
resp->init = 1;
ret = _asn1_strict_der_decode(&resp->resp, data->data, data->size, NULL);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(ret);
}
if (gnutls_ocsp_resp_get_status(resp) !=
GNUTLS_OCSP_RESP_SUCCESSFUL)
return GNUTLS_E_SUCCESS;
ret =
_gnutls_x509_read_value(resp->resp,
"responseBytes.responseType",
&resp->response_type_oid);
if (ret < 0) {
gnutls_assert();
return ret;
}
#define OCSP_BASIC "1.3.6.1.5.5.7.48.1.1"
if (resp->response_type_oid.size == sizeof(OCSP_BASIC)
&& memcmp(resp->response_type_oid.data, OCSP_BASIC,
resp->response_type_oid.size) == 0) {
ret =
_gnutls_x509_read_value(resp->resp,
"responseBytes.response", &resp->der);
if (ret < 0) {
gnutls_assert();
return ret;
}
ret =
_asn1_strict_der_decode(&resp->basicresp, resp->der.data, resp->der.size,
NULL);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(ret);
}
} else {
asn1_delete_structure(&resp->basicresp);
resp->basicresp = NULL;
}
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_req_export:
* @req: Holds the OCSP request
* @data: newly allocate buffer holding DER encoded OCSP request
*
* This function will export the OCSP request to DER format.
*
* Returns: In case of failure a negative error code will be
* returned, and 0 on success.
**/
int gnutls_ocsp_req_export(gnutls_ocsp_req_t req, gnutls_datum_t * data)
{
int ret;
if (req == NULL || data == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
/* XXX remove when we support these fields */
(void)asn1_write_value(req->req, "tbsRequest.requestorName", NULL, 0);
(void)asn1_write_value(req->req, "optionalSignature", NULL, 0);
/* prune extension field if we don't have any extension */
ret = gnutls_ocsp_req_get_extension(req, 0, NULL, NULL, NULL);
if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
(void)asn1_write_value(req->req, "tbsRequest.requestExtensions",
NULL, 0);
return _gnutls_x509_get_raw_field(req->req, "", data);
}
/**
* gnutls_ocsp_resp_export:
* @resp: Holds the OCSP response
* @data: newly allocate buffer holding DER encoded OCSP response
*
* This function will export the OCSP response to DER format.
*
* Returns: In case of failure a negative error code will be
* returned, and 0 on success.
**/
int gnutls_ocsp_resp_export(gnutls_ocsp_resp_t resp, gnutls_datum_t * data)
{
if (resp == NULL || data == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
return _gnutls_x509_get_raw_field(resp->resp, "", data);
}
/**
* gnutls_ocsp_req_get_version:
* @req: should contain a #gnutls_ocsp_req_t type
*
* This function will return the version of the OCSP request.
* Typically this is always 1 indicating version 1.
*
* Returns: version of OCSP request, or a negative error code on error.
**/
int gnutls_ocsp_req_get_version(gnutls_ocsp_req_t req)
{
uint8_t version[8];
int len, ret;
if (req == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
len = sizeof(version);
ret =
asn1_read_value(req->req, "tbsRequest.version", version, &len);
if (ret != ASN1_SUCCESS) {
if (ret == ASN1_ELEMENT_NOT_FOUND)
return 1; /* the DEFAULT version */
gnutls_assert();
return _gnutls_asn2err(ret);
}
return (int) version[0] + 1;
}
/**
* gnutls_ocsp_req_get_cert_id:
* @req: should contain a #gnutls_ocsp_req_t type
* @indx: Specifies which extension OID to get. Use (0) to get the first one.
* @digest: output variable with #gnutls_digest_algorithm_t hash algorithm
* @issuer_name_hash: output buffer with hash of issuer's DN
* @issuer_key_hash: output buffer with hash of issuer's public key
* @serial_number: output buffer with serial number of certificate to check
*
* This function will return the certificate information of the
* @indx'ed request in the OCSP request. The information returned
* corresponds to the CertID structure:
*
* <informalexample><programlisting>
* CertID ::= SEQUENCE {
* hashAlgorithm AlgorithmIdentifier,
* issuerNameHash OCTET STRING, -- Hash of Issuer's DN
* issuerKeyHash OCTET STRING, -- Hash of Issuers public key
* serialNumber CertificateSerialNumber }
* </programlisting></informalexample>
*
* Each of the pointers to output variables may be NULL to indicate
* that the caller is not interested in that value.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned. If you have reached the last
* CertID available %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE will be
* returned.
**/
int
gnutls_ocsp_req_get_cert_id(gnutls_ocsp_req_t req,
unsigned indx,
gnutls_digest_algorithm_t * digest,
gnutls_datum_t * issuer_name_hash,
gnutls_datum_t * issuer_key_hash,
gnutls_datum_t * serial_number)
{
gnutls_datum_t sa;
char name[MAX_NAME_SIZE];
int ret;
if (req == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
snprintf(name, sizeof(name),
"tbsRequest.requestList.?%u.reqCert.hashAlgorithm.algorithm",
indx + 1);
ret = _gnutls_x509_read_value(req->req, name, &sa);
if (ret == GNUTLS_E_ASN1_ELEMENT_NOT_FOUND)
return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
else if (ret < 0) {
gnutls_assert();
return ret;
}
ret = gnutls_oid_to_digest((char *) sa.data);
_gnutls_free_datum(&sa);
if (ret < 0) {
gnutls_assert();
return ret;
}
if (digest)
*digest = ret;
if (issuer_name_hash) {
snprintf(name, sizeof(name),
"tbsRequest.requestList.?%u.reqCert.issuerNameHash",
indx + 1);
ret =
_gnutls_x509_read_value(req->req, name,
issuer_name_hash);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
}
if (issuer_key_hash) {
snprintf(name, sizeof(name),
"tbsRequest.requestList.?%u.reqCert.issuerKeyHash",
indx + 1);
ret =
_gnutls_x509_read_value(req->req, name,
issuer_key_hash);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
if (issuer_name_hash)
gnutls_free(issuer_name_hash->data);
return ret;
}
}
if (serial_number) {
snprintf(name, sizeof(name),
"tbsRequest.requestList.?%u.reqCert.serialNumber",
indx + 1);
ret =
_gnutls_x509_read_value(req->req, name, serial_number);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
if (issuer_name_hash)
gnutls_free(issuer_name_hash->data);
if (issuer_key_hash)
gnutls_free(issuer_key_hash->data);
return ret;
}
}
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_req_add_cert_id:
* @req: should contain a #gnutls_ocsp_req_t type
* @digest: hash algorithm, a #gnutls_digest_algorithm_t value
* @issuer_name_hash: hash of issuer's DN
* @issuer_key_hash: hash of issuer's public key
* @serial_number: serial number of certificate to check
*
* This function will add another request to the OCSP request for a
* particular certificate having the issuer name hash of
* @issuer_name_hash and issuer key hash of @issuer_key_hash (both
* hashed using @digest) and serial number @serial_number.
*
* The information needed corresponds to the CertID structure:
*
* <informalexample><programlisting>
* CertID ::= SEQUENCE {
* hashAlgorithm AlgorithmIdentifier,
* issuerNameHash OCTET STRING, -- Hash of Issuer's DN
* issuerKeyHash OCTET STRING, -- Hash of Issuers public key
* serialNumber CertificateSerialNumber }
* </programlisting></informalexample>
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned.
**/
int
gnutls_ocsp_req_add_cert_id(gnutls_ocsp_req_t req,
gnutls_digest_algorithm_t digest,
const gnutls_datum_t * issuer_name_hash,
const gnutls_datum_t * issuer_key_hash,
const gnutls_datum_t * serial_number)
{
int result;
const char *oid;
if (req == NULL || issuer_name_hash == NULL
|| issuer_key_hash == NULL || serial_number == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
oid = _gnutls_x509_digest_to_oid(hash_to_entry(digest));
if (oid == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
result =
asn1_write_value(req->req, "tbsRequest.requestList", "NEW", 1);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
result = asn1_write_value
(req->req,
"tbsRequest.requestList.?LAST.reqCert.hashAlgorithm.algorithm",
oid, 1);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
/* XXX we don't support any algorithm with parameters */
result = asn1_write_value
(req->req,
"tbsRequest.requestList.?LAST.reqCert.hashAlgorithm.parameters",
ASN1_NULL, ASN1_NULL_SIZE);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
result = asn1_write_value
(req->req,
"tbsRequest.requestList.?LAST.reqCert.issuerNameHash",
issuer_name_hash->data, issuer_name_hash->size);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
result = asn1_write_value
(req->req,
"tbsRequest.requestList.?LAST.reqCert.issuerKeyHash",
issuer_key_hash->data, issuer_key_hash->size);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
result = asn1_write_value
(req->req, "tbsRequest.requestList.?LAST.reqCert.serialNumber",
serial_number->data, serial_number->size);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
/* XXX add separate function that can add extensions too */
result = asn1_write_value
(req->req,
"tbsRequest.requestList.?LAST.singleRequestExtensions", NULL,
0);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_req_add_cert:
* @req: should contain a #gnutls_ocsp_req_t type
* @digest: hash algorithm, a #gnutls_digest_algorithm_t value
* @issuer: issuer of @subject certificate
* @cert: certificate to request status for
*
* This function will add another request to the OCSP request for a
* particular certificate. The issuer name hash, issuer key hash, and
* serial number fields is populated as follows. The issuer name and
* the serial number is taken from @cert. The issuer key is taken
* from @issuer. The hashed values will be hashed using the @digest
* algorithm, normally %GNUTLS_DIG_SHA1.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned.
**/
int
gnutls_ocsp_req_add_cert(gnutls_ocsp_req_t req,
gnutls_digest_algorithm_t digest,
gnutls_x509_crt_t issuer, gnutls_x509_crt_t cert)
{
int ret;
gnutls_datum_t sn, tmp, inh, ikh;
uint8_t inh_buf[MAX_HASH_SIZE];
uint8_t ikh_buf[MAX_HASH_SIZE];
size_t inhlen = MAX_HASH_SIZE;
size_t ikhlen = MAX_HASH_SIZE;
if (req == NULL || issuer == NULL || cert == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
ret = _gnutls_x509_der_encode(cert->cert,
"tbsCertificate.issuer.rdnSequence",
&tmp, 0);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
ret = gnutls_fingerprint(digest, &tmp, inh_buf, &inhlen);
gnutls_free(tmp.data);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
inh.size = inhlen;
inh.data = inh_buf;
ret = _gnutls_x509_read_value
(issuer->cert,
"tbsCertificate.subjectPublicKeyInfo.subjectPublicKey", &tmp);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
ret = gnutls_fingerprint(digest, &tmp, ikh_buf, &ikhlen);
gnutls_free(tmp.data);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
ikh.size = ikhlen;
ikh.data = ikh_buf;
ret =
_gnutls_x509_read_value(cert->cert,
"tbsCertificate.serialNumber", &sn);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
ret = gnutls_ocsp_req_add_cert_id(req, digest, &inh, &ikh, &sn);
gnutls_free(sn.data);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_req_get_extension:
* @req: should contain a #gnutls_ocsp_req_t type
* @indx: Specifies which extension OID to get. Use (0) to get the first one.
* @oid: will hold newly allocated buffer with OID of extension, may be NULL
* @critical: output variable with critical flag, may be NULL.
* @data: will hold newly allocated buffer with extension data, may be NULL
*
* This function will return all information about the requested
* extension in the OCSP request. The information returned is the
* OID, the critical flag, and the data itself. The extension OID
* will be stored as a string. Any of @oid, @critical, and @data may
* be NULL which means that the caller is not interested in getting
* that information back.
*
* The caller needs to deallocate memory by calling gnutls_free() on
* @oid->data and @data->data.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned. If you have reached the last
* extension available %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE will
* be returned.
**/
int
gnutls_ocsp_req_get_extension(gnutls_ocsp_req_t req,
unsigned indx,
gnutls_datum_t * oid,
unsigned int *critical,
gnutls_datum_t * data)
{
int ret;
char str_critical[10];
char name[MAX_NAME_SIZE];
int len;
if (!req) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
snprintf(name, sizeof(name),
"tbsRequest.requestExtensions.?%u.critical", indx + 1);
len = sizeof(str_critical);
ret = asn1_read_value(req->req, name, str_critical, &len);
if (ret == ASN1_ELEMENT_NOT_FOUND)
return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
else if (ret != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(ret);
}
if (critical) {
if (str_critical[0] == 'T')
*critical = 1;
else
*critical = 0;
}
if (oid) {
snprintf(name, sizeof(name),
"tbsRequest.requestExtensions.?%u.extnID",
indx + 1);
ret = _gnutls_x509_read_value(req->req, name, oid);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
}
if (data) {
snprintf(name, sizeof(name),
"tbsRequest.requestExtensions.?%u.extnValue",
indx + 1);
ret = _gnutls_x509_read_value(req->req, name, data);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
if (oid)
gnutls_free(oid->data);
return ret;
}
}
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_req_set_extension:
* @req: should contain a #gnutls_ocsp_req_t type
* @oid: buffer with OID of extension as a string.
* @critical: critical flag, normally false.
* @data: the extension data
*
* This function will add an extension to the OCSP request. Calling
* this function multiple times for the same OID will overwrite values
* from earlier calls.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned.
**/
int
gnutls_ocsp_req_set_extension(gnutls_ocsp_req_t req,
const char *oid,
unsigned int critical,
const gnutls_datum_t * data)
{
if (req == NULL || oid == NULL || data == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
return _gnutls_set_extension(req->req, "tbsRequest.requestExtensions", oid,
data, critical);
}
/**
* gnutls_ocsp_req_get_nonce:
* @req: should contain a #gnutls_ocsp_req_t type
* @critical: whether nonce extension is marked critical, or NULL
* @nonce: will hold newly allocated buffer with nonce data
*
* This function will return the OCSP request nonce extension data.
*
* The caller needs to deallocate memory by calling gnutls_free() on
* @nonce->data.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned.
**/
int
gnutls_ocsp_req_get_nonce(gnutls_ocsp_req_t req,
unsigned int *critical, gnutls_datum_t * nonce)
{
int ret;
gnutls_datum_t tmp;
if (req == NULL || nonce == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
ret = _gnutls_get_extension(req->req, "tbsRequest.requestExtensions",
GNUTLS_OCSP_NONCE, 0, &tmp, critical);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
ret =
_gnutls_x509_decode_string(ASN1_ETYPE_OCTET_STRING, tmp.data,
(size_t) tmp.size, nonce, 0);
if (ret < 0) {
gnutls_assert();
gnutls_free(tmp.data);
return ret;
}
gnutls_free(tmp.data);
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_req_set_nonce:
* @req: should contain a #gnutls_ocsp_req_t type
* @critical: critical flag, normally false.
* @nonce: the nonce data
*
* This function will add an nonce extension to the OCSP request.
* Calling this function multiple times will overwrite values from
* earlier calls.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned.
**/
int
gnutls_ocsp_req_set_nonce(gnutls_ocsp_req_t req,
unsigned int critical,
const gnutls_datum_t * nonce)
{
int ret;
gnutls_datum_t dernonce;
unsigned char temp[SIZEOF_UNSIGNED_LONG_INT + 1];
int len;
if (req == NULL || nonce == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
asn1_length_der(nonce->size, temp, &len);
dernonce.size = 1 + len + nonce->size;
dernonce.data = gnutls_malloc(dernonce.size);
if (dernonce.data == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
dernonce.data[0] = '\x04';
memcpy(dernonce.data + 1, temp, len);
memcpy(dernonce.data + 1 + len, nonce->data, nonce->size);
ret = _gnutls_set_extension(req->req, "tbsRequest.requestExtensions",
GNUTLS_OCSP_NONCE, &dernonce, critical);
gnutls_free(dernonce.data);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
return ret;
}
/**
* gnutls_ocsp_req_randomize_nonce:
* @req: should contain a #gnutls_ocsp_req_t type
*
* This function will add or update an nonce extension to the OCSP
* request with a newly generated random value.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned.
**/
int gnutls_ocsp_req_randomize_nonce(gnutls_ocsp_req_t req)
{
int ret;
uint8_t rndbuf[23];
gnutls_datum_t nonce = { rndbuf, sizeof(rndbuf) };
if (req == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
ret = gnutls_rnd(GNUTLS_RND_NONCE, rndbuf, sizeof(rndbuf));
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
ret = gnutls_ocsp_req_set_nonce(req, 0, &nonce);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_resp_get_status:
* @resp: should contain a #gnutls_ocsp_resp_t type
*
* This function will return the status of a OCSP response, an
* #gnutls_ocsp_resp_status_t enumeration.
*
* Returns: status of OCSP request as a #gnutls_ocsp_resp_status_t, or
* a negative error code on error.
**/
int gnutls_ocsp_resp_get_status(gnutls_ocsp_resp_t resp)
{
uint8_t str[1];
int len, ret;
if (resp == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
len = sizeof(str);
ret = asn1_read_value(resp->resp, "responseStatus", str, &len);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(ret);
}
if (len != 1)
return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET);
switch (str[0]) {
case GNUTLS_OCSP_RESP_SUCCESSFUL:
case GNUTLS_OCSP_RESP_MALFORMEDREQUEST:
case GNUTLS_OCSP_RESP_INTERNALERROR:
case GNUTLS_OCSP_RESP_TRYLATER:
case GNUTLS_OCSP_RESP_SIGREQUIRED:
case GNUTLS_OCSP_RESP_UNAUTHORIZED:
break;
default:
return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET);
}
return (int) str[0];
}
/**
* gnutls_ocsp_resp_get_response:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @response_type_oid: newly allocated output buffer with response type OID
* @response: newly allocated output buffer with DER encoded response
*
* This function will extract the response type OID in and the
* response data from an OCSP response. Normally the
* @response_type_oid is always "1.3.6.1.5.5.7.48.1.1" which means the
* @response should be decoded as a Basic OCSP Response, but
* technically other response types could be used.
*
* This function is typically only useful when you want to extract the
* response type OID of an response for diagnostic purposes.
* Otherwise gnutls_ocsp_resp_import() will decode the basic OCSP
* response part and the caller need not worry about that aspect.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error value.
**/
int
gnutls_ocsp_resp_get_response(gnutls_ocsp_resp_t resp,
gnutls_datum_t * response_type_oid,
gnutls_datum_t * response)
{
int ret;
if (resp == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
if (response_type_oid != NULL) {
ret =
_gnutls_x509_read_value(resp->resp,
"responseBytes.responseType",
response_type_oid);
if (ret < 0) {
gnutls_assert();
return ret;
}
}
if (response != NULL) {
ret =
_gnutls_x509_read_value(resp->resp,
"responseBytes.response",
response);
if (ret < 0) {
gnutls_assert();
return ret;
}
}
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_resp_get_version:
* @resp: should contain a #gnutls_ocsp_resp_t type
*
* This function will return the version of the Basic OCSP Response.
* Typically this is always 1 indicating version 1.
*
* Returns: version of Basic OCSP response, or a negative error code
* on error.
**/
int gnutls_ocsp_resp_get_version(gnutls_ocsp_resp_t resp)
{
uint8_t version[8];
int len, ret;
if (resp == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
len = sizeof(version);
ret =
asn1_read_value(resp->resp, "tbsResponseData.version", version,
&len);
if (ret != ASN1_SUCCESS) {
if (ret == ASN1_ELEMENT_NOT_FOUND)
return 1; /* the DEFAULT version */
gnutls_assert();
return _gnutls_asn2err(ret);
}
return (int) version[0] + 1;
}
/**
* gnutls_ocsp_resp_get_responder:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @dn: newly allocated buffer with name
*
* This function will extract the name of the Basic OCSP Response in
* the provided buffer. The name will be in the form
* "C=xxxx,O=yyyy,CN=zzzz" as described in RFC2253. The output string
* will be ASCII or UTF-8 encoded, depending on the certificate data.
*
* If the responder ID is not a name but a hash, this function
* will return zero and the @dn elements will be set to %NULL.
*
* The caller needs to deallocate memory by calling gnutls_free() on
* @dn->data.
*
* This function does not output a fully RFC4514 compliant string, if
* that is required see gnutls_ocsp_resp_get_responder2().
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned. When no data exist it will
* return success and set @dn elements to zero.
**/
int
gnutls_ocsp_resp_get_responder(gnutls_ocsp_resp_t resp,
gnutls_datum_t * dn)
{
int ret;
ret = gnutls_ocsp_resp_get_responder2(resp, dn, GNUTLS_X509_DN_FLAG_COMPAT);
if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
dn->data = NULL;
dn->size = 0;
return 0; /* for backwards compatibility */
}
return ret;
}
/**
* gnutls_ocsp_resp_get_responder2:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @dn: newly allocated buffer with name
* @flags: zero or %GNUTLS_X509_DN_FLAG_COMPAT
*
* This function will extract the name of the Basic OCSP Response in
* the provided buffer. The name will be in the form
* "C=xxxx,O=yyyy,CN=zzzz" as described in RFC2253. The output string
* will be ASCII or UTF-8 encoded, depending on the certificate data.
*
* If the responder ID is not a name but a hash, this function
* will return zero and the @dn elements will be set to %NULL.
*
* The caller needs to deallocate memory by calling gnutls_free() on
* @dn->data.
*
* When the flag %GNUTLS_X509_DN_FLAG_COMPAT is specified, the output
* format will match the format output by previous to 3.5.6 versions of GnuTLS
* which was not not fully RFC4514-compliant.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned. When no data exist it will return
* %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE.
**/
int
gnutls_ocsp_resp_get_responder2(gnutls_ocsp_resp_t resp,
gnutls_datum_t * dn, unsigned flags)
{
if (resp == NULL || dn == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
dn->data = NULL;
dn->size = 0;
return _gnutls_x509_get_dn(resp->basicresp,
"tbsResponseData.responderID.byName",
dn, flags);
}
/**
* gnutls_ocsp_resp_get_responder_by_key:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @type: should be %GNUTLS_OCSP_RESP_ID_KEY or %GNUTLS_OCSP_RESP_ID_DN
* @raw: newly allocated buffer with the raw ID
*
* This function will extract the raw key (or DN) ID of the Basic OCSP Response in
* the provided buffer. If the responder ID is not a key ID then
* this function will return %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE.
*
* The caller needs to deallocate memory by calling gnutls_free() on
* @dn->data.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned.
**/
int
gnutls_ocsp_resp_get_responder_raw_id(gnutls_ocsp_resp_t resp,
unsigned type,
gnutls_datum_t * raw)
{
int ret;
if (resp == NULL || raw == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
if (type == GNUTLS_OCSP_RESP_ID_KEY)
ret = _gnutls_x509_read_value(resp->basicresp, "tbsResponseData.responderID.byKey", raw);
else {
gnutls_datum_t tmp;
/* simply reading a CHOICE of CHOICE value doesn't work in libtasn1 */
ret = _gnutls_x509_get_raw_field2(resp->basicresp, &resp->der,
"tbsResponseData.responderID.byName",
&tmp);
if (ret >= 0) {
int real;
/* skip the tag */
if (tmp.size < 2) {
gnutls_assert();
ret = GNUTLS_E_ASN1_GENERIC_ERROR;
goto fail;
}
tmp.data++;
tmp.size--;
ret = asn1_get_length_der(tmp.data, tmp.size, &real);
if (ret < 0) {
gnutls_assert();
ret = GNUTLS_E_ASN1_GENERIC_ERROR;
goto fail;
}
if (tmp.size < (unsigned)real) {
gnutls_assert();
ret = GNUTLS_E_ASN1_GENERIC_ERROR;
goto fail;
}
tmp.data+=real;
tmp.size-=real;
ret = _gnutls_set_datum(raw, tmp.data, tmp.size);
}
}
if (ret == GNUTLS_E_ASN1_ELEMENT_NOT_FOUND || ret == GNUTLS_E_ASN1_VALUE_NOT_FOUND)
return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
fail:
return ret;
}
/**
* gnutls_ocsp_resp_get_produced:
* @resp: should contain a #gnutls_ocsp_resp_t type
*
* This function will return the time when the OCSP response was
* signed.
*
* Returns: signing time, or (time_t)-1 on error.
**/
time_t gnutls_ocsp_resp_get_produced(gnutls_ocsp_resp_t resp)
{
char ttime[MAX_TIME];
int len, ret;
time_t c_time;
if (resp == NULL || resp->basicresp == NULL) {
gnutls_assert();
return (time_t) (-1);
}
len = sizeof(ttime) - 1;
ret =
asn1_read_value(resp->basicresp, "tbsResponseData.producedAt",
ttime, &len);
if (ret != ASN1_SUCCESS) {
gnutls_assert();
return (time_t) (-1);
}
c_time = _gnutls_x509_generalTime2gtime(ttime);
return c_time;
}
/**
* gnutls_ocsp_resp_check_crt:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @indx: Specifies response number to get. Use (0) to get the first one.
* @crt: The certificate to check
*
* This function will check whether the OCSP response
* is about the provided certificate.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned.
*
* Since: 3.1.3
**/
int
gnutls_ocsp_resp_check_crt(gnutls_ocsp_resp_t resp,
unsigned int indx, gnutls_x509_crt_t crt)
{
int ret;
gnutls_digest_algorithm_t digest;
gnutls_datum_t rdn_hash = { NULL, 0 }, rserial = {
NULL, 0};
gnutls_datum_t cserial = { NULL, 0 };
gnutls_datum_t dn = { NULL, 0 };
uint8_t cdn_hash[MAX_HASH_SIZE];
size_t t, hash_len;
ret =
gnutls_ocsp_resp_get_single(resp, indx, &digest, &rdn_hash,
NULL, &rserial, NULL, NULL, NULL,
NULL, NULL);
if (ret < 0)
return gnutls_assert_val(ret);
if (rserial.size == 0 || digest == GNUTLS_DIG_UNKNOWN) {
ret = gnutls_assert_val(GNUTLS_E_OCSP_RESPONSE_ERROR);
goto cleanup;
}
hash_len = _gnutls_hash_get_algo_len(hash_to_entry(digest));
if (hash_len != rdn_hash.size) {
ret = gnutls_assert_val(GNUTLS_E_OCSP_RESPONSE_ERROR);
goto cleanup;
}
cserial.size = rserial.size;
cserial.data = gnutls_malloc(cserial.size);
if (cserial.data == NULL) {
ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
goto cleanup;
}
t = cserial.size;
ret = gnutls_x509_crt_get_serial(crt, cserial.data, &t);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
cserial.size = t;
if (rserial.size != cserial.size
|| memcmp(cserial.data, rserial.data, rserial.size) != 0) {
ret = GNUTLS_E_OCSP_RESPONSE_ERROR;
gnutls_assert();
goto cleanup;
}
ret = gnutls_x509_crt_get_raw_issuer_dn(crt, &dn);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = _gnutls_hash_fast(digest, dn.data, dn.size, cdn_hash);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
if (memcmp(cdn_hash, rdn_hash.data, hash_len) != 0) {
ret = GNUTLS_E_OCSP_RESPONSE_ERROR;
gnutls_assert();
goto cleanup;
}
ret = 0;
cleanup:
gnutls_free(rdn_hash.data);
gnutls_free(rserial.data);
gnutls_free(cserial.data);
gnutls_free(dn.data);
return ret;
}
/**
* gnutls_ocsp_resp_get_single:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @indx: Specifies response number to get. Use (0) to get the first one.
* @digest: output variable with #gnutls_digest_algorithm_t hash algorithm
* @issuer_name_hash: output buffer with hash of issuer's DN
* @issuer_key_hash: output buffer with hash of issuer's public key
* @serial_number: output buffer with serial number of certificate to check
* @cert_status: a certificate status, a #gnutls_ocsp_cert_status_t enum.
* @this_update: time at which the status is known to be correct.
* @next_update: when newer information will be available, or (time_t)-1 if unspecified
* @revocation_time: when @cert_status is %GNUTLS_OCSP_CERT_REVOKED, holds time of revocation.
* @revocation_reason: revocation reason, a #gnutls_x509_crl_reason_t enum.
*
* This function will return the certificate information of the
* @indx'ed response in the Basic OCSP Response @resp. The
* information returned corresponds to the OCSP SingleResponse structure
* except the final singleExtensions.
*
* Each of the pointers to output variables may be NULL to indicate
* that the caller is not interested in that value.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned. If you have reached the last
* CertID available %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE will be
* returned.
**/
int
gnutls_ocsp_resp_get_single(gnutls_ocsp_resp_t resp,
unsigned indx,
gnutls_digest_algorithm_t * digest,
gnutls_datum_t * issuer_name_hash,
gnutls_datum_t * issuer_key_hash,
gnutls_datum_t * serial_number,
unsigned int *cert_status,
time_t * this_update,
time_t * next_update,
time_t * revocation_time,
unsigned int *revocation_reason)
{
char name[MAX_NAME_SIZE];
int ret, result;
char oidtmp[MAX_OID_SIZE];
int len;
char ttime[MAX_TIME];
/* initialize any allocated values to NULL, to allow deallocation
* on error. */
if (issuer_name_hash)
issuer_name_hash->data = NULL;
if (issuer_key_hash)
issuer_key_hash->data = NULL;
if (serial_number)
serial_number->data = NULL;
if (digest) {
snprintf(name, sizeof(name),
"tbsResponseData.responses.?%u.certID.hashAlgorithm.algorithm",
indx + 1);
len = sizeof(oidtmp);
result = asn1_read_value(resp->basicresp, name, oidtmp, &len);
if (result == ASN1_ELEMENT_NOT_FOUND) {
return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
} else if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
ret = gnutls_oid_to_digest(oidtmp);
if (ret < 0) {
gnutls_assert();
return ret;
}
*digest = ret;
}
if (issuer_name_hash) {
snprintf(name, sizeof(name),
"tbsResponseData.responses.?%u.certID.issuerNameHash",
indx + 1);
ret = _gnutls_x509_read_value(resp->basicresp, name,
issuer_name_hash);
if (ret < 0) {
gnutls_assert();
return ret;
}
}
if (issuer_key_hash) {
snprintf(name, sizeof(name),
"tbsResponseData.responses.?%u.certID.issuerKeyHash",
indx + 1);
ret = _gnutls_x509_read_value(resp->basicresp, name,
issuer_key_hash);
if (ret < 0) {
gnutls_assert();
goto fail;
}
}
if (serial_number) {
snprintf(name, sizeof(name),
"tbsResponseData.responses.?%u.certID.serialNumber",
indx + 1);
ret = _gnutls_x509_read_value(resp->basicresp, name,
serial_number);
if (ret < 0) {
gnutls_assert();
goto fail;
}
}
if (cert_status) {
snprintf(name, sizeof(name),
"tbsResponseData.responses.?%u.certStatus",
indx + 1);
len = sizeof(oidtmp);
result = asn1_read_value(resp->basicresp, name, oidtmp, &len);
if (result == ASN1_ELEMENT_NOT_FOUND) {
gnutls_assert();
ret = GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
goto fail;
} else if (result != ASN1_SUCCESS) {
gnutls_assert();
ret = _gnutls_asn2err(result);
goto fail;
}
if (len == 5 && memcmp(oidtmp, "good", len) == 0)
*cert_status = GNUTLS_OCSP_CERT_GOOD;
else if (len == 8
&& memcmp(oidtmp, "revoked", len) == 0)
*cert_status = GNUTLS_OCSP_CERT_REVOKED;
else if (len == 8
&& memcmp(oidtmp, "unknown", len) == 0)
*cert_status = GNUTLS_OCSP_CERT_UNKNOWN;
else {
gnutls_assert();
ret = GNUTLS_E_ASN1_DER_ERROR;
goto fail;
}
}
if (this_update) {
snprintf(name, sizeof(name),
"tbsResponseData.responses.?%u.thisUpdate",
indx + 1);
len = sizeof(ttime) - 1;
result = asn1_read_value(resp->basicresp, name, ttime, &len);
if (result != ASN1_SUCCESS) {
gnutls_assert();
ret = GNUTLS_E_ASN1_DER_ERROR;
goto fail;
} else {
*this_update =
_gnutls_x509_generalTime2gtime(ttime);
}
}
if (next_update) {
snprintf(name, sizeof(name),
"tbsResponseData.responses.?%u.nextUpdate",
indx + 1);
len = sizeof(ttime) - 1;
result = asn1_read_value(resp->basicresp, name, ttime, &len);
if (result != ASN1_SUCCESS) {
gnutls_assert();
*next_update = (time_t) (-1);
} else
*next_update =
_gnutls_x509_generalTime2gtime(ttime);
}
if (revocation_time) {
snprintf(name, sizeof(name),
"tbsResponseData.responses.?%u.certStatus."
"revoked.revocationTime", indx + 1);
len = sizeof(ttime) - 1;
result = asn1_read_value(resp->basicresp, name, ttime, &len);
if (result != ASN1_SUCCESS) {
gnutls_assert();
*revocation_time = (time_t) (-1);
} else
*revocation_time =
_gnutls_x509_generalTime2gtime(ttime);
}
/* revocation_reason */
if (revocation_reason) {
snprintf(name, sizeof(name),
"tbsResponseData.responses.?%u.certStatus."
"revoked.revocationReason", indx + 1);
ret = _gnutls_x509_read_uint(resp->basicresp, name,
revocation_reason);
if (ret < 0)
*revocation_reason =
GNUTLS_X509_CRLREASON_UNSPECIFIED;
}
return GNUTLS_E_SUCCESS;
fail:
if (issuer_name_hash) {
gnutls_free(issuer_name_hash->data);
issuer_name_hash->data = NULL;
}
if (issuer_key_hash) {
gnutls_free(issuer_key_hash->data);
issuer_key_hash->data = NULL;
}
if (serial_number) {
gnutls_free(serial_number->data);
serial_number->data = NULL;
}
return ret;
}
/**
* gnutls_ocsp_resp_get_extension:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @indx: Specifies which extension OID to get. Use (0) to get the first one.
* @oid: will hold newly allocated buffer with OID of extension, may be NULL
* @critical: output variable with critical flag, may be NULL.
* @data: will hold newly allocated buffer with extension data, may be NULL
*
* This function will return all information about the requested
* extension in the OCSP response. The information returned is the
* OID, the critical flag, and the data itself. The extension OID
* will be stored as a string. Any of @oid, @critical, and @data may
* be NULL which means that the caller is not interested in getting
* that information back.
*
* The caller needs to deallocate memory by calling gnutls_free() on
* @oid->data and @data->data.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned. If you have reached the last
* extension available %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE will
* be returned.
**/
int
gnutls_ocsp_resp_get_extension(gnutls_ocsp_resp_t resp,
unsigned indx,
gnutls_datum_t * oid,
unsigned int *critical,
gnutls_datum_t * data)
{
int ret;
char str_critical[10];
char name[MAX_NAME_SIZE];
int len;
if (!resp) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
snprintf(name, sizeof(name),
"tbsResponseData.responseExtensions.?%u.critical",
indx + 1);
len = sizeof(str_critical);
ret = asn1_read_value(resp->basicresp, name, str_critical, &len);
if (ret == ASN1_ELEMENT_NOT_FOUND)
return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
else if (ret != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(ret);
}
if (critical) {
if (str_critical[0] == 'T')
*critical = 1;
else
*critical = 0;
}
if (oid) {
snprintf(name, sizeof(name),
"tbsResponseData.responseExtensions.?%u.extnID",
indx + 1);
ret = _gnutls_x509_read_value(resp->basicresp, name, oid);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
}
if (data) {
snprintf(name, sizeof(name),
"tbsResponseData.responseExtensions.?%u.extnValue",
indx + 1);
ret = _gnutls_x509_read_value(resp->basicresp, name, data);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
if (oid)
gnutls_free(oid->data);
return ret;
}
}
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_resp_get_nonce:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @critical: whether nonce extension is marked critical
* @nonce: will hold newly allocated buffer with nonce data
*
* This function will return the Basic OCSP Response nonce extension
* data.
*
* The caller needs to deallocate memory by calling gnutls_free() on
* @nonce->data.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error code is returned.
**/
int
gnutls_ocsp_resp_get_nonce(gnutls_ocsp_resp_t resp,
unsigned int *critical, gnutls_datum_t * nonce)
{
int ret;
gnutls_datum_t tmp;
ret =
_gnutls_get_extension(resp->basicresp,
"tbsResponseData.responseExtensions",
GNUTLS_OCSP_NONCE, 0, &tmp, critical);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
ret =
_gnutls_x509_decode_string(ASN1_ETYPE_OCTET_STRING, tmp.data,
(size_t) tmp.size, nonce, 0);
if (ret < 0) {
gnutls_assert();
gnutls_free(tmp.data);
return ret;
}
gnutls_free(tmp.data);
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_resp_get_signature_algorithm:
* @resp: should contain a #gnutls_ocsp_resp_t type
*
* This function will return a value of the #gnutls_sign_algorithm_t
* enumeration that is the signature algorithm that has been used to
* sign the OCSP response.
*
* Returns: a #gnutls_sign_algorithm_t value, or a negative error code
* on error.
**/
int gnutls_ocsp_resp_get_signature_algorithm(gnutls_ocsp_resp_t resp)
{
int ret;
gnutls_datum_t sa;
ret = _gnutls_x509_read_value(resp->basicresp,
"signatureAlgorithm.algorithm", &sa);
if (ret < 0) {
gnutls_assert();
return ret;
}
ret = gnutls_oid_to_sign((char *) sa.data);
_gnutls_free_datum(&sa);
return ret;
}
/**
* gnutls_ocsp_resp_get_signature:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @sig: newly allocated output buffer with signature data
*
* This function will extract the signature field of a OCSP response.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error value.
**/
int
gnutls_ocsp_resp_get_signature(gnutls_ocsp_resp_t resp,
gnutls_datum_t * sig)
{
int ret;
if (resp == NULL || sig == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
ret = _gnutls_x509_read_value(resp->basicresp, "signature", sig);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
return ret;
}
return GNUTLS_E_SUCCESS;
}
/**
* gnutls_ocsp_resp_get_certs:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @certs: newly allocated array with #gnutls_x509_crt_t certificates
* @ncerts: output variable with number of allocated certs.
*
* This function will extract the X.509 certificates found in the
* Basic OCSP Response. The @certs output variable will hold a newly
* allocated zero-terminated array with X.509 certificates.
*
* Every certificate in the array needs to be de-allocated with
* gnutls_x509_crt_deinit() and the array itself must be freed using
* gnutls_free().
*
* Both the @certs and @ncerts variables may be NULL. Then the
* function will work as normal but will not return the NULL:d
* information. This can be used to get the number of certificates
* only, or to just get the certificate array without its size.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error value.
**/
int
gnutls_ocsp_resp_get_certs(gnutls_ocsp_resp_t resp,
gnutls_x509_crt_t ** certs, size_t * ncerts)
{
int ret;
size_t ctr = 0, i;
gnutls_x509_crt_t *tmpcerts = NULL, *tmpcerts2;
gnutls_datum_t c = { NULL, 0 };
if (resp == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
tmpcerts = gnutls_malloc(sizeof(*tmpcerts));
if (tmpcerts == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
for (;;) {
char name[MAX_NAME_SIZE];
snprintf(name, sizeof(name), "certs.?%u",
(unsigned int) (ctr + 1));
ret =
_gnutls_x509_der_encode(resp->basicresp, name, &c, 0);
if (ret == GNUTLS_E_ASN1_ELEMENT_NOT_FOUND)
break;
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
goto error;
}
tmpcerts2 =
gnutls_realloc_fast(tmpcerts,
(ctr + 2) * sizeof(*tmpcerts));
if (tmpcerts2 == NULL) {
gnutls_assert();
ret = GNUTLS_E_MEMORY_ERROR;
goto error;
}
tmpcerts = tmpcerts2;
ret = gnutls_x509_crt_init(&tmpcerts[ctr]);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
goto error;
}
ctr++;
ret = gnutls_x509_crt_import(tmpcerts[ctr - 1], &c,
GNUTLS_X509_FMT_DER);
if (ret != GNUTLS_E_SUCCESS) {
gnutls_assert();
goto error;
}
gnutls_free(c.data);
c.data = NULL;
}
tmpcerts[ctr] = NULL;
if (ncerts)
*ncerts = ctr;
if (certs)
*certs = tmpcerts;
else {
/* clean up memory */
ret = GNUTLS_E_SUCCESS;
goto error;
}
return GNUTLS_E_SUCCESS;
error:
gnutls_free(c.data);
for (i = 0; i < ctr; i++)
gnutls_x509_crt_deinit(tmpcerts[i]);
gnutls_free(tmpcerts);
return ret;
}
/* Search the OCSP response for a certificate matching the responderId
mentioned in the OCSP response. */
static gnutls_x509_crt_t find_signercert(gnutls_ocsp_resp_t resp)
{
int rc;
gnutls_x509_crt_t *certs = NULL;
size_t ncerts = 0, i;
gnutls_datum_t riddn = {NULL, 0};
gnutls_datum_t keyid = {NULL, 0};
gnutls_x509_crt_t signercert = NULL;
rc = gnutls_ocsp_resp_get_responder_raw_id(resp, GNUTLS_OCSP_RESP_ID_DN, &riddn);
if (rc == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
gnutls_assert();
rc = gnutls_ocsp_resp_get_responder_raw_id(resp, GNUTLS_OCSP_RESP_ID_KEY, &keyid);
}
if (rc != GNUTLS_E_SUCCESS) {
gnutls_assert();
return NULL;
}
rc = gnutls_ocsp_resp_get_certs(resp, &certs, &ncerts);
if (rc != GNUTLS_E_SUCCESS) {
gnutls_assert();
signercert = NULL;
goto quit;
}
for (i = 0; i < ncerts; i++) {
if (keyid.data != NULL) {
uint8_t digest[20];
gnutls_datum_t spki;
rc = _gnutls_x509_get_raw_field2(certs[i]->cert, &certs[i]->der,
"tbsCertificate.subjectPublicKeyInfo.subjectPublicKey",
&spki);
if (rc < 0 || spki.size < 6) {
signercert = NULL;
goto quit;
}
/* For some reason the protocol requires we skip the
* tag, length and number of unused bits.
*/
spki.data += 5;
spki.size -= 5;
rc = gnutls_hash_fast(GNUTLS_DIG_SHA1, spki.data, spki.size, digest);
if (rc < 0) {
gnutls_assert();
signercert = NULL;
goto quit;
}
if ((20 == keyid.size) &&
memcmp(keyid.data, digest, 20) == 0) {
signercert = certs[i];
goto quit;
}
} else {
assert(riddn.data != NULL);
if ((certs[i]->raw_dn.size == riddn.size)
&& memcmp(riddn.data, certs[i]->raw_dn.data, riddn.size) == 0) {
signercert = certs[i];
goto quit;
}
}
}
gnutls_assert();
signercert = NULL;
quit:
gnutls_free(riddn.data);
gnutls_free(keyid.data);
for (i = 0; i < ncerts; i++)
if (certs[i] != signercert)
gnutls_x509_crt_deinit(certs[i]);
gnutls_free(certs);
return signercert;
}
static int
_ocsp_resp_verify_direct(gnutls_ocsp_resp_t resp,
gnutls_x509_crt_t signercert,
unsigned int *verify, unsigned int flags)
{
gnutls_datum_t sig = { NULL };
gnutls_datum_t data = { NULL };
gnutls_pubkey_t pubkey = NULL;
int sigalg;
int rc;
if (resp == NULL || signercert == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
rc = gnutls_ocsp_resp_get_signature_algorithm(resp);
if (rc < 0) {
gnutls_assert();
goto done;
}
sigalg = rc;
rc = _gnutls_x509_get_raw_field2(resp->basicresp, &resp->der, "tbsResponseData", &data);
if (rc != GNUTLS_E_SUCCESS) {
gnutls_assert();
goto done;
}
rc = gnutls_pubkey_init(&pubkey);
if (rc != GNUTLS_E_SUCCESS) {
gnutls_assert();
goto done;
}
rc = gnutls_pubkey_import_x509(pubkey, signercert, 0);
if (rc != GNUTLS_E_SUCCESS) {
gnutls_assert();
goto done;
}
rc = gnutls_ocsp_resp_get_signature(resp, &sig);
if (rc != GNUTLS_E_SUCCESS) {
gnutls_assert();
goto done;
}
rc = gnutls_pubkey_verify_data2(pubkey, sigalg, flags, &data, &sig);
if (rc == GNUTLS_E_PK_SIG_VERIFY_FAILED) {
gnutls_assert();
*verify = GNUTLS_OCSP_VERIFY_SIGNATURE_FAILURE;
} else if (rc < 0) {
gnutls_assert();
goto done;
} else
*verify = 0;
rc = GNUTLS_E_SUCCESS;
done:
gnutls_free(sig.data);
gnutls_pubkey_deinit(pubkey);
return rc;
}
static inline unsigned int vstatus_to_ocsp_status(unsigned int status)
{
unsigned int ostatus;
if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
ostatus = GNUTLS_OCSP_VERIFY_INSECURE_ALGORITHM;
else if (status & GNUTLS_CERT_NOT_ACTIVATED)
ostatus = GNUTLS_OCSP_VERIFY_CERT_NOT_ACTIVATED;
else if (status & GNUTLS_CERT_EXPIRED)
ostatus = GNUTLS_OCSP_VERIFY_CERT_EXPIRED;
else
ostatus = GNUTLS_OCSP_VERIFY_UNTRUSTED_SIGNER;
return ostatus;
}
static int check_ocsp_purpose(gnutls_x509_crt_t signercert)
{
char oidtmp[MAX_OID_SIZE];
size_t oidsize;
int indx, rc;
for (indx = 0;; indx++) {
oidsize = sizeof(oidtmp);
rc = gnutls_x509_crt_get_key_purpose_oid(signercert, indx,
oidtmp, &oidsize,
NULL);
if (rc == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
gnutls_assert();
return rc;
} else if (rc == GNUTLS_E_SHORT_MEMORY_BUFFER) {
gnutls_assert();
continue;
} else if (rc != GNUTLS_E_SUCCESS) {
return gnutls_assert_val(rc);
}
if (memcmp(oidtmp, GNUTLS_KP_OCSP_SIGNING, oidsize) != 0) {
gnutls_assert();
continue;
}
break;
}
return 0;
}
/**
* gnutls_ocsp_resp_verify_direct:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @issuer: certificate believed to have signed the response
* @verify: output variable with verification status, an #gnutls_ocsp_verify_reason_t
* @flags: verification flags from #gnutls_certificate_verify_flags
*
* Verify signature of the Basic OCSP Response against the public key
* in the @issuer certificate.
*
* The output @verify variable will hold verification status codes
* (e.g., %GNUTLS_OCSP_VERIFY_SIGNER_NOT_FOUND,
* %GNUTLS_OCSP_VERIFY_INSECURE_ALGORITHM) which are only valid if the
* function returned %GNUTLS_E_SUCCESS.
*
* Note that the function returns %GNUTLS_E_SUCCESS even when
* verification failed. The caller must always inspect the @verify
* variable to find out the verification status.
*
* The @flags variable should be 0 for now.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error value.
**/
int
gnutls_ocsp_resp_verify_direct(gnutls_ocsp_resp_t resp,
gnutls_x509_crt_t issuer,
unsigned int *verify, unsigned int flags)
{
gnutls_x509_crt_t signercert;
int rc;
if (resp == NULL || issuer == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
signercert = find_signercert(resp);
if (!signercert) {
signercert = issuer;
} else if (!gnutls_x509_crt_equals(signercert, issuer)) {
/* response contains a signer. Verify him */
unsigned int vtmp;
rc = gnutls_x509_crt_verify(signercert, &issuer, 1, flags,
&vtmp);
if (rc != GNUTLS_E_SUCCESS) {
gnutls_assert();
goto done;
}
if (vtmp != 0) {
*verify = vstatus_to_ocsp_status(vtmp);
gnutls_assert();
rc = GNUTLS_E_SUCCESS;
goto done;
}
rc = check_ocsp_purpose(signercert);
if (rc < 0) {
gnutls_assert();
*verify = GNUTLS_OCSP_VERIFY_SIGNER_KEYUSAGE_ERROR;
rc = GNUTLS_E_SUCCESS;
goto done;
}
}
rc = _ocsp_resp_verify_direct(resp, signercert, verify, flags);
done:
if (signercert != issuer)
gnutls_x509_crt_deinit(signercert);
return rc;
}
/**
* gnutls_ocsp_resp_verify:
* @resp: should contain a #gnutls_ocsp_resp_t type
* @trustlist: trust anchors as a #gnutls_x509_trust_list_t type
* @verify: output variable with verification status, an #gnutls_ocsp_verify_reason_t
* @flags: verification flags from #gnutls_certificate_verify_flags
*
* Verify signature of the Basic OCSP Response against the public key
* in the certificate of a trusted signer. The @trustlist should be
* populated with trust anchors. The function will extract the signer
* certificate from the Basic OCSP Response and will verify it against
* the @trustlist. A trusted signer is a certificate that is either
* in @trustlist, or it is signed directly by a certificate in
* @trustlist and has the id-ad-ocspSigning Extended Key Usage bit
* set.
*
* The output @verify variable will hold verification status codes
* (e.g., %GNUTLS_OCSP_VERIFY_SIGNER_NOT_FOUND,
* %GNUTLS_OCSP_VERIFY_INSECURE_ALGORITHM) which are only valid if the
* function returned %GNUTLS_E_SUCCESS.
*
* Note that the function returns %GNUTLS_E_SUCCESS even when
* verification failed. The caller must always inspect the @verify
* variable to find out the verification status.
*
* The @flags variable should be 0 for now.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
* negative error value.
**/
int
gnutls_ocsp_resp_verify(gnutls_ocsp_resp_t resp,
gnutls_x509_trust_list_t trustlist,
unsigned int *verify, unsigned int flags)
{
gnutls_x509_crt_t signercert = NULL;
int rc;
/* Algorithm:
1. Find signer cert.
1a. Search in OCSP response Certificate field for responderID.
1b. Verify that signer cert is trusted.
2a. It is in trustlist?
2b. It has OCSP key usage and directly signed by a CA in trustlist?
3. Verify signature of Basic Response using public key from signer cert.
*/
signercert = find_signercert(resp);
if (!signercert) {
gnutls_datum_t dn;
rc = gnutls_ocsp_resp_get_responder_raw_id(resp, GNUTLS_OCSP_RESP_ID_DN, &dn);
if (rc < 0) {
gnutls_assert();
*verify = GNUTLS_OCSP_VERIFY_SIGNER_NOT_FOUND;
rc = GNUTLS_E_SUCCESS;
goto done;
}
rc = gnutls_x509_trust_list_get_issuer_by_dn(trustlist, &dn, &signercert, 0);
gnutls_free(dn.data);
if (rc < 0) {
gnutls_assert();
*verify = GNUTLS_OCSP_VERIFY_SIGNER_NOT_FOUND;
rc = GNUTLS_E_SUCCESS;
goto done;
}
} else {
/* Either the signer is directly trusted (i.e., in trustlist) or it
is directly signed by something in trustlist and has proper OCSP
extkeyusage. */
rc = _gnutls_trustlist_inlist(trustlist, signercert);
if (rc == 0) {
/* not in trustlist, need to verify signature and bits */
unsigned vtmp;
gnutls_typed_vdata_st vdata;
vdata.type = GNUTLS_DT_KEY_PURPOSE_OID;
vdata.data = (void*)GNUTLS_KP_OCSP_SIGNING;
vdata.size = 0;
gnutls_assert();
rc = gnutls_x509_trust_list_verify_crt2(trustlist,
&signercert, 1,
&vdata, 1,
flags, &vtmp, NULL);
if (rc != GNUTLS_E_SUCCESS) {
gnutls_assert();
goto done;
}
if (vtmp != 0) {
*verify = vstatus_to_ocsp_status(vtmp);
gnutls_assert();
rc = GNUTLS_E_SUCCESS;
goto done;
}
}
}
rc = check_ocsp_purpose(signercert);
if (rc < 0) {
gnutls_assert();
*verify = GNUTLS_OCSP_VERIFY_SIGNER_KEYUSAGE_ERROR;
rc = GNUTLS_E_SUCCESS;
goto done;
}
rc = _ocsp_resp_verify_direct(resp, signercert, verify, flags);
done:
gnutls_x509_crt_deinit(signercert);
return rc;
}
|