From c5cf0ffa75ba55201346f786fe6db8cb4f610223 Mon Sep 17 00:00:00 2001 From: Mark C Date: Wed, 1 Oct 2025 11:55:51 +0100 Subject: [PATCH 1/2] added java cryptographic check queries --- .../Analysis/InsecureNonceGeneration.ql | 23 ++++++++++++++ .../quantum/Analysis/NonAESGCMCipher.ql | 25 ++++++++++++++++ .../quantum/Analysis/NonceReuse.ql | 18 +++++++++++ .../quantum/Analysis/WeakAsymmetric.ql | 25 ++++++++++++++++ .../quantum/Analysis/WeakBlockModes.ql | 30 +++++++++++++++++++ .../quantum/Analysis/WeakHashing.ql | 20 +++++++++++++ .../quantum/Analysis/WeakKDFIterationCount.ql | 21 +++++++++++++ .../quantum/Analysis/WeakKDFKeySize.ql | 21 +++++++++++++ .../experimental/quantum/Analysis/WeakRSA.ql | 25 ++++++++++++++++ .../quantum/Analysis/WeakSymmetricCiphers.ql | 20 +++++++++++++ 10 files changed, 228 insertions(+) create mode 100644 java/ql/src/experimental/quantum/Analysis/InsecureNonceGeneration.ql create mode 100644 java/ql/src/experimental/quantum/Analysis/NonAESGCMCipher.ql create mode 100644 java/ql/src/experimental/quantum/Analysis/NonceReuse.ql create mode 100644 java/ql/src/experimental/quantum/Analysis/WeakAsymmetric.ql create mode 100644 java/ql/src/experimental/quantum/Analysis/WeakBlockModes.ql create mode 100644 java/ql/src/experimental/quantum/Analysis/WeakHashing.ql create mode 100644 java/ql/src/experimental/quantum/Analysis/WeakKDFIterationCount.ql create mode 100644 java/ql/src/experimental/quantum/Analysis/WeakKDFKeySize.ql create mode 100644 java/ql/src/experimental/quantum/Analysis/WeakRSA.ql create mode 100644 java/ql/src/experimental/quantum/Analysis/WeakSymmetricCiphers.ql diff --git a/java/ql/src/experimental/quantum/Analysis/InsecureNonceGeneration.ql b/java/ql/src/experimental/quantum/Analysis/InsecureNonceGeneration.ql new file mode 100644 index 000000000000..792287445f07 --- /dev/null +++ b/java/ql/src/experimental/quantum/Analysis/InsecureNonceGeneration.ql @@ -0,0 +1,23 @@ +/** + * @name Insecure nonce at a cipher operation + * @id java/quantum/insecure-nonce + * @description A nonce is generated from a source that is not secure. This can lead to + * vulnerabilities such as replay attacks or key recovery. + * @kind problem + * @problem.severity error + * @security.severity low + * @precision high + * @tags quantum + * experimental + */ + +import experimental.quantum.Language + +predicate isInsecureNonceSource(Crypto::NonceArtifactNode n, Crypto::NodeBase src) { + src = n.getSourceNode() and + not src.asElement() instanceof SecureRandomnessInstance +} + +from Crypto::KeyOperationNode op, Crypto::NodeBase src +where isInsecureNonceSource(op.getANonce(), src) +select op, "Operation uses insecure nonce source $@", src, src.toString() \ No newline at end of file diff --git a/java/ql/src/experimental/quantum/Analysis/NonAESGCMCipher.ql b/java/ql/src/experimental/quantum/Analysis/NonAESGCMCipher.ql new file mode 100644 index 000000000000..65b00f94d739 --- /dev/null +++ b/java/ql/src/experimental/quantum/Analysis/NonAESGCMCipher.ql @@ -0,0 +1,25 @@ +/** + * @name Cipher not AES-GCM mode + * @id java/quantum/non-aes-gcm + * @description An AES cipher is in use without GCM + * @kind problem + * @problem.severity error + * @security.severity low + * @precision high + * @tags quantum + * experimental + */ + +import experimental.quantum.Language + +class NonAESGCMAlgorithmNode extends Crypto::KeyOperationAlgorithmNode { + NonAESGCMAlgorithmNode() { + this.getAlgorithmType() = Crypto::KeyOpAlg::TSymmetricCipher(Crypto::KeyOpAlg::AES()) and + this.getModeOfOperation().getModeType() != Crypto::KeyOpAlg::GCM() + } +} + +from Crypto::KeyOperationNode op, Crypto::KeyOperationOutputNode codeNode +where op.getAKnownAlgorithm() instanceof NonAESGCMAlgorithmNode and + codeNode = op.getAnOutputArtifact() +select op, "Non-AES-GCM instance." \ No newline at end of file diff --git a/java/ql/src/experimental/quantum/Analysis/NonceReuse.ql b/java/ql/src/experimental/quantum/Analysis/NonceReuse.ql new file mode 100644 index 000000000000..7f92123fe2e5 --- /dev/null +++ b/java/ql/src/experimental/quantum/Analysis/NonceReuse.ql @@ -0,0 +1,18 @@ +/** + * @name Reuse of cryptographic nonce + * @description Reuse of nonce in cryptographic operations can lead to vulnerabilities. + * @id java/quantum/reused-nonce + * @kind problem + * @problem.severity error + * @security.severity low + * @precision medium + * @tags quantum + * experimental + */ + +import java +import ArtifactReuse + +from Crypto::NonceArtifactNode nonce1, Crypto::NonceArtifactNode nonce2 +where isArtifactReuse(nonce1, nonce2) +select nonce1, "Reuse with nonce $@", nonce2, nonce2.toString() \ No newline at end of file diff --git a/java/ql/src/experimental/quantum/Analysis/WeakAsymmetric.ql b/java/ql/src/experimental/quantum/Analysis/WeakAsymmetric.ql new file mode 100644 index 000000000000..531b7e01d60b --- /dev/null +++ b/java/ql/src/experimental/quantum/Analysis/WeakAsymmetric.ql @@ -0,0 +1,25 @@ +/** + * @name Weak Asymetric Key Size + * @id java/quantum/weak-asymmetric-key-size + * @description An asymmetric cipher with a short key size is in use + * @kind problem + * @problem.severity error + * @security.severity low + * @precision high + * @tags quantum + * experimental + */ + +import java +import experimental.quantum.Language + +from Crypto::KeyOperationAlgorithmNode op, DataFlow::Node configSrc, int keySize, string algName +where + keySize = op.getKeySizeFixed() and + keySize < 2048 and + algName = op.getAlgorithmName() and + // Can't be an elliptic curve + not Crypto::isEllipticCurveAlgorithmName(algName) +select op, + "Use of weak asymmetric key size (int bits)" + keySize.toString() + " for algorithm " + + algName.toString() + " at config source $@", configSrc, configSrc.toString() \ No newline at end of file diff --git a/java/ql/src/experimental/quantum/Analysis/WeakBlockModes.ql b/java/ql/src/experimental/quantum/Analysis/WeakBlockModes.ql new file mode 100644 index 000000000000..dec3296a38b2 --- /dev/null +++ b/java/ql/src/experimental/quantum/Analysis/WeakBlockModes.ql @@ -0,0 +1,30 @@ +/** + * @name Weak AES Block mode + * @id java/quantum/weak-block-modes + * @description An AES cipher is in use with an insecure block mode + * @kind problem + * @problem.severity error + * @security.severity low + * @precision high + * @tags quantum + * experimental + */ + +import java +import experimental.quantum.Language + +class WeakAESBlockModeAlgNode extends Crypto::KeyOperationAlgorithmNode { + WeakAESBlockModeAlgNode() { + this.getAlgorithmType() = Crypto::KeyOpAlg::TSymmetricCipher(Crypto::KeyOpAlg::AES()) and + (this.getModeOfOperation().getModeType() = Crypto::KeyOpAlg::ECB() or + this.getModeOfOperation().getModeType() = Crypto::KeyOpAlg::CFB() or + this.getModeOfOperation().getModeType() = Crypto::KeyOpAlg::OFB() or + this.getModeOfOperation().getModeType() = Crypto::KeyOpAlg::CTR() + ) + } +} + +from Crypto::KeyOperationNode op, Crypto::KeyOperationOutputNode codeNode +where op.getAKnownAlgorithm() instanceof WeakAESBlockModeAlgNode and + codeNode = op.getAnOutputArtifact() +select op, "Weak AES block mode instance." diff --git a/java/ql/src/experimental/quantum/Analysis/WeakHashing.ql b/java/ql/src/experimental/quantum/Analysis/WeakHashing.ql new file mode 100644 index 000000000000..8a725ec6a5ee --- /dev/null +++ b/java/ql/src/experimental/quantum/Analysis/WeakHashing.ql @@ -0,0 +1,20 @@ +/** + * @name Weak hashes + * @description Finds uses of cryptographic hashing algorithms that are unapproved or otherwise weak. + * @id java/quantum/slices/weak-hashes + * @kind problem + * @problem.severity error + * @security.severity low + * @precision high + * @tags external/cwe/cwe-327 + */ + +import java +import experimental.quantum.Language + +from Crypto::HashAlgorithmNode alg, string name, string msg +where + name = alg.getAlgorithmName() and + not name in ["SHA256", "SHA384", "SHA512", "SHA-256", "SHA-384", "SHA-512"] and + msg = "Use of unapproved hash algorithm or API " + name + "." +select alg, msg diff --git a/java/ql/src/experimental/quantum/Analysis/WeakKDFIterationCount.ql b/java/ql/src/experimental/quantum/Analysis/WeakKDFIterationCount.ql new file mode 100644 index 000000000000..c902b286b195 --- /dev/null +++ b/java/ql/src/experimental/quantum/Analysis/WeakKDFIterationCount.ql @@ -0,0 +1,21 @@ +/** + * @name Weak known key derivation function iteration count + * @description Detects key derivation operations with a known weak iteration count. + * @id java/quantum/weak-kdf-iteration-count + * @kind problem + * @problem.severity error + * @security.severity low + * @precision high + * @tags quantum + * experimental + */ + +import java +import experimental.quantum.Language + +from Crypto::KeyDerivationOperationNode op, Literal l +where + op.getIterationCount().asElement() = l and + l.getValue().toInt() < 100000 +select op, "Key derivation operation configures iteration count below 100k: $@", l, + l.getValue().toString() \ No newline at end of file diff --git a/java/ql/src/experimental/quantum/Analysis/WeakKDFKeySize.ql b/java/ql/src/experimental/quantum/Analysis/WeakKDFKeySize.ql new file mode 100644 index 000000000000..0161fc1186d6 --- /dev/null +++ b/java/ql/src/experimental/quantum/Analysis/WeakKDFKeySize.ql @@ -0,0 +1,21 @@ +/** + * @name Weak known key derivation function output length + * @description Detects key derivation operations with a known weak output length + * @id java/quantum/weak-kdf-iteration-count + * @kind problem + * @problem.severity error + * @security.severity low + * @precision high + * @tags quantum + * experimental + */ + +import java +import experimental.quantum.Language + +from Crypto::KeyDerivationOperationNode op, Literal l +where + op.getOutputKeySize().asElement() = l and + l.getValue().toInt() < 256 +select op, "Key derivation operation configures output key length below 256: $@", l, + l.getValue().toString() \ No newline at end of file diff --git a/java/ql/src/experimental/quantum/Analysis/WeakRSA.ql b/java/ql/src/experimental/quantum/Analysis/WeakRSA.ql new file mode 100644 index 000000000000..3bc15529363d --- /dev/null +++ b/java/ql/src/experimental/quantum/Analysis/WeakRSA.ql @@ -0,0 +1,25 @@ +/** + * @name Cipher is Weak RSA Implementation + * @id java/quantum/weak-rsa + * @description RSA with a key length <2048 found + * @kind problem + * @problem.severity error + * @security.severity low + * @precision high + * @tags quantum + * experimental + */ + +import experimental.quantum.Language + +class WeakRSAAlgorithmNode extends Crypto::KeyOperationAlgorithmNode { + WeakRSAAlgorithmNode() { + this.getAlgorithmType() = Crypto::KeyOpAlg::TAsymmetricCipher(Crypto::KeyOpAlg::RSA()) and + this.getKeySizeFixed() < 2048 + } +} + +from Crypto::KeyOperationNode op, string message +where op.getAKnownAlgorithm() instanceof WeakRSAAlgorithmNode and + message = "Weak RSA instance found with key length <2048" +select op, message diff --git a/java/ql/src/experimental/quantum/Analysis/WeakSymmetricCiphers.ql b/java/ql/src/experimental/quantum/Analysis/WeakSymmetricCiphers.ql new file mode 100644 index 000000000000..3ab18c85e54e --- /dev/null +++ b/java/ql/src/experimental/quantum/Analysis/WeakSymmetricCiphers.ql @@ -0,0 +1,20 @@ +/** + * @name Weak symmetric ciphers + * @description Finds uses of cryptographic symmetric cipher algorithms that are unapproved or otherwise weak. + * @id java/quantum/slices/weak-ciphers + * @kind problem + * @problem.severity error + * @security.severity low + * @precision high + * @tags external/cwe/cwe-327 + */ + +import java +import experimental.quantum.Language + +from Crypto::KeyOperationAlgorithmNode alg, string name, string msg +where + name = alg.getAlgorithmName() and + name in ["DES", "TripleDES", "DoubleDES", "RC2", "RC4", "IDEA", "Blowfish"] and + msg = "Use of unapproved symmetric cipher algorithm or API: " + name + "." +select alg, msg \ No newline at end of file From f38ab45e94fc2ed969876f490afaa04909a9ad6b Mon Sep 17 00:00:00 2001 From: Mark C Date: Wed, 1 Oct 2025 17:49:45 +0100 Subject: [PATCH 2/2] removed all @security.severity ratings to keep the main impartial --- .../src/experimental/quantum/Analysis/InsecureNonceGeneration.ql | 1 - java/ql/src/experimental/quantum/Analysis/NonAESGCMCipher.ql | 1 - java/ql/src/experimental/quantum/Analysis/NonceReuse.ql | 1 - .../experimental/quantum/Analysis/UnknownKDFIterationCount.ql | 1 - java/ql/src/experimental/quantum/Analysis/WeakAsymmetric.ql | 1 - java/ql/src/experimental/quantum/Analysis/WeakBlockModes.ql | 1 - java/ql/src/experimental/quantum/Analysis/WeakHashing.ql | 1 - .../src/experimental/quantum/Analysis/WeakKDFIterationCount.ql | 1 - java/ql/src/experimental/quantum/Analysis/WeakKDFKeySize.ql | 1 - java/ql/src/experimental/quantum/Analysis/WeakRSA.ql | 1 - .../ql/src/experimental/quantum/Analysis/WeakSymmetricCiphers.ql | 1 - 11 files changed, 11 deletions(-) diff --git a/java/ql/src/experimental/quantum/Analysis/InsecureNonceGeneration.ql b/java/ql/src/experimental/quantum/Analysis/InsecureNonceGeneration.ql index 792287445f07..2514f6b384a4 100644 --- a/java/ql/src/experimental/quantum/Analysis/InsecureNonceGeneration.ql +++ b/java/ql/src/experimental/quantum/Analysis/InsecureNonceGeneration.ql @@ -5,7 +5,6 @@ * vulnerabilities such as replay attacks or key recovery. * @kind problem * @problem.severity error - * @security.severity low * @precision high * @tags quantum * experimental diff --git a/java/ql/src/experimental/quantum/Analysis/NonAESGCMCipher.ql b/java/ql/src/experimental/quantum/Analysis/NonAESGCMCipher.ql index 65b00f94d739..659ae4d02866 100644 --- a/java/ql/src/experimental/quantum/Analysis/NonAESGCMCipher.ql +++ b/java/ql/src/experimental/quantum/Analysis/NonAESGCMCipher.ql @@ -4,7 +4,6 @@ * @description An AES cipher is in use without GCM * @kind problem * @problem.severity error - * @security.severity low * @precision high * @tags quantum * experimental diff --git a/java/ql/src/experimental/quantum/Analysis/NonceReuse.ql b/java/ql/src/experimental/quantum/Analysis/NonceReuse.ql index 7f92123fe2e5..f185e48d6b2b 100644 --- a/java/ql/src/experimental/quantum/Analysis/NonceReuse.ql +++ b/java/ql/src/experimental/quantum/Analysis/NonceReuse.ql @@ -4,7 +4,6 @@ * @id java/quantum/reused-nonce * @kind problem * @problem.severity error - * @security.severity low * @precision medium * @tags quantum * experimental diff --git a/java/ql/src/experimental/quantum/Analysis/UnknownKDFIterationCount.ql b/java/ql/src/experimental/quantum/Analysis/UnknownKDFIterationCount.ql index 21bca11cc1af..db22bf4a3698 100644 --- a/java/ql/src/experimental/quantum/Analysis/UnknownKDFIterationCount.ql +++ b/java/ql/src/experimental/quantum/Analysis/UnknownKDFIterationCount.ql @@ -4,7 +4,6 @@ * @id java/quantum/unknown-kdf-iteration-count * @kind problem * @precision medium - * @severity warning * @tags quantum * experimental */ diff --git a/java/ql/src/experimental/quantum/Analysis/WeakAsymmetric.ql b/java/ql/src/experimental/quantum/Analysis/WeakAsymmetric.ql index 531b7e01d60b..9ae4ea9130e6 100644 --- a/java/ql/src/experimental/quantum/Analysis/WeakAsymmetric.ql +++ b/java/ql/src/experimental/quantum/Analysis/WeakAsymmetric.ql @@ -4,7 +4,6 @@ * @description An asymmetric cipher with a short key size is in use * @kind problem * @problem.severity error - * @security.severity low * @precision high * @tags quantum * experimental diff --git a/java/ql/src/experimental/quantum/Analysis/WeakBlockModes.ql b/java/ql/src/experimental/quantum/Analysis/WeakBlockModes.ql index dec3296a38b2..3a2d97659153 100644 --- a/java/ql/src/experimental/quantum/Analysis/WeakBlockModes.ql +++ b/java/ql/src/experimental/quantum/Analysis/WeakBlockModes.ql @@ -4,7 +4,6 @@ * @description An AES cipher is in use with an insecure block mode * @kind problem * @problem.severity error - * @security.severity low * @precision high * @tags quantum * experimental diff --git a/java/ql/src/experimental/quantum/Analysis/WeakHashing.ql b/java/ql/src/experimental/quantum/Analysis/WeakHashing.ql index 8a725ec6a5ee..74a3a19d472b 100644 --- a/java/ql/src/experimental/quantum/Analysis/WeakHashing.ql +++ b/java/ql/src/experimental/quantum/Analysis/WeakHashing.ql @@ -4,7 +4,6 @@ * @id java/quantum/slices/weak-hashes * @kind problem * @problem.severity error - * @security.severity low * @precision high * @tags external/cwe/cwe-327 */ diff --git a/java/ql/src/experimental/quantum/Analysis/WeakKDFIterationCount.ql b/java/ql/src/experimental/quantum/Analysis/WeakKDFIterationCount.ql index c902b286b195..3fd84c9ecc41 100644 --- a/java/ql/src/experimental/quantum/Analysis/WeakKDFIterationCount.ql +++ b/java/ql/src/experimental/quantum/Analysis/WeakKDFIterationCount.ql @@ -4,7 +4,6 @@ * @id java/quantum/weak-kdf-iteration-count * @kind problem * @problem.severity error - * @security.severity low * @precision high * @tags quantum * experimental diff --git a/java/ql/src/experimental/quantum/Analysis/WeakKDFKeySize.ql b/java/ql/src/experimental/quantum/Analysis/WeakKDFKeySize.ql index 0161fc1186d6..789d7952997a 100644 --- a/java/ql/src/experimental/quantum/Analysis/WeakKDFKeySize.ql +++ b/java/ql/src/experimental/quantum/Analysis/WeakKDFKeySize.ql @@ -4,7 +4,6 @@ * @id java/quantum/weak-kdf-iteration-count * @kind problem * @problem.severity error - * @security.severity low * @precision high * @tags quantum * experimental diff --git a/java/ql/src/experimental/quantum/Analysis/WeakRSA.ql b/java/ql/src/experimental/quantum/Analysis/WeakRSA.ql index 3bc15529363d..5ed405fe3d95 100644 --- a/java/ql/src/experimental/quantum/Analysis/WeakRSA.ql +++ b/java/ql/src/experimental/quantum/Analysis/WeakRSA.ql @@ -4,7 +4,6 @@ * @description RSA with a key length <2048 found * @kind problem * @problem.severity error - * @security.severity low * @precision high * @tags quantum * experimental diff --git a/java/ql/src/experimental/quantum/Analysis/WeakSymmetricCiphers.ql b/java/ql/src/experimental/quantum/Analysis/WeakSymmetricCiphers.ql index 3ab18c85e54e..8d938e7dd1b6 100644 --- a/java/ql/src/experimental/quantum/Analysis/WeakSymmetricCiphers.ql +++ b/java/ql/src/experimental/quantum/Analysis/WeakSymmetricCiphers.ql @@ -4,7 +4,6 @@ * @id java/quantum/slices/weak-ciphers * @kind problem * @problem.severity error - * @security.severity low * @precision high * @tags external/cwe/cwe-327 */