|
1 | 1 | /**
|
2 | 2 | * Provides classes modeling cryptographic algorithms, separated into strong and weak variants.
|
3 |
| - * |
4 |
| - * The classification into strong and weak are based on Wikipedia, OWASP and Google (2021). |
5 | 3 | */
|
6 | 4 |
|
7 |
| -private import codeql.concepts.internal.CryptoAlgorithmNames |
8 |
| - |
9 |
| -/** |
10 |
| - * A cryptographic algorithm. |
11 |
| - */ |
12 |
| -private newtype TCryptographicAlgorithm = |
13 |
| - MkHashingAlgorithm(string name, boolean isWeak) { |
14 |
| - isStrongHashingAlgorithm(name) and isWeak = false |
15 |
| - or |
16 |
| - isWeakHashingAlgorithm(name) and isWeak = true |
17 |
| - } or |
18 |
| - MkEncryptionAlgorithm(string name, boolean isWeak) { |
19 |
| - isStrongEncryptionAlgorithm(name) and isWeak = false |
20 |
| - or |
21 |
| - isWeakEncryptionAlgorithm(name) and isWeak = true |
22 |
| - } or |
23 |
| - MkPasswordHashingAlgorithm(string name, boolean isWeak) { |
24 |
| - isStrongPasswordHashingAlgorithm(name) and isWeak = false |
25 |
| - or |
26 |
| - isWeakPasswordHashingAlgorithm(name) and isWeak = true |
27 |
| - } |
28 |
| - |
29 |
| -/** |
30 |
| - * Gets the most specific `CryptographicAlgorithm` that matches the given `name`. |
31 |
| - * A matching algorithm is one where the name of the algorithm matches the start of name, with allowances made for different name formats. |
32 |
| - * In the case that multiple `CryptographicAlgorithm`s match the given `name`, the algorithm(s) with the longest name will be selected. This is intended to select more specific versions of algorithms when multiple versions could match - for example "SHA3_224" matches against both "SHA3" and "SHA3224", but the latter is a more precise match. |
33 |
| - */ |
34 |
| -bindingset[name] |
35 |
| -private CryptographicAlgorithm getBestAlgorithmForName(string name) { |
36 |
| - result = |
37 |
| - max(CryptographicAlgorithm algorithm | |
38 |
| - algorithm.getName() = |
39 |
| - [ |
40 |
| - name.toUpperCase(), // the full name |
41 |
| - name.toUpperCase().regexpCapture("^([\\w]+)(?:-.*)?$", 1), // the name prior to any dashes or spaces |
42 |
| - name.toUpperCase().regexpCapture("^([A-Z0-9]+)(?:(-|_).*)?$", 1) // the name prior to any dashes, spaces, or underscores |
43 |
| - ].regexpReplaceAll("[-_ ]", "") // strip dashes, underscores, and spaces |
44 |
| - | |
45 |
| - algorithm order by algorithm.getName().length() |
46 |
| - ) |
47 |
| -} |
48 |
| - |
49 |
| -/** |
50 |
| - * A cryptographic algorithm. |
51 |
| - */ |
52 |
| -abstract class CryptographicAlgorithm extends TCryptographicAlgorithm { |
53 |
| - /** Gets a textual representation of this element. */ |
54 |
| - string toString() { result = this.getName() } |
55 |
| - |
56 |
| - /** |
57 |
| - * Gets the normalized name of this algorithm (upper-case, no spaces, dashes or underscores). |
58 |
| - */ |
59 |
| - abstract string getName(); |
60 |
| - |
61 |
| - /** |
62 |
| - * Holds if the name of this algorithm is the most specific match for `name`. |
63 |
| - * This predicate matches quite liberally to account for different ways of formatting algorithm names, e.g. using dashes, underscores, or spaces as separators, including or not including block modes of operation, etc. |
64 |
| - */ |
65 |
| - bindingset[name] |
66 |
| - predicate matchesName(string name) { this = getBestAlgorithmForName(name) } |
67 |
| - |
68 |
| - /** |
69 |
| - * Holds if this algorithm is weak. |
70 |
| - */ |
71 |
| - abstract predicate isWeak(); |
72 |
| -} |
73 |
| - |
74 |
| -/** |
75 |
| - * A hashing algorithm such as `MD5` or `SHA512`. |
76 |
| - */ |
77 |
| -class HashingAlgorithm extends MkHashingAlgorithm, CryptographicAlgorithm { |
78 |
| - string name; |
79 |
| - boolean isWeak; |
80 |
| - |
81 |
| - HashingAlgorithm() { this = MkHashingAlgorithm(name, isWeak) } |
82 |
| - |
83 |
| - override string getName() { result = name } |
84 |
| - |
85 |
| - override predicate isWeak() { isWeak = true } |
86 |
| -} |
87 |
| - |
88 |
| -/** |
89 |
| - * An encryption algorithm such as `DES` or `AES512`. |
90 |
| - */ |
91 |
| -class EncryptionAlgorithm extends MkEncryptionAlgorithm, CryptographicAlgorithm { |
92 |
| - string name; |
93 |
| - boolean isWeak; |
94 |
| - |
95 |
| - EncryptionAlgorithm() { this = MkEncryptionAlgorithm(name, isWeak) } |
96 |
| - |
97 |
| - override string getName() { result = name } |
98 |
| - |
99 |
| - override predicate isWeak() { isWeak = true } |
100 |
| - |
101 |
| - /** Holds if this algorithm is a stream cipher. */ |
102 |
| - predicate isStreamCipher() { isStreamCipher(name) } |
103 |
| -} |
104 |
| - |
105 |
| -/** |
106 |
| - * A password hashing algorithm such as `PBKDF2` or `SCRYPT`. |
107 |
| - */ |
108 |
| -class PasswordHashingAlgorithm extends MkPasswordHashingAlgorithm, CryptographicAlgorithm { |
109 |
| - string name; |
110 |
| - boolean isWeak; |
111 |
| - |
112 |
| - PasswordHashingAlgorithm() { this = MkPasswordHashingAlgorithm(name, isWeak) } |
113 |
| - |
114 |
| - override string getName() { result = name } |
115 |
| - |
116 |
| - override predicate isWeak() { isWeak = true } |
117 |
| -} |
| 5 | +private import codeql.concepts.CryptoAlgorithms |
0 commit comments