| 1 |
|
| 2 |
|
| 3 |
use crate::{ |
| 4 |
Error, Result, |
| 5 |
v4::metric::{Metric, MetricType}, |
| 6 |
}; |
| 7 |
use alloc::borrow::ToOwned; |
| 8 |
use core::{fmt, str::FromStr}; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 23 |
pub enum AttackVector { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
Physical, |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
he |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
Local, |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Adjacent, |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
eing |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
Network, |
| 70 |
} |
| 71 |
|
| 72 |
impl Default for AttackVector { |
| 73 |
fn default() -> Self { |
| 74 |
Self::Network |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl Metric for AttackVector { |
| 79 |
const TYPE: MetricType = MetricType::AV; |
| 80 |
|
| 81 |
fn as_str(self) -> &'static str { |
| 82 |
match self { |
| 83 |
AttackVector::Network => "N", |
| 84 |
AttackVector::Adjacent => "A", |
| 85 |
AttackVector::Local => "L", |
| 86 |
AttackVector::Physical => "P", |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
impl fmt::Display for AttackVector { |
| 92 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 93 |
write!(f, "{}:{}", Self::name(), self.as_str()) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
impl FromStr for AttackVector { |
| 98 |
type Err = Error; |
| 99 |
|
| 100 |
fn from_str(s: &str) -> Result<Self> { |
| 101 |
match s { |
| 102 |
"N" => Ok(AttackVector::Network), |
| 103 |
"A" => Ok(AttackVector::Adjacent), |
| 104 |
"L" => Ok(AttackVector::Local), |
| 105 |
"P" => Ok(AttackVector::Physical), |
| 106 |
_ => Err(Error::InvalidMetricV4 { |
| 107 |
metric_type: Self::TYPE, |
| 108 |
value: s.to_owned(), |
| 109 |
}), |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
#[cfg(feature = "std")] |
| 115 |
pub(crate) mod merge { |
| 116 |
use super::*; |
| 117 |
use crate::{ |
| 118 |
Error, |
| 119 |
v4::{ |
| 120 |
MetricType, |
| 121 |
metric::{MetricLevel, environmental::ModifiedAttackVector}, |
| 122 |
}, |
| 123 |
}; |
| 124 |
use alloc::borrow::ToOwned; |
| 125 |
use core::str::FromStr; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 131 |
pub(crate) enum MergedAttackVector { |
| 132 |
Physical, |
| 133 |
Local, |
| 134 |
Adjacent, |
| 135 |
Network, |
| 136 |
} |
| 137 |
|
| 138 |
impl Default for MergedAttackVector { |
| 139 |
fn default() -> Self { |
| 140 |
Self::Network |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
impl FromStr for MergedAttackVector { |
| 145 |
type Err = Error; |
| 146 |
|
| 147 |
fn from_str(s: &str) -> Result<Self> { |
| 148 |
match s { |
| 149 |
"P" => Ok(MergedAttackVector::Physical), |
| 150 |
"L" => Ok(MergedAttackVector::Local), |
| 151 |
"A" => Ok(MergedAttackVector::Adjacent), |
| 152 |
"N" => Ok(MergedAttackVector::Network), |
| 153 |
_ => Err(Error::InvalidMetricV4 { |
| 154 |
metric_type: MetricType::AV, |
| 155 |
value: s.to_owned(), |
| 156 |
}), |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
impl MetricLevel for MergedAttackVector { |
| 162 |
fn level(self) -> f64 { |
| 163 |
|
| 164 |
match self { |
| 165 |
Self::Physical => 0.3, |
| 166 |
Self::Local => 0.2, |
| 167 |
Self::Adjacent => 0.1, |
| 168 |
Self::Network => 0.0, |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
impl AttackVector { |
| 174 |
pub(crate) fn merge(self, value: Option<ModifiedAttackVector>) -> MergedAttackVector { |
| 175 |
match value { |
| 176 |
Some(ModifiedAttackVector::NotDefined) | None => match self { |
| 177 |
Self::Network => MergedAttackVector::Network, |
| 178 |
Self::Adjacent => MergedAttackVector::Adjacent, |
| 179 |
Self::Local => MergedAttackVector::Local, |
| 180 |
Self::Physical => MergedAttackVector::Physical, |
| 181 |
}, |
| 182 |
Some(ModifiedAttackVector::Network) => MergedAttackVector::Network, |
| 183 |
Some(ModifiedAttackVector::Adjacent) => MergedAttackVector::Adjacent, |
| 184 |
Some(ModifiedAttackVector::Local) => MergedAttackVector::Local, |
| 185 |
Some(ModifiedAttackVector::Physical) => MergedAttackVector::Physical, |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |