How to export all certificates in a certificate chain to separate .crt files with a single command
I would like to export all certificates in a certificate chain to separate `.crt` files with a single command. How can I do that?
To provide some background information:
1. I would like to use the `openssl` bash utility: `(openssl s_client -showcerts -connect <host>:<port> & sleep 4)`
2. the above command **may** print more than one certificate, that is, it may print more than one string with the following pattern: `-----BEGIN CERTIFICATE-----` X.509 certificate encoded in base64 `-----END CERTIFICATE-----`. For example:
-----BEGIN CERTIFICATE-----
MIIFNzCCAx+gAwIBAgITUwAAAAJpqCKn3YTQ6gAAAAAAAjANBgkqhkiG9w0BAQsF...
-----END CERTIFICATE-----
3. the contents of the `.crt` files should be exactly the printed base64 encoded certificates, including tags.
It turns out `awk` can be used to solve the problem: