netviper-talos 0.2.4

A Rust-based secure licensing system.
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
//! Secure cached validation state for offline/air-gapped systems.
//!
//! This module provides encrypted, hardware-bound storage for license validation
//! state that can be used for offline validation during grace periods.
//!
//! ## Security Model
//!
//! The cached validation data is protected by:
//!
//! 1. **AES-256-GCM encryption** - Data is encrypted at rest
//! 2. **Hardware binding** - Encryption key is derived from hardware fingerprint
//! 3. **Tamper detection** - GCM authentication tag prevents modification
//! 4. **Server authority** - Grace period comes from server, cannot be forged
//!
//! A user cannot:
//! - Read the cache contents without the hardware-bound key
//! - Modify the cache (authentication tag would fail)
//! - Copy the cache to another machine (different hardware = different key)
//! - Extend the grace period (server-provided, stored encrypted)

use crate::client::storage::{clear_from_storage, load_from_storage, save_to_storage, StorageKey};
use crate::encryption::{decrypt_from_base64, encrypt_to_base64, KEY_SIZE};
use crate::errors::{LicenseError, LicenseResult};
use crate::hardware::get_hardware_id;

use chrono::{DateTime, Utc};
use ring::digest::{digest, SHA256};
use serde::{Deserialize, Serialize};

/// Cached validation state for offline use.
///
/// This struct stores the essential license validation data that can be used
/// to validate a license offline during a grace period.
///
/// **Security Note:** All fields are server-provided and stored encrypted.
/// The client cannot forge or modify this data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedValidation {
    /// The license key this cache belongs to
    pub license_key: String,

    /// Hardware ID this cache is bound to (for verification)
    pub hardware_id: String,

    /// List of features enabled for this license
    pub features: Vec<String>,

    /// License tier name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tier: Option<String>,

    /// License expiration date (ISO 8601)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,

    /// When the offline grace period ends (ISO 8601)
    ///
    /// After this time, the license must be validated online.
    /// This value comes from the server and cannot be modified.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub grace_period_ends_at: Option<String>,

    /// When this cache was last updated from the server (ISO 8601)
    pub validated_at: String,
}

impl CachedValidation {
    /// Create a new cached validation from server response data.
    pub fn new(
        license_key: String,
        hardware_id: String,
        features: Vec<String>,
        tier: Option<String>,
        expires_at: Option<String>,
        grace_period_ends_at: Option<String>,
    ) -> Self {
        Self {
            license_key,
            hardware_id,
            features,
            tier,
            expires_at,
            grace_period_ends_at,
            validated_at: Utc::now().to_rfc3339(),
        }
    }

    /// Check if this cache is still valid for offline use.
    ///
    /// Returns `true` if:
    /// - The cache has a grace period that hasn't expired yet, OR
    /// - The cache has no grace period (normal online license)
    ///
    /// Returns `false` if:
    /// - The grace period has expired (must go online)
    pub fn is_valid_for_offline(&self) -> bool {
        match &self.grace_period_ends_at {
            Some(ends_at) => {
                // Parse the grace period end time
                match DateTime::parse_from_rfc3339(ends_at) {
                    Ok(end_time) => Utc::now() < end_time.with_timezone(&Utc),
                    // If we can't parse it, assume invalid (fail safe)
                    Err(_) => false,
                }
            }
            // No grace period means this is a normal license, not air-gapped
            // For safety, require online validation
            None => false,
        }
    }

    /// Check if the license itself has expired (separate from grace period).
    pub fn is_license_expired(&self) -> bool {
        match &self.expires_at {
            Some(expires) => match DateTime::parse_from_rfc3339(expires) {
                Ok(exp_time) => Utc::now() >= exp_time.with_timezone(&Utc),
                Err(_) => false, // Can't parse, assume not expired
            },
            None => false, // No expiration = never expires
        }
    }

    /// Get time remaining until grace period ends.
    ///
    /// Returns `None` if no grace period or if it has already expired.
    pub fn grace_period_remaining(&self) -> Option<chrono::Duration> {
        self.grace_period_ends_at.as_ref().and_then(|ends_at| {
            DateTime::parse_from_rfc3339(ends_at)
                .ok()
                .and_then(|end_time| {
                    let remaining = end_time.with_timezone(&Utc) - Utc::now();
                    if remaining > chrono::Duration::zero() {
                        Some(remaining)
                    } else {
                        None
                    }
                })
        })
    }

    /// Check if this cache belongs to the current hardware.
    pub fn matches_hardware(&self) -> bool {
        self.hardware_id == get_hardware_id()
    }

    /// Check if a specific feature is enabled.
    pub fn has_feature(&self, feature: &str) -> bool {
        self.features.iter().any(|f| f == feature)
    }
}

// === Storage Functions ===

