【问题标题】:Python Selenium - How to specify a client certificate to use in client SSL authenticationPython Selenium - 如何指定要在客户端 SSL 身份验证中使用的客户端证书
【发布时间】:2016-07-12 20:05:22
【问题描述】:

我根据Python Selenium - What are possible keys in FireFox webdriver profile preferences 中的答案查看了所有 json 文件中所有可能的密钥,但我找不到指定要在我的 SSL 连接中使用的客户端证书的密钥。

我对此进行了研究,但找不到确切的答案。我发现我们需要根据How to import SSL certificates for Firefox with Selenium [in Python]? 中的答案将证书添加到 FireFox 配置文件中,但我有点卡在这里,我无法弄清楚到底需要如何添加这个证书。

请注意,我不是在谈论信任服务器的证书。默认情况下,当您启动 SSL 连接时,分配给您的工作站的本地证书用作客户端证书。在这里,我需要为我的 SSL 连接使用新的证书/私钥对。我需要这样做以测试 SSL 中的客户端身份验证。

所以,总而言之,我正在寻找一些看起来像这样的配置:

profile.add_client_cert(path_to_cert)
profile.add_private_key(path_to_private_key)

我发现了一些我可能需要调整的文件,但不确定如何将证书和密钥添加到这些文件中,cert8.dbkey3.db。我在 FireFox 配置文件目录中找到了这些文件。

我搜索了 selenium 的源代码,但找不到答案: https://github.com/SeleniumHQ/selenium/search?utf8=%E2%9C%93&q=cert

【问题讨论】:

    标签: python selenium authentication ssl


    【解决方案1】:

    正如我在source code 中看到的,您可以使用参数 (profile_directory) 创建一个 firefox 配置文件,并使用给定的配置文件启动 firefox。我想你也可以设置偏好profile.accept_untrusted_certs = True

    给定的配置文件目录应该准备好客户端证书。

    # Prepared Firefox profile directory
    profile = FirefoxProfile(profile_diretory)
    
    profile.set_preference("security.default_personal_cert", "Select Automatically")
    profile.set_preference("webdriver_accept_untrusted_certs", True)
    self.driver = WebDriver(firefox_profile=profile)
    

    【讨论】:

    • 是的@LittleQ,但是问题仍然存在,使配置文件使用我的证书和密钥的语句是什么。我们有类似profile.add_client_cert(path_to_cert) 的声明吗?另外,profile. accept_untrusted_certs = True 使客户端信任服务器的证书,它与客户端证书无关。
    • @gixxer 给定的配置文件目录应该准备好客户端证书(即包括cert8.dbkey3.db)。
    • 嗨,当“自动选择”时,将自动选择一个(如果存在的话)客户端证书,我明白这一点。问题是如何使用特定的证书,换句话说,如果 cert8.db 是客户端证书的数据库,它应该只有我的特定客户端证书,我该怎么做?
    • 我建议你做一个 cert8.db 来包含唯一的一个证书。我认为这比代码更改更容易。 @gixxer
    • 是的,我们如何制作只有一个证书的 cert8.db?
    【解决方案2】:

    我就是这样做的。

    在我的 python 代码中:我有:

    import os
    
    profile_directory = os.path.join(os.path.abspath(os.sep),"home","rumpelstiltskin","my_cert_db")
    self.driver = WebDriver(firefox_profile=profile)
    

    然后为了创建cert8.db 文件,我使用了以下终端命令:

    cd /home/rumpelstiltskin
    mkdir my_cert_db
    certutil -N -d sql:my_cert_db/
    pk12util -n my-cert-name -d sql:my_cert_db/ -i /my/path/to/cert.p12
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 2013-10-07
      • 1970-01-01
      • 2014-04-04
      相关资源
      最近更新 更多