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

Use @Certificates instead of keytool-maven-plugin #1525

Merged
merged 1 commit into from
Sep 25, 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
206 changes: 5 additions & 201 deletions integration-tests/santuario-xmlsec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.certs</groupId>
<artifactId>smallrye-certificate-generator-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -48,207 +53,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>keytool-maven-plugin</artifactId>
<configuration>
<validity>3650</validity>
<keyalg>RSA</keyalg>
<skip>${keytool.skip}</skip>
</configuration>
<executions>
<!-- CA -->
<execution>
<id>generate-cxfca-keypair</id>
<phase>generate-sources</phase>
<goals>
<goal>clean</goal>
<goal>generateKeyPair</goal>
</goals>
<configuration>
<alias>cxfca</alias>
<dname>CN=cxfca, OU=eng, O=apache.org</dname>
<exts>
<ext>bc:c=ca:true,pathlen:2147483647</ext>
<ext>IssuerAlternativeName=DNS:NOT-FOR-PRODUCTION-USE</ext>
</exts>
<keypass>cxfca-password</keypass>
<storepass>cxfca-password</storepass>
<keystore>${project.build.outputDirectory}/cxfca.jks</keystore>
</configuration>
</execution>
<execution>
<id>export-cxfca-certificate</id>
<phase>generate-sources</phase>
<goals>
<goal>exportCertificate</goal>
</goals>
<configuration>
<alias>cxfca</alias>
<keystore>${project.build.outputDirectory}/cxfca.jks</keystore>
<rfc>true</rfc>
<storepass>cxfca-password</storepass>
<file>${project.build.outputDirectory}/cxfca.pem</file>
</configuration>
</execution>
<!-- Myclient -->
<execution>
<id>generate-myclient-keypair</id>
<phase>generate-sources</phase>
<goals>
<goal>clean</goal>
<goal>generateKeyPair</goal>
</goals>
<configuration>
<alias>myclient</alias>
<dname>CN=myclient, OU=eng, O=apache.org</dname>
<exts>
<ext>bc:c=ca:true,pathlen:2147483647</ext>
<ext>IssuerAlternativeName=DNS:NOT-FOR-PRODUCTION-USE</ext>
</exts>
<keypass>myclient-keystore-password</keypass>
<storepass>myclient-keystore-password</storepass>
<keystore>${project.build.outputDirectory}/myclient-keystore.jks</keystore>
</configuration>
</execution>
<execution>
<id>generate-myclient-certificate-request</id>
<phase>generate-sources</phase>
<goals>
<goal>generateCertificateRequest</goal>
</goals>
<configuration>
<alias>myclient</alias>
<storepass>myclient-keystore-password</storepass>
<keystore>${project.build.outputDirectory}/myclient-keystore.jks</keystore>
<file>${project.build.outputDirectory}/myclient.csr</file>
</configuration>
</execution>
<execution>
<id>sign-myclient-certificate</id>
<phase>generate-sources</phase>
<goals>
<goal>generateCertificate</goal>
</goals>
<configuration>
<alias>cxfca</alias>
<storepass>cxfca-password</storepass>
<keystore>${project.build.outputDirectory}/cxfca.jks</keystore>
<rfc>true</rfc>
<infile>${project.build.outputDirectory}/myclient.csr</infile>
<outfile>${project.build.outputDirectory}/myclient.pem</outfile>
</configuration>
</execution>
<execution>
<id>import-cxfca-certificate-to-myclientkey-keystore</id>
<phase>generate-sources</phase>
<goals>
<goal>importCertificate</goal>
</goals>
<configuration>
<alias>cxfca</alias>
<trustcacerts>true</trustcacerts>
<noprompt>true</noprompt>
<storepass>myclient-keystore-password</storepass>
<keystore>${project.build.outputDirectory}/myclient-keystore.jks</keystore>
<file>${project.build.outputDirectory}/cxfca.pem</file>
</configuration>
</execution>
<execution>
<id>import-signed-myclientkey-certificate-to-myclient-keystore</id>
<phase>generate-sources</phase>
<goals>
<goal>importCertificate</goal>
</goals>
<configuration>
<alias>myclient</alias>
<trustcacerts>true</trustcacerts>
<noprompt>true</noprompt>
<storepass>myclient-keystore-password</storepass>
<keystore>${project.build.outputDirectory}/myclient-keystore.jks</keystore>
<file>${project.build.outputDirectory}/myclient.pem</file>
</configuration>
</execution>
<!-- Myservice -->
<execution>
<id>generate-myservice-keypair</id>
<phase>generate-sources</phase>
<goals>
<goal>clean</goal>
<goal>generateKeyPair</goal>
</goals>
<configuration>
<alias>myservice</alias>
<dname>CN=myservice, OU=eng, O=apache.org</dname>
<exts>
<ext>bc:c=ca:true,pathlen:2147483647</ext>
<ext>IssuerAlternativeName=DNS:NOT-FOR-PRODUCTION-USE</ext>
</exts>
<keypass>myservice-keystore-password</keypass>
<storepass>myservice-keystore-password</storepass>
<keystore>${project.build.outputDirectory}/myservice-keystore.jks</keystore>
</configuration>
</execution>
<execution>
<id>generate-myservice-certificate-request</id>
<phase>generate-sources</phase>
<goals>
<goal>generateCertificateRequest</goal>
</goals>
<configuration>
<alias>myservice</alias>
<storepass>myservice-keystore-password</storepass>
<keystore>${project.build.outputDirectory}/myservice-keystore.jks</keystore>
<file>${project.build.outputDirectory}/myservice.csr</file>
</configuration>
</execution>
<execution>
<id>sign-myservice-certificate</id>
<phase>generate-sources</phase>
<goals>
<goal>generateCertificate</goal>
</goals>
<configuration>
<alias>cxfca</alias>
<storepass>cxfca-password</storepass>
<keystore>${project.build.outputDirectory}/cxfca.jks</keystore>
<rfc>true</rfc>
<infile>${project.build.outputDirectory}/myservice.csr</infile>
<outfile>${project.build.outputDirectory}/myservice.pem</outfile>
</configuration>
</execution>
<execution>
<id>import-cxfca-certificate-to-myservice-keystore</id>
<phase>generate-sources</phase>
<goals>
<goal>importCertificate</goal>
</goals>
<configuration>
<alias>cxfca</alias>
<trustcacerts>true</trustcacerts>
<noprompt>true</noprompt>
<storepass>myservice-keystore-password</storepass>
<keystore>${project.build.outputDirectory}/myservice-keystore.jks</keystore>
<file>${project.build.outputDirectory}/cxfca.pem</file>
</configuration>
</execution>
<execution>
<id>import-signed-myservice-certificate-to-myservice-keystore</id>
<phase>generate-sources</phase>
<goals>
<goal>importCertificate</goal>
</goals>
<configuration>
<alias>myservice</alias>
<trustcacerts>true</trustcacerts>
<noprompt>true</noprompt>
<storepass>myservice-keystore-password</storepass>
<keystore>${project.build.outputDirectory}/myservice-keystore.jks</keystore>
<file>${project.build.outputDirectory}/myservice.pem</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@
@ApplicationScoped
public class XmlsecResource {

public static final String LOCALHOST_KEYSTORE_PASSWORD = "myservice-keystore-password";
Dismissed Show dismissed Hide dismissed

public static final String CLIENT_KEYSTORE_PASSWORD = "myclient-keystore-password";
Dismissed Show dismissed Hide dismissed

public static final List<QName> PAYMENT_INFO = List.of(new QName("urn:example:po", "PaymentInfo"));

private final KeyStore keyStore;

public XmlsecResource() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
// Set up the Key
keyStore = KeyStore.getInstance("jks");
keyStore = KeyStore.getInstance("pkcs12");
keyStore.load(
this.getClass().getClassLoader().getResource("myservice-keystore.jks").openStream(),
"myservice-keystore-password".toCharArray());
this.getClass().getClassLoader().getResource("localhost-keystore.p12").openStream(),
LOCALHOST_KEYSTORE_PASSWORD.toCharArray());
}

