jwk-simple 0.5.0

JWK/JWKS for Rust and WASM (RFC 7517, 7518, 7638, 8037, 9864) with jwt-simple integration
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
//! RSA key parameters as defined in RFC 7518 Section 6.3.
//!
//! This module contains the [`RsaParams`] type which holds RSA public and
//! private key components, including support for multi-prime RSA keys.

use std::fmt::{self, Debug};

use serde::{Deserialize, Serialize};
use zeroize::{Zeroize, ZeroizeOnDrop};

use crate::encoding::Base64UrlBytes;
use crate::error::{IncompatibleKeyError, InvalidKeyError, Result};

/// Validates that a Base64urlUInt value uses canonical encoding.
///
/// RFC 7518 Section 2 defines Base64urlUInt as the base64url encoding of an
/// unsigned big-endian integer, which "MUST utilize the minimum number of
/// octets needed to represent the value." This means no leading zero bytes,
/// except for the value zero itself which is represented as a single zero byte.
fn validate_base64url_uint(value: &Base64UrlBytes, name: &str) -> Result<()> {
    let bytes = value.as_bytes();
    if bytes.is_empty() {
        return Err(InvalidKeyError::InvalidParameter {
            name: "Base64urlUInt",
            reason: format!(
                "RFC 7518: '{}' must be a non-empty Base64urlUInt value",
                name
            ),
        }
        .into());
    }
    if bytes.len() > 1 && bytes[0] == 0 {
        return Err(InvalidKeyError::InvalidParameter {
            name: "Base64urlUInt",
            reason: format!(
                "RFC 7518: '{}' has non-canonical Base64urlUInt encoding (leading zero bytes)",
                name
            ),
        }
        .into());
    }
    Ok(())
}

/// Other primes info for multi-prime RSA keys (RFC 7518 Section 6.3.2.7).
///
/// When more than two prime factors are used, this structure holds the
/// additional prime factor information.
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Zeroize, ZeroizeOnDrop)]
#[non_exhaustive]
pub struct RsaOtherPrime {
    /// Prime factor.
    pub r: Base64UrlBytes,

    /// Factor CRT exponent (d mod (r - 1)).
    pub d: Base64UrlBytes,

    /// Factor CRT coefficient (inverse of all preceding primes mod r).
    pub t: Base64UrlBytes,
}

impl RsaOtherPrime {
    /// Creates a new other prime info structure.
    pub fn new(r: Base64UrlBytes, d: Base64UrlBytes, t: Base64UrlBytes) -> Self {
        Self { r, d, t }
    }

    /// Validates the other prime parameters.
    pub fn validate(&self) -> Result<()> {
        if self.r.is_empty() {
            return Err(InvalidKeyError::MissingParameter("oth.r").into());
        }
        if self.d.is_empty() {
            return Err(InvalidKeyError::MissingParameter("oth.d").into());
        }
        if self.t.is_empty() {
            return Err(InvalidKeyError::MissingParameter("oth.t").into());
        }
        validate_base64url_uint(&self.r, "oth.r")?;
        validate_base64url_uint(&self.d, "oth.d")?;
        validate_base64url_uint(&self.t, "oth.t")?;
        Ok(())
    }
}

impl Debug for RsaOtherPrime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RsaOtherPrime")
            .field("r", &format!("[{} bytes]", self.r.len()))
            .field("d", &"[REDACTED]")
            .field("t", &"[REDACTED]")
            .finish()
    }
}

/// RSA key parameters (RFC 7518 Section 6.3).
///
/// Contains the public key parameters `n` (modulus) and `e` (exponent),
/// and optionally the private key parameters.
///
/// # Examples
///
/// ```
/// use jwk_simple::RsaParams;
///
/// // Parse from JSON
/// let json = r#"{
///     "n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
///     "e": "AQAB"
/// }"#;
///
/// let params: RsaParams = serde_json::from_str(json).unwrap();
/// assert!(params.is_public_key_only());
/// ```
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Zeroize, ZeroizeOnDrop)]
#[non_exhaustive]
pub struct RsaParams {
    /// The modulus value for the RSA key.
    pub n: Base64UrlBytes,

