jwtiny 1.8.2

Minimal JWT validation library for build web services
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
//! JWK (JSON Web Key) struct and conversion

use crate::algorithm::AlgorithmType;
use crate::error::{Error, Result};
use crate::limits::{
    MAX_JWK_ALG_SIZE, MAX_JWK_CRV_SIZE, MAX_JWK_E_SIZE, MAX_JWK_KID_SIZE, MAX_JWK_N_SIZE,
    MAX_JWK_X_SIZE, MAX_JWK_Y_SIZE,
};
use crate::utils::base64url;
use miniserde::Deserialize;

/// JSON Web Key (JWK) structure
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct Jwk {
    /// Key type (e.g., "RSA")
    pub kty: Option<String>,
    /// Key ID
    pub kid: Option<String>,
    /// Algorithm (advisory field per RFC 7517)
    /// Used in `validate_jwk_algorithm()` for strict algorithm matching
    pub alg: Option<String>,
    /// Key use (RFC 7517 Section 4.2)
    ///
    /// Indicates the intended use of the key:
    /// - "sig" for signature verification
    /// - "enc" for encryption
    ///
    /// If absent, the key may be used for any purpose.
    #[serde(rename = "use")]
    pub key_use: Option<String>,
    // RSA fields
    /// RSA modulus (Base64URL-encoded)
    pub n: Option<String>,
    /// RSA exponent (Base64URL-encoded)
    pub e: Option<String>,
    // ECDSA fields
    /// Elliptic curve name (e.g., "P-256", "P-384", "P-521")
    pub crv: Option<String>,
    /// ECDSA x-coordinate (Base64URL-encoded)
    pub x: Option<String>,
    /// ECDSA y-coordinate (Base64URL-encoded)
    pub y: Option<String>,
}

impl Jwk {
    /// Convert JWK to DER-encoded public key
    ///
    /// # Arguments
    /// * `algorithm` - The algorithm from the token header
    /// * `strict_algorithm` - If true, require JWK `alg` field to match token algorithm
    ///
    /// # Returns
    /// DER-encoded SubjectPublicKeyInfo bytes
    pub(crate) fn to_key(
        &self,
        algorithm: &AlgorithmType,
        strict_algorithm: bool,
    ) -> Result<Vec<u8>> {
        self.validate_jwk_structure(algorithm)?;
        self.validate_jwk_algorithm(algorithm, strict_algorithm)?;
        // Convert based on algorithm
        match algorithm {
            AlgorithmType::RS256 | AlgorithmType::RS384 | AlgorithmType::RS512 => self.to_rsa_key(),
            AlgorithmType::ES256 | AlgorithmType::ES384 | AlgorithmType::ES512 => {
                self.to_ecdsa_key(algorithm)
            }
        }
    }

    /// Validate JWK structure: key type, key use, and field sizes
    fn validate_jwk_structure(&self, algorithm: &AlgorithmType) -> Result<()> {
        let expected_kty = match algorithm {
            AlgorithmType::RS256 | AlgorithmType::RS384 | AlgorithmType::RS512 => "RSA",
            AlgorithmType::ES256 | AlgorithmType::ES384 | AlgorithmType::ES512 => "EC",
        };

        // Check kty matches expected
        if let Some(kty) = &self.kty {
            if kty != expected_kty {
                return Err(Error::RemoteError(format!(
                    "jwks: key type mismatch: expected {expected_kty}, found {kty}"
                )));
            }
        } else {
            return Err(Error::RemoteError("jwks: missing key type (kty)".into()));
        }

        // Check key use (RFC 7517 Section 4.2)
        // If present, must be "sig" for signature verification
        // "enc" indicates encryption keys, which should not be used for signing
        if let Some(use_val) = &self.key_use {
            if use_val != "sig" {
                return Err(Error::RemoteError(format!(
                    "jwks: key use mismatch: expected 'sig' for signature verification, found '{use_val}'"
                )));
            }
        }

        // Validate algorithm field size
        if let Some(alg) = &self.alg {
            if alg.len() > MAX_JWK_ALG_SIZE {
                return Err(Error::JwkFieldTooLarge {
                    field: "alg".into(),
                    size: alg.len(),
                    max: MAX_JWK_ALG_SIZE,
                });
            }
        }

        // Validate kid field size
        if let Some(kid) = &self.kid {
            if kid.len() > MAX_JWK_KID_SIZE {
                return Err(Error::JwkFieldTooLarge {
                    field: "kid".into(),
                    size: kid.len(),
                    max: MAX_JWK_KID_SIZE,
                });
            }
        }

        // Validate ECDSA-specific fields
        if matches!(
            algorithm,
            AlgorithmType::ES256 | AlgorithmType::ES384 | AlgorithmType::ES512
        ) {
            if let Some(crv) = &self.crv {
                if crv.len() > MAX_JWK_CRV_SIZE {
                    return Err(Error::JwkFieldTooLarge {
                        field: "crv".into(),
                        size: crv.len(),
                        max: MAX_JWK_CRV_SIZE,
                    });
                }
            }
            if let Some(x) = &self.x {
                if x.len() > MAX_JWK_X_SIZE {
                    return Err(Error::JwkFieldTooLarge {
                        field: "x".into(),
                        size: x.len(),
                        max: MAX_JWK_X_SIZE,
                    });
                }
            }
            if let Some(y) = &self.y {
                if y.len() > MAX_JWK_Y_SIZE {
                    return Err(Error::JwkFieldTooLarge {
                        field: "y".into(),
                        size: y.len(),
                        max: MAX_JWK_Y_SIZE,
                    });
                }
            }
        }

        Ok(())
    }

