androidpythonpkcs#7androguard

Is there any python package for parsing pkcs7?


I'm extracting features from Android .APK files with androguard and right now I need to extract the serial number(*) from its signature file (usually CERT.RSA). I've found asn1crypto, but I don't quite understand, how to use it with pkcs7. So is there any python package suitable for this purpose?

(*): Serial number


Solution

  • Comment: I have pkcs7 as a memory object, not a file

    PyOpenSSL does not read from file!

    OpenSSL.crypto.load_pkcs7_data(type, buffer)

    Load pkcs7 data from the string buffer encoded with the type type.
    The type type must either FILETYPE_PEM or FILETYPE_ASN1).

    fromSO Answer 45111623import get_certificates

    from OpenSSL import crypto
    pkcs7 = crypto.load_pkcs7_data(crypto.FILETYPE_ASN1, 
                                   open('certs/signature.der', 'rb').read())
    certs = get_certificates(pkcs7)
    for cert in certs:
        print('Subject:{}, Serial Nnumber:{}'.
            format(cert.get_subject(), cert.get_serial_number()))
    
    >>>Subject:<X509Name object '/CN=key1'>, Serial Nnumber:13315126025841024674
    >>>Subject:<X509Name object '/CN=key2'>, Serial Nnumber:14142490995367396705
    

    Question: python package for parsing pkcs7?

    You can convert PKCS#7 to PEM using openssl, PEM is readable using PyOpenSSL

    openssl pkcs7 -print_certs -in sample.p7b -out sample.cer
    

    Read that relevant SO Answer: pyOpenSSL's PKCS7