pythonpkix509certificate-revocation

Is there a (simple) way to parse CRL in Python?


I'm trying to do something stupid: load a CRL and output the list of revoked certificates serials.

With M2Crypto loading the CRL is done with:

import M2Crypto
crl = M2crypto.X509.load_crl('my.crl')

But i'm really surpised that the returned object has only one usefull which is

crl.as_text()

With some regexp, i can parse the output to retrieve my revoked serials. But is there an another way to do that?

For information, here is a classical CRL as_text output.

Certificate Revocation List (CRL):
        Version 2 (0x1)
        Signature Algorithm: sha1WithRSAEncryption
        Issuer: /C=FR/ST=IDF/L=Paris/O=XXXXX/OU=XXXXX/CN=XXXXX Certificate Authority
        Last Update: Nov  6 21:49:51 2010 GMT
        Next Update: Nov  7 21:49:51 2010 GMT
Revoked Certificates:
    Serial Number: 02
        Revocation Date: Aug 10 15:40:09 2010 GMT
    Serial Number: 03
        Revocation Date: Sep  9 15:12:24 2010 GMT
    Serial Number: 05
        Revocation Date: Aug 17 14:18:22 2010 GMT
    Serial Number: 06
        Revocation Date: Aug 18 08:57:15 2010 GMT
    Signature Algorithm: sha1WithRSAEncryption
        d1:05:da:1f:c0:1c:68:78:0e:e2:ea:78:de:b8:b2:58:9c:ba:
        b4:7c:c5:e8:2a:8d:8c:82:1d:4b:ed:a7:2d:cb:f6:bf:da:fa:
        38:a4:7a:3d:2b:19:6c:7a:ba:4c:1c:4c:e4:d8:e6:20:3d:0a:
        95:03:75:bf:17:cf:97:ce:3e:4a:93:1c:a6:4c:36:62:97:a2:
        d3:be:f2:78:38:89:13:3e:d4:b0:80:a1:24:52:0d:3a:01:67:
        0d:4f:e7:0b:07:0c:80:04:b7:25:66:a4:61:36:dd:3a:24:29:
        30:67:f6:23:31:34:6f:0b:a8:30:c1:c9:b7:ee:4e:2b:7a:e7:
        6b:31:7d:0b:cb:12:8a:7c:5f:7e:73:a0:42:8d:ea:4f:f7:76:
        ce:1b:0b:6c:6a:3e:eb:08:a6:d6:67:81:cb:cb:98:6d:40:ec:
        8c:e5:a5:f7:f0:ed:0c:7f:38:fd:42:3d:19:c4:69:ec:eb:71:
        7a:e1:30:b4:81:98:f5:00:a0:bd:ac:75:46:15:e6:2b:1c:da:
        f4:09:19:e5:1b:4e:c9:a4:7c:11:79:24:a4:3b:13:84:84:a7:
        5b:0e:07:80:ae:ae:26:8e:d7:b3:cb:b8:6c:79:df:9d:26:b0:
        34:bc:c1:f4:8f:4b:3e:f5:9b:d0:e3:e7:ab:37:27:f6:79:09:
        47:fb:76:07

Solution

  • Job's done thanks to pyOpenSSL. Here is the code to use :

    import OpenSSL
    
    with open('path_to_the_crl', 'r') as _crl_file:
        crl = "".join(_crl_file.readlines())
    
    crl_object = OpenSSL.crypto.load_crl(OpenSSL.crypto.FILETYPE_PEM, crl)
    
    revoked_objects = crl_object.get_revoked()
    
    for rvk in revoked_objects:
        print "Serial:", rvk.get_serial()
    

    This code give the following output with my CRL example:

    Serial: 02
    Serial: 03
    Serial: 05
    Serial: 06