Skip to content

Commit 39583ab

Browse files
committed
Crypto: Update KnownAlgoirthmConstants to make a distinction between calls that return a known algorithm and calls that operate on a known algorithm. update KnownAlgorithmConstants to correct algType for signature algorithms. Update all instances and prior uses of the old mechanic to KnownAlgorithmConstants.
1 parent 0ef17ba commit 39583ab

10 files changed

+203
-128
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ private import PaddingAlgorithmInstance
1414
*/
1515
module KnownOpenSSLAlgorithmToAlgorithmValueConsumerConfig implements DataFlow::ConfigSig {
1616
predicate isSource(DataFlow::Node source) {
17-
source.asExpr() instanceof KnownOpenSSLAlgorithmConstant
17+
source.asExpr() instanceof KnownOpenSSLAlgorithmExpr and
18+
// No need to flow direct operations to AVCs
19+
not source.asExpr() instanceof OpenSSLDirectAlgorithmOperationCall
1820
}
1921

2022
predicate isSink(DataFlow::Node sink) {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgor
77
private import AlgToAVCFlow
88

99
/**
10-
* Given a `KnownOpenSSLBlockModeAlgorithmConstant`, converts this to a block family type.
10+
* Given a `KnownOpenSSLBlockModeAlgorithmExpr`, converts this to a block family type.
1111
* Does not bind if there is no mapping (no mapping to 'unknown' or 'other').
1212
*/
1313
predicate knownOpenSSLConstantToBlockModeFamilyType(
14-
KnownOpenSSLBlockModeAlgorithmConstant e, Crypto::TBlockCipherModeOfOperationType type
14+
KnownOpenSSLBlockModeAlgorithmExpr e, Crypto::TBlockCipherModeOfOperationType type
1515
) {
1616
exists(string name |
17-
name = e.getNormalizedName() and
17+
name = e.(KnownOpenSSLAlgorithmExpr).getNormalizedName() and
1818
(
1919
name.matches("CBC") and type instanceof Crypto::CBC
2020
or
@@ -40,7 +40,7 @@ predicate knownOpenSSLConstantToBlockModeFamilyType(
4040
}
4141

4242
class KnownOpenSSLBlockModeConstantAlgorithmInstance extends OpenSSLAlgorithmInstance,
43-
Crypto::ModeOfOperationAlgorithmInstance instanceof KnownOpenSSLBlockModeAlgorithmConstant
43+
Crypto::ModeOfOperationAlgorithmInstance instanceof KnownOpenSSLBlockModeAlgorithmExpr
4444
{
4545
OpenSSLAlgorithmValueConsumer getterCall;
4646

@@ -49,7 +49,7 @@ class KnownOpenSSLBlockModeConstantAlgorithmInstance extends OpenSSLAlgorithmIns
4949
// 1) The source is a literal and flows to a getter, then we know we have an instance
5050
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
5151
// Possibility 1:
52-
this instanceof Literal and
52+
this instanceof OpenSSLAlgorithmLiteral and
5353
exists(DataFlow::Node src, DataFlow::Node sink |
5454
// Sink is an argument to a CipherGetterCall
5555
sink = getterCall.(OpenSSLAlgorithmValueConsumer).getInputNode() and
@@ -60,7 +60,8 @@ class KnownOpenSSLBlockModeConstantAlgorithmInstance extends OpenSSLAlgorithmIns
6060
)
6161
or
6262
// Possibility 2:
63-
this instanceof DirectAlgorithmValueConsumer and getterCall = this
63+
this instanceof OpenSSLAlgorithmCall and
64+
getterCall = this
6465
}
6566

6667
override Crypto::TBlockCipherModeOfOperationType getModeType() {

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ private import AlgToAVCFlow
1010
private import BlockAlgorithmInstance
1111

1212
/**
13-
* Given a `KnownOpenSSLCipherAlgorithmConstant`, converts this to a cipher family type.
13+
* Given a `KnownOpenSSLCipherAlgorithmExpr`, converts this to a cipher family type.
1414
* Does not bind if there is no mapping (no mapping to 'unknown' or 'other').
1515
*/
1616
predicate knownOpenSSLConstantToCipherFamilyType(
17-
KnownOpenSSLCipherAlgorithmConstant e, Crypto::KeyOpAlg::TAlgorithm type
17+
KnownOpenSSLCipherAlgorithmExpr e, Crypto::KeyOpAlg::TAlgorithm type
1818
) {
1919
exists(string name |
20-
name = e.getNormalizedName() and
20+
name = e.(KnownOpenSSLAlgorithmExpr).getNormalizedName() and
2121
(
2222
name.matches("AES%") and type = KeyOpAlg::TSymmetricCipher(KeyOpAlg::AES())
2323
or
@@ -65,7 +65,7 @@ predicate knownOpenSSLConstantToCipherFamilyType(
6565
}
6666

6767
class KnownOpenSSLCipherConstantAlgorithmInstance extends OpenSSLAlgorithmInstance,
68-
Crypto::KeyOperationAlgorithmInstance instanceof KnownOpenSSLCipherAlgorithmConstant
68+
Crypto::KeyOperationAlgorithmInstance instanceof KnownOpenSSLCipherAlgorithmExpr
6969
{
7070
OpenSSLAlgorithmValueConsumer getterCall;
7171

@@ -74,7 +74,7 @@ class KnownOpenSSLCipherConstantAlgorithmInstance extends OpenSSLAlgorithmInstan
7474
// 1) The source is a literal and flows to a getter, then we know we have an instance
7575
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
7676
// Possibility 1:
77-
this instanceof Literal and
77+
this instanceof OpenSSLAlgorithmLiteral and
7878
exists(DataFlow::Node src, DataFlow::Node sink |
7979
// Sink is an argument to a CipherGetterCall
8080
sink = getterCall.(OpenSSLAlgorithmValueConsumer).getInputNode() and
@@ -85,7 +85,8 @@ class KnownOpenSSLCipherConstantAlgorithmInstance extends OpenSSLAlgorithmInstan
8585
)
8686
or
8787
// Possibility 2:
88-
this instanceof DirectAlgorithmValueConsumer and getterCall = this
88+
this instanceof OpenSSLAlgorithmCall and
89+
getterCall = this
8990
}
9091

9192
override Crypto::ModeOfOperationAlgorithmInstance getModeOfOperationAlgorithm() {
@@ -109,7 +110,7 @@ class KnownOpenSSLCipherConstantAlgorithmInstance extends OpenSSLAlgorithmInstan
109110
}
110111

111112
override int getKeySizeFixed() {
112-
this.(KnownOpenSSLCipherAlgorithmConstant).getExplicitKeySize() = result
113+
this.(KnownOpenSSLCipherAlgorithmExpr).getExplicitKeySize() = result
113114
}
114115

115116
override Crypto::KeyOpAlg::Algorithm getAlgorithmType() {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.DirectAlgori
77
private import AlgToAVCFlow
88

99
class KnownOpenSSLEllipticCurveConstantAlgorithmInstance extends OpenSSLAlgorithmInstance,
10-
Crypto::EllipticCurveInstance instanceof KnownOpenSSLEllipticCurveAlgorithmConstant
10+
Crypto::EllipticCurveInstance instanceof KnownOpenSSLEllipticCurveAlgorithmExpr
1111
{
1212
OpenSSLAlgorithmValueConsumer getterCall;
1313

@@ -16,7 +16,7 @@ class KnownOpenSSLEllipticCurveConstantAlgorithmInstance extends OpenSSLAlgorith
1616
// 1) The source is a literal and flows to a getter, then we know we have an instance
1717
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
1818
// Possibility 1:
19-
this instanceof Literal and
19+
this instanceof OpenSSLAlgorithmLiteral and
2020
exists(DataFlow::Node src, DataFlow::Node sink |
2121
// Sink is an argument to a CipherGetterCall
2222
sink = getterCall.getInputNode() and
@@ -27,7 +27,8 @@ class KnownOpenSSLEllipticCurveConstantAlgorithmInstance extends OpenSSLAlgorith
2727
)
2828
or
2929
// Possibility 2:
30-
this instanceof DirectAlgorithmValueConsumer and getterCall = this
30+
this instanceof OpenSSLAlgorithmCall and
31+
getterCall = this
3132
}
3233

3334
override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall }
@@ -43,11 +44,11 @@ class KnownOpenSSLEllipticCurveConstantAlgorithmInstance extends OpenSSLAlgorith
4344
}
4445

4546
override string getParsedEllipticCurveName() {
46-
result = this.(KnownOpenSSLEllipticCurveAlgorithmConstant).getNormalizedName()
47+
result = this.(KnownOpenSSLAlgorithmExpr).getNormalizedName()
4748
}
4849

4950
override int getKeySize() {
50-
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(this.(KnownOpenSSLEllipticCurveAlgorithmConstant)
51+
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(this.(KnownOpenSSLAlgorithmExpr)
5152
.getNormalizedName(), result, _)
5253
}
5354
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ private import experimental.quantum.OpenSSL.AlgorithmInstances.OpenSSLAlgorithmI
66
private import AlgToAVCFlow
77

88
predicate knownOpenSSLConstantToHashFamilyType(
9-
KnownOpenSSLHashAlgorithmConstant e, Crypto::THashType type
9+
KnownOpenSSLHashAlgorithmExpr e, Crypto::THashType type
1010
) {
1111
exists(string name |
12-
name = e.getNormalizedName() and
12+
name = e.(KnownOpenSSLAlgorithmExpr).getNormalizedName() and
1313
(
1414
name.matches("BLAKE2B") and type instanceof Crypto::BLAKE2B
1515
or
@@ -45,7 +45,7 @@ predicate knownOpenSSLConstantToHashFamilyType(
4545
}
4646

4747
class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance,
48-
Crypto::HashAlgorithmInstance instanceof KnownOpenSSLHashAlgorithmConstant
48+
Crypto::HashAlgorithmInstance instanceof KnownOpenSSLHashAlgorithmExpr
4949
{
5050
OpenSSLAlgorithmValueConsumer getterCall;
5151

@@ -54,7 +54,7 @@ class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance
5454
// 1) The source is a literal and flows to a getter, then we know we have an instance
5555
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
5656
// Possibility 1:
57-
this instanceof Literal and
57+
this instanceof OpenSSLAlgorithmLiteral and
5858
exists(DataFlow::Node src, DataFlow::Node sink |
5959
// Sink is an argument to a CipherGetterCall
6060
sink = getterCall.(OpenSSLAlgorithmValueConsumer).getInputNode() and
@@ -65,7 +65,8 @@ class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance
6565
)
6666
or
6767
// Possibility 2:
68-
this instanceof DirectAlgorithmValueConsumer and getterCall = this
68+
this instanceof OpenSSLAlgorithmCall and
69+
getterCall = this
6970
}
7071

7172
override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall }
@@ -83,6 +84,6 @@ class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance
8384
}
8485

8586
override int getFixedDigestLength() {
86-
this.(KnownOpenSSLHashAlgorithmConstant).getExplicitDigestLength() = result
87+
this.(KnownOpenSSLHashAlgorithmExpr).getExplicitDigestLength() = result
8788
}
8889
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ private import experimental.quantum.OpenSSL.AlgorithmInstances.OpenSSLAlgorithmI
66
private import AlgToAVCFlow
77

88
predicate knownOpenSSLConstantToKeyAgreementFamilyType(
9-
KnownOpenSSLKeyAgreementAlgorithmConstant e, Crypto::TKeyAgreementType type
9+
KnownOpenSSLKeyAgreementAlgorithmExpr e, Crypto::TKeyAgreementType type
1010
) {
1111
exists(string name |
12-
name = e.getNormalizedName() and
12+
name = e.(KnownOpenSSLAlgorithmExpr).getNormalizedName() and
1313
(
1414
name = "ECDH" and type = Crypto::ECDH()
1515
or
@@ -23,7 +23,7 @@ predicate knownOpenSSLConstantToKeyAgreementFamilyType(
2323
}
2424

2525
class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance,
26-
Crypto::KeyAgreementAlgorithmInstance instanceof KnownOpenSSLKeyAgreementAlgorithmConstant
26+
Crypto::KeyAgreementAlgorithmInstance instanceof KnownOpenSSLKeyAgreementAlgorithmExpr
2727
{
2828
OpenSSLAlgorithmValueConsumer getterCall;
2929

@@ -32,7 +32,7 @@ class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance
3232
// 1) The source is a literal and flows to a getter, then we know we have an instance
3333
// 2) The source is a KnownOpenSSLAlgorithm is call, and we know we have an instance immediately from that
3434
// Possibility 1:
35-
this instanceof Literal and
35+
this instanceof OpenSSLAlgorithmLiteral and
3636
exists(DataFlow::Node src, DataFlow::Node sink |
3737
// Sink is an argument to a CipherGetterCall
3838
sink = getterCall.getInputNode() and
@@ -43,7 +43,9 @@ class KnownOpenSSLHashConstantAlgorithmInstance extends OpenSSLAlgorithmInstance
4343
)
4444
or
4545
// Possibility 2:
46-
this instanceof DirectAlgorithmValueConsumer and getterCall = this
46+
this instanceof OpenSSLAlgorithmCall and
47+
this instanceof DirectAlgorithmValueConsumer and
48+
getterCall = this
4749
}
4850

4951
override OpenSSLAlgorithmValueConsumer getAVC() { result = getterCall }

0 commit comments

Comments
 (0)