/// Derive a 256-bit storage key from the hardware ID.
///
/// This key is:
/// - Unique to this device (hardware-bound)
/// - Never sent to the server
/// - Used only for local at-rest encryption
fn derive_cache_storage_key() -> [u8; KEY_SIZE] {
    let hw_id = get_hardware_id();
    // Add a salt to differentiate from license storage key
    let salted = format!("talos_cache_v1:{}", hw_id);
    let hash = digest(&SHA256, salted.as_bytes());

    let mut key = [0u8; KEY_SIZE];
    key.copy_from_slice(hash.as_ref());
    key
}

/// Save cached validation to secure storage.
///
/// The cache is:
/// - Serialized to JSON
/// - Encrypted with AES-256-GCM using a hardware-bound key
/// - Stored in OS keyring (or app data directory as fallback)
pub async fn save_cache_to_disk(cache: &CachedValidation) -> LicenseResult<()> {
    let key = derive_cache_storage_key();

    let json_bytes = serde_json::to_vec(cache)
        .map_err(|e| LicenseError::EncryptionError(format!("Failed to serialize cache: {e}")))?;

    let encrypted_b64 = encrypt_to_base64(&json_bytes, &key)?;

    save_to_storage(StorageKey::Cache, &encrypted_b64).await
}

/// Load cached validation from secure storage.
///
/// Checks storage locations in order:
/// 1. OS keyring
/// 2. App data directory file
/// 3. Legacy CWD file (with automatic migration)
///
/// Returns an error if:
/// - No cache is found in any location
/// - Decryption fails (wrong key, tampered data)
/// - Deserialization fails
/// - Cache doesn't match current hardware
pub async fn load_cache_from_disk() -> LicenseResult<CachedValidation> {
    let encrypted_b64 = load_from_storage(StorageKey::Cache).await?;

    let key = derive_cache_storage_key();

    let decrypted_bytes = decrypt_from_base64(encrypted_b64.trim(), &key)?;

    let cache: CachedValidation = serde_json::from_slice(&decrypted_bytes)
        .map_err(|e| LicenseError::DecryptionError(format!("Failed to deserialize cache: {e}")))?;

    // Verify the cache belongs to this hardware
    if !cache.matches_hardware() {
        return Err(LicenseError::InvalidLicense(
            "Cache does not match current hardware.".to_string(),
        ));
    }

    Ok(cache)
}

