python-3.xx509asn1crypto

Searching for certain values in asn1 cert


The asn1crypto package with x509 is being used. I'd like to find particular values in the .der file. The file is opened and read(), then:

mycert = x509.Certificate.load(data)

This returns an object of type asn1crypto.x509.Certificate like so b'0\x81\x50\...'. In debug, mycert can be expanded to show the various keys and values, however I'd like to search directly in the 'mycert' for such keys/values. How can I do this?

EDIT:

The asn1crypto package doesn't have to be used, another one can be used instead.

EDIT:

Expanded code:

with open(cert_path, 'rb') as cert_file:
    data = cert_file.read()

mycert = x509.Certificate.load(data)

a = mycert.native # doesn't work!

Solution

  • In asn1crypto.x509 the attribute native contains the native Python datatype representation of the certificate. The values are hierarchically structured and can be OrderedDicts as well:

    import asn1crypto.x509 as x509
    import pprint
    
    with open('crt.der', mode='rb') as file:
        data = file.read()
    
        mycert = x509.Certificate.load(data)
    
        pprint.pprint(mycert.native)
    

    Output:

    OrderedDict([('tbs_certificate',
                  OrderedDict([('version', 'v3'),
                               ('serial_number', 15158908894724103801),
                               ('signature',
                                OrderedDict([('algorithm', 'sha256_rsa'),
                                             ('parameters', None)])),
                               ('issuer',
                                OrderedDict([('country_name', 'XX'),
                                             ('state_or_province_name',
                                              'Some-State'),
                                             ('locality_name', 'Some-City'),
                                             ('organization_name', 'example ltd'),
                                             ('common_name', 'www.example.com'),
                                             ('email_address',
                                              'info@example.com')])),
                               ('validity',
                                OrderedDict([('not_before',
                                              datetime.datetime(2022, 9, 5, 6, 58, 21, tzinfo=datetime.timezone.utc)),
                                             ('not_after',
                                              datetime.datetime(2022, 10, 5, 6, 58, 21, tzinfo=datetime.timezone.utc))])),
                               ('subject',
                                OrderedDict([('country_name', 'XX'),
                                             ('state_or_province_name',
                                              'Some-State'),
                                             ('locality_name', 'Some-City'),
                                             ('organization_name', 'example ltd'),
                                             ('common_name', 'www.example.com'),
                                             ('email_address',
                                              'info@example.com')])),
    ...
    

    You can find several discussions in SO on how to search in a nested dict like "Find all occurrences of a key in nested dictionaries and lists".