Export a digital certificate via the command line (MacOS)

Have you ever tried to capture and/or view the certificate of a website you are connecting to with Safari?

Ever wonder how to export an SSL certificate from a site on the Safari web browser?

It seems quite easy on Internet Explorer and FireFox to perform this task.  However, after many minutes of trying to figure it out it seems, Safari doesn’t contain its own certificate repository, but instead makes use of the certificate infrastructure built into Mac OS X.  You can import/export/view certificates using the Keychain Access utility.

Here’s one method for grabbing a site’s certificate yourself:

  1. Open a Terminal window.
  2.  Enter the following command: openssl s_client -connect someSSLserver.com:443

Make sure to replace ‘someSSLserver.com’ with the actual host you want to connect to. The output in Terminal should contain a block of text that looks like this:

…encoded certificate data

There may be more than one of these blocks if more than one certificate is involved. Copy each block (including the BEGIN and END lines) into a file whose name ends in “.pem” (for example, “cert.pem”). Terminal has a “Save Selected Text As…” menu item which works great for this purpose.