synta 0.1.4

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
# Synta C API Reference

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents**  *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [Overview]#overview
  - [Header File]#header-file
  - [Linking]#linking
- [Error Handling]#error-handling
  - [Error Codes]#error-codes
  - [Error Functions]#error-functions
    - [synta_error_message]#syntaerrormessage
    - [synta_get_last_error_message]#syntagetlasterrormessage
    - [synta_clear_last_error]#syntaclearlast_error
- [Decoder API]#decoder-api
  - [Decoder Lifecycle]#decoder-lifecycle
    - [synta_decoder_new]#syntadecodernew
    - [synta_decoder_new_with_config]#syntadecodernewwithconfig
    - [synta_decoder_free]#syntadecoderfree
  - [Decoder Utilities]#decoder-utilities
    - [synta_decoder_remaining]#syntadecoderremaining
    - [synta_decoder_at_end]#syntadecoderat_end
    - [synta_decoder_peek_tag]#syntadecoderpeek_tag
  - [Primitive Type Decoding]#primitive-type-decoding
    - [synta_decode_boolean]#syntadecodeboolean
    - [synta_decode_integer]#syntadecodeinteger
    - [synta_decode_null]#syntadecodenull
    - [synta_decode_octet_string]#syntadecodeoctet_string
    - [synta_decode_bit_string]#syntadecodebit_string
    - [synta_decode_object_identifier]#syntadecodeobject_identifier
  - [String Type Decoding]#string-type-decoding
    - [synta_decode_utf8_string]#syntadecodeutf8_string
    - [synta_decode_printable_string]#syntadecodeprintable_string
    - [synta_decode_ia5_string]#syntadecodeia5_string
  - [Constructed Type Decoding]#constructed-type-decoding
    - [synta_decoder_enter_sequence]#syntadecoderenter_sequence
    - [synta_decoder_enter_set]#syntadecoderenter_set
    - [synta_decoder_enter_constructed]#syntadecoderenter_constructed
- [Encoder API]#encoder-api
  - [Encoder Lifecycle]#encoder-lifecycle
    - [synta_encoder_new]#syntaencodernew
    - [synta_encoder_finish]#syntaencoderfinish
    - [synta_encoder_free]#syntaencoderfree
  - [Primitive Type Encoding]#primitive-type-encoding
    - [synta_encode_boolean]#syntaencodeboolean
    - [synta_encode_integer_i64]#syntaencodeinteger_i64
    - [synta_encode_integer]#syntaencodeinteger
    - [synta_encode_null]#syntaencodenull
    - [synta_encode_octet_string]#syntaencodeoctet_string
    - [synta_encode_bit_string]#syntaencodebit_string
    - [synta_encode_object_identifier]#syntaencodeobject_identifier
  - [String Type Encoding]#string-type-encoding
    - [synta_encode_utf8_string]#syntaencodeutf8_string
    - [synta_encode_printable_string]#syntaencodeprintable_string
    - [synta_encode_ia5_string]#syntaencodeia5_string
  - [Constructed Type Encoding]#constructed-type-encoding
    - [synta_encoder_start_sequence]#syntaencoderstart_sequence
    - [synta_encoder_start_set]#syntaencoderstart_set
    - [synta_encoder_start_constructed]#syntaencoderstart_constructed
    - [synta_encoder_end_constructed]#syntaencoderend_constructed
- [Helper Types]#helper-types
  - [Integer Helpers]#integer-helpers
    - [synta_integer_new_i64]#syntaintegernew_i64
    - [synta_integer_new_u64]#syntaintegernew_u64
    - [synta_integer_new_bytes]#syntaintegernew_bytes
    - [synta_integer_to_i64]#syntaintegerto_i64
    - [synta_integer_to_u64]#syntaintegerto_u64
    - [synta_integer_to_bytes]#syntaintegerto_bytes
    - [synta_integer_is_negative]#syntaintegeris_negative
    - [synta_integer_free]#syntaintegerfree
  - [OctetString Helpers]#octetstring-helpers
    - [synta_octet_string_new]#syntaoctetstring_new
    - [synta_octet_string_data]#syntaoctetstring_data
    - [synta_octet_string_len]#syntaoctetstring_len
    - [synta_octet_string_free]#syntaoctetstring_free
  - [ObjectIdentifier Helpers]#objectidentifier-helpers
    - [synta_oid_new]#syntaoidnew
    - [synta_oid_from_string]#syntaoidfrom_string
    - [synta_oid_to_string]#syntaoidto_string
    - [synta_oid_components]#syntaoidcomponents
    - [synta_oid_equals]#syntaoidequals
    - [synta_oid_free]#syntaoidfree
  - [ByteArray Helpers]#bytearray-helpers
    - [synta_byte_array_free]#syntabytearray_free
    - [synta_byte_array_clone]#syntabytearray_clone
- [X.509 PKI APIs]#x509-pki-apis
  - [CRL API]#crl-api
    - [synta_crl_parse_der]#syntacrlparse_der
    - [synta_crl_parse_pem]#syntacrlparse_pem
    - [synta_crl_free]#syntacrlfree
    - [synta_crl_get_issuer_der]#syntacrlgetissuerder
    - [synta_crl_get_signature_algorithm_der]#syntacrlgetsignaturealgorithm_der
    - [synta_crl_get_signature_value_der]#syntacrlgetsignaturevalue_der
    - [synta_crl_get_this_update_der]#syntacrlgetthisupdate_der
    - [synta_crl_get_next_update_der]#syntacrlgetnextupdate_der
    - [synta_crl_get_revoked_count]#syntacrlgetrevokedcount
  - [CSR API]#csr-api
    - [synta_csr_parse_der]#syntacsrparse_der
    - [synta_csr_parse_pem]#syntacsrparse_pem
    - [synta_csr_free]#syntacsrfree
    - [synta_csr_get_version]#syntacsrget_version
    - [synta_csr_get_subject_der]#syntacsrgetsubjectder
    - [synta_csr_get_signature_algorithm_der]#syntacsrgetsignaturealgorithm_der
    - [synta_csr_get_signature_der]#syntacsrgetsignatureder
    - [synta_csr_get_public_key_der]#syntacsrgetpublickey_der
  - [OCSP API]#ocsp-api
    - [synta_ocsp_parse_der]#syntaocspparse_der
    - [synta_ocsp_free]#syntaocspfree
    - [synta_ocsp_get_status_code]#syntaocspgetstatuscode
    - [synta_ocsp_get_status]#syntaocspget_status
    - [synta_ocsp_get_response_type_oid]#syntaocspgetresponsetype_oid
    - [synta_ocsp_get_response_bytes_der]#syntaocspgetresponsebytes_der
  - [PEM API]#pem-api
    - [synta_pem_to_der]#syntapemto_der
    - [synta_der_list_free]#syntaderlist_free
    - [synta_der_list_count]#syntaderlist_count
    - [synta_der_list_get_der]#syntaderlistgetder
    - [synta_der_to_pem]#syntaderto_pem
  - [PKCS#7 and PKCS#12 API]#pkcs7-and-pkcs12-api
    - [synta_read_pki_file]#syntareadpki_file
    - [synta_pkcs7_certs_from_der]#syntapkcs7certsfromder
    - [synta_pkcs7_certs_from_pem]#syntapkcs7certsfrompem
    - [synta_pkcs12_certs_from_der]#syntapkcs12certsfromder
- [CMS API]#cms-api
  - [ContentInfo API]#contentinfo-api
    - [synta_cms_content_info_parse_der]#syntacmscontentinfoparse_der
    - [synta_cms_content_info_free]#syntacmscontentinfofree
    - [synta_cms_content_info_get_content_type]#syntacmscontentinfogetcontenttype
    - [synta_cms_content_info_get0_signed_data]#syntacmscontentinfoget0signeddata
    - [synta_cms_content_info_get0_enveloped_data]#syntacmscontentinfoget0envelopeddata
    - [synta_cms_content_info_get0_encrypted_data]#syntacmscontentinfoget0encrypteddata
    - [synta_cms_content_info_get0_digested_data]#syntacmscontentinfoget0digesteddata
  - [SignedData API]#signeddata-api
  - [SignerInfo API]#signerinfo-api
    - [synta_cms_signer_info_parse_der]#syntacmssignerinfoparse_der
    - [synta_cms_signer_info_free]#syntacmssignerinfofree
  - [EnvelopedData API]#envelopeddata-api
  - [EncryptedData API]#encrypteddata-api
    - [synta_cms_encrypted_data_parse]#syntacmsencrypteddataparse
    - [synta_cms_encrypted_data_free]#syntacmsencrypteddatafree
    - [synta_cms_encrypted_data_decrypt]#syntacmsencrypteddatadecrypt
    - [synta_cms_encrypted_data_create]#syntacmsencrypteddatacreate
  - [DigestedData API]#digesteddata-api
- [Thread Safety]#thread-safety
- [See Also]#see-also

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Complete reference for the Synta C Foreign Function Interface (FFI).

## Table of Contents

- [Overview]#overview
- [Error Handling]#error-handling
- [Decoder API]#decoder-api
- [Encoder API]#encoder-api
- [Helper Types]#helper-types
- [Thread Safety]#thread-safety

## Overview

The Synta C API provides a safe, ergonomic interface for ASN.1 encoding and decoding from C and C++ programs. All functions follow consistent patterns for error handling, memory management, and resource cleanup.

### Header File

```c
#include <synta.h>
```

### Linking

```bash
# With pkg-config
gcc myapp.c $(pkg-config --cflags --libs synta) -o myapp

# With CMake
find_package(Synta REQUIRED)
target_link_libraries(myapp Synta::Synta)

# Manual
gcc myapp.c -I/path/to/include -L/path/to/lib -lcsynta -lpthread -ldl -lm -o myapp
```

## Error Handling

### Error Codes

```c
typedef enum SyntaErrorCode {
    SyntaErrorCode_Success = 0,
    SyntaErrorCode_InvalidTag = 1,
    SyntaErrorCode_InvalidLength = 2,
    SyntaErrorCode_UnexpectedEof = 3,
    SyntaErrorCode_InvalidEncoding = 4,
    SyntaErrorCode_InvalidInteger = 5,
    SyntaErrorCode_InvalidOid = 6,
    SyntaErrorCode_InvalidUtf8 = 7,
    SyntaErrorCode_InvalidPrintableString = 8,
    SyntaErrorCode_InvalidIA5String = 9,
    SyntaErrorCode_InvalidBoolean = 10,
    SyntaErrorCode_InvalidTime = 11,
    SyntaErrorCode_InvalidBitString = 12,
    SyntaErrorCode_MaxDepthExceeded = 13,
    SyntaErrorCode_MaxSequenceElementsExceeded = 14,
    SyntaErrorCode_MaxLengthExceeded = 15,
    SyntaErrorCode_BerViolation = 16,
    SyntaErrorCode_DerViolation = 17,
    SyntaErrorCode_CerViolation = 18,
    SyntaErrorCode_NullPointer = 19,
    SyntaErrorCode_InvalidArgument = 20,
    SyntaErrorCode_OutOfMemory = 21,
    SyntaErrorCode_IntegerOverflow = 22,
    SyntaErrorCode_IntegerUnderflow = 23,
    SyntaErrorCode_Unknown = 999,
} SyntaErrorCode;
```

### Error Functions

#### synta_error_message

Get a static error message for an error code.

```c
const char *synta_error_message(SyntaErrorCode code);
```

**Parameters:**
- `code` - Error code

**Returns:** Static string describing the error (never NULL)

**Example:**
```c
SyntaErrorCode err = synta_decode_integer(decoder, &integer);
if (err != SyntaErrorCode_Success) {
    printf("Error: %s\n", synta_error_message(err));
}
```

#### synta_get_last_error_message

Get detailed error message for the last error (thread-local).

```c
const char *synta_get_last_error_message(void);
```

**Returns:** Detailed error message, or NULL if no error

**Example:**
```c
if (err != SyntaErrorCode_Success) {
    const char *msg = synta_get_last_error_message();
    if (msg) {
        printf("Detailed error: %s\n", msg);
    }
}
```

#### synta_clear_last_error

Clear the thread-local error message.

```c
void synta_clear_last_error(void);
```

## Decoder API

### Decoder Lifecycle

#### synta_decoder_new

Create a new decoder from a byte array.

```c
SyntaDecoder *synta_decoder_new(const uint8_t *data,
                                 size_t len,
                                 SyntaEncoding encoding);
```

**Parameters:**
- `data` - Pointer to encoded data (can be NULL if len is 0)
- `len` - Length of data in bytes
- `encoding` - Encoding rules (SyntaEncoding_Der, SyntaEncoding_Ber, or SyntaEncoding_Cer)

**Returns:** Decoder pointer, or NULL on error

**Example:**
```c
const uint8_t data[] = {0x02, 0x01, 0x2A}; // INTEGER 42
SyntaDecoder *decoder = synta_decoder_new(data, sizeof(data), SyntaEncoding_Der);
if (!decoder) {
    fprintf(stderr, "Failed to create decoder\n");
    return 1;
}
```

**Memory:** Returned decoder must be freed with `synta_decoder_free()`

#### synta_decoder_new_with_config

Create a decoder with custom configuration.

```c
SyntaDecoder *synta_decoder_new_with_config(const uint8_t *data,
                                             size_t len,
                                             SyntaEncoding encoding,
                                             const SyntaDecoderConfig *config);
```

**Parameters:**
- `data` - Pointer to encoded data
- `len` - Length of data
- `encoding` - Encoding rules
- `config` - Decoder configuration

**Example:**
```c
SyntaDecoderConfig config = {
    .max_depth = 32,
    .max_sequence_elements = 10000,
    .max_length = 16 * 1024 * 1024
};
SyntaDecoder *decoder = synta_decoder_new_with_config(
    data, len, SyntaEncoding_Der, &config);
```

#### synta_decoder_free

Free a decoder and all associated resources.

```c
void synta_decoder_free(SyntaDecoder *decoder);
```

**Parameters:**
- `decoder` - Decoder to free (can be NULL)

**Example:**
```c
synta_decoder_free(decoder);
decoder = NULL; // Good practice
```

### Decoder Utilities

#### synta_decoder_remaining

Get number of remaining bytes in the decoder.

```c
size_t synta_decoder_remaining(const SyntaDecoder *decoder);
```

**Returns:** Number of unread bytes

#### synta_decoder_at_end

Check if decoder is at end of input.

```c
bool synta_decoder_at_end(const SyntaDecoder *decoder);
```

**Returns:** true if no more data to read

#### synta_decoder_peek_tag

Peek at the next tag without consuming it.

```c
SyntaErrorCode synta_decoder_peek_tag(const SyntaDecoder *decoder,
                                       SyntaTag *out);
```

**Parameters:**
- `decoder` - Decoder to peek
- `out` - Output tag structure

**Returns:** SyntaErrorCode_Success or error code

**Example:**
```c
SyntaTag tag;
if (synta_decoder_peek_tag(decoder, &tag) == SyntaErrorCode_Success) {
    if (tag.class_ == SyntaTagClass_ContextSpecific && tag.number == 0) {
        // Handle optional [0] field
    }
}
```

### Primitive Type Decoding

#### synta_decode_boolean

Decode a BOOLEAN value.

```c
SyntaErrorCode synta_decode_boolean(SyntaDecoder *decoder, bool *out);
```

**Example:**
```c
bool value;
SyntaErrorCode err = synta_decode_boolean(decoder, &value);
if (err == SyntaErrorCode_Success) {
    printf("Boolean: %s\n", value ? "true" : "false");
}
```

#### synta_decode_integer

Decode an INTEGER value.

```c
SyntaErrorCode synta_decode_integer(SyntaDecoder *decoder,
                                     SyntaInteger **out);
```

**Example:**
```c
SyntaInteger *integer = NULL;
if (synta_decode_integer(decoder, &integer) == SyntaErrorCode_Success) {
    int64_t value;
    if (synta_integer_to_i64(integer, &value) == SyntaErrorCode_Success) {
        printf("Integer: %lld\n", (long long)value);
    }
    synta_integer_free(integer);
}
```

**Memory:** Returned integer must be freed with `synta_integer_free()`

#### synta_decode_null

Decode a NULL value.

```c
SyntaErrorCode synta_decode_null(SyntaDecoder *decoder);
```

#### synta_decode_octet_string

Decode an OCTET STRING.

```c
SyntaErrorCode synta_decode_octet_string(SyntaDecoder *decoder,
                                          SyntaOctetString **out);
```

**Memory:** Returned octet string must be freed with `synta_octet_string_free()`

#### synta_decode_bit_string

Decode a BIT STRING.

```c
SyntaErrorCode synta_decode_bit_string(SyntaDecoder *decoder,
                                        SyntaByteArray *out,
                                        uint8_t *unused_bits);
```

**Parameters:**
- `decoder` - Decoder
- `out` - Output byte array
- `unused_bits` - Number of unused bits in last byte (0-7)

**Memory:** Output byte array must be freed with `synta_byte_array_free()`

#### synta_decode_object_identifier

Decode an OBJECT IDENTIFIER.

```c
SyntaErrorCode synta_decode_object_identifier(SyntaDecoder *decoder,
                                                SyntaObjectIdentifier **out);
```

**Memory:** Returned OID must be freed with `synta_oid_free()`

### String Type Decoding

#### synta_decode_utf8_string

Decode a UTF8String.

```c
SyntaErrorCode synta_decode_utf8_string(SyntaDecoder *decoder,
                                         SyntaByteArray *out);
```

**Memory:** Output byte array must be freed with `synta_byte_array_free()`

#### synta_decode_printable_string

Decode a PrintableString.

```c
SyntaErrorCode synta_decode_printable_string(SyntaDecoder *decoder,
                                               SyntaByteArray *out);
```

#### synta_decode_ia5_string

Decode an IA5String.

```c
SyntaErrorCode synta_decode_ia5_string(SyntaDecoder *decoder,
                                        SyntaByteArray *out);
```

### Constructed Type Decoding

#### synta_decoder_enter_sequence

Enter a SEQUENCE and get a new decoder for its contents.

```c
SyntaErrorCode synta_decoder_enter_sequence(SyntaDecoder *decoder,
                                              SyntaDecoder **out);
```

**Example:**
```c
SyntaDecoder *seq_decoder = NULL;
if (synta_decoder_enter_sequence(decoder, &seq_decoder) == SyntaErrorCode_Success) {
    // Decode sequence contents
    while (!synta_decoder_at_end(seq_decoder)) {
        // Decode element
    }
    synta_decoder_free(seq_decoder);
}
```

**Memory:** Returned decoder must be freed with `synta_decoder_free()`

#### synta_decoder_enter_set

Enter a SET.

```c
SyntaErrorCode synta_decoder_enter_set(SyntaDecoder *decoder,
                                        SyntaDecoder **out);
```

#### synta_decoder_enter_constructed

Enter a constructed type with explicit tag.

```c
SyntaErrorCode synta_decoder_enter_constructed(SyntaDecoder *decoder,
                                                 SyntaTag tag,
                                                 SyntaDecoder **out);
```

**Example:**
```c
SyntaTag tag;
synta_decoder_peek_tag(decoder, &tag);
if (tag.class_ == SyntaTagClass_ContextSpecific && tag.number == 0) {
    SyntaDecoder *inner = NULL;
    synta_decoder_enter_constructed(decoder, tag, &inner);
    // Decode contents
    synta_decoder_free(inner);
}
```

## Encoder API

### Encoder Lifecycle

#### synta_encoder_new

Create a new encoder.

```c
SyntaEncoder *synta_encoder_new(SyntaEncoding encoding);
```

**Example:**
```c
SyntaEncoder *encoder = synta_encoder_new(SyntaEncoding_Der);
if (!encoder) {
    fprintf(stderr, "Failed to create encoder\n");
    return 1;
}
```

#### synta_encoder_finish

Finish encoding and get the encoded bytes.

```c
SyntaErrorCode synta_encoder_finish(SyntaEncoder *encoder,
                                     SyntaByteArray *out);
```

**Parameters:**
- `encoder` - Encoder to finish (consumed, do not use after)
- `out` - Output byte array

**Returns:** Error code

**Example:**
```c
SyntaByteArray output = {0};
if (synta_encoder_finish(encoder, &output) == SyntaErrorCode_Success) {
    // Use output.data and output.len
    synta_byte_array_free(&output);
}
// encoder is consumed, do not call synta_encoder_free()
```

**Memory:** Output byte array must be freed with `synta_byte_array_free()`. The encoder is consumed and must not be used or freed after this call.

#### synta_encoder_free

Free an encoder without finishing it.

```c
void synta_encoder_free(SyntaEncoder *encoder);
```

**Note:** Do not call this after `synta_encoder_finish()`.

### Primitive Type Encoding

#### synta_encode_boolean

Encode a BOOLEAN value.

```c
SyntaErrorCode synta_encode_boolean(SyntaEncoder *encoder, bool value);
```

#### synta_encode_integer_i64

Encode an INTEGER from int64_t.

```c
SyntaErrorCode synta_encode_integer_i64(SyntaEncoder *encoder, int64_t value);
```

**Example:**
```c
synta_encode_integer_i64(encoder, 42);
```

#### synta_encode_integer

Encode an INTEGER from Integer object.

```c
SyntaErrorCode synta_encode_integer(SyntaEncoder *encoder,
                                     const SyntaInteger *integer);
```

#### synta_encode_null

Encode a NULL value.

```c
SyntaErrorCode synta_encode_null(SyntaEncoder *encoder);
```

#### synta_encode_octet_string

Encode an OCTET STRING.

```c
SyntaErrorCode synta_encode_octet_string(SyntaEncoder *encoder,
                                          const uint8_t *data,
                                          size_t len);
```

#### synta_encode_bit_string

Encode a BIT STRING.

```c
SyntaErrorCode synta_encode_bit_string(SyntaEncoder *encoder,
                                        const uint8_t *data,
                                        size_t len,
                                        uint8_t unused_bits);
```

#### synta_encode_object_identifier

Encode an OBJECT IDENTIFIER.

```c
SyntaErrorCode synta_encode_object_identifier(SyntaEncoder *encoder,
                                                const SyntaObjectIdentifier *oid);
```

### String Type Encoding

#### synta_encode_utf8_string

Encode a UTF8String.

```c
SyntaErrorCode synta_encode_utf8_string(SyntaEncoder *encoder,
                                         const char *str);
```

#### synta_encode_printable_string

Encode a PrintableString.

```c
SyntaErrorCode synta_encode_printable_string(SyntaEncoder *encoder,
                                               const char *str);
```

#### synta_encode_ia5_string

Encode an IA5String.

```c
SyntaErrorCode synta_encode_ia5_string(SyntaEncoder *encoder,
                                        const char *str);
```

### Constructed Type Encoding

#### synta_encoder_start_sequence

Start encoding a SEQUENCE.

```c
SyntaErrorCode synta_encoder_start_sequence(SyntaEncoder *encoder,
                                              SyntaEncoder **out);
```

**Example:**
```c
SyntaEncoder *seq_encoder = NULL;
synta_encoder_start_sequence(encoder, &seq_encoder);
synta_encode_integer_i64(seq_encoder, 42);
synta_encode_boolean(seq_encoder, true);
synta_encoder_end_constructed(seq_encoder);
```

**Note:** The returned encoder pointer is the same as the input encoder.

#### synta_encoder_start_set

Start encoding a SET.

```c
SyntaErrorCode synta_encoder_start_set(SyntaEncoder *encoder,
                                        SyntaEncoder **out);
```

#### synta_encoder_start_constructed

Start encoding a constructed type with explicit tag.

```c
SyntaErrorCode synta_encoder_start_constructed(SyntaEncoder *encoder,
                                                 SyntaTag tag,
                                                 SyntaEncoder **out);
```

#### synta_encoder_end_constructed

End the current constructed type.

```c
SyntaErrorCode synta_encoder_end_constructed(SyntaEncoder *encoder);
```

**Important:** Must be called for each `start_*` call to properly encode lengths.

## Helper Types

### Integer Helpers

#### synta_integer_new_i64

Create an Integer from int64_t.

```c
SyntaInteger *synta_integer_new_i64(int64_t value);
```

#### synta_integer_new_u64

Create an Integer from uint64_t.

```c
SyntaInteger *synta_integer_new_u64(uint64_t value);
```

#### synta_integer_new_bytes

Create an Integer from bytes.

```c
SyntaInteger *synta_integer_new_bytes(const uint8_t *data,
                                       size_t len,
                                       bool is_negative);
```

#### synta_integer_to_i64

Convert Integer to int64_t.

```c
SyntaErrorCode synta_integer_to_i64(const SyntaInteger *integer,
                                     int64_t *out);
```

Returns `SyntaErrorCode_IntegerOverflow` if value doesn't fit.

#### synta_integer_to_u64

Convert Integer to uint64_t.

```c
SyntaErrorCode synta_integer_to_u64(const SyntaInteger *integer,
                                     uint64_t *out);
```

#### synta_integer_to_bytes

Get Integer as bytes.

```c
SyntaErrorCode synta_integer_to_bytes(const SyntaInteger *integer,
                                       SyntaByteArray *out);
```

#### synta_integer_is_negative

Check if Integer is negative.

```c
bool synta_integer_is_negative(const SyntaInteger *integer);
```

#### synta_integer_free

Free an Integer.

```c
void synta_integer_free(SyntaInteger *integer);
```

### OctetString Helpers

#### synta_octet_string_new

Create an OctetString.

```c
SyntaOctetString *synta_octet_string_new(const uint8_t *data, size_t len);
```

#### synta_octet_string_data

Get OctetString data pointer.

```c
const uint8_t *synta_octet_string_data(const SyntaOctetString *octet_string);
```

#### synta_octet_string_len

Get OctetString length.

```c
size_t synta_octet_string_len(const SyntaOctetString *octet_string);
```

#### synta_octet_string_free

Free an OctetString.

```c
void synta_octet_string_free(SyntaOctetString *octet_string);
```

### ObjectIdentifier Helpers

#### synta_oid_new

Create an OID from components.

```c
SyntaObjectIdentifier *synta_oid_new(const uint32_t *components, size_t len);
```

#### synta_oid_from_string

Create an OID from string (e.g., "1.2.840.113549.1.1.1").

```c
SyntaObjectIdentifier *synta_oid_from_string(const char *str);
```

#### synta_oid_to_string

Convert OID to string.

```c
size_t synta_oid_to_string(const SyntaObjectIdentifier *oid,
                            char *buffer,
                            size_t buffer_len);
```

**Returns:** Number of characters written (excluding null terminator), or 0 if buffer too small.

#### synta_oid_components

Get OID components array.

```c
void synta_oid_components(const SyntaObjectIdentifier *oid,
                          const uint32_t **components,
                          size_t *len);
```

#### synta_oid_equals

Compare two OIDs for equality.

```c
bool synta_oid_equals(const SyntaObjectIdentifier *a,
                      const SyntaObjectIdentifier *b);
```

#### synta_oid_free

Free an OID.

```c
void synta_oid_free(SyntaObjectIdentifier *oid);
```

### ByteArray Helpers

#### synta_byte_array_free

Free a byte array (if owned).

```c
void synta_byte_array_free(SyntaByteArray *array);
```

**Note:** Only frees if `array->owned` is non-zero (1 = owned, 0 = borrowed).

#### synta_byte_array_clone

Clone a byte array.

```c
SyntaByteArray synta_byte_array_clone(const SyntaByteArray *array);
```

## X.509 PKI APIs

### CRL API

Parse and inspect X.509 Certificate Revocation Lists (RFC 5280 §5).

All getter functions return a borrowed `SyntaByteArray` (`owned == 0`) that
points into the parsed CRL object.  The slice is valid while the `SyntaCrl *`
handle is alive.  Do not call `synta_byte_array_free()` on borrowed arrays.

#### synta_crl_parse_der

Parse an X.509 CRL from DER bytes.  Returns NULL on error.

```c
SyntaCrl *synta_crl_parse_der(const uint8_t *data, uintptr_t len);
```

#### synta_crl_parse_pem

Parse an X.509 CRL from PEM bytes (first block only).  Returns NULL on error.

```c
SyntaCrl *synta_crl_parse_pem(const uint8_t *data, uintptr_t len);
```

#### synta_crl_free

Free a CRL handle.  Must be called exactly once per handle.  Safe to call with NULL.

```c
void synta_crl_free(SyntaCrl *crl);
```

#### synta_crl_get_issuer_der

Return the CRL issuer Name as raw DER bytes (SEQUENCE tag).

```c
SyntaErrorCode synta_crl_get_issuer_der(const SyntaCrl *crl, SyntaByteArray *out);
```

#### synta_crl_get_signature_algorithm_der

Return the outer `signatureAlgorithm` AlgorithmIdentifier as raw DER bytes.

```c
SyntaErrorCode synta_crl_get_signature_algorithm_der(const SyntaCrl *crl,
                                                       SyntaByteArray *out);
```

#### synta_crl_get_signature_value_der

Return the `signatureValue` BIT STRING (with tag) as raw DER bytes.

```c
SyntaErrorCode synta_crl_get_signature_value_der(const SyntaCrl *crl, SyntaByteArray *out);
```

#### synta_crl_get_this_update_der

Return the `thisUpdate` Time as raw DER bytes (UTCTime or GeneralizedTime tag).

```c
SyntaErrorCode synta_crl_get_this_update_der(const SyntaCrl *crl, SyntaByteArray *out);
```

#### synta_crl_get_next_update_der

Return the `nextUpdate` Time as raw DER bytes, or a zero-length array (`out->len == 0`)
if `nextUpdate` is absent.

```c
SyntaErrorCode synta_crl_get_next_update_der(const SyntaCrl *crl, SyntaByteArray *out);
```

#### synta_crl_get_revoked_count

Return the number of entries in `revokedCertificates` (0 when absent or empty).

```c
SyntaErrorCode synta_crl_get_revoked_count(const SyntaCrl *crl, uintptr_t *out);
```

---

### CSR API

Parse and inspect PKCS#10 Certificate Signing Requests (RFC 2986).

#### synta_csr_parse_der

Parse a CSR from DER bytes.  Returns NULL on error.

```c
SyntaCsr *synta_csr_parse_der(const uint8_t *data, uintptr_t len);
```

#### synta_csr_parse_pem

Parse a CSR from PEM bytes (first block only).  Returns NULL on error.

```c
SyntaCsr *synta_csr_parse_pem(const uint8_t *data, uintptr_t len);
```

#### synta_csr_free

Free a CSR handle.

```c
void synta_csr_free(SyntaCsr *csr);
```

#### synta_csr_get_version

Return the CSR version field (PKCS#10 defines only version 0).

```c
SyntaErrorCode synta_csr_get_version(const SyntaCsr *csr, int64_t *out);
```

#### synta_csr_get_subject_der

Return the subject Name as raw DER bytes.

```c
SyntaErrorCode synta_csr_get_subject_der(const SyntaCsr *csr, SyntaByteArray *out);
```

#### synta_csr_get_signature_algorithm_der

Return the signature AlgorithmIdentifier as raw DER bytes.

```c
SyntaErrorCode synta_csr_get_signature_algorithm_der(const SyntaCsr *csr,
                                                       SyntaByteArray *out);
```

#### synta_csr_get_signature_der

Return the signature BIT STRING (with tag) as raw DER bytes.

```c
SyntaErrorCode synta_csr_get_signature_der(const SyntaCsr *csr, SyntaByteArray *out);
```

#### synta_csr_get_public_key_der

Return the SubjectPublicKeyInfo as raw DER bytes.

```c
SyntaErrorCode synta_csr_get_public_key_der(const SyntaCsr *csr, SyntaByteArray *out);
```

---

### OCSP API

Parse and inspect OCSP responses (RFC 6960).

#### synta_ocsp_parse_der

Parse an OCSP response from DER bytes.  Returns NULL on error.

```c
SyntaOcsp *synta_ocsp_parse_der(const uint8_t *data, uintptr_t len);
```

#### synta_ocsp_free

Free an OCSP response handle.

```c
void synta_ocsp_free(SyntaOcsp *ocsp);
```

#### synta_ocsp_get_status_code

Return the numeric `OCSPResponseStatus` value:
`0` = successful, `1` = malformedRequest, `2` = internalError,
`3` = tryLater, `5` = sigRequired, `6` = unauthorized.

```c
SyntaErrorCode synta_ocsp_get_status_code(const SyntaOcsp *ocsp, int32_t *out);
```

#### synta_ocsp_get_status

Return the status as a NUL-terminated string (e.g. `"successful"`) using the
size-query pattern: call with `buffer == NULL` to get the required length (including
NUL), then call again with an allocated buffer.  Returns 0 when `ocsp` is NULL.

```c
uintptr_t synta_ocsp_get_status(const SyntaOcsp *ocsp, char *buffer, uintptr_t buffer_len);
```

#### synta_ocsp_get_response_type_oid

Return the `responseType` OID string using the size-query pattern.
Returns 0 when status is not `successful` or `ocsp` is NULL.

```c
uintptr_t synta_ocsp_get_response_type_oid(const SyntaOcsp *ocsp, char *buffer, uintptr_t buffer_len);
```

#### synta_ocsp_get_response_bytes_der

Return the OCTET STRING content of `responseBytes`.  `out->len == 0` when absent.

```c
SyntaErrorCode synta_ocsp_get_response_bytes_der(const SyntaOcsp *ocsp, SyntaByteArray *out);
```

---

### PEM API

Dependency-free PEM encode/decode following RFC 7468.

#### synta_pem_to_der

Decode all PEM blocks in `data`.  Returns an opaque `SyntaDerList *`; NULL on error.
Free with `synta_der_list_free()`.

```c
SyntaDerList *synta_pem_to_der(const uint8_t *data, uintptr_t len);
```

#### synta_der_list_free

Free a DER list.  Safe to call with NULL.

```c
void synta_der_list_free(SyntaDerList *list);
```

#### synta_der_list_count

Return the number of DER entries in the list.

```c
uintptr_t synta_der_list_count(const SyntaDerList *list);
```

#### synta_der_list_get_der

Return the `index`-th DER entry as a borrowed `SyntaByteArray` (`owned == 0`).
Valid while the `SyntaDerList *` is alive.

```c
SyntaErrorCode synta_der_list_get_der(const SyntaDerList *list, uintptr_t index,
                                       SyntaByteArray *out);
```

#### synta_der_to_pem

Encode `der` bytes as a PEM block with `label` (NUL-terminated, e.g. `"CERTIFICATE"`).
Returns an **owned** `SyntaByteArray` (`owned != 0`).  Caller must call
`synta_byte_array_free()` when done.

```c
SyntaByteArray synta_der_to_pem(const uint8_t *data, uintptr_t len, const char *label);
```

---

### PKCS#7 and PKCS#12 API

Extract X.509 certificates from container formats.

#### synta_read_pki_file

Auto-detect the encoding of `data` (PEM, PKCS#7, PKCS#12, or raw DER) and
return every X.509 certificate as a DER blob.  This is the recommended
one-stop function when you do not know the input format in advance.

`password` applies to PKCS#12 archives; pass NULL or a zero-length slice for
password-less archives.  Encrypted PKCS#12 bags are silently skipped (the C
FFI layer does not bundle an OpenSSL backend); unencrypted certificates in the
same archive are still returned.  Returns NULL on hard structural failure.

```c
SyntaDerList *synta_read_pki_file(const uint8_t *data, uintptr_t len,
                                   const uint8_t *password,
                                   uintptr_t password_len);
```

Example:

```c
uint8_t *buf; size_t n; /* ... read file ... */
SyntaDerList *list = synta_read_pki_file(buf, n, NULL, 0);
if (!list) { fprintf(stderr, "%s\n", synta_get_last_error_message()); }
size_t count = synta_der_list_count(list);
for (size_t i = 0; i < count; i++) {
    SyntaByteArray der = {0};
    synta_der_list_get_der(list, i, &der);
    /* use der.data / der.len */
}
synta_der_list_free(list);
```

#### synta_pkcs7_certs_from_der

Extract DER certificates from a PKCS#7 `SignedData` blob (DER or BER input).
Returns an owned `SyntaDerList *`; NULL on error.

```c
SyntaDerList *synta_pkcs7_certs_from_der(const uint8_t *data, uintptr_t len);
```

#### synta_pkcs7_certs_from_pem

Same as `synta_pkcs7_certs_from_der` but first decodes the first PEM block.

```c
SyntaDerList *synta_pkcs7_certs_from_pem(const uint8_t *data, uintptr_t len);
```

#### synta_pkcs12_certs_from_der

Extract DER certificates from a PKCS#12 PFX archive.  `password` may be NULL for
unencrypted archives.  Encrypted bags require the library to have been built with
the `openssl` feature enabled; without it, encrypted bags return an error.

```c
SyntaDerList *synta_pkcs12_certs_from_der(const uint8_t *data, uintptr_t len,
                                            const uint8_t *password,
                                            uintptr_t password_len);
```

---

## CMS API

Parse and inspect Cryptographic Message Syntax (CMS, RFC 5652) structures:
`SignedData`, `EnvelopedData`, `EncryptedData`, and `DigestedData`.

The recommended entry point is `synta_cms_content_info_parse_der`, which parses the
outer `ContentInfo` SEQUENCE and gives access to the inner content type through
borrowed pointers.  Alternatively, `synta_cms_encrypted_data_parse` can parse a
bare `EncryptedData` SEQUENCE directly.

**Ownership model:** Pointers returned by `synta_cms_content_info_get0_*` functions
are **borrowed** from the `SyntaCmsContentInfo` handle.  Do not free them separately.
Free only the `SyntaCmsContentInfo` itself with `synta_cms_content_info_free`.

### ContentInfo API

#### synta_cms_content_info_parse_der

Parse a DER-encoded `ContentInfo` SEQUENCE (RFC 5652 §3).  Returns NULL on error.

```c
SyntaCmsContentInfo *synta_cms_content_info_parse_der(const uint8_t *data,
                                                       uintptr_t len);
```

**Memory:** Must be freed with `synta_cms_content_info_free()`.

**Example:**
```c
SyntaCmsContentInfo *ci = synta_cms_content_info_parse_der(buf, len);
if (!ci) {
    fprintf(stderr, "%s\n", synta_get_last_error_message());
    return 1;
}
// Access inner data via get0_* functions ...
synta_cms_content_info_free(ci);
```

#### synta_cms_content_info_free

Free a `SyntaCmsContentInfo` handle.  Safe to call with NULL.

```c
void synta_cms_content_info_free(SyntaCmsContentInfo *ci);
```

#### synta_cms_content_info_get_content_type

Return the `contentType` OID string using the size-query pattern: call with
`buffer == NULL` to get the required length (including NUL), then call again
with an allocated buffer.  Returns 0 when `ci` is NULL.

```c
uintptr_t synta_cms_content_info_get_content_type(const SyntaCmsContentInfo *ci,
                                                    char *buffer,
                                                    uintptr_t buffer_len);
```

Known content type OIDs:

| OID                        | Name               |
|----------------------------|--------------------|
| `1.2.840.113549.1.7.1`     | `id-data`          |
| `1.2.840.113549.1.7.2`     | `id-signedData`    |
| `1.2.840.113549.1.7.3`     | `id-envelopedData` |
| `1.2.840.113549.1.7.5`     | `id-digestedData`  |
| `1.2.840.113549.1.7.6`     | `id-encryptedData` |

#### synta_cms_content_info_get0_signed_data

Return a borrowed pointer to the inner `SignedData` when the content type is
`id-signedData`; returns NULL for any other content type.  The pointer is valid
until `synta_cms_content_info_free` is called.

```c
const SyntaCmsSignedData *synta_cms_content_info_get0_signed_data(
    const SyntaCmsContentInfo *ci);
```

#### synta_cms_content_info_get0_enveloped_data

Return a borrowed pointer to the inner `EnvelopedData` when the content type is
`id-envelopedData`; NULL otherwise.

```c
const SyntaCmsEnvelopedData *synta_cms_content_info_get0_enveloped_data(
    const SyntaCmsContentInfo *ci);
```

#### synta_cms_content_info_get0_encrypted_data

Return a borrowed pointer to the inner `EncryptedData` when the content type is
`id-encryptedData`; NULL otherwise.

```c
const SyntaCmsEncryptedData *synta_cms_content_info_get0_encrypted_data(
    const SyntaCmsContentInfo *ci);
```

#### synta_cms_content_info_get0_digested_data

Return a borrowed pointer to the inner `DigestedData` when the content type is
`id-digestedData`; NULL otherwise.

```c
const SyntaCmsDigestedData *synta_cms_content_info_get0_digested_data(
    const SyntaCmsContentInfo *ci);
```

---

### SignedData API

Inspect a `SignedData` structure (RFC 5652 §5).  Obtain a `SyntaCmsSignedData *`
via `synta_cms_content_info_get0_signed_data`; it is a borrowed pointer owned by
the `SyntaCmsContentInfo` and must not be freed separately.

```c
// CMS version field
SyntaErrorCode synta_cms_signed_data_get_version(
    const SyntaCmsSignedData *sd, int32_t *out);

// encapContentInfo contentType OID (size-query pattern)
uintptr_t synta_cms_signed_data_get_encap_content_type(
    const SyntaCmsSignedData *sd, char *buffer, uintptr_t buffer_len);

// encapContentInfo eContent raw bytes (borrowed SyntaByteArray; out->len==0 if absent)
SyntaErrorCode synta_cms_signed_data_get_encap_content(
    const SyntaCmsSignedData *sd, SyntaByteArray *out);

// All embedded certificates as DER (borrowed SyntaByteArray; out->len==0 if absent)
SyntaErrorCode synta_cms_signed_data_get_certificates_der(
    const SyntaCmsSignedData *sd, SyntaByteArray *out);

// CRLs DER (borrowed SyntaByteArray; out->len==0 if absent)
SyntaErrorCode synta_cms_signed_data_get_crls_der(
    const SyntaCmsSignedData *sd, SyntaByteArray *out);

// Number of SignerInfo entries
SyntaErrorCode synta_cms_signed_data_get_signer_info_count(
    const SyntaCmsSignedData *sd, uintptr_t *out);

// DER bytes for the index-th SignerInfo (borrowed; pass to synta_cms_signer_info_parse_der)
SyntaErrorCode synta_cms_signed_data_get_signer_info_der(
    const SyntaCmsSignedData *sd, uintptr_t index, SyntaByteArray *out);
```

---

### SignerInfo API

Parse an individual `SignerInfo` structure (RFC 5652 §5.3) from its DER bytes.
Obtain the DER bytes from `synta_cms_signed_data_get_signer_info_der`.

#### synta_cms_signer_info_parse_der

```c
SyntaCmsSignerInfo *synta_cms_signer_info_parse_der(const uint8_t *data, uintptr_t len);
```

**Memory:** Must be freed with `synta_cms_signer_info_free()`.

#### synta_cms_signer_info_free

```c
void synta_cms_signer_info_free(SyntaCmsSignerInfo *si);
```

**Getters** (all return borrowed `SyntaByteArray` unless noted):

```c
SyntaErrorCode synta_cms_signer_info_get_version(
    const SyntaCmsSignerInfo *si, int32_t *out);

// SID: IssuerAndSerialNumber or SubjectKeyIdentifier DER
SyntaErrorCode synta_cms_signer_info_get_sid_der(
    const SyntaCmsSignerInfo *si, SyntaByteArray *out);

// digestAlgorithm AlgorithmIdentifier DER
SyntaErrorCode synta_cms_signer_info_get_digest_algorithm_der(
    const SyntaCmsSignerInfo *si, SyntaByteArray *out);

// digestAlgorithm OID string (size-query pattern)
uintptr_t synta_cms_signer_info_get_digest_algorithm_oid(
    const SyntaCmsSignerInfo *si, char *buffer, uintptr_t buffer_len);

// signedAttrs DER (out->len==0 if absent)
SyntaErrorCode synta_cms_signer_info_get_signed_attrs_der(
    const SyntaCmsSignerInfo *si, SyntaByteArray *out);

// signatureAlgorithm AlgorithmIdentifier DER
SyntaErrorCode synta_cms_signer_info_get_signature_algorithm_der(
    const SyntaCmsSignerInfo *si, SyntaByteArray *out);

// signatureAlgorithm OID string (size-query pattern)
uintptr_t synta_cms_signer_info_get_signature_algorithm_oid(
    const SyntaCmsSignerInfo *si, char *buffer, uintptr_t buffer_len);

// signature bytes
SyntaErrorCode synta_cms_signer_info_get_signature(
    const SyntaCmsSignerInfo *si, SyntaByteArray *out);

// unsignedAttrs DER (out->len==0 if absent)
SyntaErrorCode synta_cms_signer_info_get_unsigned_attrs_der(
    const SyntaCmsSignerInfo *si, SyntaByteArray *out);
```

---

### EnvelopedData API

Inspect an `EnvelopedData` structure (RFC 5652 §6).  Obtain via
`synta_cms_content_info_get0_enveloped_data`; the pointer is borrowed.

```c
SyntaErrorCode synta_cms_enveloped_data_get_version(
    const SyntaCmsEnvelopedData *ed, int32_t *out);

// All RecipientInfo entries as a single DER blob
SyntaErrorCode synta_cms_enveloped_data_get_recipient_infos_der(
    const SyntaCmsEnvelopedData *ed, SyntaByteArray *out);

// encryptedContentInfo contentType OID (size-query pattern)
uintptr_t synta_cms_enveloped_data_get_content_type(
    const SyntaCmsEnvelopedData *ed, char *buffer, uintptr_t buffer_len);

// contentEncryptionAlgorithm AlgorithmIdentifier DER
SyntaErrorCode synta_cms_enveloped_data_get_content_encryption_algorithm_der(
    const SyntaCmsEnvelopedData *ed, SyntaByteArray *out);

// contentEncryptionAlgorithm OID string (size-query pattern)
uintptr_t synta_cms_enveloped_data_get_content_encryption_algorithm_oid(
    const SyntaCmsEnvelopedData *ed, char *buffer, uintptr_t buffer_len);

// encryptedContent bytes (out->len==0 if absent)
SyntaErrorCode synta_cms_enveloped_data_get_encrypted_content(
    const SyntaCmsEnvelopedData *ed, SyntaByteArray *out);
```

---

### EncryptedData API

Inspect and operate on an `EncryptedData` structure (RFC 5652 §8).  Obtain via
`synta_cms_content_info_get0_encrypted_data` (borrowed) or by parsing a bare
`EncryptedData` SEQUENCE with `synta_cms_encrypted_data_parse`.

#### synta_cms_encrypted_data_parse

Parse a bare `EncryptedData` SEQUENCE directly (without a `ContentInfo` wrapper).

```c
SyntaErrorCode synta_cms_encrypted_data_parse(const uint8_t *data, uintptr_t len,
                                               SyntaCmsEncryptedData **out);
```

**Memory:** Returned handle must be freed with `synta_cms_encrypted_data_free()`.

#### synta_cms_encrypted_data_free

```c
void synta_cms_encrypted_data_free(SyntaCmsEncryptedData *ed);
```

**Getters** (all return borrowed `SyntaByteArray` unless noted):

```c
SyntaErrorCode synta_cms_encrypted_data_get_version(
    const SyntaCmsEncryptedData *ed, int32_t *out);

// encryptedContentInfo contentType OID (size-query pattern)
uintptr_t synta_cms_encrypted_data_get_content_type(
    const SyntaCmsEncryptedData *ed, char *buffer, uintptr_t buffer_len);

// contentEncryptionAlgorithm AlgorithmIdentifier DER
SyntaErrorCode synta_cms_encrypted_data_get_content_encryption_algorithm_der(
    const SyntaCmsEncryptedData *ed, SyntaByteArray *out);

// contentEncryptionAlgorithm OID string (size-query pattern)
uintptr_t synta_cms_encrypted_data_get_content_encryption_algorithm_oid(
    const SyntaCmsEncryptedData *ed, char *buffer, uintptr_t buffer_len);

// encryptedContent bytes (out->len==0 if absent)
SyntaErrorCode synta_cms_encrypted_data_get_encrypted_content(
    const SyntaCmsEncryptedData *ed, SyntaByteArray *out);
```

#### synta_cms_encrypted_data_decrypt

Decrypt the `EncryptedData` content under a raw symmetric key.  Requires the
library to be built with the `openssl` Cargo feature.

```c
SyntaErrorCode synta_cms_encrypted_data_decrypt(const SyntaCmsEncryptedData *ed,
                                                 const uint8_t *key, uintptr_t key_len,
                                                 SyntaByteArray *out);
```

**Returns:** Plaintext as an owned `SyntaByteArray`.  Caller must call
`synta_byte_array_free()` when done.

#### synta_cms_encrypted_data_create

Encrypt `plaintext` under `key` and produce a DER-encoded `EncryptedData` SEQUENCE.
A fresh random IV is generated for each call.  Requires the `openssl` feature.

```c
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,
    SyntaByteArray *der_out);
```

- `content_type_oid_der` — DER-encoded OID TLV for the content type (`id-data` if NULL).
- `enc_alg_oid_der` — DER-encoded OID TLV for the content-encryption algorithm (e.g. AES-128-CBC).

**Returns:** DER as an owned `SyntaByteArray`.  Caller must call `synta_byte_array_free()`.

**Example:**
```c
// AES-128-CBC OID: 2.16.840.1.101.3.4.1.2
static const uint8_t AES128_CBC_OID[] = {
    0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x02
};
uint8_t key[16] = { /* 128-bit key */ };
SyntaByteArray der = {0};
SyntaErrorCode rc = synta_cms_encrypted_data_create(
    plaintext, plaintext_len, key, sizeof(key),
    NULL, 0,                        // id-data content type
    AES128_CBC_OID, sizeof(AES128_CBC_OID),
    &der);
if (rc == SyntaErrorCode_Success) {
    /* use der.data / der.len */
    synta_byte_array_free(&der);
}
```

---

### DigestedData API

Inspect a `DigestedData` structure (RFC 5652 §7).  Obtain via
`synta_cms_content_info_get0_digested_data`; the pointer is borrowed.

```c
SyntaErrorCode synta_cms_digested_data_get_version(
    const SyntaCmsDigestedData *dd, int32_t *out);

// digestAlgorithm AlgorithmIdentifier DER
SyntaErrorCode synta_cms_digested_data_get_digest_algorithm_der(
    const SyntaCmsDigestedData *dd, SyntaByteArray *out);

// digestAlgorithm OID string (size-query pattern)
uintptr_t synta_cms_digested_data_get_digest_algorithm_oid(
    const SyntaCmsDigestedData *dd, char *buffer, uintptr_t buffer_len);

// encapContentInfo contentType OID (size-query pattern)
uintptr_t synta_cms_digested_data_get_encap_content_type(
    const SyntaCmsDigestedData *dd, char *buffer, uintptr_t buffer_len);

// encapContentInfo eContent raw bytes (out->len==0 if absent)
SyntaErrorCode synta_cms_digested_data_get_encap_content(
    const SyntaCmsDigestedData *dd, SyntaByteArray *out);

// digest OCTET STRING bytes
SyntaErrorCode synta_cms_digested_data_get_digest(
    const SyntaCmsDigestedData *dd, SyntaByteArray *out);
```

---

## Thread Safety

- **Encoder/Decoder:** Not thread-safe. Each thread must use its own encoder/decoder instances.
- **Error Messages:** Thread-local. `synta_get_last_error_message()` and `synta_clear_last_error()` are thread-safe.
- **Helper Functions:** Thread-safe for read-only operations. Mutations require external synchronization.

## See Also

- [Memory Management Guide]C_MEMORY.md
- [Migration from OpenSSL]MIGRATION_OPENSSL.md
- [Migration from libtasn1]MIGRATION_LIBTASN1.md
- [C Examples]../synta-ffi/examples/c/
- [C Integration Tests]../tests/c/