Skip to content

Commit

Permalink
remove Kind use from CipherContext
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoqnc committed Sep 18, 2024
1 parent 28028c9 commit 7914d0f
Show file tree
Hide file tree
Showing 35 changed files with 127 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,6 @@ public class CipherContext extends DetectionContext
implements IDetectionContext, ISupportKind<CipherContext.Kind> {

public enum Kind {
PKE,
RSA,
Fernet,
OAEP,
CHACHA20POLY1305,
AES_WRAP,
AES_WRAP_WITH_PADDING,
ENCRYPTION_STATUS,
WRAPPING_STATUS,
ENCODING,
ENCODING_SIGNATURE,
WRAP,
BLOCK_CIPHER,
BLOCK_CIPHER_ENGINE,
BLOCK_CIPHER_ENGINE_FOR_AEAD,
STREAM_CIPHER_ENGINE,
ASYMMETRIC_CIPHER_ENGINE,
ASYMMETRIC_CIPHER_ENGINE_SIGNATURE,
ASYMMETRIC_BUFFERED_BLOCK_CIPHER,
BUFFERED_BLOCK_CIPHER,
AEAD_BLOCK_CIPHER,
AEAD_ENGINE,
PADDING,
PBE,
HASH,
NONE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private BcAEADCipherEngine() {
.forConstructor()
.shouldBeDetectedAs(new ValueActionFactory<>(engine))
.withoutParameters()
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_ENGINE))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_ENGINE")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules()));
} else {
Expand All @@ -88,7 +88,7 @@ private BcAEADCipherEngine() {
new AlgorithmParameterFactory<>(
AlgorithmParameter.Kind.ANY))
.asChildOfParameterWithId(-1)
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_ENGINE))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_ENGINE")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.cipherparameters.BcCipherParameters;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -44,7 +45,7 @@ private BcAEADCipherInit() {
.shouldBeDetectedAs(new BooleanFactory<>())
.withMethodParameter("org.bouncycastle.crypto.CipherParameters")
.addDependingDetectionRules(BcCipherParameters.rules())
.buildForContext(new CipherContext(CipherContext.Kind.ENCRYPTION_STATUS))
.buildForContext(new CipherContext(Map.of("kind", "ENCRYPTION_STATUS")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.blockcipher.BcBlockCipher;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -47,8 +48,8 @@ private BcCCMBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand All @@ -62,8 +63,8 @@ private BcCCMBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.mac.BcMac;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -44,7 +45,7 @@ private BcChaCha20Poly1305() {
.forConstructor()
.shouldBeDetectedAs(new ValueActionFactory<>(AEAD))
.withoutParameters()
.buildForContext(new CipherContext(CipherContext.Kind.CHACHA20POLY1305))
.buildForContext(new CipherContext(Map.of("kind", "CHACHA20POLY1305")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand All @@ -60,7 +61,7 @@ private BcChaCha20Poly1305() {
.shouldBeDetectedAs(new ValueActionFactory<>(AEAD + "[WITH_MAC]"))
.withMethodParameter("org.bouncycastle.crypto.Mac")
.addDependingDetectionRules(BcMac.rules())
.buildForContext(new CipherContext(CipherContext.Kind.CHACHA20POLY1305))
.buildForContext(new CipherContext(Map.of("kind", "CHACHA20POLY1305")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.blockcipher.BcBlockCipher;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -47,8 +48,8 @@ private BcEAXBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.blockcipher.BcBlockCipher;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -47,8 +48,8 @@ private BcGCMBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand All @@ -62,9 +63,9 @@ private BcGCMBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.withMethodParameter("org.bouncycastle.crypto.modes.gcm.GCMMultiplier")
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand All @@ -78,8 +79,8 @@ private BcGCMBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand All @@ -93,9 +94,9 @@ private BcGCMBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.withMethodParameter("org.bouncycastle.crypto.modes.gcm.GCMMultiplier")
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.blockcipher.BcBlockCipher;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -44,7 +45,7 @@ private BcGCMSIVBlockCipher() {
.forConstructor()
.shouldBeDetectedAs(new ValueActionFactory<>(MODE))
.withoutParameters()
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand All @@ -58,8 +59,8 @@ private BcGCMSIVBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand All @@ -73,9 +74,9 @@ private BcGCMSIVBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.withMethodParameter("org.bouncycastle.crypto.modes.gcm.GCMMultiplier")
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.blockcipher.BcBlockCipher;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -47,8 +48,8 @@ private BcKCCMBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand All @@ -62,9 +63,9 @@ private BcKCCMBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.withMethodParameter("int")
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.blockcipher.BcBlockCipher;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -47,8 +48,8 @@ private BcKGCMBlockCipher() {
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.blockcipher.BcBlockCipher;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -47,13 +48,13 @@ private BcOCBBlockCipher() {
.shouldBeDetectedAs(new ValueActionFactory<>(MODE))
.withMethodParameter("org.bouncycastle.crypto.BlockCipher") // hash cipher
.addDependingDetectionRules(
BcBlockCipher.all(new CipherContext(CipherContext.Kind.HASH)))
BcBlockCipher.all(new CipherContext(Map.of("kind", "HASH"))))
.withMethodParameter("org.bouncycastle.crypto.BlockCipher") // main cipher
.addDependingDetectionRules(
BcBlockCipher.all(
new CipherContext(
CipherContext.Kind.BLOCK_CIPHER_ENGINE_FOR_AEAD)))
.buildForContext(new CipherContext(CipherContext.Kind.AEAD_BLOCK_CIPHER))
Map.of("kind", "BLOCK_CIPHER_ENGINE_FOR_AEAD"))))
.buildForContext(new CipherContext(Map.of("kind", "AEAD_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcAEADCipherInit.rules());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -53,7 +54,7 @@ private BcAsymCipherEngine() {
IDetectionContext context =
detectionValueContext != null
? detectionValueContext
: new CipherContext(CipherContext.Kind.ASYMMETRIC_CIPHER_ENGINE);
: new CipherContext(Map.of("kind", "ASYMMETRIC_CIPHER_ENGINE"));

for (String engine : blockCiphers) {
constructorsList.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.cipherparameters.BcCipherParameters;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -44,7 +45,7 @@ private BcAsymCipherInit() {
.shouldBeDetectedAs(new BooleanFactory<>())
.withMethodParameter("org.bouncycastle.crypto.CipherParameters")
.addDependingDetectionRules(BcCipherParameters.rules())
.buildForContext(new CipherContext(CipherContext.Kind.ENCRYPTION_STATUS))
.buildForContext(new CipherContext(Map.of("kind", "ENCRYPTION_STATUS")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.ibm.engine.rule.builder.DetectionRuleBuilder;
import com.ibm.plugin.rules.detection.bc.cipherparameters.BcCipherParameters;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -47,7 +48,7 @@ private BcBufferedAsymmetricBlockCipher() {
.shouldBeDetectedAs(new BooleanFactory<>())
.withMethodParameter("org.bouncycastle.crypto.CipherParameters")
.addDependingDetectionRules(BcCipherParameters.rules())
.buildForContext(new CipherContext(CipherContext.Kind.ENCRYPTION_STATUS))
.buildForContext(new CipherContext(Map.of("kind", "ENCRYPTION_STATUS")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules();

Expand All @@ -60,7 +61,7 @@ private BcBufferedAsymmetricBlockCipher() {
.withMethodParameter("org.bouncycastle.crypto.AsymmetricBlockCipher")
.addDependingDetectionRules(BcAsymmetricBlockCipher.rules())
.buildForContext(
new CipherContext(CipherContext.Kind.ASYMMETRIC_BUFFERED_BLOCK_CIPHER))
new CipherContext(Map.of("kind", "ASYMMETRIC_BUFFERED_BLOCK_CIPHER")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(List.of(INIT));

Expand Down
Loading

0 comments on commit 7914d0f

Please sign in to comment.