LoraIlieva has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to extract the data from a .pem certificate with the Crypt::X509 library, but I get an error in object construction. Here is what I'm doing: 1. Read the .pem file's content:

my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat $s_filename; open FILE, "<$s_filename" or die "no such file"; binmode FILE; my $pem_cert; read FILE, $pem_cert, $size; close FILE;

2. Decode the content from base64 to receive the DER formatted content. This is done because the CPAN documentation of the Crypt::X509 library states that it needs to be passed "A variable containing the DER formatted certificate to be parsed":

my $der = MIME::Base64::decode($pem_cert);

3. Call the Crypt::X509 constructor and check for errors:

my $oref_x509= Crypt::X509->new($der); if ( $oref_x509->error ) { warn "Error on parsing certificate: ", $oref_x509->error; }

I get the following error:

Odd number of elements in hash assignment at ..<path>../Crypt/X509.pm +line 95. Use of uninitialized value $end in numeric ge (>=) at ..<path>../Conve +rt/ASN1/_decode.pm line 626. Use of uninitialized value $end in numeric eq (==) at ..<path>../Conve +rt/ASN1/_decode.pm line 63. Error on parsing certificate: decode error at ..<path>../Convert/ASN1/ +_decode.pm line 64.

Replies are listed 'Best First'.
Re: Extract data from certificate with Crypt::X509
by Anonymous Monk on Aug 17, 2016 at 08:20 UTC

      Thanks. I've corrected it but now I get a different error:

      Error on parsing certificate: decode error 04<=>30 0 8 at ...<path>.. +./Convert/ASN1/_decode.pm line 113.

      I've tried with other certificate but the error is still the same

      ---Edit

      The question was answered here: http://stackoverflow.com/questions/38991171/extract-data-from-certificate-with-perl-cryptx509

      Before decoding from Base64, the certificate header and footer (Begin, End Certificate) should be removed.