[英]How to manually calculate and verify the signature of a certificate signing request
[英]Manually created signature in certificate signing request is not matching with the openssl req generated signature
$ openssl genrsa -out test.key 2048
$ openssl req -new -key test.key -subj "/CN=foo" -out foo.csr
在@marc提供的步驟的幫助下,我從中提取了 4 個文件:
openssl asn1parse -in foo.csr -strparse 4 -out info.der
)openssl req -pubkey -in foo.csr -noout -out pub.pem
)"sha256 info.der"
十六進制輸出)openssl asn1parse -in foo.csr -strparse 338 -out sig.raw
)我的理解/懷疑是,foo.csr 中提到的“簽名”只不過是帶有私鑰“test.key”的“hash.manual”的“加密輸出”。 所以為了驗證我的理解,我使用了
$ openssl rsautl -encrypt -in hash_manual -inkey test.key -out manual_signature
現在,當我對這兩個文件執行 diff 時,它們不匹配並且 hexdump -C 確認 sig.raw 與 (openssl req -in csr --text) 中提到的簽名輸出匹配。
請幫助澄清為什么 manual_signature 和 sig.raw 不匹配。
你有兩個問題:
sign
而不是encrypt
。 對於RSA, encrypt
是用公鑰加密,而sign
是用私鑰加密rsautl
的輸出格式錯誤第一個很容易修復,只需使用-sign
。
第二個更煩人,不僅僅是sha256
輸出被簽名,它是一個 ASN.1 結構,如下所示:
0:d=0 hl=2 l= 49 cons: SEQUENCE
2:d=1 hl=2 l= 13 cons: SEQUENCE
4:d=2 hl=2 l= 9 prim: OBJECT :sha256
15:d=2 hl=2 l= 0 prim: NULL
17:d=1 hl=2 l= 32 prim: OCTET STRING
0000 - dc 31 c9 99 51 ce 03 a2-aa 14 13 f1 c4 f6 3e ea .1..Q.........>.
0010 - 4f 87 a2 56 37 de 7f a7-c1 87 49 f0 43 c9 ba bb O..V7.....I.C...
其中最后一個OCTET STRING
字段是原始sha256
哈希值。
生成它的最簡單方法是使用openssl dgst
結合散列和簽名:
# Hash and sign the certificationRequestInfo
$ openssl dgst -sha256 -sign test.key info.der > manual_signature
# Compare to extracted sig.raw (no output means no diff)
$ diff manual_signature sig.raw
# Verify both the extracted sig.raw and the manual_signature using the public key
$ openssl rsautl -verify -pubin -inkey pub.pem -in sig.raw -asn1parse
0:d=0 hl=2 l= 49 cons: SEQUENCE
2:d=1 hl=2 l= 13 cons: SEQUENCE
4:d=2 hl=2 l= 9 prim: OBJECT :sha256
15:d=2 hl=2 l= 0 prim: NULL
17:d=1 hl=2 l= 32 prim: OCTET STRING
0000 - dc 31 c9 99 51 ce 03 a2-aa 14 13 f1 c4 f6 3e ea .1..Q.........>.
0010 - 4f 87 a2 56 37 de 7f a7-c1 87 49 f0 43 c9 ba bb O..V7.....I.C...
$ openssl rsautl -verify -pubin -inkey pub.pem -in manual_signature -asn1parse
0:d=0 hl=2 l= 49 cons: SEQUENCE
2:d=1 hl=2 l= 13 cons: SEQUENCE
4:d=2 hl=2 l= 9 prim: OBJECT :sha256
15:d=2 hl=2 l= 0 prim: NULL
17:d=1 hl=2 l= 32 prim: OCTET STRING
0000 - dc 31 c9 99 51 ce 03 a2-aa 14 13 f1 c4 f6 3e ea .1..Q.........>.
0010 - 4f 87 a2 56 37 de 7f a7-c1 87 49 f0 43 c9 ba bb O..V7.....I.C...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.