/**
Expand All @@ -62,7 +66,7 @@
@POST
@Path("/{encryption}/encrypt")
public byte[] encrypt(byte[] plaintext, @PathParam("encryption") Encryption encryption) throws Exception {
X509Certificate cert = (X509Certificate) keyStore.getCertificate("myservice");
X509Certificate cert = (X509Certificate) keyStore.getCertificate("localhost");

// Set up the secret Key
KeyGenerator keygen = KeyGenerator.getInstance("AES");
Expand All @@ -86,7 +90,7 @@
@POST
@Path("/{encryption}/decrypt")
public byte[] decrypt(byte[] encrypted, @PathParam("encryption") Encryption encryption) throws Exception {
Key privateKey = keyStore.getKey("myservice", "myservice-keystore-password".toCharArray());
Key privateKey = keyStore.getKey("localhost", LOCALHOST_KEYSTORE_PASSWORD.toCharArray());
return encryption.decrypt(encrypted, "http://www.w3.org/2009/xmlenc11#aes256-gcm", privateKey);
}

Expand All @@ -104,12 +108,12 @@
public byte[] signEnveloped(byte[] plaintext, @PathParam("signature") Signing signature) throws Exception {

// Set up the Key
KeyStore keyStore = KeyStore.getInstance("jks");
KeyStore keyStore = KeyStore.getInstance("pkcs12");
keyStore.load(
this.getClass().getClassLoader().getResource("myclient-keystore.jks").openStream(),
"myclient-keystore-password".toCharArray());
Key key = keyStore.getKey("myclient", "myclient-keystore-password".toCharArray());
X509Certificate cert = (X509Certificate) keyStore.getCertificate("myclient");
this.getClass().getClassLoader().getResource("localhost-client-keystore.p12").openStream(),
LOCALHOST_KEYSTORE_PASSWORD.toCharArray());
Key key = keyStore.getKey("client", CLIENT_KEYSTORE_PASSWORD.toCharArray());
X509Certificate cert = (X509Certificate) keyStore.getCertificate("client");
return signature.sign(plaintext, key, cert, PAYMENT_INFO);
}

Expand All @@ -127,11 +131,11 @@
public void verifyEnveloped(byte[] plaintext, @PathParam("signature") Signing signature) throws Exception {

// Set up the Key
KeyStore keyStore = KeyStore.getInstance("jks");
KeyStore keyStore = KeyStore.getInstance("pkcs12");
keyStore.load(
this.getClass().getClassLoader().getResource("myclient-keystore.jks").openStream(),
"myclient-keystore-password".toCharArray());
X509Certificate cert = (X509Certificate) keyStore.getCertificate("myclient");
this.getClass().getClassLoader().getResource("localhost-client-keystore.p12").openStream(),
LOCALHOST_KEYSTORE_PASSWORD.toCharArray());
X509Certificate cert = (X509Certificate) keyStore.getCertificate("client");
signature.verify(plaintext, cert);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
quarkus.native.resources.includes=myclient-keystore.jks,myservice-keystore.jks
quarkus.native.resources.includes = localhost-client-keystore.p12,localhost-keystore.p12
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@
import org.xml.sax.SAXException;

import io.quarkus.test.junit.QuarkusTest;

import io.smallrye.certs.Format;
import io.smallrye.certs.junit5.Alias;
import io.smallrye.certs.junit5.Certificate;
import io.smallrye.certs.junit5.Certificates;

@Certificates(baseDir = "target/classes", //
certificates = @Certificate( //
name = "localhost", //
password = XmlsecResource.LOCALHOST_KEYSTORE_PASSWORD, //
aliases = @Alias(//
name = "client", //
password = io.quarkiverse.xmlsec.it.XmlsecResource.CLIENT_KEYSTORE_PASSWORD, //
client = true), //
formats = { Format.PKCS12 }))
@QuarkusTest
public class XmlsecResourceTest {

Expand Down
Loading