Skip to content

Commit 6a4659f

Browse files
committed
Updating known constants for OpenSSL to handle direct algorithm getters from older versions of openssl (e.g., EVP_md5())
1 parent 3316d61 commit 6a4659f

1 file changed

Lines changed: 53 additions & 9 deletions

File tree

cpp/ql/lib/experimental/Quantum/OpenSSL/OpenSSLKnownAlgorithmConstants.qll

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
11
import cpp
2+
import LibraryDetector
3+
4+
class KnownOpenSSLAlgorithmConstant extends Expr {
5+
string normalizedName;
6+
string algType;
7+
8+
KnownOpenSSLAlgorithmConstant() {
9+
resolveAlgorithmFromCall(this, normalizedName, algType)
10+
or
11+
resolveAlgorithmFromLiteral(this, normalizedName, algType)
12+
}
13+
14+
string getNormalizedName() { result = normalizedName }
15+
16+
string getAlgType() { result = algType }
17+
}
18+
19+
/**
20+
* Resolves a call to a 'direct algorithm getter', e.g., EVP_MD5()
21+
* This approach to fetching algorithms was used in OpenSSL 1.0.2.
22+
* The strategy for resolving these calls is to parse the target name
23+
* and resolve the name as though it were a known literal.
24+
* There are a few exceptions where the name doesn't directly match the
25+
* known literal set. If that occurs, users must add the name to the
26+
* set of aliases. E.g., EVP_dss() and EVP_dss1() needed such mappings
27+
* alias = "dss" and target = "dsa"
28+
* or
29+
* alias = "dss1" and target = "dsaWithSHA1"
30+
*/
31+
predicate resolveAlgorithmFromCall(Call c, string normalized, string algType) {
32+
isPossibleOpenSSLFunction(c.getTarget()) and
33+
exists(string name, string parsedTargetName |
34+
parsedTargetName =
35+
c.getTarget().getName().replaceAll("EVP_", "").toLowerCase().replaceAll("_", "-") and
36+
name = resolveAlgorithmAlias(parsedTargetName) and
37+
knownOpenSSLAlgorithmLiteral(name, _, normalized, algType)
38+
)
39+
}
240

341
/**
442
* Resolves literal `e` to a known algorithm name, nid, normalized name, and algType
@@ -7,21 +45,23 @@ import cpp
745
*/
846
predicate resolveAlgorithmFromLiteral(Literal e, string normalized, string algType) {
947
exists(int nid |
10-
nid = getPossibleNidFromLiteral(e) and knownOpenSSLAlgorithm(_, nid, normalized, algType)
48+
nid = getPossibleNidFromLiteral(e) and knownOpenSSLAlgorithmLiteral(_, nid, normalized, algType)
1149
)
1250
or
1351
exists(string name |
14-
name = resolveAlgorithmAlias(e) and knownOpenSSLAlgorithm(name, _, normalized, algType)
52+
name = resolveAlgorithmAlias(e.getValue()) and
53+
knownOpenSSLAlgorithmLiteral(name, _, normalized, algType)
1554
)
1655
}
1756

18-
string resolveAlgorithmAlias(StringLiteral name) {
19-
exists(string lower | lower = name.getValue().toLowerCase() |
57+
bindingset[name]
58+
string resolveAlgorithmAlias(string name) {
59+
exists(string lower | lower = name.toLowerCase() |
2060
// The result is an alias algorithm name if known
2161
result = getAlgorithmAlias(lower)
2262
or
2363
// or the name is itself a known algorithm
24-
knownOpenSSLAlgorithm(lower, _, _, _) and result = lower
64+
knownOpenSSLAlgorithmLiteral(lower, _, _, _) and result = lower
2565
)
2666
}
2767

@@ -133,6 +173,10 @@ predicate defaultAliases(string target, string alias) {
133173
or
134174
alias = "desx" and target = "desx-cbc"
135175
or
176+
alias = "dss" and target = "dsa"
177+
or
178+
alias = "dss1" and target = "dsaWithSHA1"
179+
or
136180
alias = "idea" and target = "idea-cbc"
137181
or
138182
alias = "rc2" and target = "rc2-cbc"
@@ -165,7 +209,7 @@ predicate defaultAliases(string target, string alias) {
165209
* `normalized` is the normalized name of the algorithm (e.g., "AES128" for "aes-128-cbc")
166210
* `algType` is the type of algorithm (e.g., "SYMMETRIC_ENCRYPTION")
167211
*/
168-
predicate knownOpenSSLAlgorithm(string name, int nid, string normalized, string algType) {
212+
predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized, string algType) {
169213
name = "rsa" and nid = 19 and normalized = "RSA" and algType = "ASYMMETRIC_ENCRYPTION"
170214
or
171215
name = "prime192v1" and nid = 409 and normalized = "PRIME192V1" and algType = "ELLIPTIC_CURVE"
@@ -895,7 +939,7 @@ predicate knownOpenSSLAlgorithm(string name, int nid, string normalized, string
895939
or
896940
name = "md_gost94" and nid = 809 and normalized = "GOST94" and algType = "HASH"
897941
or
898-
name = "gost94" and nid = 812 and normalized = "GOST94" and algType = "SYMMETRIC_ENCRYPTION"
942+
name = "gost94" and nid = 812 and normalized = "GOST94" and algType = "HASH"
899943
or
900944
name = "gost89" and nid = 813 and normalized = "GOST89" and algType = "SYMMETRIC_ENCRYPTION"
901945
or
@@ -1114,9 +1158,9 @@ predicate knownOpenSSLAlgorithm(string name, int nid, string normalized, string
11141158
or
11151159
name = "gost-mac-12" and nid = 976 and normalized = "GOST" and algType = "SYMMETRIC_ENCRYPTION"
11161160
or
1117-
name = "md_gost12_256" and nid = 982 and normalized = "GOST" and algType = "SYMMETRIC_ENCRYPTION"
1161+
name = "md_gost12_256" and nid = 982 and normalized = "GOST" and algType = "HASH"
11181162
or
1119-
name = "md_gost12_512" and nid = 983 and normalized = "GOST" and algType = "SYMMETRIC_ENCRYPTION"
1163+
name = "md_gost12_512" and nid = 983 and normalized = "GOST" and algType = "HASH"
11201164
or
11211165
name = "id-tc26-signwithdigest-gost3410-2012-256" and
11221166
nid = 985 and

0 commit comments

Comments
 (0)