synta 0.2.2

ASN.1 parser, decoder, and encoder library with DER/BER support and C FFI
Documentation
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
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
#ifndef SYNTA_H
#define SYNTA_H

/* Warning: This file is automatically generated by cbindgen. Do not modify manually. */

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

/**
 * FFI error codes (C-compatible)
 */
typedef enum SyntaErrorCode {
    /**
     * Operation succeeded
     */
    SyntaErrorCode_Success = 0,
    /**
     * Invalid tag encoding
     */
    SyntaErrorCode_InvalidTag = 1,
    /**
     * Invalid length encoding
     */
    SyntaErrorCode_InvalidLength = 2,
    /**
     * Unexpected end of input
     */
    SyntaErrorCode_UnexpectedEof = 3,
    /**
     * Invalid encoding for the current mode
     */
    SyntaErrorCode_InvalidEncoding = 4,
    /**
     * Invalid integer encoding
     */
    SyntaErrorCode_InvalidInteger = 5,
    /**
     * Invalid OID encoding
     */
    SyntaErrorCode_InvalidOid = 6,
    /**
     * Invalid UTF-8 string
     */
    SyntaErrorCode_InvalidUtf8 = 7,
    /**
     * Invalid PrintableString characters
     */
    SyntaErrorCode_InvalidPrintableString = 8,
    /**
     * Invalid IA5String characters
     */
    SyntaErrorCode_InvalidIA5String = 9,
    /**
     * Invalid boolean encoding
     */
    SyntaErrorCode_InvalidBoolean = 10,
    /**
     * Invalid time format
     */
    SyntaErrorCode_InvalidTime = 11,
    /**
     * Invalid bit string encoding
     */
    SyntaErrorCode_InvalidBitString = 12,
    /**
     * Maximum nesting depth exceeded
     */
    SyntaErrorCode_MaxDepthExceeded = 13,
    /**
     * Maximum sequence elements exceeded
     */
    SyntaErrorCode_MaxSequenceElementsExceeded = 14,
    /**
     * Maximum length exceeded
     */
    SyntaErrorCode_MaxLengthExceeded = 15,
    /**
     * BER-specific violation (indefinite length in DER, etc.)
     */
    SyntaErrorCode_BerViolation = 16,
    /**
     * DER-specific violation
     */
    SyntaErrorCode_DerViolation = 17,
    /**
     * CER-specific violation
     */
    SyntaErrorCode_CerViolation = 18,
    /**
     * NULL pointer passed as argument
     */
    SyntaErrorCode_NullPointer = 19,
    /**
     * Invalid argument
     */
    SyntaErrorCode_InvalidArgument = 20,
    /**
     * Memory allocation failed
     */
    SyntaErrorCode_OutOfMemory = 21,
    /**
     * Integer overflow
     */
    SyntaErrorCode_IntegerOverflow = 22,
    /**
     * Integer underflow
     */
    SyntaErrorCode_IntegerUnderflow = 23,
    /**
     * Unknown or unhandled error
     */
    SyntaErrorCode_Unknown = 999,
} SyntaErrorCode;

/**
 * ASN.1 encoding rules
 */
typedef enum SyntaEncoding {
    /**
     * Distinguished Encoding Rules (deterministic)
     */
    SyntaEncoding_Der = 0,
    /**
     * Basic Encoding Rules (flexible)
     */
    SyntaEncoding_Ber = 1,
    /**
     * Canonical Encoding Rules (streaming)
     */
    SyntaEncoding_Cer = 2,
} SyntaEncoding;

/**
 * ASN.1 tag class
 */
typedef enum SyntaTagClass {
    /**
     * Universal class (0)
     */
    SyntaTagClass_Universal = 0,
    /**
     * Application class (1)
     */
    SyntaTagClass_Application = 1,
    /**
     * Context-specific class (2)
     */
    SyntaTagClass_ContextSpecific = 2,
    /**
     * Private class (3)
     */
    SyntaTagClass_Private = 3,
} SyntaTagClass;

/**
 * Opaque handle to a Certificate
 */
typedef struct SyntaCertificate {
    uint8_t _private[0];
} SyntaCertificate;

/**
 * C-compatible byte array with ownership tracking
 *
 * Layout (16 bytes on 64-bit, no tail padding):
 *   offset  0: data  (*const u8,  8 bytes)
 *   offset  8: len   (u32,        4 bytes)
 *   offset 12: owned (u32,        4 bytes)  0 = borrowed, 1 = owned
 */
typedef struct SyntaByteArray {
    /**
     * Pointer to the data (may be null if len is 0)
     */
    const uint8_t *data;
    /**
     * Length of the data in bytes (max ~4 GiB; sufficient for any ASN.1 element)
     */
    uint32_t len;
    /**
     * Ownership flag: 1 = caller must free with `synta_byte_array_free`, 0 = borrowed
     */
    uint32_t owned;
} SyntaByteArray;

/**
 * Opaque handle to a parsed CMS `SignedData`.
 *
 * Obtained as a borrowed pointer via [`synta_cms_content_info_get0_signed_data`].
 * Must **not** be freed; ownership belongs to the parent `SyntaCmsContentInfo`.
 * All getter pointers remain valid until the parent handle is freed.
 */
typedef struct SyntaCmsSignedData {
    uint8_t _private[0];
} SyntaCmsSignedData;

/**
 * Opaque handle to a parsed CMS `SignerInfo`.
 *
 * Obtain with [`synta_cms_signer_info_parse_der`] (directly) or by passing
 * the bytes from [`synta_cms_signed_data_get_signer_info_der`] to it.
 * Release with [`synta_cms_signer_info_free`].
 */
typedef struct SyntaCmsSignerInfo {
    uint8_t _private[0];
} SyntaCmsSignerInfo;

/**
 * Opaque handle to a parsed CMS `EnvelopedData`.
 *
 * Obtained as a borrowed pointer via [`synta_cms_content_info_get0_enveloped_data`].
 * Must **not** be freed; ownership belongs to the parent `SyntaCmsContentInfo`.
 */
typedef struct SyntaCmsEnvelopedData {
    uint8_t _private[0];
} SyntaCmsEnvelopedData;

/**
 * Opaque handle to a parsed CMS `EncryptedData`.
 *
 * Obtained as a borrowed pointer via [`synta_cms_content_info_get0_encrypted_data`].
 * Must **not** be freed; ownership belongs to the parent `SyntaCmsContentInfo`.
 */
typedef struct SyntaCmsEncryptedData {
    uint8_t _private[0];
} SyntaCmsEncryptedData;

/**
 * Opaque handle to a parsed CMS `DigestedData`.
 *
 * Obtained as a borrowed pointer via [`synta_cms_content_info_get0_digested_data`].
 * Must **not** be freed; ownership belongs to the parent `SyntaCmsContentInfo`.
 */
typedef struct SyntaCmsDigestedData {
    uint8_t _private[0];
} SyntaCmsDigestedData;

/**
 * Opaque handle to a parsed CMS `ContentInfo`.
 *
 * Obtain with [`synta_cms_content_info_parse_der`]; release with
 * [`synta_cms_content_info_free`].
 *
 * ## `get0_*` pointer ownership
 *
 * Pointers returned by `synta_cms_content_info_get0_*` functions are
 * **borrowed from this handle**.  They are valid only while the
 * `SyntaCmsContentInfo` is alive and must **not** be passed to the
 * corresponding `synta_cms_*_free` function.  Free only the
 * `SyntaCmsContentInfo` itself with `synta_cms_content_info_free`.
 *
 * This follows the OpenSSL `get0_` convention for borrowed internal pointers,
 * distinguishable from owned (`get1_`) pointers by the naming convention.
 */
typedef struct SyntaCmsContentInfo {
    uint8_t _private[0];
} SyntaCmsContentInfo;

/**
 * Opaque handle to a parsed X.509 CRL.
 */
typedef struct SyntaCrl {
    uint8_t _private[0];
} SyntaCrl;

/**
 * Opaque handle to a parsed PKCS#10 CSR.
 */
typedef struct SyntaCsr {
    uint8_t _private[0];
} SyntaCsr;

/**
 * Opaque handle to a Decoder
 */
typedef struct SyntaDecoder {
    uint8_t _private[0];
} SyntaDecoder;

/**
 * Decoder configuration (C-compatible).
 *
 * All three limits are `size_t` (`usize`) on both sides of the FFI boundary,
 * keeping the struct layout consistent across 32-bit and 64-bit platforms.
 */
typedef struct SyntaDecoderConfig {
    /**
     * Maximum ASN.1 nesting depth (number of nested SEQUENCE/SET levels).
     */
    uintptr_t max_depth;
    /**
     * Maximum number of elements in a SEQUENCE or SET.
     */
    uintptr_t max_sequence_elements;
    /**
     * Maximum length of any single primitive element, in bytes.
     */
    uintptr_t max_length;
} SyntaDecoderConfig;

/**
 * C-compatible ASN.1 tag
 *
 * **C compatibility note:** `constructed` is mapped to `bool` by cbindgen,
 * which requires `<stdbool.h>` in C99.  The generated header includes a
 * `cpp_compat` guard so C++ consumers see `bool` without the header.
 * C89 callers should treat the field as a one-byte integer (0 = false,
 * non-zero = true).
 */
typedef struct SyntaTag {
    /**
     * Tag class
     */
    enum SyntaTagClass class_;
    /**
     * Whether the tag is constructed (true) or primitive (false).
     * Requires `<stdbool.h>` in C99; treat as `uint8_t` in C89.
     */
    bool constructed;
    /**
     * Tag number
     */
    unsigned int number;
} SyntaTag;

/**
 * Opaque handle to an Integer
 */
typedef struct SyntaInteger {
    uint8_t _private[0];
} SyntaInteger;

/**
 * Opaque handle to an OctetString
 */
typedef struct SyntaOctetString {
    uint8_t _private[0];
} SyntaOctetString;

