Skip to content

Instantly share code, notes, and snippets.

@zakird
Created November 7, 2011 21:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakird/1346293 to your computer and use it in GitHub Desktop.
Save zakird/1346293 to your computer and use it in GitHub Desktop.
M2Crypto Connect SSL Example
import M2Crypto
address = "mail.google.com"
context = M2Crypto.SSL.Context();
context.set_allow_unknown_ca(True)
context.set_verify(M2Crypto.SSL.verify_none, True)
conn = M2Crypto.SSL.Connection(context)
conn.connect((address, 443))
cert_chain = conn.get_peer_cert_chain()
for cert in cert_chain:
print cert.as_text()
import M2Crypto
address = "mail.google.com"
base_attribs = ('version','serial_number','not_before','not_after')
context = M2Crypto.SSL.Context();
context.set_allow_unknown_ca(True)
context.set_verify(M2Crypto.SSL.verify_none, True)
conn = M2Crypto.SSL.Connection(context)
conn.connect((address, 443))
cert_chain = conn.get_peer_cert_chain()
for cert in cert_chain:
attributes = {}
attributes["fingerprint"] = cert.get_fingerprint("sha1")
for attrib in base_attribs:
attributes[attrib] = getattr(cert, 'get_'+attrib)()
pubkey = cert.get_pubkey()
attributes['subject'] = cert.get_subject().as_text()
attributes['issuer'] = cert.get_issuer().as_text()
attributes['is_ca'] = cert.check_ca()
attributes['key_size'] = pubkey.size()
attributes['key_modulus'] = pubkey.get_modulus()
for i in range(0,cert.get_ext_count()):
ext = cert.get_ext_at(i)
attributes['x509_'+ext.get_name()] = ext.get_value()
for k, v in attributes.iteritems():
print k, v
@mixedmiscmail
Copy link

Thanks for these examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment