jose-rs 0.2.0

Pure-Rust JOSE library (JWS, JWE, JWK, JWT) with software and HSM key support
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
//! JWE Compact Serialization (RFC 7516 Section 3.1).
//!
//! Format: `BASE64URL(header).BASE64URL(encrypted_key).BASE64URL(iv).BASE64URL(ciphertext).BASE64URL(tag)`

use crate::algorithm::{JweAlgorithm, JweEncryption};
use crate::base64url;
use crate::error::{JoseError, Result};
use crate::header::JoseHeader;

use rand::RngCore;
use zeroize::Zeroizing;

fn ensure_token_size(token: &str) -> Result<()> {
    if token.len() > crate::MAX_TOKEN_BYTES {
        return Err(JoseError::InvalidToken(format!(
            "token size {} exceeds MAX_TOKEN_BYTES ({})",
            token.len(),
            crate::MAX_TOKEN_BYTES
        )));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Encrypt plaintext and produce a JWE Compact Serialization string.
///
/// - For `dir`: `key` is the CEK (must match the `enc` algorithm's key size).
/// - For `A128KW` / `A192KW` / `A256KW`: `key` is the Key Encryption Key.
/// - For `RSA-OAEP` / `RSA-OAEP-256`: `key` is the RSA public key in SPKI DER
///   format.
pub fn encrypt(
    key: &[u8],
    plaintext: &[u8],
    alg: JweAlgorithm,
    enc: JweEncryption,
) -> Result<String> {
    // 1. Build protected header.
    let mut header = JoseHeader::new(alg.as_str());
    header.enc = Some(enc.as_str().to_string());
    let header_json = serde_json::to_vec(&header)?;
    let header_b64 = base64url::encode(&header_json);

    // 2. Generate or use CEK, produce encrypted key.
    let (cek, encrypted_key) = produce_cek(key, alg, enc)?;

    // 3. Content encryption.
    // AAD = ASCII(BASE64URL(header)) per RFC 7516 Section 5.1 step 14.
    let aad = header_b64.as_bytes();
    let (iv, ciphertext, tag) = content_encrypt(enc, &cek, plaintext, aad)?;

    // 4. Assemble compact serialization.
    let encrypted_key_b64 = base64url::encode(&encrypted_key);
    let iv_b64 = base64url::encode(&iv);
    let ciphertext_b64 = base64url::encode(&ciphertext);
    let tag_b64 = base64url::encode(&tag);

    Ok(format!(
        "{header_b64}.{encrypted_key_b64}.{iv_b64}.{ciphertext_b64}.{tag_b64}"
    ))
}

/// Determine which `JwkOp` governs a JWE alg on the encrypt side.
///
/// `Dir` uses the key directly for content encryption (`Encrypt`);
/// key-transport (`A*KW`, `RSA-OAEP*`) wraps a CEK (`WrapKey`).
fn jwe_alg_encrypt_op(alg: JweAlgorithm) -> crate::jwk::JwkOp {
    match alg {
        JweAlgorithm::Dir => crate::jwk::JwkOp::Encrypt,
        _ => crate::jwk::JwkOp::WrapKey,
    }
}

/// Determine which `JwkOp` governs a JWE alg on the decrypt side.
fn jwe_alg_decrypt_op(alg: JweAlgorithm) -> crate::jwk::JwkOp {
    match alg {
        JweAlgorithm::Dir => crate::jwk::JwkOp::Decrypt,
        _ => crate::jwk::JwkOp::UnwrapKey,
    }
}

/// Convert a JWK to the raw key-material form that [`encrypt`] /
/// [`decrypt`] expect for the given JWE algorithm.
///
/// - For `dir`, `A*KW`: returns the `k` bytes of an `oct` JWK.
/// - For `RSA-OAEP*` on the encrypt side: returns SPKI DER of the public key.
/// - For `RSA-OAEP*` on the decrypt side: returns PKCS#8 DER of the private key.
fn jwk_to_jwe_key_bytes(
    jwk: &crate::jwk::Jwk,
    alg: JweAlgorithm,
    for_decrypt: bool,
) -> Result<Vec<u8>> {
    match alg {
        JweAlgorithm::Dir | JweAlgorithm::A128KW | JweAlgorithm::A192KW | JweAlgorithm::A256KW => {
            let k = jwk
                .k
                .as_deref()
                .ok_or_else(|| JoseError::Key("JWK is missing `k`".into()))?;
            base64url::decode(k)
        }
        JweAlgorithm::RsaOaep256 => rsa_jwk_to_der(jwk, for_decrypt),
        #[cfg(feature = "deprecated")]
        JweAlgorithm::RsaOaep => rsa_jwk_to_der(jwk, for_decrypt),
        _ => Err(JoseError::UnsupportedAlgorithm(format!(
            "JWE alg {} not supported via JWK one-shot API",
            alg.as_str()
        ))),
    }
}

fn rsa_jwk_to_der(jwk: &crate::jwk::Jwk, for_decrypt: bool) -> Result<Vec<u8>> {
    let sw = crate::jwk::jwk_to_software_key(jwk)?;
    match &sw {
        kryptering::SoftwareKey::Rsa { public, private } => {
            if for_decrypt {
                use rsa::pkcs8::EncodePrivateKey;
                let pk = private
                    .as_ref()
                    .ok_or_else(|| JoseError::Key("JWK lacks RSA private components".into()))?;
                Ok(pk
                    .to_pkcs8_der()
                    .map_err(|e| JoseError::Key(format!("PKCS#8 encode: {e}")))?
                    .as_bytes()
                    .to_vec())
            } else {
                use rsa::pkcs8::EncodePublicKey;
                Ok(public
                    .to_public_key_der()
                    .map_err(|e| JoseError::Key(format!("SPKI encode: {e}")))?
                    .as_ref()
                    .to_vec())
            }
        }
        _ => Err(JoseError::Key("RSA JWK expected".into())),
    }
}

/// Encrypt plaintext using a JWK directly — the one-shot JWE encrypt API.
///
/// The key management algorithm is read from `jwk.alg` and
/// `Jwk::check_op` is enforced: `Encrypt` for `dir`, `WrapKey` for
/// `A*KW` / `RSA-OAEP*`. The caller supplies the content-encryption
/// algorithm.
pub fn encrypt_with_jwk(
    jwk: &crate::jwk::Jwk,
    plaintext: &[u8],
    enc: JweEncryption,
) -> Result<String> {
    let jwk_alg_str = jwk
        .alg
        .as_deref()
        .ok_or_else(|| JoseError::Key("JWK alg must be set for encrypt_with_jwk".into()))?;
    let alg = JweAlgorithm::from_str(jwk_alg_str)?;
    jwk.check_op(jwe_alg_encrypt_op(alg))?;
    let key_bytes = jwk_to_jwe_key_bytes(jwk, alg, false)?;
    encrypt(&key_bytes, plaintext, alg, enc)
}

/// Decrypt a JWE token using a JWK directly — the one-shot JWE decrypt API.
///
/// The key management algorithm is read from the token's protected
/// header (and cross-checked against `jwk.alg` if pinned).
/// `Jwk::check_op` is enforced: `Decrypt` for `dir`, `UnwrapKey` for
/// `A*KW` / `RSA-OAEP*`.
pub fn decrypt_with_jwk(jwk: &crate::jwk::Jwk, token: &str) -> Result<Vec<u8>> {
    // Read the header's alg for dispatch.
    let header = decode_header(token)?;
    let alg = JweAlgorithm::from_str(&header.alg)?;

    // If the JWK pins an alg, it must agree with the token.
    if let Some(jwk_alg) = jwk.alg.as_deref() {
        if jwk_alg != header.alg {
            return Err(JoseError::InvalidToken(format!(
                "JWK alg {jwk_alg} does not match token header alg {}",
                header.alg
            )));
        }
    }

    jwk.check_op(jwe_alg_decrypt_op(alg))?;
    let key_bytes = jwk_to_jwe_key_bytes(jwk, alg, true)?;
    decrypt(&key_bytes, token)
}

/// Decrypt a JWE Compact Serialization string and return the plaintext.
///
/// Accepts any key-management / content-encryption algorithm the library
/// supports. For strict deployments, use [`decrypt_with_options`] to pin
/// an explicit allow-list.
///
/// - For `dir`: `key` is the CEK.
/// - For `A128KW` / `A192KW` / `A256KW`: `key` is the Key Encryption Key.
/// - For `RSA-OAEP` / `RSA-OAEP-256`: `key` is the RSA private key in PKCS#8
///   DER format.
pub fn decrypt(key: &[u8], token: &str) -> Result<Vec<u8>> {
    decrypt_with_options(key, token, &JweDecryptOptions::permissive())
}

/// Options controlling which JWE algorithms `decrypt_with_options` accepts.
///
/// Use [`JweDecryptOptions::new`] to pin an explicit allow-list of key
/// management algorithms and content encryption algorithms. Tokens whose
/// header advertises anything outside the allow-list are rejected before
/// any cryptographic operation — the canonical defence against
/// algorithm-substitution attacks when one key is reused across configs.
#[derive(Debug, Clone, Default)]
pub struct JweDecryptOptions {
    allowed_alg: Vec<JweAlgorithm>,
    allowed_enc: Vec<JweEncryption>,
    allow_all: bool,
}

impl JweDecryptOptions {
    /// Construct options pinning an explicit allow-list.
    pub fn new(allowed_alg: Vec<JweAlgorithm>, allowed_enc: Vec<JweEncryption>) -> Self {
        Self {
            allowed_alg,
            allowed_enc,
            allow_all: false,
        }
    }

    /// Permit every algorithm the library supports. Used internally by
    /// [`decrypt`] for backwards compatibility — new code should prefer
    /// [`JweDecryptOptions::new`].
    pub fn permissive() -> Self {
        Self {
            allowed_alg: Vec::new(),
            allowed_enc: Vec::new(),
            allow_all: true,
        }
    }

    fn check(&self, alg: JweAlgorithm, enc: JweEncryption) -> Result<()> {
        if self.allow_all {
            return Ok(());
        }
        if !self.allowed_alg.contains(&alg) {
            return Err(JoseError::UnsupportedAlgorithm(format!(
                "JWE alg {} is not in the caller's allow-list",
                alg.as_str()
            )));
        }
        if !self.allowed_enc.contains(&enc) {
            return Err(JoseError::UnsupportedAlgorithm(format!(
                "JWE enc {} is not in the caller's allow-list",
                enc.as_str()
            )));
        }
        Ok(())
    }
}

/// Decrypt a JWE Compact Serialization token, enforcing the caller's
/// algorithm allow-list.
///
/// Rejects the token before any cryptographic operation if its `alg` or
/// `enc` header is outside the allow-list supplied via `options`.
pub fn decrypt_with_options(
    key: &[u8],
    token: &str,
    options: &JweDecryptOptions,
) -> Result<Vec<u8>> {
    ensure_token_size(token)?;
    // 1. Parse compact serialization.
    let parts: Vec<&str> = token.splitn(6, '.').collect();
    if parts.len() != 5 {
        return Err(JoseError::InvalidToken(
            "JWE compact serialization must have 5 dot-separated parts".into(),
        ));
    }

    let header_b64 = parts[0];
    let encrypted_key_b64 = parts[1];
    let iv_b64 = parts[2];
    let ciphertext_b64 = parts[3];
    let tag_b64 = parts[4];

    // 2. Decode header.
    let header_json = base64url::decode(header_b64)?;
    let header: JoseHeader = serde_json::from_slice(&header_json)?;

    // RFC 7516 §4.1.13 / RFC 7515 §4.1.11: reject unknown crit extensions.
    if let Some(crit) = &header.crit {
        if !crit.is_empty() {
            return Err(JoseError::InvalidHeader(format!(
                "unsupported crit extensions: {crit:?}"
            )));
        }
    }

    let alg = JweAlgorithm::from_str(&header.alg)?;
    let enc_str = header
        .enc
        .as_deref()
        .ok_or_else(|| JoseError::InvalidHeader("missing enc field".into()))?;
    let enc = JweEncryption::from_str(enc_str)?;

    // Enforce caller's allow-list before touching any key material.
    options.check(alg, enc)?;

    // 3. Decode parts.
    let encrypted_key = base64url::decode(encrypted_key_b64)?;
    let iv = base64url::decode(iv_b64)?;
    let ciphertext = base64url::decode(ciphertext_b64)?;
    let tag = base64url::decode(tag_b64)?;

    // 4. Recover CEK.
    let cek = recover_cek(key, alg, enc, &encrypted_key)?;

    // 5. Content decryption.
    // AAD = ASCII(BASE64URL(header)).
    let aad = header_b64.as_bytes();
    content_decrypt(enc, &cek, &iv, &ciphertext, &tag, aad)
}

/// Decode the protected header from a JWE compact token without decrypting.
pub fn decode_header(token: &str) -> Result<JoseHeader> {
    ensure_token_size(token)?;
    let header_b64 = token
        .split('.')
        .next()
        .ok_or_else(|| JoseError::InvalidToken("empty token".into()))?;
    let header_json = base64url::decode(header_b64)?;
    serde_json::from_slice(&header_json).map_err(Into::into)
}

// ---------------------------------------------------------------------------
// Key management: produce CEK (encrypt side)
// ---------------------------------------------------------------------------

/// Returns `(cek, encrypted_key)`. For `dir`, `encrypted_key` is empty.
///
/// The CEK is returned wrapped in `Zeroizing` so its heap buffer is
/// wiped when it goes out of scope — key material never lingers in
/// the allocator after the encryption completes.
fn produce_cek(
    key: &[u8],
    alg: JweAlgorithm,
    enc: JweEncryption,
) -> Result<(Zeroizing<Vec<u8>>, Vec<u8>)> {
    let cek_len = enc.cek_size();

    match alg {
        // Direct: the provided key *is* the CEK.
        JweAlgorithm::Dir => {
            if key.len() != cek_len {
                return Err(JoseError::Key(format!(
                    "dir: key length {} does not match required CEK size {}",
                    key.len(),
                    cek_len
                )));
            }
            Ok((Zeroizing::new(key.to_vec()), Vec::new()))
        }

        // AES Key Wrap.
        JweAlgorithm::A128KW | JweAlgorithm::A192KW | JweAlgorithm::A256KW => {
            check_kek_size(alg, key)?;
            let kw_alg = jwe_alg_to_keywrap(alg)?;
            let cek = Zeroizing::new(random_bytes(cek_len));
            let wrapped =
                kryptering::keywrap::wrap(kw_alg, key, &cek).map_err(JoseError::Crypto)?;
            Ok((cek, wrapped))
        }

        // RSA-OAEP.
        JweAlgorithm::RsaOaep256 => {
            let pub_key = parse_rsa_public_key(key)?;
            let kt_alg = jwe_alg_to_keytransport(alg)?;
            let cek = Zeroizing::new(random_bytes(cek_len));
            let encrypted = kryptering::keytransport::kt_encrypt(kt_alg, &pub_key, &cek, None)
                .map_err(JoseError::Crypto)?;
            Ok((cek, encrypted))
        }

        #[cfg(feature = "deprecated")]
        JweAlgorithm::RsaOaep => {
            let pub_key = parse_rsa_public_key(key)?;
            let kt_alg = jwe_alg_to_keytransport(alg)?;
            let cek = Zeroizing::new(random_bytes(cek_len));
            let encrypted = kryptering::keytransport::kt_encrypt(kt_alg, &pub_key, &cek, None)
                .map_err(JoseError::Crypto)?;
            Ok((cek, encrypted))
        }

        _ => Err(JoseError::UnsupportedAlgorithm(format!(
            "key management algorithm {} not yet implemented",
            alg.as_str()
        ))),
    }
}

// ---------------------------------------------------------------------------
// Key management: recover CEK (decrypt side)
// ---------------------------------------------------------------------------

fn recover_cek(
    key: &[u8],
    alg: JweAlgorithm,
    enc: JweEncryption,
    encrypted_key: &[u8],
) -> Result<Zeroizing<Vec<u8>>> {
    let cek_len = enc.cek_size();

    match alg {
        JweAlgorithm::Dir => {
            if !encrypted_key.is_empty() {
                return Err(JoseError::InvalidToken(
                    "dir: encrypted key must be empty".into(),
                ));
            }
            if key.len() != cek_len {
                return Err(JoseError::Key(format!(
                    "dir: key length {} does not match required CEK size {}",
                    key.len(),
                    cek_len
                )));
            }
            Ok(Zeroizing::new(key.to_vec()))
        }

        JweAlgorithm::A128KW | JweAlgorithm::A192KW | JweAlgorithm::A256KW => {
            check_kek_size(alg, key)?;
            let kw_alg = jwe_alg_to_keywrap(alg)?;
            let cek = Zeroizing::new(
                kryptering::keywrap::unwrap(kw_alg, key, encrypted_key)
                    .map_err(JoseError::Crypto)?,
            );
            if cek.len() != cek_len {
                return Err(JoseError::Key(format!(
                    "unwrapped CEK length {} does not match expected {}",
                    cek.len(),
                    cek_len
                )));
            }
            Ok(cek)
        }

        JweAlgorithm::RsaOaep256 => rsa_oaep_recover_cek(key, alg, encrypted_key, cek_len),

        #[cfg(feature = "deprecated")]
        JweAlgorithm::RsaOaep => rsa_oaep_recover_cek(key, alg, encrypted_key, cek_len),

        _ => Err(JoseError::UnsupportedAlgorithm(format!(
            "key management algorithm {} not yet implemented",
            alg.as_str()
        ))),
    }
}

/// Shared RSA-OAEP CEK recovery (used by both RSA-OAEP-256 and the deprecated
/// SHA-1 RSA-OAEP variant). Post-decrypt error paths are deliberately merged
/// into a single opaque error to avoid distinguishable oracles. The recovered
/// CEK is wrapped in `Zeroizing` so it is wiped when dropped.
fn rsa_oaep_recover_cek(
    key: &[u8],
    alg: JweAlgorithm,
    encrypted_key: &[u8],
    cek_len: usize,
) -> Result<Zeroizing<Vec<u8>>> {
    let priv_key = parse_rsa_private_key(key)?;
    let kt_alg = jwe_alg_to_keytransport(alg)?;
    let cek_result = kryptering::keytransport::kt_decrypt(kt_alg, &priv_key, encrypted_key, None);
    let opaque_err = || {
        JoseError::Crypto(kryptering::Error::Crypto(
            "RSA-OAEP key unwrap failed".into(),
        ))
    };
    let cek = Zeroizing::new(cek_result.map_err(|_| opaque_err())?);
    if cek.len() != cek_len {
        return Err(opaque_err());
    }
    Ok(cek)
}

// ---------------------------------------------------------------------------
// Content encryption
// ---------------------------------------------------------------------------

/// Encrypt plaintext and return `(iv, ciphertext, tag)`.
fn content_encrypt(
    enc: JweEncryption,
    cek: &[u8],
    plaintext: &[u8],
    aad: &[u8],
) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>)> {
    match enc {
        JweEncryption::A128GCM | JweEncryption::A192GCM | JweEncryption::A256GCM => {
            aes_gcm_encrypt(cek, plaintext, aad)
        }
        JweEncryption::A128CbcHs256 | JweEncryption::A192CbcHs384 | JweEncryption::A256CbcHs512 => {
            aes_cbc_hs_encrypt(enc, cek, plaintext, aad)
        }
    }
}

/// Decrypt and return plaintext.
fn content_decrypt(
    enc: JweEncryption,
    cek: &[u8],
    iv: &[u8],
    ciphertext: &[u8],
    tag: &[u8],
    aad: &[u8],
) -> Result<Vec<u8>> {
    match enc {
        JweEncryption::A128GCM | JweEncryption::A192GCM | JweEncryption::A256GCM => {
            aes_gcm_decrypt(cek, iv, ciphertext, tag, aad)
        }
        JweEncryption::A128CbcHs256 | JweEncryption::A192CbcHs384 | JweEncryption::A256CbcHs512 => {
            aes_cbc_hs_decrypt(enc, cek, iv, ciphertext, tag, aad)
        }
    }
}

// ---------------------------------------------------------------------------
// AES-GCM content encryption (RFC 7518 Section 5.3)
// ---------------------------------------------------------------------------

fn aes_gcm_encrypt(
    cek: &[u8],
    plaintext: &[u8],
    aad: &[u8],
) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>)> {
    use aes_gcm::aead::AeadInPlace;
    use aes_gcm::{KeyInit, Nonce};

    let mut nonce_bytes = [0u8; 12];
    rand::thread_rng().fill_bytes(&mut nonce_bytes);
    let nonce = Nonce::from_slice(&nonce_bytes);

    let mut buffer = plaintext.to_vec();

    let gcm_err =
        |e: aes_gcm::Error| JoseError::Crypto(kryptering::Error::Crypto(format!("AES-GCM: {e}")));
    let key_err = |e| JoseError::Key(format!("AES-GCM key init: {e}"));

    let tag_bytes = match cek.len() {
        16 => {
            let cipher = aes_gcm::Aes128Gcm::new_from_slice(cek).map_err(key_err)?;
            cipher
                .encrypt_in_place_detached(nonce, aad, &mut buffer)
                .map_err(gcm_err)?
        }
        24 => {
            use aes_gcm::aead::consts::U12;
            let cipher =
                aes_gcm::AesGcm::<aes::Aes192, U12>::new_from_slice(cek).map_err(key_err)?;
            cipher
                .encrypt_in_place_detached(nonce, aad, &mut buffer)
                .map_err(gcm_err)?
        }
        32 => {
            let cipher = aes_gcm::Aes256Gcm::new_from_slice(cek).map_err(key_err)?;
            cipher
                .encrypt_in_place_detached(nonce, aad, &mut buffer)
                .map_err(gcm_err)?
        }
        other => {
            return Err(JoseError::Key(format!("invalid AES-GCM key size: {other}")));
        }
    };

    Ok((nonce_bytes.to_vec(), buffer, tag_bytes.to_vec()))
}

