-
Notifications
You must be signed in to change notification settings - Fork 901
/
weak_cryptographic_key.rs
165 lines (152 loc) · 5.78 KB
/
weak_cryptographic_key.rs
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
use std::fmt::{Display, Formatter};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Constant, Expr, ExprAttribute, ExprCall};
use ruff_text_size::{Ranged, TextRange};
use crate::checkers::ast::Checker;
#[derive(Debug, PartialEq, Eq)]
enum CryptographicKey {
Dsa { key_size: u16 },
Ec { algorithm: String },
Rsa { key_size: u16 },
}
impl CryptographicKey {
const fn minimum_key_size(&self) -> u16 {
match self {
Self::Dsa { .. } | Self::Rsa { .. } => 2048,
Self::Ec { .. } => 224,
}
}
fn is_vulnerable(&self) -> bool {
match self {
Self::Dsa { key_size } | Self::Rsa { key_size } => key_size < &self.minimum_key_size(),
Self::Ec { algorithm } => {
matches!(algorithm.as_str(), "SECP192R1" | "SECT163K1" | "SECT163R2")
}
}
}
}
impl Display for CryptographicKey {
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
match self {
CryptographicKey::Dsa { .. } => fmt.write_str("DSA"),
CryptographicKey::Ec { .. } => fmt.write_str("EC"),
CryptographicKey::Rsa { .. } => fmt.write_str("RSA"),
}
}
}
/// ## What it does
/// Checks for uses of `cryptographic key lengths known to be vulnerable.
///
/// ## Why is this bad?
/// Small key lengths can easily be breakable, as computational power
/// increases. For DSA and RSA keys, it is recommended to use key lengths equal
/// or higher to 2048 bits. For EC, it is recommended to use curves higher than
/// 224 bits.
///
/// ## Example
/// ```python
/// from cryptography.hazmat.primitives.asymmetric import dsa, ec
///
/// dsa.generate_private_key(key_size=512)
/// ec.generate_private_key(curve=ec.SECT163K1)
/// ```
///
/// Use instead:
/// ```python
/// from cryptography.hazmat.primitives.asymmetric import dsa, ec
///
/// dsa.generate_private_key(key_size=4096)
/// ec.generate_private_key(curve=ec.SECP384R1)
/// ```
///
/// ## References
/// - [CSRC: Transitioning the Use of Cryptographic Algorithms and Key Lengths](https://csrc.nist.gov/pubs/sp/800/131/a/r2/final)
#[violation]
pub struct WeakCryptographicKey {
cryptographic_key: CryptographicKey,
}
impl Violation for WeakCryptographicKey {
#[derive_message_formats]
fn message(&self) -> String {
let WeakCryptographicKey { cryptographic_key } = self;
let minimum_key_size = cryptographic_key.minimum_key_size();
format!(
"{cryptographic_key} key sizes below {minimum_key_size} bits are considered breakable"
)
}
}
fn extract_int_argument(call: &ExprCall, name: &str, position: usize) -> Option<(u16, TextRange)> {
let Some(argument) = call.arguments.find_argument(name, position) else {
return None;
};
let Expr::Constant(ast::ExprConstant {
value: Constant::Int(i),
..
}) = argument
else {
return None;
};
Some((i.as_u16()?, argument.range()))
}
fn extract_cryptographic_key(
checker: &mut Checker,
call: &ExprCall,
) -> Option<(CryptographicKey, TextRange)> {
return checker
.semantic()
.resolve_call_path(&call.func)
.and_then(|call_path| match call_path.as_slice() {
["cryptography", "hazmat", "primitives", "asymmetric", function, "generate_private_key"] => {
return match *function {
"dsa" => {
let Some((key_size, range)) = extract_int_argument(call, "key_size", 0) else {return None};
return Some((CryptographicKey::Dsa { key_size }, range));
},
"rsa" => {
let Some((key_size, range)) = extract_int_argument(call, "key_size", 1) else {return None};
return Some((CryptographicKey::Rsa { key_size }, range));
},
"ec" => {
let Some(argument) = call.arguments.find_argument("curve", 0) else { return None };
let Expr::Attribute(ExprAttribute { attr, value, .. }) = argument else { return None };
if checker
.semantic()
.resolve_call_path(value)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["cryptography", "hazmat", "primitives", "asymmetric", "ec"]))
{
return Some((CryptographicKey::Ec{algorithm: attr.as_str().to_string()}, argument.range()));
}
return None;
},
_ => None,
};
},
["Crypto" | "Cryptodome", "PublicKey", function, "generate"] => {
return match *function {
"DSA" => {
let Some((key_size, range)) = extract_int_argument(call, "bits", 0) else {return None};
return Some((CryptographicKey::Dsa { key_size }, range));
},
"RSA" => {
let Some((key_size, range)) = extract_int_argument(call, "bits", 0) else {return None};
return Some((CryptographicKey::Dsa { key_size }, range));
},
_ => None,
};
},
_ => None,
});
}
/// S505
pub(crate) fn weak_cryptographic_key(checker: &mut Checker, call: &ExprCall) {
let Some((cryptographic_key, range)) = extract_cryptographic_key(checker, call) else {
return;
};
if cryptographic_key.is_vulnerable() {
checker.diagnostics.push(Diagnostic::new(
WeakCryptographicKey { cryptographic_key },
range,
));
}
}