I am currently using openSSL
commands to parse a signed file, extract .clrs
and convert to .pem
at last.
to parse the signed file into asn1parse_content.txt
openssl asn1parse -inform DER -in signed.p7s >> asn1parse_content.txt
to extract crl content from signed.p7s using offsets and lengths got from asn1parse_content.txt
dd if=signed.p7s of=crl1.crl bs=1 count=5000 skip=4000
convert to pem from crl
openssl crl -inform DER -in crl1.crl -outform PEM -out crl1_pem.pem
I need to do the same things using python pyOpenSSL
module in windows, have checked SO for related posts, but none matches my requirement, most of the posts are related to RSA keys, private/public keys, and direct extraction of .pem
from .p7s
etc.
I am very much new to python
and OpenSSL
and have come up with a small program going through this
Below is the program to get asn1parse data from the p7s file.
import sys
from OpenSSL import crypto
from OpenSSL._util import (
ffi as _ffi,
lib as _lib,
)
p7s_file = sys.argv[1]
with open(p7s_file, 'rb') as f:
p7data = f.read()
p7 = crypto.load_pkcs7_data(crypto.FILETYPE_ASN1, p7data)
print p7
Actual Output
C:\Python27\lib\site-packages\OpenSSL\crypto.py:14: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography and will be removed in a future release.
from cryptography import utils, x509
<OpenSSL.crypto.PKCS7 object at 0x000000000359DE48>
But was expecting a series of lines like below
0:d=0 hl=2 l=inf cons: SEQUENCE
2:d=1 hl=2 l= 9 prim: OBJECT :pkcs7-signedData 13:d=1 hl=2 l=inf cons: cont [ 0 ]
15:d=2 hl=2 l=inf cons: SEQUENCE
17:d=3 hl=2 l= 1 prim: INTEGER :01 20:d=3 hl=2 l= 15 cons: SET
22:d=4 hl=2 l= 13 cons: SEQUENCE
24:d=5 hl=2 l= 9 prim: OBJECT :sha256 35:d=5 hl=2 l= 0 prim: NULL
37:d=3 hl=2 l=inf cons: SEQUENCE
39:d=4 hl=2 l= 9 prim: OBJECT :pkcs7-data 50:d=4 hl=2 l= 0 prim: EOC
and many other lines
I am extracting the crl
content using dd
command by using the offset i.e the first value, hl and l
I would appreciate any help with references and examples in this regard.
following are the post i have checked already
1
2
3
and many others
Update 1:
to the above code, I have added the below lines
pkcs7_PEM = crypto.load_pkcs7_data(crypto.FILETYPE_PEM, p7data)
pkcs7_crl = pkcs7_PEM.get_crls()
print pkcs7_crl
then I got this error
C:\Python27\lib\site-packages\OpenSSL\crypto.py:14: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography and will be removed in a future release. from cryptography import utils, x509
<OpenSSL.crypto.PKCS7 object at 0x000000000360EE80>
Traceback (most recent call last):
File "asn1_check.py", line 51, in
pkcs7_PEM = crypto.load_pkcs7_data(crypto.FILETYPE_PEM, p7data)
File "C:\Python27\lib\site-packages\OpenSSL\crypto.py", line 3131, in load_pkcs7_data _raise_current_error()
File "C:\Python27\lib\site-packages\OpenSSL_util.py", line 57, in exception_from_error_queue raise exception_type(errors)
OpenSSL.crypto.Error: [('PEM routines', 'get_name', 'no start line')]