fn aes_gcm_decrypt(
    cek: &[u8],
    iv: &[u8],
    ciphertext: &[u8],
    tag: &[u8],
    aad: &[u8],
) -> Result<Vec<u8>> {
    use aes_gcm::aead::AeadInPlace;
    use aes_gcm::{KeyInit, Nonce, Tag};

    if iv.len() != 12 {
        return Err(JoseError::InvalidToken(format!(
            "AES-GCM IV must be 12 bytes, got {}",
            iv.len()
        )));
    }
    if tag.len() != 16 {
        return Err(JoseError::InvalidToken(format!(
            "AES-GCM tag must be 16 bytes, got {}",
            tag.len()
        )));
    }

    let nonce = Nonce::from_slice(iv);
    let auth_tag = Tag::from_slice(tag);
    let mut buffer = ciphertext.to_vec();

    let auth_fail = || {
        JoseError::Crypto(kryptering::Error::Crypto(
            "AES-GCM authentication failed".into(),
        ))
    };
    let key_err = |e| JoseError::Key(format!("AES-GCM key init: {e}"));

    match cek.len() {
        16 => {
            let cipher = aes_gcm::Aes128Gcm::new_from_slice(cek).map_err(key_err)?;
            cipher
                .decrypt_in_place_detached(nonce, aad, &mut buffer, auth_tag)
                .map_err(|_| auth_fail())?;
        }
        24 => {
            use aes_gcm::aead::consts::U12;
            let cipher =
                aes_gcm::AesGcm::<aes::Aes192, U12>::new_from_slice(cek).map_err(key_err)?;
            cipher
                .decrypt_in_place_detached(nonce, aad, &mut buffer, auth_tag)
                .map_err(|_| auth_fail())?;
        }
        32 => {
            let cipher = aes_gcm::Aes256Gcm::new_from_slice(cek).map_err(key_err)?;
            cipher
                .decrypt_in_place_detached(nonce, aad, &mut buffer, auth_tag)
                .map_err(|_| auth_fail())?;
        }
        other => {
            return Err(JoseError::Key(format!("invalid AES-GCM key size: {other}")));
        }
    }

    Ok(buffer)
}

