我正在尝试使用 openSSL 验证 XML 数字签名。当我实际使用 EVP_VerifyFinal 时,我得到错误代码 0D07209B (ASN1_get_object:too long)。他是我从证书加载 KeyInfo 的方式:
<KeyInfo>
<KeyValue>
<DSAKeyValue>
<P>/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQTxeEu0ImbzRMqzVDZkVG9xD7nN1kuFw==</P>
<Q>li7dzDacuo67Jg7mtqEm2TRuOMU=</Q>
<G>Z4Rxsnqc9E7pGknFFH2xqaryRPBaQ01khpMdLRQnG541Awtx/XPaF5Bpsy4pNWMOHCBiNU0NogpsQW5QvnlMpA==</G>
<Y>butK4tBy8dwSJFjTRpTvmYZYnsDGO4CzMVgcD8EQ2UJrQZd0ZapQI/Ea2DZzQBTFjjdnNkFuNOtjVI615lhiBQ==</Y>
</DSAKeyValue>
</KeyValue>
</KeyInfo>
我加载 PDSA 的(伪代码)方法:
pDSA = DSA_new();
pDSA.p = BN_bin2bn(base64Decode(text of P element));
pDSA.q = BN_bin2bn(base64Decode(text of Q element));
pDSA.g = BN_bin2bn(base64Decode(text of G element));
pDSA.pub_key = BN_bin2bn(base64Decode(text of Y element));
在此之后使用 BN_bn2hex 会给我以下值:
p: FCA682CE8E12CABA26EFCCF7110E526DB078B05EDECBCD1EB4A208F3AE1617AE01F35B91A47E6DF63413C5E12ED0899BCD132ACD50D99151BDC43EE737592E17
q: 962EDDCC369CBA8EBB260EE6B6A126D9346E38C5
g: 678471B27A9CF44EE91A49C5147DB1A9AAF244F05A434D6486931D2D14271B9E35030B71FD73DA179069B32E2935630E1C2062354D0DA20A6C416E50BE794CA4
pub_key: 6EEB4AE2D072F1DC122458D34694EF9986589EC0C63B80B331581C0FC110D9426B41977465AA5023F11AD836734014C58E376736416E34EB63548EB5E6586205
,验证签名:
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#dsa-sha1"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>uooqbWYa5VCqcJCbuymBKqm17vY=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>L4h4TFDj5rDKCHm0D+aH2LeSfAMV2t0V5S91Afu6U0NlfpjxdTXRUA==</SignatureValue>
<KeyInfo>
<KeyValue>
<DSAKeyValue>
<P>/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQTxeEu0ImbzRMqzVDZkVG9xD7nN1kuFw=</P>
<Q>li7dzDacuo67Jg7mtqEm2TRuOMU=</Q>
<G>Z4Rxsnqc9E7pGknFFH2xqaryRPBaQ01khpMdLRQnG541Awtx/XPaF5Bpsy4pNWMOHCBiNU0NogpsQW5QvnlMpA==</G>
<Y>POKkQtGuazaz6OEWi91P4C9z1tREpeP9f7L3piRD/3TiNFzvt0BmYzNO0CoPSjEVTXYKIRo/+HXK6MhBRk2eUw=</Y>
</DSAKeyValue>
</KeyValue>
</KeyInfo>
</Signature>
</Envelope>
这是我验证签名的方法:
pkey = EVP_PKEY_new;
EVP_PKEY_set1_DSA(pkey, pdsa)
EVP_MD_CTX_init(ctx);
EVP_VerifyInit(@ctx, EVP_sha1)
EVP_VerifyUpdate(@ctx, digest of SignedInfo);
bytes = unbase64(text of SignatureValue)
EVP_VerifyFinal(@ctx, btes, length(bytes)}, pKey);
验证最终返回以下错误:
0D07207B:asn1 encoding routines:ASN1_get_object:header too long)
0D068066:asn1 encoding routines:ASN1_CHECK_TLEN:bad object header)
0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error)
所以要么我错误地加载了证书或签名值。根据规范,签名值是“签名值由两个八位字节流的连接的base64编码组成,这两个八位字节流分别由按顺序对值r和s进行八位字节编码产生” - 但我在任何地方都找不到openSSL 文档应该将什么传递给 EVP_VerifyInit 以获得 DSA 签名的签名值。据我所知,我在一封电子邮件存档中找到了一个指向 RFC 3279 的参考,它似乎指定了相同的格式(Dss-Sig-Value ::= SEQUENCE { r INTEGER, s INTEGER }