/**
 * Opaque handle to an ObjectIdentifier
 */
typedef struct SyntaObjectIdentifier {
    uint8_t _private[0];
} SyntaObjectIdentifier;

/**
 * Opaque handle to an Encoder
 */
typedef struct SyntaEncoder {
    uint8_t _private[0];
} SyntaEncoder;

/**
 * Opaque handle to a parsed OCSP response.
 */
typedef struct SyntaOcsp {
    uint8_t _private[0];
} SyntaOcsp;

/**
 * Opaque handle to an ordered list of DER buffers.
 *
 * Created by [`synta_pem_to_der`], [`synta_pkcs7_certs_from_der`], or
 * [`synta_pkcs12_certs_from_der`].  Must be freed with [`synta_der_list_free`].
 */
typedef struct SyntaDerList {
    uint8_t _private[0];
} SyntaDerList;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

/**
 * Parse an X.509 certificate from DER bytes
 *
 * # Safety
 *
 * - `data` must be a valid pointer to DER-encoded certificate bytes
 * - The returned certificate must be freed with `synta_certificate_free`
 *
 * # Returns
 *
 * Returns a pointer to the certificate, or NULL on error.
 */
struct SyntaCertificate *synta_certificate_parse_der(const uint8_t *data, uintptr_t len);

/**
 * Free a certificate
 *
 * # Safety
 *
 * - `cert` must be a valid pointer returned from `synta_certificate_parse_der`
 * - This function must only be called once for each certificate
 */
void synta_certificate_free(struct SyntaCertificate *cert);

/**
 * Get certificate version (v1=0, v2=1, v3=2)
 *
 * # Safety
 *
 * - `cert` must be a valid pointer to a certificate
 * - `out` must be a valid pointer
 *
 * # Returns
 *
 * Returns Success if version is present, or error if not present (defaults to v1=0)
 */
enum SyntaErrorCode synta_certificate_get_version(const struct SyntaCertificate *cert,
                                                  int64_t *out);

/**
 * Get certificate serial number
 *
 * # Safety
 *
 * - `cert` must be a valid pointer to a certificate
 * - `out` must be a valid pointer
 *
 * # Returns
 *
 * Returns the serial number bytes as a borrowed slice.
 * The data is valid while `cert` is alive; do not free with `synta_byte_array_free`.
 */
enum SyntaErrorCode synta_certificate_get_serial_number(const struct SyntaCertificate *cert,
                                                        struct SyntaByteArray *out);

/**
 * Get the certificate signature algorithm OID as a dotted-decimal string.
 *
 * # Return value
 *
 * | Condition                          | Return value            |
 * |------------------------------------|-------------------------|
 * | `cert` is null                     | `0`                     |
 * | `buffer` is null or `buffer_len` 0 | required size (incl. `\0`) |
 * | `buffer_len` too small             | required size (incl. `\0`) |
 * | success                            | bytes written, excl. `\0` |
 *
 * When the buffer is too small the return value is the minimum `buffer_len`
 * needed for a subsequent successful call (size-query / retry pattern):
 *
 * ```c
 * size_t needed = synta_certificate_get_signature_algorithm(cert, NULL, 0);
 * char *buf = malloc(needed);
 * synta_certificate_get_signature_algorithm(cert, buf, needed);
 * ```
 *
 * The OID string is computed at most once per certificate and cached;
 * repeated calls incur no formatting cost.
 *
 * # Safety
 *
 * - `cert` must be a valid pointer to a certificate, or `NULL`.
 * - If `buffer` is non-null it must point to a writable buffer of at least
 *   `buffer_len` bytes.
 */
uintptr_t synta_certificate_get_signature_algorithm(const struct SyntaCertificate *cert,
                                                    char *buffer,
                                                    uintptr_t buffer_len);

/**
 * Get certificate signature algorithm AlgorithmIdentifier (full raw DER)
 *
 * Returns the complete DER encoding of the inner AlgorithmIdentifier SEQUENCE,
 * including the algorithm OID and any parameters (e.g. NULL for RSA,
 * named curve OID for EC public keys in SubjectPublicKeyInfo).
 *
 * # Safety
 *
 * - `cert` must be a valid pointer to a certificate
 * - `out` must be a valid pointer
 *
 * # Returns
 *
 * Returns a borrowed slice of the pre-computed AlgorithmIdentifier DER bytes.
 * The data is valid while `cert` is alive; do not free with `synta_byte_array_free`.
 */
enum SyntaErrorCode synta_certificate_get_signature_algorithm_der(const struct SyntaCertificate *cert,
                                                                  struct SyntaByteArray *out);

/**
 * Get outer certificate signature algorithm AlgorithmIdentifier (full raw DER)
 *
 * Returns the complete DER encoding of the outer AlgorithmIdentifier SEQUENCE.
 * Must match the inner TBS copy for a well-formed certificate.
 *
 * # Safety
 *
 * - `cert` must be a valid pointer to a certificate
 * - `out` must be a valid pointer
 *
 * # Returns
 *
 * Returns a borrowed slice of the pre-computed outer AlgorithmIdentifier DER bytes.
 * The data is valid while `cert` is alive; do not free with `synta_byte_array_free`.
 */
enum SyntaErrorCode synta_certificate_get_outer_signature_algorithm_der(const struct SyntaCertificate *cert,
                                                                        struct SyntaByteArray *out);

/**
 * Get certificate issuer DN (raw DER bytes)
 *
 * # Safety
 *
 * - `cert` must be a valid pointer to a certificate
 * - `out` must be a valid pointer
 *
 * # Returns
 *
 * Returns a zero-copy borrowed slice into the original DER buffer.
 * The data is valid while `cert` is alive; do not free with `synta_byte_array_free`.
 */
enum SyntaErrorCode synta_certificate_get_issuer_der(const struct SyntaCertificate *cert,
                                                     struct SyntaByteArray *out);

/**
 * Get certificate subject DN (raw DER bytes)
 *
 * # Safety
 *
 * - `cert` must be a valid pointer to a certificate
 * - `out` must be a valid pointer
 *
 * # Returns
 *
 * Returns a zero-copy borrowed slice into the original DER buffer.
 * The data is valid while `cert` is alive; do not free with `synta_byte_array_free`.
 */
enum SyntaErrorCode synta_certificate_get_subject_der(const struct SyntaCertificate *cert,
                                                      struct SyntaByteArray *out);

/**
 * Get the `CMSVersion` integer for `SignedData`.
 *
 * RFC 5652 §5.1 defines the version as:
 *
 * - `1` — all signers use `v1` `SignerInfo` and no `AttributeCertificateV2`
 *   entries appear in `certificates`
 * - `3` — at least one signer uses `v3` `SignerInfo` (subjectKeyIdentifier),
 *   or an `AttributeCertificateV2` appears in `certificates`
 *
 * # Safety
 *
 * - `sd` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signed_data_get_version(const struct SyntaCmsSignedData *sd,
                                                      int64_t *out);

/**
 * Get the encapsulated content type OID as a NUL-terminated dotted-decimal string.
 *
 * Returns the number of bytes written (excluding the NUL terminator) on
 * success, or the required buffer size (including NUL) when `buffer` is null
 * or too small.  Returns 0 when `sd` is null.
 *
 * # Safety
 *
 * - `sd` must be a valid non-null pointer.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_cms_signed_data_get_encap_content_type(const struct SyntaCmsSignedData *sd,
                                                       char *buffer,
                                                       uintptr_t buffer_len);

/**
 * Get the encapsulated content bytes (the `eContent` OCTET STRING value).
 *
 * The returned slice points directly into the parsed buffer; it is valid
 * while `sd` is alive.  Returns zero-length output when `eContent` is absent.
 *
 * # Safety
 *
 * - `sd` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signed_data_get_encap_content(const struct SyntaCmsSignedData *sd,
                                                            struct SyntaByteArray *out);

/**
 * Get the raw content bytes of the `certificates` field (`[0] IMPLICIT`).
 *
 * These are the **value** bytes captured during IMPLICIT decoding (no
 * context-tag prefix).  Returns zero-length output when the field is absent.
 *
 * # Safety
 *
 * - `sd` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signed_data_get_certificates_der(const struct SyntaCmsSignedData *sd,
                                                               struct SyntaByteArray *out);

/**
 * Get the raw content bytes of the `crls` field (`[1] IMPLICIT`).
 *
 * Returns zero-length output when the field is absent.
 *
 * # Safety
 *
 * - `sd` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signed_data_get_crls_der(const struct SyntaCmsSignedData *sd,
                                                       struct SyntaByteArray *out);

/**
 * Get the number of `SignerInfo` elements.
 *
 * # Safety
 *
 * - `sd` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signed_data_get_signer_info_count(const struct SyntaCmsSignedData *sd,
                                                                uintptr_t *out);

/**
 * Get the DER encoding of the `SignerInfo` at position `index`.
 *
 * Returns a borrowed [`SyntaByteArray`] pointing into a pre-encoded buffer;
 * the slice is valid while `sd` is alive.  The bytes are a complete,
 * self-contained `SignerInfo` SEQUENCE that can be passed directly to
 * [`synta_cms_signer_info_parse_der`] for field-level access.
 *
 * Returns [`SyntaErrorCode::InvalidArgument`] when `index` is out of bounds.
 *
 * # Safety
 *
 * - `sd` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signed_data_get_signer_info_der(const struct SyntaCmsSignedData *sd,
                                                              uintptr_t index,
                                                              struct SyntaByteArray *out);

/**
 * Parse a BER/DER-encoded CMS `SignerInfo` SEQUENCE.
 *
 * The typical workflow is to obtain the per-element DER bytes via
 * [`synta_cms_signed_data_get_signer_info_der`] and pass them here, rather
 * than constructing a standalone `SignerInfo` buffer from scratch.
 *
 * # Safety
 *
 * - `data` must be a valid pointer to at least `len` readable bytes.
 * - The returned handle must be freed with [`synta_cms_signer_info_free`].
 *
 * Returns `NULL` on parse error; call `synta_get_last_error_message()` for
 * details.
 */
