【发布时间】:2020-09-08 16:06:50
【问题描述】:
我正在学习一些 OpenSSL RSA 的用法。我注意到有两种不同的方式来生成和验证文件签名。一个使用 openssl-dgst(1),另一个使用 openssl-pkeyutl(1),它们似乎都在验证、接受私有和公共证书、输出签名文件,接受算法,但它们不可互换。 openssl-pkeyutl 生成的签名不能作为 openssl-dgst 的签名进行验证,反之亦然。即使两个签名文件具有相似的数据结构格式。
注意:这是专门针对 RSA 证书的。我不完全确定 X.509 证书是否存在同样的可能性。
问题:哪种用法更合适? openssl-pkeyutl 还是 openssl-dgst?
方法一:openssl dgst
# directory/
# test.txt - File to create a signature for
# cert.pem - Private/Public RSA Key, encrypted with hmacWithSHA256 via PKCS#8
# cert.pub.pem - Public Key of cert.pem extracted with `openssl rsa -pubout...`
# test.sig - File signature created by OpenSSL
# Create test.sig
$ openssl dgst -sha256 -sign cert.pem -out test.sig test.txt
Enter pass phrase for cert.pem: test
# Verify with the private key
$ openssl dgst -sha256 -verify cert.pub.pem -signature test.sig test.txt
Verified OK
# Verify with the public key
$ openssl dgst -sha256 -prverify cert.pem -signature test.sig test.txt
Enter pass phrase for cert.pem: test
Verified OK
方法二:openssl pkeyutl
# directory/
# test.txt - File to create a signature for
# cert.pem - Private/Public RSA Key, encrypted with hmacWithSHA256 via PKCS#8
# cert.pub.pem - Public Key of cert.pem extracted with `openssl rsa -pubout...`
# test.sig - File signature created by OpenSSL
# Create test.sig
$ openssl pkeyutl -sign -in test.txt -out test.sig -inkey cert.pem
Enter pass phrase for cert.pem: test
# Verify with the private key
$ openssl pkeyutl -verify -sigfile test.sig -in test.txt -inkey cert.pub.pem -pubin
Signature Verified Successfully
# Verify with the public key
$ openssl pkeyutl -verify -sigfile test.sig -in test.txt -inkey cert.pem
Enter pass phrase for cert.pem: test
Signature Verified Successfully
【问题讨论】:
-
@dave_thompson_085 - 首先,我知道这两个工具会产生不同的数据。其次,您的第一个欺骗是比较 rsautl 和 dgst,而不是 dgst 和 pkeyutl。 first suggested dupe。第三个second suggested dupe 已通过 pkeyutl 解决。再次 #3、#4、#5 dgst 和 rsautl。 #6,#7,又是 404,#8 rsautl。我知道它们的哈希值不同。哪一个是合适的?这就是问题所在。
-
@dave_thompson_085 是的,我知道“openssl dgst -sha256”和“openssl pkeyutl -pkeyopt digest:sha256”之间有区别,它们不一样。
标签: encryption openssl rsa digital-signature