jose-rs 0.14.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
//! JSON Web Key (JWK) types and serialization (RFC 7517).

pub mod agreement;
pub mod cert;
pub mod convert;
mod ed448;
pub mod encrypted_pkcs8;
pub mod generate;
pub mod import;
pub mod pem;
pub mod selection;
pub mod thumbprint;

pub use cert::{
    public_jwk_from_certificate_der, public_jwk_from_certificate_pem, MAX_CERTIFICATE_DER_BYTES,
};
/// The X25519 base point, shared by key generation and by the `d`-derives-`x`
/// import check so the two cannot drift apart.
pub(crate) use convert::X25519_BASEPOINT;
pub use convert::{jwk_to_signature_key, jwk_to_software_key, software_key_to_jwk};
pub use encrypted_pkcs8::{
    encrypted_pkcs8_der_from_jwk, encrypted_pkcs8_pem_from_jwk, jwk_from_encrypted_pkcs8_der,
    jwk_from_encrypted_pkcs8_der_with_options, jwk_from_encrypted_pkcs8_pem,
    jwk_from_encrypted_pkcs8_pem_with_options, Pkcs8Encryption, Pkcs8ReadOptions,
    PKCS8_DEFAULT_ITERATIONS, PKCS8_DEFAULT_MAX_ITERATIONS, PKCS8_MIN_ITERATIONS,
};
#[cfg(feature = "post-quantum")]
pub use generate::{generate_composite_mldsa, generate_mldsa};
pub use generate::{
    generate_direct_symmetric, generate_ec, generate_ed25519, generate_ed448, generate_key,
    generate_rsa, generate_symmetric, generate_symmetric_for_alg, generate_x25519, EcCurve,
    KeyGeneration,
};
pub use import::{
    jwk_from_pkcs1_der, jwk_from_pkcs1_public_der, jwk_from_pkcs8_der, jwk_from_sec1_der,
    jwk_from_spki_der,
};
// The same type, reachable from the module that needs a password for it; see
// `crate::password` for why it is shared.
pub use crate::password::Password;
pub use pem::{
    jwk_from_pem, jwk_from_pkcs8_pem, jwk_from_spki_pem, private_pem_from_jwk, public_pem_from_jwk,
};

// Re-export JwkOp at the crate root pattern.

use crate::error::{JoseError, Result};
use serde::{Deserialize, Serialize};

fn zeroize_json_value(value: &mut serde_json::Value) {
    use zeroize::Zeroize;

    match value {
        serde_json::Value::String(s) => s.zeroize(),
        serde_json::Value::Array(items) => {
            for item in items {
                zeroize_json_value(item);
            }
        }
        serde_json::Value::Object(map) => {
            for item in map.values_mut() {
                zeroize_json_value(item);
            }
        }
        _ => {}
    }
}

/// A JSON Web Key (RFC 7517).
///
/// The `Debug` implementation redacts the private-key fields (`d`, `p`,
/// `q`, `dp`, `dq`, `qi`, `k`) so accidentally logging a `Jwk` does not
/// spill the private material — any present private component is
/// rendered as `<redacted>`.
#[derive(Clone, Serialize, Deserialize)]
pub struct Jwk {
    /// Key type (`kty`): "RSA", "EC", "oct", "OKP", or "AKP".
    pub kty: String,

    /// Key use (`use`): "sig" or "enc"
    #[serde(rename = "use", skip_serializing_if = "Option::is_none")]
    pub use_: Option<String>,

    /// Key operations (`key_ops`)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key_ops: Option<Vec<String>>,

    /// Algorithm (`alg`)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alg: Option<String>,

    /// Key ID (`kid`)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kid: Option<String>,

    // RSA parameters
    /// Base64url-encoded RSA modulus (`n`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub n: Option<String>,
    /// Base64url-encoded RSA public exponent (`e`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub e: Option<String>,
    /// Base64url-encoded RSA private exponent (`d`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub d: Option<String>,
    /// Base64url-encoded first RSA prime factor (`p`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub p: Option<String>,
    /// Base64url-encoded second RSA prime factor (`q`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub q: Option<String>,
    /// Base64url-encoded first RSA CRT exponent (`d mod (p - 1)`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dp: Option<String>,
    /// Base64url-encoded second RSA CRT exponent (`d mod (q - 1)`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dq: Option<String>,
    /// Base64url-encoded RSA CRT coefficient (`q⁻¹ mod p`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub qi: Option<String>,

    // EC parameters
    /// Elliptic-curve identifier (`crv`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub crv: Option<String>,
    /// Base64url-encoded public-key x-coordinate (`x`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub x: Option<String>,
    /// Base64url-encoded public-key y-coordinate (`y`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub y: Option<String>,

    // Symmetric key parameter
    /// Base64url-encoded symmetric key material (`k`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub k: Option<String>,

    // Algorithm Key Pair (AKP) parameters from RFC 9964, kty="AKP".
    // Used for post-quantum signatures (ML-DSA). Carried only when the
    // `post-quantum` feature is enabled at the kryptering layer but kept
    // unconditionally in the struct so JWKs round-trip through the
    // deserializer regardless of which features a consumer picked.
    /// Base64url-encoded AKP public-key material (`pub`).
    #[serde(rename = "pub", skip_serializing_if = "Option::is_none")]
    pub pub_: Option<String>,
    /// Base64url-encoded AKP private-key material (`priv`).
    #[serde(rename = "priv", skip_serializing_if = "Option::is_none")]
    pub priv_: Option<String>,

    // Catch-all for additional parameters
    /// Additional JWK parameters not represented by dedicated fields.
    #[serde(flatten)]
    pub extra: std::collections::HashMap<String, serde_json::Value>,
}

/// A cryptographic operation a JWK may be authorized to perform
/// (RFC 7517 §4.3 "key_ops" values).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum JwkOp {
    /// Create a digital signature or MAC.
    Sign,
    /// Verify a digital signature or MAC.
    Verify,
    /// Encrypt content directly.
    Encrypt,
    /// Decrypt content directly.
    Decrypt,
    /// Wrap or transport a content-encryption key.
    WrapKey,
    /// Unwrap or recover a content-encryption key.
    UnwrapKey,
    /// Derive key material.
    DeriveKey,
    /// Derive raw shared-secret bits.
    DeriveBits,
}

impl JwkOp {
    /// RFC 7517 §4.3 operation identifier.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Sign => "sign",
            Self::Verify => "verify",
            Self::Encrypt => "encrypt",
            Self::Decrypt => "decrypt",
            Self::WrapKey => "wrapKey",
            Self::UnwrapKey => "unwrapKey",
            Self::DeriveKey => "deriveKey",
            Self::DeriveBits => "deriveBits",
        }
    }

    /// RFC 7517 §4.2 "use" category this operation belongs to.
    /// Returns `"sig"` for sign/verify, `"enc"` otherwise.
    pub fn use_category(self) -> &'static str {
        match self {
            Self::Sign | Self::Verify => "sig",
            _ => "enc",
        }
    }
}

/// Wipe the private-key components when a `Jwk` is dropped.
///
/// The struct stores RSA `d`/`p`/`q`/`dp`/`dq`/`qi` and symmetric `k` as
/// `Option<String>`. On drop we call `zeroize::Zeroize` on each present
/// value so the heap-backed `String` is overwritten before its allocation
/// is returned to the allocator — defence against a heap-inspection bug
/// or a core dump exposing secret material.
///
/// This does not change public behaviour; `Clone` still produces an
/// independent copy (which will itself zeroize on its own drop).
impl Drop for Jwk {
    fn drop(&mut self) {
        use zeroize::Zeroize;
        if let Some(s) = self.d.as_mut() {
            s.zeroize();
        }
        if let Some(s) = self.p.as_mut() {
            s.zeroize();
        }
        if let Some(s) = self.q.as_mut() {
            s.zeroize();
        }
        if let Some(s) = self.dp.as_mut() {
            s.zeroize();
        }
        if let Some(s) = self.dq.as_mut() {
            s.zeroize();
        }
        if let Some(s) = self.qi.as_mut() {
            s.zeroize();
        }
        if let Some(s) = self.k.as_mut() {
            s.zeroize();
        }
        if let Some(s) = self.priv_.as_mut() {
            s.zeroize();
        }
        for value in self.extra.values_mut() {
            zeroize_json_value(value);
        }
    }
}

impl std::fmt::Debug for Jwk {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Redact any private-key component that is present, but still show
        // that it existed (so debug output conveys "this is a private key").
        let redact =
            |opt: &Option<String>| -> Option<&'static str> { opt.as_ref().map(|_| "<redacted>") };
        f.debug_struct("Jwk")
            .field("kty", &self.kty)
            .field("use_", &self.use_)
            .field("key_ops", &self.key_ops)
            .field("alg", &self.alg)
            .field("kid", &self.kid)
            .field("n", &self.n)
            .field("e", &self.e)
            .field("d", &redact(&self.d))
            .field("p", &redact(&self.p))
            .field("q", &redact(&self.q))
            .field("dp", &redact(&self.dp))
            .field("dq", &redact(&self.dq))
            .field("qi", &redact(&self.qi))
            .field("crv", &self.crv)
            .field("x", &self.x)
            .field("y", &self.y)
            .field("k", &redact(&self.k))
            .field("pub_", &self.pub_)
            .field("priv_", &redact(&self.priv_))
            .field(
                "extra",
                &if self.extra.is_empty() {
                    None::<&str>
                } else {
                    Some("<redacted>")
                },
            )
            .finish()
    }
}

impl Jwk {
    /// Parse a JWK from a JSON string.
    pub fn from_json(s: &str) -> Result<Self> {
        serde_json::from_str(s).map_err(JoseError::from)
    }

    /// Serialize this JWK to a compact JSON string.
    pub fn to_json(&self) -> Result<String> {
        serde_json::to_string(self).map_err(JoseError::from)
    }

    /// Serialize this JWK to a pretty-printed JSON string.
    pub fn to_json_pretty(&self) -> Result<String> {
        serde_json::to_string_pretty(self).map_err(JoseError::from)
    }

    /// Return a copy of this JWK with all private-key components removed.
    ///
    /// Wipes `d`, `p`, `q`, `dp`, `dq`, `qi`, `k`, and the AKP `priv`
    /// member. The AKP `pub` member survives. From the flattened
    /// `extra` parameters, preserves only the RFC 7517 §4.6–§4.9 X.509
    /// public parameters (`x5c`, `x5t`, `x5t#S256`, `x5u`) and drops
    /// everything else — any unrecognised field, including the RFC 7517
    /// `oth` array of "other primes info" (whose `r`/`d`/`t` members are
    /// private), is not propagated.
    ///
    /// For symmetric (`kty: "oct"`) keys the result has no key material at
    /// all — there is no public companion — which is the correct outcome
    /// if someone tries to publish a symmetric key.
    ///
    /// Use this before serializing a JWK to any audience that should not
    /// see private material (a JWK Set endpoint, logs, error messages).
    pub fn to_public_jwk(&self) -> Jwk {
        // RFC 7517 §4.6–§4.9: the four X.509 parameters are public by
        // design (cert chains, thumbprints, URLs). Everything else in
        // `extra` has unknown provenance and is dropped.
        const PUBLIC_EXTRA_ALLOWLIST: &[&str] = &["x5c", "x5t", "x5t#S256", "x5u"];
        let extra = self
            .extra
            .iter()
            .filter(|(k, _)| PUBLIC_EXTRA_ALLOWLIST.contains(&k.as_str()))
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();

        Jwk {
            kty: self.kty.clone(),
            use_: self.use_.clone(),
            key_ops: self.key_ops.clone(),
            alg: self.alg.clone(),
            kid: self.kid.clone(),
            n: self.n.clone(),
            e: self.e.clone(),
            d: None,
            p: None,
            q: None,
            dp: None,
            dq: None,
            qi: None,
            crv: self.crv.clone(),
            x: self.x.clone(),
            y: self.y.clone(),
            k: None,
            pub_: self.pub_.clone(),
            priv_: None,
            extra,
        }
    }

    /// Check whether this JWK is authorized for the requested operation
    /// per RFC 7517 §4.2 (`use`) and §4.3 (`key_ops`).
    ///
    /// Rules:
    /// - If `use` is set, it must equal the operation's category
    ///   (`"sig"` for `Sign`/`Verify`, `"enc"` otherwise).
    /// - If `key_ops` is set, the operation's JOSE name must appear in it.
    /// - If both are set, they must be consistent with each other.
    /// - If neither is set, the key is unrestricted.
    ///
    /// Returns `Ok(())` when the operation is permitted. Errors are
    /// `JoseError::Key` with a message identifying which field blocked
    /// the operation.
    pub fn check_op(&self, op: JwkOp) -> Result<()> {
        if let Some(use_) = self.use_.as_deref() {
            if use_ != op.use_category() {
                return Err(JoseError::Key(format!(
                    "JWK `use` is {use_}, not permitted for operation {}",
                    op.as_str()
                )));
            }
        }
        if let Some(ops) = &self.key_ops {
            let name = op.as_str();
            if !ops.iter().any(|o| o == name) {
                return Err(JoseError::Key(format!(
                    "JWK `key_ops` does not include {name}"
                )));
            }
        }
        // If both are set, RFC 7517 §4.3 says they SHOULD be consistent;
        // the two checks above already enforce each independently, which
        // implies consistency at the point where a specific op is checked.
        Ok(())
    }
}

