opentdf 0.18.0

Rust implementation of OpenTDF (Trusted Data Format)
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
//! TDF-CBOR format implementation (per TDF-CBOR specification draft-00)
//!
//! This module provides support for CBOR-encoded TDF payloads optimized for
//! binary protocols and size-constrained environments.
//!
//! # Overview
//!
//! TDF-CBOR defines a CBOR-based container format for TDF that:
//! - Uses integer keys for compact encoding
//! - Stores binary payloads directly (no base64)
//! - Includes self-describe CBOR tag for format detection
//!
//! # Example
//!
//! ```rust,no_run
//! # #[cfg(feature = "cbor")]
//! use opentdf::{Policy, tdf_cbor::TdfCbor};
//!
//! # #[cfg(feature = "cbor")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a policy
//! let policy = opentdf::Policy::new(
//!     uuid::Uuid::new_v4().to_string(),
//!     vec![],
//!     vec!["user@example.com".to_string()]
//! );
//!
//! // Encrypt data and get TDF-CBOR container
//! let container = TdfCbor::encrypt(b"Sensitive data")
//!     .kas_url("https://kas.example.com")
//!     .policy(policy)
//!     .build()?;
//!
//! // Serialize to CBOR bytes
//! let cbor_bytes = container.to_bytes()?;
//!
//! // Later: parse from bytes
//! let parsed = TdfCbor::from_bytes(&cbor_bytes)?;
//! assert_eq!(parsed.tdf, "cbor");
//! assert_eq!(parsed.version, [1, 0, 0]);
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "cbor"))]
//! # fn main() {}
//! ```

use crate::manifest::{
    Assertion, EncryptionInformation, EncryptionMethod, IntegrityInformation,
    IntegrityInformationExt, KeyAccess, RootSignature, Segment,
};
use crate::policy::Policy;
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
use opentdf_crypto::{EncryptionError, TdfEncryption, calculate_policy_binding};
use serde::{Deserialize, Serialize};

/// AES-256-GCM nonce length in bytes; `method.iv` must carry at least this many.
const GCM_NONCE_LENGTH: usize = 12;

// ============================================================================
// TDF-CBOR Magic Bytes
// ============================================================================

/// Self-describe CBOR tag (55799) + map marker
/// D9 D9F7 = tag(55799)
/// A5 = map(5) for 5 top-level elements
pub const CBOR_MAGIC: [u8; 4] = [0xD9, 0xD9, 0xF7, 0xA5];

/// Integer key mappings per TDF-CBOR spec section 3.1
pub mod key {
    pub const TDF: u64 = 1;
    pub const VERSION: u64 = 2;
    pub const CREATED: u64 = 3;
    pub const MANIFEST: u64 = 4;
    pub const PAYLOAD: u64 = 5;
}

/// Payload integer key mappings per TDF-CBOR spec section 3.1
pub mod payload_key {
    pub const TYPE: u64 = 1;
    pub const PROTOCOL: u64 = 2;
    pub const MIME_TYPE: u64 = 3;
    pub const IS_ENCRYPTED: u64 = 4;
    pub const LENGTH: u64 = 5;
    pub const VALUE: u64 = 6;
}

/// Enumerated values per TDF-CBOR spec section 1.5
pub mod enums {
    // Payload type: 0=inline, 1=reference
    pub const PAYLOAD_TYPE_INLINE: u64 = 0;
    #[allow(dead_code)]
    pub const PAYLOAD_TYPE_REFERENCE: u64 = 1;

    // Payload protocol: 0=binary, 1=binary-chunked
    pub const PAYLOAD_PROTOCOL_BINARY: u64 = 0;
    #[allow(dead_code)]
    pub const PAYLOAD_PROTOCOL_BINARY_CHUNKED: u64 = 1;

    // Encryption type: 0=split
    pub const ENCRYPTION_TYPE_SPLIT: u64 = 0;

    // Key access type: 0=wrapped, 1=remote
    pub const KEY_ACCESS_TYPE_WRAPPED: u64 = 0;
    #[allow(dead_code)]
    pub const KEY_ACCESS_TYPE_REMOTE: u64 = 1;

    // Key protocol: 0=kas
    pub const KEY_PROTOCOL_KAS: u64 = 0;

    // Symmetric algorithm: 0=AES-256-GCM
    pub const SYMMETRIC_ALG_AES_256_GCM: u64 = 0;

    // Hash/Signature algorithm
    pub const HASH_ALG_HS256: u64 = 0;
    #[allow(dead_code)]
    pub const HASH_ALG_HS384: u64 = 1;
    #[allow(dead_code)]
    pub const HASH_ALG_HS512: u64 = 2;
    pub const HASH_ALG_GMAC: u64 = 3;
    #[allow(dead_code)]
    pub const HASH_ALG_SHA256: u64 = 4;
    #[allow(dead_code)]
    pub const HASH_ALG_ES256: u64 = 5;
    #[allow(dead_code)]
    pub const HASH_ALG_ES384: u64 = 6;
    #[allow(dead_code)]
    pub const HASH_ALG_ES512: u64 = 7;
}

/// Manifest integer key mappings per TDF-CBOR spec section 3.1
pub mod manifest_key {
    pub const ENCRYPTION_INFORMATION: u64 = 1;
    #[allow(dead_code)]
    pub const ASSERTIONS: u64 = 2;
}

/// EncryptionInformation integer key mappings
pub mod enc_info_key {
    pub const TYPE: u64 = 1;
    pub const KEY_ACCESS: u64 = 2;
    pub const METHOD: u64 = 3;
    pub const INTEGRITY_INFORMATION: u64 = 4;
    pub const POLICY: u64 = 5;
}

/// KeyAccess integer key mappings
pub mod key_access_key {
    pub const TYPE: u64 = 1;
    pub const URL: u64 = 2;
    pub const PROTOCOL: u64 = 3;
    pub const WRAPPED_KEY: u64 = 4;
    pub const POLICY_BINDING: u64 = 5;
    #[allow(dead_code)]
    pub const ENCRYPTED_METADATA: u64 = 6;
    pub const KID: u64 = 7;
    pub const EPHEMERAL_PUBLIC_KEY: u64 = 8;
    pub const SCHEMA_VERSION: u64 = 9;
}

/// PolicyBinding integer key mappings
pub mod policy_binding_key {
    pub const ALG: u64 = 1;
    pub const HASH: u64 = 2;
}

/// Method integer key mappings
pub mod method_key {
    pub const ALGORITHM: u64 = 1;
    pub const IV: u64 = 2;
    pub const IS_STREAMABLE: u64 = 3;
}

/// IntegrityInformation integer key mappings
pub mod integrity_key {
    pub const ROOT_SIGNATURE: u64 = 1;
    pub const SEGMENT_HASH_ALG: u64 = 2;
    pub const SEGMENTS: u64 = 3;
    pub const SEGMENT_SIZE_DEFAULT: u64 = 4;
    pub const ENCRYPTED_SEGMENT_SIZE_DEFAULT: u64 = 5;
}

/// RootSignature integer key mappings
pub mod root_sig_key {
    pub const ALG: u64 = 1;
    pub const SIG: u64 = 2;
}

/// Segment integer key mappings
pub mod segment_key {
    pub const HASH: u64 = 1;
    pub const SEGMENT_SIZE: u64 = 2;
    pub const ENCRYPTED_SEGMENT_SIZE: u64 = 3;
}

