Encryption 证书签名请求中手动创建的签名与openssl req生成的签名不匹配 使用以下命令创建了证书签名请求:

Encryption 证书签名请求中手动创建的签名与openssl req生成的签名不匹配 使用以下命令创建了证书签名请求:,encryption,openssl,public-key-encryption,Encryption,Openssl,Public Key Encryption,借助提供的步骤,我从中提取了4个文件: info.der(opensslasn1parse-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) 我的

借助提供的步骤,我从中提取了4个文件:

  • info.der(
    opensslasn1parse-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中提到的“签名”只不过是“hash.manual”中带有私钥“test.key”的“加密输出”。所以为了验证我的理解,我用了

    • 现在,当我对这两个文件执行diff时,它们不匹配,hexdump-C确认sig.raw与(openssl req-in csr--text)中提到的签名输出匹配

    • 请帮助澄清手动签名和sig.raw不匹配的原因

      • 您有两个问题:

        • 您需要使用
          签名
          而不是
          加密
          。对于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...
        
        其中,最后一个
        八位字节字符串
        字段是原始
        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...
        

        verify
        的反义词是
        sign
        ,而不是
        encrypt
        。sign/signatue==rsa_encruption of(hash of(data))??这是我的理解。如果不是,请更正。是的,但它是用私钥加密的(请参阅)。普通加密使用公钥进行加密。因为其他算法不是这样工作的,所以我们通常说
        符号
        验证
        。因为
        test.key
        同时包含私钥和公钥,
        openssl
        只使用它需要的密钥。谢谢@Marc。了解使用
        openssl dgst
        一步完成散列和签名的简易性。当我使用私钥使用openssl rsautl
        echo boo>boo对文件进行加密时,会出现奇怪的行为
        openssl rsautl-encrypt-in boo-inkey test.key-out boo.encrypted.withprivatekey
        openssl rsautl-encrypt-in boo-pubin-inkey pub.pem-out boo.encrypted.withpublickey
        diff boo.encrypted.withprivatekey boo.encrypted.withpublickey
        -二进制文件boo.encrypted.withprivatekey和boo.encrypted.withpublickey不同
        如果在我输入私钥时rsautl使用公钥进行加密,则不确定原因boo.encrypted.withprivatekey和boo.encrypted.withpublickey是不同的?这是预期的,RSA加密使用随机填充(rsautl的默认值为
        PKCS#1 v1.5
        )来接近安全。请注意,如果多次运行相同的加密命令,也会得到不同的结果。详见问题。
            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...
        
        # 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...