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
//! CVSS v2 base metrics

use crate::common::{AsStr, NumValue, ParseError};
use std::str;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub enum AccessVector {
    Local,
    AdjacentNetwork,
    Network,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub enum AccessComplexity {
    High,
    Medium,
    Low,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub enum Authentication {
    Multiple,
    Single,
    None,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub enum ConfidentialityImpact {
    None,
    Partial,
    Complete,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub enum IntegrityImpact {
    None,
    Partial,
    Complete,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub enum AvailabilityImpact {
    None,
    Partial,
    Complete,
}

impl AsStr for AccessVector {
    fn as_str(&self) -> &str {
        match self {
            AccessVector::Local => "L",
            AccessVector::AdjacentNetwork => "A",
            AccessVector::Network => "N",
        }
    }
}

impl str::FromStr for AccessVector {
    type Err = ParseError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "L" => Ok(AccessVector::Local),
            "A" => Ok(AccessVector::AdjacentNetwork),
            "N" => Ok(AccessVector::Network),
            _ => Err(ParseError::IncorrectValue),
        }
    }
}

impl NumValue for AccessVector {
    fn num_value(&self) -> f64 {
        match self {
            AccessVector::Local => 0.395,
            AccessVector::AdjacentNetwork => 0.646,
            AccessVector::Network => 1.0,
        }
    }
}

impl AsStr for AccessComplexity {
    fn as_str(&self) -> &str {
        match self {
            AccessComplexity::High => "H",
            AccessComplexity::Medium => "M",
            AccessComplexity::Low => "L",
        }
    }
}

impl str::FromStr for AccessComplexity {
    type Err = ParseError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "H" => Ok(AccessComplexity::High),
            "M" => Ok(AccessComplexity::Medium),
            "L" => Ok(AccessComplexity::Low),
            _ => Err(ParseError::IncorrectValue),
        }
    }
}

impl NumValue for AccessComplexity {
    fn num_value(&self) -> f64 {
        match self {
            AccessComplexity::High => 0.35,
            AccessComplexity::Medium => 0.61,
            AccessComplexity::Low => 0.71,
        }
    }
}

impl AsStr for Authentication {
    fn as_str(&self) -> &str {
        match self {
            Authentication::Multiple => "M",
            Authentication::Single => "S",
            Authentication::None => "N",
        }
    }
}

impl str::FromStr for Authentication {
    type Err = ParseError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "M" => Ok(Authentication::Multiple),
            "S" => Ok(Authentication::Single),
            "N" => Ok(Authentication::None),
            _ => Err(ParseError::IncorrectValue),
        }
    }
}

impl NumValue for Authentication {
    fn num_value(&self) -> f64 {
        match self {
            Authentication::Multiple => 0.45,
            Authentication::Single => 0.56,
            Authentication::None => 0.704,
        }
    }
}

impl AsStr for ConfidentialityImpact {
    fn as_str(&self) -> &str {
        match self {
            ConfidentialityImpact::None => "N",
            ConfidentialityImpact::Partial => "P",
            ConfidentialityImpact::Complete => "C",
        }
    }
}

impl str::FromStr for ConfidentialityImpact {
    type Err = ParseError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "N" => Ok(ConfidentialityImpact::None),
            "P" => Ok(ConfidentialityImpact::Partial),
            "C" => Ok(ConfidentialityImpact::Complete),
            _ => Err(ParseError::IncorrectValue),
        }
    }
}

impl NumValue for ConfidentialityImpact {
    fn num_value(&self) -> f64 {
        match self {
            ConfidentialityImpact::None => 0.0,
            ConfidentialityImpact::Partial => 0.275,
            ConfidentialityImpact::Complete => 0.660,
        }
    }
}

impl AsStr for IntegrityImpact {
    fn as_str(&self) -> &str {
        match self {
            IntegrityImpact::None => "N",
            IntegrityImpact::Partial => "P",
            IntegrityImpact::Complete => "C",
        }
    }
}

impl str::FromStr for IntegrityImpact {
    type Err = ParseError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "N" => Ok(IntegrityImpact::None),
            "P" => Ok(IntegrityImpact::Partial),
            "C" => Ok(IntegrityImpact::Complete),
            _ => Err(ParseError::IncorrectValue),
        }
    }
}

impl NumValue for IntegrityImpact {
    fn num_value(&self) -> f64 {
        match self {
            IntegrityImpact::None => 0.0,
            IntegrityImpact::Partial => 0.275,
            IntegrityImpact::Complete => 0.660,
        }
    }
}

impl AsStr for AvailabilityImpact {
    fn as_str(&self) -> &str {
        match self {
            AvailabilityImpact::None => "N",
            AvailabilityImpact::Partial => "P",
            AvailabilityImpact::Complete => "C",
        }
    }
}

impl str::FromStr for AvailabilityImpact {
    type Err = ParseError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "N" => Ok(AvailabilityImpact::None),
            "P" => Ok(AvailabilityImpact::Partial),
            "C" => Ok(AvailabilityImpact::Complete),
            _ => Err(ParseError::IncorrectValue),
        }
    }
}

impl NumValue for AvailabilityImpact {
    fn num_value(&self) -> f64 {
        match self {
            AvailabilityImpact::None => 0.0,
            AvailabilityImpact::Partial => 0.275,
            AvailabilityImpact::Complete => 0.660,
        }
    }
}