python - 从 asn1 格式的签名数据中导出时间戳 'signing_time'

标签 python timestamp asn.1 asn1crypto

问题是:从签名数据导出时间戳(签名时间)? 我之前验证过我的签名。 我有一个 asn1 签名的数据要验证。

import cryptography
import OpenSSL
from OpenSSL.crypto import *
from asn1crypto import cms, util, x509, pem, tsp
import os
import sys
import base64bs64 = base64.b64decode(response)  
content = cms.ContentInfo.load(bs64)                 
signed_data = content['content']
encap_content_info = signed_data['encap_content_info']
tst_info = encap_content_info['content']
pk = load_pkcs7_data(FILETYPE_ASN1,bs64)
signers = signed_data['signer_infos']

我想从签名数据中导出时间戳。

我使用这段代码:

att = cms.CMSAttribute.load(bs64)            
oid = att._oid_specs
st = oid['signing_time']
child = st._child_spec
alt = child._alternatives

另一个尝试:

time_Res = tsp.TimeStampedData.load(bs64)
time_content = time_Res['content']

但是没有结果。我该如何解决这个问题?

最佳答案

Question: My question is getting time stamp (signing time)

import os
from asn1crypto import cms

"""
https://github.com/wbond/asn1crypto/blob/master/tests/fixtures/pkcs7-signed.der
"""
fixtures_dir = 'tests/fixtures'
with open(os.path.join(fixtures_dir, 'pkcs7-signed.der'), 'rb') as f:
    der_bytes = f.read()

info = cms.ContentInfo.load(der_bytes)
signed_data = info['content']

encap_content_info = signed_data['encap_content_info']
print("message:{}".format(encap_content_info['content'].native.decode()))
#>>> message:This is the message to encapsulate in PKCS#7/CMS

signer = signed_data['signer_infos'][0]

# signed_attrs == cms.CMSAttributes()
signed_attrs = signer['signed_attrs']
print("signing_time:{}".format(signed_attrs[1]['values'][0].native))

Output: >>> signing_time:2015-06-03 05:55:12+00:00


显示所有属性

# signed_attrs.native == List of OrderedDict with keys 'type' and 'values'
for i, attr in enumerate(signed_attrs.native):
    print("[{}]:{}:\t{}".format(i, attr['type'], attr['values']))

Output:

[0]:content_type:   ['data']
[1]:signing_time:   [datetime.datetime(2015, 6, 3, 5, 55, 12, tzinfo=datetime.timezone.utc)]
[2]:message_digest: [b'R\x88%G\x15[-PDh\x05$\xc8qZ\xccb(6\x17\xb7h\xee\xa1\x12\x90\x96O\x94\xae\xdby']
[3]:1.2.840.113549.1.9.15:  [OrderedDict([('0', OrderedDict([('0', '2.16.840.1.101.3.4.1.42')])), ... (omitted for brevity)]

按名称查找属性

f = [attr['values'] for attr in signed_attrs.native if attr['type'] == 'signing_time'][0]
print(f)
>>> [datetime.datetime(2015, 6, 3, 5, 55, 12, tzinfo=datetime.timezone.utc)]

使用 Python:3.5.3 asn1crypto:0.24.0 测试

关于python - 从 asn1 格式的签名数据中导出时间戳 'signing_time',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52800223/

相关文章:

python - GUI 卡住了其余的 python 代码

python - 如何将 dataframe.iat 与字符串列索引一起使用

python - 如何从 UTC 转换为 tzutc() 时区?

swift - 创建一个继承自UITouch的类

asn.1 - 什么是 ASN.1 DER 标签值 0xA0?

PhpStorm被动监听请求

python - 发出具有任意签名的新型 PyQt 信号

sql - 在 Firebird 中创建一个整数时间戳

char * 到 ASN1_INTEGER

ssl - 将 x.509 SSL 证书的主题字段中的 "emailAdress=<email-address>"转换为十六进制