Ok, I am trying to verify data from PKCS7 envelop using Python.
I have the working code in Java: http://nyal.developpez.com/tutoriel/java/bouncycastle/#L4.2
What I want is first get the certificate from the envelop.
I am able to open it with that command:
openssl pkcs7 -in pkcs7 -print_certs -text
Then I want to verify that the data is ok.
I tried this:
import base64
from M2Crypto import SMIME, X509, BIO
raw_sig = """base64 PKCS7 envelop"""
msg = "challenge message to verify"
sm_obj = SMIME.SMIME()
x509 = X509.load_cert('/etc/ssl/certs/ca-certificates.crt') # public key cert used by the remote
# client when signing the message
sk = X509.X509_Stack()
sk.push(x509)
sm_obj.set_x509_stack(sk)
st = X509.X509_Store()
st.load_info('/etc/ssl/certs/ca-certificates.crt') # Public cert for the CA which signed
# the above certificate
sm_obj.set_x509_store(st)
# re-wrap signature so that it fits base64 standards
cooked_sig = '\n'.join(raw_sig[pos:pos+76] for pos in xrange(0, len(raw_sig), 76))
# now, wrap the signature in a PKCS7 block
sig = """
-----BEGIN PKCS7-----
%s
-----END PKCS7-----
""" % cooked_sig
# print sig
# and load it into an SMIME p7 object through the BIO I/O buffer:
buf = BIO.MemoryBuffer(sig)
p7 = SMIME.load_pkcs7_bio(buf)
# do the same for the message text
data_bio = BIO.MemoryBuffer(msg)
cert = sm_obj.verify(p7, data_bio)
I think one of the /etc/ssl/certs/ca-certificates.crt should be the userCertificate.
After having the certificate I want to check that it is still valid (using validatity date) and verify it against the CRL and the CPS for revocation.
I hope you can help me.
So I was almost there:
Then you might also be interested in CRL and OCSP validation:
cert_parentis the file with ROOT.crt and ISSUER.crt concatenates together.cert_parent_crlis the file with ROOT.crt, ISSUER.crt and CRL concatenates together.To concat the CRL with other certificate I use: