V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
master13
V2EX  ›  Python

如何用 Python 解析 ssl 证书文件?

  •  1
     
  •   master13 · 2016-12-21 09:12:42 +08:00 · 6261 次点击
    这是一个创建于 2694 天前的主题,其中的信息可能已经有所发展或是发生改变。
    现在有一堆 SSL 证书,需要提取其中的 Issuer 、 Subject 等信息吧,就是 windows 下双击这个证书里能看到的颁发者、使用时间啥的。之前用 C#的 System.Security.Cryptography.X509Certificates 包来做的,现在开发环境换 python 了,也没找到比较好用的库。

    求老司机引导一下……
    第 1 条附言  ·  2016-12-21 15:04:13 +08:00
    补充一下, SSL 证书都是离线的,不能再次与远程站点建立连接。
    9 条回复    2016-12-22 08:22:32 +08:00
    bxb100
        1
    bxb100  
       2016-12-21 09:28:01 +08:00 via Android
    好像自带库里有,没用过,我也不清楚,帮
    way2exp
        2
    way2exp  
       2016-12-21 09:35:05 +08:00
    vvoody
        3
    vvoody  
       2016-12-21 09:35:07 +08:00
    PyOpenSSL

    OpenSSL.SSL.Connection()
    get_peer_certificate()
    get_peer_cert_chain()
    BOYPT
        4
    BOYPT  
       2016-12-21 10:16:01 +08:00   ❤️ 1
    import os
    os.system("openssl x509 -text -noout -in cert.crt")
    est
        5
    est  
       2016-12-21 10:29:48 +08:00
    @BOYPT 我喜欢这个方式。
    wujunze
        6
    wujunze  
       2016-12-21 10:50:52 +08:00
    @est
    @BOYPT 简单粗暴
    master13
        7
    master13  
    OP
       2016-12-21 15:00:06 +08:00
    @BOYPT 啊这也能叫用 python 来解决……你赢了
    master13
        8
    master13  
    OP
       2016-12-21 15:04:40 +08:00
    @way2exp 感谢。我没有说清楚,证书是离线的,无法再次与服务器建立连接。
    已补充到题目。
    master13
        9
    master13  
    OP
       2016-12-22 08:22:32 +08:00
    好,自问自答一波

    使用 pyOpenSSL:

    OpenSSL.crypto.load_certificate(type, buffer)
    get_subject()
    get_issuer()
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3534 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 11:37 · PVG 19:37 · LAX 04:37 · JFK 07:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.