Skip to content

Commit

Permalink
remove Kind use in BC for KeyContext
Browse files Browse the repository at this point in the history
Signed-off-by: Hugo Queinnec <[email protected]>
  • Loading branch information
hugoqnc committed Sep 19, 2024
1 parent ab22747 commit ae4720a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 48 deletions.
29 changes: 4 additions & 25 deletions engine/src/main/java/com/ibm/engine/model/context/KeyContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,15 @@
public class KeyContext extends DetectionContext
implements IDetectionContext, ISupportKind<KeyContext.Kind> {
public enum Kind {
KDF,
KEM,
/* TODO: they are still used in JCA and Python, but should be removed */
EC,
DES,
DESede,
DH,
DH_FULL,
DSA,
EC,
PBE,
RSA,
X25519,
X448,
Ed25519,
Ed448,
Fernet,
CHACHA20POLY1305,
AESGCM,
AESGCMIV,
AESOCB3,
AESSIV,
AESCCM,
PBKDF2HMAC,
SCRYPT,
ConcatKDFHash,
ConcatKDFHMAC,
HKDF,
HKDFExpand,
KBKDFHMAC,
KBKDFCMAC,
X963KDF,
KDF,
KEM,
NONE,
UNKNOWN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
Expand Down Expand Up @@ -58,7 +59,7 @@ private BcBasicAgreement() {
.forConstructor()
.shouldBeDetectedAs(new ValueActionFactory<>(agreement))
.withoutParameters()
.buildForContext(new KeyContext(KeyContext.Kind.DH))
.buildForContext(new KeyContext(Map.of("kind", "DH")))
.inBundle(() -> "Bc")
.withDependingDetectionRules(BcBasicAgreementInit.rules()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private BcDerivationFunction() {
.shouldBeDetectedAs(new ValueActionFactory<>(generator))
.withMethodParameter("org.bouncycastle.crypto.Digest")
.addDependingDetectionRules(BcDigests.rules())
.buildForContext(new KeyContext(KeyContext.Kind.KDF))
.buildForContext(new KeyContext(Map.of("kind", "KDF")))
// TODO: .withDependingDetectionRules(DerivationFunctionInit.rules()));
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());
Expand All @@ -116,7 +116,7 @@ private BcDerivationFunction() {
.shouldBeDetectedAs(new ValueActionFactory<>(generator))
.withMethodParameter("org.bouncycastle.crypto.Mac")
.addDependingDetectionRules(BcMac.rules())
.buildForContext(new KeyContext(KeyContext.Kind.KDF))
.buildForContext(new KeyContext(Map.of("kind", "KDF")))
// TODO: .withDependingDetectionRules(DerivationFunctionInit.rules()));
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());
Expand All @@ -138,7 +138,7 @@ private BcDerivationFunction() {
.shouldBeDetectedAs(new OperationModeFactory<>())
.withMethodParameter("org.bouncycastle.crypto.Digest")
.addDependingDetectionRules(BcDigests.rules())
.buildForContext(new KeyContext(KeyContext.Kind.KDF))
.buildForContext(new KeyContext(Map.of("kind", "KDF")))
// TODO: .withDependingDetectionRules(DerivationFunctionInit.rules()));
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private BcEncapsulatedSecretExtractor() {
.shouldBeDetectedAs(new ValueActionFactory<>(extractor))
// We want to capture all possible constructors (some have arguments)
.withAnyParameters()
.buildForContext(new KeyContext(KeyContext.Kind.KEM))
.buildForContext(new KeyContext(Map.of("kind", "KEM")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());
}
Expand All @@ -95,7 +95,7 @@ private BcEncapsulatedSecretExtractor() {
.asChildOfParameterWithId(-1)
.withMethodParameter("org.bouncycastle.crypto.DerivationFunction")
.addDependingDetectionRules(BcDerivationFunction.rules())
.buildForContext(new KeyContext(KeyContext.Kind.KEM))
.buildForContext(new KeyContext(Map.of("kind", "KEM")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());

Expand All @@ -112,7 +112,7 @@ private BcEncapsulatedSecretExtractor() {
.asChildOfParameterWithId(-1)
.withMethodParameter("org.bouncycastle.crypto.DerivationFunction")
.addDependingDetectionRules(BcDerivationFunction.rules())
.buildForContext(new KeyContext(KeyContext.Kind.KEM))
.buildForContext(new KeyContext(Map.of("kind", "KEM")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());

Expand All @@ -132,7 +132,7 @@ private BcEncapsulatedSecretExtractor() {
.withMethodParameter("boolean")
.withMethodParameter("boolean")
.withMethodParameter("boolean")
.buildForContext(new KeyContext(KeyContext.Kind.KEM))
.buildForContext(new KeyContext(Map.of("kind", "KEM")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private BcEncapsulatedSecretGenerator() {
.forConstructor()
.shouldBeDetectedAs(new ValueActionFactory<>(generator))
.withMethodParameter("java.security.SecureRandom")
.buildForContext(new KeyContext(KeyContext.Kind.KEM))
.buildForContext(new KeyContext(Map.of("kind", "KEM")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());
}
Expand All @@ -94,7 +94,7 @@ private BcEncapsulatedSecretGenerator() {
.withMethodParameter("org.bouncycastle.crypto.DerivationFunction")
.addDependingDetectionRules(BcDerivationFunction.rules())
.withMethodParameter("java.security.SecureRandom")
.buildForContext(new KeyContext(KeyContext.Kind.KEM))
.buildForContext(new KeyContext(Map.of("kind", "KEM")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());

Expand All @@ -110,7 +110,7 @@ private BcEncapsulatedSecretGenerator() {
.withMethodParameter("org.bouncycastle.crypto.DerivationFunction")
.addDependingDetectionRules(BcDerivationFunction.rules())
.withMethodParameter("java.security.SecureRandom")
.buildForContext(new KeyContext(KeyContext.Kind.KEM))
.buildForContext(new KeyContext(Map.of("kind", "KEM")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());

Expand All @@ -129,7 +129,7 @@ private BcEncapsulatedSecretGenerator() {
.withMethodParameter("boolean")
.withMethodParameter("boolean")
.withMethodParameter("boolean")
.buildForContext(new KeyContext(KeyContext.Kind.KEM))
.buildForContext(new KeyContext(Map.of("kind", "KEM")))
.inBundle(() -> "Bc")
.withoutDependingDetectionRules());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public final class JavaKeyAgreementContextTranslator extends JavaAbstractLibrary
@NotNull IValue<Tree> value,
@NotNull IDetectionContext detectionContext,
@NotNull DetectionLocation detectionLocation) {
if (value instanceof Algorithm<Tree> algorithm) {
return Optional.empty(); // TODO
} else if (value instanceof KeySize<Tree> keySize) {
if (value instanceof KeySize<Tree> keySize) {
KeyLength keyLength = new KeyLength(keySize.getValue(), detectionLocation);
return Optional.of(keyLength);
} else if (value instanceof KeyAction<Tree> action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import com.ibm.engine.model.KeySize;
import com.ibm.engine.model.OperationMode;
import com.ibm.engine.model.ValueAction;
import com.ibm.engine.model.context.DetectionContext;
import com.ibm.engine.model.context.IDetectionContext;
import com.ibm.engine.model.context.KeyContext;
import com.ibm.engine.model.context.PrivateKeyContext;
import com.ibm.engine.model.context.PublicKeyContext;
import com.ibm.engine.model.context.SecretKeyContext;
Expand Down Expand Up @@ -97,26 +97,26 @@ public final class JavaKeyContextTranslator extends JavaAbstractLibraryTranslato
@NotNull IValue<Tree> value,
@NotNull IDetectionContext detectionContext,
@NotNull DetectionLocation detectionLocation) {
if (value instanceof ValueAction<Tree> valueAction) {
final KeyContext.Kind kind = ((KeyContext) detectionContext).kind();
// com.ibm.mapper.model.Algorithm algorithm;
if (value instanceof ValueAction<Tree> valueAction
&& detectionContext instanceof DetectionContext context) {
String kind = context.get("kind").map(k -> k).orElse("");
switch (kind) {
case DH:
case "DH":
BcAgreementMapper bcAgreementMapper = new BcAgreementMapper();
return bcAgreementMapper
.parse(valueAction.asString(), detectionLocation)
.map(f -> f);
case KDF:
case "KDF":
BcDerivationFunctionMapper bcDerivationFunctionMapper =
new BcDerivationFunctionMapper();
return bcDerivationFunctionMapper
.parse(valueAction.asString(), detectionLocation)
.map(f -> f);
case KEM:
case "KEM":
BcKemMapper bcKEMMapper = new BcKemMapper();
return bcKEMMapper.parse(valueAction.asString(), detectionLocation).map(f -> f);
default:
break;
return Optional.empty();
}
} else if (value instanceof KeySize<Tree> keySize) {
KeyLength keyLength = new KeyLength(keySize.getValue(), detectionLocation);
Expand Down

0 comments on commit ae4720a

Please sign in to comment.