【问题标题】:Use python to access a site with PKI security使用 python 访问具有 PKI 安全性的站点
【发布时间】:2015-08-20 09:17:32
【问题描述】:

我有一个启用了 PKI 安全性的网站。每个客户要么使用读卡器加载他们的证书,要么将证书安装在他们盒子上的 IE 证书存储中。

所以我的问题是:

  1. 如何使用读卡器证书或系统中存储的证书来验证系统?
  2. 如何将凭据传递到站点上,以表明“嘿,我是我,我可以访问该服务”?他们的示例可以使用软证书。稍后我可以弄清楚读卡器部分。

我一直在四处寻找,但在这种情况下我没有想出任何可以帮助我的方法。 Django 有一堆模块,但这不是一个选项,因为我只关心客户端的事情。我没有创建一个站点来托管该服务。我只需要访问这些服务。

我有这个代码工作的排序。我只是不知道如何处理我得到的重定向:

import httplib
KEYFILE = r"C:\cert\my.key"
CERTFILE = r"c:\cert\my.pem"
HOSTNAME = 'machine.com'

conn = httplib.HTTPSConnection(
    HOSTNAME,
    key_file = KEYFILE,
    cert_file = CERTFILE
)

conn.putrequest('GET', '/arcgis/sharing/rest?f=json')
conn.endheaders()
response = conn.getresponse()
print response.read()

这一切的结果是:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://machine.com/pki?https://machine.com/arcgis/sharing/rest%3f&amp;f=json">here</a>.</p>
</body></html>

提供的任何帮助都会很棒!

软件规格:python 2.7.8、Windows 2012 R2

【问题讨论】:

  • 你能分享一下你到目前为止所做的尝试吗?暂时在#1(获取证书)上进行检查,假设“PKI auth”表示“客户端证书身份验证”,我相信#2 已包含在包装 https/ssl/tls 的库中。例如,在请求中,使用:requests.get(url, cert=('path/to/cert', 'path/to/key'))(抱歉 - 并不是要作为答案发布)
  • 看起来您所说的“PKI”实际上是“客户端证书身份验证”(PKI 是一个更广泛的领域,通常包括验证服务器证书)。您知道您的读卡器是否提供 PKCS#11 驱动程序,还是仅提供 Windows API?
  • 顺便问一下,您打算在 Python 中使用任何特定的库吗?
  • @Bruno - 我没有任何 python 库。我不接受任何一种解决方案。
  • @bimsapi - 我尝试了以下解决方案的变体:code.activestate.com/recipes/… 没有成功。

标签: python python-2.7 ssl urllib2 pki


【解决方案1】:

我创建了一个 PKI 处理程序来处理请求,因此我可以使用它来工作 urllib2 库。

import httplib, urllib2

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, timeout=300):
        return  httplib.HTTPSConnection(host,
                                             key_file=self.key,
                                             cert_file=self.cert,
                                             timeout=timeout)

要使用它,您需要使用带有处理程序的 cookiejar。

from cookielib import CookieJar
cookiejar = CookieJay()
handlers = []
handlers.append(HTTPSClientAuthHandler(somekey, somecert))
handlers.append(urllib2.HTTPCookieProcessor(cookiejar))
opener = urllib2.build_opener(*handlers)
... do other urllib2 calls ....

希望对大家有帮助!

【讨论】:

    【解决方案2】:

    试试这个代码

    #!/usr/bin/env python
    
    import httplib
    
    CERTFILE = '/home/robr/mycert'
    HOSTNAME = 'localhost'
    
    conn = httplib.HTTPSConnection(
        HOSTNAME,
        key_file = CERTFILE,
        cert_file = CERTFILE
    )
    conn.putrequest('GET', '/ssltest/')
    conn.endheaders()
    response = conn.getresponse()
    print response.read()
    

    【讨论】:

    • 感谢您的回复。 httplib 可以处理重定向吗?
    猜你喜欢
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多