struct SyntaCmsSignerInfo *synta_cms_signer_info_parse_der(const uint8_t *data, uintptr_t len);

/**
 * Free a `SyntaCmsSignerInfo` handle.
 *
 * # Safety
 *
 * - `si` must be a valid pointer returned from [`synta_cms_signer_info_parse_der`].
 * - Must be called at most once.
 */
void synta_cms_signer_info_free(struct SyntaCmsSignerInfo *si);

/**
 * Get the `CMSVersion` integer for `SignerInfo`.
 *
 * RFC 5652 §5.3 defines the version as:
 *
 * - `1` — `sid` contains an `IssuerAndSerialNumber` (SEQUENCE, tag `0x30`)
 * - `3` — `sid` contains a `SubjectKeyIdentifier` (`[0] IMPLICIT` OCTET STRING)
 *
 * The version determines how to interpret the bytes returned by
 * [`synta_cms_signer_info_get_sid_der`].
 *
 * # Safety
 *
 * - `si` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signer_info_get_version(const struct SyntaCmsSignerInfo *si,
                                                      int64_t *out);

/**
 * Get the raw content bytes of the `sid` (SignerIdentifier) field.
 *
 * `SignerIdentifier` is a CHOICE between `IssuerAndSerialNumber` (version 1)
 * and `SubjectKeyIdentifier` (version 3, `[0] IMPLICIT` OCTET STRING).  The
 * field is stored as `RawDer`; the returned bytes are the **content** bytes
 * captured during decoding — without any outer context-tag prefix.
 *
 * Callers should check the version first to decide how to interpret the bytes:
 *
 * - version `1`: bytes are a raw `IssuerAndSerialNumber` SEQUENCE (tag `0x30`)
 * - version `3`: bytes are a raw `OCTET STRING` value (key identifier)
 *
 * The slice is valid while `si` is alive; do not free with
 * `synta_byte_array_free`.
 *
 * # Safety
 *
 * - `si` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signer_info_get_sid_der(const struct SyntaCmsSignerInfo *si,
                                                      struct SyntaByteArray *out);

/**
 * Get the full DER encoding of the `digestAlgorithm` AlgorithmIdentifier.
 *
 * Returns a borrowed slice into a pre-encoded buffer; valid while `si` is alive.
 *
 * # Safety
 *
 * - `si` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signer_info_get_digest_algorithm_der(const struct SyntaCmsSignerInfo *si,
                                                                   struct SyntaByteArray *out);

/**
 * Get the `digestAlgorithm` OID as a NUL-terminated dotted-decimal string.
 *
 * Uses the size-query / retry pattern.  Returns 0 when `si` is null.
 *
 * # Safety
 *
 * - `si` must be a valid non-null pointer.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_cms_signer_info_get_digest_algorithm_oid(const struct SyntaCmsSignerInfo *si,
                                                         char *buffer,
                                                         uintptr_t buffer_len);

/**
 * Get the raw content bytes of the `signedAttrs` field (`[0] IMPLICIT`).
 *
 * These are the value bytes captured during IMPLICIT decoding (no context-tag
 * prefix).  Returns zero-length output when absent.
 *
 * # Safety
 *
 * - `si` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signer_info_get_signed_attrs_der(const struct SyntaCmsSignerInfo *si,
                                                               struct SyntaByteArray *out);

/**
 * Get the full DER encoding of the `signatureAlgorithm` AlgorithmIdentifier.
 *
 * # Safety
 *
 * - `si` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signer_info_get_signature_algorithm_der(const struct SyntaCmsSignerInfo *si,
                                                                      struct SyntaByteArray *out);

/**
 * Get the `signatureAlgorithm` OID as a NUL-terminated dotted-decimal string.
 *
 * Uses the size-query / retry pattern.  Returns 0 when `si` is null.
 *
 * # Safety
 *
 * - `si` must be a valid non-null pointer.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_cms_signer_info_get_signature_algorithm_oid(const struct SyntaCmsSignerInfo *si,
                                                            char *buffer,
                                                            uintptr_t buffer_len);

/**
 * Get the raw bytes of the `signature` OCTET STRING value.
 *
 * # Safety
 *
 * - `si` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signer_info_get_signature(const struct SyntaCmsSignerInfo *si,
                                                        struct SyntaByteArray *out);

/**
 * Get the raw content bytes of the `unsignedAttrs` field (`[1] IMPLICIT`).
 *
 * Returns zero-length output when absent.
 *
 * # Safety
 *
 * - `si` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_signer_info_get_unsigned_attrs_der(const struct SyntaCmsSignerInfo *si,
                                                                 struct SyntaByteArray *out);

/**
 * Get the `CMSVersion` integer for `EnvelopedData`.
 *
 * RFC 5652 §6.1 defines the version based on the recipient info types
 * present:
 *
 * - `0` — all recipients use `KeyTransRecipientInfo` v0, no originator info
 * - `2` — `KeyAgreeRecipientInfo` or `KEKRecipientInfo` present, no originator info
 * - `3` — `PasswordRecipientInfo` or `OtherRecipientInfo` present, or originator info present
 * - `4` — `KeyTransRecipientInfo` v2 present
 *
 * # Safety
 *
 * - `ed` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_enveloped_data_get_version(const struct SyntaCmsEnvelopedData *ed,
                                                         int64_t *out);

/**
 * Get the raw content bytes of the `recipientInfos` SET OF.
 *
 * `recipientInfos` is stored as `RawDer`; the returned slice is the captured
 * content bytes.  Valid while `ed` is alive.
 *
 * # Safety
 *
 * - `ed` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_enveloped_data_get_recipient_infos_der(const struct SyntaCmsEnvelopedData *ed,
                                                                     struct SyntaByteArray *out);

/**
 * Get the encrypted content type OID as a NUL-terminated dotted-decimal string.
 *
 * Returns 0 when `ed` is null.
 *
 * # Safety
 *
 * - `ed` must be a valid non-null pointer.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_cms_enveloped_data_get_content_type(const struct SyntaCmsEnvelopedData *ed,
                                                    char *buffer,
                                                    uintptr_t buffer_len);

/**
 * Get the full DER of the `contentEncryptionAlgorithm` AlgorithmIdentifier.
 *
 * # Safety
 *
 * - `ed` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_enveloped_data_get_content_encryption_algorithm_der(const struct SyntaCmsEnvelopedData *ed,
                                                                                  struct SyntaByteArray *out);

/**
 * Get the `contentEncryptionAlgorithm` OID as a NUL-terminated dotted-decimal string.
 *
 * Returns 0 when `ed` is null.
 *
 * # Safety
 *
 * - `ed` must be a valid non-null pointer.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_cms_enveloped_data_get_content_encryption_algorithm_oid(const struct SyntaCmsEnvelopedData *ed,
                                                                        char *buffer,
                                                                        uintptr_t buffer_len);

/**
 * Get the encrypted content bytes (the `encryptedContent` `[0] IMPLICIT` value).
 *
 * Returns zero-length output when absent.
 *
 * # Safety
 *
 * - `ed` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_enveloped_data_get_encrypted_content(const struct SyntaCmsEnvelopedData *ed,
                                                                   struct SyntaByteArray *out);

/**
 * Get the `CMSVersion` integer for `EncryptedData`.
 *
 * RFC 5652 §8 defines:
 *
 * - `0` — no unprotected attributes or attributes use only v0 types
 * - `2` — unprotected attributes include an `AttributeCertificateV2`
 *
 * # Safety
 *
 * - `ed` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_encrypted_data_get_version(const struct SyntaCmsEncryptedData *ed,
                                                         int64_t *out);

/**
 * Get the encrypted content type OID as a NUL-terminated dotted-decimal string.
 *
 * Returns 0 when `ed` is null.
 *
 * # Safety
 *
 * - `ed` must be a valid non-null pointer.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_cms_encrypted_data_get_content_type(const struct SyntaCmsEncryptedData *ed,
                                                    char *buffer,
                                                    uintptr_t buffer_len);

/**
 * Get the full DER of the `contentEncryptionAlgorithm` AlgorithmIdentifier.
 *
 * # Safety
 *
 * - `ed` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_encrypted_data_get_content_encryption_algorithm_der(const struct SyntaCmsEncryptedData *ed,
                                                                                  struct SyntaByteArray *out);

/**
 * Get the `contentEncryptionAlgorithm` OID as a NUL-terminated dotted-decimal string.
 *
 * Returns 0 when `ed` is null.
 *
 * # Safety
 *
 * - `ed` must be a valid non-null pointer.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_cms_encrypted_data_get_content_encryption_algorithm_oid(const struct SyntaCmsEncryptedData *ed,
                                                                        char *buffer,
                                                                        uintptr_t buffer_len);

/**
 * Get the encrypted content bytes (the `encryptedContent` `[0] IMPLICIT` value).
 *
 * Returns zero-length output when absent.
 *
 * # Safety
 *
 * - `ed` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_encrypted_data_get_encrypted_content(const struct SyntaCmsEncryptedData *ed,
                                                                   struct SyntaByteArray *out);

/**
 * Decrypt the `encryptedContent` of a CMS `EncryptedData` using the provided
 * symmetric key.
 *
 * The content encryption algorithm and its parameters (IV etc.) are read from
 * the `AlgorithmIdentifier` embedded in `ed`.  Decryption uses the
 * `synta_certificate::CmsDecryptor` trait internally:
 *
 * - When `synta-ffi` is built **with** the `openssl` feature, the
 *   OpenSSL-backed implementation is used and supports `id-aes128-cbc`,
 *   `id-aes192-cbc`, and `id-aes256-cbc`.
 * - Without the `openssl` feature, this function always returns an error.
 *
 * On success, `*plaintext_out` is set to the decrypted plaintext and must be
 * freed with [`synta_byte_array_free`].
 *
 * # Safety
 *
 * - `ed` must be a valid non-null pointer obtained from
 *   [`synta_cms_content_info_get0_encrypted_data`].
 * - `key` must be a valid pointer to at least `key_len` bytes.
 * - `plaintext_out` must be a valid non-null pointer.
 */
