Class PEMReader

java.lang.Object
org.apache.cassandra.security.PEMReader

public final class PEMReader extends Object
This is a helper class to read private keys and X509 certifificates encoded based on PEM (RFC 1421) format. It can read Password Based Encrypted (PBE henceforth) private keys as well as non-encrypted private keys along with the X509 certificates/cert-chain based on the textual encoding defined in the RFC 7468

The input private key must be in PKCS#8 format.

It returns PKCS#8 formatted private key and X509 certificates.

  • Field Details

    • SUPPORTED_PRIVATE_KEY_ALGORITHMS

      public static final Set<String> SUPPORTED_PRIVATE_KEY_ALGORITHMS
      The private key can be with any of these algorithms in order for this read to successfully parse it. Currently, supported algorithms are,
           RSA, DSA or EC
       
      The first one to be evaluated is RSA, being the most common for private keys.
  • Constructor Details

    • PEMReader

      public PEMReader()
  • Method Details

    • extractPrivateKey

      public static PrivateKey extractPrivateKey(String unencryptedPEMKey) throws IOException, GeneralSecurityException
      Extracts private key from the PEM content for the private key, assuming its not PBE.
      Parameters:
      unencryptedPEMKey - private key stored as PEM content
      Returns:
      PrivateKey upon successful reading of the private key
      Throws:
      IOException - in case PEM reading fails
      GeneralSecurityException - in case any issue encountered while reading the private key
    • extractPrivateKey

      public static PrivateKey extractPrivateKey(String pemKey, String keyPassword) throws IOException, GeneralSecurityException
      Extracts private key from the Password Based Encrypted PEM content for the private key.
      Parameters:
      pemKey - PBE private key stored as PEM content
      keyPassword - password to be used for the private key decryption
      Returns:
      PrivateKey upon successful reading of the private key
      Throws:
      IOException - in case PEM reading fails
      GeneralSecurityException - in case any issue encountered while reading the private key
    • extractCertificates

      public static Certificate[] extractCertificates(String pemCerts) throws GeneralSecurityException
      Extracts the certificates/cert-chain from the PEM content.
      Parameters:
      pemCerts - certificates/cert-chain stored as PEM content
      Returns:
      X509 certiificate list
      Throws:
      GeneralSecurityException - in case any issue encountered while reading the certificates