// ---------------------------------------------------------------------------
// AES-CBC-HS content encryption (RFC 7518 Section 5.2)
// ---------------------------------------------------------------------------

/// Returns `(aes_key_size_bytes, hmac_tag_truncation_bytes)` for each variant.
fn cbc_hs_params(enc: JweEncryption) -> (usize, usize) {
    match enc {
        JweEncryption::A128CbcHs256 => (16, 16), // AES-128, HMAC-SHA-256, tag = 16
        JweEncryption::A192CbcHs384 => (24, 24), // AES-192, HMAC-SHA-384, tag = 24
        JweEncryption::A256CbcHs512 => (32, 32), // AES-256, HMAC-SHA-512, tag = 32
        _ => unreachable!(),
    }
}

fn aes_cbc_hs_encrypt(
    enc: JweEncryption,
    cek: &[u8],
    plaintext: &[u8],
    aad: &[u8],
) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>)> {
    let (aes_key_size, tag_len) = cbc_hs_params(enc);
    let expected_cek_len = aes_key_size * 2; // CEK = MAC_KEY || ENC_KEY

    if cek.len() != expected_cek_len {
        return Err(JoseError::Key(format!(
            "AES-CBC-HS: expected {} byte CEK, got {}",
            expected_cek_len,
            cek.len()
        )));
    }

    // Split CEK: first half is MAC key, second half is encryption key.
    let mac_key = &cek[..aes_key_size];
    let enc_key = &cek[aes_key_size..];

    // Generate random 128-bit IV.
    let mut iv = [0u8; 16];
    rand::thread_rng().fill_bytes(&mut iv);

    // PKCS#7 pad and encrypt with AES-CBC.
    let ciphertext = aes_cbc_encrypt_raw(enc_key, &iv, plaintext)?;

    // Compute HMAC tag: HMAC(AAD || IV || ciphertext || AAD_len_bits).
    let tag = compute_cbc_hs_tag(enc, mac_key, aad, &iv, &ciphertext, tag_len)?;

    Ok((iv.to_vec(), ciphertext, tag))
}

