Score:1

Divide one .pem file containing several certificates to several new .pem files

US flag

I am currently trying to figure out how I can divide one .pem file, containing several certificates to several new .pem files, but I do not know where to start...

The first .pem file looks like this:

-----BEGIN CERTIFICATE-----
bla bla bla
bla bla bla
bla lba bal
-----END CERTIFICATE-----
Bag attributes:
subject=blabla
issuer=bla
-----BEGIN CERTIFICATE-----
bla bla bla
bla bla bla
bla lba bal
-----END CERTIFICATE-----
Bag attributes:
subject=blabla
issuer=bla

......

However, I need a script to divide this .pem file to four new .pem files that only contains each certificate, so they look like:

-----BEGIN CERTIFICATE-----
bla bla bla
bla bla bla
bla lba bal
-----END CERTIFICATE-----

.......

how would the script look like?
I am working in MobaXtrem (and i am very very new with server management so i am a bit lost...)

I was thinking a structure like this, is that possible?

for i in 'seq 1 4'; do <some regex expression i guess> -out cert$i.pem; done

Hopefully someone can help.

Thanks

Romeo Ninov avatar
in flag
Does this answer your question? [How to split a PEM file](https://serverfault.com/questions/391396/how-to-split-a-pem-file)
Romeo Ninov avatar
in flag
Also https://stackoverflow.com/a/68864197/2908599
Romeo Ninov avatar
in flag
And https://stackoverflow.com/a/25070700/2908599
Score:0
cl flag
A.B

Just use openssl x509 and nothing else to handle certificates in a loop. openssl doesn't flush file descriptors it's using and always reads the minimum. You have to ensure that there's a single input stream feeding the loop.

for i in $(seq 1 4); do
    openssl x509 -out cert$i.pem
done < bundle.pem

Or even:

i=1; while openssl x509 -out cert$i.pem; do
    i=$((i+1))
done < bundle.pem

< bundle.pem could itself be the output of an other certificate processing command like below (which will still ask a password if any in this example):

openssl pkcs12 -in bundle.p12 -nokeys | {
    i=1; while ...
        ....
    done
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.