1 //! Integrity Impact (I)
2 
3 use crate::{
4     error::{Error, ErrorKind},
5     v3::Metric,
6 };
7 use std::{fmt, str::FromStr};
8 
9 /// Integrity Impact (I) - CVSS v3.1 Base Metric Group
10 ///
11 /// Described in CVSS v3.1 Specification: Section 2.3.2:
12 /// <https://www.first.org/cvss/specification-document#t6>
13 ///
14 /// > This metric measures the impact to integrity of a successfully exploited
15 /// > vulnerability. Integrity refers to the trustworthiness and veracity of
16 /// > information. The Base Score is greatest when the consequence to the
17 /// > impacted component is highest.
18 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
19 pub enum Integrity {
20     /// None (N)
21     ///
22     /// > There is no loss of integrity within the impacted component.
23     None,
24 
25     /// Low (L)
26     ///
27     /// > Modification of data is possible, but the attacker does not have
28     /// > control over the consequence of a modification, or the amount of
29     /// > modification is limited. The data modification does not have a
30     /// > direct, serious impact on the impacted component.
31     Low,
32 
33     /// High (H)
34     ///
35     /// > There is a total loss of integrity, or a complete loss of protection.
36     /// > For example, the attacker is able to modify any/all files protected
37     /// > by the impacted component. Alternatively, only some files can be
38     /// > modified, but malicious modification would present a direct, serious
39     /// > consequence to the impacted component.
40     High,
41 }
42 
43 impl Metric for Integrity {
44     const NAME: &'static str = "I";
45 
score(self) -> f6446     fn score(self) -> f64 {
47         match self {
48             Integrity::None => 0.0,
49             Integrity::Low => 0.22,
50             Integrity::High => 0.56,
51         }
52     }
53 
as_str(self) -> &'static str54     fn as_str(self) -> &'static str {
55         match self {
56             Integrity::None => "N",
57             Integrity::Low => "L",
58             Integrity::High => "H",
59         }
60     }
61 }
62 
63 impl fmt::Display for Integrity {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result64     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65         write!(f, "{}:{}", Self::NAME, self.as_str())
66     }
67 }
68 
69 impl FromStr for Integrity {
70     type Err = Error;
71 
from_str(s: &str) -> Result<Self, Error>72     fn from_str(s: &str) -> Result<Self, Error> {
73         match s {
74             "N" => Ok(Integrity::None),
75             "L" => Ok(Integrity::Low),
76             "H" => Ok(Integrity::High),
77             other => fail!(ErrorKind::Parse, "invalid I (Base): {}", other),
78         }
79     }
80 }
81