我正在尝试创建一个Python代码来从PDF文件中的数字签名获取信息,但是使用下面的代码,它无法识别PDF文件中的可用数字签名。
import os
import re
import base64
import ssl
import glob
pdf_dir = r'C:\Users\est.matheus.ro\Desktop\Certificados\push\pdfs'
cert_regex = re.compile(b'-----BEGIN PKCS7-----\r?\n.+?\r?\n-----END PKCS7-----', re.DOTALL)
for filepath in glob.glob(os.path.join(pdf_dir, '*.pdf')):
with open(filepath, 'rb') as f:
pdf_content = f.read()
cert_match = cert_regex.search(pdf_content)
if cert_match is None:
print(f'Digital signature not found in file {filepath}.')
continue
cert_data = cert_match.group(0)
try:
pkcs7 = ssl.PKCS7(cert_data)
pkcs7_bio = ssl.BIO.MemoryBuffer(cert_data)
pkcs7.verify(None, pkcs7_bio)
except ssl.Error:
print(f'Error decoding the digital signature of file {filepath}.')
continue
signers = pkcs7.get0_signers(ssl.X509.X509_Stack())
if len(signers) < 1:
print(f'Invalid digital signature in file {filepath}.')
continue
signer = signers[0]
subject = signer.get_subject()
name = subject.commonName
cpf = subject.serialNumber
date = signer.get_notBefore().decode('ascii')
validity = signer.get_notAfter().decode('ascii')
print(f'Digital signature found in file {filepath}.')
print('Name: ', name)
print('CPF: ', cpf)
print('Issuance date: ', date)
print('Expiration date: ', validity)
我希望此代码提取以下数字证书信息:
请检查以下 StackOverflow 答案:如何使用 python 在 PDF 文件中查找数字签名
通常我们不读“发行日期”,而是读
certificate.validity.not_before
日期。到期日为 certificate.validity.not_after