// ============================================================================
// TDF-CBOR Spec-Compliant Types
// ============================================================================

/// TDF-CBOR envelope for binary payload transmission (spec-compliant)
///
/// This structure represents a complete TDF-CBOR package per the TDF-CBOR
/// specification draft-00. The format uses integer keys and binary payloads
/// for optimal size and parsing efficiency.
///
/// # Integer Key Mapping
///
/// | Key | Field    | Type                |
/// |-----|----------|---------------------|
/// | 1   | tdf      | string "cbor"       |
/// | 2   | version  | [u8; 3] semver      |
/// | 3   | created  | u64 Unix timestamp  |
/// | 4   | manifest | TdfCborManifest     |
/// | 5   | payload  | CborPayload         |
#[derive(Debug, Clone)]
pub struct TdfCbor {
    /// Format identifier. MUST be "cbor" for TDF-CBOR documents.
    pub tdf: String,

    /// Semantic version as [major, minor, patch] array
    pub version: [u8; 3],

    /// Unix timestamp of document creation (optional)
    pub created: Option<u64>,

    /// TDF manifest containing encryption and policy information
    pub manifest: TdfCborManifest,

    /// Binary encrypted payload container
    pub payload: CborPayload,
}

/// TDF manifest for TDF-CBOR format
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TdfCborManifest {
    /// Encryption information including key access and policy
    pub encryption_information: EncryptionInformation,

    /// Optional assertions (spec: assertion.md), same shape as
    /// `TdfManifest::assertions`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assertions: Option<Vec<Assertion>>,
}

/// Binary payload for TDF-CBOR transport
///
/// Contains the encrypted data directly as binary bytes (no base64 encoding).
#[derive(Debug, Clone)]
pub struct CborPayload {
    /// Payload type. MUST be "inline" for TDF-CBOR
    pub payload_type: String,

    /// Protocol. MUST be "binary" for TDF-CBOR (not "base64")
    pub protocol: String,

    /// MIME type of the original (unencrypted) data
    pub mime_type: Option<String>,

    /// Whether the payload is encrypted. MUST be true
    pub is_encrypted: bool,

    /// Raw encrypted bytes (not base64 encoded)
    pub value: Vec<u8>,
}

// ============================================================================
// TDF-CBOR Error Types
// ============================================================================

/// Errors specific to TDF-CBOR parsing and validation
#[derive(Debug)]
pub enum TdfCborError {
    /// Invalid or missing CBOR magic bytes
    InvalidMagicBytes,
    /// Invalid tdf identifier
    InvalidTdfIdentifier(String),
    /// Unexpected integer key in CBOR structure
    UnexpectedKey { expected: u64, got: u64 },
    /// CBOR encoding error
    EncodingError(String),
    /// CBOR decoding error
    DecodingError(String),
    /// Missing required field
    MissingField(String),
    /// Encryption error
    EncryptionError(EncryptionError),
}

impl std::fmt::Display for TdfCborError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidMagicBytes => write!(f, "Invalid or missing CBOR magic bytes"),
            Self::InvalidTdfIdentifier(s) => {
                write!(f, "Invalid tdf identifier: expected 'cbor', got '{}'", s)
            }
            Self::UnexpectedKey { expected, got } => {
                write!(f, "Expected integer key {}, got {}", expected, got)
            }
            Self::EncodingError(e) => write!(f, "CBOR encoding error: {}", e),
            Self::DecodingError(e) => write!(f, "CBOR decoding error: {}", e),
            Self::MissingField(s) => write!(f, "Missing required field: {}", s),
            Self::EncryptionError(e) => write!(f, "Encryption error: {}", e),
        }
    }
}

impl std::error::Error for TdfCborError {}

impl From<EncryptionError> for TdfCborError {
    fn from(e: EncryptionError) -> Self {
        Self::EncryptionError(e)
    }
}

impl From<base64::DecodeError> for TdfCborError {
    fn from(e: base64::DecodeError) -> Self {
        Self::DecodingError(format!("Base64 decode error: {}", e))
    }
}

// ============================================================================
// TDF-CBOR Builder
// ============================================================================

/// Builder for creating TDF-CBOR containers
///
/// Requires the `kas-client` feature for EC key wrapping support.
#[cfg(feature = "kas-client")]
pub struct TdfCborBuilder {
    data: Vec<u8>,
    kas_url: Option<String>,
    kas_public_key_pem: Option<String>,
    policy: Option<Policy>,
    mime_type: Option<String>,
    include_created: bool,
}

impl TdfCbor {
    /// Create a new builder for encrypting data into TDF-CBOR format
    ///
    /// Requires the `kas-client` feature for EC key wrapping support.
    #[cfg(feature = "kas-client")]
    pub fn encrypt(data: &[u8]) -> TdfCborBuilder {
        TdfCborBuilder {
            data: data.to_vec(),
            kas_url: None,
            kas_public_key_pem: None,
            policy: None,
            mime_type: None,
            include_created: true,
        }
    }

    /// Format identifier (always "cbor")
    pub fn format_id(&self) -> &str {
        &self.tdf
    }

    /// Check if data starts with CBOR self-describe tag
    ///
    /// The self-describe CBOR tag is 0xD9 0xD9 0xF7 (tag 55799)
    pub fn has_magic_bytes(data: &[u8]) -> bool {
        data.len() >= 3 && data[0] == 0xD9 && data[1] == 0xD9 && data[2] == 0xF7
    }