enum SyntaErrorCode synta_cms_encrypted_data_decrypt(const struct SyntaCmsEncryptedData *ed,
                                                     const uint8_t *key,
                                                     uintptr_t key_len,
                                                     struct SyntaByteArray *plaintext_out);

/**
 * Encrypt `plaintext` and assemble a DER-encoded CMS `EncryptedData` SEQUENCE.
 *
 * - `content_type_oid_der`: DER-encoded OID TLV (`06 xx ...`) for the
 *   content type (e.g. id-data).  Pass `NULL` to use the default `id-data`
 *   (1.2.840.113549.1.7.1).
 * - `enc_alg_oid_der`: DER-encoded OID TLV for the content-encryption
 *   algorithm (e.g. `id-aes128-cbc`).  Must not be `NULL`.
 * - `key` / `key_len`: raw symmetric key bytes.
 * - `plaintext` / `plaintext_len`: bytes to encrypt.
 * - `der_out`: receives an owned [`SyntaByteArray`] containing the
 *   complete `EncryptedData` DER on success.  Must be freed with
 *   [`synta_byte_array_free`].
 *
 * Requires the `openssl` feature; returns [`SyntaErrorCode::Unknown`] with a
 * descriptive error message otherwise.
 *
 * # Safety
 *
 * - `enc_alg_oid_der`, `key`, `plaintext`, and `der_out` must be valid
 *   non-null pointers.
 * - `content_type_oid_der` may be null to select the `id-data` default.
 */
enum SyntaErrorCode synta_cms_encrypted_data_create(const uint8_t *plaintext,
                                                    uintptr_t plaintext_len,
                                                    const uint8_t *key,
                                                    uintptr_t key_len,
                                                    const uint8_t *content_type_oid_der,
                                                    uintptr_t content_type_oid_der_len,
                                                    const uint8_t *enc_alg_oid_der,
                                                    uintptr_t enc_alg_oid_der_len,
                                                    struct SyntaByteArray *der_out);

/**
 * Get the `CMSVersion` integer for `DigestedData`.
 *
 * RFC 5652 §7 defines:
 *
 * - `0` — `eContentType` is `id-data`
 * - `2` — `eContentType` is any other OID
 *
 * # Safety
 *
 * - `dd` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_digested_data_get_version(const struct SyntaCmsDigestedData *dd,
                                                        int64_t *out);

/**
 * Get the full DER of the `digestAlgorithm` AlgorithmIdentifier.
 *
 * # Safety
 *
 * - `dd` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_digested_data_get_digest_algorithm_der(const struct SyntaCmsDigestedData *dd,
                                                                     struct SyntaByteArray *out);

/**
 * Get the `digestAlgorithm` OID as a NUL-terminated dotted-decimal string.
 *
 * Returns 0 when `dd` is null.
 *
 * # Safety
 *
 * - `dd` must be a valid non-null pointer.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_cms_digested_data_get_digest_algorithm_oid(const struct SyntaCmsDigestedData *dd,
                                                           char *buffer,
                                                           uintptr_t buffer_len);

/**
 * Get the encapsulated content type OID as a NUL-terminated dotted-decimal string.
 *
 * Returns 0 when `dd` is null.
 *
 * # Safety
 *
 * - `dd` must be a valid non-null pointer.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_cms_digested_data_get_encap_content_type(const struct SyntaCmsDigestedData *dd,
                                                         char *buffer,
                                                         uintptr_t buffer_len);

/**
 * Get the encapsulated content bytes (the `eContent` OCTET STRING value).
 *
 * Returns zero-length output when absent.
 *
 * # Safety
 *
 * - `dd` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_digested_data_get_encap_content(const struct SyntaCmsDigestedData *dd,
                                                              struct SyntaByteArray *out);

/**
 * Get the raw bytes of the `digest` OCTET STRING value.
 *
 * # Safety
 *
 * - `dd` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_cms_digested_data_get_digest(const struct SyntaCmsDigestedData *dd,
                                                       struct SyntaByteArray *out);

/**
 * Parse a BER/DER-encoded CMS `ContentInfo` SEQUENCE.
 *
 * Unlike the type-specific parse functions, this function accepts the **full
 * `ContentInfo`** (outer `SEQUENCE`, `contentType` OID, and `[0] EXPLICIT`
 * wrapper) and dispatches automatically to the appropriate inner-type parser.
 * This is the Synta equivalent of OpenSSL's `d2i_CMS_ContentInfo`.
 *
 * | Content type OID           | Name                | Accessor                                     |
 * |----------------------------|---------------------|----------------------------------------------|
 * | `1.2.840.113549.1.7.2`     | `id-signedData`     | `synta_cms_content_info_get0_signed_data`    |
 * | `1.2.840.113549.1.7.3`     | `id-envelopedData`  | `synta_cms_content_info_get0_enveloped_data` |
 * | `1.2.840.113549.1.7.6`     | `id-encryptedData`  | `synta_cms_content_info_get0_encrypted_data` |
 * | `1.2.840.113549.1.7.5`     | `id-digestedData`   | `synta_cms_content_info_get0_digested_data`  |
 *
 * Other content types parse successfully — `synta_cms_content_info_get_content_type`
 * still returns the OID, but all `get0_*` accessors return `NULL`.
 *
 * # Safety
 *
 * - `data` must be a valid pointer to at least `len` readable bytes.
 * - The returned handle must be freed with [`synta_cms_content_info_free`].
 *
 * Returns `NULL` on parse error; call `synta_get_last_error_message()` for
 * the error detail.
 */
struct SyntaCmsContentInfo *synta_cms_content_info_parse_der(const uint8_t *data,
                                                             uintptr_t len);

/**
 * Free a `SyntaCmsContentInfo` handle and all inner content.
 *
 * Any `*const Synta*` pointers previously returned by `get0_*` functions on
 * this handle become invalid after this call.
 *
 * # Safety
 *
 * - `ci` must be a valid pointer returned from
 *   [`synta_cms_content_info_parse_der`], or `NULL` (no-op).
 * - Do **not** free borrowed `get0_*` pointers separately; free only `ci`.
 */
void synta_cms_content_info_free(struct SyntaCmsContentInfo *ci);

/**
 * Get the outer `contentType` OID as a NUL-terminated dotted-decimal string.
 *
 * Returns the `contentType` of the `ContentInfo` envelope — e.g.
 * `1.2.840.113549.1.7.2` for `id-signedData` — not the encapsulated content
 * type inside a `SignedData`.  This is the Synta equivalent of OpenSSL's
 * `CMS_get0_type`.
 *
 * Uses the size-query / retry pattern:
 * - Returns `bytes_written` (excluding NUL) on success.
 * - Returns `len + 1` (required buffer size including NUL) when `buffer` is
 *   `NULL` or `buffer_len` is too small.
 * - Returns `0` when `ci` is `NULL`.
 *
 * # Safety
 *
 * - `ci` must be a valid non-null pointer.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_cms_content_info_get_content_type(const struct SyntaCmsContentInfo *ci,
                                                  char *buffer,
                                                  uintptr_t buffer_len);

/**
 * Return a **borrowed** pointer to the inner `SignedData`, or `NULL`.
 *
 * Returns `NULL` if the `contentType` is not `id-signedData`
 * (`1.2.840.113549.1.7.2`).
 *
 * The returned `*const SyntaCmsSignedData` may be passed to all
 * `synta_cms_signed_data_get_*` accessor functions.  Do **not** call
 * `synta_cms_signed_data_free` on it — the memory is owned by the
 * `SyntaCmsContentInfo` handle and freed by `synta_cms_content_info_free`.
 *
 * This follows the OpenSSL `get0_` convention: the pointer is borrowed from
 * the parent handle (`*const`, not `*mut`) and must not be independently freed.
 *
 * # Safety
 *
 * - `ci` must be a valid non-null pointer.
 * - The returned pointer is valid only while `ci` is alive.
 */
const struct SyntaCmsSignedData *synta_cms_content_info_get0_signed_data(const struct SyntaCmsContentInfo *ci);

/**
 * Return a **borrowed** pointer to the inner `EnvelopedData`, or `NULL`.
 *
 * Returns `NULL` if the `contentType` is not `id-envelopedData`
 * (`1.2.840.113549.1.7.3`).
 *
 * Do **not** call `synta_cms_enveloped_data_free` on the returned pointer.
 * See [`synta_cms_content_info_get0_signed_data`] for full ownership semantics.
 *
 * # Safety
 *
 * - `ci` must be a valid non-null pointer.
 * - The returned pointer is valid only while `ci` is alive.
 */
const struct SyntaCmsEnvelopedData *synta_cms_content_info_get0_enveloped_data(const struct SyntaCmsContentInfo *ci);

/**
 * Return a **borrowed** pointer to the inner `EncryptedData`, or `NULL`.
 *
 * Returns `NULL` if the `contentType` is not `id-encryptedData`
 * (`1.2.840.113549.1.7.6`).
 *
 * Do **not** call `synta_cms_encrypted_data_free` on the returned pointer.
 * See [`synta_cms_content_info_get0_signed_data`] for full ownership semantics.
 *
 * # Safety
 *
 * - `ci` must be a valid non-null pointer.
 * - The returned pointer is valid only while `ci` is alive.
 */
const struct SyntaCmsEncryptedData *synta_cms_content_info_get0_encrypted_data(const struct SyntaCmsContentInfo *ci);

