Class PEMFile

java.lang.Object
org.apache.tomcat.util.net.jsse.PEMFile

public class PEMFile extends Object
RFC 1421 PEM file containing X509 certificates or private keys.
  • Constructor Details

    • PEMFile

      public PEMFile(String filename) throws IOException, GeneralSecurityException
      Construct a PEMFile from the given file without a password.
      Parameters:
      filename - The path to the PEM file
      Throws:
      IOException - If the file cannot be read
      GeneralSecurityException - If the file cannot be parsed
    • PEMFile

      public PEMFile(String filename, String password) throws IOException, GeneralSecurityException
      Construct a PEMFile from the given file with the given password.
      Parameters:
      filename - The path to the PEM file
      password - The password to decrypt any encrypted private key
      Throws:
      IOException - If the file cannot be read
      GeneralSecurityException - If the file cannot be parsed
    • PEMFile

      public PEMFile(String filename, String password, String keyAlgorithm) throws IOException, GeneralSecurityException
      Construct a PEMFile from the given file with the given password and key algorithm.
      Parameters:
      filename - The path to the PEM file
      password - The password to decrypt any encrypted private key
      keyAlgorithm - The key algorithm, or null to auto-detect
      Throws:
      IOException - If the file cannot be read
      GeneralSecurityException - If the file cannot be parsed
    • PEMFile

      public PEMFile(String filename, String password, String passwordFilename, String keyAlgorithm) throws IOException, GeneralSecurityException
      Construct a PEMFile from the given file with the password read from a separate file.
      Parameters:
      filename - The path to the PEM file
      password - The password to decrypt any encrypted private key
      passwordFilename - The path to the file containing the password
      keyAlgorithm - The key algorithm, or null to auto-detect
      Throws:
      IOException - If the file cannot be read
      GeneralSecurityException - If the file cannot be parsed
    • PEMFile

      public PEMFile(String filename, InputStream fileStream, String password, String keyAlgorithm) throws IOException, GeneralSecurityException
      Construct a PEMFile from the given input stream with the given password and key algorithm.
      Parameters:
      filename - The filename to mention in error messages
      fileStream - The input stream containing the PEM data
      password - The password to decrypt any encrypted private key
      keyAlgorithm - The key algorithm, or null to auto-detect
      Throws:
      IOException - If the stream cannot be read
      GeneralSecurityException - If the data cannot be parsed
    • PEMFile

      public PEMFile(String filename, InputStream fileStream, String password, String passwordFilename, InputStream passwordFileStream, String keyAlgorithm) throws IOException, GeneralSecurityException
      Construct a PEMFile from the given input stream with the given password and key algorithm.
      Parameters:
      filename - the filename to mention in error messages, not used for anything else.
      fileStream - the stream containing the pem(s).
      password - password to load the pem objects.
      passwordFilename - the password filename to mention in error messages, not used for anything else.
      passwordFileStream - stream containing the password to load the pem objects.
      keyAlgorithm - the algorithm to help to know how to load the objects (guessed if null).
      Throws:
      IOException - if input can't be read.
      GeneralSecurityException - if input can't be parsed/loaded.
  • Method Details

    • toPEM

      public static String toPEM(X509Certificate certificate) throws CertificateEncodingException
      Converts an X509 certificate to PEM format.
      Parameters:
      certificate - The certificate to convert
      Returns:
      The PEM-encoded certificate string
      Throws:
      CertificateEncodingException - If the certificate cannot be encoded
    • getCertificates

      public List<X509Certificate> getCertificates()
      Returns the list of X509 certificates loaded from this PEM file.
      Returns:
      the list of X509 certificates
    • getPrivateKey

      public PrivateKey getPrivateKey()
      Returns the private key loaded from this PEM file.
      Returns:
      the private key, or null if no private key was loaded