Skip to content

Commit 286c091

Browse files
authored
Merge pull request github#3837 from geoffw0/qldoc5
C++/Java: Update QLDoc and terminology in Encryption.qll
2 parents cb39525 + cf75397 commit 286c091

File tree

9 files changed

+135
-50
lines changed

9 files changed

+135
-50
lines changed

cpp/ql/src/Security/CWE/CWE-327/BrokenCryptoAlgorithm.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class InsecureCryptoSpec extends Locatable {
1818
}
1919

2020
Function getAnInsecureFunction() {
21-
result.getName().regexpMatch(algorithmBlacklistRegex()) and
21+
result.getName().regexpMatch(getInsecureAlgorithmRegex()) and
2222
exists(result.getACallToThisFunction())
2323
}
2424

@@ -33,7 +33,7 @@ class InsecureFunctionCall extends InsecureCryptoSpec, FunctionCall {
3333
}
3434

3535
Macro getAnInsecureMacro() {
36-
result.getName().regexpMatch(algorithmBlacklistRegex()) and
36+
result.getName().regexpMatch(getInsecureAlgorithmRegex()) and
3737
exists(result.getAnInvocation())
3838
}
3939

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
1-
// Common predicates relating to encryption in C and C++
1+
/**
2+
* Provides predicates relating to encryption in C and C++.
3+
*/
4+
25
import cpp
36