    /// Validate JWK algorithm field against token algorithm
    ///
    /// When strict_algorithm is false, JWK alg field is advisory per RFC 7517.
    /// The token algorithm from the header is authoritative.
    fn validate_jwk_algorithm(
        &self,
        algorithm: &AlgorithmType,
        strict_algorithm: bool,
    ) -> Result<()> {
        if strict_algorithm {
            if let Some(jwk_alg) = &self.alg {
                let token_alg = algorithm.as_str();
                if jwk_alg != token_alg {
                    return Err(Error::JwkAlgorithmMismatch {
                        jwk_alg: jwk_alg.to_string(),
                        token_alg: token_alg.to_string(),
                    });
                }
            }
            // In strict mode, alg field must be present
            if self.alg.is_none() {
                return Err(Error::RemoteError(
                    "jwks: strict algorithm mode requires JWK alg field".into(),
                ));
            }
        }
        Ok(())
    }

    /// Convert JWK to DER-encoded RSA public key
    fn to_rsa_key(&self) -> Result<Vec<u8>> {
        // Base64URL: 4 chars → 3 bytes, so max_decoded = (max_encoded * 3) / 4
        // For n: 12KB encoded → 9KB decoded (conservative: 12KB * 3 / 4 = 9KB)
        // For e: 64 bytes encoded → 48 bytes decoded (64 * 3 / 4 = 48)
        const MAX_DECODED_JWK_N: usize = (MAX_JWK_N_SIZE * 3) / 4;
        const MAX_DECODED_JWK_E: usize = (MAX_JWK_E_SIZE * 3) / 4;

        // Extract and validate field sizes before decoding
        let n = self
            .n
            .as_deref()
            .ok_or_else(|| Error::RemoteError("jwks: rsa key missing n (modulus)".into()))?;
        let e = self
            .e
            .as_deref()
            .ok_or_else(|| Error::RemoteError("jwks: rsa key missing e (exponent)".into()))?;

        // Validate Base64URL-encoded field sizes before decoding
        if n.len() > MAX_JWK_N_SIZE {
            return Err(Error::JwkFieldTooLarge {
                field: "n".into(),
                size: n.len(),
                max: MAX_JWK_N_SIZE,
            });
        }
        if e.len() > MAX_JWK_E_SIZE {
            return Err(Error::JwkFieldTooLarge {
                field: "e".into(),
                size: e.len(),
                max: MAX_JWK_E_SIZE,
            });
        }

        let n_bytes = base64url::decode_bytes(n, MAX_DECODED_JWK_N)
            .map_err(|e| Error::RemoteError(format!("jwks: failed to decode n: {e}")))?;
        let e_bytes = base64url::decode_bytes(e, MAX_DECODED_JWK_E)
            .map_err(|e| Error::RemoteError(format!("jwks: failed to decode e: {e}")))?;

        // Convert to DER SubjectPublicKeyInfo
        crate::utils::der::rsa_spki_from_n_e(&n_bytes, &e_bytes)
    }

