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 voidensureSelfSigned(String keystorePath, String keystorePassword, String hostname) Ensures a self-signed certificate exists atkeystorePath.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 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 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. The method is idempotent; it does nothing when a certificate is already present.- 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
-
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
-
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
-