Created
November 7, 2011 21:45
-
-
Save zakird/1346293 to your computer and use it in GitHub Desktop.
M2Crypto Connect SSL Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for these examples