Package org.cryptacular.pem
Class PemParser
java.lang.Object
org.cryptacular.pem.PemParser
Parser to decode PEM data into a
PemObject.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidassertLineLength(Format format, int maxLineLength) Throws an exception if the data contains rules restricted by their respective RFCs.private static FormatDetermines the RFC governing the format of the PEM file based of the parameters provided.private static StringgetPemType(boolean isRFC4716Markers, String firstPemLine) Returns the message type based on the marker format provided.(package private) PemObjectparse(byte[] encoded) Parses the encoded bytes.private PemObjectparse(BufferedReader reader) Parses the supplied reader.private static DescriptorparseDescriptor(BufferedReader reader) Determines the RFC format governing the PEM file in the reader and constructs aDescriptoraccordingly.private static org.bouncycastle.util.io.pem.PemHeaderparseHeader(BufferedReader reader, String line, Format format) Reads a header line which takes into account header types from RFC 1421 invalid input: '&' RFC 4716private static PemObjectparseInternal(BufferedReader reader, Descriptor descriptor) Reads the contents of the PEM data between the BEGIN and END markers by format specified.private static StringpeekNextLine(BufferedReader reader, int maximumReadLength) Reads the next line in aBufferedReaderinstance without consuming it from the bufferprivate static StringreadExplanatoryText(BufferedReader reader) Reads the explanatory text as described by RFC 7468 5.2.
-
Constructor Details
-
PemParser
PemParser()
-
-
Method Details
-
parse
Parses the encoded bytes.- Parameters:
encoded- to parse- Returns:
- new PEM object
-
parse
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-BufferedReaderreader that contains the data to parsedescriptor- Descriptor regarding the PEM encoded format (seeDescriptor)- 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-BufferedReaderinstance instantiated with PEM file dataline- Current line read in the bufferformat- RFC format governing the PEM file- Returns:
PemHeaderif 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 datamaxLineLength- maximum length in b64buffer lines prior to concatenation- Throws:
IllegalArgumentException- In case of a constraint violation
-
parseDescriptor
Determines the RFC format governing the PEM file in the reader and constructs aDescriptoraccordingly.- Parameters:
reader-BufferedReaderinstance 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
Determines the RFC governing the format of the PEM file based of the parameters provided.- Parameters:
explanatoryText- Explanatory text is only allowed in RFC 7468pemType- All types begin with PGP in RFC 2440isRFC4716Markers- It either starts with four dashes (RFC RFC4716) or five (RFC 1421)- Returns:
- Format determined (see
Descriptor.getFormat())
-
getPemType
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
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
Reads the next line in aBufferedReaderinstance without consuming it from the buffer- Parameters:
reader-BufferedReaderinstancemaximumReadLength- Maximum number of characters to peek- Returns:
- Next line
- Throws:
IOException- In case of errors reading the buffer
-