Skip to content

Commit b1a1ce7

Browse files
committed
Fixed QL for QL findings
1 parent 6c71893 commit b1a1ce7

File tree

6 files changed

+123
-53
lines changed

6 files changed

+123
-53
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
import java
2-
import BouncyCastle.AlgorithmInstances
3-
import BouncyCastle.AlgorithmValueConsumers
42
import BouncyCastle.OperationInstances

java/ql/lib/experimental/quantum/BouncyCastle/AlgorithmInstances.qll

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import java
2-
import experimental.quantum.Language
3-
import AlgorithmValueConsumers
42
import OperationInstances
53
import FlowAnalysis
64

@@ -31,8 +29,8 @@ class EllipticCurveStringLiteralInstance extends Crypto::EllipticCurveInstance i
3129
}
3230

3331
/**
34-
* Represents an elliptic curve algorithm where the elliptic curve is implicitly
35-
* defined by the underlying type.
32+
* An elliptic curve algorithm where the elliptic curve is implicitly defined by
33+
* the underlying type.
3634
*/
3735
abstract class KnownEllipticCurveInstance extends Crypto::EllipticCurveInstance,
3836
Crypto::EllipticCurveConsumingAlgorithmInstance, Crypto::AlgorithmValueConsumer instanceof ClassInstanceExpr
@@ -51,7 +49,7 @@ abstract class KnownEllipticCurveInstance extends Crypto::EllipticCurveInstance,
5149
}
5250

5351
/**
54-
* Signature algorithms where the algorithm is implicitly defined by the type.
52+
* A signature algorithm where the algorithm is implicitly defined by the type.
5553
*/
5654
abstract class SignatureAlgorithmInstance extends Crypto::KeyOperationAlgorithmInstance,
5755
SignatureAlgorithmValueConsumer instanceof ClassInstanceExpr
@@ -100,10 +98,10 @@ abstract class KnownEllipticCurveSignatureAlgorithmInstance extends KnownEllipti
10098
}
10199

