forked from jenkinsci/oic-auth-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BEE-51338] Add validator and filter for checking algo used for oic c…
…onfiguration
- Loading branch information
1 parent
769f395
commit cb59d9a
Showing
9 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
src/main/java/org/jenkinsci/plugins/oic/OicAlgorithmValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package org.jenkinsci.plugins.oic; | ||
|
||
import com.nimbusds.jose.EncryptionMethod; | ||
import com.nimbusds.jose.JWEAlgorithm; | ||
import com.nimbusds.jose.JWSAlgorithm; | ||
import com.nimbusds.jose.crypto.ECDSASigner; | ||
import com.nimbusds.jose.crypto.MACSigner; | ||
import com.nimbusds.jose.crypto.RSASSASigner; | ||
import com.nimbusds.jose.crypto.impl.AESCryptoProvider; | ||
import com.nimbusds.jose.crypto.impl.ContentCryptoProvider; | ||
import com.nimbusds.jose.crypto.impl.ECDHCryptoProvider; | ||
import com.nimbusds.jose.crypto.impl.PasswordBasedCryptoProvider; | ||
import com.nimbusds.jose.crypto.impl.RSACryptoProvider; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import jenkins.security.FIPS140; | ||
|
||
/** | ||
* This class helps in validating algorithms for FIPS compliance and filtering the non-compliant algorithms when in | ||
* FIPS mode. | ||
*/ | ||
public class OicAlgorithmValidator { | ||
|
||
private static final boolean isFIPSMode = FIPS140.useCompliantAlgorithms(); | ||
|
||
/** | ||
* Checks if the Jws signing algorithm used for OIC configuration is FIPS compliant. | ||
*/ | ||
public static boolean isJwsAlgorithmFipsNonCompliant(String algorithm) { | ||
boolean matchNotFound = false; | ||
if (isFIPSMode && algorithm != null) { | ||
Set<JWSAlgorithm> jwsSupportedAlgorithms = new LinkedHashSet<>(); | ||
jwsSupportedAlgorithms.addAll(MACSigner.SUPPORTED_ALGORITHMS); | ||
jwsSupportedAlgorithms.addAll(RSASSASigner.SUPPORTED_ALGORITHMS); | ||
jwsSupportedAlgorithms.addAll(ECDSASigner.SUPPORTED_ALGORITHMS); | ||
|
||
if (!jwsSupportedAlgorithms.isEmpty()) { | ||
matchNotFound = jwsSupportedAlgorithms.stream() | ||
.map(JWSAlgorithm::getName) | ||
.noneMatch(name -> name.equals(algorithm)); | ||
} | ||
} | ||
return matchNotFound; | ||
} | ||
|
||
/** | ||
* Checks if the Jwe encryption algorithm used for OIC configuration is FIPS compliant. | ||
*/ | ||
public static boolean isJweAlgorithmFipsNonCompliant(String algorithm) { | ||
boolean matchNotFound = false; | ||
if (isFIPSMode && algorithm != null) { | ||
Set<JWEAlgorithm> jweSupportedAlgorithms = new LinkedHashSet<>(); | ||
jweSupportedAlgorithms.addAll(AESCryptoProvider.SUPPORTED_ALGORITHMS); | ||
jweSupportedAlgorithms.addAll(RSACryptoProvider.SUPPORTED_ALGORITHMS); | ||
// RSA1_5 is deprecated and not a compliant algorithm. | ||
jweSupportedAlgorithms.remove(JWEAlgorithm.RSA1_5); | ||
jweSupportedAlgorithms.addAll(ECDHCryptoProvider.SUPPORTED_ALGORITHMS); | ||
jweSupportedAlgorithms.addAll(PasswordBasedCryptoProvider.SUPPORTED_ALGORITHMS); | ||
|
||
if (!jweSupportedAlgorithms.isEmpty()) { | ||
matchNotFound = jweSupportedAlgorithms.stream() | ||
.map(JWEAlgorithm::getName) | ||
.noneMatch(name -> name.equals(algorithm)); | ||
} | ||
} | ||
return matchNotFound; | ||
} | ||
|
||
/** | ||
* Filter FIPS non-compliant Jwe encryption algorithm used for OIC configuration. | ||
*/ | ||
public static void filterFipsNonCompliantJweAlgorithm(List<JWEAlgorithm> algorithm) { | ||
boolean matchNotFound = false; | ||
if (isFIPSMode && algorithm != null && !algorithm.isEmpty()) { | ||
List<JWEAlgorithm> itemsToBeRemoved = new ArrayList<>(); | ||
for (JWEAlgorithm jweAlgorithm : algorithm) { | ||
matchNotFound = isJweAlgorithmFipsNonCompliant(jweAlgorithm.getName()); | ||
if (matchNotFound) { | ||
itemsToBeRemoved.add(jweAlgorithm); | ||
} | ||
} | ||
if (!itemsToBeRemoved.isEmpty()) { | ||
algorithm.removeAll(itemsToBeRemoved); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* validate FIPS non-compliant Jwe encryption algorithm used for OIC configuration. | ||
*/ | ||
public static boolean isJwsAlgoFipsNonCompliant(List<JWSAlgorithm> algorithm) { | ||
boolean matchNotFound = false; | ||
if (isFIPSMode && algorithm != null && !algorithm.isEmpty()) { | ||
for (JWSAlgorithm jwsAlgorithm : algorithm) { | ||
matchNotFound = isJwsAlgorithmFipsNonCompliant(jwsAlgorithm.getName()); | ||
if (matchNotFound) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Filter FIPS non-compliant Jws encryption algorithm used for OIC configuration. | ||
*/ | ||
public static void filterFipsNonCompliantJwsAlgorithm(List<JWSAlgorithm> algorithm) { | ||
boolean matchNotFound = false; | ||
if (isFIPSMode && algorithm != null && !algorithm.isEmpty()) { | ||
List<JWSAlgorithm> itemsToBeRemoved = new ArrayList<>(); | ||
for (JWSAlgorithm jwsAlgorithm : algorithm) { | ||
matchNotFound = isJwsAlgorithmFipsNonCompliant(jwsAlgorithm.getName()); | ||
if (matchNotFound) { | ||
itemsToBeRemoved.add(jwsAlgorithm); | ||
} | ||
} | ||
if (!itemsToBeRemoved.isEmpty()) { | ||
algorithm.removeAll(itemsToBeRemoved); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the encryption method used for OIC configuration is FIPS compliant. | ||
*/ | ||
public static boolean isEncryptionMethodFipsNonCompliant(String encryptionMethod) { | ||
boolean matchNotFound = false; | ||
if (isFIPSMode && encryptionMethod != null) { | ||
Set<EncryptionMethod> supportedEncryptionMethod = | ||
new LinkedHashSet<>(ContentCryptoProvider.SUPPORTED_ENCRYPTION_METHODS); | ||
if (!supportedEncryptionMethod.isEmpty()) { | ||
matchNotFound = supportedEncryptionMethod.stream() | ||
.map(EncryptionMethod::getName) | ||
.noneMatch(name -> name.equals(encryptionMethod)); | ||
} | ||
} | ||
return matchNotFound; | ||
} | ||
|
||
/** | ||
* Filter FIPS non-compliant encryption algorithm used for OIC configuration. | ||
*/ | ||
public static void filterFipsNonCompliantEncryptionMethod(List<EncryptionMethod> algorithm) { | ||
boolean matchNotFound = false; | ||
if (isFIPSMode && algorithm != null && !algorithm.isEmpty()) { | ||
List<EncryptionMethod> itemsToBeRemoved = new ArrayList<>(); | ||
for (EncryptionMethod encryptionMethod : algorithm) { | ||
matchNotFound = isEncryptionMethodFipsNonCompliant(encryptionMethod.getName()); | ||
if (matchNotFound) { | ||
itemsToBeRemoved.add(encryptionMethod); | ||
} | ||
} | ||
if (!itemsToBeRemoved.isEmpty()) { | ||
algorithm.removeAll(itemsToBeRemoved); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/test/java/org/jenkinsci/plugins/oic/OicAlgorithmValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.jenkinsci.plugins.oic; | ||
|
||
import jenkins.security.FIPS140; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.mockStatic; | ||
|
||
class OicAlgorithmValidatorTest { | ||
|
||
private MockedStatic<FIPS140> fips140Mock; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
fips140Mock = mockStatic(FIPS140.class); | ||
} | ||
|
||
@Test | ||
void isJwsAlgorithmFipsCompliant() { | ||
fips140Mock.when(FIPS140::useCompliantAlgorithms).thenReturn(true); | ||
assertTrue(OicAlgorithmValidator.isJwsAlgorithmFipsNonCompliant("")); | ||
assertTrue(OicAlgorithmValidator.isJwsAlgorithmFipsNonCompliant(" ")); | ||
assertTrue(OicAlgorithmValidator.isJwsAlgorithmFipsNonCompliant("invalid-algo")); | ||
|
||
String[] validAlgoArray = { | ||
"HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "ES256", "ES256K", "ES384", "ES512", "PS256", "PS384", | ||
"PS512" | ||
}; | ||
for (String algo : validAlgoArray) { | ||
assertFalse(OicAlgorithmValidator.isJwsAlgorithmFipsNonCompliant(algo)); | ||
} | ||
assertTrue(OicAlgorithmValidator.isJwsAlgorithmFipsNonCompliant("EdDSA")); | ||
assertTrue(OicAlgorithmValidator.isJwsAlgorithmFipsNonCompliant("Ed25519")); | ||
assertTrue(OicAlgorithmValidator.isJwsAlgorithmFipsNonCompliant("Ed448")); | ||
} | ||
} |
Oops, something went wrong.