NAME

Crypt::SecretBuffer::PEM - Parse PEM format from a SecretBuffer

SYNOPSIS

use Crypt::SecretBuffer::PEM;
my $secret= secret(load_file => "secrets.pem");
my @pem= Crypt::SecretBuffer::PEM->parse_all($secret->span);

DESCRIPTION

This module parses the PEM format used by OpenSSL and OpenSSH. PEM is a simple text format made of a block of Base64 data with optional headers and begin/end markers. This module parses the begin/end markers, copies that span of bytes into a new SecretBuffer, makes the attributes into a hash, and marks the Base64 span in case you want to process the bytes.

The label around the PEM block and the keys of its headers (if any) are considered non-secret, and copied out of the SecretBuffer into perl scalars. The values of the headers, and the Base64 payload remain secret.

CONSTRUCTORS

parse

my $pem= Crypt::SecretBuffer::PEM->parse($span);

Parse the next PEM block found in the Span. The span is updated to begin on the line following the PEM block. If no PEM block is found, the span is unaltered.

Invalid PEM blocks (such as mismatched BEGIN/END markers) are ignored, as well as any text outside of the markers.

parse_all

my @pem_blocks= Crypt::SecretBuffer::PEM->parse_all($span);

A file can contain more than one PEM block (such as a SSL certificate chain, and its key) This just calls "parse" in a loop until no more PEM blocks are found.

new

my $pem= Crypt::SecretBuffer::PEM->new(%attributes);

You can construct a PEM object from attributes, in case you want to serialize one.

ATTRIBUTES

label

The text from the PEM begin-marker:

-----BEGIN SOME LABEL-----
...
-----END SOME LABEL-----

IN this case the attribute would hold 'SOME LABEL'.

buffer

A Crypt::SecretBuffer holding the complete PEM text from BEGIN marker to END marker, inclusive.

headers

PEM format has optional 'NAME: VALUE' pairs that can appear right after the BEGIN marker. This presents them as a hashref. Note that the values are Span objects.

header_kv

To preserve order of headers, this attribute stores a list of [ $key, $value, ... ]. Note that the values are Span objects.

content

A Span or SecretBuffer that contains the bytes of the PEM payload.

METHODS

serialize

$buffer= $pem->serialize;

This writes a PEM block into a SecretBuffer object. The headers (if any) come from "header_kv", falling back to the "headers" hashref.

VERSION

version 0.013

AUTHOR

Michael Conrad <mike@nrdvana.net>

COPYRIGHT AND LICENSE

This software is copyright (c) 2025 by Michael Conrad.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.