Skip to content

Commit eb20955

Browse files
committed
Crypto: Further simplify test caes to only use edges/nodes/properties. Update model to account for MAC algorithms.
1 parent 20e2c7c commit eb20955

15 files changed

+268
-60
lines changed

cpp/ql/lib/experimental/quantum/OpenSSL/AlgorithmInstances/KnownAlgorithmConstants.qll

Lines changed: 131 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,30 @@ class KnownOpenSSLHashAlgorithmExpr extends Expr instanceof KnownOpenSSLAlgorith
134134
}
135135
}
136136

137+
class KnownOpenSSLMACAlgorithmExpr extends Expr instanceof KnownOpenSSLAlgorithmExpr {
138+
KnownOpenSSLMACAlgorithmExpr() { resolveAlgorithmFromExpr(this, _, "MAC") }
139+
}
140+
141+
class KnownOpenSSLHMACAlgorithmExpr extends Expr instanceof KnownOpenSSLMACAlgorithmExpr {
142+
KnownOpenSSLHMACAlgorithmExpr() { resolveAlgorithmFromExpr(this, "HMAC", "MAC") }
143+
144+
/**
145+
* Gets an explicit cipher algorithm for this MAC algorithm.
146+
* This occurs when the MAC specifies the algorithm at the same time "HMAC-SHA-256"
147+
*/
148+
KnownOpenSSLHashAlgorithmExpr getExplicitHashAlgorithm() { result = this }
149+
}
150+
151+
class KnownOpenSSLCMACAlgorithmExpr extends Expr instanceof KnownOpenSSLMACAlgorithmExpr {
152+
KnownOpenSSLCMACAlgorithmExpr() { resolveAlgorithmFromExpr(this, "CMAC", "MAC") }
153+
154+
/**
155+
* Gets an explicit cipher algorithm for this MAC algorithm.
156+
* This occurs when the MAC specifies the algorithm at the same time "HMAC-SHA-256"
157+
*/
158+
KnownOpenSSLCipherAlgorithmExpr getExplicitCipherAlgorithm() { result = this }
159+
}
160+
137161
class KnownOpenSSLEllipticCurveAlgorithmExpr extends Expr instanceof KnownOpenSSLAlgorithmExpr {
138162
KnownOpenSSLEllipticCurveAlgorithmExpr() { resolveAlgorithmFromExpr(this, _, "ELLIPTIC_CURVE") }
139163
}
@@ -946,6 +970,8 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
946970
or
947971
name = "id-alg-dh-sig-hmac-sha1" and nid = 325 and normalized = "DH" and algType = "KEY_AGREEMENT"
948972
or
973+
name = "id-alg-dh-sig-hmac-sha1" and nid = 325 and normalized = "HMAC" and algType = "MAC"
974+
or
949975
name = "aes-128-ofb" and nid = 420 and normalized = "AES-128" and algType = "SYMMETRIC_ENCRYPTION"
950976
or
951977
name = "aes-128-ofb" and nid = 420 and normalized = "OFB" and algType = "BLOCK_MODE"
@@ -1124,8 +1150,12 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
11241150
or
11251151
name = "hmac-md5" and nid = 780 and normalized = "MD5" and algType = "HASH"
11261152
or
1153+
name = "hmac-md5" and nid = 780 and normalized = "HMAC" and algType = "HASH"
1154+
or
11271155
name = "hmac-sha1" and nid = 781 and normalized = "SHA1" and algType = "HASH"
11281156
or
1157+
name = "hmac-sha1" and nid = 781 and normalized = "HMAC" and algType = "MAC"
1158+
or
11291159
name = "md_gost94" and nid = 809 and normalized = "GOST94" and algType = "HASH"
11301160
or
11311161
name = "gost94" and nid = 812 and normalized = "GOST94" and algType = "HASH"
@@ -1200,10 +1230,14 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
12001230
or
12011231
name = "rc4-hmac-md5" and nid = 915 and normalized = "MD5" and algType = "HASH"
12021232
or
1233+
name = "rc4-hmac-md5" and nid = 915 and normalized = "HMAC" and algType = "MAC"
1234+
or
12031235
name = "rc4-hmac-md5" and nid = 915 and normalized = "RC4" and algType = "SYMMETRIC_ENCRYPTION"
12041236
or
12051237
name = "aes-128-cbc-hmac-sha1" and nid = 916 and normalized = "SHA1" and algType = "HASH"
12061238
or
1239+
name = "aes-128-cbc-hmac-sha1" and nid = 916 and normalized = "HMAC" and algType = "MAC"
1240+
or
12071241
name = "aes-128-cbc-hmac-sha1" and
12081242
nid = 916 and
12091243
normalized = "AES-128" and
@@ -1213,6 +1247,8 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
12131247
or
12141248
name = "aes-192-cbc-hmac-sha1" and nid = 917 and normalized = "SHA1" and algType = "HASH"
12151249
or
1250+
name = "aes-192-cbc-hmac-sha1" and nid = 917 and normalized = "HMAC" and algType = "MAC"
1251+
or
12161252
name = "aes-192-cbc-hmac-sha1" and
12171253
nid = 917 and
12181254
normalized = "AES-192" and
@@ -1227,6 +1263,8 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
12271263
or
12281264
name = "aes-256-cbc-hmac-sha1" and nid = 918 and normalized = "CBC" and algType = "BLOCK_MODE"
12291265
or
1266+
name = "aes-256-cbc-hmac-sha1" and nid = 918 and normalized = "HMAC" and algType = "MAC"
1267+
or
12301268
name = "aes-128-cbc-hmac-sha256" and nid = 948 and normalized = "SHA-256" and algType = "HASH"
12311269
or
12321270
name = "aes-128-cbc-hmac-sha256" and
@@ -1238,6 +1276,8 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
12381276
or
12391277
name = "aes-192-cbc-hmac-sha256" and nid = 949 and normalized = "SHA-256" and algType = "HASH"
12401278
or
1279+
name = "aes-192-cbc-hmac-sha256" and nid = 949 and normalized = "HMAC" and algType = "MAC"
1280+
or
12411281
name = "aes-192-cbc-hmac-sha256" and
12421282
nid = 949 and
12431283
normalized = "AES-192" and
@@ -1247,6 +1287,8 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
12471287
or
12481288
name = "aes-256-cbc-hmac-sha256" and nid = 950 and normalized = "SHA-256" and algType = "HASH"
12491289
or
1290+
name = "aes-256-cbc-hmac-sha256" and nid = 950 and normalized = "HMAC" and algType = "MAC"
1291+
or
12501292
name = "aes-256-cbc-hmac-sha256" and
12511293
nid = 950 and
12521294
normalized = "AES-256" and
@@ -1286,6 +1328,11 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
12861328
normalized = "CAMELLIA-128" and
12871329
algType = "SYMMETRIC_ENCRYPTION"
12881330
or
1331+
name = "camellia-128-cmac" and
1332+
nid = 964 and
1333+
normalized = "CMAC" and
1334+
algType = "MAC"
1335+
or
12891336
name = "camellia-192-gcm" and
12901337
nid = 965 and
12911338
normalized = "CAMELLIA-192" and
@@ -1338,6 +1385,11 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
13381385
normalized = "CAMELLIA-256" and
13391386
algType = "SYMMETRIC_ENCRYPTION"
13401387
or
1388+
name = "camellia-256-cmac" and
1389+
nid = 972 and
1390+
normalized = "CMAC" and
1391+
algType = "MAC"
1392+
or
13411393
name = "id-scrypt" and nid = 973 and normalized = "SCRYPT" and algType = "KEY_DERIVATION"
13421394
or
13431395
name = "gost89-cnt-12" and
@@ -1351,11 +1403,13 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
13511403
or
13521404
name = "md_gost12_512" and nid = 983 and normalized = "GOST" and algType = "HASH"
13531405
or
1406+
// TODO: re-evaluate: this is a signing algorithm using hashing and curves
13541407
name = "id-tc26-signwithdigest-gost3410-2012-256" and
13551408
nid = 985 and
13561409
normalized = "GOST34102012" and
13571410
algType = "SYMMETRIC_ENCRYPTION"
13581411
or
1412+
// TODO: re-evaluate: this is a signing algorithm using hashing and curves
13591413
name = "id-tc26-signwithdigest-gost3410-2012-512" and
13601414
nid = 986 and
13611415
normalized = "GOST34102012" and
@@ -1364,22 +1418,42 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
13641418
name = "id-tc26-hmac-gost-3411-2012-256" and
13651419
nid = 988 and
13661420
normalized = "GOST34112012" and
1367-
algType = "SYMMETRIC_ENCRYPTION"
1421+
algType = "HASH"
1422+
or
1423+
name = "id-tc26-hmac-gost-3411-2012-256" and
1424+
nid = 988 and
1425+
normalized = "HMAC" and
1426+
algType = "MAC"
13681427
or
13691428
name = "id-tc26-hmac-gost-3411-2012-512" and
13701429
nid = 989 and
13711430
normalized = "GOST34112012" and
1372-
algType = "SYMMETRIC_ENCRYPTION"
1431+
algType = "HASH"
1432+
or
1433+
name = "id-tc26-hmac-gost-3411-2012-512" and
1434+
nid = 989 and
1435+
normalized = "HMAC" and
1436+
algType = "MAC"
13731437
or
13741438
name = "id-tc26-agreement-gost-3410-2012-256" and
13751439
nid = 992 and
13761440
normalized = "GOST34102012" and
1377-
algType = "SYMMETRIC_ENCRYPTION"
1441+
algType = "ELLIPTIC_CURVE"
1442+
or
1443+
name = "id-tc26-agreement-gost-3410-2012-256" and
1444+
nid = 992 and
1445+
normalized = "GOST34102012" and
1446+
algType = "KEY_AGREEMENT"
13781447
or
13791448
name = "id-tc26-agreement-gost-3410-2012-512" and
13801449
nid = 993 and
13811450
normalized = "GOST34102012" and
1382-
algType = "SYMMETRIC_ENCRYPTION"
1451+
algType = "ELLIPTIC_CURVE"
1452+
or
1453+
name = "id-tc26-agreement-gost-3410-2012-512" and
1454+
nid = 993 and
1455+
normalized = "GOST34102012" and
1456+
algType = "KEY_AGREEMENT"
13831457
or
13841458
name = "id-tc26-gost-3410-2012-512-constants" and
13851459
nid = 996 and
@@ -1467,12 +1541,20 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
14671541
or
14681542
name = "hmac-sha3-224" and nid = 1102 and normalized = "SHA3-224" and algType = "HASH"
14691543
or
1544+
name = "hmac-sha3-224" and nid = 1102 and normalized = "HMAC" and algType = "MAC"
1545+
or
14701546
name = "hmac-sha3-256" and nid = 1103 and normalized = "SHA3-256" and algType = "HASH"
14711547
or
1548+
name = "hmac-sha3-256" and nid = 1103 and normalized = "HMAC" and algType = "MAC"
1549+
or
14721550
name = "hmac-sha3-384" and nid = 1104 and normalized = "SHA3-384" and algType = "HASH"
14731551
or
1552+
name = "hmac-sha3-384" and nid = 1104 and normalized = "HMAC" and algType = "MAC"
1553+
or
14741554
name = "hmac-sha3-512" and nid = 1105 and normalized = "SHA3-512" and algType = "HASH"
14751555
or
1556+
name = "hmac-sha3-512" and nid = 1105 and normalized = "HMAC" and algType = "MAC"
1557+
or
14761558
name = "id-dsa-with-sha384" and nid = 1106 and normalized = "DSA" and algType = "SIGNATURE"
14771559
or
14781560
name = "id-dsa-with-sha384" and nid = 1106 and normalized = "SHA-384" and algType = "HASH"
@@ -2240,34 +2322,67 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
22402322
normalized = "GOST" and
22412323
algType = "SYMMETRIC_ENCRYPTION"
22422324
or
2325+
name = "hmac gost 34.11-2012 256 bit" and
2326+
nid = 988 and
2327+
normalized = "HMAC" and
2328+
algType = "MAC"
2329+
or
22432330
name = "hmac gost 34.11-2012 512 bit" and
22442331
nid = 989 and
22452332
normalized = "GOST" and
22462333
algType = "SYMMETRIC_ENCRYPTION"
22472334
or
2335+
name = "hmac gost 34.11-2012 512 bit" and
2336+
nid = 989 and
2337+
normalized = "HMAC" and
2338+
algType = "MAC"
2339+
or
22482340
name = "hmac gost 34.11-94" and
22492341
nid = 810 and
22502342
normalized = "GOST" and
22512343
algType = "SYMMETRIC_ENCRYPTION"
22522344
or
2345+
name = "hmac gost 34.11-94" and
2346+
nid = 810 and
2347+
normalized = "HMAC" and
2348+
algType = "MAC"
2349+
or
22532350
name = "hmacwithmd5" and nid = 797 and normalized = "MD5" and algType = "HASH"
22542351
or
2352+
name = "hmacwithmd5" and nid = 797 and normalized = "HMAC" and algType = "MAC"
2353+
or
22552354
name = "hmacwithsha1" and nid = 163 and normalized = "SHA1" and algType = "HASH"
22562355
or
2356+
name = "hmacwithsha1" and nid = 163 and normalized = "HMAC" and algType = "MAC"
2357+
or
22572358
name = "hmacwithsha224" and nid = 798 and normalized = "SHA-224" and algType = "HASH"
22582359
or
2360+
name = "hmacwithsha224" and nid = 798 and normalized = "HMAC" and algType = "MAC"
2361+
or
22592362
name = "hmacwithsha256" and nid = 799 and normalized = "SHA-256" and algType = "HASH"
22602363
or
2364+
name = "hmacwithsha256" and nid = 799 and normalized = "HMAC" and algType = "MAC"
2365+
or
22612366
name = "hmacwithsha384" and nid = 800 and normalized = "SHA-384" and algType = "HASH"
22622367
or
2368+
name = "hmacwithsha384" and nid = 800 and normalized = "HMAC" and algType = "MAC"
2369+
or
22632370
name = "hmacwithsha512" and nid = 801 and normalized = "SHA-512" and algType = "HASH"
22642371
or
2372+
name = "hmacwithsha512" and nid = 801 and normalized = "HMAC" and algType = "MAC"
2373+
or
22652374
name = "hmacwithsha512-224" and nid = 1193 and normalized = "SHA-512-224" and algType = "HASH"
22662375
or
2376+
name = "hmacwithsha512-224" and nid = 1193 and normalized = "HMAC" and algType = "MAC"
2377+
or
22672378
name = "hmacwithsha512-256" and nid = 1194 and normalized = "SHA-512-256" and algType = "HASH"
22682379
or
2380+
name = "hmacwithsha512-256" and nid = 1194 and normalized = "HMAC" and algType = "MAC"
2381+
or
22692382
name = "hmacwithsm3" and nid = 1281 and normalized = "SM3" and algType = "HASH"
22702383
or
2384+
name = "hmacwithsm3" and nid = 1281 and normalized = "HMAC" and algType = "MAC"
2385+
or
22712386
name = "id-aes128-ccm" and
22722387
nid = 896 and
22732388
normalized = "AES-128" and
@@ -2517,12 +2632,20 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
25172632
or
25182633
name = "id-hmacwithsha3-224" and nid = 1102 and normalized = "SHA3-224" and algType = "HASH"
25192634
or
2635+
name = "id-hmacwithsha3-224" and nid = 1102 and normalized = "HMAC" and algType = "MAC"
2636+
or
25202637
name = "id-hmacwithsha3-256" and nid = 1103 and normalized = "SHA3-256" and algType = "HASH"
25212638
or
2639+
name = "id-hmacwithsha3-256" and nid = 1103 and normalized = "HMAC" and algType = "MAC"
2640+
or
25222641
name = "id-hmacwithsha3-384" and nid = 1104 and normalized = "SHA3-384" and algType = "HASH"
25232642
or
2643+
name = "id-hmacwithsha3-384" and nid = 1104 and normalized = "HMAC" and algType = "MAC"
2644+
or
25242645
name = "id-hmacwithsha3-512" and nid = 1105 and normalized = "SHA3-512" and algType = "HASH"
25252646
or
2647+
name = "id-hmacwithsha3-512" and nid = 1105 and normalized = "HMAC" and algType = "MAC"
2648+
or
25262649
name = "id-regctrl" and nid = 313 and normalized = "CTR" and algType = "BLOCK_MODE"
25272650
or
25282651
name = "id-smime-alg-3deswrap" and
@@ -3080,4 +3203,8 @@ predicate knownOpenSSLAlgorithmLiteral(string name, int nid, string normalized,
30803203
algType = "SIGNATURE"
30813204
or
30823205
name = "sm3withrsaencryption" and nid = 1144 and normalized = "SM3" and algType = "HASH"
3206+
or
3207+
name = "hmac" and nid = 855 and normalized = "HMAC" and algType = "MAC"
3208+
or
3209+
name = "cmac" and nid = 894 and normalized = "CMAC" and algType = "MAC"
30833210
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import cpp
2+
private import experimental.quantum.Language
3+
private import KnownAlgorithmConstants
4+
private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumers
5+
private import experimental.quantum.OpenSSL.AlgorithmInstances.OpenSSLAlgorithmInstanceBase
6+
private import experimental.quantum.OpenSSL.Operations.OpenSSLOperations
7+
private import AlgToAVCFlow
8+
9+
class KnownOpenSSLMACConstantAlgorithmInstance extends OpenSSLAlgorithmInstance,
10+
Crypto::MACAlgorithmInstance instanceof KnownOpenSSLMACAlgorithmExpr
11+
{
12+
OpenSSLAlgorithmValueConsumer getterCall;
13+
14+
KnownOpenSSLMACConstantAlgorithmInstance() {
15+
// Two possibilities:
16+
// 1) The source is a literal and flows to a getter, then we know we have an instance
17+
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
18+
// Possibility 1:
19+
this instanceof OpenSSLAlgorithmLiteral and
20+
exists(DataFlow::Node src, DataFlow::Node sink |
21+
// Sink is an argument to a CipherGetterCall
22+
sink = getterCall.(OpenSSLAlgorithmValueConsumer).getInputNode() and
23+
// Source is `this`
24+
src.asExpr() = this and
25+
// This traces to a getter
26+
KnownOpenSSLAlgorithmToAlgorithmValueConsumerFlow::flow(src, sink)
27+
)
28+
or
29+
// Possibility 2:
30+
this instanceof OpenSSLAlgorithmCall and
31+
getterCall = this
32+
}
33+
34+
override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall }
35+
36+
override string getRawMACAlgorithmName() {
37+
result = this.(Literal).getValue().toString()
38+
or
39+
result = this.(Call).getTarget().getName()
40+
}
41+
42+
override Crypto::TMACType getMACType() {
43+
this instanceof KnownOpenSSLHMACAlgorithmExpr and result instanceof Crypto::THMAC
44+
or
45+
this instanceof KnownOpenSSLCMACAlgorithmExpr and result instanceof Crypto::TCMAC
46+
}
47+
}
48+
49+
class KnownOpenSSLHMACConstantAlgorithmInstance extends Crypto::HMACAlgorithmInstance,
50+
KnownOpenSSLMACConstantAlgorithmInstance
51+
{
52+
override Crypto::AlgorithmValueConsumer getHashAlgorithmValueConsumer() {
53+
if exists(this.(KnownOpenSSLHMACAlgorithmExpr).getExplicitHashAlgorithm())
54+
then
55+
// ASSUMPTION: if there is an explicit hash algorithm, it is already modeled
56+
// and we can simply grab that model's AVC
57+
exists(OpenSSLAlgorithmInstance inst | inst.getAVC() = result and inst = this)
58+
else
59+
// ASSUMPTION: If no explicit algorithm is given, then it is assumed to be configured by
60+
// a signature operation
61+
exists(Crypto::SignatureOperationInstance s |
62+
s.getHashAlgorithmValueConsumer() = result and
63+
s.getAnAlgorithmValueConsumer() = this.getAVC()
64+
)
65+
}
66+
}

0 commit comments

Comments
 (0)