Skip to content
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

Feature/update python detection tests #119

Merged
merged 7 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions enricher/src/main/java/com/ibm/enricher/Enricher.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.ibm.enricher.algorithm.MacOrDigestEnricher;
import com.ibm.enricher.algorithm.PBKDF2Enricher;
import com.ibm.enricher.algorithm.RSAEnricher;
import com.ibm.enricher.algorithm.RSAoaepEnricher;
import com.ibm.enricher.algorithm.RSAssaPSSEnricher;
import com.ibm.enricher.algorithm.SHA2Enricher;
import com.ibm.enricher.algorithm.SHA3Enricher;
Expand Down Expand Up @@ -120,6 +121,9 @@ public INode enrich(@Nonnull INode node) {
if (node instanceof RSAssaPSS) {
node = new RSAssaPSSEnricher().enrich(node);
}
if (node instanceof RSA) {
node = new RSAoaepEnricher().enrich(node);
}

if (node instanceof Signature) {
node = new SignatureEnricher().enrich(node);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2024 IBM
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.enricher.algorithm;

import com.ibm.enricher.IEnricher;
import com.ibm.mapper.model.INode;
import com.ibm.mapper.model.Oid;
import com.ibm.mapper.model.Padding;
import com.ibm.mapper.model.Signature;
import com.ibm.mapper.model.algorithms.RSA;
import com.ibm.mapper.model.padding.OAEP;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;

public class RSAoaepEnricher implements IEnricher {

@Override
public @NotNull INode enrich(@NotNull INode node) {
if (node instanceof RSA rsa) {
final Optional<INode> padding = rsa.hasChildOfType(Padding.class);
if (padding.isEmpty()) {
return node;
}
if (padding.get() instanceof OAEP) {
final RSA newRSA = new RSA(Signature.class, rsa);
newRSA.put(new Oid("1.2.840.113549.1.1.7", rsa.getDetectionContext()));
return newRSA;
}
}
return node;
}
}
6 changes: 3 additions & 3 deletions mapper/src/main/java/com/ibm/mapper/ITranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected abstract Optional<INode> translate(
*/
static class Traverser<R, T, S, P> {
@Nonnull final DetectionStore<R, T, S, P> rootDetectionStore;
@Nonnull final List<INode> newParents = new ArrayList<>();
@Nonnull final List<INode> newRoots = new ArrayList<>();
@Nonnull final Function<DetectionStore<R, T, S, P>, Map<Integer, List<INode>>> translator;

public Traverser(
Expand All @@ -131,7 +131,7 @@ public List<INode> translate() {
travers(rootDetectionStore, rootNodes);
final List<INode> translatedRootNodes =
new ArrayList<>(rootNodes.values().stream().flatMap(List::stream).toList());
translatedRootNodes.addAll(newParents);
translatedRootNodes.addAll(newRoots);
return translatedRootNodes;
}

Expand Down Expand Up @@ -196,7 +196,7 @@ private void append(
if (parentNode.hasChildOfType(childNode.getKind()).isPresent()) {
final INode newParent = parentNode.deepCopy();
newParent.put(childNode);
newParents.add(newParent);
newRoots.add(newParent);
} else {
parentNode.put(childNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.ibm.mapper.model.algorithms.ChaCha20;
import com.ibm.mapper.model.algorithms.ChaCha20Poly1305;
import com.ibm.mapper.model.algorithms.IDEA;
import com.ibm.mapper.model.algorithms.MGF1;
import com.ibm.mapper.model.algorithms.RC4;
import com.ibm.mapper.model.algorithms.RSA;
import com.ibm.mapper.model.algorithms.SEED;
Expand All @@ -48,7 +47,6 @@ public final class PycaCipherMapper implements IMapper {
}

return switch (str.toUpperCase().trim()) {
case "MGF1" -> Optional.of(new MGF1(detectionLocation));
case "AES" -> Optional.of(new AES(detectionLocation));
case "AES128" -> Optional.of(new AES(128, detectionLocation));
case "AES256" -> Optional.of(new AES(256, detectionLocation));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2024 IBM
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.mapper.mapper.pyca;

import com.ibm.mapper.mapper.IMapper;
import com.ibm.mapper.model.INode;
import com.ibm.mapper.model.KeyDerivationFunction;
import com.ibm.mapper.model.Mac;
import com.ibm.mapper.model.algorithms.MD5;
import com.ibm.mapper.model.algorithms.Poly1305;
import com.ibm.mapper.model.algorithms.SHA;
import com.ibm.mapper.model.algorithms.SHA2;
import com.ibm.mapper.model.algorithms.SHA3;
import com.ibm.mapper.model.algorithms.SHAKE;
import com.ibm.mapper.model.algorithms.SM3;
import com.ibm.mapper.utils.DetectionLocation;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class PycaHashBasedKeyDerivationMapper implements IMapper {
@Override
public @NotNull Optional<? extends INode> parse(
@Nullable String str, @NotNull DetectionLocation detectionLocation) {
if (str == null) {
return Optional.empty();
}

return switch (str.toUpperCase().trim()) {
case "SHA1" ->
Optional.of(new SHA(KeyDerivationFunction.class, new SHA(detectionLocation)));
case "SHA512_224" ->
Optional.of(
new SHA2(
KeyDerivationFunction.class,
new SHA2(
224,
new SHA2(512, detectionLocation),
detectionLocation)));
case "SHA512_256" ->
Optional.of(
new SHA2(
KeyDerivationFunction.class,
new SHA2(
256,
new SHA2(512, detectionLocation),
detectionLocation)));
case "SHA224" ->
Optional.of(
new SHA2(
KeyDerivationFunction.class, new SHA2(224, detectionLocation)));
case "SHA256" ->
Optional.of(
new SHA2(
KeyDerivationFunction.class, new SHA2(256, detectionLocation)));
case "SHA384" ->
Optional.of(
new SHA2(
KeyDerivationFunction.class, new SHA2(384, detectionLocation)));
case "SHA512" ->
Optional.of(
new SHA2(
KeyDerivationFunction.class, new SHA2(512, detectionLocation)));
case "SHA3_224" ->
Optional.of(
new SHA3(
KeyDerivationFunction.class, new SHA3(224, detectionLocation)));
case "SHA3_256" ->
Optional.of(
new SHA3(
KeyDerivationFunction.class, new SHA3(256, detectionLocation)));
case "SHA3_384" ->
Optional.of(
new SHA3(
KeyDerivationFunction.class, new SHA3(384, detectionLocation)));
case "SHA3_512" ->
Optional.of(
new SHA3(
KeyDerivationFunction.class, new SHA3(512, detectionLocation)));
case "SHAKE128" ->
Optional.of(
new SHAKE(
KeyDerivationFunction.class,
new SHAKE(128, detectionLocation)));
case "SHAKE256" ->
Optional.of(
new SHAKE(
KeyDerivationFunction.class,
new SHAKE(256, detectionLocation)));
case "MD5" -> Optional.of(new MD5(Mac.class, detectionLocation));
case "BLAKE2B" -> Optional.empty(); // TODO
case "BLAKE2S" -> Optional.empty(); // TODO
case "SM3" ->
Optional.of(new SM3(KeyDerivationFunction.class, new SM3(detectionLocation)));
case "POLY1305" ->
Optional.of(
new Poly1305(
KeyDerivationFunction.class, new Poly1305(detectionLocation)));
default -> Optional.empty();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.ibm.mapper.model.Algorithm;
import com.ibm.mapper.model.EllipticCurve;
import com.ibm.mapper.model.KeyAgreement;
import com.ibm.mapper.model.Oid;
import com.ibm.mapper.utils.DetectionLocation;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.NotNull;
Expand All @@ -31,11 +32,13 @@ public final class ECDH extends Algorithm implements KeyAgreement {

public ECDH(@NotNull DetectionLocation detectionLocation) {
super(NAME, KeyAgreement.class, detectionLocation);
this.put(new Oid("1.3.132.1.12", detectionLocation));
}

public ECDH(
@Nonnull EllipticCurve ellipticCurve, @Nonnull DetectionLocation detectionLocation) {
super(NAME, KeyAgreement.class, detectionLocation);
this.put(new Oid("1.3.132.1.12", detectionLocation));
this.put(ellipticCurve);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ public RSA(
super(NAME, asKind, detectionLocation);
this.put(new Oid(OID, detectionLocation));
}

public RSA(@Nonnull final Class<? extends IPrimitive> asKind, @NotNull RSA rsa) {
super(rsa, asKind);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2024 IBM
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.mapper.model.curves;

import com.ibm.mapper.model.EllipticCurve;
import com.ibm.mapper.utils.DetectionLocation;
import org.jetbrains.annotations.NotNull;

public final class Brainpoolp256r1 extends EllipticCurve {
public Brainpoolp256r1(@NotNull DetectionLocation detectionLocation) {
super("Brainpoolp256r1", detectionLocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2024 IBM
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.mapper.model.curves;

import com.ibm.mapper.model.EllipticCurve;
import com.ibm.mapper.utils.DetectionLocation;
import org.jetbrains.annotations.NotNull;

public final class Brainpoolp384r1 extends EllipticCurve {
public Brainpoolp384r1(@NotNull DetectionLocation detectionLocation) {
super("Brainpoolp384r1", detectionLocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2024 IBM
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.mapper.model.curves;

import com.ibm.mapper.model.EllipticCurve;
import com.ibm.mapper.utils.DetectionLocation;
import org.jetbrains.annotations.NotNull;

public final class Brainpoolp512r1 extends EllipticCurve {
public Brainpoolp512r1(@NotNull DetectionLocation detectionLocation) {
super("Brainpoolp512r1", detectionLocation);
}
}
30 changes: 30 additions & 0 deletions mapper/src/main/java/com/ibm/mapper/model/curves/Secp192r1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2024 IBM
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.mapper.model.curves;

import com.ibm.mapper.model.EllipticCurve;
import com.ibm.mapper.utils.DetectionLocation;
import org.jetbrains.annotations.NotNull;

public final class Secp192r1 extends EllipticCurve {
public Secp192r1(@NotNull DetectionLocation detectionLocation) {
super("secp192r1", detectionLocation);
}
}
Loading
Loading