Skip to content

Commit

Permalink
Feature/update python translation (#109)
Browse files Browse the repository at this point in the history
* update

Signed-off-by: Nicklas Körtge <nicklas.koertge1@ibm.com>

* update digests

Signed-off-by: Nicklas Körtge <nicklas.koertge1@ibm.com>

* update python translation

Signed-off-by: Nicklas Körtge <nicklas.koertge1@ibm.com>

* update python translation

Signed-off-by: Nicklas Körtge <nicklas.koertge1@ibm.com>

* update python translation

Signed-off-by: Nicklas Körtge <nicklas.koertge1@ibm.com>

* update python translation

Signed-off-by: Nicklas Körtge <nicklas.koertge1@ibm.com>

* update python translation

Signed-off-by: Nicklas Körtge <nicklas.koertge1@ibm.com>

* add TODOs/comments

Signed-off-by: Hugo Queinnec <hugo.queinnec@ibm.com>

* fix some errors

Signed-off-by: Hugo Queinnec <hugo.queinnec@ibm.com>

---------

Signed-off-by: Nicklas Körtge <nicklas.koertge1@ibm.com>
Signed-off-by: Hugo Queinnec <hugo.queinnec@ibm.com>
Co-authored-by: Hugo Queinnec <hugo.queinnec@ibm.com>
  • Loading branch information
n1ckl0sk0rtge and hugoqnc authored Aug 20, 2024
1 parent a4059a5 commit aec6658
Show file tree
Hide file tree
Showing 82 changed files with 1,458 additions and 1,775 deletions.
62 changes: 62 additions & 0 deletions engine/src/main/java/com/ibm/engine/model/Padding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.engine.model;

import javax.annotation.Nonnull;
import org.jetbrains.annotations.NotNull;

public class Padding<T> extends AbstractValue<T> {
@Nonnull private final String value;
@Nonnull private final T location;

public Padding(@Nonnull String value, @Nonnull T location) {
this.value = value;
this.location = location;
}

@Override
public @NotNull T getLocation() {
return this.location;
}

@Override
public String toString() {
return this.value;
}

@Override
public @NotNull String asString() {
return this.value;
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Padding<?> padding)) return false;
return value.equals(padding.value) && location.equals(padding.location);
}

@Override
public int hashCode() {
int result = value.hashCode();
result = 31 * result + location.hashCode();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class SignatureAction<T> extends AbstractValue<T> implements IAction<T> {
public enum Action {
SIGN,
VERIFY,
PADDING
}

@Nonnull private final Action action;
Expand Down
2 changes: 1 addition & 1 deletion engine/src/main/java/com/ibm/engine/model/ValueAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import javax.annotation.Nonnull;

public class ValueAction<T> extends AbstractValue<T> implements IAction<T> {
public final class ValueAction<T> extends AbstractValue<T> implements IAction<T> {

@Nonnull private final String value;
@Nonnull private final T location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@
*/
package com.ibm.engine.model.context;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;

public class CipherContext implements IDetectionContext, ISupportKind<CipherContext.Kind> {
public class CipherContext extends DetectionContext
implements IDetectionContext, ISupportKind<CipherContext.Kind> {

public enum Kind {
PKE,
RSA,
Fernet,
OAEP,
CHACHA20POLY1305,
AESGCM,
AESGCMIV,
AESOCB3,
AESSIV,
AESCCM,
PKCS7,
ANSIX923,
AES_WRAP,
AES_WRAP_WITH_PADDING,
ENCRYPTION_STATUS,
Expand All @@ -61,14 +57,33 @@ public enum Kind {

@Nonnull private final Kind kind;

/**
* use a property map instead
*
* @deprecated
*/
@Deprecated(since = "1.3.0")
public CipherContext(@Nonnull Kind kind) {
super(new HashMap<>());
this.kind = kind;
}

public CipherContext() {
super(new HashMap<>());
this.kind = Kind.NONE;
}

public CipherContext(@Nonnull Map<String, String> properties) {
super(properties);
this.kind = Kind.NONE;
}

/**
* use a property map instead
*
* @deprecated
*/
@Deprecated(since = "1.3.0")
@Nonnull
public Kind kind() {
return kind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.mapper;
package com.ibm.engine.model.context;

import com.ibm.engine.model.IValue;
import com.ibm.engine.model.context.IDetectionContext;
import com.ibm.engine.rule.IBundle;
import com.ibm.mapper.model.INode;
import com.ibm.mapper.utils.DetectionLocation;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Unmodifiable;

public interface IContextTranslationWithKind<T> {
public abstract class DetectionContext {
@Unmodifiable @Nonnull private final Map<String, String> properties;

protected DetectionContext(@Nonnull Map<String, String> properties) {
this.properties = properties;
}

public boolean contains(@Nonnull String key) {
return properties.containsKey(key);
}

@Nonnull
public Optional<INode> translate(
@Nonnull final IBundle bundleIdentifier,
@Nonnull final IValue<T> value,
@Nonnull final IDetectionContext detectionContext,
@Nonnull final DetectionLocation detectionLocation);
public Optional<String> get(@Nonnull String key) {
return Optional.ofNullable(properties.get(key));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,50 @@
*/
package com.ibm.engine.model.context;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;

public class DigestContext implements IDetectionContext, ISupportKind<DigestContext.Kind> {
public class DigestContext extends DetectionContext
implements IDetectionContext, ISupportKind<DigestContext.Kind> {

// Currently, Kind contain all the types of hashes in Python's cryptography library.
// This means that Kind is library sepcific, which is not optimal.
// TODO: If at some point Kind would have to be used for another language/library, Kind would
// have to be changed to contain library-independent values. Additionally, a new mapping layer
// would have to be built between each library hash values and these library-independent values.
public enum Kind {
NONE,
MGF1,
MGF,
CRAMER_SHOUP,
NTRU,
SHA1,
SHA512_224,
SHA512_256,
SHA224,
SHA256,
SHA384,
SHA512,
SHA3_224,
SHA3_256,
SHA3_384,
SHA3_512,
SHAKE128,
SHAKE256,
MD5,
BLAKE2b,
BLAKE2s,
SM3
}

@Nonnull private final Kind kind;

public DigestContext() {
super(new HashMap<>());
this.kind = Kind.NONE;
}

/**
* use a property map instead
*
* @deprecated
*/
@Deprecated(since = "1.3.0")
public DigestContext(@Nonnull Kind kind) {
super(new HashMap<>());
this.kind = kind;
}

public DigestContext(@Nonnull Map<String, String> properties) {
super(properties);
this.kind = Kind.NONE;
}

/**
* use a property map instead
*
* @deprecated
*/
@Deprecated(since = "1.3.0")
@Nonnull
public Kind kind() {
return kind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,25 @@
*/
package com.ibm.engine.model.context;

import java.util.Map;
import org.jetbrains.annotations.NotNull;

public class KeyAgreementContext implements IDetectionContext {
public class KeyAgreementContext extends DetectionContext implements IDetectionContext {

public KeyAgreementContext(@NotNull Map<String, String> properties) {
super(properties);
}

/**
* use a property map instead
*
* @deprecated
*/
@Deprecated(since = "1.3.0")
public KeyAgreementContext() {
super(Map.of());
}

@NotNull @Override
public Class<? extends IDetectionContext> type() {
return KeyAgreementContext.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
*/
package com.ibm.engine.model.context;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;

@SuppressWarnings("java:S115")
public class KeyContext implements IDetectionContext, ISupportKind<KeyContext.Kind> {
public class KeyContext extends DetectionContext
implements IDetectionContext, ISupportKind<KeyContext.Kind> {
public enum Kind {
KDF,
KEM,
Expand Down Expand Up @@ -60,14 +63,33 @@ public enum Kind {

@Nonnull private final Kind kind;

/**
* use a property map instead
*
* @deprecated
*/
@Deprecated(since = "1.3.0")
public KeyContext(@Nonnull Kind kind) {
super(new HashMap<>());
this.kind = kind;
}

public KeyContext() {
super(new HashMap<>());
this.kind = Kind.NONE;
}

public KeyContext(@Nonnull Map<String, String> properties) {
super(properties);
this.kind = Kind.NONE;
}

/**
* use a property map instead
*
* @deprecated
*/
@Deprecated(since = "1.3.0")
@Nonnull
public Kind kind() {
return kind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.plugin.translation;
package com.ibm.engine.model.context;

import com.ibm.mapper.configuration.Configuration;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;

public class PythonMapperConfig extends Configuration {
public class KeyDerivationFunctionContext extends DetectionContext implements IDetectionContext {

// TODO: Is there something to change compared to the Java case in this file?
@Nonnull
@Override
public String changeStringValue(@Nonnull String value) {

if (value.contains("NoPadding")) {
return "";
}
public KeyDerivationFunctionContext() {
super(new HashMap<>());
}

if (value.contains("Padding")) {
return value.replace("Padding", "");
}
public KeyDerivationFunctionContext(@NotNull Map<String, String> properties) {
super(properties);
}

return value;
@Override
public @NotNull Class<? extends IDetectionContext> type() {
return KeyDerivationFunctionContext.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@
*/
package com.ibm.engine.model.context;

import java.util.Map;
import javax.annotation.Nonnull;

public class PrivateKeyContext extends KeyContext implements IDetectionContext {
/**
* use a property map instead
*
* @deprecated
*/
@Deprecated(since = "1.3.0")
public PrivateKeyContext(@Nonnull Kind kind) {
super(kind);
}

public PrivateKeyContext(@Nonnull Map<String, String> properties) {
super(properties);
}

@Nonnull
@Override
public Class<? extends IDetectionContext> type() {
Expand Down
Loading

0 comments on commit aec6658

Please sign in to comment.