Class PKCS8PEMFileReader

java.lang.Object
com.unboundid.util.ssl.cert.PKCS8PEMFileReader
All Implemented Interfaces:
Closeable, AutoCloseable

@ThreadSafety(level=NOT_THREADSAFE) public final class PKCS8PEMFileReader extends Object implements Closeable
This class provides a mechanism for reading a PEM-encoded PKCS #8 private key from a specified file. While it is generally expected that a private key file will contain only a single key, it is possible to read multiple keys from the same file. Each private key should consist of the following:
  • A line containing only the string "-----BEGIN PRIVATE KEY-----" or ""-----BEGIN RSA PRIVATE KEY-----.
  • One or more lines representing the base64-encoded representation of the bytes that comprise the PKCS #8 private key.
  • A line containing only the string "-----END PRIVATE KEY-----" or ""-----END RSA PRIVATE KEY-----.


Any spaces that appear at the beginning or end of each line will be ignored. Empty lines and lines that start with the octothorpe (#) character will also be ignored.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The header string that should appear on a line by itself before the base64-encoded representation of the bytes that comprise an encrypted PKCS #8 private key.
    static final String
    The header string that should appear on a line by itself before the base64-encoded representation of the bytes that comprise a PKCS #8 private key.
    static final String
    An alternative begin header string that may appear on a line by itself for cases in which the certificate uses an RSA key pair.
    static final String
    The footer string that should appear on a line by itself after the base64-encoded representation of the bytes that comprise an encrypted PKCS #8 private key.
    static final String
    The footer string that should appear on a line by itself after the base64-encoded representation of the bytes that comprise a PKCS #8 private key.
    static final String
    An alternative end footer string that may appear on a line by itself for cases in which the certificate uses an RSA key pair.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new PKCS #8 PEM file reader that will read private key information from the specified file.
    Creates a new PKCS #8 PEM file reader that will read private key information from the provided input stream.
    Creates a new PKCS #8 PEM file reader that will read private key information from the specified file.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this PKCS #8 PEM file reader.
    Reads the next private key from the PEM file.
    readPrivateKey(char[] encryptionPassword)
    Reads the next private key from the PEM file.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • PKCS8PEMFileReader

      public PKCS8PEMFileReader(@NotNull String pemFilePath) throws IOException
      Creates a new PKCS #8 PEM file reader that will read private key information from the specified file.
      Parameters:
      pemFilePath - The path to the PEM file from which the private key should be read. This must not be null and the file must exist.
      Throws:
      IOException - If a problem occurs while attempting to open the file for reading.
    • PKCS8PEMFileReader

      public PKCS8PEMFileReader(@NotNull File pemFile) throws IOException
      Creates a new PKCS #8 PEM file reader that will read private key information from the specified file.
      Parameters:
      pemFile - The PEM file from which the private key should be read. This must not be null and the file must exist.
      Throws:
      IOException - If a problem occurs while attempting to open the file for reading.
    • PKCS8PEMFileReader

      Creates a new PKCS #8 PEM file reader that will read private key information from the provided input stream.
      Parameters:
      inputStream - The input stream from which the private key should be read. This must not be null and it must be open for reading.
  • Method Details

    • readPrivateKey

      Reads the next private key from the PEM file. The private key must be unencrypted.
      Returns:
      The private key that was read, or null if the end of the file has been reached.
      Throws:
      IOException - If a problem occurs while trying to read data from the PEM file.
      CertException - If a problem occurs while trying to interpret data read from the PEM file as a PKCS #8 private key.
    • readPrivateKey

      @Nullable public PKCS8PrivateKey readPrivateKey(@Nullable char[] encryptionPassword) throws IOException, CertException
      Reads the next private key from the PEM file. The private key may optionally be encrypted.
      Parameters:
      encryptionPassword - The password used to encrypt the private key. It must not be null if the private key is encrypted. It may be null if the private key is not encrypted.
      Returns:
      The private key that was read, or null if the end of the file has been reached.
      Throws:
      IOException - If a problem occurs while trying to read data from the PEM file.
      CertException - If a problem occurs while trying to interpret data read from the PEM file as a PKCS #8 private key.
    • close

      public void close() throws IOException
      Closes this PKCS #8 PEM file reader.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - If a problem is encountered while attempting to close the reader.