Background
I have signed a file using openssl SHA256 and a private key as follows:
with subprocess.Popen(
# Pipe the signature to openssl to convert it from raw binary encoding to base64 encoding.
# This will prevent any potential corruption due to line ending conversions, and also allows
# a human to read and copy the signature (e.g. for manual verification).
'openssl dgst -sha256 -sign private.key sign_me.zip | openssl base64 > signature.sha256',
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
) as proc:
out, _ = proc.communicate()
Requirements
- I need to use
signature.sha256
andpublic_key.crt
to verify thatsign_me.zip
has not been modified. - Compatible with Python 3.2 - 3.4
- Needs to work on both Windows and Redhat, and there is no guarantee that OpenSSL will be on the path or in a known location. Ideally I'd like to use a core Python module, but I will consider a 3rd party module if it reduces complexity.
What I've Tried
I've done a lot of searching trying to figure out how to do this, but I haven't been able to find a satisfactory answer. Here is a list of things I've tried and/or researched:
I am able to manually verify the signature via the following shell command. This won't work as a permanent solution due to requirement 3.
openssl dgst -sha256 -verify <(openssl x509 -in public_key.crt -pubkey -noout) -signature signature.sha256 sign_me.zip
I found this question, which is almost exactly what I want to do. It hasn't been answered or even commented on in nearly 2 years. It mentions the ssl python library, which deals mostly with client/server certificates and sockets.
- This question appears to use the crypto library to verify a "SHA256withRSA and PKCS1 padding" signature. Unfortunately it targets Python 2.7, and additionally I wasn't able to locate the
verify()
method documentation in the Python 2.7 crypto module that the question references. - I also discovered a 3rd party module called cryptography. Consensus on Stack Overflow seems to be that this is the latest/greatest module for encryption and such, but I wasn't able to find documentation that matched my requirements.
Perhaps I'm missing something obvious? I haven't done much work with security/encryption/hashing, so feedback is welcome.