/**
 * Return a **borrowed** pointer to the inner `DigestedData`, or `NULL`.
 *
 * Returns `NULL` if the `contentType` is not `id-digestedData`
 * (`1.2.840.113549.1.7.5`).
 *
 * Do **not** call `synta_cms_digested_data_free` on the returned pointer.
 * See [`synta_cms_content_info_get0_signed_data`] for full ownership semantics.
 *
 * # Safety
 *
 * - `ci` must be a valid non-null pointer.
 * - The returned pointer is valid only while `ci` is alive.
 */
const struct SyntaCmsDigestedData *synta_cms_content_info_get0_digested_data(const struct SyntaCmsContentInfo *ci);

/**
 * Parse an X.509 CRL from DER bytes.
 *
 * Returns NULL on parse error; free with [`synta_crl_free`].
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` bytes.
 */
struct SyntaCrl *synta_crl_parse_der(const uint8_t *data, uintptr_t len);

/**
 * Parse an X.509 CRL from PEM bytes (first block only).
 *
 * Returns NULL on error; free with [`synta_crl_free`].
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` bytes.
 */
struct SyntaCrl *synta_crl_parse_pem(const uint8_t *data, uintptr_t len);

/**
 * Free a CRL returned by a `synta_crl_parse_*` function.
 *
 * # Safety
 *
 * - `crl` must be a valid pointer, or NULL.  Must be called exactly once.
 */
void synta_crl_free(struct SyntaCrl *crl);

/**
 * Get the CRL issuer distinguished name as raw DER bytes.
 *
 * Returns a borrowed slice valid while `crl` is alive.
 *
 * # Safety
 *
 * - `crl` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_crl_get_issuer_der(const struct SyntaCrl *crl,
                                             struct SyntaByteArray *out);

/**
 * Get the CRL signature algorithm AlgorithmIdentifier as raw DER bytes.
 *
 * Returns a borrowed slice valid while `crl` is alive.
 *
 * # Safety
 *
 * - `crl` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_crl_get_signature_algorithm_der(const struct SyntaCrl *crl,
                                                          struct SyntaByteArray *out);

/**
 * Get the CRL signature value as raw DER bytes (BIT STRING with tag).
 *
 * Returns a borrowed slice valid while `crl` is alive.
 *
 * # Safety
 *
 * - `crl` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_crl_get_signature_value_der(const struct SyntaCrl *crl,
                                                      struct SyntaByteArray *out);

/**
 * Get the CRL thisUpdate time as raw DER bytes.
 *
 * Returns a borrowed slice valid while `crl` is alive.
 *
 * # Safety
 *
 * - `crl` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_crl_get_this_update_der(const struct SyntaCrl *crl,
                                                  struct SyntaByteArray *out);

/**
 * Get the CRL nextUpdate time as raw DER bytes.
 *
 * Returns a borrowed slice valid while `crl` is alive, or a zero-length
 * array (`out->len == 0`) if nextUpdate is absent.
 *
 * # Safety
 *
 * - `crl` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_crl_get_next_update_der(const struct SyntaCrl *crl,
                                                  struct SyntaByteArray *out);

/**
 * Get the number of revoked certificate entries in the CRL.
 *
 * Returns 0 for an absent or empty `revokedCertificates` sequence.
 *
 * # Safety
 *
 * - `crl` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_crl_get_revoked_count(const struct SyntaCrl *crl, uintptr_t *out);

/**
 * Parse a PKCS#10 CSR from DER bytes.
 *
 * Returns NULL on parse error; free with [`synta_csr_free`].
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` bytes.
 */
struct SyntaCsr *synta_csr_parse_der(const uint8_t *data, uintptr_t len);

/**
 * Parse a PKCS#10 CSR from PEM bytes (first block only).
 *
 * Returns NULL on error; free with [`synta_csr_free`].
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` bytes.
 */
struct SyntaCsr *synta_csr_parse_pem(const uint8_t *data, uintptr_t len);

/**
 * Free a CSR returned by a `synta_csr_parse_*` function.
 *
 * # Safety
 *
 * - `csr` must be a valid pointer, or NULL.  Must be called exactly once.
 */
void synta_csr_free(struct SyntaCsr *csr);

/**
 * Get the CSR version (v1 = 0).
 *
 * # Safety
 *
 * - `csr` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_csr_get_version(const struct SyntaCsr *csr, int64_t *out);

/**
 * Get the CSR subject distinguished name as raw DER bytes.
 *
 * Returns a borrowed slice valid while `csr` is alive.
 *
 * # Safety
 *
 * - `csr` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_csr_get_subject_der(const struct SyntaCsr *csr,
                                              struct SyntaByteArray *out);

/**
 * Get the CSR signature algorithm AlgorithmIdentifier as raw DER bytes.
 *
 * Returns a borrowed slice valid while `csr` is alive.
 *
 * # Safety
 *
 * - `csr` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_csr_get_signature_algorithm_der(const struct SyntaCsr *csr,
                                                          struct SyntaByteArray *out);

/**
 * Get the CSR signature as raw DER bytes (BIT STRING with tag).
 *
 * Returns a borrowed slice valid while `csr` is alive.
 *
 * # Safety
 *
 * - `csr` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_csr_get_signature_der(const struct SyntaCsr *csr,
                                                struct SyntaByteArray *out);

/**
 * Get the CSR SubjectPublicKeyInfo as raw DER bytes.
 *
 * Returns a borrowed slice valid while `csr` is alive.
 *
 * # Safety
 *
 * - `csr` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_csr_get_public_key_der(const struct SyntaCsr *csr,
                                                 struct SyntaByteArray *out);

/**
 * Create a new decoder
 *
 * # Safety
 *
 * - `data` must be a valid pointer to a byte array of length `len`
 * - The data must remain valid for the lifetime of the decoder
 * - The returned decoder must be freed with `synta_decoder_free`
 *
 * # Returns
 *
 * Returns a pointer to the decoder, or NULL on error.
 */
struct SyntaDecoder *synta_decoder_new(const uint8_t *data,
                                       uintptr_t len,
                                       enum SyntaEncoding encoding);

/**
 * Create a new decoder with custom configuration
 *
 * # Safety
 *
 * - `data` must be a valid pointer to a byte array of length `len`
 * - `config` must be a valid pointer to a SyntaDecoderConfig
 * - The data must remain valid for the lifetime of the decoder
 * - The returned decoder must be freed with `synta_decoder_free`
 *
 * # Returns
 *
 * Returns a pointer to the decoder, or NULL on error.
 */
struct SyntaDecoder *synta_decoder_new_with_config(const uint8_t *data,
                                                   uintptr_t len,
                                                   enum SyntaEncoding encoding,
                                                   const struct SyntaDecoderConfig *config);

/**
 * Free a decoder
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer returned from `synta_decoder_new` or
 *   `synta_decoder_new_with_config`
 * - This function must only be called once for each decoder
 * - After calling this function, the decoder pointer is invalid
 */
void synta_decoder_free(struct SyntaDecoder *decoder);

/**
 * Get the number of remaining bytes in the decoder
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 */
uintptr_t synta_decoder_remaining(const struct SyntaDecoder *decoder);

/**
 * Check if the decoder is at the end of input
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 */
bool synta_decoder_at_end(const struct SyntaDecoder *decoder);

/**
 * Peek at the next tag without consuming it
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to a SyntaTag
 */
enum SyntaErrorCode synta_decoder_peek_tag(const struct SyntaDecoder *decoder,
                                           struct SyntaTag *out);

/**
 * Decode a boolean value
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to a bool
 */
enum SyntaErrorCode synta_decode_boolean(struct SyntaDecoder *decoder, bool *out);

/**
 * Decode an integer value
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the integer pointer
 * - The returned integer must be freed with `synta_integer_free`
 */
enum SyntaErrorCode synta_decode_integer(struct SyntaDecoder *decoder, struct SyntaInteger **out);

/**
 * Decode a NULL value
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 */
enum SyntaErrorCode synta_decode_null(struct SyntaDecoder *decoder);

/**
 * Decode a REAL (f64) value
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to an f64
 */
enum SyntaErrorCode synta_decode_real(struct SyntaDecoder *decoder, double *out);

/**
 * Decode an octet string value
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the octet string pointer
 * - The returned octet string must be freed with `synta_octet_string_free`
 */
enum SyntaErrorCode synta_decode_octet_string(struct SyntaDecoder *decoder,
                                              struct SyntaOctetString **out);

/**
 * Decode a UTF8String value (universal tag 12) into a `SyntaOctetString*`.
 *
 * Returns the raw content bytes without UTF-8 validation.  The bytes may
 * not be well-formed UTF-8.  Use `synta_decode_utf8_string` if you need the
 * decoder to enforce valid UTF-8 encoding.
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the octet string pointer
 * - The returned octet string must be freed with `synta_octet_string_free`
 */
enum SyntaErrorCode synta_decode_utf8_string_os(struct SyntaDecoder *decoder,
                                                struct SyntaOctetString **out);

/**
 * Decode a PrintableString value (universal tag 19) into a `SyntaOctetString*`.
 *
 * Returns the raw content bytes without character-set validation.  The bytes
 * may contain characters outside the PrintableString alphabet.  Use
 * `synta_decode_printable_string` if you need the decoder to enforce the
 * allowed character set.
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the octet string pointer
 * - The returned octet string must be freed with `synta_octet_string_free`
 */
enum SyntaErrorCode synta_decode_printable_string_os(struct SyntaDecoder *decoder,
                                                     struct SyntaOctetString **out);

/**
 * Decode an IA5String value (universal tag 22) into a `SyntaOctetString*`.
 *
 * Returns the raw content bytes without character-set validation.  The bytes
 * may contain code points above 127.  Use `synta_decode_ia5_string` if you
 * need the decoder to enforce the ASCII range.
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the octet string pointer
 * - The returned octet string must be freed with `synta_octet_string_free`
 */
enum SyntaErrorCode synta_decode_ia5_string_os(struct SyntaDecoder *decoder,
                                               struct SyntaOctetString **out);