    /// Convert JWK to DER-encoded ECDSA public key
    fn to_ecdsa_key(&self, algorithm: &AlgorithmType) -> Result<Vec<u8>> {
        use crate::utils::der::{EcdsaCurve, ecdsa_spki_from_xy};

        // Base64URL: 4 chars → 3 bytes
        // For P-521: 66 bytes raw → ~88 bytes Base64URL
        const MAX_DECODED_JWK_X: usize = (MAX_JWK_X_SIZE * 3) / 4;
        const MAX_DECODED_JWK_Y: usize = (MAX_JWK_Y_SIZE * 3) / 4;

        // Determine curve from algorithm
        let curve = match algorithm {
            AlgorithmType::ES256 => EcdsaCurve::P256,
            AlgorithmType::ES384 => EcdsaCurve::P384,
            AlgorithmType::ES512 => EcdsaCurve::P521,
            _ => {
                return Err(Error::RemoteError(format!(
                    "jwks: algorithm {} is not an ECDSA algorithm",
                    algorithm
                )));
            }
        };

        // Validate curve matches JWK crv field if present
        if let Some(crv) = &self.crv {
            let expected_crv = match curve {
                EcdsaCurve::P256 => "P-256",
                EcdsaCurve::P384 => "P-384",
                EcdsaCurve::P521 => "P-521",
            };
            if crv != expected_crv {
                return Err(Error::RemoteError(format!(
                    "jwks: curve mismatch: {} requires {}, found {}",
                    algorithm, expected_crv, crv
                )));
            }
        } else {
            return Err(Error::RemoteError("jwks: missing curve (crv) field".into()));
        }

        // Extract x and y coordinates
        let x = self
            .x
            .as_deref()
            .ok_or_else(|| Error::RemoteError("jwks: ecdsa key missing x coordinate".into()))?;
        let y = self
            .y
            .as_deref()
            .ok_or_else(|| Error::RemoteError("jwks: ecdsa key missing y coordinate".into()))?;

        let x_bytes = base64url::decode_bytes(x, MAX_DECODED_JWK_X)
            .map_err(|e| Error::RemoteError(format!("jwks: failed to decode x: {e}")))?;
        let y_bytes = base64url::decode_bytes(y, MAX_DECODED_JWK_Y)
            .map_err(|e| Error::RemoteError(format!("jwks: failed to decode y: {e}")))?;

        // Convert to DER SubjectPublicKeyInfo
        ecdsa_spki_from_xy(&x_bytes, &y_bytes, curve)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::algorithm::AlgorithmType;
    use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};

    #[test]
    fn test_jwk_to_rsa_key() {
        // Create a test RSA JWK with minimal valid values
        let jwk = Jwk {
            kty: Some("RSA".to_string()),
            kid: Some("test-key".to_string()),
            alg: Some("RS256".to_string()),
            key_use: None,
            n: Some(URL_SAFE_NO_PAD.encode([0x00, 0x01, 0x02, 0x03])),
            e: Some(URL_SAFE_NO_PAD.encode([0x01, 0x00, 0x01])), // 65537
            crv: None,
            x: None,
            y: None,
        };

        let key = jwk.to_key(&AlgorithmType::RS256, false);
        assert!(key.is_ok());
        let key = key.unwrap();
        // Key should be RSA public key
        assert!(!key.is_empty());
    }

    #[test]
    fn test_jwk_to_rsa_key_missing_n() {
        let jwk = Jwk {
            kty: Some("RSA".to_string()),
            kid: None,
            alg: None,
            key_use: None,
            n: None,
            e: Some(URL_SAFE_NO_PAD.encode([0x01, 0x00, 0x01])),
            crv: None,
            x: None,
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::RS256, false);
        assert!(matches!(result, Err(Error::RemoteError(msg)) if msg.contains("missing n")));
    }

    #[test]
    fn test_jwk_to_rsa_key_missing_kty() {
        let jwk = Jwk {
            kty: None,
            kid: None,
            alg: None,
            key_use: None,
            n: Some(URL_SAFE_NO_PAD.encode([0x01])),
            e: Some(URL_SAFE_NO_PAD.encode([0x01])),
            crv: None,
            x: None,
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::RS256, false);
        assert!(matches!(result, Err(Error::RemoteError(msg)) if msg.contains("missing key type")));
    }

    #[test]
    fn test_jwk_to_rsa_key_wrong_kty() {
        let jwk = Jwk {
            kty: Some("EC".to_string()),
            kid: None,
            alg: None,
            key_use: None,
            n: Some(URL_SAFE_NO_PAD.encode([0x01])),
            e: Some(URL_SAFE_NO_PAD.encode([0x01])),
            crv: None,
            x: None,
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::RS256, false);
        assert!(
            matches!(result, Err(Error::RemoteError(msg)) if msg.contains("key type mismatch"))
        );
    }

    #[test]
    fn test_jwk_key_use_sig() {
        // Key with use="sig" should be accepted
        let jwk = Jwk {
            kty: Some("RSA".to_string()),
            kid: None,
            alg: None,
            key_use: Some("sig".to_string()),
            n: Some(URL_SAFE_NO_PAD.encode([0x00, 0x01, 0x02, 0x03])),
            e: Some(URL_SAFE_NO_PAD.encode([0x01, 0x00, 0x01])),
            crv: None,
            x: None,
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::RS256, false);
        assert!(result.is_ok(), "Key with use='sig' should be accepted");
    }

