Various PEM for DER in perl

I have an application with client-server architecture.

client (program C):

  • generate various DER encoded data
  • convert DER to PEM (using openssl PEM_write_bio) with a different PEM header
  • send PEM to the server

server (Perl script):

  • receive PEM data
  • convert PEM to DER
  • ....

My question is how to convert various PEM data to DER / BER (binary data) in perl?

+3
source share
2 answers

You can disable the PEM tags yourself and do the decoding of the Base64 block internally using MIME::Base64.

It should be as simple as

$derBlob = decode_base64($base64Blob);
+5
source

An example based on the accepted answer:

#!/usr/bin/perl

use strict;
use warnings;
use MIME::Base64;

my $certPath = 'cert.pem';

open my $fh, '<', $certPath or die(sprintf('Could not open %s file: %s', $certPath, $!));
my $derBlob = do { local $/; decode_base64(<$fh> =~ s/^-.*?\n//gmr); };
close($fh);
print $derBlob;

1;
__END__
+3

Source: https://habr.com/ru/post/1758923/


All Articles