Skip to main content

MITM 代理的简单证书颁发机构

项目描述

证书颁发机构证书制作工具

https://travis-ci.org/ikreymer/certauth.svg?branch=master https://coveralls.io/repos/ikreymer/certauth/badge.svg?branch=master

这个包提供了一个小型库,建立在pyOpenSSL 之上,它允许创建自定义证书颁发机构证书,并使用该 CA 证书生成按需动态主机证书。

它最适合与中间人 HTTPS 代理一起使用,例如,用于记录或重放 Web 内容。

在受控设置中应谨慎使用信任此工具创建的 CA,以避免安全风险。

证书授权 API

CertificateAuthority类提供了一个接口来管理根 CA 并生成适用于本机 Python ssl库以及 pyOpenSSL SSL模块的动态主机证书。

该类提供了几个用于存储根 CA 和生成的主机 CA 的选项。

基于文件的证书缓存

ca = CertificateAuthority('My Custom CA', 'my-ca.pem', cert_cache='/tmp/certs')
filename = ca.cert_for_host('example.com')

在此配置中,根 CA 存储在my-ca.pem 中,动态生成的证书放置在/tmp/certs中。在此示例中,返回的文件名将是/tmp/certs/example.com.pem 。

然后可以将此文件名与 Python ssl.load_cert_chain(certfile)命令一起使用。

请注意,动态创建的证书永远不会被certauth删除,如果需要,它仍然由用户偶尔处理清理。

内存中证书缓存

ca = CertificateAuthority('My Custom CA', 'my-ca.pem', cert_cache=50)
cert, key = ca.load_cert('example.com')

此配置将根 CA 存储在my-ca.pem中,但使用内存中的证书缓存来动态创建证书。这些证书存储在 LRU 缓存中,配置为最多保留 50 个证书。

然后可以将证书密钥与OpenSSL.SSL.Context.use_certificate一起使用

context = SSl.Context(...)
context.use_privatekey(key)
context.use_certificate(cert)

自定义缓存

还可以提供存储和检索每个主机证书的自定义缓存实现:

ca = CertificateAuthority('My Custom CA', 'my-ca.pem', cert_cache=CustomCache())
cert, key = ca.load_cert('example.com')

class CustomCache:
    def __setitem__(self, host, cert_string):
       # store cert_string for host

    def get(self, host):
       # return cached cert_string, if available
       cert_string = ...
       return cert_string

通配符证书

为了减少生成的证书数量,生成通配符证书很方便。

cert, key = ca.load_cert('example.com', wildcard=True)

这将为*.example.com生成一个证书。

要为父域自动生成通配符证书,请使用:

cert, key = ca.load_cert('test.example.com', wildcard=True, wildcard_for_parent=True)

这也将为*.example.com生成一个证书

从 1.3.0 开始,certauth使用tldextract来确定给定主机的 tld,如果它本身是 tld 后缀,则不会使用父域。

例如,调用:

cert, key = ca.load_cert('example.co.uk', wildcard=True, wildcard_for_parent=True)

现在将生成*.example.co.uk的证书,而不是*.co.uk

CLI 使用示例

certauth还包括一个用于证书创建和管理的简单命令行 API。

usage: certauth [-h] [-c CERTNAME] [-n HOSTNAME] [-d CERTS_DIR] [-f] [-w]
              root_ca_cert

positional arguments:
  root_ca_cert          Path to existing or new root CA file

optional arguments:
  -h, --help            show this help message and exit
  -c CERTNAME, --certname CERTNAME
                      Name for root certificate
  -n HOSTNAME, --hostname HOSTNAME
                      Hostname certificate to create
  -d CERTS_DIR, --certs-dir CERTS_DIR
                      Directory for host certificates
  -f, --force           Overwrite certificates if they already exist
  -w, --wildcard_cert   add wildcard SAN to host: *.<host>, <host>

要创建新的根 CA 证书:

certauth myrootca.pem --certname "我的测试 CA"

要在目录certs_dir中创建使用 CA 证书签名的主机证书:

certauth myrootca.pem --hostname "example.com" -d ./certs_dir

如果根证书不存在,则会自动创建。如果certs_dir不存在,它也会自动创建。

example.com的证书将创建为certs_dir/example.com.pem。如果它已经存在,则不会被覆盖(除非使用-f选项)。

-w选项可用于创建具有主题备用名称 (SAN) 的通配符证书,例如example.com* .example.com

历史

CertificateAuthority 功能从最初在Nadeem Douba的中间人代理pymiproxy中发现的证书管理演变而来。

Internet Archive的Noah Levitt在warcprox中也对其进行了扩展。

CA 功能也在pywb中被重用,并最终被分解到这个单独的包中以实现模块化。

wsgiprox现在也使用它来为任何 WSGI 应用程序提供通用的 HTTPS 代理包装器。

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

certauth-1.3.0.tar.gz (10.8 kB 查看哈希

已上传 source

内置分布

certauth-1.3.0-py2.py3-none-any.whl (10.6 kB 查看哈希

已上传 py2 py3