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?
Asked
Active
Viewed 6,800 times
1 Answers
7
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).
from
SO Answer 45111623
import 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

stovfl
- 14,998
- 7
- 24
- 51
-
Thank you for your answer! I already know that, but I have pkcs7 as a memory object, not a file. It's not that I couldn't save it as a file, I just wanted to do it this way. That's why I wanted to find a python package so bad. Anyway, since a signature has always the same structure I simply parse ASN1 directly with [asn1 package](https://pypi.python.org/pypi/asn1/2.1.0) and extract a serial number. – Gleb Ignatev Aug 21 '17 at 15:30
-
`get_certificates()` defined here https://stackoverflow.com/questions/45104923/pyopenssls-pkcs7-object-provide-very-little-information-how-can-i-get-the-sha1#answer-45111623 – evandrix Aug 27 '19 at 06:40
-
`AttributeError: module 'OpenSSL' has no attribute 'get_certificates'`, What is `get_certificates`? – e-info128 Feb 12 '20 at 01:12
-
@e-info128 ***What is `get_certificates`"?"***: Follow the link in **fromSO Answer 45111623 `import get_certificates`** – stovfl Feb 12 '20 at 08:35
-
Some of the features of this packages are deprecated.. like support of x509. So its better to use [cryptography](https://cryptography.io/en/latest/) Please take a look at [this](https://www.pyopenssl.org/en/stable/api/crypto.html?highlight=cryptography#OpenSSL.crypto.X509.from_cryptography) – Aug 27 '21 at 09:22