I'm currently working to extract properly each contents in a CSR file's subject. I have here a working snippet, However I got stuck when values have a slash / (ex. CSR content has OrganizationUnit = orgunit/testou) on the values. The way I'm doing to extract the contents is to use regex,split it and push that in a Hash, and throw it back in the front end. See below:
sub CSRDecode{
###########################################################################
################Do Your Validation#########################################
###########################################################################
my @returnInfo = `openssl req -in /opt2/myfiles/perllib/custom/OpenSSL/certreq.csr -text -noout` or die "Could not validate CSR";
my $Subj= `openssl req -in /opt2/myfiles/perllib/custom/OpenSSL/certreq.csr -subject -noout` or die "Could not validate CSR";
print $Subj;
print @returnInfo;
my $KeySize= @returnInfo[6];
my $SubjAltName =`openssl req -in /opt2/myfiles/perllib/custom/OpenSSL/certreq.csr -text -noout|grep -E 'email|DNS'`; #or die "Could not get SAN";
$KeySize=~s/^\s+|\s+$//g;
$KeySize=~/(.+?)/;
$Subj =~ s/^\s+|\s+$//g;
print $Subj;
$SubjAltName=~ s/^\s+|\s+$//g;
my %CSRInfo=split/[=\/]/,$Subj;
if(%CSRInfo){
%CSRInfo->{SubjAltName}.=$SubjAltName;
%CSRInfo->{keysize}.=$KeySize;
}
print Dumper \%CSRInfo;
#######################################################################
Input: CSR File with Subject similar to this:
subject=/O=ABCCommon/OU=abcfoundation/ops1/[email protected]/L=NYC/ST=AMER/C=AMER/CN=commonName
Expected Output after extraction (HASH) - Note the OU content which has "/"
$VAR1 = {
'CN' => 'commonName',
'keysize' => 'RSA Public Key: (2048 bit)',
'SubjAltName' => 'DNS:[email protected], IP Address:192.168.1.1',
'ST' => 'AMER',
'O' => 'ABCCommon',
'emailAddress' => '[email protected]',
'subject' => '',
'OU' => 'abcfoundation/ops1',
'C' => 'AMER',
'L' => 'NYC'
};
Currently the output is juggling because I think the regex isn't properly handling the "split". I'm referring to my %CSRInfo=split/[=\/]/,$Subj;
in the code snippet.
I might have some issues in my regex and I appreciate your help, Thank you!
Ah, ok. Yes I see. You're trying to split on a /
but have a pattern including a /
. This gets complicated, but I'd probably try and approach it like this:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my $subj =
'subject=/O=ABCCommon/OU=abcfoundation/ops1/[email protected]/L=NYC/ST=AMER/C=AMER/CN=commonName';
my %subjinfo = ( $subj =~ m,(\w+)=([^=]*)(?:/|$),g );
print Dumper \%subjinfo;
This then gives:
$VAR1 = {
'subject' => '',
'L' => 'NYC',
'C' => 'AMER',
'OU' => 'abcfoundation/ops1',
'emailAddress' => '[email protected]',
'ST' => 'AMER',
'CN' => 'commonName',
'O' => 'ABCCommon'
};
I think that gives what you need. This regular expression repeats, and captures pairs of 'things' on either side of an =
ending with either /
or 'end of line' $
Because we're matching in pairs (last group has (?:
to denote it's non-capturing) these can be directly assigned into a hash.