fn aes_cbc_hs_decrypt(
    enc: JweEncryption,
    cek: &[u8],
    iv: &[u8],
    ciphertext: &[u8],
    tag: &[u8],
    aad: &[u8],
) -> Result<Vec<u8>> {
    let (aes_key_size, tag_len) = cbc_hs_params(enc);
    let expected_cek_len = aes_key_size * 2;

    if cek.len() != expected_cek_len {
        return Err(JoseError::Key(format!(
            "AES-CBC-HS: expected {} byte CEK, got {}",
            expected_cek_len,
            cek.len()
        )));
    }

    if iv.len() != 16 {
        return Err(JoseError::InvalidToken(format!(
            "AES-CBC IV must be 16 bytes, got {}",
            iv.len()
        )));
    }

    let mac_key = &cek[..aes_key_size];
    let enc_key = &cek[aes_key_size..];

    // Verify HMAC tag first (encrypt-then-MAC: verify before decrypting).
    let expected_tag = compute_cbc_hs_tag(enc, mac_key, aad, iv, ciphertext, tag_len)?;

    // Constant-time comparison.
    if !constant_time_eq(tag, &expected_tag) {
        return Err(JoseError::Crypto(kryptering::Error::Crypto(
            "AES-CBC-HS authentication tag mismatch".into(),
        )));
    }

    // Decrypt AES-CBC and remove PKCS#7 padding.
    aes_cbc_decrypt_raw(enc_key, iv, ciphertext)
}

/// Compute the HMAC authentication tag for AES-CBC-HS (RFC 7518 Section 5.2.2).
///
/// Input to HMAC: AAD || IV || ciphertext || AAD_len_in_bits (big-endian u64).
fn compute_cbc_hs_tag(
    enc: JweEncryption,
    mac_key: &[u8],
    aad: &[u8],
    iv: &[u8],
    ciphertext: &[u8],
    tag_len: usize,
) -> Result<Vec<u8>> {
    use hmac::Mac;

    // AAD length in bits as big-endian u64.
    let aad_len_bits = (aad.len() as u64) * 8;
    let al = aad_len_bits.to_be_bytes();

    let full_mac = match enc {
        JweEncryption::A128CbcHs256 => {
            let mut mac = hmac::Hmac::<sha2::Sha256>::new_from_slice(mac_key)
                .map_err(|e| JoseError::Key(format!("HMAC init: {e}")))?;
            mac.update(aad);
            mac.update(iv);
            mac.update(ciphertext);
            mac.update(&al);
            mac.finalize().into_bytes().to_vec()
        }
        JweEncryption::A192CbcHs384 => {
            let mut mac = hmac::Hmac::<sha2::Sha384>::new_from_slice(mac_key)
                .map_err(|e| JoseError::Key(format!("HMAC init: {e}")))?;
            mac.update(aad);
            mac.update(iv);
            mac.update(ciphertext);
            mac.update(&al);
            mac.finalize().into_bytes().to_vec()
        }
        JweEncryption::A256CbcHs512 => {
            let mut mac = hmac::Hmac::<sha2::Sha512>::new_from_slice(mac_key)
                .map_err(|e| JoseError::Key(format!("HMAC init: {e}")))?;
            mac.update(aad);
            mac.update(iv);
            mac.update(ciphertext);
            mac.update(&al);
            mac.finalize().into_bytes().to_vec()
        }
        _ => unreachable!(),
    };

    // Authentication tag = first tag_len bytes of HMAC output.
    Ok(full_mac[..tag_len].to_vec())
}

// ---------------------------------------------------------------------------
// Raw AES-CBC helpers (PKCS#7 padding)
// ---------------------------------------------------------------------------

fn aes_cbc_encrypt_raw(key: &[u8], iv: &[u8], plaintext: &[u8]) -> Result<Vec<u8>> {
    use cbc::cipher::{BlockEncryptMut, KeyIvInit};

    let padded = pkcs7_pad(plaintext, 16);
    let padded_len = padded.len();
    let mut buf = padded;

    macro_rules! do_encrypt {
        ($aes:ty) => {{
            let enc = cbc::Encryptor::<$aes>::new_from_slices(key, iv)
                .map_err(|e| JoseError::Key(format!("AES-CBC init: {e}")))?;
            enc.encrypt_padded_mut::<cbc::cipher::block_padding::NoPadding>(&mut buf, padded_len)
                .map_err(|e| {
                    JoseError::Crypto(kryptering::Error::Crypto(format!("AES-CBC encrypt: {e}")))
                })?;
        }};
    }

    match key.len() {
        16 => do_encrypt!(aes::Aes128),
        24 => do_encrypt!(aes::Aes192),
        32 => do_encrypt!(aes::Aes256),
        other => {
            return Err(JoseError::Key(format!("invalid AES key size: {other}")));
        }
    }

    Ok(buf)
}

fn aes_cbc_decrypt_raw(key: &[u8], iv: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>> {
    use cbc::cipher::{BlockDecryptMut, KeyIvInit};

    if ciphertext.is_empty() || ciphertext.len() % 16 != 0 {
        return Err(JoseError::InvalidToken(
            "AES-CBC ciphertext length must be a multiple of 16".into(),
        ));
    }

    let mut buf = ciphertext.to_vec();

    macro_rules! do_decrypt {
        ($aes:ty) => {{
            let dec = cbc::Decryptor::<$aes>::new_from_slices(key, iv)
                .map_err(|e| JoseError::Key(format!("AES-CBC init: {e}")))?;
            dec.decrypt_padded_mut::<cbc::cipher::block_padding::NoPadding>(&mut buf)
                .map_err(|e| {
                    JoseError::Crypto(kryptering::Error::Crypto(format!("AES-CBC decrypt: {e}")))
                })?;
        }};
    }

    match key.len() {
        16 => do_decrypt!(aes::Aes128),
        24 => do_decrypt!(aes::Aes192),
        32 => do_decrypt!(aes::Aes256),
        other => {
            return Err(JoseError::Key(format!("invalid AES key size: {other}")));
        }
    }

    pkcs7_unpad(&buf)
}

/// Apply PKCS#7 padding.
fn pkcs7_pad(data: &[u8], block_size: usize) -> Vec<u8> {
    let pad_len = block_size - (data.len() % block_size);
    let mut padded = Vec::with_capacity(data.len() + pad_len);
    padded.extend_from_slice(data);
    padded.extend(std::iter::repeat_n(pad_len as u8, pad_len));
    padded
}

/// Remove PKCS#7 padding.
///
/// The padding bytes are validated in constant time over a fixed 16-byte
/// window — the decision about padding validity does not branch on the
/// trailing-byte value once the input length is known. Mitigated further
/// upstream by encrypt-then-MAC (HMAC is verified before this function
/// runs), but kept constant-time as defence in depth.
fn pkcs7_unpad(data: &[u8]) -> Result<Vec<u8>> {
    use subtle::ConstantTimeEq;

    const BLOCK: usize = 16;

    // Structural checks on the *length* of the buffer are not secret-dependent
    // (the attacker already controls the ciphertext length) and are safe to
    // branch on.
    if data.is_empty() || data.len() % BLOCK != 0 {
        return Err(JoseError::Crypto(kryptering::Error::Crypto(
            "invalid PKCS#7 padding".into(),
        )));
    }

    let pad_byte = *data.last().unwrap();
    let pad_len_u = pad_byte as u16;

    // `good` accumulates validity as 0xFF (valid) or 0x00 (invalid).
    let mut good: u8 = 0xFF;

    // Branch-free length check. pad_len is valid iff 1 <= pad_byte <= BLOCK.
    //   is_zero_bit : 1 iff pad_byte == 0     (from top bit of (p - 1))
    //   over_block_bit : 1 iff pad_byte > BLOCK  (top bit of ((BLOCK + 1) - 1 - p) after widening)
    let is_zero_bit: u16 = pad_len_u.wrapping_sub(1) >> 15;
    let over_block_bit: u16 = ((BLOCK as u16).wrapping_sub(pad_len_u)) >> 15;
    let invalid_bit: u16 = is_zero_bit | over_block_bit;
    let invalid_mask: u8 = 0u8.wrapping_sub(invalid_bit as u8);
    good &= !invalid_mask;

    // Compare every byte of the final block against pad_byte. For positions
    // whose distance from the end is strictly less than pad_len, the byte
    // MUST equal pad_byte; otherwise the comparison contributes nothing.
    let last_block = &data[data.len() - BLOCK..];
    for (i, &b) in last_block.iter().enumerate() {
        // distance_from_end in [1, BLOCK].
        let dist = (BLOCK - i) as u16;
        // in_region mask: 0xFF iff dist <= pad_len, else 0x00. Branch-free:
        // dist - pad_len - 1 underflows (top bit = 1) when dist <= pad_len.
        let in_region_bit: u16 = (dist.wrapping_sub(pad_len_u).wrapping_sub(1)) >> 15;
        let in_region: u8 = 0u8.wrapping_sub(in_region_bit as u8);

        // ct_eq → Choice → 0/1 → full-byte mask.
        let eq_bit = b.ct_eq(&pad_byte).unwrap_u8();
        let eq_mask: u8 = 0u8.wrapping_sub(eq_bit);

        // Contribution is 0xFF unless we're in the region AND the byte disagrees.
        let contribution = !in_region | eq_mask;
        good &= contribution;
    }

    if good != 0xFF {
        return Err(JoseError::Crypto(kryptering::Error::Crypto(
            "invalid PKCS#7 padding".into(),
        )));
    }
    Ok(data[..data.len() - pad_byte as usize].to_vec())
}

// ---------------------------------------------------------------------------
// Algorithm mapping helpers
// ---------------------------------------------------------------------------

/// Verify that the supplied Key Encryption Key is exactly the size
/// demanded by the declared AES Key Wrap algorithm.
///
/// Prevents silent acceptance of a 32-byte key for `A128KW` (or similar
/// mis-sized KEKs) if the underlying crypto backend does not validate
/// strictly.
fn check_kek_size(alg: JweAlgorithm, key: &[u8]) -> Result<()> {
    let expected = match alg {
        JweAlgorithm::A128KW => 16,
        JweAlgorithm::A192KW => 24,
        JweAlgorithm::A256KW => 32,
        _ => return Ok(()),
    };
    if key.len() != expected {
        return Err(JoseError::Key(format!(
            "{}: KEK must be {} bytes, got {}",
            alg.as_str(),
            expected,
            key.len()
        )));
    }
    Ok(())
}

fn jwe_alg_to_keywrap(alg: JweAlgorithm) -> Result<kryptering::KeyWrapAlgorithm> {
    use kryptering::{AesKeySize, KeyWrapAlgorithm};
    match alg {
        JweAlgorithm::A128KW => Ok(KeyWrapAlgorithm::AesKw(AesKeySize::Aes128)),
        JweAlgorithm::A192KW => Ok(KeyWrapAlgorithm::AesKw(AesKeySize::Aes192)),
        JweAlgorithm::A256KW => Ok(KeyWrapAlgorithm::AesKw(AesKeySize::Aes256)),
        _ => Err(JoseError::UnsupportedAlgorithm(format!(
            "{} is not a key wrap algorithm",
            alg.as_str()
        ))),
    }
}

fn jwe_alg_to_keytransport(alg: JweAlgorithm) -> Result<kryptering::KeyTransportAlgorithm> {
    use kryptering::{HashAlgorithm, KeyTransportAlgorithm, OaepConfig};
    match alg {
        #[cfg(feature = "deprecated")]
        JweAlgorithm::RsaOaep => Ok(KeyTransportAlgorithm::RsaOaep(OaepConfig {
            digest: HashAlgorithm::Sha1,
            mgf_digest: HashAlgorithm::Sha1,
        })),
        JweAlgorithm::RsaOaep256 => Ok(KeyTransportAlgorithm::RsaOaep(OaepConfig {
            digest: HashAlgorithm::Sha256,
            mgf_digest: HashAlgorithm::Sha256,
        })),
        _ => Err(JoseError::UnsupportedAlgorithm(format!(
            "{} is not a key transport algorithm",
            alg.as_str()
        ))),
    }
}

// ---------------------------------------------------------------------------
// RSA key parsing
// ---------------------------------------------------------------------------

fn parse_rsa_public_key(der: &[u8]) -> Result<rsa::RsaPublicKey> {
    use rsa::pkcs8::DecodePublicKey;
    use rsa::traits::PublicKeyParts;
    let key = rsa::RsaPublicKey::from_public_key_der(der)
        .map_err(|e| JoseError::Key(format!("failed to parse RSA public key DER: {e}")))?;
    if key.n().bits() < crate::MIN_RSA_BITS {
        return Err(JoseError::Key(format!(
            "RSA key size {} bits is below the required minimum of {}",
            key.n().bits(),
            crate::MIN_RSA_BITS
        )));
    }
    Ok(key)
}

fn parse_rsa_private_key(der: &[u8]) -> Result<rsa::RsaPrivateKey> {
    use rsa::pkcs8::DecodePrivateKey;
    use rsa::traits::PublicKeyParts;
    let key = rsa::RsaPrivateKey::from_pkcs8_der(der)
        .map_err(|e| JoseError::Key(format!("failed to parse RSA private key PKCS#8 DER: {e}")))?;
    if key.n().bits() < crate::MIN_RSA_BITS {
        return Err(JoseError::Key(format!(
            "RSA key size {} bits is below the required minimum of {}",
            key.n().bits(),
            crate::MIN_RSA_BITS
        )));
    }
    Ok(key)
}

// ---------------------------------------------------------------------------
// Utility
// ---------------------------------------------------------------------------

fn random_bytes(len: usize) -> Vec<u8> {
    let mut buf = vec![0u8; len];
    rand::thread_rng().fill_bytes(&mut buf);
    buf
}

/// Constant-time byte comparison to prevent timing attacks on HMAC tags.
///
/// Uses the audited `subtle` crate. The length-inequality branch is safe
/// because HMAC tag lengths are fixed per `enc` algorithm — mismatched
/// lengths indicate a malformed token, not a secret-dependent value.
fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    use subtle::ConstantTimeEq;
    if a.len() != b.len() {
        return false;
    }
    a.ct_eq(b).into()
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::algorithm::{JweAlgorithm, JweEncryption};

    // -- dir + AES-GCM roundtrips --

    #[test]
    fn dir_a256gcm_roundtrip() {
        let cek = [0x42u8; 32]; // 256-bit CEK for A256GCM
        let plaintext = b"Hello, JWE world!";

        let token = encrypt(&cek, plaintext, JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();

        // Should have 5 dot-separated parts.
        assert_eq!(token.split('.').count(), 5);

        // Encrypted key part should be empty for dir.
        let parts: Vec<&str> = token.splitn(5, '.').collect();
        assert_eq!(parts[1], "", "dir: encrypted key must be empty");

        let recovered = decrypt(&cek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    #[test]
    fn dir_a128gcm_roundtrip() {
        let cek = [0x33u8; 16];
        let plaintext = b"AES-128-GCM test";

        let token = encrypt(&cek, plaintext, JweAlgorithm::Dir, JweEncryption::A128GCM).unwrap();
        let recovered = decrypt(&cek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    #[test]
    fn dir_a192gcm_roundtrip() {
        let cek = [0x55u8; 24];
        let plaintext = b"AES-192-GCM test";

        let token = encrypt(&cek, plaintext, JweAlgorithm::Dir, JweEncryption::A192GCM).unwrap();
        let recovered = decrypt(&cek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    // -- AES Key Wrap + AES-GCM roundtrips --

    #[test]
    fn a256kw_a128gcm_roundtrip() {
        let kek = [0xABu8; 32]; // 256-bit KEK
        let plaintext = b"Wrapped key encryption test";

        let token = encrypt(
            &kek,
            plaintext,
            JweAlgorithm::A256KW,
            JweEncryption::A128GCM,
        )
        .unwrap();

        // Encrypted key part must not be empty for key wrapping.
        let parts: Vec<&str> = token.splitn(5, '.').collect();
        assert!(
            !parts[1].is_empty(),
            "A256KW: encrypted key must not be empty"
        );

        let recovered = decrypt(&kek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    #[test]
    fn a128kw_a256gcm_roundtrip() {
        let kek = [0xCDu8; 16]; // 128-bit KEK
        let plaintext = b"A128KW + A256GCM";

        let token = encrypt(
            &kek,
            plaintext,
            JweAlgorithm::A128KW,
            JweEncryption::A256GCM,
        )
        .unwrap();
        let recovered = decrypt(&kek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    #[test]
    fn a192kw_a256gcm_roundtrip() {
        let kek = [0xEFu8; 24]; // 192-bit KEK
        let plaintext = b"A192KW + A256GCM";

        let token = encrypt(
            &kek,
            plaintext,
            JweAlgorithm::A192KW,
            JweEncryption::A256GCM,
        )
        .unwrap();
        let recovered = decrypt(&kek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    // -- RSA-OAEP + AES-GCM roundtrips --

    fn generate_rsa_keypair() -> (Vec<u8>, Vec<u8>) {
        use rsa::pkcs8::{EncodePrivateKey, EncodePublicKey};
        let mut rng = rand::thread_rng();
        let private_key = rsa::RsaPrivateKey::new(&mut rng, 2048).unwrap();
        let public_key = rsa::RsaPublicKey::from(&private_key);

        let pub_der = public_key.to_public_key_der().unwrap();
        let priv_der = private_key.to_pkcs8_der().unwrap();

        (pub_der.as_ref().to_vec(), priv_der.as_bytes().to_vec())
    }

    #[cfg(feature = "deprecated")]
    #[test]
    fn rsa_oaep_a256gcm_roundtrip() {
        let (pub_der, priv_der) = generate_rsa_keypair();
        let plaintext = b"RSA-OAEP encrypted message";

        let token = encrypt(
            &pub_der,
            plaintext,
            JweAlgorithm::RsaOaep,
            JweEncryption::A256GCM,
        )
        .unwrap();
        let recovered = decrypt(&priv_der, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    #[test]
    fn rsa_oaep_256_a256gcm_roundtrip() {
        let (pub_der, priv_der) = generate_rsa_keypair();
        let plaintext = b"RSA-OAEP-256 encrypted message";

        let token = encrypt(
            &pub_der,
            plaintext,
            JweAlgorithm::RsaOaep256,
            JweEncryption::A256GCM,
        )
        .unwrap();
        let recovered = decrypt(&priv_der, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    // -- AES-CBC-HS roundtrips --

    #[test]
    fn a128cbc_hs256_roundtrip() {
        let cek = [0x42u8; 32]; // 32-byte CEK: 16 HMAC + 16 AES
        let plaintext = b"AES-128-CBC-HS256 test";

        let token = encrypt(
            &cek,
            plaintext,
            JweAlgorithm::Dir,
            JweEncryption::A128CbcHs256,
        )
        .unwrap();
        let recovered = decrypt(&cek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    #[test]
    fn a192cbc_hs384_roundtrip() {
        let cek = [0x55u8; 48]; // 48-byte CEK: 24 HMAC + 24 AES
        let plaintext = b"AES-192-CBC-HS384 test";

        let token = encrypt(
            &cek,
            plaintext,
            JweAlgorithm::Dir,
            JweEncryption::A192CbcHs384,
        )
        .unwrap();
        let recovered = decrypt(&cek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    #[test]
    fn a256cbc_hs512_roundtrip() {
        let cek = [0x77u8; 64]; // 64-byte CEK: 32 HMAC + 32 AES
        let plaintext = b"AES-256-CBC-HS512 test";

        let token = encrypt(
            &cek,
            plaintext,
            JweAlgorithm::Dir,
            JweEncryption::A256CbcHs512,
        )
        .unwrap();
        let recovered = decrypt(&cek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    #[test]
    fn a256kw_a128cbc_hs256_roundtrip() {
        let kek = [0xBBu8; 32]; // 256-bit KEK
        let plaintext = b"Key-wrapped CBC-HS test";

        let token = encrypt(
            &kek,
            plaintext,
            JweAlgorithm::A256KW,
            JweEncryption::A128CbcHs256,
        )
        .unwrap();
        let recovered = decrypt(&kek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    // -- Error cases --

    #[test]
    fn wrong_key_fails_decryption_gcm() {
        let key1 = [0x42u8; 32];
        let key2 = [0x99u8; 32];
        let plaintext = b"secret data";

        let token = encrypt(&key1, plaintext, JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();
        let result = decrypt(&key2, &token);
        assert!(result.is_err(), "decryption with wrong key should fail");
    }

    #[test]
    fn wrong_key_fails_decryption_cbc_hs() {
        let key1 = [0x42u8; 32];
        let key2 = [0x99u8; 32];
        let plaintext = b"secret data";

        let token = encrypt(
            &key1,
            plaintext,
            JweAlgorithm::Dir,
            JweEncryption::A128CbcHs256,
        )
        .unwrap();
        let result = decrypt(&key2, &token);
        assert!(result.is_err(), "decryption with wrong key should fail");
    }

    #[test]
    fn malformed_token_returns_error() {
        let key = [0x42u8; 32];

        // Too few parts.
        let result = decrypt(&key, "one.two.three");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("5 dot-separated parts"),
            "unexpected error: {err}"
        );

        // Empty string.
        let result = decrypt(&key, "");
        assert!(result.is_err());
    }

    #[test]
    fn tampered_ciphertext_fails_gcm() {
        let key = [0x42u8; 32];
        let plaintext = b"integrity test";

        let token = encrypt(&key, plaintext, JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();

        // Tamper with the ciphertext part (fourth segment).
        let parts: Vec<&str> = token.splitn(5, '.').collect();
        let mut ct_bytes = base64url::decode(parts[3]).unwrap();
        if !ct_bytes.is_empty() {
            ct_bytes[0] ^= 0xFF;
        }
        let tampered_ct = base64url::encode(&ct_bytes);
        let tampered_token = format!(
            "{}.{}.{}.{}.{}",
            parts[0], parts[1], parts[2], tampered_ct, parts[4]
        );

        let result = decrypt(&key, &tampered_token);
        assert!(result.is_err(), "tampered ciphertext should fail GCM auth");
    }

    #[test]
    fn tampered_ciphertext_fails_cbc_hs() {
        let key = [0x42u8; 64];
        let plaintext = b"integrity test cbc";

        let token = encrypt(
            &key,
            plaintext,
            JweAlgorithm::Dir,
            JweEncryption::A256CbcHs512,
        )
        .unwrap();

        // Tamper with the ciphertext part (fourth segment).
        let parts: Vec<&str> = token.splitn(5, '.').collect();
        let mut ct_bytes = base64url::decode(parts[3]).unwrap();
        if !ct_bytes.is_empty() {
            ct_bytes[0] ^= 0xFF;
        }
        let tampered_ct = base64url::encode(&ct_bytes);
        let tampered_token = format!(
            "{}.{}.{}.{}.{}",
            parts[0], parts[1], parts[2], tampered_ct, parts[4]
        );

        let result = decrypt(&key, &tampered_token);
        assert!(
            result.is_err(),
            "tampered ciphertext should fail CBC-HS auth"
        );
    }

    #[test]
    fn dir_wrong_cek_size_fails() {
        let key = [0x42u8; 15]; // Wrong size for any algorithm
        let plaintext = b"test";

        let result = encrypt(&key, plaintext, JweAlgorithm::Dir, JweEncryption::A256GCM);
        assert!(result.is_err());
    }

    #[test]
    fn decode_header_works() {
        let cek = [0x42u8; 32];
        let plaintext = b"header test";

        let token = encrypt(&cek, plaintext, JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();

        let header = decode_header(&token).unwrap();
        assert_eq!(header.alg, "dir");
        assert_eq!(header.enc.as_deref(), Some("A256GCM"));
    }

    #[test]
    fn empty_plaintext_roundtrip() {
        let cek = [0x42u8; 32];
        let plaintext = b"";

        let token = encrypt(&cek, plaintext, JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();
        let recovered = decrypt(&cek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    #[test]
    fn large_plaintext_roundtrip() {
        let cek = [0x42u8; 32];
        let plaintext: Vec<u8> = (0..10_000).map(|i| (i % 256) as u8).collect();

        let token = encrypt(&cek, &plaintext, JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();
        let recovered = decrypt(&cek, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    /// Phase 9: encrypt_with_jwk + decrypt_with_jwk roundtrip, dir + A256GCM.
    #[test]
    fn jwk_dir_roundtrip() {
        let mut jwk = crate::jwk::generate_symmetric(32).unwrap();
        jwk.alg = Some("dir".into());
        let plaintext = b"jwk-dir";

        let token = encrypt_with_jwk(&jwk, plaintext, JweEncryption::A256GCM).unwrap();
        let recovered = decrypt_with_jwk(&jwk, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    /// Phase 9: encrypt_with_jwk + decrypt_with_jwk roundtrip, A256KW + A128GCM.
    #[test]
    fn jwk_aes_kw_roundtrip() {
        let mut jwk = crate::jwk::generate_symmetric(32).unwrap();
        jwk.alg = Some("A256KW".into());
        let plaintext = b"jwk-a256kw";

        let token = encrypt_with_jwk(&jwk, plaintext, JweEncryption::A128GCM).unwrap();
        let recovered = decrypt_with_jwk(&jwk, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    /// Phase 9: encrypt_with_jwk + decrypt_with_jwk roundtrip, RSA-OAEP-256.
    #[test]
    fn jwk_rsa_oaep_roundtrip() {
        let mut jwk = crate::jwk::generate_rsa(2048).unwrap();
        jwk.alg = Some("RSA-OAEP-256".into());
        let plaintext = b"jwk-rsa-oaep";

        let token = encrypt_with_jwk(&jwk, plaintext, JweEncryption::A256GCM).unwrap();
        let recovered = decrypt_with_jwk(&jwk, &token).unwrap();
        assert_eq!(recovered, plaintext);
    }

    /// Phase 9: encrypt_with_jwk rejects JWK with no alg.
    #[test]
    fn jwk_encrypt_requires_alg() {
        let jwk = crate::jwk::generate_symmetric(32).unwrap();
        let err = encrypt_with_jwk(&jwk, b"p", JweEncryption::A256GCM)
            .unwrap_err()
            .to_string();
        assert!(err.contains("alg must be set"), "unexpected: {err}");
    }

    /// Phase 9: encrypt_with_jwk honours use="sig" → reject.
    #[test]
    fn jwk_encrypt_rejects_use_sig() {
        let mut jwk = crate::jwk::generate_symmetric(32).unwrap();
        jwk.alg = Some("dir".into());
        jwk.use_ = Some("sig".into());
        let err = encrypt_with_jwk(&jwk, b"p", JweEncryption::A256GCM)
            .unwrap_err()
            .to_string();
        assert!(err.contains("`use` is sig"), "unexpected: {err}");
    }

    /// Phase 9: decrypt_with_jwk rejects use="sig" → reject.
    #[test]
    fn jwk_decrypt_rejects_use_sig() {
        let mut jwk = crate::jwk::generate_symmetric(32).unwrap();
        jwk.alg = Some("dir".into());
        // Encrypt using raw bytes to build a token, then flip use before decrypt.
        let k = crate::base64url::decode(jwk.k.as_ref().unwrap()).unwrap();
        let token = encrypt(&k, b"p", JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();

        jwk.use_ = Some("sig".into());
        let err = decrypt_with_jwk(&jwk, &token).unwrap_err().to_string();
        assert!(err.contains("`use` is sig"), "unexpected: {err}");
    }

    /// Phase 9: decrypt_with_jwk rejects alg mismatch between JWK and token.
    #[test]
    fn jwk_decrypt_pinned_alg_mismatch() {
        // Encrypt with dir, but JWK claims A256KW at decrypt time.
        let jwk_bytes = crate::jwk::generate_symmetric(32).unwrap();
        let k = crate::base64url::decode(jwk_bytes.k.as_ref().unwrap()).unwrap();
        let token = encrypt(&k, b"p", JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();

        let mut jwk = jwk_bytes;
        jwk.alg = Some("A256KW".into());
        let err = decrypt_with_jwk(&jwk, &token).unwrap_err().to_string();
        assert!(
            err.contains("does not match token header alg"),
            "unexpected: {err}"
        );
    }

    /// Phase 3: oversized JWE tokens are rejected before any decoding.
    #[test]
    fn oversize_jwe_token_is_rejected() {
        let big = "a".repeat(crate::MAX_TOKEN_BYTES + 1);
        let cek = [0x42u8; 32];
        let err = decrypt(&cek, &big).unwrap_err().to_string();
        assert!(err.contains("MAX_TOKEN_BYTES"), "unexpected error: {err}");
    }

    #[test]
    fn oversize_jwe_decode_header_is_rejected() {
        let big = "a".repeat(crate::MAX_TOKEN_BYTES + 1);
        let err = decode_header(&big).unwrap_err().to_string();
        assert!(err.contains("MAX_TOKEN_BYTES"), "unexpected error: {err}");
    }

    /// Phase 3: A128KW rejects a 32-byte KEK.
    #[test]
    fn a128kw_rejects_wrong_size_kek_on_encrypt() {
        let too_big = [0x11u8; 32];
        let err = encrypt(&too_big, b"p", JweAlgorithm::A128KW, JweEncryption::A128GCM)
            .unwrap_err()
            .to_string();
        assert!(err.contains("A128KW"), "unexpected error: {err}");
        assert!(err.contains("16 bytes"), "unexpected error: {err}");
    }

    /// Phase 3: A256KW rejects a 16-byte KEK.
    #[test]
    fn a256kw_rejects_wrong_size_kek_on_encrypt() {
        let too_small = [0x22u8; 16];
        let err = encrypt(
            &too_small,
            b"p",
            JweAlgorithm::A256KW,
            JweEncryption::A128GCM,
        )
        .unwrap_err()
        .to_string();
        assert!(err.contains("A256KW"), "unexpected error: {err}");
        assert!(err.contains("32 bytes"), "unexpected error: {err}");
    }

    /// Phase 3: decryption also rejects a mis-sized KEK.
    #[test]
    fn aes_kw_rejects_wrong_size_kek_on_decrypt() {
        // Encrypt with a correct KEK...
        let kek = [0xABu8; 32];
        let token = encrypt(&kek, b"p", JweAlgorithm::A256KW, JweEncryption::A128GCM).unwrap();
        // ...then attempt decryption with a wrong-size key.
        let wrong_size = [0xABu8; 16];
        let err = decrypt(&wrong_size, &token).unwrap_err().to_string();
        assert!(err.contains("A256KW"), "unexpected error: {err}");
        assert!(err.contains("32 bytes"), "unexpected error: {err}");
    }

    /// Phase 2: direct unit tests for the constant-time PKCS#7 unpad implementation.
    #[test]
    fn pkcs7_unpad_valid_cases() {
        // Single block with one pad byte.
        let mut block = [0u8; 16];
        block[15] = 0x01;
        assert_eq!(pkcs7_unpad(&block).unwrap().len(), 15);

        // Single block, all padding (pad_len == 16).
        let block = [16u8; 16];
        assert_eq!(pkcs7_unpad(&block).unwrap(), b"");

        // Two blocks, pad_len = 5.
        let mut buf = [0xAAu8; 32];
        for b in &mut buf[27..] {
            *b = 5;
        }
        let out = pkcs7_unpad(&buf).unwrap();
        assert_eq!(out.len(), 27);
    }

    #[test]
    fn pkcs7_unpad_rejects_zero_pad() {
        let block = [0u8; 16]; // pad_byte = 0 → pad_len = 0 → invalid
        assert!(pkcs7_unpad(&block).is_err());
    }

    #[test]
    fn pkcs7_unpad_rejects_pad_len_over_16() {
        let mut block = [0u8; 16];
        block[15] = 17; // claims pad_len 17 in a 16-byte block
        assert!(pkcs7_unpad(&block).is_err());
    }

    #[test]
    fn pkcs7_unpad_rejects_mismatched_padding() {
        let mut block = [0u8; 16];
        // Claim pad_len = 4 but only the last byte is 4; prior bytes are 0.
        block[15] = 4;
        assert!(pkcs7_unpad(&block).is_err());
    }

    #[test]
    fn pkcs7_unpad_rejects_empty_and_non_block_aligned() {
        assert!(pkcs7_unpad(&[]).is_err());
        assert!(pkcs7_unpad(&[0u8; 15]).is_err());
    }

    /// J-09 regression: a JWE whose alg is not in the caller's allow-list is rejected.
    #[test]
    fn allow_list_rejects_wrong_alg() {
        // Token created with dir + A256GCM...
        let cek = [0x42u8; 32];
        let token = encrypt(&cek, b"p", JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();

        // ...but caller only accepts AES key-wrap algorithms.
        let options =
            JweDecryptOptions::new(vec![JweAlgorithm::A256KW], vec![JweEncryption::A256GCM]);
        let result = decrypt_with_options(&cek, &token, &options);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("allow-list"), "unexpected error: {err}");
    }

    /// J-09 regression: a JWE whose enc is not in the caller's allow-list is rejected.
    #[test]
    fn allow_list_rejects_wrong_enc() {
        // Token uses A256GCM...
        let cek = [0x42u8; 32];
        let token = encrypt(&cek, b"p", JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();

        // ...but caller only accepts CBC-HS.
        let options =
            JweDecryptOptions::new(vec![JweAlgorithm::Dir], vec![JweEncryption::A256CbcHs512]);
        let result = decrypt_with_options(&cek, &token, &options);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("allow-list"), "unexpected error: {err}");
    }

    /// J-09 regression: permitted combinations still succeed.
    #[test]
    fn allow_list_accepts_permitted() {
        let cek = [0x42u8; 32];
        let plaintext = b"allow-listed";
        let token = encrypt(&cek, plaintext, JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();

        let options = JweDecryptOptions::new(vec![JweAlgorithm::Dir], vec![JweEncryption::A256GCM]);
        let recovered = decrypt_with_options(&cek, &token, &options).unwrap();
        assert_eq!(recovered, plaintext);
    }

    /// J-04 regression: a JWE with a non-empty crit header must be rejected.
    #[test]
    fn nonempty_crit_rejected_on_decrypt() {
        let cek = [0x42u8; 32];
        let token = encrypt(&cek, b"p", JweAlgorithm::Dir, JweEncryption::A256GCM).unwrap();

        // Rewrite the header to include a non-empty crit.
        let parts: Vec<&str> = token.splitn(5, '.').collect();
        let mut header: JoseHeader =
            serde_json::from_slice(&base64url::decode(parts[0]).unwrap()).unwrap();
        header.crit = Some(vec!["myext".to_string()]);
        let new_header_b64 = base64url::encode(&serde_json::to_vec(&header).unwrap());
        let tampered_token = format!(
            "{}.{}.{}.{}.{}",
            new_header_b64, parts[1], parts[2], parts[3], parts[4]
        );

        let result = decrypt(&cek, &tampered_token);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("crit"), "unexpected error: {err}");
    }

    #[test]
    fn wrong_kek_fails_unwrap() {
        let kek1 = [0xABu8; 32];
        let kek2 = [0xCDu8; 32];
        let plaintext = b"kek test";

        let token = encrypt(
            &kek1,
            plaintext,
            JweAlgorithm::A256KW,
            JweEncryption::A128GCM,
        )
        .unwrap();
        let result = decrypt(&kek2, &token);
        assert!(result.is_err(), "wrong KEK should fail unwrap");
    }
}