tags = new LinkedHashMap<>();
+ for (JsonNode tag : tagsNode) {
+ String key = tag.path("Key").asText(null);
+ if (key != null) {
+ tags.put(key, tag.path("Value").asText(null));
+ }
+ }
+ return tags.isEmpty() ? null : tags;
+ }
+
+ /**
+ * Decodes a base64-encoded blob field. AWS SDKs send binary fields as
+ * base64-encoded strings. If the value is already in PEM format, it is returned as-is.
+ */
+ private String decodeBlob(String value) {
+ if (value == null || value.startsWith("-----")) {
+ return value;
+ }
+ try {
+ return new String(Base64.getDecoder().decode(value), java.nio.charset.StandardCharsets.UTF_8);
+ } catch (IllegalArgumentException e) {
+ return value;
+ }
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudhsmv2/CloudHsmV2Service.java b/src/main/java/io/github/hectorvent/floci/services/cloudhsmv2/CloudHsmV2Service.java
new file mode 100644
index 0000000000..ca644c371c
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudhsmv2/CloudHsmV2Service.java
@@ -0,0 +1,794 @@
+package io.github.hectorvent.floci.services.cloudhsmv2;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+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.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 jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.bouncycastle.asn1.x500.X500Name;
+import org.bouncycastle.cert.X509v3CertificateBuilder;
+import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
+import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
+
+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;
+import java.io.StringWriter;
+import java.math.BigInteger;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.SecureRandom;
+import java.security.cert.X509Certificate;
+import io.github.hectorvent.floci.services.cloudhsmv2.model.Backup;
+import io.github.hectorvent.floci.services.cloudhsmv2.model.BackupRetentionPolicy;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.time.temporal.ChronoUnit;
+import java.util.stream.Collectors;
+import java.time.Instant;
+import java.util.*;
+
+import io.github.hectorvent.floci.services.ec2.Ec2Service;
+import io.github.hectorvent.floci.services.ec2.model.Subnet;
+
+/**
+ * CloudHSM v2 service implementation for the local emulator.
+ *
+ * Provides cluster initialization and lifecycle management operations
+ * compatible with the AWS CloudHSM v2 API. Clusters follow a strict lifecycle:
+ * {@code CREATE_IN_PROGRESS → UNINITIALIZED → INITIALIZED → ACTIVE}.
+ *
+ * @see AWS CloudHSM v2 API Reference
+ */
+@ApplicationScoped
+public class CloudHsmV2Service {
+
+ private static final Logger LOG = Logger.getLogger(CloudHsmV2Service.class);
+ private static final String DEFAULT_HSM_TYPE = "hsm1.medium";
+ private static final String DEFAULT_BACKUP_POLICY = "DEFAULT";
+
+ private final SecureRandom SECURE_RANDOM = new SecureRandom();
+ private final StorageBackend clusters;
+ private final StorageBackend backups;
+ private final Ec2Service ec2Service;
+
+ @Inject
+ public CloudHsmV2Service(StorageFactory storageFactory, Ec2Service ec2Service) {
+ this.clusters = storageFactory.create("cloudhsmv2", "cloudhsmv2-clusters.json",
+ new TypeReference