Able to decrypt smime with openssl, but not with python and M2crypto
I need to create a searchable archive of old emails, many of which are encrypted with S / MIME.
I can decrypt .eml files using openssl. It works:
openssl smime -decrypt -in mails/example.eml -inkey certs/example.pem
However, when I try to do the same with python and M2crypto, I get an error.
emailfile='mails/example.eml'
# Instantiate an SMIME object.
s = SMIME.SMIME()
# Load private key and cert. can be one arg or two
s.load_key('certs/example.pem')
# Load the encrypted data.
try:
p7, data = SMIME.smime_load_pkcs7(emailfile)
except SMIME.SMIME_Error, e:
print 'Error: could not load {file} because {error}'.format(file=emailfile,error=e)
sys.exit()
# Decrypt p7.
try:
out = s.decrypt(p7,0)
print out
except SMIME.PKCS7_Error, e:
sys.stderr.write('Error: could not decrypt {file} because PKCS7 says {error}\n'.format(file=emailfile,error=e))
except SMIME.SMIME_Error, e:
sys.stderr.write('Error: could not decrypt {file} because SMIME {error}\n'.format(file=emailfile,error=e))
When I run this code with the same email file and the same .pem file with the same private key and certificate, I get:
Error: could not decrypt example.eml because PKCS7 says key values mismatch
When I trace it, it looks like it doesn't verify the signature:
mailarcher.py(110): try:
mailarcher.py(111): out = s.decrypt(p7,0)
--- modulename: SMIME, funcname: decrypt
SMIME.py(182): if not hasattr(self, 'pkey'):
SMIME.py(184): if not hasattr(self, 'x509'):
SMIME.py(186): blob = m2.pkcs7_decrypt(pkcs7._ptr(), self.pkey._ptr(), self.x509._ptr(), flags)
--- modulename: SMIME, funcname: _ptr
SMIME.py(44): return self.pkcs7
--- modulename: EVP, funcname: _ptr
EVP.py(158): return self.pkey
--- modulename: X509, funcname: _ptr
X509.py(342): assert m2.x509_type_check(self.x509), "'x509' type error"
X509.py(343): return self.x509
mailarcher.py(113): except SMIME.PKCS7_Error, e:
mailarcher.py(114): sys.stderr.write('Error: could not decrypt {file} because PKCS7 says {error}\n'.format(file=emailfile,error=e))
I checked to see if there is a NOVERIFY flag that I can set and tried several flags with a call to s.decrypt but to no avail.
I can of course have a script just calling openssl, but I would like to stay inside python because I need a lot of other processing (multiple certificates, group lists, etc.) that would be easier with python.
Thanks for any help anyone can provide.
source to share