-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Crypto: Add Java Cryptographic Analysis Queries #20605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 9 commits
c5cf0ff
f38ab45
bba541c
cf88e3f
1b1b333
143be8c
bd34b6c
83ff70b
8e10e19
75b5a9f
11e8139
7a57496
f524de4
fdba3ac
c6cc4ff
3dedda4
deb4373
fba8087
758759a
3667365
ffd191d
d68f3cf
e76ced1
08abdb8
4b241d7
bd068c2
76128ed
7847e92
8b5a423
7e8acd7
55bbcee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @name Insecure nonce/iv (static value or weak random source) | ||
* @id java/quantum/insecure-iv-or-nonce | ||
* @description A nonce/iv 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 | ||
* @precision high | ||
* @tags quantum | ||
* experimental | ||
*/ | ||
|
||
import experimental.quantum.Language | ||
|
||
from Crypto::NonceArtifactNode nonce, Crypto::NodeBase src | ||
where | ||
nonce.getSourceNode() = src and | ||
not src.asElement() instanceof SecureRandomnessInstance | ||
select nonce, "Nonce or IV uses insecure or constant source $@", src, src.toString() |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @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 | ||
* @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." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @name Unknown nonce/iv initialization | ||
* @id java/quantum/unknown-iv-or-nonce-initialization | ||
* @description A nonce/iv is generated from a source that is not secure. Failure to initialize | ||
* an IV or nonce properly can lead to vulnerabilities such as replay attacks or key recovery. | ||
* @kind problem | ||
* @problem.severity error | ||
* @precision high | ||
* @tags quantum | ||
* experimental | ||
*/ | ||
|
||
import experimental.quantum.Language | ||
|
||
from Crypto::NonceArtifactNode nonce | ||
where exists(nonce.getSourceNode()) | ||
select nonce, "Unknown (unobserved) IV/Nonce initialization." |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,24 @@ | ||||||||
/** | ||||||||
* @name Weak Asymmetric 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 | ||||||||
* @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) | ||||||||
|
not Crypto::isEllipticCurveAlgorithmName(algName) | |
not Crypto::isEllipticCurveAlgorithmName(algName) and | |
configSrc = op.getConfigSource() |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment does indeed point out an issue with the query. I would suggest a rewrite to something like the following (I have not tested this change... it's a draft revision):
/**
* @name Weak Asymmetric 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
* @precision high
* @tags quantum
* experimental
*/
import java
import experimental.quantum.Language
from
Crypto::KeyOperationAlgorithmNode alg, Crypto::NodeBase configSrc, string configSrcDesc,
int keySize, string algName
where
algName = alg.getAlgorithmName() and
// Can't be an elliptic curve
not Crypto::isEllipticCurveAlgorithmName(algName) and
(
// Case 1: Key size from config source
exists(Crypto::GenericSourceNode src |
src = alg.getKeySize() and
keySize = src.toString().toInt() and
configSrc = src and
configSrcDesc = src.toString()
)
or
// Case 2: Fixed key size (no config source, or config source is fine)
not exists(Crypto::GenericSourceNode src |
src = alg.getKeySize() and
src.toString().toInt() < 2048
) and
keySize = alg.getKeySizeFixed() and
configSrc = alg and
configSrcDesc = "(implicit)"
) and
keySize < 2048
select alg,
"Use of weak asymmetric key size (" + keySize.toString() + " bits) for algorithm " + algName +
" at config source $@.", configSrc, configSrcDesc
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @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 | ||
* @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." |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @name Weak hashes | ||
* @description Finds uses of cryptographic hashing algorithms that are unapproved or otherwise weak. | ||
* @id java/quantum/weak-hashes | ||
* @kind problem | ||
* @problem.severity error | ||
* @precision high | ||
* @tags external/cwe/cwe-327 | ||
* quantum | ||
* experimental | ||
*/ | ||
|
||
import java | ||
import experimental.quantum.Language | ||
|
||
from Crypto::HashAlgorithmNode alg, Crypto::HashType htype, string msg | ||
where | ||
htype = alg.getHashType() and | ||
( | ||
htype != Crypto::SHA2() and | ||
msg = "Use of unapproved hash algorithm or API " + htype.toString() + "." | ||
or | ||
htype = Crypto::SHA2() and | ||
not exists(alg.getDigestLength()) and | ||
msg = | ||
"Use of approved hash algorithm or API type " + htype.toString() + " but unknown digest size." | ||
or | ||
htype = Crypto::SHA2() and | ||
alg.getDigestLength() < 256 and | ||
msg = | ||
"Use of approved hash algorithm or API type " + htype.toString() + " but weak digest size (" + | ||
alg.getDigestLength() + ")." | ||
) | ||
select alg, msg |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @name Weak known key derivation function output length | ||
* @description Detects key derivation operations with a known weak output length | ||
* @id java/quantum/weak-kdf-key-size | ||
* @kind problem | ||
* @problem.severity error | ||
* @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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
* @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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @name Weak symmetric ciphers | ||
* @description Finds uses of cryptographic symmetric cipher algorithms that are unapproved or otherwise weak. | ||
* @id java/quantum/weak-ciphers | ||
* @kind problem | ||
* @problem.severity error | ||
* @precision high | ||
* @tags external/cwe/cwe-327 | ||
* quantum | ||
* experimental | ||
*/ | ||
|
||
import java | ||
import experimental.quantum.Language | ||
import Crypto::KeyOpAlg as KeyOpAlg | ||
|
||
from Crypto::KeyOperationAlgorithmNode alg, KeyOpAlg::AlgorithmType algType, string msg | ||
where | ||
algType = alg.getAlgorithmType() and | ||
( | ||
algType = KeyOpAlg::TSymmetricCipher(KeyOpAlg::DES()) or | ||
algType = KeyOpAlg::TSymmetricCipher(KeyOpAlg::TRIPLE_DES()) or | ||
algType = KeyOpAlg::TSymmetricCipher(KeyOpAlg::DOUBLE_DES()) or | ||
algType = KeyOpAlg::TSymmetricCipher(KeyOpAlg::RC2()) or | ||
algType = KeyOpAlg::TSymmetricCipher(KeyOpAlg::RC4()) or | ||
algType = KeyOpAlg::TSymmetricCipher(KeyOpAlg::IDEA()) or | ||
algType = KeyOpAlg::TSymmetricCipher(KeyOpAlg::BLOWFISH()) | ||
) and | ||
msg = "Use of unapproved symmetric cipher algorithm or API: " + algType.toString() + "." | ||
select alg, msg |
Uh oh!
There was an error while loading. Please reload this page.