Class PemParser

java.lang.Object
org.cryptacular.pem.PemParser

final class PemParser extends Object
Parser to decode PEM data into a PemObject.
  • Constructor Details

    • PemParser

      PemParser()
  • Method Details

    • parse

      PemObject parse(byte[] encoded)
      Parses the encoded bytes.
      Parameters:
      encoded - to parse
      Returns:
      new PEM object
    • parse

      private PemObject parse(BufferedReader reader) throws IOException
      Parses the supplied reader.
      Parameters:
      reader - to parse
      Returns:
      new PEM object
      Throws:
      IOException
    • parseInternal

      private static PemObject parseInternal(BufferedReader reader, Descriptor descriptor) throws IOException
      Reads the contents of the PEM data between the BEGIN and END markers by format specified.
      Parameters:
      reader - BufferedReader reader that contains the data to parse
      descriptor - Descriptor regarding the PEM encoded format (see Descriptor)
      Returns:
      ExtendedPemObject instance with the data read
      Throws:
      IOException - In case of exceptions reading the buffer
    • parseHeader

      private static org.bouncycastle.util.io.pem.PemHeader parseHeader(BufferedReader reader, String line, Format format) throws IOException
      Reads a header line which takes into account header types from RFC 1421 invalid input: '&' RFC 4716
      Parameters:
      reader - BufferedReader instance instantiated with PEM file data
      line - Current line read in the buffer
      format - RFC format governing the PEM file
      Returns:
      PemHeader if a header value pair could be successfully read, otherwise null is returned
      Throws:
      IOException - In case of any read errors in the buffer
    • assertLineLength

      private static void assertLineLength(Format format, int maxLineLength) throws IllegalArgumentException
      Throws an exception if the data contains rules restricted by their respective RFCs.
      Parameters:
      format - Format which governs this PEM data
      maxLineLength - maximum length in b64buffer lines prior to concatenation
      Throws:
      IllegalArgumentException - In case of a constraint violation
    • parseDescriptor

      private static Descriptor parseDescriptor(BufferedReader reader) throws IOException
      Determines the RFC format governing the PEM file in the reader and constructs a Descriptor accordingly.
      Parameters:
      reader - BufferedReader instance instantiated with PEM file data
      Returns:
      Populated Descriptor instance or null if the descriptor could not be parsed
      Throws:
      IOException - In case of exceptions reading the buffer, or malformed PEM data
    • getFormat

      private static Format getFormat(String explanatoryText, String pemType, boolean isRFC4716Markers)
      Determines the RFC governing the format of the PEM file based of the parameters provided.
      Parameters:
      explanatoryText - Explanatory text is only allowed in RFC 7468
      pemType - All types begin with PGP in RFC 2440
      isRFC4716Markers - It either starts with four dashes (RFC RFC4716) or five (RFC 1421)
      Returns:
      Format determined (see Descriptor.getFormat())
    • getPemType

      private static String getPemType(boolean isRFC4716Markers, String firstPemLine)
      Returns the message type based on the marker format provided.
      Parameters:
      isRFC4716Markers - isRFC4716Markers (either four dashes and a space, or five dashes)
      firstPemLine - First line of the PEM file
      Returns:
      PEM type or null if the type cannot be determined
    • readExplanatoryText

      private static String readExplanatoryText(BufferedReader reader) throws IOException
      Reads the explanatory text as described by RFC 7468 5.2. Method simply reads a line until a known header marker is found.
      Parameters:
      reader - only the explanatory text will actually be read off the reader
      Returns:
      Explanatory text
      Throws:
      IOException - In case of errors reading the buffer
    • peekNextLine

      private static String peekNextLine(BufferedReader reader, int maximumReadLength) throws IOException
      Reads the next line in a BufferedReader instance without consuming it from the buffer
      Parameters:
      reader - BufferedReader instance
      maximumReadLength - Maximum number of characters to peek
      Returns:
      Next line
      Throws:
      IOException - In case of errors reading the buffer