Skip to content

Commit 4042081

Browse files
committed
Missing files, should have been part of last commit.
1 parent 0a0be41 commit 4042081

File tree

6 files changed

+365
-82
lines changed

6 files changed

+365
-82
lines changed

cpp/ql/lib/experimental/Quantum/OpenSSL/AlgorithmInstances/CipherAlgorithmInstance.qll

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,25 @@ predicate knownOpenSSLConstantToCipherFamilyType(
6666
class KnownOpenSSLCipherConstantAlgorithmInstance extends OpenSSLAlgorithmInstance,
6767
Crypto::KeyOperationAlgorithmInstance instanceof KnownOpenSSLCipherAlgorithmConstant
6868
{
69-
//OpenSSLAlgorithmInstance,
7069
OpenSSLAlgorithmValueConsumer getterCall;
7170

7271
KnownOpenSSLCipherConstantAlgorithmInstance() {
73-
(
74-
// Two possibilities:
75-
// 1) The source is a literal and flows to a getter, then we know we have an instance
76-
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
77-
// Possibility 1:
78-
this instanceof Literal and
79-
exists(DataFlow::Node src, DataFlow::Node sink |
80-
// Sink is an argument to a CipherGetterCall
81-
sink = getterCall.(OpenSSLAlgorithmValueConsumer).getInputNode() and
82-
// Source is `this`
83-
src.asExpr() = this and
84-
// This traces to a getter
85-
KnownOpenSSLAlgorithmToAlgorithmValueConsumerFlow::flow(src, sink)
86-
)
87-
or
88-
// Possibility 2:
89-
this instanceof DirectAlgorithmValueConsumer and getterCall = this
72+
// Two possibilities:
73+
// 1) The source is a literal and flows to a getter, then we know we have an instance
74+
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
75+
// Possibility 1:
76+
this instanceof Literal and
77+
exists(DataFlow::Node src, DataFlow::Node sink |
78+
// Sink is an argument to a CipherGetterCall
79+
sink = getterCall.(OpenSSLAlgorithmValueConsumer).getInputNode() and
80+
// Source is `this`
81+
src.asExpr() = this and
82+
// This traces to a getter
83+
KnownOpenSSLAlgorithmToAlgorithmValueConsumerFlow::flow(src, sink)
9084
)
85+
or
86+
// Possibility 2:
87+
this instanceof DirectAlgorithmValueConsumer and getterCall = this
9188
}
9289

9390
override Crypto::ModeOfOperationAlgorithmInstance getModeOfOperationAlgorithm() {
@@ -101,7 +98,7 @@ class KnownOpenSSLCipherConstantAlgorithmInstance extends OpenSSLAlgorithmInstan
10198
//TODO: the padding is either self, or it flows through getter ctx to a set padding call
10299
// like EVP_PKEY_CTX_set_rsa_padding
103100
result = this
104-
// or trace through getter ctx to set padding
101+
// TODO or trace through getter ctx to set padding
105102
}
106103

107104
override string getRawAlgorithmName() { result = this.(Literal).getValue().toString() }
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import cpp
2+
import experimental.Quantum.Language
3+
import KnownAlgorithmConstants
4+
import experimental.Quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumers
5+
import AlgToAVCFlow
6+
7+
predicate knownOpenSSLConstantToHashFamilyType(
8+
KnownOpenSSLHashAlgorithmConstant e, Crypto::THashType type
9+
) {
10+
exists(string name |
11+
name = e.getNormalizedName() and
12+
(
13+
name.matches("BLAKE2B") and type instanceof Crypto::BLAKE2B
14+
or
15+
name.matches("BLAKE2S") and type instanceof Crypto::BLAKE2S
16+
or
17+
name.matches("GOST%") and type instanceof Crypto::GOSTHash
18+
or
19+
name.matches("MD2") and type instanceof Crypto::MD2
20+
or
21+
name.matches("MD4") and type instanceof Crypto::MD4
22+
or
23+
name.matches("MD5") and type instanceof Crypto::MD5
24+
or
25+
name.matches("MDC2") and type instanceof Crypto::MDC2
26+
or
27+
name.matches("POLY1305") and type instanceof Crypto::POLY1305
28+
or
29+
name.matches(["SHA", "SHA1"]) and type instanceof Crypto::SHA1
30+
or
31+
name.matches("SHA+%") and not name.matches(["SHA1", "SHA3-"]) and type instanceof Crypto::SHA2
32+
or
33+
name.matches("SHA3-%") and type instanceof Crypto::SHA3
34+
or
35+
name.matches(["SHAKE"]) and type instanceof Crypto::SHAKE
36+
or
37+
name.matches("SM3") and type instanceof Crypto::SM3
38+
or
39+
name.matches("RIPEMD160") and type instanceof Crypto::RIPEMD160
40+
or
41+
name.matches("WHIRLPOOL") and type instanceof Crypto::WHIRLPOOL
42+
)
43+
)
44+
}
45+
46+
class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance,
47+
Crypto::HashAlgorithmInstance instanceof KnownOpenSSLHashAlgorithmConstant
48+
{
49+
OpenSSLAlgorithmValueConsumer getterCall;
50+
51+
KnownOpenSSLHashConstantAlgorithmInstance() {
52+
// Two possibilities:
53+
// 1) The source is a literal and flows to a getter, then we know we have an instance
54+
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
55+
// Possibility 1:
56+
this instanceof Literal and
57+
exists(DataFlow::Node src, DataFlow::Node sink |
58+
// Sink is an argument to a CipherGetterCall
59+
sink = getterCall.(OpenSSLAlgorithmValueConsumer).getInputNode() and
60+
// Source is `this`
61+
src.asExpr() = this and
62+
// This traces to a getter
63+
KnownOpenSSLAlgorithmToAlgorithmValueConsumerFlow::flow(src, sink)
64+
)
65+
or
66+
// Possibility 2:
67+
this instanceof DirectAlgorithmValueConsumer and getterCall = this
68+
}
69+
70+
override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall }
71+
72+
override Crypto::THashType getHashFamily() {
73+
knownOpenSSLConstantToHashFamilyType(this, result)
74+
or
75+
not knownOpenSSLConstantToHashFamilyType(this, _) and result = Crypto::OtherHashType()
76+
}
77+
78+
override string getRawHashAlgorithmName() { result = this.(Literal).getValue().toString() }
79+
80+
override int getFixedDigestLength() {
81+
this.(KnownOpenSSLHashAlgorithmConstant).getExplicitDigestLength() = result
82+
}
83+
}

0 commit comments

Comments
 (0)