/// Delete cached validation from all storage locations.
///
/// Clears from keyring, app data directory, and legacy CWD location.
pub async fn clear_cache_from_disk() -> LicenseResult<()> {
    clear_from_storage(StorageKey::Cache).await
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Duration;
    use serial_test::serial;
    use std::io::ErrorKind;
    use std::path::Path;
    use tokio::fs;
    use tokio::test as tokio_test;

    // Test-specific file paths for encryption layer tests
    const TEST_CACHE_FILE_1: &str = "talos_cache_test_roundtrip.enc";
    const TEST_CACHE_FILE_2: &str = "talos_cache_test_tamper.enc";

    async fn cleanup_test_file(path: &str) {
        let _ = fs::remove_file(Path::new(path)).await;
    }

    // Helper to clear cache from storage
    async fn cleanup() {
        let _ = clear_cache_from_disk().await;
    }

    // Helper to save cache to a specific test file (for encryption layer tests)
    async fn save_cache_to_test_file(cache: &CachedValidation, path: &str) -> LicenseResult<()> {
        let key = derive_cache_storage_key();

        let json_bytes = serde_json::to_vec(cache).map_err(|e| {
            LicenseError::EncryptionError(format!("Failed to serialize cache: {e}"))
        })?;

        let encrypted_b64 = encrypt_to_base64(&json_bytes, &key)?;

        fs::write(Path::new(path), encrypted_b64).await?;

        Ok(())
    }

    // Helper to load cache from a specific test file (for encryption layer tests)
    async fn load_cache_from_test_file(path: &str) -> LicenseResult<CachedValidation> {
        let encrypted_b64 = match fs::read_to_string(Path::new(path)).await {
            Ok(s) => s,
            Err(e) if e.kind() == ErrorKind::NotFound => {
                return Err(LicenseError::InvalidLicense(
                    "No cached validation found.".to_string(),
                ));
            }
            Err(e) => return Err(LicenseError::StorageError(e)),
        };

        let key = derive_cache_storage_key();

        let decrypted_bytes = decrypt_from_base64(encrypted_b64.trim(), &key)?;

        let cache: CachedValidation = serde_json::from_slice(&decrypted_bytes).map_err(|e| {
            LicenseError::DecryptionError(format!("Failed to deserialize cache: {e}"))
        })?;

        // Verify the cache belongs to this hardware
        if !cache.matches_hardware() {
            return Err(LicenseError::InvalidLicense(
                "Cache does not match current hardware.".to_string(),
            ));
        }

        Ok(cache)
    }

    fn create_test_cache(grace_period_hours: Option<i64>) -> CachedValidation {
        let grace_period_ends_at =
            grace_period_hours.map(|hours| (Utc::now() + Duration::hours(hours)).to_rfc3339());

        CachedValidation {
            license_key: "TEST-XXXX-XXXX-XXXX".to_string(),
            hardware_id: get_hardware_id(),
            features: vec!["feature_a".to_string(), "feature_b".to_string()],
            tier: Some("pro".to_string()),
            expires_at: Some((Utc::now() + Duration::days(365)).to_rfc3339()),
            grace_period_ends_at,
            validated_at: Utc::now().to_rfc3339(),
        }
    }

    #[tokio_test]
    async fn round_trip_cache_encrypt_decrypt() {
        cleanup_test_file(TEST_CACHE_FILE_1).await;

        let cache = create_test_cache(Some(24)); // 24 hours grace period

        save_cache_to_test_file(&cache, TEST_CACHE_FILE_1)
            .await
            .expect("save should succeed");

        let loaded = load_cache_from_test_file(TEST_CACHE_FILE_1)
            .await
            .expect("load should succeed");

        assert_eq!(loaded.license_key, cache.license_key);
        assert_eq!(loaded.hardware_id, cache.hardware_id);
        assert_eq!(loaded.features, cache.features);
        assert_eq!(loaded.tier, cache.tier);
        assert_eq!(loaded.grace_period_ends_at, cache.grace_period_ends_at);

        cleanup_test_file(TEST_CACHE_FILE_1).await;
    }

    #[tokio_test]
    #[serial]
    async fn missing_cache_returns_error() {
        cleanup().await;

        let result = load_cache_from_disk().await;
        assert!(matches!(result, Err(LicenseError::InvalidLicense(_))));
    }

    #[tokio_test]
    #[serial]
    async fn storage_api_round_trip() {
        cleanup().await;

        let cache = create_test_cache(Some(24));

        save_cache_to_disk(&cache)
            .await
            .expect("save should succeed");

        let loaded = load_cache_from_disk().await.expect("load should succeed");

        assert_eq!(loaded.license_key, cache.license_key);
        assert_eq!(loaded.hardware_id, cache.hardware_id);
        assert_eq!(loaded.features, cache.features);
        assert_eq!(loaded.tier, cache.tier);

        cleanup().await;
    }

    #[test]
    fn cache_with_valid_grace_period() {
        let cache = create_test_cache(Some(24)); // 24 hours in future
        assert!(cache.is_valid_for_offline());
        assert!(cache.grace_period_remaining().is_some());
    }

    #[test]
    fn cache_with_expired_grace_period() {
        let mut cache = create_test_cache(None);
        // Set grace period to 1 hour in the past
        cache.grace_period_ends_at = Some((Utc::now() - Duration::hours(1)).to_rfc3339());

        assert!(!cache.is_valid_for_offline());
        assert!(cache.grace_period_remaining().is_none());
    }

    #[test]
    fn cache_without_grace_period() {
        let cache = create_test_cache(None);
        // No grace period = not valid for offline (fail safe)
        assert!(!cache.is_valid_for_offline());
    }

    #[test]
    fn cache_license_expired() {
        let mut cache = create_test_cache(Some(24));
        cache.expires_at = Some((Utc::now() - Duration::days(1)).to_rfc3339());

        assert!(cache.is_license_expired());
    }

    #[test]
    fn cache_license_not_expired() {
        let cache = create_test_cache(Some(24));
        assert!(!cache.is_license_expired());
    }

    #[test]
    fn cache_has_feature() {
        let cache = create_test_cache(Some(24));
        assert!(cache.has_feature("feature_a"));
        assert!(cache.has_feature("feature_b"));
        assert!(!cache.has_feature("feature_c"));
    }

    #[test]
    fn cache_matches_hardware() {
        let cache = create_test_cache(Some(24));
        assert!(cache.matches_hardware());

        let mut wrong_hw_cache = cache.clone();
        wrong_hw_cache.hardware_id = "wrong-hardware-id".to_string();
        assert!(!wrong_hw_cache.matches_hardware());
    }

    #[tokio_test]
    async fn tampered_cache_fails_to_load() {
        cleanup_test_file(TEST_CACHE_FILE_2).await;

        let cache = create_test_cache(Some(24));
        save_cache_to_test_file(&cache, TEST_CACHE_FILE_2)
            .await
            .expect("save should succeed");

        // Read the encrypted file
        let encrypted = fs::read_to_string(Path::new(TEST_CACHE_FILE_2))
            .await
            .expect("read should succeed");

        // Tamper with the data by modifying a character
        let mut tampered = encrypted.clone();
        if let Some(c) = tampered.pop() {
            // Change the last character
            let new_char = if c == 'A' { 'B' } else { 'A' };
            tampered.push(new_char);
        }

        // Write tampered data back
        fs::write(Path::new(TEST_CACHE_FILE_2), tampered)
            .await
            .expect("write should succeed");

        // Try to load - should fail due to authentication tag mismatch
        let result = load_cache_from_test_file(TEST_CACHE_FILE_2).await;
        assert!(result.is_err());

        cleanup_test_file(TEST_CACHE_FILE_2).await;
    }
}