pythontimestampasn.1asn1crypto

Export Time stamp 'signing_time' from signed data in asn1 format


Question is: Exporting timestamp (signing time) from signeddata? I verify my signature before. I have an asn1 signed data to verification.

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']

I want to export time stamp from signed data.

I use this code:

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

Another try:

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

But no result. How can I fix this?


Solution

  • 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


    Show all Attributes

    # 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)]
    

    Find Attribute by name

    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)]
    

    Tested with Python:3.5.3 asn1crypto:0.24.0