/// A JWK Set (RFC 7517 Section 5).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JwkSet {
    /// Keys published in the set.
    pub keys: Vec<Jwk>,
}

impl JwkSet {
    /// Parse a JWK Set from a JSON string.
    ///
    /// This parses structure, not key usability: unsupported key types/curves
    /// and duplicate key identifiers are retained. Material and operation
    /// checks happen when a key is used. Malformed typed fields still fail.
    pub fn from_json(s: &str) -> Result<Self> {
        serde_json::from_str(s).map_err(JoseError::from)
    }

    /// Parse a JWK Set under explicit import budgets.
    ///
    /// Identical to [`from_json`](Self::from_json) except that the document size is checked before
    /// JSON parsing and the entry count is checked immediately after deserialization. The byte
    /// limit is therefore the pre-allocation defence. The entry limit prevents an oversized set
    /// from being returned or used, but the `Vec<Jwk>` has already been allocated by the time its
    /// length is knowable; callers must set an appropriate byte limit to bound parsing memory.
    ///
    /// [`from_json`](Self::from_json) deliberately keeps its unbounded behaviour — a limit
    /// that appears silently
    /// is a breaking change wearing a compatibility mask. New code that ingests sets from
    /// elsewhere should prefer this entry point.
    ///
    /// ```
    /// use jose_rs::jwk::JwkSet;
    /// use jose_rs::jwk::selection::SetLimits;
    ///
    /// let text = r#"{"keys":[{"kty":"oct","k":"AAAA"}]}"#;
    /// let set = JwkSet::from_json_with_limits(text, &SetLimits::default())?;
    /// assert_eq!(set.keys.len(), 1);
    ///
    /// let tiny = SetLimits { max_bytes: 8, ..Default::default() };
    /// assert!(JwkSet::from_json_with_limits(text, &tiny).is_err());
    /// # Ok::<(), jose_rs::JoseError>(())
    /// ```
    ///
    /// # Errors
    ///
    /// [`JoseError::Key`] when a budget is exceeded; otherwise as [`from_json`](Self::from_json).
    pub fn from_json_with_limits(s: &str, limits: &selection::SetLimits) -> Result<Self> {
        limits.check_bytes(s.len())?;
        let set: Self = serde_json::from_str(s).map_err(JoseError::from)?;
        limits.check_entries(set.keys.len())?;
        Ok(set)
    }

    /// Describe every entry of this set, keeping identifiers and unusable entries.
    ///
    /// See [`selection::diagnostics`]; this is the same call as a method, because it is
    /// something you ask *of* a set.
    pub fn diagnostics(&self) -> Vec<selection::EntryDiagnostic> {
        selection::diagnostics(self)
    }

