Skip to main content

licenz_core/
error.rs

1//! Error types for the license system
2
3use thiserror::Error;
4
5/// Errors that can occur during license operations
6#[derive(Debug, Error)]
7pub enum LicenseError {
8    #[error("Key generation failed: {0}")]
9    KeyGenerationFailed(String),
10
11    #[error("Invalid key format: {0}")]
12    InvalidKeyFormat(String),
13
14    #[error("License signing failed: {0}")]
15    SigningFailed(String),
16
17    #[error("License verification failed: {0}")]
18    VerificationFailed(String),
19
20    #[error("License has expired (expired on {0})")]
21    LicenseExpired(String),
22
23    #[error("Hardware binding mismatch: {field}")]
24    HardwareBindingMismatch { field: String },
25
26    #[error("Invalid license format: {0}")]
27    InvalidLicenseFormat(String),
28
29    #[error("IO error: {0}")]
30    IoError(#[from] std::io::Error),
31
32    #[error("Serialization error: {0}")]
33    SerializationError(String),
34
35    #[error("License not yet valid (valid from {0})")]
36    NotYetValid(String),
37
38    #[error("Missing required field: {0}")]
39    MissingField(String),
40
41    // Anti-tamper errors
42    #[error("Clock manipulation detected: system time moved backwards by {drift_hours} hours")]
43    ClockManipulationDetected { drift_hours: i64 },
44
45    #[error("Clock drift too large: {drift_hours} hours difference from expected")]
46    ClockDriftTooLarge { drift_hours: i64 },
47
48    #[error("License state file has been tampered with")]
49    StateFileTampered,
50
51    #[error("License state file is for a different license")]
52    StateLicenseMismatch,
53
54    #[error("Activation denied: {0}")]
55    ActivationDenied(String),
56
57    #[error("Activation limit reached: {current} of {max} activations used")]
58    ActivationLimitReached { max: u32, current: u32 },
59
60    #[error("Hardware fingerprint mismatch: only {percentage:.1}% match (minimum 70% required)")]
61    HardwareFingerprintMismatch { percentage: f32 },
62
63    #[error("Insecure key permissions on {path}: mode {mode}. {suggestion}")]
64    InsecureKeyPermissions {
65        path: std::path::PathBuf,
66        mode: String,
67        suggestion: String,
68    },
69
70    #[error("Validation error: {0}")]
71    Validation(String),
72
73    // Sneakernet (offline activation) errors
74    #[error("Invalid activation request: {0}")]
75    InvalidActivationRequest(String),
76
77    #[error("Invalid activation response: {0}")]
78    InvalidActivationResponse(String),
79
80    #[error("Activation request checksum mismatch - data may be corrupted")]
81    ActivationRequestCorrupted,
82
83    #[error("Activation response signature mismatch - data may be corrupted")]
84    ActivationResponseCorrupted,
85
86    #[error(
87        "Activation request/response version mismatch: request v{request}, response v{response}"
88    )]
89    ActivationVersionMismatch { request: u8, response: u8 },
90
91    #[error("Activation response does not match request ID")]
92    ActivationRequestMismatch,
93
94    #[error("Activation response has expired")]
95    ActivationResponseExpired,
96
97    // Admin unlock errors
98    #[error("Invalid unlock response code: {0}")]
99    InvalidResponseCode(String),
100
101    #[error("Unlock response has expired")]
102    UnlockResponseExpired,
103
104    #[error("Machine is locked: {0}")]
105    MachineLocked(String),
106}
107
108/// Result type alias for license operations
109pub type Result<T> = std::result::Result<T, LicenseError>;