pulith-fetch 0.2.0

HTTP downloading with streaming verification and atomic placement
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
//! Digital signature verification functionality.
//!
//! This module provides types and interfaces for verifying
//! digital signatures of downloaded content. Currently provides
//! type definitions and interfaces for future implementation.

use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Supported signature algorithms.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SignatureAlgorithm {
    /// RSA with PKCS#1 v1.5 padding
    RsaPkcs1v15,
    /// RSA with PSS padding
    RsaPss,
    /// Elliptic Curve Digital Signature Algorithm (ECDSA)
    Ecdsa,
    /// EdDSA (Ed25519/Ed448)
    EdDsa,
    /// Digital Signature Algorithm (DSA) - deprecated
    Dsa,
}

impl SignatureAlgorithm {
    /// Get the string representation of this algorithm.
    pub fn as_str(&self) -> &'static str {
        match self {
            SignatureAlgorithm::RsaPkcs1v15 => "rsa-pkcs1v15",
            SignatureAlgorithm::RsaPss => "rsa-pss",
            SignatureAlgorithm::Ecdsa => "ecdsa",
            SignatureAlgorithm::EdDsa => "eddsa",
            SignatureAlgorithm::Dsa => "dsa",
        }
    }

    /// Get the minimum key size in bits for this algorithm.
    pub fn min_key_size(&self) -> usize {
        match self {
            SignatureAlgorithm::RsaPkcs1v15 => 2048,
            SignatureAlgorithm::RsaPss => 2048,
            SignatureAlgorithm::Ecdsa => 256,
            SignatureAlgorithm::EdDsa => 256,
            SignatureAlgorithm::Dsa => 2048,
        }
    }

    /// Check if this algorithm is considered secure for current use.
    pub fn is_secure(&self) -> bool {
        match self {
            SignatureAlgorithm::RsaPkcs1v15 => true,
            SignatureAlgorithm::RsaPss => true,
            SignatureAlgorithm::Ecdsa => true,
            SignatureAlgorithm::EdDsa => true,
            SignatureAlgorithm::Dsa => false, // DSA is deprecated
        }
    }
}

/// Format of the signature data.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SignatureFormat {
    /// Raw binary signature
    Raw,
    /// Base64-encoded signature
    Base64,
    /// Hexadecimal-encoded signature
    Hex,
    /// PEM format
    Pem,
    /// DER format
    Der,
}

impl SignatureFormat {
    /// Get the string representation of this format.
    pub fn as_str(&self) -> &'static str {
        match self {
            SignatureFormat::Raw => "raw",
            SignatureFormat::Base64 => "base64",
            SignatureFormat::Hex => "hex",
            SignatureFormat::Pem => "pem",
            SignatureFormat::Der => "der",
        }
    }
}

/// Public key format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PublicKeyFormat {
    /// PEM format
    Pem,
    /// DER format
    Der,
    /// Raw key bytes
    Raw,
    /// JWK (JSON Web Key) format
    Jwk,
    /// SSH public key format
    Ssh,
}

impl PublicKeyFormat {
    /// Get the string representation of this format.
    pub fn as_str(&self) -> &'static str {
        match self {
            PublicKeyFormat::Pem => "pem",
            PublicKeyFormat::Der => "der",
            PublicKeyFormat::Raw => "raw",
            PublicKeyFormat::Jwk => "jwk",
            PublicKeyFormat::Ssh => "ssh",
        }
    }
}

/// A public key for signature verification.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PublicKey {
    /// The key algorithm
    pub algorithm: SignatureAlgorithm,
    /// The key format
    pub format: PublicKeyFormat,
    /// The key data
    pub data: Vec<u8>,
    /// Optional key identifier
    pub key_id: Option<String>,
    /// Optional key usage constraints
    pub usage: Option<KeyUsage>,
}

impl PublicKey {
    /// Create a new public key.
    pub fn new(algorithm: SignatureAlgorithm, format: PublicKeyFormat, data: Vec<u8>) -> Self {
        Self {
            algorithm,
            format,
            data,
            key_id: None,
            usage: None,
        }
    }

