用Python库pyOpenSSL读取iOS的p12证书!
1、前言
本文主要是讲解如何用python读取 p12 的信息。
如果有过iOS团队开发经验的朋友,一定对 p12 有所了解,因为苹果开发者网站制作的cer证书,只能用生成上传苹果后台的 CSR (Certificate Signing Request)文件 ———— CertificateSigningRequest.certSigningRequest 的macOS系统安装使用,同时,普通个人开发账号最多可注册iOS Development/Distribution 证书各2个。所以, p12 就是解决在多个电脑之间共享证书的一种方式。本文不会详解iOS证书相关知识,可以查看本文末的参考链接。
2、p12证书
Python学习交流群:1004391443
在讲解之前,先给大家说说原理。 p12 — KCS12 file holds the private key and certificate. 个人信息交换(personal information exchange file)
p12 存放了证书和私钥,使用的是DER编码。那什么是DER呢? 这里就要讲讲证书的知识:
证书标准
X.509 - 一种证书标准,主要定义了证书中应该包含哪些内容。其详情可以参考 RFC5280 , SSL 使用的就是这种证书标准。
编码格式
同样的 X.509 证书,可能有不同的编码格式,目前有以下两种编码格式:
- DER - Distinguished Encoding Rules,打开看是二进制格式,不可读.
- PEM - Privacy Enhanced Mail,打开看文本格式,以”—–BEGIN…”开头,”—–END…”结尾,内容是BASE64编码。
所以,要查看 p12 的内容,最好是转换成 PEM 格式。那么这里一种证书标准的格式,就需要实现了这种标准的工具就能解析:
- OpenSSL - OpenSSL是 SSL 的一个实现,SSL只是一种规范。理论上来说,SSL这种规范是安全的,目前的技术水平很难破解,但SSL的实现就可能有些漏洞,如著名的”心脏出血”。OpenSSL还提供了一大堆强大的工具软件,强大到90%我们都用不到。
所以,使用 OpenSSL 就可以读取 p12 的信息,可以通过以下命令把 p12 转换为 pem :
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">openssl pkcs12 -in XXX.p12 -out XXX.pem -nodes
</pre>
执行命令后,会要求输入 p12 文件的密码,如果密码错误: Mac verify error: invalid password? ,如果密码正确: MAC verified OK 。
示例:
p12:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561969166904" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
pem:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561969166908" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
pyOpenSSL 使用
所以,python 解析 p12 可以使用 pyOpenSSL ,这里就没有什么多说的,看代码就好:
先安装 pyOpenSSL :
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install pyOpenSSL
</pre>
p12 文件读取代码:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># load OpenSSL.crypto
from OpenSSL import crypto
open it, using password. Supply/read your own from stdin.
p12 = crypto.load_pkcs12(open("/Users/HTC/Desktop/HTC.p12", 'rb').read(), '123456')
cer = p12.get_certificate() # (signed) certificate object
pkey = p12.get_privatekey() # private key.
ca_cer = p12.get_ca_certificates() # ca chain.
print(cer, pkey, ca_cer)
print('版本', cer.get_version())
print('签名算法', cer.get_signature_algorithm())
print('序列号:', cer.get_serial_number())
print('证书是否过期:', cer.has_expired())
print('在此之前无效:', cer.get_notBefore())
print('在此之后无效', cer.get_notAfter())
主题名称
subject = cer.get_subject()
s_components = subject.get_components()
print(s_components)
key_dict = {'UID': '用户 ID',
'CN': '常用名称',
'OU': '组织单位',
'O': '组织',
'C': '国家或地区'
}
for (key, value) in s_components:
print(key, value)
print(key_dict.get(key.decode(), key))
签发者名称
suer = cer.get_issuer()
print(suer.get_components())
证书扩展信息
print('扩展数:', cer.get_extension_count())
print('扩展1:', cer.get_extension(0))
</pre>
总结
现在 Xcode8 之后,已经可以通过登陆开发者账号,自动下载和管理证书,从而减少开发者配置证书遇到的各位问题,非常的方便。当然,如果是多人开发或跨域团队,或者不想让开发人员拿到最高权限,使用 p12 还是目前最好的方式!因为如果一个主账号下的子账号都可以操作证书,其实影响和安全性很难保证,所以,苹果也有一定道理,很难在Xcode的账号中,分权限管理吧。
关于证书,这里只是介绍的说说 p12 的简单知识,证书体系有非常多的标准和知识,还需要多学习多实战,加油!