    /// The exponent value for the RSA key.
    pub e: Base64UrlBytes,

    /// The private exponent value for the RSA private key.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub d: Option<Base64UrlBytes>,

    /// The first prime factor.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub p: Option<Base64UrlBytes>,

    /// The second prime factor.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub q: Option<Base64UrlBytes>,

    /// The first factor CRT exponent (d mod (p-1)).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dp: Option<Base64UrlBytes>,

    /// The second factor CRT exponent (d mod (q-1)).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dq: Option<Base64UrlBytes>,

    /// The first CRT coefficient (q^-1 mod p).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub qi: Option<Base64UrlBytes>,

    /// Other primes info for multi-prime RSA (RFC 7518 Section 6.3.2.7).
    ///
    /// This field is only used when the RSA key has more than two prime factors.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub oth: Option<Vec<RsaOtherPrime>>,
}

impl RsaParams {
    /// Creates new RSA public key parameters.
    #[must_use]
    pub fn new_public(n: Base64UrlBytes, e: Base64UrlBytes) -> Self {
        Self {
            n,
            e,
            d: None,
            p: None,
            q: None,
            dp: None,
            dq: None,
            qi: None,
            oth: None,
        }
    }

    /// Returns a builder for constructing RSA private key parameters.
    ///
    /// # Examples
    ///
    /// ```
    /// use jwk_simple::RsaParams;
    /// use jwk_simple::encoding::Base64UrlBytes;
    ///
    /// let params = RsaParams::builder(
    ///     Base64UrlBytes::new(vec![1; 256]),
    ///     Base64UrlBytes::new(vec![1, 0, 1]),
    ///     Base64UrlBytes::new(vec![2; 256]),
    /// )
    /// .p(Base64UrlBytes::new(vec![3; 128]))
    /// .q(Base64UrlBytes::new(vec![4; 128]))
    /// .build();
    /// ```
    #[must_use]
    pub fn builder(n: Base64UrlBytes, e: Base64UrlBytes, d: Base64UrlBytes) -> RsaParamsBuilder {
        RsaParamsBuilder::new(n, e, d)
    }

    /// Creates new RSA private key parameters.
    ///
    /// For more complex key construction, prefer using [`RsaParams::builder`].
    #[must_use]
    #[allow(clippy::too_many_arguments)]
    pub fn new_private(
        n: Base64UrlBytes,
        e: Base64UrlBytes,
        d: Base64UrlBytes,
        p: Option<Base64UrlBytes>,
        q: Option<Base64UrlBytes>,
        dp: Option<Base64UrlBytes>,
        dq: Option<Base64UrlBytes>,
        qi: Option<Base64UrlBytes>,
    ) -> Self {
        Self {
            n,
            e,
            d: Some(d),
            p,
            q,
            dp,
            dq,
            qi,
            oth: None,
        }
    }

    /// Creates new multi-prime RSA private key parameters.
    ///
    /// For more complex key construction, prefer using [`RsaParams::builder`].
    #[must_use]
    #[allow(clippy::too_many_arguments)]
    pub fn new_multi_prime(
        n: Base64UrlBytes,
        e: Base64UrlBytes,
        d: Base64UrlBytes,
        p: Base64UrlBytes,
        q: Base64UrlBytes,
        dp: Base64UrlBytes,
        dq: Base64UrlBytes,
        qi: Base64UrlBytes,
        oth: Vec<RsaOtherPrime>,
    ) -> Self {
        Self {
            n,
            e,
            d: Some(d),
            p: Some(p),
            q: Some(q),
            dp: Some(dp),
            dq: Some(dq),
            qi: Some(qi),
            oth: Some(oth),
        }
    }

    /// Returns `true` if this contains only public key parameters.
    pub fn is_public_key_only(&self) -> bool {
        self.d.is_none()
    }