/**
 * Decode a UTCTime value (universal tag 23) into a `SyntaOctetString*`.
 *
 * Returns the raw content bytes without time-string syntax validation.  The
 * bytes may not represent a valid UTCTime.  The caller is responsible for
 * parsing and validating the time string if correctness matters.
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the octet string pointer
 * - The returned octet string must be freed with `synta_octet_string_free`
 */
enum SyntaErrorCode synta_decode_utctime_os(struct SyntaDecoder *decoder,
                                            struct SyntaOctetString **out);

/**
 * Decode a GeneralizedTime value (universal tag 24) into a `SyntaOctetString*`.
 *
 * Returns the raw content bytes without time-string syntax validation.  The
 * bytes may not represent a valid GeneralizedTime.  The caller is responsible
 * for parsing and validating the time string if correctness matters.
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the octet string pointer
 * - The returned octet string must be freed with `synta_octet_string_free`
 */
enum SyntaErrorCode synta_decode_generalized_time_os(struct SyntaDecoder *decoder,
                                                     struct SyntaOctetString **out);

/**
 * Decode a bit string value
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to a SyntaByteArray
 * - `unused_bits` must be a valid pointer to receive the unused bits count
 * - The returned byte array is owned and must be freed with `synta_byte_array_free`
 */
enum SyntaErrorCode synta_decode_bit_string(struct SyntaDecoder *decoder,
                                            struct SyntaByteArray *out,
                                            uint8_t *unused_bits);

/**
 * Decode an object identifier value
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the OID pointer
 * - The returned OID must be freed with `synta_oid_free`
 */
enum SyntaErrorCode synta_decode_object_identifier(struct SyntaDecoder *decoder,
                                                   struct SyntaObjectIdentifier **out);

/**
 * Decode a UTF-8 string value
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to a SyntaByteArray
 * - The returned byte array is owned and must be freed with `synta_byte_array_free`
 */
enum SyntaErrorCode synta_decode_utf8_string(struct SyntaDecoder *decoder,
                                             struct SyntaByteArray *out);

/**
 * Decode a PrintableString value
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to a SyntaByteArray
 * - The returned byte array is owned and must be freed with `synta_byte_array_free`
 */
enum SyntaErrorCode synta_decode_printable_string(struct SyntaDecoder *decoder,
                                                  struct SyntaByteArray *out);

/**
 * Decode an IA5String value
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to a SyntaByteArray
 * - The returned byte array is owned and must be freed with `synta_byte_array_free`
 */
enum SyntaErrorCode synta_decode_ia5_string(struct SyntaDecoder *decoder,
                                            struct SyntaByteArray *out);

/**
 * Enter a SEQUENCE and return a new decoder for its contents
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the new decoder pointer
 * - The returned decoder must be freed with `synta_decoder_free`
 */
enum SyntaErrorCode synta_decoder_enter_sequence(struct SyntaDecoder *decoder,
                                                 struct SyntaDecoder **out);

/**
 * Enter a SET and return a new decoder for its contents
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the new decoder pointer
 * - The returned decoder must be freed with `synta_decoder_free`
 */
enum SyntaErrorCode synta_decoder_enter_set(struct SyntaDecoder *decoder,
                                            struct SyntaDecoder **out);

/**
 * Enter a constructed type with a specific tag
 *
 * # Safety
 *
 * - `decoder` must be a valid pointer to a decoder
 * - `out` must be a valid pointer to receive the new decoder pointer
 * - The returned decoder must be freed with `synta_decoder_free`
 */
enum SyntaErrorCode synta_decoder_enter_constructed(struct SyntaDecoder *decoder,
                                                    struct SyntaTag tag,
                                                    struct SyntaDecoder **out);

/**
 * Create a new encoder
 *
 * # Safety
 *
 * The returned encoder must be freed with `synta_encoder_free`
 */
struct SyntaEncoder *synta_encoder_new(enum SyntaEncoding encoding);

/**
 * Finish encoding and consume the encoder, returning the encoded bytes.
 *
 * **The encoder is destroyed by this call.**  The `encoder` pointer is
 * invalid after `synta_encoder_finish` returns, regardless of whether the
 * call succeeds or fails.  Do **not** pass the same pointer to
 * `synta_encoder_free` afterwards — that would be a double-free (undefined
 * behaviour).  The typical usage pattern is:
 *
 * ```c
 * SyntaEncoder *enc = synta_encoder_new(SyntaEncoding_Der);
 * // ... encode values ...
 * SyntaByteArray result;
 * SyntaErrorCode err = synta_encoder_finish(enc, &result);
 * enc = NULL;  // pointer is now invalid; set to NULL immediately
 * if (err != SyntaErrorCode_Success) { // handle error
 *     // ...
 * }
 * // use result ...
 * synta_byte_array_free(&result);
 * ```
 *
 * # Safety
 *
 * - `encoder` must be a valid, non-null pointer returned by `synta_encoder_new`.
 * - `out` must be a valid, non-null pointer to a `SyntaByteArray`.
 * - The encoder is **consumed** (destroyed) by this call.  The pointer must
 *   not be dereferenced or passed to any function (including
 *   `synta_encoder_free`) after this call returns.
 * - On success, the byte array written to `*out` is owned by the caller and
 *   must be freed with `synta_byte_array_free`.
 * - On failure, `*out` is left unchanged; call `synta_get_last_error_message`
 *   for details.
 */
enum SyntaErrorCode synta_encoder_finish(struct SyntaEncoder *encoder, struct SyntaByteArray *out);

/**
 * Discard an encoder without finishing it.
 *
 * Use this to clean up an encoder when encoding has been abandoned (e.g.
 * after an error).  Do **not** call this after `synta_encoder_finish` —
 * `synta_encoder_finish` already destroys the encoder, so calling
 * `synta_encoder_free` on the same pointer would be a double-free (undefined
 * behaviour).
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer returned by `synta_encoder_new`, **or**
 *   `NULL` (a null pointer is silently ignored).
 * - This function must only be called once per encoder.
 * - Must **not** be called if `synta_encoder_finish` has already been called
 *   with this pointer — that function destroys the encoder.
 */
void synta_encoder_free(struct SyntaEncoder *encoder);

/**
 * Encode a boolean value
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 */
enum SyntaErrorCode synta_encode_boolean(struct SyntaEncoder *encoder, bool value);

/**
 * Encode an integer value from an Integer object
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `integer` must be a valid pointer to an Integer
 */
enum SyntaErrorCode synta_encode_integer(struct SyntaEncoder *encoder,
                                         const struct SyntaInteger *integer);

/**
 * Encode an integer value from an i64
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 */
enum SyntaErrorCode synta_encode_integer_i64(struct SyntaEncoder *encoder, int64_t value);

/**
 * Encode a NULL value
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 */
enum SyntaErrorCode synta_encode_null(struct SyntaEncoder *encoder);

/**
 * Encode a REAL (f64) value
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 */
enum SyntaErrorCode synta_encode_real(struct SyntaEncoder *encoder, double value);

/**
 * Encode an octet string value
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `data` must be a valid pointer to a byte array of length `len`, or NULL if len is 0
 */
enum SyntaErrorCode synta_encode_octet_string(struct SyntaEncoder *encoder,
                                              const uint8_t *data,
                                              uintptr_t len);

/**
 * Encode a UTF8String value (universal tag 12) from raw bytes.
 *
 * Unlike `synta_encode_utf8_string`, this function does not require the data to
 * be null-terminated.  The data is written as-is; no UTF-8 validation is
 * performed because the library stores string values as raw byte arrays.
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `data` must be a valid pointer to a byte array of length `len`, or NULL if len is 0
 */
enum SyntaErrorCode synta_encode_utf8_string_bytes(struct SyntaEncoder *encoder,
                                                   const uint8_t *data,
                                                   uintptr_t len);

/**
 * Encode a PrintableString value (universal tag 19) from raw bytes.
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `data` must be a valid pointer to a byte array of length `len`, or NULL if len is 0
 */
enum SyntaErrorCode synta_encode_printable_string_bytes(struct SyntaEncoder *encoder,
                                                        const uint8_t *data,
                                                        uintptr_t len);

/**
 * Encode an IA5String value (universal tag 22) from raw bytes.
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `data` must be a valid pointer to a byte array of length `len`, or NULL if len is 0
 */
enum SyntaErrorCode synta_encode_ia5_string_bytes(struct SyntaEncoder *encoder,
                                                  const uint8_t *data,
                                                  uintptr_t len);

/**
 * Encode a UTCTime value (universal tag 23) from raw bytes.
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `data` must be a valid pointer to a byte array of length `len`, or NULL if len is 0
 */
enum SyntaErrorCode synta_encode_utctime_bytes(struct SyntaEncoder *encoder,
                                               const uint8_t *data,
                                               uintptr_t len);

/**
 * Encode a GeneralizedTime value (universal tag 24) from raw bytes.
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `data` must be a valid pointer to a byte array of length `len`, or NULL if len is 0
 */
enum SyntaErrorCode synta_encode_generalized_time_bytes(struct SyntaEncoder *encoder,
                                                        const uint8_t *data,
                                                        uintptr_t len);

/**
 * Encode a bit string value
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `data` must be a valid pointer to a byte array of length `len`, or NULL if len is 0
 * - `unused_bits` must be less than 8
 */
enum SyntaErrorCode synta_encode_bit_string(struct SyntaEncoder *encoder,
                                            const uint8_t *data,
                                            uintptr_t len,
                                            uint8_t unused_bits);

/**
 * Encode an object identifier value
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `oid` must be a valid pointer to an ObjectIdentifier
 */
enum SyntaErrorCode synta_encode_object_identifier(struct SyntaEncoder *encoder,
                                                   const struct SyntaObjectIdentifier *oid);

/**
 * Encode a UTF-8 string value
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `str` must be a valid null-terminated C string
 */
enum SyntaErrorCode synta_encode_utf8_string(struct SyntaEncoder *encoder, const char *str);