    #[test]
    fn test_jwk_key_use_enc() {
        // Key with use="enc" should be rejected (wrong purpose)
        let jwk = Jwk {
            kty: Some("RSA".to_string()),
            kid: None,
            alg: None,
            key_use: Some("enc".to_string()),
            n: Some(URL_SAFE_NO_PAD.encode([0x00, 0x01, 0x02, 0x03])),
            e: Some(URL_SAFE_NO_PAD.encode([0x01, 0x00, 0x01])),
            crv: None,
            x: None,
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::RS256, false);
        assert!(
            matches!(result, Err(Error::RemoteError(msg)) if msg.contains("key use mismatch")),
            "Key with use='enc' should be rejected"
        );
    }

    #[test]
    fn test_jwk_key_use_missing() {
        // Key with use missing should be accepted (optional field)
        let jwk = Jwk {
            kty: Some("RSA".to_string()),
            kid: None,
            alg: None,
            key_use: None,
            n: Some(URL_SAFE_NO_PAD.encode([0x00, 0x01, 0x02, 0x03])),
            e: Some(URL_SAFE_NO_PAD.encode([0x01, 0x00, 0x01])),
            crv: None,
            x: None,
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::RS256, false);
        assert!(result.is_ok(), "Key with use missing should be accepted");
    }

    #[test]
    fn test_jwk_strict_algorithm_match() {
        // Key with matching alg should succeed in strict mode
        let jwk = Jwk {
            kty: Some("RSA".to_string()),
            kid: None,
            alg: Some("RS256".to_string()),
            key_use: None,
            n: Some(URL_SAFE_NO_PAD.encode([0x00, 0x01, 0x02, 0x03])),
            e: Some(URL_SAFE_NO_PAD.encode([0x01, 0x00, 0x01])),
            crv: None,
            x: None,
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::RS256, true);
        assert!(
            result.is_ok(),
            "Key with matching alg should succeed in strict mode"
        );
    }

    #[test]
    fn test_jwk_strict_algorithm_mismatch() {
        // Key with mismatched alg should fail in strict mode
        let jwk = Jwk {
            kty: Some("RSA".to_string()),
            kid: None,
            alg: Some("RS384".to_string()),
            key_use: None,
            n: Some(URL_SAFE_NO_PAD.encode([0x00, 0x01, 0x02, 0x03])),
            e: Some(URL_SAFE_NO_PAD.encode([0x01, 0x00, 0x01])),
            crv: None,
            x: None,
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::RS256, true);
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(Error::JwkAlgorithmMismatch {
                jwk_alg,
                token_alg
            }) if jwk_alg == "RS384" && token_alg == "RS256"
        ));
    }

    #[test]
    fn test_jwk_strict_algorithm_missing() {
        // Key without alg should fail in strict mode
        let jwk = Jwk {
            kty: Some("RSA".to_string()),
            kid: None,
            alg: None,
            key_use: None,
            n: Some(URL_SAFE_NO_PAD.encode([0x00, 0x01, 0x02, 0x03])),
            e: Some(URL_SAFE_NO_PAD.encode([0x01, 0x00, 0x01])),
            crv: None,
            x: None,
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::RS256, true);
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(Error::RemoteError(msg)) if msg.contains("strict algorithm mode requires JWK alg field")
        ));
    }

    #[test]
    fn test_jwk_non_strict_algorithm_mismatch_allowed() {
        // Key with mismatched alg should succeed in non-strict mode (advisory field)
        let jwk = Jwk {
            kty: Some("RSA".to_string()),
            kid: None,
            alg: Some("RS384".to_string()),
            key_use: None,
            n: Some(URL_SAFE_NO_PAD.encode([0x00, 0x01, 0x02, 0x03])),
            e: Some(URL_SAFE_NO_PAD.encode([0x01, 0x00, 0x01])),
            crv: None,
            x: None,
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::RS256, false);
        assert!(
            result.is_ok(),
            "Key with mismatched alg should succeed in non-strict mode"
        );
    }

    #[test]
    fn test_jwk_to_ecdsa_key_p256() {
        // Create a test ECDSA JWK for P-256
        let x = vec![0x01; 32];
        let y = vec![0x02; 32];
        let jwk = Jwk {
            kty: Some("EC".to_string()),
            kid: Some("test-ec-key".to_string()),
            alg: Some("ES256".to_string()),
            key_use: None,
            n: None,
            e: None,
            crv: Some("P-256".to_string()),
            x: Some(URL_SAFE_NO_PAD.encode(&x)),
            y: Some(URL_SAFE_NO_PAD.encode(&y)),
        };

        let key = jwk.to_key(&AlgorithmType::ES256, false);
        assert!(key.is_ok(), "Valid P-256 key should encode successfully");
        assert!(!key.unwrap().is_empty());
    }

    #[test]
    fn test_jwk_to_ecdsa_key_p384() {
        // Create a test ECDSA JWK for P-384
        let x = vec![0x03; 48];
        let y = vec![0x04; 48];
        let jwk = Jwk {
            kty: Some("EC".to_string()),
            kid: None,
            alg: Some("ES384".to_string()),
            key_use: None,
            n: None,
            e: None,
            crv: Some("P-384".to_string()),
            x: Some(URL_SAFE_NO_PAD.encode(&x)),
            y: Some(URL_SAFE_NO_PAD.encode(&y)),
        };

        let key = jwk.to_key(&AlgorithmType::ES384, false);
        assert!(key.is_ok(), "Valid P-384 key should encode successfully");
        assert!(!key.unwrap().is_empty());
    }

    #[test]
    fn test_jwk_to_ecdsa_key_p521() {
        // Create a test ECDSA JWK for P-521
        let x = vec![0x05; 66];
        let y = vec![0x06; 66];
        let jwk = Jwk {
            kty: Some("EC".to_string()),
            kid: None,
            alg: Some("ES512".to_string()),
            key_use: None,
            n: None,
            e: None,
            crv: Some("P-521".to_string()),
            x: Some(URL_SAFE_NO_PAD.encode(&x)),
            y: Some(URL_SAFE_NO_PAD.encode(&y)),
        };

        let key = jwk.to_key(&AlgorithmType::ES512, false);
        assert!(key.is_ok(), "Valid P-521 key should encode successfully");
        assert!(!key.unwrap().is_empty());
    }

    #[test]
    fn test_jwk_to_ecdsa_key_missing_crv() {
        let x = vec![0x01; 32];
        let y = vec![0x02; 32];
        let jwk = Jwk {
            kty: Some("EC".to_string()),
            kid: None,
            alg: None,
            key_use: None,
            n: None,
            e: None,
            crv: None,
            x: Some(URL_SAFE_NO_PAD.encode(&x)),
            y: Some(URL_SAFE_NO_PAD.encode(&y)),
        };

        let result = jwk.to_key(&AlgorithmType::ES256, false);
        assert!(matches!(
            result,
            Err(Error::RemoteError(msg)) if msg.contains("missing curve")
        ));
    }

    #[test]
    fn test_jwk_to_ecdsa_key_wrong_crv() {
        let x = vec![0x01; 32];
        let y = vec![0x02; 32];
        let jwk = Jwk {
            kty: Some("EC".to_string()),
            kid: None,
            alg: None,
            key_use: None,
            n: None,
            e: None,
            crv: Some("P-384".to_string()), // Wrong curve for ES256
            x: Some(URL_SAFE_NO_PAD.encode(&x)),
            y: Some(URL_SAFE_NO_PAD.encode(&y)),
        };

        let result = jwk.to_key(&AlgorithmType::ES256, false);
        assert!(matches!(
            result,
            Err(Error::RemoteError(msg)) if msg.contains("curve mismatch")
        ));
    }

    #[test]
    fn test_jwk_to_ecdsa_key_missing_x() {
        let y = vec![0x02; 32];
        let jwk = Jwk {
            kty: Some("EC".to_string()),
            kid: None,
            alg: None,
            key_use: None,
            n: None,
            e: None,
            crv: Some("P-256".to_string()),
            x: None,
            y: Some(URL_SAFE_NO_PAD.encode(&y)),
        };

        let result = jwk.to_key(&AlgorithmType::ES256, false);
        assert!(matches!(
            result,
            Err(Error::RemoteError(msg)) if msg.contains("missing x")
        ));
    }

    #[test]
    fn test_jwk_to_ecdsa_key_missing_y() {
        let x = vec![0x01; 32];
        let jwk = Jwk {
            kty: Some("EC".to_string()),
            kid: None,
            alg: None,
            key_use: None,
            n: None,
            e: None,
            crv: Some("P-256".to_string()),
            x: Some(URL_SAFE_NO_PAD.encode(&x)),
            y: None,
        };

        let result = jwk.to_key(&AlgorithmType::ES256, false);
        assert!(matches!(
            result,
            Err(Error::RemoteError(msg)) if msg.contains("missing y")
        ));
    }
}