11import cpp
2+ import experimental.quantum.OpenSSL.GenericSourceCandidateLiteral
23
34predicate resolveAlgorithmFromExpr ( Expr e , string normalizedName , string algType ) {
45 resolveAlgorithmFromCall ( e , normalizedName , algType )
@@ -32,30 +33,20 @@ class KnownOpenSSLCipherAlgorithmConstant extends KnownOpenSSLAlgorithmConstant
3233}
3334
3435class KnownOpenSSLPaddingAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
35- string algType ;
36-
3736 KnownOpenSSLPaddingAlgorithmConstant ( ) {
38- resolveAlgorithmFromExpr ( this , _, algType ) and
39- algType .matches ( "%PADDING" )
37+ exists ( string algType |
38+ resolveAlgorithmFromExpr ( this , _, algType ) and
39+ algType .matches ( "%PADDING" )
40+ )
4041 }
4142}
4243
4344class 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" ) }
5046}
5147
5248class KnownOpenSSLHashAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
53- string algType ;
54-
55- KnownOpenSSLHashAlgorithmConstant ( ) {
56- resolveAlgorithmFromExpr ( this , _, algType ) and
57- algType .matches ( "%HASH" )
58- }
49+ KnownOpenSSLHashAlgorithmConstant ( ) { resolveAlgorithmFromExpr ( this , _, "HASH" ) }
5950
6051 int getExplicitDigestLength ( ) {
6152 exists ( string name |
@@ -68,13 +59,14 @@ class KnownOpenSSLHashAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
6859
6960class KnownOpenSSLEllipticCurveAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
7061 KnownOpenSSLEllipticCurveAlgorithmConstant ( ) {
71- exists ( string algType |
72- resolveAlgorithmFromExpr ( this , _, algType ) and
73- algType .matches ( "ELLIPTIC_CURVE" )
74- )
62+ resolveAlgorithmFromExpr ( this , _, "ELLIPTIC_CURVE" )
7563 }
7664}
7765
66+ class KnownOpenSSLSignatureAlgorithmConstant extends KnownOpenSSLAlgorithmConstant {
67+ KnownOpenSSLSignatureAlgorithmConstant ( ) { resolveAlgorithmFromExpr ( this , _, "SIGNATURE" ) }
68+ }
69+
7870/**
7971 * Resolves a call to a 'direct algorithm getter', e.g., EVP_MD5()
8072 * This approach to fetching algorithms was used in OpenSSL 1.0.2.
@@ -101,10 +93,10 @@ predicate resolveAlgorithmFromCall(Call c, string normalized, string algType) {
10193 * if `e` resolves to a known algorithm.
10294 * If this predicate does not hold, then `e` can be interpreted as being of `UNKNOWN` type.
10395 */
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 )
108100 or
109101 exists ( string name |
110102 name = resolveAlgorithmAlias ( e .getValue ( ) ) and
@@ -123,30 +115,6 @@ string resolveAlgorithmAlias(string name) {
123115 )
124116}
125117
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-
150118string getAlgorithmAlias ( string alias ) {
151119 customAliases ( result , alias )
152120 or
@@ -260,11 +228,6 @@ predicate defaultAliases(string target, string alias) {
260228 alias = "ssl3-sha1" and target = "sha1"
261229}
262230
263- predicate tbd ( string normalized , string algType ) {
264- knownOpenSSLAlgorithmLiteral ( _, _, normalized , algType ) and
265- algType = "HASH"
266- }
267-
268231/**
269232 * Enumeration of all known crypto algorithms for openSSL
270233 * `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,
291254 or
292255 name = "ed25519" and nid = 1087 and normalized = "ED25519" and algType = "ELLIPTIC_CURVE"
293256 or
257+ name = "ed25519" and nid = 1087 and normalized = "ED25519" and algType = "SIGNATURE"
258+ or
294259 name = "ed448" and nid = 1088 and normalized = "ED448" and algType = "ELLIPTIC_CURVE"
295260 or
261+ name = "ed448" and nid = 1088 and normalized = "ED448" and algType = "SIGNATURE"
262+ or
296263 name = "md2" and nid = 3 and normalized = "MD2" and algType = "HASH"
297264 or
298265 name = "sha" and nid = 41 and normalized = "SHA" and algType = "HASH"
@@ -1712,8 +1679,12 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
17121679 or
17131680 name = "x448" and nid = 1035 and normalized = "X448" and algType = "ELLIPTIC_CURVE"
17141681 or
1682+ name = "x448" and nid = 1035 and normalized = "X448" and algType = "KEY_EXCHANGE"
1683+ or
17151684 name = "x25519" and nid = 1034 and normalized = "X25519" and algType = "ELLIPTIC_CURVE"
17161685 or
1686+ name = "x25519" and nid = 1034 and normalized = "X25519" and algType = "KEY_EXCHANGE"
1687+ or
17171688 name = "authecdsa" and nid = 1047 and normalized = "ECDSA" and algType = "SIGNATURE"
17181689 or
17191690 name = "authgost01" and nid = 1050 and normalized = "GOST" and algType = "SYMMETRIC_ENCRYPTION"
0 commit comments