Class CertificateManager

java.lang.Object
ecmwf.common.security.CertificateManager

public final class CertificateManager extends Object
The Class CertificateManager.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Immutable snapshot of the metadata fields of an X.509 certificate that are displayed in the administration UI.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Alias used for the single entry stored in every managed keystore.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    activatePendingKey(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 void
    discardPendingKey(String keystorePath)
    Deletes the pending key after successful activation or when an administrator cancels a request.
    static void
    ensureSelfSigned(String keystorePath, String keystorePassword, String hostname)
    Ensures a self-signed certificate exists at keystorePath.
    static void
    ensureSelfSigned(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 String
    exportPublicCertPem(String keystorePath, String keystorePassword, String keystoreType)
    Exports the public certificate from the keystore at keystorePath as a PEM-encoded string.
    static String
    generateCsr(String keystorePath, String keystorePassword)
    Generates a PKCS#10 Certificate Signing Request (CSR) for the private key stored in the keystore at keystorePath and returns it as a PEM string.
    static String
    generateCsr(String keystorePath, String keystorePassword, String hostname)
    Generates a PKCS#10 Certificate Signing Request (CSR).
    static String
    generateNewKeyCsr(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 void
    generateSelfSigned(String keystorePath, String keystorePassword, String hostname)
    Generates a new RSA-2048 / SHA-256 self-signed certificate and stores it as a PKCS#12 keystore at keystorePath, overwriting any existing file.
    getCertificateInfo(String keystorePath, String keystorePassword, String keystoreType)
    Reads the certificate stored under ALIAS in the keystore at keystorePath and returns a CertificateManager.CertificateInfo snapshot.
    static boolean
    hasPendingKey(String keystorePath)
    Returns whether a pending new-key request exists for the active keystore.
    static void
    importCertificate(String targetKeystorePath, String targetPassword, byte[] inputBytes, String inputPassword)
    Imports a certificate (and optional private key) from raw bytes into the keystore at targetKeystorePath, replacing any existing entry.
    static boolean
    Returns true when the certificate is self-signed, i.e. when its issuer DN equals its subject DN.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ALIAS

      public static final String 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 at keystorePath. 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 created
      keystorePassword - password to protect the keystore and private key
      hostname - 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 created
      keystorePassword - password to protect the keystore and private key
      hostname - the CN / SAN hostname to embed when generating a new certificate
      keystoreType - 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 at keystorePath, overwriting any existing file.
      Parameters:
      keystorePath - destination path for the PKCS#12 keystore
      keystorePassword - password to protect the keystore and private key
      hostname - 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 under ALIAS in the keystore at keystorePath and returns a CertificateManager.CertificateInfo snapshot.
      Parameters:
      keystorePath - path to the PKCS#12 (or JKS) keystore
      keystorePassword - password for the keystore
      keystoreType - "PKCS12" or "JKS"
      Returns:
      certificate metadata, or null if the file does not exist
      Throws:
      Exception - on any I/O or crypto error
    • isSelfSigned

      public static boolean isSelfSigned(X509Certificate cert)
      Returns true when the certificate is self-signed, i.e. when its issuer DN equals its subject DN.
      Parameters:
      cert - the X.509 certificate to inspect
      Returns:
      true if self-signed
    • generateCsr

      public static String generateCsr(String keystorePath, String keystorePassword) throws Exception
      Generates a PKCS#10 Certificate Signing Request (CSR) for the private key stored in the keystore at keystorePath and returns it as a PEM string.
      Parameters:
      keystorePath - path to the PKCS#12 keystore containing the private key
      keystorePassword - 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). If hostname is 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 key
      keystorePassword - password for the keystore
      hostname - optional CN to embed; null to 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 it
      keystorePassword - password protecting both keystores
      hostname - CSR Common Name
      dnsNames - DNS Subject Alternative Names
      ipAddresses - IP Subject Alternative Names
      Returns:
      PEM-encoded CSR
      Throws:
      Exception - if generation or writing fails, or a pending key already exists
    • hasPendingKey

      public static boolean hasPendingKey(String keystorePath)
      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

      public static void discardPendingKey(String keystorePath) throws Exception
      Deletes the pending key after successful activation or when an administrator cancels a request.
      Throws:
      Exception
    • 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 at targetKeystorePath, 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 written
      targetPassword - password for the target keystore
      inputBytes - raw bytes of the certificate/keystore to import
      inputPassword - password for the input file (may be null for 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 at keystorePath as a PEM-encoded string.
      Parameters:
      keystorePath - path to the PKCS#12 (or JKS) keystore
      keystorePassword - password for the keystore
      keystoreType - "PKCS12" or "JKS"
      Returns:
      PEM-encoded certificate
      Throws:
      Exception - on any I/O or crypto error