Class CertificateManager
java.lang.Object
ecmwf.common.security.CertificateManager
The Class CertificateManager.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordImmutable snapshot of the metadata fields of an X.509 certificate that are displayed in the administration UI. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidactivatePendingKey(String keystorePath, String keystorePassword, byte[] certificatePem) Uses the pending private key to create a replacement active PKCS#12 keystore from a CA-signed PEM certificate chain.static voiddiscardPendingKey(String keystorePath) Deletes the pending key after successful activation or when an administrator cancels a request.static voidensureSelfSigned(String keystorePath, String keystorePassword, String hostname) Ensures a self-signed certificate exists atkeystorePath.static voidensureSelfSigned(String keystorePath, String keystorePassword, String hostname, String keystoreType) Ensures a certificate exists and normalizes an existing PKCS#12 private-key keystore so it can be consumed by OpenSSL without legacy algorithms.static StringexportPublicCertPem(String keystorePath, String keystorePassword, String keystoreType) Exports the public certificate from the keystore atkeystorePathas a PEM-encoded string.static StringgenerateCsr(String keystorePath, String keystorePassword) Generates a PKCS#10 Certificate Signing Request (CSR) for the private key stored in the keystore atkeystorePathand returns it as a PEM string.static StringgenerateCsr(String keystorePath, String keystorePassword, String hostname) Generates a PKCS#10 Certificate Signing Request (CSR).static StringgenerateNewKeyCsr(String keystorePath, String keystorePassword, String hostname, List<String> dnsNames, List<String> ipAddresses) Generates a CSR with a new RSA key pair and stores that key in a separate, password-protected pending keystore.static voidgenerateSelfSigned(String keystorePath, String keystorePassword, String hostname) Generates a new RSA-2048 / SHA-256 self-signed certificate and stores it as a PKCS#12 keystore atkeystorePath, overwriting any existing file.getCertificateInfo(String keystorePath, String keystorePassword, String keystoreType) Reads the certificate stored underALIASin the keystore atkeystorePathand returns aCertificateManager.CertificateInfosnapshot.static booleanhasPendingKey(String keystorePath) Returns whether a pending new-key request exists for the active keystore.static voidimportCertificate(String targetKeystorePath, String targetPassword, byte[] inputBytes, String inputPassword) Imports a certificate (and optional private key) from raw bytes into the keystore attargetKeystorePath, replacing any existing entry.static booleanisSelfSigned(X509Certificate cert) Returnstruewhen the certificate is self-signed, i.e. when its issuer DN equals its subject DN.
-
Field Details
-
ALIAS
Alias used for the single entry stored in every managed keystore.- See Also:
-
-
Method Details
-
ensureSelfSigned
public static void ensureSelfSigned(String keystorePath, String keystorePassword, String hostname) throws Exception Ensures a self-signed certificate exists atkeystorePath. If the file is absent or empty the method generates a new RSA-2048 / SHA-256 self-signed certificate and stores it as a PKCS#12 keystore at that path. An existing PKCS#12 private-key keystore is normalized to the interoperable format without changing its identity.- Parameters:
keystorePath- path where the PKCS#12 keystore should be createdkeystorePassword- password to protect the keystore and private keyhostname- the CN / SAN hostname to embed in the certificate- Throws:
Exception- if generation or writing fails
-
ensureSelfSigned
public static void ensureSelfSigned(String keystorePath, String keystorePassword, String hostname, String keystoreType) throws Exception Ensures a certificate exists and normalizes an existing PKCS#12 private-key keystore so it can be consumed by OpenSSL without legacy algorithms. JKS keystores are left unchanged.- Parameters:
keystorePath- path where the keystore should be createdkeystorePassword- password to protect the keystore and private keyhostname- the CN / SAN hostname to embed when generating a new certificatekeystoreType- configured type of the existing keystore- Throws:
Exception- if migration, generation, or writing fails
-
generateSelfSigned
public static void generateSelfSigned(String keystorePath, String keystorePassword, String hostname) throws Exception Generates a new RSA-2048 / SHA-256 self-signed certificate and stores it as a PKCS#12 keystore atkeystorePath, overwriting any existing file.- Parameters:
keystorePath- destination path for the PKCS#12 keystorekeystorePassword- password to protect the keystore and private keyhostname- CN and DNS SAN entry for the certificate- Throws:
Exception- if generation or writing fails
-
getCertificateInfo
public static CertificateManager.CertificateInfo getCertificateInfo(String keystorePath, String keystorePassword, String keystoreType) throws Exception Reads the certificate stored underALIASin the keystore atkeystorePathand returns aCertificateManager.CertificateInfosnapshot.- Parameters:
keystorePath- path to the PKCS#12 (or JKS) keystorekeystorePassword- password for the keystorekeystoreType-"PKCS12"or"JKS"- Returns:
- certificate metadata, or
nullif the file does not exist - Throws:
Exception- on any I/O or crypto error
-
isSelfSigned
Returnstruewhen the certificate is self-signed, i.e. when its issuer DN equals its subject DN.- Parameters:
cert- the X.509 certificate to inspect- Returns:
trueif self-signed
-
generateCsr
Generates a PKCS#10 Certificate Signing Request (CSR) for the private key stored in the keystore atkeystorePathand returns it as a PEM string.- Parameters:
keystorePath- path to the PKCS#12 keystore containing the private keykeystorePassword- password for the keystore- Returns:
- PEM-encoded CSR
- Throws:
Exception- on any I/O or crypto error
-
generateCsr
public static String generateCsr(String keystorePath, String keystorePassword, String hostname) throws Exception Generates a PKCS#10 Certificate Signing Request (CSR). Ifhostnameis provided it is used as the CN; otherwise the subject of the existing certificate is reused.- Parameters:
keystorePath- path to the PKCS#12 keystore containing the private keykeystorePassword- password for the keystorehostname- optional CN to embed;nullto reuse the existing certificate subject- Returns:
- PEM-encoded CSR
- Throws:
Exception- on any I/O or crypto error
-
generateNewKeyCsr
public static String generateNewKeyCsr(String keystorePath, String keystorePassword, String hostname, List<String> dnsNames, List<String> ipAddresses) throws Exception Generates a CSR with a new RSA key pair and stores that key in a separate, password-protected pending keystore. The active keystore and certificate are not changed.- Parameters:
keystorePath- active keystore path; the pending keystore is stored beside itkeystorePassword- password protecting both keystoreshostname- CSR Common NamednsNames- DNS Subject Alternative NamesipAddresses- IP Subject Alternative Names- Returns:
- PEM-encoded CSR
- Throws:
Exception- if generation or writing fails, or a pending key already exists
-
hasPendingKey
Returns whether a pending new-key request exists for the active keystore. -
activatePendingKey
public static void activatePendingKey(String keystorePath, String keystorePassword, byte[] certificatePem) throws Exception Uses the pending private key to create a replacement active PKCS#12 keystore from a CA-signed PEM certificate chain. The first certificate must be the leaf certificate matching the pending key.- Throws:
Exception
-
discardPendingKey
-
importCertificate
public static void importCertificate(String targetKeystorePath, String targetPassword, byte[] inputBytes, String inputPassword) throws Exception Imports a certificate (and optional private key) from raw bytes into the keystore attargetKeystorePath, replacing any existing entry. The input can be in PEM, PKCS#12, or JKS format; the method auto-detects the format.- Parameters:
targetKeystorePath- path where the updated PKCS#12 keystore should be writtentargetPassword- password for the target keystoreinputBytes- raw bytes of the certificate/keystore to importinputPassword- password for the input file (may benullfor plain PEM)- Throws:
Exception- if the format is unsupported or import fails
-
exportPublicCertPem
public static String exportPublicCertPem(String keystorePath, String keystorePassword, String keystoreType) throws Exception Exports the public certificate from the keystore atkeystorePathas a PEM-encoded string.- Parameters:
keystorePath- path to the PKCS#12 (or JKS) keystorekeystorePassword- password for the keystorekeystoreType-"PKCS12"or"JKS"- Returns:
- PEM-encoded certificate
- Throws:
Exception- on any I/O or crypto error
-