102100
/**
103-
* DSA and DSADigest signers.
101+
* A DSA or DSADigest signer.
104102
*/
105-
class DSASignatureAlgorithmInstance extends SignatureAlgorithmInstance instanceof ClassInstanceExpr {
106-
DSASignatureAlgorithmInstance() {
103+
class DsaSignatureAlgorithmInstance extends SignatureAlgorithmInstance instanceof ClassInstanceExpr {
104+
DsaSignatureAlgorithmInstance() {
107105
super.getConstructedType() instanceof Signers::Signer and
108106
super.getConstructedType().getName().matches("DSA%")
109107
}
@@ -114,7 +112,7 @@ class DSASignatureAlgorithmInstance extends SignatureAlgorithmInstance instanceo
114112
}
115113

116114
/**
117-
* Ed25519, Ed25519ph, and Ed25519ctx signers.
115+
* An Ed25519, Ed25519ph, or Ed25519ctx signer.
118116
*/
119117
class Ed25519SignatureAlgorithmInstance extends KnownEllipticCurveSignatureAlgorithmInstance instanceof ClassInstanceExpr
120118
{
@@ -131,7 +129,7 @@ class Ed25519SignatureAlgorithmInstance extends KnownEllipticCurveSignatureAlgor
131129
}
132130

133131
/**
134-
* Ed448 and Ed448ph signers.
132+
* An Ed448 or Ed448ph signer.
135133
*/
136134
class Ed448SignatureAlgorithmInstance extends KnownEllipticCurveSignatureAlgorithmInstance instanceof ClassInstanceExpr
137135
{
@@ -148,7 +146,7 @@ class Ed448SignatureAlgorithmInstance extends KnownEllipticCurveSignatureAlgorit
148146
}
149147

150148
/**
151-
* ECDSA signers.
149+
* An ECDSA signer.
152150
*
153151
* ECDSA curve parameters can be set in at least five ways:
154152
* - By using the `ECDomainParameters` class, which is passed to the constructor of the signer.
@@ -157,9 +155,9 @@ class Ed448SignatureAlgorithmInstance extends KnownEllipticCurveSignatureAlgorit
157155
* - By using the `ECNamedCurveSpec` class, which is passed to the constructor of the signer.
158156
* - By using the `ECParameterSpec` class, which is passed to the constructor of the signer.
159157
*/
160-
class ECDSASignatureAlgorithmInstance extends SignatureAlgorithmInstance instanceof ClassInstanceExpr
158+
class EcdsaSignatureAlgorithmInstance extends SignatureAlgorithmInstance instanceof ClassInstanceExpr
161159
{
162-
ECDSASignatureAlgorithmInstance() {
160+
EcdsaSignatureAlgorithmInstance() {
163161
super.getConstructedType() instanceof Signers::OneShotSigner and
164162
super.getConstructedType().getName().matches("ECDSA%")
165163
}
@@ -176,7 +174,7 @@ class ECDSASignatureAlgorithmInstance extends SignatureAlgorithmInstance instanc
176174
}
177175

178176
/**
179-
* An LMS or HSS stateful hash-based signer.
177+
* An LMS or HSS stateful, hash-based signer.
180178
*/
181179
class StatefulSignatureAlgorithmInstance extends SignatureAlgorithmInstance instanceof ClassInstanceExpr
182180
{
@@ -199,7 +197,7 @@ class StatefulSignatureAlgorithmInstance extends SignatureAlgorithmInstance inst
199197
}
200198

201199
/**
202-
* Key generation algorithms where the algorithm is implicitly defined by the
200+
* A key generation algorithm where the algorithm is implicitly defined by the
203201
* type.
204202
*/
205203
abstract class KeyGenerationAlgorithmInstance extends Crypto::KeyOperationAlgorithmInstance,
@@ -237,9 +235,8 @@ abstract class KeyGenerationAlgorithmInstance extends Crypto::KeyOperationAlgori
237235
}
238236

239237
/**
240-
* Represents an elliptic curve key generation algorithm where both the key
241-
* generation algorithm and elliptic curve are implicitly defined by the
242-
* underlying type.
238+
* An elliptic curve key generation algorithm where both the key generation
239+
* algorithm and elliptic curve are implicitly defined by the underlying type.
243240
*/
244241
abstract class KnownEllipticCurveKeyGenerationAlgorithmInstance extends KnownEllipticCurveInstance,
245242
KeyGenerationAlgorithmInstance
@@ -270,7 +267,7 @@ class Ed448KeyGenerationAlgorithmInstance extends KnownEllipticCurveKeyGeneratio
270267
}
271268

272269
/**
273-
* Represents a generic `ECKeyPairGenerator` instance.
270+
* A generic `ECKeyPairGenerator` instance.
274271
*/
275272
class GenericEllipticCurveKeyGenerationAlgorithmInstance extends KeyGenerationAlgorithmInstance,
276273
Crypto::EllipticCurveConsumingAlgorithmInstance instanceof ClassInstanceExpr
@@ -312,8 +309,8 @@ class GenericEllipticCurveKeyGenerationAlgorithmInstance extends KeyGenerationAl
312309
}
313310

314311
/**
315-
* Represents LMS or HSS key generation instances. The algorithm is implicitly
316-
* defined by the type.
312+
* An LMS or HSS key generation instances. The algorithm is implicitly defined
313+
* by the type.
317314
*/
318315
class StatefulSignatureKeyGenerationAlgorithmInstance extends KeyGenerationAlgorithmInstance instanceof ClassInstanceExpr
319316
{

java/ql/lib/experimental/quantum/BouncyCastle/AlgorithmValueConsumers.qll

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import java
2-
import experimental.quantum.Language
32
import AlgorithmInstances
43

54
abstract class HashAlgorithmValueConsumer extends Crypto::AlgorithmValueConsumer { }
@@ -9,9 +8,9 @@ abstract class CipherAlgorithmValueConsumer extends Crypto::AlgorithmValueConsum
98
abstract class EllipticCurveAlgorithmValueConsumer extends Crypto::AlgorithmValueConsumer { }
109

1110
class EllipticCurveStringLiteralArg extends EllipticCurveAlgorithmValueConsumer instanceof Expr {
12-
Params::ParametersInstantiation params;
13-
14-
EllipticCurveStringLiteralArg() { this = params.getAlgorithmArg() }
11+
EllipticCurveStringLiteralArg() {
12+
this = any(Params::ParametersInstantiation params).getAlgorithmArg()
13+
}
1514

1615
override Crypto::ConsumerInputDataFlowNode getInputNode() { result.asExpr() = this }
1716

@@ -21,7 +20,8 @@ class EllipticCurveStringLiteralArg extends EllipticCurveAlgorithmValueConsumer
2120
}
2221

2322
/**
24-
* Signature algorithms are implicitly defined by the constructor.
23+
* The AVC for a signature algorithm where the algorithm is implicitly defined
24+
* by the constructor.
2525
*/
2626
abstract class SignatureAlgorithmValueConsumer extends Crypto::AlgorithmValueConsumer {
2727
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() { result = this }
@@ -30,7 +30,8 @@ abstract class SignatureAlgorithmValueConsumer extends Crypto::AlgorithmValueCon
3030
}
3131

3232
/**
33-
* Key generation algorithms are implicitly defined by the constructor.
33+
* The AVC for a key generation algorithm where the algorithm is implicitly
34+
* defined by the constructor.
3435
*/
3536
abstract class KeyGenerationAlgorithmValueConsumer extends Crypto::AlgorithmValueConsumer {
3637
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() { result = this }

java/ql/lib/experimental/quantum/BouncyCastle/FlowAnalysis.qll

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import java
22
import semmle.code.java.dataflow.DataFlow
33
import experimental.quantum.Language
4-
import AlgorithmInstances
54
import AlgorithmValueConsumers
65

76
/**
@@ -223,7 +222,7 @@ module ParametersToInitFlowAnalysis<NewCallSig New, InitCallSig Init> {
223222
predicate isSink(DataFlow::Node sink) { exists(Init init | sink = init.getParametersInput()) }
224223

225224
/**
226-
* Pass-through for parameters created from other parameters.
225+
* A flow step for parameters created from other parameters.
227226
*
228227
* As an example, below we want to track the flow from the `X9ECParameters`
229228
* constructor call to the `keyPairGenerator.init()` call to be able to
@@ -269,6 +268,9 @@ module ParametersToInitFlowAnalysis<NewCallSig New, InitCallSig Init> {
269268

270269
private module ParametersToInitFlow = DataFlow::Global<ParametersToInitConfig>;
271270

271+
/**
272+
* Gets a parameter instantiation from a call to `init()`.
273+
*/
272274
New getParametersFromInit(
273275
Init init, ParametersToInitFlow::PathNode src, ParametersToInitFlow::PathNode sink
274276
) {
@@ -279,8 +281,8 @@ module ParametersToInitFlowAnalysis<NewCallSig New, InitCallSig Init> {
279281
}
280282

281283
/**
282-
* Model data flow from a key pair to the public and private components of the
283-
* key pair.
284+
* A model for data flow from a key pair to the public and private components of
285+
* the key pair.
284286
*/
285287
class KeyAdditionalFlowSteps extends MethodCall {
286288
KeyAdditionalFlowSteps() {
@@ -299,30 +301,27 @@ class KeyAdditionalFlowSteps extends MethodCall {
299301
}
300302

301303
/**
302-
* Model data flow from an ECDSA signature to the scalars r and s passed to
303-
* `verifySignature()`. The ECDSA signature is represented as a `BigInteger`
304+
* A model for data flow from an ECDSA signature to the scalars r and s passed
305+
* to `verifySignature()`. The ECDSA signature is represented as a `BigInteger`
304306
* array, where the first element is the scalar r and the second element is the
305307
* scalar s.
306308
*/
307-
class ECDSASignatureAdditionalFlowSteps extends ArrayAccess {
308-
ECDSASignatureAdditionalFlowSteps() {
309+
class EcdsaSignatureAdditionalFlowSteps extends ArrayAccess {
310+
EcdsaSignatureAdditionalFlowSteps() {
309311
this.getArray().getType().getName() = "BigInteger[]" and
310312
// It is reasonable to assume that the indices are integer literals
311313
this.getIndexExpr().(IntegerLiteral).getValue().toInt() = [0, 1]
312314
}
313315

314-
/**
315-
* The input node is the ECDSA signature represented as a `BigInteger` array.
316-
*/
317316
DataFlow::Node getInputNode() {
318-
// TODO: This should be the array node `this.getArray()`
317+
// The ECDSA signature is represented as a `BigInteger` array.
319318
result.asExpr() = this.getArray()
320319
}
321320

322-
/**
323-
* The output node is the `BigInteger` element accessed by this array access.
324-
*/
325-
DataFlow::Node getOutputNode() { result.asExpr() = this }
321+
DataFlow::Node getOutputNode() {
322+
// r or s is the `BigInteger` element accessed by this array access.
323+
result.asExpr() = this
324+
}
326325
}
327326

328327
predicate additionalFlowSteps(DataFlow::Node node1, DataFlow::Node node2) {
@@ -331,12 +330,15 @@ predicate additionalFlowSteps(DataFlow::Node node1, DataFlow::Node node2) {
331330
node2 = fs.getOutputNode()
332331
)
333332
or
334-
exists(ECDSASignatureAdditionalFlowSteps fs |
333+
exists(EcdsaSignatureAdditionalFlowSteps fs |
335334
node1 = fs.getInputNode() and
336335
node2 = fs.getOutputNode()
337336
)
338337
}
339338

339+
/**
340+
* An additional flow step for a cryptographic artifact.
341+
*/
340342
class ArtifactAdditionalFlowStep extends AdditionalFlowInputStep {
341343
DataFlow::Node output;
342344

@@ -347,7 +349,7 @@ class ArtifactAdditionalFlowStep extends AdditionalFlowInputStep {
347349

348350
module EllipticCurveStringLiteralToConsumer {
349351
/**
350-
* Flow from a known elliptic curve name to an elliptic curve algorithm consumer.
352+
* A flow from a known elliptic curve name to an elliptic curve algorithm consumer.
351353
*/
352354
module EllipticCurveStringLiteralToAlgorithmValueConsumerConfig implements DataFlow::ConfigSig {
353355
// NOTE: We do not reference EllipticCurveStringLiteralInstance directly

java/ql/lib/experimental/quantum/BouncyCastle/OperationInstances.qll

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ module Params {
77
/**
88
* A model of the `Parameters` class in Bouncy Castle.
99
*/
10-
class Parameters extends RefType {
10+
class Parameters extends Class {
1111
Parameters() {
1212
// Matches `org.bouncycastle.crypto.params`, `org.bouncycastle.asn1.x9`, etc.
1313
this.getPackage().getName().matches("org.bouncycastle.%") and
1414
this.getName().matches(["%Parameters", "%ParameterSpec"])
1515
}
1616
}
1717

18-
class Curve extends RefType {
18+
class Curve extends Class {
1919
Curve() {
2020
this.getPackage().getName() = "org.bouncycastle.math.ec" and
2121
this.getName().matches("ECCurve")
@@ -126,7 +126,7 @@ module Signers {
126126
* This class represents a BouncyCastle signer with a streaming API. For signers
127127
* with a one-shot API, see `OneShotSigner` below.
128128
*/
129-
class Signer extends RefType {
129+
class Signer extends Class {
130130
Signer() {
131131
this.getPackage().getName() =
132132
["org.bouncycastle.crypto.signers", "org.bouncycastle.pqc.crypto.lms"] and
@@ -167,7 +167,7 @@ module Signers {
167167
* is passed to either `generateSignature()` or `verifySignature`.).
168168
*/
169169
class OneShotSigner extends Signer {
170-
OneShotSigner() { this.getName().matches(["ECDSA%", "LMS%", "HSS%"]) }
170+
OneShotSigner() { this.getName().matches(["DSASigner", "ECDSA%", "LMS%", "HSS%"]) }
171171

172172
override Expr getMessageArg(MethodCall call) {
173173
// For ECDSA and LMS, the message is passed directly to `generateSignature()`.
@@ -300,7 +300,7 @@ module Generators {
300300
/**
301301
* A model of the `KeyGenerator` and `KeyPairGenerator` classes in Bouncy Castle.
302302
*/
303-
class KeyGenerator extends RefType {
303+
class KeyGenerator extends Class {
304304
Crypto::KeyArtifactType type;
305305

306306
KeyGenerator() {
@@ -422,4 +422,73 @@ module Generators {
422422
}
423423
}
424424

425-
module Modes { }
425+
module Modes {
426+
import FlowAnalysis
427+
428+
class BlockCipherMode extends Class {
429+
BlockCipherMode() {
430+
this.getPackage().getName() = "org.bouncycastle.crypto.modes" and
431+
this.getName().matches("%BlockCipher")
432+
}
433+
434+
MethodCall getAnInitCall() { result = this.getAMethodCall("init") }
435+
436+
MethodCall getAUseCall() {
437+
result =
438+
this.getAMethodCall([
439+
"processBlock", "processBlocks", "returnByte", "processBytes", "doFinal"
440+
])
441+
}
442+
443+
MethodCall getAMethodCall(string name) {
444+
result.getCallee().hasQualifiedName(this.getPackage().getName(), this.getName(), name)
445+
}
446+
}
447+
448+
/**
449+
* A block cipher mode instantiation.
450+
*
451+
* BouncyCastle algorithms are instantiated by calling the constructor of the
452+
* corresponding class, which also represents the algorithm instance.
453+
*/
454+
private class BlockCipherModeNewCall extends ClassInstanceExpr {
455+
BlockCipherModeNewCall() { this.getConstructedType() instanceof BlockCipherMode }
456+
457+
DataFlow::Node getParametersInput() { none() }
458+
459+
DataFlow::Node getEllipticCurveInput() { none() }
460+
}
461+
462+
/**
463+
* A call to a block cipher mode `init()` method.
464+
*
465+
* The type is instantiated by a constructor call and initialized by a call to
466+
* `init()` which takes a single `KeyGenerationParameters` argument.
467+
*/
468+
private class BlockCipherModeInitCall extends MethodCall {
469+
BlockCipherMode mode;
470+
471+
BlockCipherModeInitCall() { this = mode.getAnInitCall() }
472+
473+
Crypto::ConsumerInputDataFlowNode getKeySizeConsumer() { none() }
474+
475+
// The `CipherParameters` argument used to configure the cipher.
476+
DataFlow::Node getParametersInput() { result.asExpr() = this.getArgument(1) }
477+
}
478+
479+
/**
480+
* A `processX()`, `returnByte()` or `doFinal()` method call to encrypt or
481+
* decrypt data.
482+
*/
483+
private class BlockCipherModeUseCall extends MethodCall {
484+
BlockCipherMode mode;
485+
486+
BlockCipherModeUseCall() { this = mode.getAUseCall() }
487+
488+
predicate isIntermediate() { not this.getCallee().getName() = "doFinal" }
489+
}
490+
491+
module BlockCipherModeFlow =
492+
NewToInitToUseFlowAnalysis<BlockCipherModeNewCall, BlockCipherModeInitCall,
493+
BlockCipherModeUseCall>;
494+
}

0 commit comments

Comments
 (0)