    /// Parse TDF-CBOR from bytes
    #[cfg(feature = "cbor")]
    pub fn from_bytes(data: &[u8]) -> Result<Self, TdfCborError> {
        use ciborium::Value;

        // Verify magic bytes (at least the self-describe tag)
        if data.len() < 3 || data[0] != 0xD9 || data[1] != 0xD9 || data[2] != 0xF7 {
            return Err(TdfCborError::InvalidMagicBytes);
        }

        // Parse CBOR
        let value: Value = ciborium::from_reader(&data[3..])
            .map_err(|e| TdfCborError::DecodingError(e.to_string()))?;

        // Extract map
        let map = match value {
            Value::Map(m) => m,
            _ => return Err(TdfCborError::DecodingError("Expected CBOR map".to_string())),
        };

        // Parse fields by integer key
        let mut tdf = None;
        let mut version = None;
        let mut created = None;
        let mut manifest = None;
        let mut payload = None;

        for (k, v) in map {
            let key = match k {
                Value::Integer(i) => {
                    let i128_val: i128 = i.into();
                    i128_val as u64
                }
                _ => continue,
            };

            match key {
                key::TDF => {
                    if let Value::Text(s) = v {
                        tdf = Some(s);
                    }
                }
                key::VERSION => {
                    if let Value::Array(arr) = v
                        && arr.len() == 3
                    {
                        let mut ver = [0u8; 3];
                        for (i, val) in arr.iter().enumerate() {
                            if let Value::Integer(n) = val {
                                let i128_val: i128 = (*n).into();
                                ver[i] = i128_val as u8;
                            }
                        }
                        version = Some(ver);
                    }
                }
                key::CREATED => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        created = Some(i128_val as u64);
                    }
                }
                key::MANIFEST => {
                    // Support both native CBOR map (new) and JSON string (legacy)
                    match v {
                        Value::Map(_) => {
                            // Native CBOR manifest with integer keys
                            let m = Self::decode_manifest_from_cbor(v)?;
                            manifest = Some(m);
                        }
                        Value::Text(s) => {
                            // Legacy JSON string format
                            let m: TdfCborManifest = serde_json::from_str(&s)
                                .map_err(|e| TdfCborError::DecodingError(e.to_string()))?;
                            manifest = Some(m);
                        }
                        _ => {}
                    }
                }
                key::PAYLOAD => {
                    if let Value::Map(payload_map) = v {
                        let mut payload_type = String::new();
                        let mut protocol = String::new();
                        let mut mime_type = None;
                        let mut is_encrypted = true;
                        let mut value = Vec::new();

                        for (pk, pv) in payload_map {
                            // Support both integer keys (new spec) and string keys (legacy)
                            let pkey: u64 = match pk {
                                Value::Integer(i) => {
                                    let i128_val: i128 = i.into();
                                    i128_val as u64
                                }
                                Value::Text(s) => match s.as_str() {
                                    "type" => payload_key::TYPE,
                                    "protocol" => payload_key::PROTOCOL,
                                    "mimeType" => payload_key::MIME_TYPE,
                                    "isEncrypted" => payload_key::IS_ENCRYPTED,
                                    "value" => payload_key::VALUE,
                                    _ => continue,
                                },
                                _ => continue,
                            };

                            match pkey {
                                payload_key::TYPE => {
                                    // Support both integer enum (new) and string (legacy)
                                    match pv {
                                        Value::Integer(i) => {
                                            let i128_val: i128 = i.into();
                                            payload_type = match i128_val as u64 {
                                                enums::PAYLOAD_TYPE_INLINE => "inline".to_string(),
                                                enums::PAYLOAD_TYPE_REFERENCE => {
                                                    "reference".to_string()
                                                }
                                                _ => "inline".to_string(),
                                            };
                                        }
                                        Value::Text(s) => payload_type = s,
                                        _ => {}
                                    }
                                }
                                payload_key::PROTOCOL => {
                                    // Support both integer enum (new) and string (legacy)
                                    match pv {
                                        Value::Integer(i) => {
                                            let i128_val: i128 = i.into();
                                            protocol = match i128_val as u64 {
                                                enums::PAYLOAD_PROTOCOL_BINARY => {
                                                    "binary".to_string()
                                                }
                                                enums::PAYLOAD_PROTOCOL_BINARY_CHUNKED => {
                                                    "binary-chunked".to_string()
                                                }
                                                _ => "binary".to_string(),
                                            };
                                        }
                                        Value::Text(s) => protocol = s,
                                        _ => {}
                                    }
                                }
                                payload_key::MIME_TYPE => {
                                    if let Value::Text(s) = pv {
                                        mime_type = Some(s);
                                    }
                                }
                                payload_key::IS_ENCRYPTED => {
                                    if let Value::Bool(b) = pv {
                                        is_encrypted = b;
                                    }
                                }
                                payload_key::VALUE => {
                                    if let Value::Bytes(b) = pv {
                                        value = b;
                                    }
                                }
                                _ => {}
                            }
                        }

                        payload = Some(CborPayload {
                            payload_type,
                            protocol,
                            mime_type,
                            is_encrypted,
                            value,
                        });
                    }
                }
                _ => {}
            }
        }

        let tdf_value = tdf.ok_or_else(|| TdfCborError::MissingField("tdf".to_string()))?;
        if tdf_value != "cbor" {
            return Err(TdfCborError::InvalidTdfIdentifier(tdf_value));
        }

        Ok(TdfCbor {
            tdf: tdf_value,
            version: version.ok_or_else(|| TdfCborError::MissingField("version".to_string()))?,
            created,
            manifest: manifest.ok_or_else(|| TdfCborError::MissingField("manifest".to_string()))?,
            payload: payload.ok_or_else(|| TdfCborError::MissingField("payload".to_string()))?,
        })
    }

    /// Serialize to CBOR bytes with magic header
    #[cfg(feature = "cbor")]
    pub fn to_bytes(&self) -> Result<Vec<u8>, TdfCborError> {
        use ciborium::Value;

        // Build the manifest as native CBOR with integer keys and enums
        let manifest_cbor = self.encode_manifest_to_cbor()?;

        let mut map = vec![
            (
                Value::Integer(key::TDF.into()),
                Value::Text(self.tdf.clone()),
            ),
            (
                Value::Integer(key::VERSION.into()),
                Value::Array(vec![
                    Value::Integer(self.version[0].into()),
                    Value::Integer(self.version[1].into()),
                    Value::Integer(self.version[2].into()),
                ]),
            ),
        ];

        if let Some(ts) = self.created {
            map.push((
                Value::Integer(key::CREATED.into()),
                Value::Integer(ts.into()),
            ));
        }

        map.push((Value::Integer(key::MANIFEST.into()), manifest_cbor));

        // Build payload map with integer keys and enum values per spec section 1.5
        let payload_type_enum = match self.payload.payload_type.as_str() {
            "inline" => enums::PAYLOAD_TYPE_INLINE,
            "reference" => enums::PAYLOAD_TYPE_REFERENCE,
            _ => enums::PAYLOAD_TYPE_INLINE,
        };
        let protocol_enum = match self.payload.protocol.as_str() {
            "binary" => enums::PAYLOAD_PROTOCOL_BINARY,
            "binary-chunked" => enums::PAYLOAD_PROTOCOL_BINARY_CHUNKED,
            _ => enums::PAYLOAD_PROTOCOL_BINARY,
        };

        let mut payload_map = vec![
            (
                Value::Integer(payload_key::TYPE.into()),
                Value::Integer(payload_type_enum.into()),
            ),
            (
                Value::Integer(payload_key::PROTOCOL.into()),
                Value::Integer(protocol_enum.into()),
            ),
            (
                Value::Integer(payload_key::IS_ENCRYPTED.into()),
                Value::Bool(self.payload.is_encrypted),
            ),
            (
                Value::Integer(payload_key::VALUE.into()),
                Value::Bytes(self.payload.value.clone()),
            ),
        ];

        if let Some(ref mt) = self.payload.mime_type {
            payload_map.push((
                Value::Integer(payload_key::MIME_TYPE.into()),
                Value::Text(mt.clone()),
            ));
        }

        map.push((Value::Integer(key::PAYLOAD.into()), Value::Map(payload_map)));

        let cbor_value = Value::Map(map);

        // Encode with self-describe tag
        let mut result = vec![0xD9, 0xD9, 0xF7]; // Self-describe CBOR tag(55799)
        ciborium::into_writer(&cbor_value, &mut result)
            .map_err(|e| TdfCborError::EncodingError(e.to_string()))?;

        Ok(result)
    }

    /// Encode manifest to native CBOR with integer keys and enums
    #[cfg(feature = "cbor")]
    fn encode_manifest_to_cbor(&self) -> Result<ciborium::Value, TdfCborError> {
        use ciborium::Value;

        let enc_info = &self.manifest.encryption_information;

        // Encode policy as raw bytes (decode from base64)
        let policy_bytes = BASE64
            .decode(&enc_info.policy)
            .map_err(|e| TdfCborError::EncodingError(format!("Invalid policy base64: {}", e)))?;

        // Encode key access array
        let key_access_array: Vec<Value> = enc_info
            .key_access
            .iter()
            .map(|ka| self.encode_key_access_to_cbor(ka))
            .collect::<Result<Vec<_>, _>>()?;

        // Encode method
        let method_cbor = self.encode_method_to_cbor(&enc_info.method)?;

        // Encode integrity information
        let integrity_cbor = self.encode_integrity_to_cbor(&enc_info.integrity_information)?;

        // Encryption type enum: "split" -> 0
        let enc_type_enum = match enc_info.encryption_type.as_str() {
            "split" => enums::ENCRYPTION_TYPE_SPLIT,
            _ => enums::ENCRYPTION_TYPE_SPLIT,
        };

        // Build encryptionInformation map
        let enc_info_map = vec![
            (
                Value::Integer(enc_info_key::TYPE.into()),
                Value::Integer(enc_type_enum.into()),
            ),
            (
                Value::Integer(enc_info_key::KEY_ACCESS.into()),
                Value::Array(key_access_array),
            ),
            (Value::Integer(enc_info_key::METHOD.into()), method_cbor),
            (
                Value::Integer(enc_info_key::INTEGRITY_INFORMATION.into()),
                integrity_cbor,
            ),
            (
                Value::Integer(enc_info_key::POLICY.into()),
                Value::Bytes(policy_bytes),
            ),
        ];

        // Build manifest map
        let manifest_map = vec![(
            Value::Integer(manifest_key::ENCRYPTION_INFORMATION.into()),
            Value::Map(enc_info_map),
        )];

        Ok(Value::Map(manifest_map))
    }

    /// Encode a single key access object to CBOR
    #[cfg(feature = "cbor")]
    fn encode_key_access_to_cbor(&self, ka: &KeyAccess) -> Result<ciborium::Value, TdfCborError> {
        use ciborium::Value;

        // Key access type enum: "wrapped" -> 0, "remote" -> 1
        let ka_type_enum = match ka.access_type.as_str() {
            "wrapped" => enums::KEY_ACCESS_TYPE_WRAPPED,
            "remote" => enums::KEY_ACCESS_TYPE_REMOTE,
            _ => enums::KEY_ACCESS_TYPE_WRAPPED,
        };

        // Protocol enum: "kas" -> 0
        let protocol_enum = match ka.protocol.as_str() {
            "kas" => enums::KEY_PROTOCOL_KAS,
            _ => enums::KEY_PROTOCOL_KAS,
        };

        // Decode wrapped key from base64 to raw bytes
        let wrapped_key_bytes = BASE64.decode(&ka.wrapped_key).map_err(|e| {
            TdfCborError::EncodingError(format!("Invalid wrappedKey base64: {}", e))
        })?;

        // Encode policy binding
        let binding = &ka.policy_binding;
        let binding_alg_enum = self.hash_alg_to_enum(&binding.alg);
        let binding_hash_bytes = BASE64.decode(&binding.hash).map_err(|e| {
            TdfCborError::EncodingError(format!("Invalid binding hash base64: {}", e))
        })?;

        let policy_binding_map = vec![
            (
                Value::Integer(policy_binding_key::ALG.into()),
                Value::Integer(binding_alg_enum.into()),
            ),
            (
                Value::Integer(policy_binding_key::HASH.into()),
                Value::Bytes(binding_hash_bytes),
            ),
        ];

        let mut ka_map = vec![
            (
                Value::Integer(key_access_key::TYPE.into()),
                Value::Integer(ka_type_enum.into()),
            ),
            (
                Value::Integer(key_access_key::URL.into()),
                Value::Text(ka.url.clone()),
            ),
            (
                Value::Integer(key_access_key::PROTOCOL.into()),
                Value::Integer(protocol_enum.into()),
            ),
            (
                Value::Integer(key_access_key::WRAPPED_KEY.into()),
                Value::Bytes(wrapped_key_bytes),
            ),
            (
                Value::Integer(key_access_key::POLICY_BINDING.into()),
                Value::Map(policy_binding_map),
            ),
        ];

        // Add optional fields
        if let Some(ref kid) = ka.kid {
            ka_map.push((
                Value::Integer(key_access_key::KID.into()),
                Value::Text(kid.clone()),
            ));
        }

        if let Some(ref epk) = ka.ephemeral_public_key {
            let epk_bytes = BASE64.decode(epk).map_err(|e| {
                TdfCborError::EncodingError(format!("Invalid ephemeralPublicKey base64: {}", e))
            })?;
            ka_map.push((
                Value::Integer(key_access_key::EPHEMERAL_PUBLIC_KEY.into()),
                Value::Bytes(epk_bytes),
            ));
        }

        if let Some(ref sv) = ka.schema_version {
            ka_map.push((
                Value::Integer(key_access_key::SCHEMA_VERSION.into()),
                Value::Text(sv.clone()),
            ));
        }

        Ok(Value::Map(ka_map))
    }

    /// Encode method to CBOR
    #[cfg(feature = "cbor")]
    fn encode_method_to_cbor(
        &self,
        method: &EncryptionMethod,
    ) -> Result<ciborium::Value, TdfCborError> {
        use ciborium::Value;

        // Algorithm enum: "AES-256-GCM" -> 0
        let alg_enum = match method.algorithm.as_str() {
            "AES-256-GCM" => enums::SYMMETRIC_ALG_AES_256_GCM,
            _ => enums::SYMMETRIC_ALG_AES_256_GCM,
        };

        // Decode IV from base64 to raw bytes
        let iv_bytes = BASE64
            .decode(&method.iv)
            .map_err(|e| TdfCborError::EncodingError(format!("Invalid IV base64: {}", e)))?;

        let mut method_map = vec![
            (
                Value::Integer(method_key::ALGORITHM.into()),
                Value::Integer(alg_enum.into()),
            ),
            (
                Value::Integer(method_key::IV.into()),
                Value::Bytes(iv_bytes),
            ),
        ];

        method_map.push((
            Value::Integer(method_key::IS_STREAMABLE.into()),
            Value::Bool(method.is_streamable),
        ));

        Ok(Value::Map(method_map))
    }

    /// Encode integrity information to CBOR
    #[cfg(feature = "cbor")]
    fn encode_integrity_to_cbor(
        &self,
        integrity: &IntegrityInformation,
    ) -> Result<ciborium::Value, TdfCborError> {
        use ciborium::Value;

        // Encode root signature
        let root_sig = &integrity.root_signature;
        let root_alg_enum = self.hash_alg_to_enum(&root_sig.alg);
        let root_sig_bytes = BASE64.decode(&root_sig.sig).map_err(|e| {
            TdfCborError::EncodingError(format!("Invalid root signature base64: {}", e))
        })?;

        let root_sig_map = vec![
            (
                Value::Integer(root_sig_key::ALG.into()),
                Value::Integer(root_alg_enum.into()),
            ),
            (
                Value::Integer(root_sig_key::SIG.into()),
                Value::Bytes(root_sig_bytes),
            ),
        ];

        // Segment hash algorithm enum
        let seg_hash_alg_enum = self.hash_alg_to_enum(&integrity.segment_hash_alg);

        let mut integrity_map = vec![
            (
                Value::Integer(integrity_key::ROOT_SIGNATURE.into()),
                Value::Map(root_sig_map),
            ),
            (
                Value::Integer(integrity_key::SEGMENT_HASH_ALG.into()),
                Value::Integer(seg_hash_alg_enum.into()),
            ),
        ];

        // Encode segments
        if !integrity.segments.is_empty() {
            let segments_array: Vec<Value> = integrity
                .segments
                .iter()
                .map(|seg| self.encode_segment_to_cbor(seg))
                .collect::<Result<Vec<_>, _>>()?;
            integrity_map.push((
                Value::Integer(integrity_key::SEGMENTS.into()),
                Value::Array(segments_array),
            ));
        }

        integrity_map.push((
            Value::Integer(integrity_key::SEGMENT_SIZE_DEFAULT.into()),
            Value::Integer(integrity.segment_size_default.into()),
        ));

        integrity_map.push((
            Value::Integer(integrity_key::ENCRYPTED_SEGMENT_SIZE_DEFAULT.into()),
            Value::Integer(integrity.encrypted_segment_size_default.into()),
        ));

        Ok(Value::Map(integrity_map))
    }

    /// Encode a segment to CBOR
    #[cfg(feature = "cbor")]
    fn encode_segment_to_cbor(&self, segment: &Segment) -> Result<ciborium::Value, TdfCborError> {
        use ciborium::Value;

        let hash_bytes = BASE64.decode(&segment.hash).map_err(|e| {
            TdfCborError::EncodingError(format!("Invalid segment hash base64: {}", e))
        })?;

        let mut seg_map = vec![(
            Value::Integer(segment_key::HASH.into()),
            Value::Bytes(hash_bytes),
        )];

        if let Some(size) = segment.segment_size {
            seg_map.push((
                Value::Integer(segment_key::SEGMENT_SIZE.into()),
                Value::Integer(size.into()),
            ));
        }

        if let Some(size) = segment.encrypted_segment_size {
            seg_map.push((
                Value::Integer(segment_key::ENCRYPTED_SEGMENT_SIZE.into()),
                Value::Integer(size.into()),
            ));
        }

        Ok(Value::Map(seg_map))
    }

    /// Convert hash algorithm string to enum value
    fn hash_alg_to_enum(&self, alg: &str) -> u64 {
        match alg {
            "HS256" => enums::HASH_ALG_HS256,
            "HS384" => enums::HASH_ALG_HS384,
            "HS512" => enums::HASH_ALG_HS512,
            "GMAC" => enums::HASH_ALG_GMAC,
            "SHA256" => enums::HASH_ALG_SHA256,
            "ES256" => enums::HASH_ALG_ES256,
            "ES384" => enums::HASH_ALG_ES384,
            "ES512" => enums::HASH_ALG_ES512,
            _ => enums::HASH_ALG_HS256,
        }
    }

    /// Convert enum value to hash algorithm string
    fn enum_to_hash_alg(val: u64) -> String {
        match val {
            enums::HASH_ALG_HS256 => "HS256".to_string(),
            enums::HASH_ALG_HS384 => "HS384".to_string(),
            enums::HASH_ALG_HS512 => "HS512".to_string(),
            enums::HASH_ALG_GMAC => "GMAC".to_string(),
            enums::HASH_ALG_SHA256 => "SHA256".to_string(),
            enums::HASH_ALG_ES256 => "ES256".to_string(),
            enums::HASH_ALG_ES384 => "ES384".to_string(),
            enums::HASH_ALG_ES512 => "ES512".to_string(),
            _ => "HS256".to_string(),
        }
    }

    /// Decode manifest from native CBOR
    #[cfg(feature = "cbor")]
    fn decode_manifest_from_cbor(value: ciborium::Value) -> Result<TdfCborManifest, TdfCborError> {
        use ciborium::Value;

        let manifest_map = match value {
            Value::Map(m) => m,
            _ => {
                return Err(TdfCborError::DecodingError(
                    "Expected manifest map".to_string(),
                ));
            }
        };

        let mut enc_info = None;

        for (k, v) in manifest_map {
            let key = Self::extract_int_key(&k)?;

            if key == manifest_key::ENCRYPTION_INFORMATION {
                enc_info = Some(Self::decode_encryption_info(v)?);
            }
        }

        Ok(TdfCborManifest {
            encryption_information: enc_info
                .ok_or_else(|| TdfCborError::MissingField("encryptionInformation".to_string()))?,
            assertions: None, // TODO: decode assertions if present
        })
    }

    /// Decode encryption information from CBOR
    #[cfg(feature = "cbor")]
    fn decode_encryption_info(
        value: ciborium::Value,
    ) -> Result<EncryptionInformation, TdfCborError> {
        use ciborium::Value;

        let enc_map = match value {
            Value::Map(m) => m,
            _ => {
                return Err(TdfCborError::DecodingError(
                    "Expected encryptionInformation map".to_string(),
                ));
            }
        };

        let mut enc_type = "split".to_string();
        let mut key_access = Vec::new();
        let mut method = None;
        let mut integrity = None;
        let mut policy = String::new();

        for (k, v) in enc_map {
            let key = Self::extract_int_key(&k)?;

            match key {
                enc_info_key::TYPE => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        enc_type = match i128_val as u64 {
                            enums::ENCRYPTION_TYPE_SPLIT => "split".to_string(),
                            _ => "split".to_string(),
                        };
                    }
                }
                enc_info_key::KEY_ACCESS => {
                    if let Value::Array(arr) = v {
                        for ka_val in arr {
                            key_access.push(Self::decode_key_access(ka_val)?);
                        }
                    }
                }
                enc_info_key::METHOD => {
                    method = Some(Self::decode_method(v)?);
                }
                enc_info_key::INTEGRITY_INFORMATION => {
                    integrity = Some(Self::decode_integrity(v)?);
                }
                enc_info_key::POLICY => {
                    // Policy is stored as raw bytes, encode to base64
                    if let Value::Bytes(b) = v {
                        policy = BASE64.encode(&b);
                    }
                }
                _ => {}
            }
        }

        Ok(EncryptionInformation {
            encryption_type: enc_type,
            key_access,
            method: method.ok_or_else(|| TdfCborError::MissingField("method".to_string()))?,
            integrity_information: integrity
                .ok_or_else(|| TdfCborError::MissingField("integrityInformation".to_string()))?,
            policy,
        })
    }

    /// Decode key access from CBOR
    #[cfg(feature = "cbor")]
    fn decode_key_access(value: ciborium::Value) -> Result<KeyAccess, TdfCborError> {
        use ciborium::Value;

        let ka_map = match value {
            Value::Map(m) => m,
            _ => {
                return Err(TdfCborError::DecodingError(
                    "Expected keyAccess map".to_string(),
                ));
            }
        };

        let mut access_type = "wrapped".to_string();
        let mut url = String::new();
        let mut protocol = "kas".to_string();
        let mut wrapped_key = String::new();
        let mut policy_binding = None;
        let mut kid = None;
        let mut ephemeral_public_key = None;
        let mut schema_version = None;

        for (k, v) in ka_map {
            let key = Self::extract_int_key(&k)?;

            match key {
                key_access_key::TYPE => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        access_type = match i128_val as u64 {
                            enums::KEY_ACCESS_TYPE_WRAPPED => "wrapped".to_string(),
                            enums::KEY_ACCESS_TYPE_REMOTE => "remote".to_string(),
                            _ => "wrapped".to_string(),
                        };
                    }
                }
                key_access_key::URL => {
                    if let Value::Text(s) = v {
                        url = s;
                    }
                }
                key_access_key::PROTOCOL => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        protocol = match i128_val as u64 {
                            enums::KEY_PROTOCOL_KAS => "kas".to_string(),
                            _ => "kas".to_string(),
                        };
                    }
                }
                key_access_key::WRAPPED_KEY => {
                    if let Value::Bytes(b) = v {
                        wrapped_key = BASE64.encode(&b);
                    }
                }
                key_access_key::POLICY_BINDING => {
                    policy_binding = Some(Self::decode_policy_binding(v)?);
                }
                key_access_key::KID => {
                    if let Value::Text(s) = v {
                        kid = Some(s);
                    }
                }
                key_access_key::EPHEMERAL_PUBLIC_KEY => {
                    if let Value::Bytes(b) = v {
                        ephemeral_public_key = Some(BASE64.encode(&b));
                    }
                }
                key_access_key::SCHEMA_VERSION => {
                    if let Value::Text(s) = v {
                        schema_version = Some(s);
                    }
                }
                _ => {}
            }
        }

        Ok(KeyAccess {
            access_type,
            url,
            protocol,
            wrapped_key,
            policy_binding: policy_binding
                .ok_or_else(|| TdfCborError::MissingField("policyBinding".to_string()))?,
            encrypted_metadata: None,
            kid,
            // The TDF-CBOR KAO integer-key table defines no split id.
            sid: None,
            ephemeral_public_key,
            schema_version,
        })
    }

    /// Decode policy binding from CBOR
    #[cfg(feature = "cbor")]
    fn decode_policy_binding(
        value: ciborium::Value,
    ) -> Result<crate::manifest::PolicyBinding, TdfCborError> {
        use ciborium::Value;

        let pb_map = match value {
            Value::Map(m) => m,
            _ => {
                return Err(TdfCborError::DecodingError(
                    "Expected policyBinding map".to_string(),
                ));
            }
        };

        let mut alg = "HS256".to_string();
        let mut hash = String::new();

        for (k, v) in pb_map {
            let key = Self::extract_int_key(&k)?;

            match key {
                policy_binding_key::ALG => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        alg = Self::enum_to_hash_alg(i128_val as u64);
                    }
                }
                policy_binding_key::HASH => {
                    if let Value::Bytes(b) = v {
                        hash = BASE64.encode(&b);
                    }
                }
                _ => {}
            }
        }

        Ok(crate::manifest::PolicyBinding { alg, hash })
    }

    /// Decode method from CBOR
    #[cfg(feature = "cbor")]
    fn decode_method(value: ciborium::Value) -> Result<EncryptionMethod, TdfCborError> {
        use ciborium::Value;

        let method_map = match value {
            Value::Map(m) => m,
            _ => {
                return Err(TdfCborError::DecodingError(
                    "Expected method map".to_string(),
                ));
            }
        };

        let mut algorithm = "AES-256-GCM".to_string();
        let mut iv = String::new();
        let mut is_streamable = true; // Default to true

        for (k, v) in method_map {
            let key = Self::extract_int_key(&k)?;

            match key {
                method_key::ALGORITHM => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        algorithm = match i128_val as u64 {
                            enums::SYMMETRIC_ALG_AES_256_GCM => "AES-256-GCM".to_string(),
                            _ => "AES-256-GCM".to_string(),
                        };
                    }
                }
                method_key::IV => {
                    if let Value::Bytes(b) = v {
                        iv = BASE64.encode(&b);
                    }
                }
                method_key::IS_STREAMABLE => {
                    if let Value::Bool(b) = v {
                        is_streamable = b;
                    }
                }
                _ => {}
            }
        }

        Ok(EncryptionMethod {
            algorithm,
            iv,
            is_streamable,
        })
    }

    /// Decode integrity information from CBOR
    #[cfg(feature = "cbor")]
    fn decode_integrity(value: ciborium::Value) -> Result<IntegrityInformation, TdfCborError> {
        use ciborium::Value;

        let int_map = match value {
            Value::Map(m) => m,
            _ => {
                return Err(TdfCborError::DecodingError(
                    "Expected integrityInformation map".to_string(),
                ));
            }
        };

        let mut root_signature = None;
        let mut segment_hash_alg = "GMAC".to_string();
        let mut segments = Vec::new();
        let mut segment_size_default: u64 = 0;
        let mut encrypted_segment_size_default: u64 = 0;

        for (k, v) in int_map {
            let key = Self::extract_int_key(&k)?;

            match key {
                integrity_key::ROOT_SIGNATURE => {
                    root_signature = Some(Self::decode_root_signature(v)?);
                }
                integrity_key::SEGMENT_HASH_ALG => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        segment_hash_alg = Self::enum_to_hash_alg(i128_val as u64);
                    }
                }
                integrity_key::SEGMENTS => {
                    if let Value::Array(arr) = v {
                        for seg_val in arr {
                            segments.push(Self::decode_segment(seg_val)?);
                        }
                    }
                }
                integrity_key::SEGMENT_SIZE_DEFAULT => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        segment_size_default = i128_val as u64;
                    }
                }
                integrity_key::ENCRYPTED_SEGMENT_SIZE_DEFAULT => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        encrypted_segment_size_default = i128_val as u64;
                    }
                }
                _ => {}
            }
        }

        Ok(IntegrityInformation {
            root_signature: root_signature
                .ok_or_else(|| TdfCborError::MissingField("rootSignature".to_string()))?,
            segment_hash_alg,
            segments,
            segment_size_default,
            encrypted_segment_size_default,
        })
    }

    /// Decode root signature from CBOR
    #[cfg(feature = "cbor")]
    fn decode_root_signature(value: ciborium::Value) -> Result<RootSignature, TdfCborError> {
        use ciborium::Value;

        let sig_map = match value {
            Value::Map(m) => m,
            _ => {
                return Err(TdfCborError::DecodingError(
                    "Expected rootSignature map".to_string(),
                ));
            }
        };

        let mut alg = "HS256".to_string();
        let mut sig = String::new();

        for (k, v) in sig_map {
            let key = Self::extract_int_key(&k)?;

            match key {
                root_sig_key::ALG => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        alg = Self::enum_to_hash_alg(i128_val as u64);
                    }
                }
                root_sig_key::SIG => {
                    if let Value::Bytes(b) = v {
                        sig = BASE64.encode(&b);
                    }
                }
                _ => {}
            }
        }

        Ok(RootSignature { alg, sig })
    }

    /// Decode segment from CBOR
    #[cfg(feature = "cbor")]
    fn decode_segment(value: ciborium::Value) -> Result<Segment, TdfCborError> {
        use ciborium::Value;

        let seg_map = match value {
            Value::Map(m) => m,
            _ => {
                return Err(TdfCborError::DecodingError(
                    "Expected segment map".to_string(),
                ));
            }
        };

        let mut hash = String::new();
        let mut segment_size = None;
        let mut encrypted_segment_size = None;

        for (k, v) in seg_map {
            let key = Self::extract_int_key(&k)?;

            match key {
                segment_key::HASH => {
                    if let Value::Bytes(b) = v {
                        hash = BASE64.encode(&b);
                    }
                }
                segment_key::SEGMENT_SIZE => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        segment_size = Some(i128_val as u64);
                    }
                }
                segment_key::ENCRYPTED_SEGMENT_SIZE => {
                    if let Value::Integer(i) = v {
                        let i128_val: i128 = i.into();
                        encrypted_segment_size = Some(i128_val as u64);
                    }
                }
                _ => {}
            }
        }

        Ok(Segment {
            hash,
            segment_size,
            encrypted_segment_size,
        })
    }

    /// Extract integer key from CBOR value
    #[cfg(feature = "cbor")]
    fn extract_int_key(value: &ciborium::Value) -> Result<u64, TdfCborError> {
        use ciborium::Value;

        match value {
            Value::Integer(i) => {
                let i128_val: i128 = (*i).into();
                Ok(i128_val as u64)
            }
            _ => Err(TdfCborError::DecodingError(
                "Expected integer key".to_string(),
            )),
        }
    }

    /// Decrypt the payload with a provided data encryption key
    pub fn decrypt_with_key(&self, payload_key: &[u8]) -> Result<Vec<u8>, EncryptionError> {
        use aes_gcm::{
            Aes256Gcm, Nonce,
            aead::{Aead, KeyInit},
        };

        let ciphertext = &self.payload.value;

        // Extract IV from encryption method
        let iv_bytes = BASE64.decode(&self.manifest.encryption_information.method.iv)?;

        // `method.iv` defaults to empty on read (Java omits it), so a short IV
        // must be rejected here: `Nonce::from_slice` asserts the length and
        // would abort the process.
        if iv_bytes.len() < GCM_NONCE_LENGTH {
            return Err(EncryptionError::InvalidIvLength(iv_bytes.len()));
        }
        let payload_iv = &iv_bytes[0..GCM_NONCE_LENGTH];

        let cipher = Aes256Gcm::new_from_slice(payload_key)
            .map_err(|_| EncryptionError::InvalidKeyLength)?;

        #[allow(deprecated)]
        let nonce = Nonce::from_slice(payload_iv);

        let plaintext = cipher
            .decrypt(nonce, ciphertext.as_ref())
            .map_err(EncryptionError::AeadError)?;

        Ok(plaintext)
    }
}