    /// Set the key identifier.
    pub fn with_key_id(mut self, key_id: String) -> Self {
        self.key_id = Some(key_id);
        self
    }

    /// Set the key usage constraints.
    pub fn with_usage(mut self, usage: KeyUsage) -> Self {
        self.usage = Some(usage);
        self
    }

    /// Get the key size in bits.
    pub fn key_size(&self) -> usize {
        self.data.len() * 8
    }

    /// Validate the key parameters.
    pub fn validate(&self) -> Result<()> {
        if !self.algorithm.is_secure() {
            return Err(Error::InvalidState(format!(
                "Algorithm {:?} is not considered secure",
                self.algorithm
            )));
        }

        if self.key_size() < self.algorithm.min_key_size() {
            return Err(Error::InvalidState(format!(
                "Key size {} is below minimum {} for algorithm {:?}",
                self.key_size(),
                self.algorithm.min_key_size(),
                self.algorithm
            )));
        }

        Ok(())
    }
}

/// Key usage constraints.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyUsage {
    /// Allowed purposes for this key
    pub purposes: Vec<KeyPurpose>,
    /// Expiration time (Unix timestamp)
    pub expires_at: Option<u64>,
    /// Not valid before (Unix timestamp)
    pub not_before: Option<u64>,
}

/// Key purpose types.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum KeyPurpose {
    /// Code signing
    CodeSigning,
    /// Document signing
    DocumentSigning,
    /// Timestamp signing
    TimestampSigning,
    /// Certificate signing
    CertificateSigning,
    /// Custom purpose
    Custom(String),
}

/// A digital signature.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Signature {
    /// The signature algorithm
    pub algorithm: SignatureAlgorithm,
    /// The signature format
    pub format: SignatureFormat,
    /// The signature data
    pub data: Vec<u8>,
    /// Optional identifier of the key used
    pub key_id: Option<String>,
    /// Timestamp when signature was created (Unix timestamp)
    pub created_at: Option<u64>,
    /// Timestamp when signature expires (Unix timestamp)
    pub expires_at: Option<u64>,
}

impl Signature {
    /// Create a new signature.
    pub fn new(algorithm: SignatureAlgorithm, format: SignatureFormat, data: Vec<u8>) -> Self {
        Self {
            algorithm,
            format,
            data,
            key_id: None,
            created_at: None,
            expires_at: None,
        }
    }

    /// Set the key identifier.
    pub fn with_key_id(mut self, key_id: String) -> Self {
        self.key_id = Some(key_id);
        self
    }

    /// Set the creation timestamp.
    pub fn with_created_at(mut self, created_at: u64) -> Self {
        self.created_at = Some(created_at);
        self
    }

    /// Set the expiration timestamp.
    pub fn with_expires_at(mut self, expires_at: u64) -> Self {
        self.expires_at = Some(expires_at);
        self
    }

    /// Check if the signature has expired.
    pub fn is_expired(&self) -> bool {
        if let Some(expires_at) = self.expires_at {
            // Use current time (placeholder - would use actual current time)
            let now = 0; // Placeholder
            now > expires_at
        } else {
            false
        }
    }

    /// Decode the signature data based on its format.
    pub fn decode_data(&self) -> Result<Vec<u8>> {
        match self.format {
            SignatureFormat::Raw => Ok(self.data.clone()),
            SignatureFormat::Base64 => {
                use base64::{Engine as _, engine::general_purpose};
                general_purpose::STANDARD
                    .decode(&self.data)
                    .map_err(|e| Error::InvalidState(format!("Invalid base64 signature: {}", e)))
            }
            SignatureFormat::Hex => hex::decode(&self.data)
                .map_err(|e| Error::InvalidState(format!("Invalid hex signature: {}", e))),
            SignatureFormat::Pem | SignatureFormat::Der => {
                // Would need proper PEM/DER parsing
                Err(Error::InvalidState(
                    "PEM/DER signature parsing not implemented".to_string(),
                ))
            }
        }
    }
}

