fix(cloudhsm): add issuer key identifiers to hardware chain (#4707)

* fix(cloudhsm): add issuer key identifiers to hardware chain

* fix(cloudhsm): certify the shared hardware key twice

* chore(cloudhsm): sort service imports
This commit is contained in:
XuJian authored and GitHub committed 2026-09-30 06:00:13 -04:00
1 parent 70efa35fe3
commit a5dd3c2f2e
2 files changed
+137 -19

No files matched your search

@@ -5,23 +5,26 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
import io.github.hectorvent.floci.services.cloudhsmv2.model.Backup;
import io.github.hectorvent.floci.services.cloudhsmv2.model.BackupRetentionPolicy;
import io.github.hectorvent.floci.services.cloudhsmv2.model.Certificates;
import io.github.hectorvent.floci.services.cloudhsmv2.model.Cluster;
import io.github.hectorvent.floci.services.cloudhsmv2.model.ClusterState;
import io.github.hectorvent.floci.services.cloudhsmv2.model.Hsm;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.openssl.PEMParser;
import org.jboss.logging.Logger;
import java.io.StringReader;
@@ -29,15 +32,16 @@ import java.io.StringWriter;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import io.github.hectorvent.floci.services.cloudhsmv2.model.Backup;
import io.github.hectorvent.floci.services.cloudhsmv2.model.BackupRetentionPolicy;
import java.time.temporal.ChronoUnit;
import java.util.stream.Collectors;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.util.*;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* CloudHSM v2 service implementation for the local emulator.
@@ -170,20 +174,40 @@ public class CloudHsmV2Service {
certs.setClusterCsr(generateCsr(clusterId));
try {
KeyPair mfrKeyPair = generateKeyPair();
X500Name mfrName = new X500Name("CN=HSM Manufacturer CA,O=AWS,C=US");
certs.setManufacturerHardwareCertificate(certificateGenerator.toPem(certificateGenerator.signCertificate(
mfrName, mfrKeyPair.getPublic(), mfrName, mfrKeyPair.getPrivate(), List.of(), true, null, 365)));
Instant certificateNotBefore = Instant.now();
Instant certificateNotAfter = certificateNotBefore.plus(365, ChronoUnit.DAYS);
Instant rootNotAfter = certificateNotBefore.plus(3650, ChronoUnit.DAYS);
KeyPair awsKeyPair = generateKeyPair();
X500Name awsName = new X500Name("CN=AWS CloudHSM Hardware CA,O=AWS,C=US");
KeyPair manufacturerRootKeyPair = generateKeyPair();
X500Name manufacturerRootName = new X500Name("CN=HSM Manufacturer Root CA,O=AWS,C=US");
X509Certificate manufacturerRoot = certificateGenerator.signCertificate(
manufacturerRootName, manufacturerRootKeyPair.getPublic(), manufacturerRootName,
manufacturerRootKeyPair.getPrivate(), List.of(), true, null,
certificateNotBefore, rootNotAfter);
KeyPair awsRootKeyPair = generateKeyPair();
X500Name awsRootName = new X500Name("CN=AWS CloudHSM Root CA,O=AWS,C=US");
X509Certificate awsRoot = certificateGenerator.signCertificate(
awsRootName, awsRootKeyPair.getPublic(), awsRootName,
awsRootKeyPair.getPrivate(), List.of(), true, null,
certificateNotBefore, rootNotAfter);
KeyPair hardwareKeyPair = generateKeyPair();
X500Name hardwareName = new X500Name("CN=HSM Hardware " + clusterId + ",O=AWS,C=US");
certs.setManufacturerHardwareCertificate(certificateGenerator.toPem(certificateGenerator.signCertificate(
hardwareName, hardwareKeyPair.getPublic(), manufacturerRootName,
manufacturerRootKeyPair.getPrivate(), List.of(), true, null,
certificateNotBefore, certificateNotAfter, manufacturerRoot.getPublicKey())));
certs.setAwsHardwareCertificate(certificateGenerator.toPem(certificateGenerator.signCertificate(
awsName, awsKeyPair.getPublic(), mfrName, mfrKeyPair.getPrivate(), List.of(), true, null, 365)));
hardwareName, hardwareKeyPair.getPublic(), awsRootName,
awsRootKeyPair.getPrivate(), List.of(), true, null,
certificateNotBefore, certificateNotAfter, awsRoot.getPublicKey())));
KeyPair hsmKeyPair = generateKeyPair();
X500Name hsmName = new X500Name("CN=HSM Instance " + clusterId + ",O=AWS,C=US");
certs.setHsmCertificate(certificateGenerator.toPem(certificateGenerator.signCertificate(
hsmName, hsmKeyPair.getPublic(), awsName, awsKeyPair.getPrivate(), List.of(), false, null, 365)));
hsmName, hsmKeyPair.getPublic(), hardwareName, hardwareKeyPair.getPrivate(), List.of(), false, null,
certificateNotBefore, certificateNotAfter, hardwareKeyPair.getPublic())));
} catch (Exception e) {
LOG.warnv("Failed to generate emulated hardware certs: {0}", e.getMessage());
}