#[cfg(feature = "kas-client")]
impl TdfCborBuilder {
    /// Set the KAS (Key Access Service) URL
    #[must_use]
    pub fn kas_url(mut self, url: &str) -> Self {
        self.kas_url = Some(url.to_string());
        self
    }

    /// Set the KAS public key PEM for EC key wrapping (required)
    ///
    /// Uses EC (ECDH + HKDF + AES-GCM) key wrapping. This produces smaller
    /// wrapped keys (~60 bytes vs 256 bytes for RSA-2048).
    ///
    /// This method is required - TDF-CBOR documents cannot be created without
    /// a valid KAS public key for key wrapping.
    #[must_use]
    pub fn kas_public_key(mut self, pem: &str) -> Self {
        self.kas_public_key_pem = Some(pem.to_string());
        self
    }

    /// Set the access control policy
    #[must_use]
    pub fn policy(mut self, policy: Policy) -> Self {
        self.policy = Some(policy);
        self
    }

    /// Set the MIME type of the original data
    #[must_use]
    pub fn mime_type(mut self, mime_type: &str) -> Self {
        self.mime_type = Some(mime_type.to_string());
        self
    }

    /// Include created timestamp (default: true)
    #[must_use]
    pub fn include_created(mut self, include: bool) -> Self {
        self.include_created = include;
        self
    }

