Problem with two way SSL connection

Asked by apache

Hey, I have got an API that I have to work with. The API is secured by a two way SSL.

I have a pem file and a crt file.

When I connect to the server regularly, using PyOpenSSL I have no problem, here is the code: http://dpaste.com/533095/

But, because this is an HTTP API and I want to get rid of the http protocol, well, I have implemented an opener for it,
the code somehow modified is here: http://dpaste.com/533097/

but when I run the second code, I get the following error:

  File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.6/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "network.py", line 37, in https_open
    return self.do_open(IRNICHTTPSConnection,req)
  File "/usr/lib/python2.6/urllib2.py", line 1142, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/lib/python2.6/httplib.py", line 914, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.6/httplib.py", line 951, in _send_request
    self.endheaders()
  File "/usr/lib/python2.6/httplib.py", line 908, in endheaders
    self._send_output()
  File "/usr/lib/python2.6/httplib.py", line 780, in _send_output
    self.send(msg)
  File "/usr/lib/python2.6/httplib.py", line 759, in send
    self.sock.sendall(str)
OpenSSL.SSL.Error: [('SSL routines', 'SSL_write', 'uninitialized')]

Question information

Language:
English Edit question
Status:
Answered
For:
pyOpenSSL Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Sandro Tosi (morph-debian) said :
#1

On Mon, Apr 18, 2011 at 09:27, apache
<email address hidden> wrote:
> When I connect to the server regularly, using PyOpenSSL I have no problem, here is the code: http://dpaste.com/533095/
>
> But, because this is an HTTP API and I want to get rid of the http protocol, well, I have implemented an opener for it,
> the code somehow modified is here: http://dpaste.com/533097/

just a side note: don't use pastebin services to refer to important
information, since they will disappear; for example, "This item will
be deleted in 6 days" is the foot note of the first link. Paste your
code here, as you did for the traceback, so even in 6 days we can
access your code:)

Regards,
--
Sandro Tosi (aka morph, morpheus, matrixhasu)
My website: http://matrixhasu.altervista.org/
Me at Debian: http://wiki.debian.org/SandroTosi

Revision history for this message
apache (apache-20o5) said :
#2

OKAY, sure:

The first code:
#==========================================
import settings
from OpenSSL import SSL
import socket

def verify(conn, cert, errnum, depth, ok):
    # This obviously has to be updated
    print 'Got certificate: %s' % cert.get_subject()
    return ok

def password_callback(maxlen, verify, extra):
        print (maxlen, verify, extra)
        return settings.DEPOSIT_CODE

context = SSL.Context(SSL.SSLv23_METHOD)
context.set_verify(SSL.VERIFY_NONE, verify)
context.set_passwd_cb(password_callback)
context.use_certificate_file(settings.CLIENT_CERT_FILE)
context.use_privatekey_file(settings.PEM_FILE)

sock = SSL.Connection(context, socket.socket(socket.AF_INET, socket.SOCK_STREAM))
sock.connect(("someserver.com",443))

http_get_request = """
GET / HTTP/1.1

"""
sock.write(http_get_request)
print sock.recv(1000)
#====================================================

second code:
#====================================================
import settings
import socket
import urllib2

def verify(conn, cert, errnum, depth, ok):
    # This obviously has to be updated
    print 'Got certificate: %s' % cert.get_subject()
    return ok

def password_callback(maxlen, verify, extra):
        print (maxlen, verify, extra)
        return settings.DEPOSIT_CODE

class MyHTTPSConnection(httplib.HTTPSConnection):
    def connect(self):
        context = SSL.Context(SSL.SSLv23_METHOD)
        context.set_passwd_cb(password_callback)
        context.use_certificate_file(settings.CLIENT_CERT_FILE)
        context.set_verify(SSL.VERIFY_NONE, verify)
        context.use_privatekey_file(settings.PEM_FILE)
        self.sock = SSL.Connection(context, socket.socket(socket.AF_INET, socket.SOCK_STREAM))

class MyHTTPSHandler(urllib2.HTTPSHandler):
    def https_open(self,req):
        return self.do_open(MyHTTPSConnection,req)

opener = urllib2.build_opener(urllib2.HTTPHandler,MyCHTTPSHandler)
urllib2.install_opener(opener)

f = urllib2.urlopen("https://sampleapiserver.com")
print f.code
#=============================================================

Error:
#=============================================================
  File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.6/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "network.py", line 37, in https_open
    return self.do_open(IRNICHTTPSConnection,req)
  File "/usr/lib/python2.6/urllib2.py", line 1142, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/lib/python2.6/httplib.py", line 914, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.6/httplib.py", line 951, in _send_request
    self.endheaders()
  File "/usr/lib/python2.6/httplib.py", line 908, in endheaders
    self._send_output()
  File "/usr/lib/python2.6/httplib.py", line 780, in _send_output
    self.send(msg)
  File "/usr/lib/python2.6/httplib.py", line 759, in send
    self.sock.sendall(str)
OpenSSL.SSL.Error: [('SSL routines', 'SSL_write', 'uninitialized')]

Revision history for this message
Jean-Paul Calderone (exarkun) said :
#3

It looks like your overridden "connect" method doesn't actually connect to anything. It creates a new Connection object and leaves it unconnected. Try connecting it to the intended host/port.

Can you help with this problem?

Provide an answer of your own, or ask apache for more information if necessary.

To post a message you must log in.