    /// Returns `true` if this contains private key parameters.
    pub fn has_private_key(&self) -> bool {
        self.d.is_some()
    }

    /// Returns the key size in bits.
    ///
    /// This is calculated from the modulus `n`.
    pub fn key_size_bits(&self) -> usize {
        let bytes = self.n.as_bytes();
        if bytes.is_empty() {
            return 0;
        }

        let mut idx = 0;
        while idx < bytes.len() && bytes[idx] == 0 {
            idx += 1;
        }
        if idx == bytes.len() {
            return 0;
        }

        let first = bytes[idx];
        let leading_zeros = first.leading_zeros() as usize;
        ((bytes.len() - idx - 1) * 8) + (8 - leading_zeros)
    }

    /// Validates the RSA parameters.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The modulus `n` is empty
    /// - The exponent `e` is empty
    /// - Any integer field uses non-canonical Base64urlUInt encoding (leading zero bytes)
    /// - Private key parameters are partially present
    /// - Multi-prime `oth` parameter present without CRT parameters
    pub fn validate(&self) -> Result<()> {
        // Modulus must not be empty
        if self.n.is_empty() {
            return Err(InvalidKeyError::MissingParameter("n").into());
        }

        // Exponent must not be empty
        if self.e.is_empty() {
            return Err(InvalidKeyError::MissingParameter("e").into());
        }

        // RFC 7518 Section 2: Base64urlUInt values MUST use the minimum number
        // of octets to represent the value. Reject non-canonical encodings
        // (leading zero bytes) which could cause thumbprint mismatches.
        validate_base64url_uint(&self.n, "n")?;
        validate_base64url_uint(&self.e, "e")?;

        // Basic RSA public parameter sanity checks.
        // n must be non-zero; e must be odd and at least 3.
        if self.n.as_bytes().iter().all(|&b| b == 0) {
            return Err(InvalidKeyError::InvalidParameter {
                name: "n",
                reason: "RSA modulus must be non-zero".to_string(),
            }
            .into());
        }

        let e_bytes = self.e.as_bytes();
        let is_odd = (e_bytes[e_bytes.len() - 1] & 1) == 1;
        let ge_three = if e_bytes.len() > 1 {
            true
        } else {
            e_bytes[0] >= 3
        };

        if !ge_three || !is_odd {
            return Err(InvalidKeyError::InvalidParameter {
                name: "e",
                reason: "RSA public exponent must be odd and >= 3".to_string(),
            }
            .into());
        }

        if let Some(ref d) = self.d {
            validate_base64url_uint(d, "d")?;
        }
        if let Some(ref p) = self.p {
            validate_base64url_uint(p, "p")?;
        }
        if let Some(ref q) = self.q {
            validate_base64url_uint(q, "q")?;
        }
        if let Some(ref dp) = self.dp {
            validate_base64url_uint(dp, "dp")?;
        }
        if let Some(ref dq) = self.dq {
            validate_base64url_uint(dq, "dq")?;
        }
        if let Some(ref qi) = self.qi {
            validate_base64url_uint(qi, "qi")?;
        }

        // If d is present, validate consistency
        if self.d.is_some() {
            // If any CRT parameter is present, all should be present (RFC 7518 recommendation)
            let has_crt = self.p.is_some()
                || self.q.is_some()
                || self.dp.is_some()
                || self.dq.is_some()
                || self.qi.is_some();

            let has_all_crt = self.p.is_some()
                && self.q.is_some()
                && self.dp.is_some()
                && self.dq.is_some()
                && self.qi.is_some();

            if has_crt && !has_all_crt {
                return Err(InvalidKeyError::InconsistentParameters(
                    "RSA CRT parameters must all be present or all be absent".to_string(),
                )
                .into());
            }

            // RFC 7518 Section 6.3.2.7: oth requires all CRT parameters
            if self.oth.is_some() && !has_all_crt {
                return Err(InvalidKeyError::InconsistentParameters(
                    "RSA 'oth' parameter requires all CRT parameters (p, q, dp, dq, qi)"
                        .to_string(),
                )
                .into());
            }

            // Validate each entry in oth
            if let Some(ref oth) = self.oth {
                if oth.is_empty() {
                    return Err(InvalidKeyError::InvalidParameter {
                        name: "oth",
                        reason: "RFC 7518: 'oth' must contain one or more entries when present"
                            .to_string(),
                    }
                    .into());
                }
                for (i, prime) in oth.iter().enumerate() {
                    prime.validate().map_err(|e| match e {
                        crate::Error::InvalidKey(source) => InvalidKeyError::InvalidOtherPrime {
                            index: i,
                            source: Box::new(source),
                        },
                        _ => {
                            unreachable!("RsaOtherPrime::validate always yields Error::InvalidKey")
                        }
                    })?;
                }
            }
        } else {
            // If d is not present, no other private params should be present
            if self.p.is_some()
                || self.q.is_some()
                || self.dp.is_some()
                || self.dq.is_some()
                || self.qi.is_some()
                || self.oth.is_some()
            {
                return Err(InvalidKeyError::InconsistentParameters(
                    "CRT parameters present without private exponent d".to_string(),
                )
                .into());
            }
        }

        Ok(())
    }