/// Signature verification configuration.
#[derive(Debug, Clone)]
pub struct SignatureConfig {
    /// The public key to verify with
    pub public_key: PublicKey,
    /// The expected signature
    pub signature: Signature,
    /// The data that was signed (if known)
    pub signed_data: Option<Vec<u8>>,
    /// Whether to ignore expired signatures
    pub ignore_expired: bool,
}

impl SignatureConfig {
    /// Create a new signature configuration.
    pub fn new(public_key: PublicKey, signature: Signature) -> Self {
        Self {
            public_key,
            signature,
            signed_data: None,
            ignore_expired: false,
        }
    }

    /// Set the signed data.
    pub fn with_signed_data(mut self, data: Vec<u8>) -> Self {
        self.signed_data = Some(data);
        self
    }

    /// Set whether to ignore expired signatures.
    pub fn with_ignore_expired(mut self, ignore: bool) -> Self {
        self.ignore_expired = ignore;
        self
    }

    /// Validate the configuration.
    pub fn validate(&self) -> Result<()> {
        // Validate public key
        self.public_key.validate()?;

        // Check algorithm compatibility
        if self.public_key.algorithm != self.signature.algorithm {
            return Err(Error::InvalidState(format!(
                "Algorithm mismatch: key is {:?}, signature is {:?}",
                self.public_key.algorithm, self.signature.algorithm
            )));
        }

        // Check expiration
        if !self.ignore_expired && self.signature.is_expired() {
            return Err(Error::InvalidState("Signature has expired".to_string()));
        }

        // Check key ID match if both present
        if let (Some(key_key_id), Some(sig_key_id)) =
            (&self.public_key.key_id, &self.signature.key_id)
            && key_key_id != sig_key_id
        {
            return Err(Error::InvalidState("Key ID mismatch".to_string()));
        }

        Ok(())
    }
}

/// Trait for signature verification implementations.
pub trait SignatureVerifier: Send + Sync {
    /// Verify a signature against the given data.
    fn verify(&self, data: &[u8], config: &SignatureConfig) -> Result<bool>;

    /// Get the supported algorithm.
    fn algorithm(&self) -> SignatureAlgorithm;
}

/// Mock signature verifier for testing purposes.
pub struct MockVerifier {
    algorithm: SignatureAlgorithm,
    should_verify: bool,
}

impl MockVerifier {
    /// Create a new mock verifier.
    pub fn new(algorithm: SignatureAlgorithm, should_verify: bool) -> Self {
        Self {
            algorithm,
            should_verify,
        }
    }

    /// Set whether verification should succeed.
    pub fn set_should_verify(&mut self, should_verify: bool) {
        self.should_verify = should_verify;
    }
}

impl SignatureVerifier for MockVerifier {
    fn verify(&self, _data: &[u8], config: &SignatureConfig) -> Result<bool> {
        config.validate()?;
        Ok(self.should_verify)
    }

    fn algorithm(&self) -> SignatureAlgorithm {
        self.algorithm
    }
}

/// Signature verification manager.
pub struct SignatureManager {
    verifiers: HashMap<SignatureAlgorithm, Box<dyn SignatureVerifier>>,
}

impl SignatureManager {
    /// Create a new signature manager.
    pub fn new() -> Self {
        let mut manager = Self {
            verifiers: HashMap::new(),
        };

        // Add mock verifiers for all algorithms
        manager.add_mock_verifiers();
        manager
    }

    /// Add a mock verifier for each algorithm.
    fn add_mock_verifiers(&mut self) {
        use SignatureAlgorithm::*;

        // Add mock verifiers that always return true for testing
        self.verifiers
            .insert(RsaPkcs1v15, Box::new(MockVerifier::new(RsaPkcs1v15, true)));
        self.verifiers
            .insert(RsaPss, Box::new(MockVerifier::new(RsaPss, true)));
        self.verifiers
            .insert(Ecdsa, Box::new(MockVerifier::new(Ecdsa, true)));
        self.verifiers
            .insert(EdDsa, Box::new(MockVerifier::new(EdDsa, true)));
        self.verifiers
            .insert(Dsa, Box::new(MockVerifier::new(Dsa, true)));
    }

