簡體   English   中英

證書簽名請求中手動創建的簽名與 openssl req 生成的簽名不匹配

[英]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 個文件:

    • info.der ( openssl asn1parse -in foo.csr -strparse 4 -out info.der )
    • pub.pem ( openssl req -pubkey -in foo.csr -noout -out pub.pem )
    • hash.manual(保存命令"sha256 info.der"十六進制輸出)
    • sig.raw ( 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM