help with private keys (blocs) extraction from file

Hello,

I need to extract blocs (private keys) from a file so that each bloc gets extracted to a separate file (*.priv), on the fly

I started to fiddle with awk, without much luck so far....

A block always has this pattern:

----- BEGIN CERTIFICATE -----
variable number of lines (never fixed, not known in advance)
----- END CERTIFICATE -----

The file could look like this :

----- BEGIN CERTIFICATE -----
zrzrfd
gdfgsgfhfgh
fghfg
fhfhhhhhhhhhhh
----- END CERTIFICATE -----
----- BEGIN CERTIFICATE -----
zerzrz
zerzrrruy
----- END CERTIFICATE -----
----- BEGIN CERTIFICATE -----
etuiyuvvvezrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrzfz
zrrrrrrrrr
zr
----- END CERTIFICATE -----
----- BEGIN CERTIFICATE -----
zerrrrrrrrrrrrrrrrrrrrrrrr
----- END CERTIFICATE -----

Thanks a ton in advance for any hints...

pat38000

That looks like PEM (base64 encoded) X509 format CRT file. Correct? You want to decode it also?
Is there has exactly 65 characters per line in the encoded section?

Hi fpmurphy

no need to decode really, I just need to extract these blocs into separate files as hey are. Also, there is no need to take the length of each line into account... It might as well be one unique long (but wrapped) line.

Actually, only the extraction of blocs should be considered here. Another example :

----- start -----
Lorem ipsum an eos sumo accusam abhorreant, an mucius fabulas mei. Eum id dicit consulatu. No nec debet ridens debitis. Vis eu luptatum salutatus adversarium, affert omnium mnesarchum usu et, ex nibh nusquam torquatos pri.
----- end -----
----- start -----
Ullum aperiri virtute an his, qui ex solet tation. Veniam viderer evertitur eos ne. Ad percipit euripidis ius, nam no exerci melius labores. Ea vide doming minimum vix. No tempor latine quaerendum vis. Et labores menandri theophrastus per, eam diam laoreet inciderint ne.
----- end -----
----- start -----
Accumsan dignissim mea ei. Est et libris molestiae. Solum vivendo consulatu ea per. Cum ad sanctus salutatus.
----- end -----

etc.

Thanks !

What is your input filename and expected output filename set?

As a proof of concept, assume I put your example in "tmp.in" and ran this script:

#! /bin/bash

declare i=1
while read line; do 
        [[ "$line" =~ "BEGIN" ]] && exec >tmp.${i} 
        printf '%s\n' "$line"
        [[ "$line" =~ "END" ]] && ((i++))
done <tmp.in

I end up with tmp.1 through tmp.4, each with it's own key. Newlines don't matter. If I put all of one key on it's own line, it still works.

Whoops, quick edit - fixed bug that created extraneous empty file

1 Like

LivinFree, you rock thanks a ton !

split -p BEGIN infile outfile.
awk '/BEGIN/{x++}{print >> ("file"x)}' in_file
1 Like

Thank you fpmurphy & danmero !