/**
 * Encode a PrintableString value
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `str` must be a valid null-terminated C string
 */
enum SyntaErrorCode synta_encode_printable_string(struct SyntaEncoder *encoder, const char *str);

/**
 * Encode an IA5String value
 *
 * # Safety
 *
 * - `encoder` must be a valid pointer to an encoder
 * - `str` must be a valid null-terminated C string
 */
enum SyntaErrorCode synta_encode_ia5_string(struct SyntaEncoder *encoder, const char *str);

/**
 * Open a SEQUENCE and return an encoder handle for writing its contents.
 *
 * **`*out` aliases `encoder` — they point to the same object.**
 * This means:
 * - Write child elements by passing `*out` to encode functions.
 * - Close the SEQUENCE by calling `synta_encoder_end_constructed(*out)`.
 * - Do **not** call `synta_encoder_free` or `synta_encoder_finish` on
 *   `encoder` while the SEQUENCE is still open; that would destroy the shared
 *   object and leave `*out` dangling.
 * - Nested calls (`synta_encoder_start_sequence(*out, &inner)`) are allowed;
 *   close the innermost SEQUENCE first.
 *
 * # Safety
 *
 * - `encoder` must be a valid, non-null pointer to a live encoder.
 * - `out` must be a valid, non-null pointer to a `*mut SyntaEncoder`.
 * - `*out` must not be passed to `synta_encoder_free` or
 *   `synta_encoder_finish`; use `synta_encoder_end_constructed` instead.
 */
enum SyntaErrorCode synta_encoder_start_sequence(struct SyntaEncoder *encoder,
                                                 struct SyntaEncoder **out);

/**
 * Open a SET and return an encoder handle for writing its contents.
 *
 * **`*out` aliases `encoder` — they point to the same object.**
 * See `synta_encoder_start_sequence` for the full aliasing contract and
 * usage rules; the same constraints apply here.
 *
 * # Safety
 *
 * - `encoder` must be a valid, non-null pointer to a live encoder.
 * - `out` must be a valid, non-null pointer to a `*mut SyntaEncoder`.
 * - `*out` must not be passed to `synta_encoder_free` or
 *   `synta_encoder_finish`; use `synta_encoder_end_constructed` instead.
 */
enum SyntaErrorCode synta_encoder_start_set(struct SyntaEncoder *encoder,
                                            struct SyntaEncoder **out);

/**
 * Open a constructed element with an arbitrary tag and return an encoder
 * handle for writing its contents.
 *
 * **`*out` aliases `encoder` — they point to the same object.**
 * See `synta_encoder_start_sequence` for the full aliasing contract and
 * usage rules; the same constraints apply here.
 *
 * # Safety
 *
 * - `encoder` must be a valid, non-null pointer to a live encoder.
 * - `out` must be a valid, non-null pointer to a `*mut SyntaEncoder`.
 * - `*out` must not be passed to `synta_encoder_free` or
 *   `synta_encoder_finish`; use `synta_encoder_end_constructed` instead.
 */
enum SyntaErrorCode synta_encoder_start_constructed(struct SyntaEncoder *encoder,
                                                    struct SyntaTag tag,
                                                    struct SyntaEncoder **out);

/**
 * Close the innermost open constructed element and back-patch its length.
 *
 * Pass the `*out` pointer returned by `synta_encoder_start_sequence`,
 * `synta_encoder_start_set`, or `synta_encoder_start_constructed`.
 * Because `*out` aliases the parent encoder, after this call the parent
 * encoder is ready to accept more elements at the same nesting level.
 *
 * # Safety
 *
 * - `encoder` must be a valid, non-null pointer to a live encoder that has an
 *   open constructed element (i.e. `synta_encoder_start_*` was called and not
 *   yet balanced by a matching `synta_encoder_end_constructed`).
 * - This function must be called exactly once for each `synta_encoder_start_*`
 *   call, in innermost-first order for nested constructed elements.
 */
enum SyntaErrorCode synta_encoder_end_constructed(struct SyntaEncoder *encoder);

/**
 * Get the last error message
 *
 * # Safety
 *
 * The returned pointer is valid until the next call to any function that sets an error,
 * or until the current thread exits. The caller must not free the pointer.
 *
 * Returns NULL if no error has occurred or if the error message is invalid.
 */
const char *synta_get_last_error_message(void);

/**
 * Clear the last error message
 */
void synta_clear_last_error(void);

/**
 * Get a static error message for an error code
 *
 * # Safety
 *
 * The returned pointer is always valid and points to a static string.
 * The caller must not free the pointer.
 */
const char *synta_error_message(enum SyntaErrorCode code);

/**
 * Create an Integer from an i64 value
 *
 * # Safety
 *
 * The returned integer must be freed with `synta_integer_free`
 */
struct SyntaInteger *synta_integer_new_i64(int64_t value);

/**
 * Create an Integer from a u64 value
 *
 * # Safety
 *
 * The returned integer must be freed with `synta_integer_free`
 */
struct SyntaInteger *synta_integer_new_u64(uint64_t value);

/**
 * Create an Integer from a DER two's-complement byte array.
 *
 * `data` must point to `len` bytes in DER INTEGER two's-complement encoding:
 * the most significant byte comes first and the sign is encoded in the
 * most-significant bit of the first byte (set = negative, clear = positive).
 * This is the same byte layout as the content octets of a DER INTEGER TLV.
 *
 * The `is_negative` parameter is accepted for API stability but is **not
 * used** — the sign is already encoded in the byte content.  Passing `true`
 * does **not** negate the value; the caller must supply the correct two's
 * complement bytes instead.
 *
 * # Safety
 *
 * - `data` must be a valid, non-null pointer to at least `len` bytes.
 * - The returned integer must be freed with `synta_integer_free`.
 */
struct SyntaInteger *synta_integer_new_bytes(const uint8_t *data, uintptr_t len, bool _is_negative);

/**
 * Convert an Integer to an i64 value
 *
 * # Safety
 *
 * - `integer` must be a valid pointer to an Integer
 * - `out` must be a valid pointer to an i64
 *
 * # Returns
 *
 * Returns Success if the integer fits in an i64, IntegerOverflow otherwise
 */
enum SyntaErrorCode synta_integer_to_i64(const struct SyntaInteger *integer, int64_t *out);

/**
 * Convert an Integer to a u64 value
 *
 * # Safety
 *
 * - `integer` must be a valid pointer to an Integer
 * - `out` must be a valid pointer to a u64
 *
 * # Returns
 *
 * Returns Success if the integer fits in a u64 and is non-negative, IntegerOverflow/IntegerUnderflow otherwise
 */
enum SyntaErrorCode synta_integer_to_u64(const struct SyntaInteger *integer,
                                         uint64_t *out);

/**
 * Convert an Integer to a byte array
 *
 * # Safety
 *
 * - `integer` must be a valid pointer to an Integer
 * - `out` must be a valid pointer to a SyntaByteArray
 * - The returned byte array is owned and must be freed with `synta_byte_array_free`
 */
enum SyntaErrorCode synta_integer_to_bytes(const struct SyntaInteger *integer,
                                           struct SyntaByteArray *out);

/**
 * Check if an Integer is negative
 *
 * # Safety
 *
 * - `integer` must be a valid pointer to an Integer
 */
bool synta_integer_is_negative(const struct SyntaInteger *integer);

/**
 * Free an Integer
 *
 * # Safety
 *
 * - `integer` must be a valid pointer returned from an integer creation function
 * - This function must only be called once for each integer
 */
void synta_integer_free(struct SyntaInteger *integer);

/**
 * Parse an OCSP response from DER bytes.
 *
 * Returns NULL on parse error; free with [`synta_ocsp_free`].
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` bytes.
 */
struct SyntaOcsp *synta_ocsp_parse_der(const uint8_t *data, uintptr_t len);

/**
 * Free an OCSP response returned by [`synta_ocsp_parse_der`].
 *
 * # Safety
 *
 * - `ocsp` must be a valid pointer, or NULL.  Must be called exactly once.
 */
void synta_ocsp_free(struct SyntaOcsp *ocsp);

/**
 * Get the OCSP response status as an RFC 6960 integer code.
 *
 * | Code | Status           |
 * |------|------------------|
 * | 0    | successful       |
 * | 1    | malformedRequest |
 * | 2    | internalError    |
 * | 3    | tryLater         |
 * | 5    | sigRequired      |
 * | 6    | unauthorized     |
 *
 * # Safety
 *
 * - `ocsp` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_ocsp_get_status_code(const struct SyntaOcsp *ocsp, int32_t *out);

/**
 * Write the OCSP response status name into `buffer` as a NUL-terminated string.
 *
 * Uses the same size-query convention as `synta_certificate_get_signature_algorithm`:
 * pass `NULL`/`0` to get the required buffer size (including the NUL terminator).
 * Returns bytes written (excluding NUL) on success, or 0 if `ocsp` is NULL.
 *
 * # Safety
 *
 * - `ocsp` must be a valid pointer, or NULL.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_ocsp_get_status(const struct SyntaOcsp *ocsp, char *buffer, uintptr_t buffer_len);

/**
 * Write the OCSP responseType OID (dotted-decimal) into `buffer`.
 *
 * Returns 0 if `ocsp` is NULL **or** if `responseBytes` is absent.
 * Uses the same size-query convention as [`synta_ocsp_get_status`].
 *
 * # Safety
 *
 * - `ocsp` must be a valid pointer, or NULL.
 * - If `buffer` is non-null it must point to `buffer_len` writable bytes.
 */
uintptr_t synta_ocsp_get_response_type_oid(const struct SyntaOcsp *ocsp,
                                           char *buffer,
                                           uintptr_t buffer_len);

