Is there a way to digitally sign a x509 certificate or any document using openssl?
Digital signature and verification A digital signature is a mathematical scheme for presenting the authenticity of digital messages or documents. Message / file to be sent is signed with private key. Message received by the recipient is authenticated using public key.
To verify a signature, the recipient first decrypts the signature using a public key that matches with the senders private key. This produces a digest. Then the recipient calculates a digest from the received data and verifies that it matches with the one in the signature. If the digest match, the signature is valid.
DESCRIPTION. This command output the message digest of a supplied file or files in hexadecimal, and also generates and verifies digital signatures using message digests. The generic name, openssl dgst, may be used with an option specifying the algorithm to be used.
To Generate Private Key
openssl genrsa -out privatekey.pem 2048
To Sign
openssl dgst -sha256 -sign privatekey.pem -out data.txt.signature data.txt
To Generate The Public Key
dgst -verify
requires the public key
openssl rsa -in privatekey.pem -outform PEM -pubout -out publickey.pem
To Verify
openssl dgst -sha256 -verify publickey.pem -signature data.txt.signature data.txt
"Verified OK"
, return code 0
"Verification Failure"
, return code 1
Yes, the dgst and rsautl component of OpenSSL can be used to compute a signature given an RSA key pair.
openssl dgst -sha256 data.txt > hash openssl rsautl -sign -inkey privatekey.pem -keyform PEM -in hash >signature
openssl rsautl -verify -inkey publickey.pem -pubin -keyform PEM -in signature
Update: Capturing Reto's comments from below because this is an important nuance. Presumably if you are going to the trouble to verify, you want to know the signature was produced on the plaintext to which it is attached:
This might sound obvious for some but: Be aware, rsautl verify
just decrypts the file signature
. The output of this call is guaranteed to be produced by the owner of the private key, but beside that nothing else is being checked. So to actually verify the consistency of data.txt
you have to regenerate the digest and then compare it against the output of openssl rsautl -verify
.
data.txt
:openssl dgst -sha256 -verify publickey.pem -signature signature data.txt
For this operation, openssl requires the public key, the signature, and the message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With