2010-01-27 335 views
3

我一直在试图找到一些关于如何为XML签名(WSDL)生成DigestValue和SignatureValue的示例。如何使用PHP/linux工具生成封装XML签名的DigestValue和SignatureValue

下面是用于所述应用的样品SOAP:

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soapenv:Header> 
<SOAP-SEC:Signature soapenv:actor="" soapenv:mustUnderstand="0"> 
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
<ds:SignedInfo> 
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/> 
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> 
<ds:Reference URI="#Body"> 
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> 
<ds:DigestValue></ds:DigestValue> 
</ds:Reference> 
</ds:SignedInfo> 
<ds:SignatureValue></ds:SignatureValue> 
</ds:Signature> 
</SOAP-SEC:Signature> 
</soapenv:Header> 
<soapenv:Body Id="Body"> 
<SomeApplicationMethod soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
<Application href="#id0"/></SomeApplicationMethod> 
<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:NCDServices" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns1:SomeApplicationMethod"> 
<param2 xsi:type="soapenc:string">123456</param2> 
<param3 xsi:type="soapenc:string">someString</param3> 
<param4 xsi:type="soapenc:string">string123 bla bla</param4> 
<param5 xsi:type="soapenc:string">0</param5> 
</multiRef> 
</soapenv:Body> 
</soapenv:Envelope> 

我具有从应用程序提供者的单个例子,我似乎无法产生相同的DigestValue和的SignatureValue作为样品。根据应用程序文档,我需要从整个Body部分创建digestValue。

我知道我必须cannonicalize,sha1,然后base64encode。但是,我如何使身体部分变得无法形容? (sha1和base64编码很容易理解)。

起初我想尝试创建一个字符串VAR:

$whole_body = '<soapenv:Body Id="Body"> ........stuff inside...... </soapenv:Body>'; 

但后来我发现,有没有这样的事情在PHP(至少不会串像var和PHP 5.2和C14N功能下面;不用担心,我有5.2.6)。

我看着xmlseclibs.php代码,它似乎使用DOMelement对象来获取C14N函数。我不是很熟悉DOMelement是什么..

我尝试使用xmlseclibs.php签署我的XML,但似乎没有工作,我从应用程序中得到'签名无效'错误。

有人可以帮助启发新手(读,我:))?

非常感谢您的合作。

回答

1
<?php 

$dom = new DOMDocument(); 
$dom->loadXML($yourXML); // the XML you specified above. 

$canonicalized = $dom->C14N(); // for the whole document 

// .. or: 
$tag = $dom->getElementById("yourID"); // give it an ID, or use getElementsByTagName with ->item(0) or something 
$canonicalized = $tag->C14N(); 

// $canonicalized is the canonicalized form of the XML 
// So, The DigestValue is just: 
$digest = base64_encode(pack("H*", sha1($canonicalized))); 
?> 

这应该做你的伎俩。

我也忙于XML-DSIG,并琢磨着要规范化什么作品,因为我收到的文档质量很差... :(

1

嗯..其实我设法得到它的工作前一阵子。

我结束了由罗布·理查兹使用xmlseclibs。http://code.google.com/p/xmlseclibs/

我建议你试试这个库,这是非常容易使用。

+0

你能请张贴代码来获取身体和签字吗?谢谢 – user447951 2012-07-12 21:48:24

+0

@ user447951,你可以克et http://code.google.com/p/xmlseclibs/中的代码 – mohdyusuf 2012-08-01 17:11:57