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

Add FIPS-140-3 2.0 jars #5

Merged
merged 3 commits into from
Oct 18, 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
27 changes: 27 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@
<stripVersion>true</stripVersion>
</configuration>
</execution>
<execution>
<id>copy-fips-libs-2.x</id>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.bouncycastle</groupId>
<artifactId>bc-fips</artifactId>
<version>2.0.0</version>
</artifactItem>
<artifactItem>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-fips</artifactId>
<version>2.0.7</version>
</artifactItem>
<artifactItem>
<groupId>org.bouncycastle</groupId>
<artifactId>bctls-fips</artifactId>
<version>2.0.19</version>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.outputDirectory}/fips-140_3-2.x</outputDirectory>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/io/jenkins/test/fips/FIPS1403BC2x.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.jenkins.test.fips;

import org.apache.commons.io.IOUtils;
import org.kohsuke.MetaInfServices;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;

@MetaInfServices(FIPSTestBundleProvider.class)
public class FIPS1403BC2x implements FIPSTestBundleProvider {

public static final String VERSION = "fips-140_3-2.x";

@Override
public String getVersion() {
return VERSION;
}

@Override
public List<String> getJavaOptions() throws IOException {
return List.of(
"-Dsecurity.useSystemPropertiesFile=false",
// please note == is not a typo, but it makes our file completely override the jvm security file
"-Djava.security.properties==" + writeFIPSJavaSecurityFile().toUri(),
"-Dorg.bouncycastle.fips.approved_only=true",
"-Djavax.net.ssl.trustStoreType=PKCS12",
"-Djenkins.security.FIPS140.COMPLIANCE=true");
}

@Override
public List<File> getBootClasspathFiles() throws IOException {
return List.of(
extractJar("bc-fips.jar").toFile(),
extractJar("bcpkix-fips.jar").toFile(),
extractJar("bctls-fips.jar").toFile());
}

private Path extractJar(String jarName) throws IOException {
// unzip jar files to a temporary directory
URL url = Thread.currentThread().getContextClassLoader().getResource(VERSION + "/" + jarName);
Path bcFips = Files.createTempFile(jarName, "jar");
bcFips.toFile().deleteOnExit();
try (OutputStream os = Files.newOutputStream(bcFips)) {
IOUtils.copy(url.openStream(), os);
}
return bcFips;
}

private Path writeFIPSJavaSecurityFile() throws IOException {
String javaHome = System.getProperty("java.home");
if (javaHome == null) {
throw new IllegalArgumentException("Cannot find java.home property");
}
Path javaSecurity = Paths.get(javaHome, "conf", "security", "java.security");
Properties properties = new Properties();
Path securityFile = Files.createTempFile("java", ".security");
securityFile.toFile().deleteOnExit();
try (InputStream inputStream = Files.newInputStream(javaSecurity);
OutputStream outputStream = Files.newOutputStream(securityFile)) {
properties.load(inputStream);
properties.keySet().removeIf(o -> ((String) o).startsWith("security.provider"));
properties.put(
"security.provider.1",
"org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider C:HYBRID;ENABLE{All};");
properties.put(
"security.provider.2", "org.bouncycastle.jsse.provider.BouncyCastleJsseProvider fips:BCFIPS");
properties.put("security.provider.3", "sun.security.provider.Sun");
properties.put(
"fips.provider.1",
"org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider C:HYBRID;ENABLE{All};");
properties.put("fips.provider.2", "org.bouncycastle.jsse.provider.BouncyCastleJsseProvider fips:BCFIPS");
properties.put("keystore.type", "BCFKS");
// properties.put("securerandom.strongAlgorithms", "PKCS11:SunPKCS11-NSS-FIPS");
properties.put("ssl.KeyManagerFactory.algorithm", "PKIX");
properties.put("fips.keystore.type", "BCFKS");
properties.store(outputStream, "");
}
return securityFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void serviceLoaderEntries() {
.map(ServiceLoader.Provider::get)
.collect(Collectors.toUnmodifiableList());
assertThat(bundles, not(empty()));
assertThat(bundles, hasSize(1));
assertThat(bundles, hasSize(2));
}

@Test
Expand All @@ -39,4 +39,12 @@ public void get_fips1401x() throws Exception {
assertThat(provider.getVersion(), is(FIPS1402BC1x.VERSION));
assertThat(provider.getBootClasspathFiles(), hasSize(3));
}

@Test
public void get_fips1402x() throws Exception {
FIPSTestBundleProvider provider = FIPSTestBundleProvider.get(FIPS1403BC2x.VERSION);
assertThat(provider, notNullValue());
assertThat(provider.getVersion(), is(FIPS1403BC2x.VERSION));
assertThat(provider.getBootClasspathFiles(), hasSize(3));
}
}