1
1
import cpp
2
+ import experimental.quantum.OpenSSL.GenericSourceCandidateLiteral
2
3
3
4
predicate resolveAlgorithmFromExpr ( Expr e , string normalizedName , string algType ) {
4
5
resolveAlgorithmFromCall ( e , normalizedName , algType )
@@ -32,30 +33,20 @@ class KnownOpenSSLCipherAlgorithmConstant extends KnownOpenSSLAlgorithmConstant
32
33
}
33
34
34
35
class KnownOpenSSLPaddingAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
35
- string algType ;
36
-
37
36
KnownOpenSSLPaddingAlgorithmConstant ( ) {
38
- resolveAlgorithmFromExpr ( this , _, algType ) and
39
- algType .matches ( "%PADDING" )
37
+ exists ( string algType |
38
+ resolveAlgorithmFromExpr ( this , _, algType ) and
39
+ algType .matches ( "%PADDING" )
40
+ )
40
41
}
41
42
}
42
43
43
44
class KnownOpenSSLBlockModeAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
44
- string algType ;
45
-
46
- KnownOpenSSLBlockModeAlgorithmConstant ( ) {
47
- resolveAlgorithmFromExpr ( this , _, algType ) and
48
- algType .matches ( "%BLOCK_MODE" )
49
- }
45
+ KnownOpenSSLBlockModeAlgorithmConstant ( ) { resolveAlgorithmFromExpr ( this , _, "BLOCK_MODE" ) }
50
46
}
51
47
52
48
class KnownOpenSSLHashAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
53
- string algType ;
54
-
55
- KnownOpenSSLHashAlgorithmConstant ( ) {
56
- resolveAlgorithmFromExpr ( this , _, algType ) and
57
- algType .matches ( "%HASH" )
58
- }
49
+ KnownOpenSSLHashAlgorithmConstant ( ) { resolveAlgorithmFromExpr ( this , _, "HASH" ) }
59
50
60
51
int getExplicitDigestLength ( ) {
61
52
exists ( string name |
@@ -68,13 +59,14 @@ class KnownOpenSSLHashAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
68
59
69
60
class KnownOpenSSLEllipticCurveAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
70
61
KnownOpenSSLEllipticCurveAlgorithmConstant ( ) {
71
- exists ( string algType |
72
- resolveAlgorithmFromExpr ( this , _, algType ) and
73
- algType .matches ( "ELLIPTIC_CURVE" )
74
- )
62
+ resolveAlgorithmFromExpr ( this , _, "ELLIPTIC_CURVE" )
75
63
}
76
64
}
77
65
66
+ class KnownOpenSSLSignatureAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
67
+ KnownOpenSSLSignatureAlgorithmConstant ( ) { resolveAlgorithmFromExpr ( this , _, "SIGNATURE" ) }
68
+ }
69
+
78
70
/**
79
71
* Resolves a call to a 'direct algorithm getter', e.g., EVP_MD5()
80
72
* This approach to fetching algorithms was used in OpenSSL 1.0.2.
@@ -101,10 +93,10 @@ predicate resolveAlgorithmFromCall(Call c, string normalized, string algType) {
101
93
* if `e` resolves to a known algorithm.
102
94
* If this predicate does not hold, then `e` can be interpreted as being of `UNKNOWN` type.
103
95
*/
104
- predicate resolveAlgorithmFromLiteral ( Literal e , string normalized , string algType ) {
105
- exists ( int nid |
106
- nid = getPossibleNidFromLiteral ( e ) and knownOpenSSLAlgorithmLiteral ( _ , nid , normalized , algType )
107
- )
96
+ predicate resolveAlgorithmFromLiteral (
97
+ OpenSSLGenericSourceCandidateLiteral e , string normalized , string algType
98
+ ) {
99
+ knownOpenSSLAlgorithmLiteral ( _ , e . getValue ( ) . toInt ( ) , normalized , algType )
108
100
or
109
101
exists ( string name |
110
102
name = resolveAlgorithmAlias ( e .getValue ( ) ) and
@@ -123,30 +115,6 @@ string resolveAlgorithmAlias(string name) {
123
115
)
124
116
}
125
117
126
- private int getPossibleNidFromLiteral ( Literal e ) {
127
- result = e .getValue ( ) .toInt ( ) and
128
- not e instanceof CharLiteral and
129
- not e instanceof StringLiteral and
130
- // ASSUMPTION, no negative numbers are allowed
131
- // RATIONALE: this is a performance improvement to avoid having to trace every number
132
- not exists ( UnaryMinusExpr u | u .getOperand ( ) = e ) and
133
- // OPENSSL has a special macro for getting every line, ignore it
134
- not exists ( MacroInvocation mi | mi .getExpr ( ) = e and mi .getMacroName ( ) = "OPENSSL_LINE" ) and
135
- // Filter out cases where an int is assigned into a pointer, e.g., char* x = NULL;
136
- not exists ( Assignment a |
137
- a .getRValue ( ) = e and a .getLValue ( ) .getType ( ) .getUnspecifiedType ( ) instanceof PointerType
138
- ) and
139
- not exists ( Initializer i |
140
- i .getExpr ( ) = e and
141
- i .getDeclaration ( ) .getADeclarationEntry ( ) .getUnspecifiedType ( ) instanceof PointerType
142
- ) and
143
- // Filter out cases where an int is returned into a pointer, e.g., return NULL;
144
- not exists ( ReturnStmt r |
145
- r .getExpr ( ) = e and
146
- r .getEnclosingFunction ( ) .getType ( ) .getUnspecifiedType ( ) instanceof PointerType
147
- )
148
- }
149
-
150
118
string getAlgorithmAlias ( string alias ) {
151
119
customAliases ( result , alias )
152
120
or
@@ -260,11 +228,6 @@ predicate defaultAliases(string target, string alias) {
260
228
alias = "ssl3-sha1" and target = "sha1"
261
229
}
262
230
263
- predicate tbd ( string normalized , string algType ) {
264
- knownOpenSSLAlgorithmLiteral ( _, _, normalized , algType ) and
265
- algType = "HASH"
266
- }
267
-
268
231
/**
269
232
* Enumeration of all known crypto algorithms for openSSL
270
233
* `name` is all lower case (caller's must ensure they pass in lower case)
@@ -291,8 +254,12 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
291
254
or
292
255
name = "ed25519" and nid = 1087 and normalized = "ED25519" and algType = "ELLIPTIC_CURVE"
293
256
or
257
+ name = "ed25519" and nid = 1087 and normalized = "ED25519" and algType = "SIGNATURE"
258
+ or
294
259
name = "ed448" and nid = 1088 and normalized = "ED448" and algType = "ELLIPTIC_CURVE"
295
260
or
261
+ name = "ed448" and nid = 1088 and normalized = "ED448" and algType = "SIGNATURE"
262
+ or
296
263
name = "md2" and nid = 3 and normalized = "MD2" and algType = "HASH"
297
264
or
298
265
name = "sha" and nid = 41 and normalized = "SHA" and algType = "HASH"
@@ -1712,8 +1679,12 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
1712
1679
or
1713
1680
name = "x448" and nid = 1035 and normalized = "X448" and algType = "ELLIPTIC_CURVE"
1714
1681
or
1682
+ name = "x448" and nid = 1035 and normalized = "X448" and algType = "KEY_EXCHANGE"
1683
+ or
1715
1684
name = "x25519" and nid = 1034 and normalized = "X25519" and algType = "ELLIPTIC_CURVE"
1716
1685
or
1686
+ name = "x25519" and nid = 1034 and normalized = "X25519" and algType = "KEY_EXCHANGE"
1687
+ or
1717
1688
name = "authecdsa" and nid = 1047 and normalized = "ECDSA" and algType = "SIGNATURE"
1718
1689
or
1719
1690
name = "authgost01" and nid = 1050 and normalized = "GOST" and algType = "SYMMETRIC_ENCRYPTION"
0 commit comments