Class Pem


  • public final class Pem
    extends Object
    A class containing utilities which can handle the PEM format. See RFC 7468 for more information.
    Author:
    David M. Lloyd
    • Constructor Detail

      • Pem

        public Pem()
    • Method Detail

      • parsePemContent

        public static <R> R parsePemContent​(org.wildfly.common.iteration.CodePointIterator pemContent,
                                            BiFunction<String,​org.wildfly.common.iteration.ByteIterator,​R> contentFunction)
                                     throws IllegalArgumentException
        Parse arbitrary PEM content. The given function is used to parse the content of the PEM representation and produce some result. The PEM type string is passed to the function. If the function throws an exception, that exception is propagated to the caller of this method. If the PEM content is malformed, an exception is thrown. If the trailing PEM content is found to be invalid after the function returns, the function result is discarded and an exception is thrown.
        Type Parameters:
        R - the value return type
        Parameters:
        pemContent - the content to parse (must not be null)
        contentFunction - a function to consume the PEM content and produce a result (must not be null)
        Returns:
        the return value of the function or null if there is no PEM content to parse
        Throws:
        IllegalArgumentException - if there is a problem with processing the content of the PEM data
      • parsePemContent

        public static Iterator<PemEntry<?>> parsePemContent​(org.wildfly.common.iteration.CodePointIterator pemContent)
        Iterate over the contents of a PEM file, returning each entry in sequence.
        Parameters:
        pemContent - the code point iterator over the content (must not be null)
        Returns:
        the iterator (not null)
      • generatePemContent

        public static void generatePemContent​(org.wildfly.common.bytes.ByteStringBuilder target,
                                              String type,
                                              org.wildfly.common.iteration.ByteIterator content)
                                       throws IllegalArgumentException
        Generate PEM content to the given byte string builder. The appropriate header and footer surrounds the base-64 encoded value.
        Parameters:
        target - the target byte string builder (must not be null)
        type - the content type (must not be null)
        content - the content iterator (must not be null)
        Throws:
        IllegalArgumentException - if there is a problem with the data or the type
      • extractDerContent

        public static byte[] extractDerContent​(org.wildfly.common.iteration.CodePointIterator pemContent)
        Extracts the DER content from the given pemContent.
        Parameters:
        pemContent - a CodePointIterator with the PEM content
        Returns:
        a byte array with the DER content
      • parsePemX509Certificate

        public static X509Certificate parsePemX509Certificate​(org.wildfly.common.iteration.CodePointIterator pemContent)
                                                       throws IllegalArgumentException
        Parse an X.509 certificate in PEM format.
        Parameters:
        pemContent - the PEM content (must not be null)
        Returns:
        the certificate (not null)
        Throws:
        IllegalArgumentException - if the certificate could not be parsed for some reason
      • parsePemPublicKey

        public static PublicKey parsePemPublicKey​(org.wildfly.common.iteration.CodePointIterator pemContent)
                                           throws IllegalArgumentException
        Parse a PublicKey in PEM format.
        Parameters:
        pemContent - the PEM content (must not be null)
        Returns:
        the public key (not null)
        Throws:
        IllegalArgumentException - if the public key could not be parsed for some reason
      • generatePemX509Certificate

        public static void generatePemX509Certificate​(org.wildfly.common.bytes.ByteStringBuilder target,
                                                      X509Certificate certificate)
        Generate PEM content containing an X.509 certificate.
        Parameters:
        target - the target byte string builder (must not be null)
        certificate - the X.509 certificate (must not be null)
      • generatePemPublicKey

        public static void generatePemPublicKey​(org.wildfly.common.bytes.ByteStringBuilder target,
                                                PublicKey publicKey)
        Generate PEM content containing a PublicKey.
        Parameters:
        target - the target byte string builder (must not be null)
        publicKey - the PublicKey (must not be null)
      • generatePemPKCS10CertificateSigningRequest

        public static void generatePemPKCS10CertificateSigningRequest​(org.wildfly.common.bytes.ByteStringBuilder target,
                                                                      PKCS10CertificateSigningRequest certificateSigningRequest)
        Generate PEM content containing a PKCS #10 certificate signing request.
        Parameters:
        target - the target byte string builder (must not be null)
        certificateSigningRequest - the PKCS #10 certificate signing request (must not be null)
        Since:
        1.2.0