4-
/** A blacklist of algorithms that are known to be insecure */
5-
string algorithmBlacklist() {
7+
/**
8+
* Gets the name of an algorithm that is known to be insecure.
9+
*/
10+
string getAnInsecureAlgorithmName() {
611
result = "DES" or
712
result = "RC2" or
813
result = "RC4" or
914
result = "RC5" or
1015
result = "ARCFOUR" // a variant of RC4
1116
}
1217

13-
// these are only bad if they're being used for encryption, and it's
14-
// hard to know when that's happening
15-
string hashAlgorithmBlacklist() {
18+
/**
19+
* Gets the name of a hash algorithm that is insecure if it is being used for
20+
* encryption (but it is hard to know when that is happening).
21+
*/
22+
string getAnInsecureHashAlgorithmName() {
1623
result = "SHA1" or
1724
result = "MD5"
1825
}
1926

20-
/** A regex for matching strings that look like they contain a blacklisted algorithm */
21-
string algorithmBlacklistRegex() {
27+
/**
28+
* Gets the regular expression used for matching strings that look like they
29+
* contain an algorithm that is known to be insecure.
30+
*/
31+
string getInsecureAlgorithmRegex() {
2232
result =
2333
// algorithms usually appear in names surrounded by characters that are not
2434
// alphabetical characters in the same case. This handles the upper and lower
2535
// case cases
26-
"(^|.*[^A-Z])(" + strictconcat(algorithmBlacklist(), "|") + ")([^A-Z].*|$)" + "|" +
36+
"(^|.*[^A-Z])(" + strictconcat(getAnInsecureAlgorithmName(), "|") + ")([^A-Z].*|$)" + "|" +
2737
// for lowercase, we want to be careful to avoid being confused by camelCase
2838
// hence we require two preceding uppercase letters to be sure of a case switch,
2939
// or a preceding non-alphabetic character
30-
"(^|.*[A-Z]{2}|.*[^a-zA-Z])(" + strictconcat(algorithmBlacklist().toLowerCase(), "|") +
40+
"(^|.*[A-Z]{2}|.*[^a-zA-Z])(" + strictconcat(getAnInsecureAlgorithmName().toLowerCase(), "|") +
3141
")([^a-z].*|$)"
3242
}
3343

34-
/** A whitelist of algorithms that are known to be secure */
35-
string algorithmWhitelist() {
44+
/**
45+
* Gets the name of an algorithm that is known to be secure.
46+
*/
47+
string getASecureAlgorithmName() {
3648
result = "RSA" or
3749
result = "SHA256" or
3850
result = "CCM" or
@@ -42,17 +54,46 @@ string algorithmWhitelist() {
4254
result = "ECIES"
4355
}
4456

45-
/** A regex for matching strings that look like they contain a whitelisted algorithm */
46-
string algorithmWhitelistRegex() {
47-
// The implementation of this is a duplicate of algorithmBlacklistRegex, as it isn't
48-
// possible to have string -> string functions at the moment
57+
/**
58+
* Gets a regular expression for matching strings that look like they
59+
* contain an algorithm that is known to be secure.
60+
*/
61+
string getSecureAlgorithmRegex() {
4962
// algorithms usually appear in names surrounded by characters that are not
5063
// alphabetical characters in the same case. This handles the upper and lower
5164
// case cases
52-
result = "(^|.*[^A-Z])" + algorithmWhitelist() + "([^A-Z].*|$)"
65+
result = "(^|.*[^A-Z])" + getASecureAlgorithmName() + "([^A-Z].*|$)"
5366
or
5467
// for lowercase, we want to be careful to avoid being confused by camelCase
55-
// hence we require two preceding uppercase letters to be sure of a case switch,
56-
// or a preceding non-alphabetic character
57-
result = "(^|.*[A-Z]{2}|.*[^a-zA-Z])" + algorithmWhitelist().toLowerCase() + "([^a-z].*|$)"
68+
// hence we require two preceding uppercase letters to be sure of a case
69+
// switch, or a preceding non-alphabetic character
70+
result = "(^|.*[A-Z]{2}|.*[^a-zA-Z])" + getASecureAlgorithmName().toLowerCase() + "([^a-z].*|$)"
5871
}
72+
73+
/**
74+
* DEPRECATED: Terminology has been updated. Use `getAnInsecureAlgorithmName()`
75+
* instead.
76+
*/
77+
deprecated string algorithmBlacklist() { result = getAnInsecureAlgorithmName() }
78+
79+
/**
80+
* DEPRECATED: Terminology has been updated. Use
81+
* `getAnInsecureHashAlgorithmName()` instead.
82+
*/
83+
deprecated string hashAlgorithmBlacklist() { result = getAnInsecureHashAlgorithmName() }
84+
85+
/**
86+
* DEPRECATED: Terminology has been updated. Use `getInsecureAlgorithmRegex()` instead.
87+
*/
88+
deprecated string algorithmBlacklistRegex() { result = getInsecureAlgorithmRegex() }
89+
90+
/**
91+
* DEPRECATED: Terminology has been updated. Use `getASecureAlgorithmName()`
92+
* instead.
93+
*/
94+
deprecated string algorithmWhitelist() { result = getASecureAlgorithmName() }
95+
96+
/**
97+
* DEPRECATED: Terminology has been updated. Use `getSecureAlgorithmRegex()` instead.
98+
*/
99+
deprecated string algorithmWhitelistRegex() { result = getSecureAlgorithmRegex() }

java/ql/src/Security/CWE/CWE-327/BrokenCryptoAlgorithm.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private class ShortStringLiteral extends StringLiteral {
2121

2222
class BrokenAlgoLiteral extends ShortStringLiteral {
2323
BrokenAlgoLiteral() {
24-
getValue().regexpMatch(algorithmBlacklistRegex()) and
24+
getValue().regexpMatch(getInsecureAlgorithmRegex()) and
2525
// Exclude German and French sentences.
2626
not getValue().regexpMatch(".*\\p{IsLowercase} des \\p{IsLetter}.*")
2727
}

java/ql/src/Security/CWE/CWE-327/MaybeBrokenCryptoAlgorithm.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class InsecureAlgoLiteral extends ShortStringLiteral {
2525
// Algorithm identifiers should be at least two characters.
2626
getValue().length() > 1 and
2727
exists(string s | s = getLiteral() |
28-
not s.regexpMatch(algorithmWhitelistRegex()) and
28+
not s.regexpMatch(getSecureAlgorithmRegex()) and
2929
// Exclude results covered by another query.
30-
not s.regexpMatch(algorithmBlacklistRegex())
30+
not s.regexpMatch(getInsecureAlgorithmRegex())
3131
)
3232
}
3333
}

java/ql/src/semmle/code/java/security/Encryption.qll

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Provides predicates and classes relating to encryption in Java.
3+
*/
4+
15
import java
26

37
class SSLClass extends RefType {
@@ -85,41 +89,51 @@ private string algorithmRegex(string algorithmString) {
8589
"((^|.*[A-Z]{2}|.*[^a-zA-Z])(" + algorithmString.toLowerCase() + ")([^a-z].*|$))"
8690
}
8791

88-
/** Gets a blacklist of algorithms that are known to be insecure. */
89-
private string algorithmBlacklist() {
92+
/**
93+
* Gets the name of an algorithm that is known to be insecure.
94+
*/
95+
string getAnInsecureAlgorithmName() {
9096
result = "DES" or
9197
result = "RC2" or
9298
result = "RC4" or
9399
result = "RC5" or
94100
result = "ARCFOUR" // a variant of RC4
95101
}
96102

97-
// These are only bad if they're being used for encryption.
98-
private string hashAlgorithmBlacklist() {
103+
/**
104+
* Gets the name of a hash algorithm that is insecure if it is being used for
105+
* encryption.
106+
*/
107+
string getAnInsecureHashAlgorithmName() {
99108
result = "SHA1" or
100109
result = "MD5"
101110
}
102111

103-
private string rankedAlgorithmBlacklist(int i) {
112+
private string rankedInsecureAlgorithm(int i) {
104113
// In this case we know these are being used for encryption, so we want to match
105114
// weak hash algorithms too.
106-
result = rank[i](string s | s = algorithmBlacklist() or s = hashAlgorithmBlacklist())
115+
result =
116+
rank[i](string s | s = getAnInsecureAlgorithmName() or s = getAnInsecureHashAlgorithmName())
107117
}
108118

109-
private string algorithmBlacklistString(int i) {
110-
i = 1 and result = rankedAlgorithmBlacklist(i)
119+
private string insecureAlgorithmString(int i) {
120+
i = 1 and result = rankedInsecureAlgorithm(i)
111121
or
112-
result = rankedAlgorithmBlacklist(i) + "|" + algorithmBlacklistString(i - 1)
122+
result = rankedInsecureAlgorithm(i) + "|" + insecureAlgorithmString(i - 1)
113123
}
114124

115-
/** Gets a regex for matching strings that look like they contain a blacklisted algorithm. */
116-
string algorithmBlacklistRegex() {
117-
result =
118-
algorithmRegex(algorithmBlacklistString(max(int i | exists(rankedAlgorithmBlacklist(i)))))
125+
/**
126+
* Gets the regular expression used for matching strings that look like they
127+
* contain an algorithm that is known to be insecure.
128+
*/
129+
string getInsecureAlgorithmRegex() {
130+
result = algorithmRegex(insecureAlgorithmString(max(int i | exists(rankedInsecureAlgorithm(i)))))
119131
}
120132

121-
/** Gets a whitelist of algorithms that are known to be secure. */
122-
private string algorithmWhitelist() {
133+
/**
134+
* Gets the name of an algorithm that is known to be secure.
135+
*/
136+
string getASecureAlgorithmName() {
123137
result = "RSA" or
124138
result = "SHA256" or
125139
result = "SHA512" or
@@ -130,20 +144,50 @@ private string algorithmWhitelist() {
130144
result = "ECIES"
131145
}
132146

133-
private string rankedAlgorithmWhitelist(int i) { result = rank[i](algorithmWhitelist()) }
147+
private string rankedSecureAlgorithm(int i) { result = rank[i](getASecureAlgorithmName()) }
134148

135-
private string algorithmWhitelistString(int i) {
136-
i = 1 and result = rankedAlgorithmWhitelist(i)
149+
private string secureAlgorithmString(int i) {
150+
i = 1 and result = rankedSecureAlgorithm(i)
137151
or
138-
result = rankedAlgorithmWhitelist(i) + "|" + algorithmWhitelistString(i - 1)
152+
result = rankedSecureAlgorithm(i) + "|" + secureAlgorithmString(i - 1)
139153
}
140154

141-
/** Gets a regex for matching strings that look like they contain a whitelisted algorithm. */
142-
string algorithmWhitelistRegex() {
143-
result =
144-
algorithmRegex(algorithmWhitelistString(max(int i | exists(rankedAlgorithmWhitelist(i)))))
155+
/**
156+
* Gets a regular expression for matching strings that look like they
157+
* contain an algorithm that is known to be secure.
158+
*/
159+
string getSecureAlgorithmRegex() {
160+
result = algorithmRegex(secureAlgorithmString(max(int i | exists(rankedSecureAlgorithm(i)))))
145161
}
146162

163+
/**
164+
* DEPRECATED: Terminology has been updated. Use `getAnInsecureAlgorithmName()`
165+
* instead.
166+
*/
167+
deprecated string algorithmBlacklist() { result = getAnInsecureAlgorithmName() }
168+
169+
/**
170+
* DEPRECATED: Terminology has been updated. Use
171+
* `getAnInsecureHashAlgorithmName()` instead.
172+
*/
173+
deprecated string hashAlgorithmBlacklist() { result = getAnInsecureHashAlgorithmName() }
174+
175+
/**
176+
* DEPRECATED: Terminology has been updated. Use `getInsecureAlgorithmRegex()` instead.
177+
*/
178+
deprecated string algorithmBlacklistRegex() { result = getInsecureAlgorithmRegex() }
179+
180+
/**
181+
* DEPRECATED: Terminology has been updated. Use `getASecureAlgorithmName()`
182+
* instead.
183+
*/
184+
deprecated string algorithmWhitelist() { result = getASecureAlgorithmName() }
185+
186+
/**
187+
* DEPRECATED: Terminology has been updated. Use `getSecureAlgorithmRegex()` instead.
188+
*/
189+
deprecated string algorithmWhitelistRegex() { result = getSecureAlgorithmRegex() }
190+
147191
/**
148192
* Any use of a cryptographic element that specifies an encryption
149193
* algorithm. For example, methods returning ciphers, decryption methods,

java/ql/test/library-tests/Encryption/whitelist.ql renamed to java/ql/test/library-tests/Encryption/insecure.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import default
22
import semmle.code.java.security.Encryption
33

44
from StringLiteral s
5-
where s.getLiteral().regexpMatch(algorithmWhitelistRegex())
5+
where s.getLiteral().regexpMatch(getInsecureAlgorithmRegex())
66
select s

java/ql/test/library-tests/Encryption/blacklist.ql renamed to java/ql/test/library-tests/Encryption/secure.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import default
22
import semmle.code.java.security.Encryption
33

44
from StringLiteral s
5-
where s.getLiteral().regexpMatch(algorithmBlacklistRegex())
5+
where s.getLiteral().regexpMatch(getSecureAlgorithmRegex())
66
select s

0 commit comments

Comments
 (0)