Extracting SSL/TLS Certificate Chains Using OpenSSL

Have you ever needed to add a certificate to your certificate chain, for instance when trusting a new self-signed certificate?

By running the following command, you'll get the full certificate chain for jvt.me at the time of execution.

$ openssl s_client -showcerts -connect "jvt.me:443" < /dev/null 2>/dev/null |\
	sed -n -e '/BEGIN\ CERTIFICATE/,/END\ CERTIFICATE/ p'
-----BEGIN CERTIFICATE-----
MIIFAjCCA+qgAwIBAgISA+A0GeGJ6cegIn2xi/y9b4/DMA0GCSqGSIb3DQEBCwUA
MEoxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MSMwIQYDVQQD
ExpMZXQncyBFbmNyeXB0IEF1dGhvcml0eSBYMzAeFw0xNzA0MDIxNTM5MDBaFw0x
NzA3MDExNTM5MDBaMBoxGDAWBgNVBAMTD2ludm9pY2VzLmp2dC5tZTCCASIwDQYJ
KoZIhvcNAQEBBQADggEPADCCAQoCggEBAOO6OwNU1N7akkbr/yOXGFCZ03qJdUiV
CIfq3vMo+6SXaHOnfn+51+vtmJ4fI8UGW3ecPWXwi8uRbsIdgf+DYxg62L/z0c2W
HdkQpAW+BNf8ll9uiD3xSimbnYgESyGalQ+g0gZTMwxrpdqSRztqU/8mzGvQZlzf
m7BIONDoquDRECqw73Ll439J23916OfkGM4dxjK+C6bFJsoZeMNNbzMm6Ilwyma1
jZ2JdD2BVsnD3oJUxWJPDKjHAMkuIYDLYANOXHd2NvsZQulSoezlwsk8t6guGDiI
+sYMUflhhtR4N/dnnJA1ayQLIR6yXc2Hef7NrlexpvS5M4XroRIHMvUCAwEAAaOC
AhAwggIMMA4GA1UdDwEB/wQEAwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYB
BQUHAwIwDAYDVR0TAQH/BAIwADAdBgNVHQ4EFgQUa/OEd/0+5la86PnK2DwDqgej
kvAwHwYDVR0jBBgwFoAUqEpqYwR93brm0Tm3pkVl7/Oo7KEwcAYIKwYBBQUHAQEE
ZDBiMC8GCCsG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gkqhkiG9w0BAQsF
AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo
uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/
wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu
X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG
PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6
KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==
-----END CERTIFICATE-----

As this is output to stdout, it is then simple to pipe it to a new file or create a more complex shell pipeline.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #bash #shell #oneliner #openssl #certificates.

This post was filed under articles.

Has this content helped you? Please consider supporting me so I can continue to create content like this!

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.