1

I'd like to automate the following process with the openssl command:

I've been working along the lines of the following command (here wrapped in python):

local("echo -n | openssl s_client -connect google.com:443 -certform DER | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /tmp/google.com.der")

but it doesn't give me what I expect.

2

Your sed command is only capturing openssl s_client's standard output, but the certificate comes with the other debugging information on standard error. Have you tried something like this?

openssl s_client -connect google.com:443 </dev/null 2>&1 |
  sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' |
  openssl x509 -outform DER >/tmp/google.com.der
1

You're probably seeing the lines which openssl outputs on stderr and are ignored by sed. You can just get rid of these before passing the output to sed:

% echo | openssl s_client -connect google.com:443 -certform DER 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'
-----BEGIN CERTIFICATE-----
MIIF/DCCBWWgAwIBAgIKUCYyawAAAAB1rzANBgkqhkiG9w0BAQUFADBGMQswCQYD
...
v6UdT7iXlxVh2oslpIksSw2bf0H7PUQQjFCMLVbiEMAQbXal4+SsyYB+GsPVvlR+
-----END CERTIFICATE-----

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.