    /// Build the TDF-CBOR container
    pub fn build(self) -> Result<TdfCbor, TdfCborError> {
        let kas_url = self
            .kas_url
            .ok_or_else(|| TdfCborError::MissingField("kas_url".to_string()))?;
        let policy = self
            .policy
            .ok_or_else(|| TdfCborError::MissingField("policy".to_string()))?;

        // Create encryption instance
        let tdf_encryption = TdfEncryption::new()?;

        // Encrypt the data
        let encrypted_payload = tdf_encryption.encrypt(&self.data)?;

        // Get the payload key for policy binding
        let payload_key = tdf_encryption.payload_key();

        // Create policy binding
        let policy_json = serde_json::to_string(&policy)
            .map_err(|e| TdfCborError::EncodingError(e.to_string()))?;
        let policy_b64 = BASE64.encode(policy_json.as_bytes());

        // Calculate policy binding hash
        let policy_hash = calculate_policy_binding(&policy_json, payload_key)
            .map_err(|e| TdfCborError::EncodingError(e.to_string()))?;

        // Decode the base64 ciphertext to get raw bytes
        let ciphertext_bytes = BASE64.decode(&encrypted_payload.ciphertext)?;

        // Wrap key using EC (ECDH + HKDF + AES-GCM) - KAS public key is required
        // This requires the kas-client feature which provides EC key wrapping
        let kas_pem = self
            .kas_public_key_pem
            .ok_or_else(|| TdfCborError::MissingField("kas_public_key".to_string()))?;
        let ec_result = opentdf_crypto::wrap_key_with_ec(&kas_pem, payload_key)
            .map_err(|e| TdfCborError::EncodingError(format!("EC wrap failed: {:?}", e)))?;
        let (wrapped_key, ephemeral_public_key) =
            (ec_result.wrapped_key, Some(ec_result.ephemeral_public_key));

        // Create key access object
        let key_access = KeyAccess {
            access_type: "wrapped".to_string(),
            url: kas_url,
            kid: None,
            sid: None,
            protocol: "kas".to_string(),
            wrapped_key,
            policy_binding: crate::manifest::PolicyBinding {
                alg: "HS256".to_string(),
                hash: policy_hash,
            },
            encrypted_metadata: None,
            schema_version: Some("1.0".to_string()),
            ephemeral_public_key,
        };

        // Extract GMAC tag from encrypted payload (last 16 bytes)
        let gmac_tag = if ciphertext_bytes.len() >= 16 {
            ciphertext_bytes[ciphertext_bytes.len() - 16..].to_vec()
        } else {
            return Err(TdfCborError::EncodingError(
                "Ciphertext too short for GMAC tag".to_string(),
            ));
        };

        // Create integrity information
        let mut integrity_info = IntegrityInformation {
            root_signature: RootSignature {
                alg: "HS256".to_string(),
                sig: String::new(),
            },
            segment_hash_alg: "GMAC".to_string(),
            segments: vec![Segment {
                hash: BASE64.encode(&gmac_tag),
                segment_size: Some(self.data.len() as u64),
                encrypted_segment_size: Some(ciphertext_bytes.len() as u64),
            }],
            segment_size_default: self.data.len() as u64,
            encrypted_segment_size_default: ciphertext_bytes.len() as u64,
        };

        // Calculate root signature
        integrity_info
            .generate_root_signature(&[gmac_tag], payload_key)
            .map_err(TdfCborError::EncodingError)?;

        // Create encryption information
        let encryption_info = EncryptionInformation {
            encryption_type: "split".to_string(),
            key_access: vec![key_access],
            method: EncryptionMethod {
                algorithm: "AES-256-GCM".to_string(),
                is_streamable: true,
                iv: encrypted_payload.iv.clone(),
            },
            integrity_information: integrity_info,
            policy: policy_b64,
        };

        // Create manifest
        let manifest = TdfCborManifest {
            encryption_information: encryption_info,
            assertions: None,
        };

        // Create payload with raw binary (not base64)
        let payload = CborPayload {
            payload_type: "inline".to_string(),
            protocol: "binary".to_string(), // Key difference from TDF-JSON
            mime_type: self.mime_type,
            is_encrypted: true,
            value: ciphertext_bytes,
        };

        // Create timestamp
        let created = if self.include_created {
            Some(
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_secs())
                    .unwrap_or(0),
            )
        } else {
            None
        };