    /// Validates the RSA key size meets minimum requirements.
    ///
    /// RFC 7518 recommends RSA keys be at least 2048 bits.
    ///
    /// # Errors
    ///
    /// Returns an error if the key size is below the minimum.
    ///
    /// # Examples
    ///
    /// ```
    /// use jwk_simple::RsaParams;
    /// use jwk_simple::encoding::Base64UrlBytes;
    ///
    /// let mut modulus = vec![0; 256];
    /// modulus[0] = 0x80;
    /// let params = RsaParams::new_public(
    ///     Base64UrlBytes::new(modulus), // 2048 bits
    ///     Base64UrlBytes::new(vec![1, 0, 1]),
    /// );
    /// assert!(params.validate_key_size(2048).is_ok());
    /// assert!(params.validate_key_size(4096).is_err());
    /// ```
    pub fn validate_key_size(&self, min_bits: usize) -> Result<()> {
        let actual_bits = self.key_size_bits();
        if actual_bits < min_bits {
            return Err(IncompatibleKeyError::InsufficientKeyStrength {
                minimum_bits: min_bits,
                actual_bits,
                context: "RSA modulus",
            }
            .into());
        }
        Ok(())
    }

    /// Returns `true` if this is a multi-prime RSA key.
    ///
    /// Multi-prime RSA keys have more than two prime factors, indicated
    /// by the presence of the `oth` parameter.
    pub fn is_multi_prime(&self) -> bool {
        self.oth.is_some()
    }

    /// Extracts only the public key parameters.
    #[must_use]
    pub fn to_public(&self) -> Self {
        Self {
            n: self.n.clone(),
            e: self.e.clone(),
            d: None,
            p: None,
            q: None,
            dp: None,
            dq: None,
            qi: None,
            oth: None,
        }
    }
}

/// Builder for constructing RSA private key parameters.
///
/// This builder provides a fluent API for constructing RSA private keys
/// with optional CRT (Chinese Remainder Theorem) parameters.
///
/// # Examples
///
/// ```
/// use jwk_simple::RsaParams;
/// use jwk_simple::encoding::Base64UrlBytes;
///
/// // Build a private key with CRT parameters
/// let params = RsaParams::builder(
///     Base64UrlBytes::new(vec![1; 256]),  // n (modulus)
///     Base64UrlBytes::new(vec![1, 0, 1]), // e (exponent)
///     Base64UrlBytes::new(vec![2; 256]),  // d (private exponent)
/// )
/// .p(Base64UrlBytes::new(vec![3; 128]))
/// .q(Base64UrlBytes::new(vec![4; 128]))
/// .dp(Base64UrlBytes::new(vec![5; 128]))
/// .dq(Base64UrlBytes::new(vec![6; 128]))
/// .qi(Base64UrlBytes::new(vec![7; 128]))
/// .build();
///
/// assert!(params.has_private_key());
/// ```
// Safety: `Base64UrlBytes` implements a redacted `Debug` that only prints
// the byte count (e.g. `Base64UrlBytes([32 bytes])`), so derived `Debug`
// here will never expose private key material (d, p, q, dp, dq, qi).
#[derive(Clone, Debug)]
pub struct RsaParamsBuilder {
    n: Base64UrlBytes,
    e: Base64UrlBytes,
    d: Base64UrlBytes,
    p: Option<Base64UrlBytes>,
    q: Option<Base64UrlBytes>,
    dp: Option<Base64UrlBytes>,
    dq: Option<Base64UrlBytes>,
    qi: Option<Base64UrlBytes>,
    oth: Option<Vec<RsaOtherPrime>>,
}