/**
 * Get the raw content bytes of the OCSP `response` OCTET STRING.
 *
 * For a `successful` response this is typically the DER of a
 * `BasicOCSPResponse`.  Returns a zero-length array (`out->len == 0`) if
 * `responseBytes` is absent.  The returned slice is valid while `ocsp` is alive.
 *
 * # Safety
 *
 * - `ocsp` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_ocsp_get_response_bytes_der(const struct SyntaOcsp *ocsp,
                                                      struct SyntaByteArray *out);

/**
 * Create an OctetString from a byte array
 *
 * # Safety
 *
 * - `data` must be a valid pointer to a byte array of length `len`
 * - The returned octet string must be freed with `synta_octet_string_free`
 */
struct SyntaOctetString *synta_octet_string_new(const uint8_t *data, uintptr_t len);

/**
 * Get a pointer to the data in an OctetString
 *
 * # Safety
 *
 * - `octet_string` must be a valid pointer to an OctetString
 * - The returned pointer is valid only while the OctetString is alive
 */
const uint8_t *synta_octet_string_data(const struct SyntaOctetString *octet_string);

/**
 * Get the length of an OctetString
 *
 * # Safety
 *
 * - `octet_string` must be a valid pointer to an OctetString
 */
uintptr_t synta_octet_string_len(const struct SyntaOctetString *octet_string);

/**
 * Free an OctetString
 *
 * # Safety
 *
 * - `octet_string` must be a valid pointer returned from an octet string creation function
 * - This function must only be called once for each octet string
 */
void synta_octet_string_free(struct SyntaOctetString *octet_string);

/**
 * Create an OID from an array of components
 *
 * # Safety
 *
 * - `components` must be a valid pointer to an array of u32 values of length `len`
 * - The returned OID must be freed with `synta_oid_free`
 */
struct SyntaObjectIdentifier *synta_oid_new(const uint32_t *components, uintptr_t len);

/**
 * Create an OID from a string (e.g., "1.2.840.113549.1.1.1")
 *
 * # Safety
 *
 * - `str` must be a valid null-terminated C string
 * - The returned OID must be freed with `synta_oid_free`
 */
struct SyntaObjectIdentifier *synta_oid_from_string(const char *str);

/**
 * Convert an OID to its dotted-decimal string representation.
 *
 * # Return value
 *
 * | Condition                          | Return value            |
 * |------------------------------------|-------------------------|
 * | `oid` is null                      | `0` (error set)         |
 * | `buf` is null or `buf_len` is `0`  | required size (incl. `\0`) |
 * | `buf_len` too small                | required size (incl. `\0`) |
 * | success                            | bytes written, excl. `\0` |
 *
 * On a null `oid`, `synta_get_last_error_message()` returns a description.
 * On a too-small buffer, the return value is the minimum `buf_len` needed
 * for a subsequent successful call.  This makes the function usable for a
 * size-query / retry pattern:
 *
 * ```c
 * size_t needed = synta_oid_to_string(oid, NULL, 0);
 * char *buf = malloc(needed);
 * synta_oid_to_string(oid, buf, needed);
 * ```
 *
 * # Safety
 *
 * - `oid` must be a valid pointer to an OID, or `NULL`.
 * - If `buf` is non-null it must point to a writable buffer of at least
 *   `buf_len` bytes.
 */
uintptr_t synta_oid_to_string(const struct SyntaObjectIdentifier *oid,
                              char *buf,
                              uintptr_t buf_len);

/**
 * Get the OID components as an array
 *
 * # Safety
 *
 * - `oid` must be a valid pointer to an OID
 * - `out` must be a valid pointer to receive the components pointer
 * - `out_len` must be a valid pointer to receive the components count
 * - The returned pointer is valid only while the OID is alive
 */
enum SyntaErrorCode synta_oid_components(const struct SyntaObjectIdentifier *oid,
                                         const uint32_t **out,
                                         uintptr_t *out_len);

/**
 * Check if two OIDs are equal
 *
 * # Safety
 *
 * - `a` and `b` must be valid pointers to OIDs
 */
bool synta_oid_equals(const struct SyntaObjectIdentifier *a, const struct SyntaObjectIdentifier *b);

/**
 * Free an OID
 *
 * # Safety
 *
 * - `oid` must be a valid pointer returned from an OID creation function
 * - This function must only be called once for each OID
 */
void synta_oid_free(struct SyntaObjectIdentifier *oid);

/**
 * Decode all PEM blocks in `data` and return an opaque list of DER buffers.
 *
 * Returns `NULL` if no PEM block is found or `data` is `NULL`.
 * Free the returned list with [`synta_der_list_free`].
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` bytes, or `NULL`.
 */
struct SyntaDerList *synta_pem_to_der(const uint8_t *data, uintptr_t len);

/**
 * Encode DER bytes as a PEM block; return the result as an owned byte array.
 *
 * The caller must free the returned `SyntaByteArray` with `synta_byte_array_free`.
 * Returns a zero-length array (`data = NULL`) on error.
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` readable bytes.
 * - `label` must be a valid null-terminated UTF-8 C string.
 */
struct SyntaByteArray synta_der_to_pem(const uint8_t *data, uintptr_t len, const char *label);

/**
 * Return the number of DER buffers in `list`.  Returns 0 if `list` is NULL.
 *
 * # Safety
 *
 * - `list` must be a valid pointer returned by a synta function, or `NULL`.
 */
uintptr_t synta_der_list_count(const struct SyntaDerList *list);

/**
 * Write the DER bytes at `index` into `*out` as a borrowed slice.
 *
 * The returned slice is valid while `list` is alive; do **not** free it with
 * `synta_byte_array_free`.  Returns `InvalidArgument` if `index` is out of range.
 *
 * # Safety
 *
 * - `list` and `out` must be valid non-null pointers.
 */
enum SyntaErrorCode synta_der_list_get_der(const struct SyntaDerList *list,
                                           uintptr_t index,
                                           struct SyntaByteArray *out);

/**
 * Free a [`SyntaDerList`] returned by a synta function.
 *
 * # Safety
 *
 * - `list` must be a valid pointer, or `NULL`.  Must be called exactly once.
 */
void synta_der_list_free(struct SyntaDerList *list);

/**
 * Extract X.509 certificates from a DER/BER-encoded PKCS#7 / CMS SignedData blob.
 *
 * Returns an opaque list of DER-encoded certificates; free with
 * [`synta_der_list_free`].  Returns NULL on error.
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` bytes.
 */
struct SyntaDerList *synta_pkcs7_certs_from_der(const uint8_t *data, uintptr_t len);

/**
 * Extract X.509 certificates from a PEM-encoded PKCS#7 / CMS SignedData blob.
 *
 * Strips the PEM envelope then calls the DER extractor on each block.
 * Returns an opaque list of DER-encoded certificates; free with
 * [`synta_der_list_free`].  Returns NULL on error or if no PEM block is found.
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` bytes.
 */
struct SyntaDerList *synta_pkcs7_certs_from_pem(const uint8_t *data, uintptr_t len);

/**
 * Extract X.509 certificates from an unencrypted PKCS#12 archive.
 *
 * Supports archives with no MAC and archives that use an empty password.
 * Encrypted content bags require an OpenSSL-backed build; this function
 * uses the dependency-free `NoCrypto` backend and returns an error for any
 * encrypted safe content it encounters.
 *
 * `password` may be NULL (treated as an empty byte string).  Returns an
 * opaque list of DER-encoded certificates; free with [`synta_der_list_free`].
 * Returns NULL on error.
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` bytes.
 * - If `password` is non-null it must point to `password_len` readable bytes.
 */
struct SyntaDerList *synta_pkcs12_certs_from_der(const uint8_t *data,
                                                 uintptr_t len,
                                                 const uint8_t *password,
                                                 uintptr_t password_len);

/**
 * Auto-detect the encoding of `data` and return every X.509 certificate as a
 * DER blob.
 *
 * Inspects the raw bytes, detects PEM / PKCS#7 / PKCS#12 / raw DER encoding,
 * and collects all entries whose label contains `"CERTIFICATE"`.  This is
 * equivalent to calling `synta_pkcs7_certs_from_der`, `synta_pem_to_der`,
 * `synta_pkcs12_certs_from_der`, or handling a raw DER blob — depending on
 * what `data` actually contains.
 *
 * `password` applies to PKCS#12 archives.  Pass NULL or a zero-length slice
 * for password-less archives.  Encrypted PKCS#12 bags are silently skipped
 * (unencrypted certificates in the same archive are still returned); the
 * C FFI layer does not include an OpenSSL backend.
 *
 * Returns an owned `SyntaDerList *`; NULL on hard structural error.
 * Free with [`synta_der_list_free`].
 *
 * # Safety
 *
 * - `data` must be a valid pointer to `len` bytes.
 * - If `password` is non-null it must point to `password_len` readable bytes.
 */
struct SyntaDerList *synta_read_pki_file(const uint8_t *data,
                                         uintptr_t len,
                                         const uint8_t *password,
                                         uintptr_t password_len);

/**
 * Free a byte array (only if owned)
 *
 * # Safety
 *
 * - `array` must be a valid pointer to a SyntaByteArray
 * - This function must only be called once for each owned array
 */
void synta_byte_array_free(struct SyntaByteArray *array);

/**
 * Allocate a zeroed `SyntaByteArray` of `len` bytes from Rust's allocator.
 *
 * The returned array is owned (`owned = 1`) and must be freed with
 * [`synta_byte_array_free`].
 *
 * Returns an empty array with `data = NULL` if `len` is 0.
 */
struct SyntaByteArray synta_byte_array_alloc(uintptr_t len);

/**
 * Clone a byte array (creates an owned copy)
 *
 * # Safety
 *
 * - `array` must be a valid pointer to a SyntaByteArray
 * - The returned array must be freed with synta_byte_array_free
 */
struct SyntaByteArray synta_byte_array_clone(const struct SyntaByteArray *array);

#ifdef __cplusplus
}  // extern "C"
#endif  // __cplusplus

#endif  /* SYNTA_H */