Para imprimir o certificado do servidor como texto usando o openssl:
#!/bin/bash
#
# Show server's certificate in a human-readable form.
#
# Usage: $ show-cert HOST [PORT]
#
exec <&- # close stdin to suppress 'read:errno=0' from openssl
exec openssl x509 -noout -text \
-in <(openssl s_client -connect "$1":"${2:-443}" -showcerts)
Ou usando o Python para obter a saída no formato json:
#!/usr/bin/env python3
"""Show server's certificate as json.
Usage:
$ %(prog)s HOST [PORT]
"""
import json
import socket
import ssl
import sys
def getcert(addr, timeout=None):
"""Retrieve server's certificate at the specified address (host, port)."""
# it is similar to ssl.get_server_certificate() but it returns a dict
# and it verifies ssl unconditionally, assuming create_default_context does
with socket.create_connection(addr, timeout=timeout) as sock:
context = ssl.create_default_context()
with context.wrap_socket(sock, server_hostname=addr[0]) as sslsock:
return sslsock.getpeercert()
def main(argv):
host = argv[1]
port = int(argv[2]) if len(argv) > 2 else 443
print(json.dumps(getcert((host, port)), indent=2, sort_keys=True))
if __name__ == "__main__":
main(sys.argv)
Exemplo:
$ getcert encrypted.google.com | jq -r '.notAfter, .subjectAltName[][1]'
Mar 20 00:00:00 2014 GMT
*.google.com
*.android.com
*.appengine.google.com
*.cloud.google.com
...
A versão mais recente: getcert.py