impl RsaParamsBuilder {
    /// Creates a new builder with required parameters.
    #[must_use]
    fn new(n: Base64UrlBytes, e: Base64UrlBytes, d: Base64UrlBytes) -> Self {
        Self {
            n,
            e,
            d,
            p: None,
            q: None,
            dp: None,
            dq: None,
            qi: None,
            oth: None,
        }
    }

    /// Sets the first prime factor.
    #[must_use]
    pub fn p(mut self, p: Base64UrlBytes) -> Self {
        self.p = Some(p);
        self
    }

    /// Sets the second prime factor.
    #[must_use]
    pub fn q(mut self, q: Base64UrlBytes) -> Self {
        self.q = Some(q);
        self
    }

    /// Sets the first factor CRT exponent (d mod (p-1)).
    #[must_use]
    pub fn dp(mut self, dp: Base64UrlBytes) -> Self {
        self.dp = Some(dp);
        self
    }

    /// Sets the second factor CRT exponent (d mod (q-1)).
    #[must_use]
    pub fn dq(mut self, dq: Base64UrlBytes) -> Self {
        self.dq = Some(dq);
        self
    }

    /// Sets the first CRT coefficient (q^-1 mod p).
    #[must_use]
    pub fn qi(mut self, qi: Base64UrlBytes) -> Self {
        self.qi = Some(qi);
        self
    }

    /// Sets all CRT parameters at once.
    #[must_use]
    pub fn crt(
        mut self,
        p: Base64UrlBytes,
        q: Base64UrlBytes,
        dp: Base64UrlBytes,
        dq: Base64UrlBytes,
        qi: Base64UrlBytes,
    ) -> Self {
        self.p = Some(p);
        self.q = Some(q);
        self.dp = Some(dp);
        self.dq = Some(dq);
        self.qi = Some(qi);
        self
    }

    /// Sets the other primes info for multi-prime RSA keys.
    ///
    /// Note: Using `oth` requires all CRT parameters to be set.
    #[must_use]
    pub fn oth(mut self, oth: Vec<RsaOtherPrime>) -> Self {
        self.oth = Some(oth);
        self
    }

    /// Builds the RSA parameters.
    #[must_use]
    pub fn build(self) -> RsaParams {
        RsaParams {
            n: self.n,
            e: self.e,
            d: Some(self.d),
            p: self.p,
            q: self.q,
            dp: self.dp,
            dq: self.dq,
            qi: self.qi,
            oth: self.oth,
        }
    }
}

impl Debug for RsaParams {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RsaParams")
            .field("n", &format!("[{} bytes]", self.n.len()))
            .field("e", &self.e)
            .field("has_private_key", &self.has_private_key())
            .finish()
    }
}

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

    #[test]
    fn test_public_key_only() {
        let params = RsaParams::new_public(
            Base64UrlBytes::new(vec![1, 2, 3]),
            Base64UrlBytes::new(vec![1, 0, 1]),
        );
        assert!(params.is_public_key_only());
        assert!(!params.has_private_key());
    }

    #[test]
    fn test_validate_empty_modulus() {
        let params =
            RsaParams::new_public(Base64UrlBytes::new(vec![]), Base64UrlBytes::new(vec![1]));
        assert!(params.validate().is_err());
    }

    #[test]
    fn test_validate_zero_modulus_rejected() {
        let params = RsaParams::new_public(
            Base64UrlBytes::new(vec![0]),
            Base64UrlBytes::new(vec![1, 0, 1]),
        );
        assert!(params.validate().is_err());
    }

    #[test]
    fn test_validate_exponent_constraints() {
        let params = RsaParams::new_public(
            Base64UrlBytes::new(vec![1, 2, 3]),
            Base64UrlBytes::new(vec![1]),
        );
        assert!(params.validate().is_err(), "e=1 should be rejected");

        let params = RsaParams::new_public(
            Base64UrlBytes::new(vec![1, 2, 3]),
            Base64UrlBytes::new(vec![2]),
        );
        assert!(
            params.validate().is_err(),
            "even exponent should be rejected"
        );

        let params = RsaParams::new_public(
            Base64UrlBytes::new(vec![1, 2, 3]),
            Base64UrlBytes::new(vec![3]),
        );
        assert!(params.validate().is_ok(), "e=3 should be accepted");
    }

    #[test]
    fn test_validate_partial_crt() {
        let params = RsaParams {
            n: Base64UrlBytes::new(vec![1]),
            e: Base64UrlBytes::new(vec![1]),
            d: Some(Base64UrlBytes::new(vec![1])),
            p: Some(Base64UrlBytes::new(vec![1])),
            q: None, // Missing
            dp: None,
            dq: None,
            qi: None,
            oth: None,
        };
        assert!(params.validate().is_err());
    }

    #[test]
    fn test_validate_rejects_empty_private_integer() {
        let params = RsaParams {
            n: Base64UrlBytes::new(vec![1]),
            e: Base64UrlBytes::new(vec![1]),
            d: Some(Base64UrlBytes::new(vec![])),
            p: None,
            q: None,
            dp: None,
            dq: None,
            qi: None,
            oth: None,
        };
        assert!(params.validate().is_err());
    }

    #[test]
    fn test_validate_rejects_empty_oth_array() {
        let params = RsaParams {
            n: Base64UrlBytes::new(vec![1]),
            e: Base64UrlBytes::new(vec![1]),
            d: Some(Base64UrlBytes::new(vec![1])),
            p: Some(Base64UrlBytes::new(vec![1])),
            q: Some(Base64UrlBytes::new(vec![1])),
            dp: Some(Base64UrlBytes::new(vec![1])),
            dq: Some(Base64UrlBytes::new(vec![1])),
            qi: Some(Base64UrlBytes::new(vec![1])),
            oth: Some(vec![]),
        };
        assert!(params.validate().is_err());
    }

    #[test]
    fn test_validate_preserves_typed_oth_errors_with_index() {
        let params = RsaParams {
            n: Base64UrlBytes::new(vec![1]),
            e: Base64UrlBytes::new(vec![3]),
            d: Some(Base64UrlBytes::new(vec![1])),
            p: Some(Base64UrlBytes::new(vec![1])),
            q: Some(Base64UrlBytes::new(vec![1])),
            dp: Some(Base64UrlBytes::new(vec![1])),
            dq: Some(Base64UrlBytes::new(vec![1])),
            qi: Some(Base64UrlBytes::new(vec![1])),
            oth: Some(vec![RsaOtherPrime::new(
                Base64UrlBytes::new(vec![]),
                Base64UrlBytes::new(vec![1]),
                Base64UrlBytes::new(vec![1]),
            )]),
        };

        let err = params.validate().unwrap_err();
        assert!(matches!(
            err,
            crate::Error::InvalidKey(InvalidKeyError::InvalidOtherPrime {
                index: 0,
                source
            }) if matches!(*source, InvalidKeyError::MissingParameter("oth.r"))
        ));
    }

    #[test]
    fn test_validate_canonical_base64url_uint() {
        // RFC 7518 Section 2: Base64urlUInt values MUST use minimum octets.
        // A modulus with a leading zero byte is non-canonical.
        let params = RsaParams::new_public(
            Base64UrlBytes::new(vec![0, 1, 2, 3]), // leading zero = non-canonical
            Base64UrlBytes::new(vec![1, 0, 1]),
        );
        let result = params.validate();
        assert!(result.is_err(), "Leading zero byte in n should be rejected");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("Base64urlUInt") || err.contains("canonical"),
            "Error should mention canonical encoding: {}",
            err
        );

        // An exponent with a leading zero byte is also non-canonical
        let params = RsaParams::new_public(
            Base64UrlBytes::new(vec![1, 2, 3]),
            Base64UrlBytes::new(vec![0, 1, 0, 1]), // leading zero = non-canonical
        );
        assert!(
            params.validate().is_err(),
            "Leading zero byte in e should be rejected"
        );

        // Single zero byte is canonical encoding for 0, but RSA modulus 0 is
        // semantically invalid and should be rejected.
        let params = RsaParams::new_public(
            Base64UrlBytes::new(vec![0]), // single zero = canonical representation of 0
            Base64UrlBytes::new(vec![1, 0, 1]),
        );
        assert!(
            params.validate().is_err(),
            "RSA modulus 0 should be rejected as semantically invalid"
        );

        // Canonical values should pass
        let params = RsaParams::new_public(
            Base64UrlBytes::new(vec![1, 2, 3]),
            Base64UrlBytes::new(vec![1, 0, 1]),
        );
        assert!(
            params.validate().is_ok(),
            "Canonical values should pass validation"
        );
    }

    #[test]
    fn test_validate_key_size() {
        // 2048-bit key (256 bytes)
        let mut n_2048 = vec![0; 256];
        n_2048[0] = 0x80; // ensure exact 2048-bit length
        let params_2048 = RsaParams::new_public(
            Base64UrlBytes::new(n_2048),
            Base64UrlBytes::new(vec![1, 0, 1]),
        );
        assert!(params_2048.validate_key_size(2048).is_ok());
        assert!(params_2048.validate_key_size(4096).is_err());

        // 1024-bit key (128 bytes) - too small
        let mut n_1024 = vec![0; 128];
        n_1024[0] = 0x80; // ensure exact 1024-bit length
        let params_1024 = RsaParams::new_public(
            Base64UrlBytes::new(n_1024),
            Base64UrlBytes::new(vec![1, 0, 1]),
        );
        assert!(params_1024.validate_key_size(2048).is_err());
    }

    #[test]
    fn test_multi_prime_detection() {
        let public_key =
            RsaParams::new_public(Base64UrlBytes::new(vec![1]), Base64UrlBytes::new(vec![1]));
        assert!(!public_key.is_multi_prime());

        let standard_private = RsaParams::new_private(
            Base64UrlBytes::new(vec![1]),
            Base64UrlBytes::new(vec![1]),
            Base64UrlBytes::new(vec![1]),
            Some(Base64UrlBytes::new(vec![1])),
            Some(Base64UrlBytes::new(vec![1])),
            Some(Base64UrlBytes::new(vec![1])),
            Some(Base64UrlBytes::new(vec![1])),
            Some(Base64UrlBytes::new(vec![1])),
        );
        assert!(!standard_private.is_multi_prime());

        let multi_prime = RsaParams::new_multi_prime(
            Base64UrlBytes::new(vec![1]),
            Base64UrlBytes::new(vec![1]),
            Base64UrlBytes::new(vec![1]),
            Base64UrlBytes::new(vec![1]),
            Base64UrlBytes::new(vec![1]),
            Base64UrlBytes::new(vec![1]),
            Base64UrlBytes::new(vec![1]),
            Base64UrlBytes::new(vec![1]),
            vec![RsaOtherPrime::new(
                Base64UrlBytes::new(vec![1]),
                Base64UrlBytes::new(vec![1]),
                Base64UrlBytes::new(vec![1]),
            )],
        );
        assert!(multi_prime.is_multi_prime());
    }
}