    /// Resolve a token's `kid` into a bounded candidate list under a policy.
    ///
    /// See [`selection::select`].
    pub fn select(
        &self,
        token_kid: Option<&str>,
        policy: &selection::SelectionPolicy,
    ) -> Result<selection::Selection<'_>> {
        selection::select(self, token_kid, policy)
    }

    /// Serialize this JWK Set to a compact JSON string.
    pub fn to_json(&self) -> Result<String> {
        serde_json::to_string(self).map_err(JoseError::from)
    }

    /// Find a key in the set by its `kid`.
    ///
    /// Returns the first match, even if the identifier is ambiguous. For
    /// verification key selection, prefer [`Self::find_unique_by_kid`].
    pub fn find_by_kid(&self, kid: &str) -> Option<&Jwk> {
        self.keys.iter().find(|k| k.kid.as_deref() == Some(kid))
    }

    /// Find exactly one key by its case-sensitive `kid`.
    ///
    /// Returns `Ok(None)` when absent and an error when two or more entries
    /// match, even if their material is identical or one key is unsupported.
    /// No keys are filtered by algorithm or usability before this check.
    /// This selects an identifier; it does not validate or authorize a key.
    ///
    /// ```
    /// use jose_rs::jwk::JwkSet;
    /// let set = JwkSet::from_json(r#"{"keys":[]}"#)?;
    /// assert!(set.find_unique_by_kid("issuer-key")?.is_none());
    /// # Ok::<(), jose_rs::JoseError>(())
    /// ```
    pub fn find_unique_by_kid(&self, kid: &str) -> Result<Option<&Jwk>> {
        let mut matches = self
            .keys
            .iter()
            .filter(|key| key.kid.as_deref() == Some(kid));
        let first = matches.next();
        if matches.next().is_some() {
            return Err(JoseError::Key(
                "multiple JWKs match the requested kid".into(),
            ));
        }
        Ok(first)
    }
}

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

    /// RFC 7517 Appendix A.1 -- Example RSA Public Key
    const RSA_JWK_JSON: &str = r#"{
        "kty": "RSA",
        "n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
        "e": "AQAB",
        "alg": "RS256",
        "kid": "2011-04-29",
        "use": "sig"
    }"#;

    #[test]
    fn parse_rsa_jwk() {
        let jwk = Jwk::from_json(RSA_JWK_JSON).unwrap();
        assert_eq!(jwk.kty, "RSA");
        assert_eq!(jwk.e.as_deref(), Some("AQAB"));
        assert!(jwk.n.is_some());
        assert_eq!(jwk.alg.as_deref(), Some("RS256"));
        assert_eq!(jwk.kid.as_deref(), Some("2011-04-29"));
        assert_eq!(jwk.use_.as_deref(), Some("sig"));
        // Private fields should be absent
        assert!(jwk.d.is_none());
        assert!(jwk.p.is_none());
    }

    #[test]
    fn parse_jwk_set_and_find_by_kid() {
        let set_json = r#"{
            "keys": [
                {
                    "kty": "EC",
                    "crv": "P-256",
                    "x": "f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU",
                    "y": "x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0",
                    "kid": "ec-key-1"
                },
                {
                    "kty": "RSA",
                    "n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
                    "e": "AQAB",
                    "kid": "rsa-key-1"
                }
            ]
        }"#;

        let set = JwkSet::from_json(set_json).unwrap();
        assert_eq!(set.keys.len(), 2);

        let ec = set.find_by_kid("ec-key-1").unwrap();
        assert_eq!(ec.kty, "EC");
        assert_eq!(ec.crv.as_deref(), Some("P-256"));

        let rsa = set.find_by_kid("rsa-key-1").unwrap();
        assert_eq!(rsa.kty, "RSA");

        assert!(set.find_by_kid("nonexistent").is_none());
    }

    #[test]
    fn roundtrip_serialize_deserialize() {
        let jwk = Jwk::from_json(RSA_JWK_JSON).unwrap();
        let json = jwk.to_json().unwrap();
        let jwk2 = Jwk::from_json(&json).unwrap();
        assert_eq!(jwk2.kty, jwk.kty);
        assert_eq!(jwk2.n, jwk.n);
        assert_eq!(jwk2.e, jwk.e);
        assert_eq!(jwk2.kid, jwk.kid);
        assert_eq!(jwk2.alg, jwk.alg);
        assert_eq!(jwk2.use_, jwk.use_);
    }

    #[test]
    fn to_json_pretty() {
        let jwk = Jwk::from_json(RSA_JWK_JSON).unwrap();
        let pretty = jwk.to_json_pretty().unwrap();
        assert!(pretty.contains('\n'));
        // Should still parse back
        let jwk2 = Jwk::from_json(&pretty).unwrap();
        assert_eq!(jwk2.kty, "RSA");
    }

    #[test]
    fn symmetric_key() {
        let json = r#"{"kty":"oct","k":"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow","kid":"hmac-key"}"#;
        let jwk = Jwk::from_json(json).unwrap();
        assert_eq!(jwk.kty, "oct");
        assert!(jwk.k.is_some());
        assert_eq!(jwk.kid.as_deref(), Some("hmac-key"));
    }

    /// Phase 3: Debug output must redact private-key fields.
    #[test]
    fn debug_redacts_private_fields() {
        let json = r#"{
            "kty": "RSA",
            "n": "publicN",
            "e": "AQAB",
            "d": "SECRET-PRIVATE-D",
            "p": "SECRET-PRIME-P",
            "q": "SECRET-PRIME-Q",
            "kid": "log-leak-test"
        }"#;
        let jwk = Jwk::from_json(json).unwrap();
        let s = format!("{jwk:?}");
        assert!(!s.contains("SECRET-PRIVATE-D"), "d leaked: {s}");
        assert!(!s.contains("SECRET-PRIME-P"), "p leaked: {s}");
        assert!(!s.contains("SECRET-PRIME-Q"), "q leaked: {s}");
        assert!(s.contains("<redacted>"), "expected redaction marker: {s}");
        // Public fields still appear.
        assert!(s.contains("publicN"));
        assert!(s.contains("log-leak-test"));
    }

    /// Phase 7: Drop zeroization is exercised — construct, clone, drop, repeat.
    /// Runtime memory-inspection is out of scope, but this smoke-tests the Drop
    /// impl so any panic or aliasing bug surfaces under `cargo test`.
    #[test]
    fn zeroize_on_drop_smoke() {
        for _ in 0..8 {
            let jwk = Jwk::from_json(
                r#"{
                    "kty":"RSA","n":"AA","e":"AQAB",
                    "d":"d-value","p":"p-value","q":"q-value",
                    "dp":"dp-v","dq":"dq-v","qi":"qi-v"
                }"#,
            )
            .unwrap();
            let _clone = jwk.clone();
            // Both jwk and _clone drop here; Drop impl zeroizes each.
        }

        // Symmetric key path.
        for _ in 0..8 {
            let jwk = Jwk::from_json(r#"{"kty":"oct","k":"secret-k"}"#).unwrap();
            let _clone = jwk.clone();
        }
    }

    /// Phase 6: to_public_jwk strips RSA private components.
    #[test]
    fn to_public_jwk_strips_rsa_private() {
        let json = r#"{
            "kty":"RSA","n":"pubN","e":"AQAB",
            "d":"SECRET-D","p":"SECRET-P","q":"SECRET-Q",
            "dp":"SECRET-DP","dq":"SECRET-DQ","qi":"SECRET-QI",
            "kid":"k1","alg":"RS256"
        }"#;
        let jwk = Jwk::from_json(json).unwrap();
        let pub_jwk = jwk.to_public_jwk();
        assert!(pub_jwk.d.is_none());
        assert!(pub_jwk.p.is_none());
        assert!(pub_jwk.q.is_none());
        assert!(pub_jwk.dp.is_none());
        assert!(pub_jwk.dq.is_none());
        assert!(pub_jwk.qi.is_none());
        // Public components preserved.
        assert_eq!(pub_jwk.n.as_deref(), Some("pubN"));
        assert_eq!(pub_jwk.e.as_deref(), Some("AQAB"));
        assert_eq!(pub_jwk.kid.as_deref(), Some("k1"));
        assert_eq!(pub_jwk.alg.as_deref(), Some("RS256"));
        // The serialized form must not contain any SECRET-* string.
        let s = pub_jwk.to_json().unwrap();
        assert!(!s.contains("SECRET"), "private leaked: {s}");
    }

    /// Phase 6: to_public_jwk strips EC d (private scalar).
    #[test]
    fn to_public_jwk_strips_ec_private() {
        let json = r#"{
            "kty":"EC","crv":"P-256",
            "x":"pubX","y":"pubY","d":"SECRET-D","kid":"ec1"
        }"#;
        let jwk = Jwk::from_json(json).unwrap();
        let pub_jwk = jwk.to_public_jwk();
        assert!(pub_jwk.d.is_none());
        assert_eq!(pub_jwk.x.as_deref(), Some("pubX"));
        assert_eq!(pub_jwk.y.as_deref(), Some("pubY"));
    }

    /// Phase 6: to_public_jwk on OKP strips d, keeps x.
    #[test]
    fn to_public_jwk_strips_okp_private() {
        let json = r#"{
            "kty":"OKP","crv":"Ed25519",
            "x":"pubX","d":"SECRET-D"
        }"#;
        let jwk = Jwk::from_json(json).unwrap();
        let pub_jwk = jwk.to_public_jwk();
        assert!(pub_jwk.d.is_none());
        assert_eq!(pub_jwk.x.as_deref(), Some("pubX"));
    }

    /// Phase 6: to_public_jwk on oct strips k entirely (symmetric keys have
    /// no public companion).
    #[test]
    fn to_public_jwk_strips_oct() {
        let json = r#"{"kty":"oct","k":"SECRET","kid":"h1"}"#;
        let jwk = Jwk::from_json(json).unwrap();
        let pub_jwk = jwk.to_public_jwk();
        assert!(pub_jwk.k.is_none());
        assert_eq!(pub_jwk.kid.as_deref(), Some("h1"));
    }

    #[test]
    fn to_public_jwk_drops_unknown_extra_fields() {
        let json = r#"{
            "kty":"RSA","n":"pubN","e":"AQAB","d":"SECRET-D",
            "oth":[{"r":"SECRET-R","d":"SECRET-DI","t":"SECRET-T"}],
            "vendor_secret":"SECRET-V"
        }"#;
        let jwk = Jwk::from_json(json).unwrap();
        let pub_jwk = jwk.to_public_jwk();
        assert!(
            pub_jwk.extra.is_empty(),
            "unknown extras must be dropped, got: {:?}",
            pub_jwk.extra.keys().collect::<Vec<_>>()
        );
        let s = pub_jwk.to_json().unwrap();
        assert!(!s.contains("SECRET"), "private leaked via extra: {s}");
    }

    /// RFC 7517 §4.6–§4.9: X.509 parameters are public by design and
    /// must survive `to_public_jwk`.
    #[test]
    fn to_public_jwk_preserves_x509_public_extras() {
        let json = r#"{
            "kty":"RSA","n":"pubN","e":"AQAB","d":"SECRET-D",
            "x5c":["MIIB-fake-cert"],
            "x5t":"fake-sha1-thumbprint",
            "x5t#S256":"fake-sha256-thumbprint",
            "x5u":"https://example.invalid/cert.pem",
            "private_metadata":"SECRET-M"
        }"#;
        let jwk = Jwk::from_json(json).unwrap();
        let pub_jwk = jwk.to_public_jwk();

        // Private scalar is gone.
        assert!(pub_jwk.d.is_none());

        // RFC 7517 §4.6–§4.9 extensions survive.
        assert!(pub_jwk.extra.contains_key("x5c"));
        assert!(pub_jwk.extra.contains_key("x5t"));
        assert!(pub_jwk.extra.contains_key("x5t#S256"));
        assert!(pub_jwk.extra.contains_key("x5u"));

        // Unknown / non-RFC-sanctioned extras are dropped.
        assert!(!pub_jwk.extra.contains_key("private_metadata"));

        let s = pub_jwk.to_json().unwrap();
        assert!(!s.contains("SECRET"), "private leaked via extra: {s}");
        assert!(s.contains("MIIB-fake-cert"));
        assert!(s.contains("fake-sha256-thumbprint"));
    }

    /// Phase 6: use="sig" permits Verify but not Encrypt.
    #[test]
    fn use_sig_permits_verify_blocks_encrypt() {
        let jwk = Jwk::from_json(r#"{"kty":"RSA","n":"AA","e":"AQAB","use":"sig"}"#).unwrap();
        jwk.check_op(JwkOp::Verify).unwrap();
        let err = jwk.check_op(JwkOp::Encrypt).unwrap_err().to_string();
        assert!(err.contains("`use` is sig"), "unexpected: {err}");
    }

    /// Phase 6: use="enc" permits Encrypt but not Sign.
    #[test]
    fn use_enc_permits_encrypt_blocks_sign() {
        let jwk = Jwk::from_json(r#"{"kty":"oct","k":"AA","use":"enc"}"#).unwrap();
        jwk.check_op(JwkOp::Encrypt).unwrap();
        let err = jwk.check_op(JwkOp::Sign).unwrap_err().to_string();
        assert!(err.contains("`use` is enc"), "unexpected: {err}");
    }

    /// Phase 6: key_ops narrows to specific operations.
    #[test]
    fn key_ops_restricts_to_listed_operations() {
        let jwk =
            Jwk::from_json(r#"{"kty":"RSA","n":"AA","e":"AQAB","key_ops":["verify"]}"#).unwrap();
        jwk.check_op(JwkOp::Verify).unwrap();
        let err = jwk.check_op(JwkOp::Sign).unwrap_err().to_string();
        assert!(err.contains("key_ops"), "unexpected: {err}");
    }

    /// Phase 6: when neither use nor key_ops is set, all ops are allowed.
    #[test]
    fn no_restriction_allows_everything() {
        let jwk = Jwk::from_json(r#"{"kty":"oct","k":"AA"}"#).unwrap();
        jwk.check_op(JwkOp::Sign).unwrap();
        jwk.check_op(JwkOp::Verify).unwrap();
        jwk.check_op(JwkOp::Encrypt).unwrap();
        jwk.check_op(JwkOp::WrapKey).unwrap();
    }

    /// Phase 6: conflicting use + key_ops is caught (sig + key_ops:["encrypt"]).
    #[test]
    fn inconsistent_use_and_key_ops_both_block() {
        // use=sig says only sig ops. key_ops=["encrypt"] says only encrypt.
        // Any op chosen is blocked by at least one constraint.
        let jwk =
            Jwk::from_json(r#"{"kty":"oct","k":"AA","use":"sig","key_ops":["encrypt"]}"#).unwrap();
        // Verify is a sig-op but not in key_ops → blocked by key_ops.
        assert!(jwk.check_op(JwkOp::Verify).is_err());
        // Encrypt is in key_ops but use=sig → blocked by use.
        assert!(jwk.check_op(JwkOp::Encrypt).is_err());
    }

    /// Phase 3: symmetric key `k` is also redacted in Debug.
    #[test]
    fn debug_redacts_symmetric_key() {
        let json = r#"{"kty":"oct","k":"SECRET-SYMMETRIC-KEY"}"#;
        let jwk = Jwk::from_json(json).unwrap();
        let s = format!("{jwk:?}");
        assert!(!s.contains("SECRET-SYMMETRIC-KEY"), "k leaked: {s}");
        assert!(s.contains("<redacted>"));
    }

    #[test]
    fn debug_redacts_extra_fields() {
        let json = r#"{
            "kty":"RSA","n":"AA","e":"AQAB",
            "oth":[{"r":"SECRET-R"}],
            "vendor_secret":"SECRET-V"
        }"#;
        let jwk = Jwk::from_json(json).unwrap();
        let s = format!("{jwk:?}");
        assert!(!s.contains("SECRET-R"), "extra leaked: {s}");
        assert!(!s.contains("SECRET-V"), "extra leaked: {s}");
        assert!(s.contains("<redacted>"));
    }

    #[test]
    fn extra_fields_preserved() {
        let json = r#"{"kty":"oct","k":"dGVzdA","custom_field":"custom_value"}"#;
        let jwk = Jwk::from_json(json).unwrap();
        assert_eq!(
            jwk.extra.get("custom_field").and_then(|v| v.as_str()),
            Some("custom_value")
        );
        // Roundtrip preserves extra field
        let json2 = jwk.to_json().unwrap();
        assert!(json2.contains("custom_field"));
    }

    /// `to_public_jwk` on an AKP JWK must strip `priv` (the 32-byte
    /// FIPS 204 seed) and preserve `pub` (the raw public key). This
    /// is the JWK-Set-publication shape — callers rely on it to hand
    /// a key to a public endpoint without leaking the seed.
    #[cfg(feature = "post-quantum")]
    #[test]
    fn to_public_jwk_strips_akp_priv_preserves_pub() {
        use kryptering::MlDsaVariant;
        let jwk = crate::jwk::generate::generate_mldsa(MlDsaVariant::MlDsa44).unwrap();
        assert!(
            jwk.priv_.is_some() && jwk.pub_.is_some(),
            "precondition: generate_mldsa must produce both pub and priv"
        );

        let public = jwk.to_public_jwk();
        assert!(public.priv_.is_none(), "priv must be stripped");
        assert_eq!(
            public.pub_, jwk.pub_,
            "pub must survive to_public_jwk byte-for-byte"
        );
        assert_eq!(public.kty, "AKP");
        assert_eq!(public.alg.as_deref(), Some("ML-DSA-44"));

        // Serialized JWK must not contain a "priv" key at all.
        let json = public.to_json().unwrap();
        let value: serde_json::Value = serde_json::from_str(&json).unwrap();
        let obj = value
            .as_object()
            .unwrap_or_else(|| panic!("serialized JWK is not a JSON object: {json}"));
        assert!(!obj.contains_key("priv"), "priv leaked in JSON: {json}");
        assert!(obj.contains_key("pub"), "pub missing from JSON: {json}");
    }
}