I have a program below, which recursively calls openssl and coverts .pkcs12 files with the password being the same name as the filename to a .pem format with client certs and private keys. I want to take the first part (which works) and then parse out the two certificates (public and private) and then name them *.cert and *.key respectively. I am having trouble figuring out how to get perl to separate the two types of certificate. File 1 should contain the private key, me.key and me .cert (public cert).
!/usr/bin/perl -w @files = <*.p12>; foreach $file(@files) { $fileconvert=$file; $fileconvert=~s/\.p12//g; `openssl pkcs12 -passin pass:$fileconvert -clcerts -nodes -in $fi +le -out $fileconvert.pem\n"`; } @secondpass = <*.pem>; foreach $secondpass(@secondpass) { $filename=$secondpass; open CERT, "<$filename"; @cert=<CERT>; print @cert; while (@cert =~s/BEGIN RSA PRIVATE KEY(.*?)END RSA PRIVATE KEY//s) + { $code = $1; print "$code.\n"; } }
The data:
Bag Attributes localKeyID: 33 32 34 33 39 38 33 33 30 32 36 30 33 friendlyName: verisign class 3 public primary certification author +ity subject=/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification + Authority issuer=/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification +Authority -----BEGIN CERTIFICATE----- ... -----END CERTIFICATE----- Bag Attributes localKeyID: 31 32 33 33 39 38 33 33 30 22 11 30 34 friendlyName: mycerts.test.com Key Attributes: <No Attributes> -----BEGIN RSA PRIVATE KEY----- THIS IS THE FILE i WANT ... -----END RSA PRIVATE KEY----- Bag Attributes localKeyID: 23 32 34 33 39 38 33 33 44 32 36 30 21 friendlyName: mycerts.test.com subject=/C=US/ST=Somewhere/L=There/O=My Org/OU=Terms of use at www.ver +isign.com/rpa (c)00/CN=MYCERTS.TEST.COM issuer=/O=VeriSign Trust Network/OU=VeriSign, Inc./OU=VeriSign Interna +tional Server CA - Class 3/OU=www.verisign.com/CPS Incorp.by Ref. LIA +BILITY LTD.(c)97 VeriSign -----BEGIN CERTIFICATE----- ... -----END CERTIFICATE----- Bag Attributes localKeyID: 31 32 34 33 39 38 44 33 30 32 36 30 36 friendlyName: verisign intermediate ca subject=/O=VeriSign Trust Network/OU=VeriSign, Inc./OU=VeriSign Intern +ational Server CA - Class 3/OU=www.verisign.com/CPS Incorp.by Ref. LI +ABILITY LTD.(c)97 VeriSign issuer=/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification +Authority -----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----
"Two Wheels good, Four wheels bad."

In reply to Using Perl to convert pkcs12 certificates to pem format, separate files for public/public keys by symgryph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.