    /// Add a custom verifier for an algorithm.
    pub fn add_verifier(&mut self, verifier: Box<dyn SignatureVerifier>) {
        self.verifiers.insert(verifier.algorithm(), verifier);
    }

    /// Verify a signature.
    pub fn verify(&self, data: &[u8], config: &SignatureConfig) -> Result<bool> {
        if let Some(verifier) = self.verifiers.get(&config.signature.algorithm) {
            verifier.verify(data, config)
        } else {
            Err(Error::InvalidState(format!(
                "No verifier available for algorithm {:?}",
                config.signature.algorithm
            )))
        }
    }

    /// Check if an algorithm is supported.
    pub fn supports_algorithm(&self, algorithm: SignatureAlgorithm) -> bool {
        self.verifiers.contains_key(&algorithm)
    }

    /// Get all supported algorithms.
    pub fn supported_algorithms(&self) -> Vec<SignatureAlgorithm> {
        self.verifiers.keys().copied().collect()
    }
}

impl Default for SignatureManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience function to verify a signature.
pub fn verify_signature(data: &[u8], config: &SignatureConfig) -> Result<bool> {
    let manager = SignatureManager::new();
    manager.verify(data, config)
}

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

    #[test]
    fn test_signature_algorithm() {
        assert_eq!(SignatureAlgorithm::RsaPkcs1v15.as_str(), "rsa-pkcs1v15");
        assert_eq!(SignatureAlgorithm::RsaPkcs1v15.min_key_size(), 2048);
        assert!(SignatureAlgorithm::RsaPkcs1v15.is_secure());
        assert!(!SignatureAlgorithm::Dsa.is_secure());
    }

    #[test]
    fn test_signature_format() {
        assert_eq!(SignatureFormat::Base64.as_str(), "base64");
    }

    #[test]
    fn test_public_key_format() {
        assert_eq!(PublicKeyFormat::Pem.as_str(), "pem");
    }

    #[test]
    fn test_public_key_creation() {
        let key = PublicKey::new(
            SignatureAlgorithm::RsaPkcs1v15,
            PublicKeyFormat::Pem,
            b"mock_key_data".to_vec(),
        )
        .with_key_id("test-key".to_string());

        assert_eq!(key.algorithm, SignatureAlgorithm::RsaPkcs1v15);
        assert_eq!(key.format, PublicKeyFormat::Pem);
        assert_eq!(key.data, b"mock_key_data");
        assert_eq!(key.key_id, Some("test-key".to_string()));
    }

    #[test]
    fn test_public_key_validation() {
        let key = PublicKey::new(
            SignatureAlgorithm::RsaPkcs1v15,
            PublicKeyFormat::Pem,
            vec![0u8; 256], // 2048 bits
        );
        assert!(key.validate().is_ok());

        // Test insecure algorithm
        let key = PublicKey::new(
            SignatureAlgorithm::Dsa,
            PublicKeyFormat::Pem,
            vec![0u8; 256],
        );
        assert!(key.validate().is_err());

        // Test key too small
        let key = PublicKey::new(
            SignatureAlgorithm::RsaPkcs1v15,
            PublicKeyFormat::Pem,
            vec![0u8; 128], // 1024 bits
        );
        assert!(key.validate().is_err());
    }

    #[test]
    fn test_signature_creation() {
        let sig = Signature::new(
            SignatureAlgorithm::RsaPkcs1v15,
            SignatureFormat::Base64,
            b"mock_signature".to_vec(),
        )
        .with_key_id("test-key".to_string())
        .with_created_at(1234567890)
        .with_expires_at(1234567990);

        assert_eq!(sig.algorithm, SignatureAlgorithm::RsaPkcs1v15);
        assert_eq!(sig.format, SignatureFormat::Base64);
        assert_eq!(sig.data, b"mock_signature");
        assert_eq!(sig.key_id, Some("test-key".to_string()));
        assert_eq!(sig.created_at, Some(1234567890));
        assert_eq!(sig.expires_at, Some(1234567990));
    }

    #[test]
    fn test_signature_decode_data() {
        let sig = Signature::new(
            SignatureAlgorithm::RsaPkcs1v15,
            SignatureFormat::Raw,
            b"raw_data".to_vec(),
        );
        assert_eq!(sig.decode_data().unwrap(), b"raw_data");

        let sig = Signature::new(
            SignatureAlgorithm::RsaPkcs1v15,
            SignatureFormat::Hex,
            b"48656c6c6f".to_vec(), // "Hello" in hex
        );
        assert_eq!(sig.decode_data().unwrap(), b"Hello");
    }

    #[test]
    fn test_signature_config() {
        let key = PublicKey::new(
            SignatureAlgorithm::RsaPkcs1v15,
            PublicKeyFormat::Pem,
            vec![0u8; 256],
        );
        let sig = Signature::new(
            SignatureAlgorithm::RsaPkcs1v15,
            SignatureFormat::Raw,
            b"signature".to_vec(),
        );

        let config = SignatureConfig::new(key, sig)
            .with_signed_data(b"data".to_vec())
            .with_ignore_expired(true);

        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_signature_config_algorithm_mismatch() {
        let key = PublicKey::new(
            SignatureAlgorithm::RsaPkcs1v15,
            PublicKeyFormat::Pem,
            vec![0u8; 256],
        );
        let sig = Signature::new(
            SignatureAlgorithm::Ecdsa,
            SignatureFormat::Raw,
            b"signature".to_vec(),
        );

        let config = SignatureConfig::new(key, sig);
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_mock_verifier() {
        let verifier = MockVerifier::new(SignatureAlgorithm::RsaPkcs1v15, true);
        assert_eq!(verifier.algorithm(), SignatureAlgorithm::RsaPkcs1v15);

        let key = PublicKey::new(
            SignatureAlgorithm::RsaPkcs1v15,
            PublicKeyFormat::Pem,
            vec![0u8; 256],
        );
        let sig = Signature::new(
            SignatureAlgorithm::RsaPkcs1v15,
            SignatureFormat::Raw,
            b"signature".to_vec(),
        );
        let config = SignatureConfig::new(key, sig);

        assert!(verifier.verify(b"data", &config).unwrap());

        let verifier = MockVerifier::new(SignatureAlgorithm::RsaPkcs1v15, false);
        assert!(!verifier.verify(b"data", &config).unwrap());
    }

    #[test]
    fn test_signature_manager() {
        let manager = SignatureManager::new();

        assert!(manager.supports_algorithm(SignatureAlgorithm::RsaPkcs1v15));
        assert!(manager.supports_algorithm(SignatureAlgorithm::Ecdsa));

        let algorithms = manager.supported_algorithms();
        assert!(algorithms.contains(&SignatureAlgorithm::RsaPkcs1v15));
        assert!(algorithms.contains(&SignatureAlgorithm::Ecdsa));

        let key = PublicKey::new(
            SignatureAlgorithm::RsaPkcs1v15,
            PublicKeyFormat::Pem,
            vec![0u8; 256],
        );
        let sig = Signature::new(
            SignatureAlgorithm::RsaPkcs1v15,
            SignatureFormat::Raw,
            b"signature".to_vec(),
        );
        let config = SignatureConfig::new(key, sig);

        // Mock verifier returns true
        assert!(manager.verify(b"data", &config).unwrap());
    }

    #[test]
    fn test_convenience_function() {
        let key = PublicKey::new(
            SignatureAlgorithm::RsaPkcs1v15,
            PublicKeyFormat::Pem,
            vec![0u8; 256],
        );
        let sig = Signature::new(
            SignatureAlgorithm::RsaPkcs1v15,
            SignatureFormat::Raw,
            b"signature".to_vec(),
        );
        let config = SignatureConfig::new(key, sig);

        // Mock verifier returns true
        assert!(verify_signature(b"data", &config).unwrap());
    }
}