m2crypto Segmentation Fault

Jon Doe <[email protected]>
Newsgroups gmane.comp.python.cryptography
Message-ID <LISTSERV%[email protected]>
Hello,
I wrote a python script that contacts an https apache server, hands the
sever its cert, so that the server can verify the machine, and then proceeds
to download a file from the server. The script works perfectly when the key
contained in the x509 (.pem) certificate is valid. However, when the key for
a certificate is not valid(ie, when certs are mixed up) m2crypto gives a seg
fault. I did gdb on the script, and traced the problem to the m2crypto
function SSL_FREE(). That function is where the seg fault occurs.Does anyone
have any ideas how to get around that bug? (The code for my script is posted
below)

#!/usr/bin/python

import sys
import string
from M2Crypto import SSL, httpslib, X509

def get_URL(cert_name):
        cert=X509.load_cert(cert_name)
        try:
                url=cert.get_subject().__str__()
        except:
                print '%s corrupted or generated inccorectly' %(cert_name)
        #URL is on the right side of the = sign
        url=string.split(url, "=")[1]
        return url


def get_ISO(certname,fname):
        ctx = SSL.Context('sslv3')
        ctx.load_client_ca(certname)
        ctx.load_cert(certname)

        #Uncommenting the line below will
        #cause Segmentation faults

#ctx.set_info_callback()

        url=get_URL(certname)
        host=string.split(url, "/")[0]

        h = httpslib.HTTPSConnection(host, 443,ssl_context=ctx)
        h.set_debuglevel(0)

        arg=""
        for i in string.split(url, "/")[1:]:
                arg=arg+"/"+i

        h.putrequest('GET', arg)
        h.putheader('Accept', 'text/html')
        h.putheader('Accept', 'text/plain')
        h.putheader('Connection', 'close')
        h.endheaders()

        resp = h.getresponse()
        file=open(fname, 'wb')
        f = resp.fp
        while 1:
                data = resp.read(1024)
                if not data: break
                file.write(data)
                file.flush()
        f.close()
        h.close()
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.