smolnar82 commented on code in PR #1212:
URL: https://github.com/apache/knox/pull/1212#discussion_r3129080779


##########
gateway-spi/src/main/java/org/apache/knox/gateway/fips/FipsUtils.java:
##########
@@ -17,11 +17,43 @@
  */
 package org.apache.knox.gateway.fips;
 
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+
 public class FipsUtils {
 
-    private static final String FIPS_SYSTEM_PROPERTY = 
"com.safelogic.cryptocomply.fips.approved_only";
+    public static final String FIPS_SYSTEM_PROPERTY = 
"com.safelogic.cryptocomply.fips.approved_only";
+
+    private static final List<String> FORBIDDEN_ALGORITHMS = 
Arrays.asList("MD5", "RC4", "ARC4", "ARCFOUR", "SHA-1", "SHA1");
 
     public static boolean isFipsEnabledWithBCProvider() {
         return Boolean.parseBoolean(System.getProperty(FIPS_SYSTEM_PROPERTY));
     }
+
+    /**
+     * Validates if the given algorithm is allowed in a FIPS environment.
+     *
+     * @param algorithm The algorithm to validate.
+     * @return true if the algorithm is allowed or FIPS is not enabled, false 
otherwise.
+     */
+    private static boolean isAlgorithmAllowed(String algorithm) {
+        if (!isFipsEnabledWithBCProvider() || algorithm == null || 
algorithm.isEmpty()) {
+            return true;
+        }
+
+        String upperCaseAlg = algorithm.toUpperCase(Locale.ROOT);
+        for (String forbidden : FORBIDDEN_ALGORITHMS) {
+            if (upperCaseAlg.contains(forbidden)) {
+                return false;
+            }
+        }

Review Comment:
   Why not a simple ` return !isFipsEnabledWithBCProvider() || algorithm == 
null || algorithm.isEmpty() || 
!FORBIDDEN_ALGORITHMS.contains(algorithm.toUpperCase(Locale.ROOT))`?



##########
gateway-server/src/main/java/org/apache/knox/gateway/services/security/impl/DefaultCryptoService.java:
##########
@@ -57,11 +58,18 @@ public void setAliasService(AliasService as) {
 
   @Override
   public void init(GatewayConfig config, Map<String, String> options)
-      throws ServiceLifecycleException {
+          throws ServiceLifecycleException {
     this.config = config;
-  if (aliasService == null) {
+    if (aliasService == null) {
       throw new ServiceLifecycleException("Alias service is not set");
     }
+    if (FipsUtils.isFipsEnabledWithBCProvider()) {
+      //invoking the following getters will throw IllegalArgumentException in 
case a forbidden algorithm is set
+      //so we can use them as a validation at service initialization time
+      config.getCredentialStoreAlgorithm();
+      config.getAlgorithm();
+      config.getPBEAlgorithm();

Review Comment:
   We may want to add a new GatewayConfig method called 
`validateFipsAlgorithms` and have the `GatewayConfigImpl` self-contain what 
does it mean to be valid in FIPS envs?



##########
gateway-spi/src/main/java/org/apache/knox/gateway/fips/FipsUtils.java:
##########
@@ -17,11 +17,43 @@
  */
 package org.apache.knox.gateway.fips;
 
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+
 public class FipsUtils {
 
-    private static final String FIPS_SYSTEM_PROPERTY = 
"com.safelogic.cryptocomply.fips.approved_only";
+    public static final String FIPS_SYSTEM_PROPERTY = 
"com.safelogic.cryptocomply.fips.approved_only";
+
+    private static final List<String> FORBIDDEN_ALGORITHMS = 
Arrays.asList("MD5", "RC4", "ARC4", "ARCFOUR", "SHA-1", "SHA1");
 
     public static boolean isFipsEnabledWithBCProvider() {
         return Boolean.parseBoolean(System.getProperty(FIPS_SYSTEM_PROPERTY));
     }
+
+    /**
+     * Validates if the given algorithm is allowed in a FIPS environment.
+     *
+     * @param algorithm The algorithm to validate.
+     * @return true if the algorithm is allowed or FIPS is not enabled, false 
otherwise.
+     */
+    private static boolean isAlgorithmAllowed(String algorithm) {
+        if (!isFipsEnabledWithBCProvider() || algorithm == null || 
algorithm.isEmpty()) {
+            return true;
+        }
+
+        String upperCaseAlg = algorithm.toUpperCase(Locale.ROOT);
+        for (String forbidden : FORBIDDEN_ALGORITHMS) {
+            if (upperCaseAlg.contains(forbidden)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    public static void validateAlgorithm(String algorithm, String paramName) {
+        if (!isAlgorithmAllowed(algorithm)) {
+            throw new IllegalArgumentException("In a FIPS environment, you are 
not allowed to use " + algorithm + " as " + paramName);
+        }
+    }

Review Comment:
   Extracting the error message out to a constant template and re-use it in the 
tests?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to