OpenSSL模块

从“crt”证书中解析信息:


# !/usr/bin/python3
# -*- coding: utf-8 -*-
import OpenSSL
import time
from dateutil import parser

# openssl x509 -inform DER -in test.cer -out certificate.crt
with open(r'C:\Users\admin\Desktop\2_1328217234828578816\u2x9.com\u2x9.com.crt', "r") as fp:
  crt_data = fp.read()

cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, crt_data)
certIssue = cert.get_issuer()

print("域名:               ", cert.get_subject().CN)
print("证书版本:            ", cert.get_version() + 1)

print("证书序列号:          ", hex(cert.get_serial_number()))

print("证书中使用的签名算法: ", cert.get_signature_algorithm().decode("UTF-8"))

print("颁发者:              ", certIssue.commonName)

datetime_struct = parser.parse(cert.get_notBefore().decode("UTF-8"))

print("有效期从:             ", datetime_struct.strftime('%Y-%m-%d %H:%M:%S'))

datetime_struct = parser.parse(cert.get_notAfter().decode("UTF-8"))

print("到:                   ", datetime_struct.strftime('%Y-%m-%d %H:%M:%S'))

print("证书是否已经过期:      ", cert.has_expired())

print("公钥长度", cert.get_pubkey().bits())

print("公钥:\n", OpenSSL.crypto.dump_publickey(OpenSSL.crypto.FILETYPE_PEM, cert.get_pubkey()).decode("utf-8"))

print("主体信息:")

print("CN : 通用名称  OU : 机构单元名称")
print("O  : 机构名    L  : 地理位置")
print("S  : 州/省名   C  : 国名")

for item in certIssue.get_components():
  print(item[0].decode("utf-8"), "  ——  ", item[1].decode("utf-8"))

print(cert.get_extension_count())

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值