使用客户端证书的自定义 urllib opener

发布于 2024-11-02 04:23:54 字数 3283 浏览 9 评论 0原文

我有一个必须使用的 API。 API 通过 HTTPS 进行保护,并使用相互身份验证/客户端证书。我有一个 PEM 文件和一个 CRT 文件。

当我使用 PyOpenSSL 定期连接到服务器时,我没有问题,这里是代码:

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)

但是,因为这是一个带有客户端证书的 HTTPS API,我已经为它实现了一个开启器,以某种方式修改的代码在这里:

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

但是当我运行第二个代码,出现以下错误:

  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')]

最后,我做错了什么吗?如果没有,请帮助我理解错误......

干杯。

I have got an API that I have to work with. The API is secured by HTTPS and uses mutual authentication/client certificates. 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:

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)

But, because this is an HTTPS API with client certificate, I have implemented an opener for it, the code somehow modified is here:

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

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')]

Finally, am I doing something wrong? If not, please help me understand the error...

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2024-11-09 04:23:54

看起来您在这里添加了很多您并不真正需要的复杂性。如果您只是进行简单的客户端证书身份验证,您可能可以摆脱以下代码段() :

import httplib
import urllib2

# HTTPS Client Auth solution for urllib2, inspired by
# http://bugs.python.org/issue3466
# and improved by David Norton of Three Pillar Software. In this
# implementation, we use properties passed in rather than static module
# fields.
class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
    def __init__(self, key, cert):
        urllib2.HTTPSHandler.__init__(self)
        self.key = key
        self.cert = cert
    def https_open(self, req):
        #Rather than pass in a reference to a connection class, we pass in
        # a reference to a function which, for all intents and purposes,
        # will behave as a constructor
        return self.do_open(self.getConnection, req)
    def getConnection(self, host):
        return httplib.HTTPSConnection(host, key_file=self.key, cert_file=self.cert)


cert_handler = HTTPSClientAuthHandler(settings.PEMFILE, settings.CLIENT_CERT_FILE)
opener = urllib2.build_opener(cert_handler)
urllib2.install_opener(opener)

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

该源用于向 Suds Client< 提供经过证书验证的 URL 打开程序/code> 构造函数,所以我将其剥离并使其成为直接的开场白。

It looks like you're adding a lot of complexity here that you don't really need. If you're just doing simple client certificate authentication, you could probably get away from the following snippet (source):

import httplib
import urllib2

# HTTPS Client Auth solution for urllib2, inspired by
# http://bugs.python.org/issue3466
# and improved by David Norton of Three Pillar Software. In this
# implementation, we use properties passed in rather than static module
# fields.
class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
    def __init__(self, key, cert):
        urllib2.HTTPSHandler.__init__(self)
        self.key = key
        self.cert = cert
    def https_open(self, req):
        #Rather than pass in a reference to a connection class, we pass in
        # a reference to a function which, for all intents and purposes,
        # will behave as a constructor
        return self.do_open(self.getConnection, req)
    def getConnection(self, host):
        return httplib.HTTPSConnection(host, key_file=self.key, cert_file=self.cert)


cert_handler = HTTPSClientAuthHandler(settings.PEMFILE, settings.CLIENT_CERT_FILE)
opener = urllib2.build_opener(cert_handler)
urllib2.install_opener(opener)

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

The source was used in the context of providing a cert-authenicated URL opener to the Suds Client constructor, so I stripped that out and made it a direct opener.

醉城メ夜风 2024-11-09 04:23:54

我不确定 - 但在我看来,您缺少在 connect() 方法中执行 connect() 调用:

self.sock.connect(("someserver.com",443))

此外 httplib 的 https 处理具有 SSL 套接字的包装类,因此也许这些是它工作所必需的?

I'm not sure - but it looks to me like you are missing doing the connect() call in the connect() method:

self.sock.connect(("someserver.com",443))

Also httplib's https handling has wrapper classes for the SSL socket, so maybe those are required for it to work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文