PHP:如何在签名的soap消息中计算SignatureValue?

时间:2016-09-16 02:37:23

标签: php xml soap cryptography digital-signature

就像我在标题中所说,我需要一种方法来生成SignatureValue。 基本上,我有SignedInfo的和平:

<ds:SignedInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"><ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="soap"/>
</ds:CanonicalizationMethod>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/><ds:Reference URI="#id-A72D6FD4C41B1F545F14700558816389"><ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"><ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList=""/></ds:Transform></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue>iiyihYsFMjO7QxIVCauydehAhjSm5LZlRGm3lT0VFY0=</ds:DigestValue></ds:Reference></ds:SignedInfo>

我需要用PHP的方式来产生这个:

<ds:SignatureValue>
cGbhgNKCwrmUPXg2AKgqs1mceNcywK8BvrmmrOH627/3vadzKVnPiTn6ZaLBAcV1pYgTpNvh7RvAa8uZYXmS77YCQcYIOErbWKSTDVwBWv63d8fLm9Ljpx/1/PZrI7zSeIafXTLwPB2Lzt239ylZWPdhfg9XMhS43k4p7u1DZerVeRNSi76Q8u6jIWadDIQkn9mVEbhL5RIRGPoGJBof9QQVk42NHChdESW2RFXG7SSs2VYmdZ+IQUdEC7uPFoT/vxK2My1hGhYhvl6HNbMd5VIz/xMlDPrOCzbLWkA7oqyqSboTCObwkTwD2V20sxn6rb8mtak55zYaGXJldno66g==
</ds:SignatureValue>

这是私钥:

-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDLdHlv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xx2rLlwSncdIIeo4lPD/wyYNO5CHYHPncD8vwst
vHnyHdnZqI+OP54Uol5VUTNwzOY94tLorIcQm/qVOKoxks8W/OOboBHWJb3jUu5j
bp7YLH8hb1DeNSpSVV1B5jn7FopTSGK23zkzVAoARqfs0uPOsTKZKBshAoGAYSko
FZU/eP6m4yonU8xSCqZf720dj74WzXJFbcuXrZfthWgv8cIO1n1cXAekmVAobogn
/XOLs2UJdEiEbdA1iwkHCVTAv6Pw42XDJ+bV7knzlSwjJAn1A0aOl2++hqYu6CUs
3HjYN4l88tsIlGJOg9lAn8+f5drhRQIivJgUe1ECgYAFKrgG54xt6nxZ1/v2/eBn
u/2vY97yPqEw3C/jaD3Oj0DHhl+SUT+6r/PIkFmwPc2EvGHuUjpHgPnH6wNaJsr5
+B+55yD6EuPRaoIIe3nxnOYt7elK4+SQ7WpNzOEtQsJpW49dtbrI3IC4hWwpvQAB
PPTfRKCbv12rwQ/y4jJpEw==
-----END PRIVATE KEY-----

PS:我已经尝试了

 openssl_sign($doc->C14N(true), $signature, $pkey, OPENSSL_ALGO_SHA256)

我做了base64编码$签名,但不幸的是它没有生成相同的签名。

1 个答案:

答案 0 :(得分:0)

我通过将DS和SOAP名称空间添加到签名信息

来解决这个问题
<ds:SignedInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">