Convert-PEM
view release on metacpan or search on metacpan
- Altered explode() to canonicalize line endings to \n, handling
DOS (\r\n) and older mac (\r) line breaks cleanly. Thanks to Matt
Gramlich for the patch.
- Removed sign() and auto_install() from Makefile.PL.
- Removed magic svn keywords.
- Converted test suite to Test::More.
- Added author tests (xt/) and modified SYNOPSIS for all modules to
make them pass the compilation test.
0.07 2005.05.25
- Allow passing in Name and Macro parameters on encode and decode,
rather than just at initialization.
- Use Class::ErrorHandler instead of our own built-in error-handling
class.
- Switched to using Module::Install in Makefile.PL.
0.06 2001.09.14
- Use Convert::PEM::ErrorHandler instead of defining error/errstr
methods for each class. This also lets us use error/errstr as
class methods.
- Added Macro param to constructor; this is useful when dealing
with an ASN.1 description that defines multiple ASN.1 macros,
and we want to decode/encode using a specific macro.
- Removed Crypt::DES_EDE3 from this distribution; it is now
packaged and distributed separately, and has been added as a
prereq for Convert::PEM.
0.05 2001.05.11
- Changed explode/implode so that they return/get list of headers
in order in which they appear in the file. This fixes the bug
where openssl-compatible PEM readers (like ssh-keygen in OpenSSH)
NAME
Convert::PEM - Read/write encrypted ASN.1 PEM files
SYNOPSIS
use Convert::PEM;
my $pem = Convert::PEM->new(
Name => "DSA PRIVATE KEY",
Macro => "DSAPrivateKey",
ASN => qq(
DSAPrivateKey SEQUENCE {
version INTEGER,
p INTEGER,
q INTEGER,
g INTEGER,
pub_key INTEGER,
priv_key INTEGER
}
));
-----BEGIN FOO BAR-----
*Name* is a required argument.
* ASN
An ASN.1 description of the content to be either encoded or decoded.
*ASN* is an optional argument.
* Macro
If your ASN.1 description (in the *ASN* parameter) includes more
than one ASN.1 macro definition, you will want to use the *Macro*
parameter to specify which definition to use when encoding/decoding
objects. For example, if your ASN.1 description looks like this:
Foo ::= SEQUENCE {
x INTEGER,
bar Bar
}
Bar ::= INTEGER
If you want to encode/decode a "Foo" object, you will need to tell
*Convert::PEM* to use the "Foo" macro definition by using the
*Macro* parameter and setting the value to "Foo".
*Macro* is an optional argument when an ASN.1 description is
provided.
* InForm
Specify what type of file to expect when using the *read* method.
Value may be either PEM or DER. Default is "PEM".
If "DER" is specified, encryption options are ignored when using the
*read* method and file is read as an unencrypted blob. This option
does not affect the *decode* behavior.
Method used internally, but may be accessed directly decode an ASN.1
string into a perl structure object. If the Convert::PEM object has no
ASN.1 definition, this method has no effect.
* DER
Binary string to convert to an object.
This option is required.
* Macro
If the object has an ASN definition, a Macro may be specified. If
specified, it will override the object's Macro if one exists.
Macro is an optional argument.
$pem->to_der(%args)
Method used internally, but may be accessed directly to encode Content
into binary data. If the Convert::PEM object has no ASN.1 definition,
this method has no effect.
* Content
An object to be ASN.1 encoded to a binary string.
* Macro
If the object has an ASN definition, a Macro may be specified. If
specified, it will override the object's Macro if one exists.
Macro is an optional argument.
$pem->errstr
Returns the value of the last error that occurred. This should only be
considered meaningful when you've received *undef* from one of the
functions above; in all other cases its relevance is undefined.
$pem->asn
Returns the *Convert::ASN1* object used internally to decode and encode
ASN.1 representations. This is useful when you wish to interact directly
with that object; for example, if you need to call *configure* on that
lib/Convert/PEM.pm view on Meta::CPAN
unless (exists $param{Name}) {
return (ref $pem)->error("init: Name is required");
}
else {
$pem->{Name} = $param{Name};
$pem->{ASN} = $param{ASN} if exists $param{ASN};
$pem->{Cipher} = $param{Cipher} if exists $param{Cipher};
}
if (exists $pem->{ASN}) {
$pem->{Macro} = $param{Macro};
my $asn = $pem->{_asn} = Convert::ASN1->new;
$asn->prepare( $pem->{ASN} ) or
return (ref $pem)->error("ASN prepare failed: $asn->{error}");
}
$pem->_getform(%param) or return;
$pem;
}
sub _getform {
lib/Convert/PEM.pm view on Meta::CPAN
my $out = defined $param{OutForm} ? uc($param{OutForm}) : 'PEM';
$out =~ m/^(PEM|DER)$/ or return $pem->error("Invalid OutForm '$out': must be PEM or DER");
$pem->{OutForm} = $out;
$pem;
}
sub asn {
my $pem = shift;
my $asn = $pem->{_asn} || return;
my %prm = @_;
my $m = $prm{Macro} || $pem->{Macro};
$m ? $asn->find($m) : $asn;
}
sub ASN { $_[0]->{ASN} }
sub name { $_[0]->{Name} }
sub cipher { $_[0]->{Cipher} }
sub inform { $_[0]->{InForm} }
sub outform { $_[0]->{OutForm} }
sub macro { $_[0]->{Macro} }
sub read {
my $pem = shift;
my %param = @_;
my $blob;
my $fname = delete $param{Filename};
open my $FH, $fname or
return $pem->error("Can't open $fname: $!");
binmode $FH;
lib/Convert/PEM.pm view on Meta::CPAN
}
sub from_der {
my $pem = shift;
my %param = @_;
# should always be unencrypted at this point
my $obj;
if (exists $pem->{ASN}) {
my $asn = $pem->asn;
if (my $macro = ($param{Macro} || $pem->{Macro})) {
$asn = $asn->find($macro) or
return $pem->error("Can't find Macro $macro");
}
$obj = $asn->decode( $param{DER} ) or
return $pem->error("ASN encode failed: $asn->{error}");
}
else {
$obj = $param{DER};
}
$obj;
}
lib/Convert/PEM.pm view on Meta::CPAN
$obj;
}
sub to_der {
my $pem = shift;
my %param = @_;
my $buf;
if (exists $pem->{ASN}) {
my $asn = $pem->asn;
if (my $macro = ($param{Macro} || $pem->{Macro})) {
$asn = $asn->find($macro) or
return $pem->error("Can't find Macro $macro");
}
$buf = $asn->encode( $param{Content} ) or
return $pem->error("ASN encode failed: $asn->{error}");
}
else {
$buf = $param{Content}
}
$buf;
}
lib/Convert/PEM.pm view on Meta::CPAN
=head1 NAME
Convert::PEM - Read/write encrypted ASN.1 PEM files
=head1 SYNOPSIS
use Convert::PEM;
my $pem = Convert::PEM->new(
Name => "DSA PRIVATE KEY",
Macro => "DSAPrivateKey",
ASN => qq(
DSAPrivateKey SEQUENCE {
version INTEGER,
p INTEGER,
q INTEGER,
g INTEGER,
pub_key INTEGER,
priv_key INTEGER
}
));
lib/Convert/PEM.pm view on Meta::CPAN
-----BEGIN FOO BAR-----
I<Name> is a required argument.
=item * ASN
An ASN.1 description of the content to be either encoded or decoded.
I<ASN> is an optional argument.
=item * Macro
If your ASN.1 description (in the I<ASN> parameter) includes more than
one ASN.1 macro definition, you will want to use the I<Macro> parameter
to specify which definition to use when encoding/decoding objects.
For example, if your ASN.1 description looks like this:
Foo ::= SEQUENCE {
x INTEGER,
bar Bar
}
Bar ::= INTEGER
If you want to encode/decode a C<Foo> object, you will need to tell
I<Convert::PEM> to use the C<Foo> macro definition by using the I<Macro>
parameter and setting the value to C<Foo>.
I<Macro> is an optional argument when an ASN.1 description is provided.
=item * InForm
Specify what type of file to expect when using the I<read> method. Value
may be either B<PEM> or B<DER>. Default is "PEM".
If "DER" is specified, encryption options are ignored when using the
I<read> method and file is read as an unencrypted blob. This option does
not affect the I<decode> behavior.
lib/Convert/PEM.pm view on Meta::CPAN
definition, this method has no effect.
=over 4
=item * DER
Binary string to convert to an object.
This option is required.
=item * Macro
If the object has an ASN definition, a Macro may be specified. If specified,
it will override the object's Macro if one exists.
Macro is an optional argument.
=back
=head2 $pem->to_der(%args)
Method used internally, but may be accessed directly to encode Content into
binary data. If the Convert::PEM object has no ASN.1 definition, this
method has no effect.
=over 4
=item * Content
An object to be ASN.1 encoded to a binary string.
=item * Macro
If the object has an ASN definition, a Macro may be specified. If specified,
it will override the object's Macro if one exists.
Macro is an optional argument.
=back
=head2 $pem->errstr
Returns the value of the last error that occurred. This should only
be considered meaningful when you've received I<undef> from one of
the functions above; in all other cases its relevance is undefined.
=head2 $pem->asn
exponent1 INTEGER, -- d mod (p-1)
exponent2 INTEGER, -- d mod (q-1)
coefficient INTEGER, -- (inverse of q) mod p
otherPrimeInfos EXPLICIT OtherPrimeInfos OPTIONAL }
ASN1
return Convert::PEM->new(
Name => "RSA PRIVATE KEY",
ASN => $rsa_asn,
Macro => "RSAPrivateKey",
@_,
);
}
1;
( run in 0.626 second using v1.01-cache-2.11-cpan-49f99fa48dc )