        Ok(TdfCbor {
            tdf: "cbor".to_string(),
            version: [1, 0, 0],
            created,
            manifest,
            payload,
        })
    }
}

// ============================================================================
// Format Detection
// ============================================================================

/// Detect if data is a TDF-CBOR document
pub fn is_tdf_cbor(data: &[u8]) -> bool {
    TdfCbor::has_magic_bytes(data)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Get test KAS URL from environment variable or use default
    fn test_kas_url() -> String {
        std::env::var("TEST_KAS_URL").unwrap_or_else(|_| "https://platform.arkavo.net".to_string())
    }

    /// Generate a test EC key pair for testing TDF-CBOR encryption
    /// Returns (private_key_pem, public_key_pem)
    fn generate_test_ec_key_pair() -> (String, String) {
        use p256::SecretKey;
        use p256::elliptic_curve::Generate;
        use p256::pkcs8::{EncodePrivateKey, EncodePublicKey, LineEnding};

        let secret_key = SecretKey::generate();
        let public_key = secret_key.public_key();

        let private_pem = secret_key
            .to_pkcs8_pem(LineEnding::LF)
            .expect("Failed to encode private key")
            .to_string();

        let public_pem = public_key
            .to_public_key_pem(LineEnding::LF)
            .expect("Failed to encode public key");

        (private_pem, public_pem)
    }

    #[test]
    fn test_cbor_magic_bytes() {
        assert_eq!(CBOR_MAGIC, [0xD9, 0xD9, 0xF7, 0xA5]);
    }

    #[test]
    fn test_has_magic_bytes() {
        // Valid self-describe tag + any map
        let valid = [0xD9, 0xD9, 0xF7, 0xA5, 0x01, 0x02];
        assert!(TdfCbor::has_magic_bytes(&valid));

        // Valid self-describe tag + different map size
        let valid2 = [0xD9, 0xD9, 0xF7, 0xB8, 0x05];
        assert!(TdfCbor::has_magic_bytes(&valid2));

        let invalid = [0x00, 0x01, 0x02, 0x03];
        assert!(!TdfCbor::has_magic_bytes(&invalid));

        let short = [0xD9, 0xD9];
        assert!(!TdfCbor::has_magic_bytes(&short));
    }

    #[cfg(feature = "kas-client")]
    #[test]
    fn test_create_cbor_envelope() {
        let policy = Policy::new(
            "test-policy".to_string(),
            vec![],
            vec!["user@example.com".to_string()],
        );
        let (_private_pem, public_pem) = generate_test_ec_key_pair();

        let container = TdfCbor::encrypt(b"Hello, World!")
            .kas_url(&test_kas_url())
            .kas_public_key(&public_pem)
            .policy(policy)
            .mime_type("text/plain")
            .build()
            .expect("Failed to create container");

        assert_eq!(container.tdf, "cbor");
        assert_eq!(container.version, [1, 0, 0]);
        assert!(container.created.is_some());
        assert_eq!(container.payload.payload_type, "inline");
        assert_eq!(container.payload.protocol, "binary");
        assert_eq!(container.payload.mime_type, Some("text/plain".to_string()));
        assert!(container.payload.is_encrypted);
        assert!(!container.payload.value.is_empty());
    }

    #[cfg(all(feature = "cbor", feature = "kas-client"))]
    #[test]
    fn test_cbor_roundtrip() {
        let policy = Policy::new(
            "test-policy".to_string(),
            vec![],
            vec!["user@example.com".to_string()],
        );
        let (_private_pem, public_pem) = generate_test_ec_key_pair();

        let original = TdfCbor::encrypt(b"Test data for CBOR")
            .kas_url(&test_kas_url())
            .kas_public_key(&public_pem)
            .policy(policy)
            .include_created(false)
            .build()
            .expect("Failed to create container");

        // Serialize to bytes
        let bytes = original.to_bytes().expect("Failed to serialize");

        // Verify magic bytes
        assert!(TdfCbor::has_magic_bytes(&bytes));

        // Parse back
        let parsed = TdfCbor::from_bytes(&bytes).expect("Failed to parse");

        assert_eq!(parsed.tdf, "cbor");
        assert_eq!(parsed.version, [1, 0, 0]);
        assert_eq!(parsed.payload.value, original.payload.value);
    }

    /// The KAS checks the binding as `HMAC-SHA256(DEK, manifest policy)`, where
    /// the manifest policy is the base64 string itself.
    #[test]
    fn test_policy_binding_matches_manifest_policy() {
        let policy = Policy::new(
            "test-policy".to_string(),
            vec![],
            vec!["user@example.com".to_string()],
        );
        let (private_pem, public_pem) = generate_test_ec_key_pair();

        let container = TdfCbor::encrypt(b"binding")
            .kas_url(&test_kas_url())
            .kas_public_key(&public_pem)
            .policy(policy)
            .build()
            .expect("Failed to create container");

        let info = &container.manifest.encryption_information;
        let kao = &info.key_access[0];
        let dek = opentdf_crypto::unwrap_key_with_ec(
            &private_pem,
            &kao.wrapped_key,
            kao.ephemeral_public_key.as_deref().unwrap(),
        )
        .unwrap();
        let expected =
            BASE64.encode(opentdf_crypto::calculate_hmac(&dek, info.policy.as_bytes()).unwrap());
        assert_eq!(kao.policy_binding.hash, expected);
    }

    #[test]
    fn test_builder_missing_kas_url() {
        let policy = Policy::new(
            "test-policy".to_string(),
            vec![],
            vec!["user@example.com".to_string()],
        );

        let result = TdfCbor::encrypt(b"Data").policy(policy).build();

        assert!(result.is_err());
    }

    #[test]
    fn test_builder_missing_policy() {
        let result = TdfCbor::encrypt(b"Data").kas_url(&test_kas_url()).build();

        assert!(result.is_err());
    }

    /// Legacy JSON-string manifests may omit `method.iv`; decrypt must return
    /// an error rather than abort on a zero-length nonce.
    #[test]
    fn tdf_cbor_decrypt_with_key_rejects_missing_iv_instead_of_panicking() {
        let manifest_json = r#"{
            "encryptionInformation": {
                "type": "split",
                "keyAccess": [],
                "method": {"algorithm": "AES-256-GCM"},
                "integrityInformation": {
                    "rootSignature": {"alg": "HS256", "sig": ""},
                    "segmentHashAlg": "GMAC",
                    "segments": [],
                    "segmentSizeDefault": 0,
                    "encryptedSegmentSizeDefault": 0
                },
                "policy": ""
            }
        }"#;
        let manifest: TdfCborManifest = serde_json::from_str(manifest_json).unwrap();
        assert_eq!(manifest.encryption_information.method.iv, "");

        let doc = TdfCbor {
            tdf: "cbor".to_string(),
            version: [1, 0, 0],
            created: None,
            manifest,
            payload: CborPayload {
                payload_type: "inline".to_string(),
                protocol: "binary".to_string(),
                mime_type: None,
                is_encrypted: true,
                value: vec![0u8; 24],
            },
        };

        let result = doc.decrypt_with_key(TdfEncryption::new().unwrap().payload_key());
        assert!(
            matches!(result, Err(EncryptionError::InvalidIvLength(0))),
            "expected InvalidIvLength(0), got {result:?}"
        );
    }
}