【问题标题】:How to send SOAP digital signed message using cert如何使用证书发送 SOAP 数字签名消息
【发布时间】:2019-04-16 16:50:22
【问题描述】:

我想使用证书签署一个 XML 文档。它应该看起来像:

<soapenv:Envelope xmlns:obs="http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1" wsu:Id="X509-UUU09456789100000">XXXX==</wsse:BinarySecurityToken><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"><ec:InclusiveNamespaces PrefixList="obs soapenv" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:CanonicalizationMethod><ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><ds:Reference URI="#id-XYZXYZ1234567890000"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"><ec:InclusiveNamespaces PrefixList="obs" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transform></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><ds:DigestValue>vVYVY4CXo60TYkSZ8S/LQJo/8Zc=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>SIGNATURExxxx</ds:SignatureValue><ds:KeyInfo><wsse:SecurityTokenReference wsse11:TokenType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1" xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd"><wsse:Reference URI="#X509-UUU09456789100000" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1"/></wsse:SecurityTokenReference></ds:KeyInfo></ds:Signature></wsse:Security></soapenv:Header>
   <soapenv:Body wsu:Id="id-XYZXYZ1234567890000" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
      <obs:zapiszKomunikatOS>

          <komunikatOS>
              ...
          </komunikatOS>

      </obs:zapiszKomunikatOS>
   </soapenv:Body>
</soapenv:Envelope>

我曾尝试使用 XMLSecLibs,但不幸的是它不能很好地工作。首先,我不知道如何使用该工具添加包容性命名空间,也许这是正确签名的关键?我还尝试通过更改对上述模板的引用来复制摘要值和签名值,但它不起作用(我从 SOAP 服务器收到消息:“验证消息时遇到安全错误原因:签名或解密无效”)。

有一些丑陋的代码试图签名:

$communicateFile = __DIR__ . 'template-3.xml';
$pemFilePrv = __DIR__ . '/../cert/15.pem';
$xml = trim(file_get_contents($communicateFile));

$wsseNamespace = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
$wsuNamespace = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';

$doc = new \DOMDocument();
$doc->load($communicateFile);

$xp = new \DOMXPath($doc);

$xp->registerNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/'); 

$xp->registerNamespace('wsse',$wsseNamespace);
$xp->registerNamespace('wsu',$wsuNamespace);
$xp->registerNamespace('ds',XMLSecurityDSig::XMLDSIGNS);

$securityNode = $xp->query('//wsse:Security')->item(0);
$bodyNode = $xp->query('//soapenv:Body')->item(0);

$objDSig = new XMLSecurityDSig();
$objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
$objDSig->addReference($bodyNode, XMLSecurityDSig::SHA1,NULL, 
    array('prefix'=>'wsu','prefix_ns'=>$wsuNamespace)); 

$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type'=>'private'));
$objKey->passphrase = 'XXXXX';
$objKey->loadKey($pemFilePrv, TRUE);
$objDSig->sign($objKey);

$objDSig->insertSignature($securityNode);

谁能帮帮我?也许只缺少一个包含命名空间?我指望你,因为我没有想法..

最好的问候

【问题讨论】:

    标签: php xml soap sign xmlseclibs


    【解决方案1】:

    这个例子是它自己的一个正在生产中的项目。希望能帮到你。

    $context = stream_context_create([ 
        'ssl' => [ 
            'verify_peer' => false, 
            'verify_peer_name' => false, 
            'local_cert' =>  LOCAL_CERT, 
            'passphrase' => PRIVATE_KEY_PASSPHRASE 
        ] 
    ]); 
    
    $client = new BinarySignedSoapClient($wsdl , ['trace' => 1, 'stream_context' => $context]); 
    

    致电:

    class BinarySignedSoapClient extends \SoapClient 
    { 
    
        function __construct($wsdl, $context, $params) { 
          $this->crt_cert_file = array_key_exists('crt_cert_file', $params) ? $params['crt_cert_file'] : null; 
          $this->private_key_passphrase = array_key_exists('private_key_passphrase', $params) ? $params['private_key_passphrase'] : null; 
          $this->private_key_file = array_key_exists('private_key_file', $params) ? $params['private_key_file'] : null; 
          parent::__construct($wsdl, $context); 
        } 
    
        public function __doRequest($request, $location, $saction, $version, $one_way = 0) 
        { 
    
            $doc = new \DOMDocument('1.0'); 
            $doc->loadXML($request); 
    
            $objWSSE = new \WSSESoap($doc); 
    
            /* add Timestamp with no expiration timestamp */ 
            $objWSSE->addTimestamp(); 
    
            /* create new XMLSec Key using RSA_SHA1 and type is private key */ 
            $objKey = new \XMLSecurityKey(\XMLSecurityKey::RSA_SHA1, ['type' => 'private']); 
    
            /* load the private key from file - last arg is bool if key in file (true) or is string (false) */ 
            $objKey->passphrase = $this->private_key_passphrase; 
            $objKey->loadKey(__DIR__ ."/../localssl/". $this->private_key_file, true, false); 
    
            /* Sign the message - also signs appropiate WS-Security items */ 
            $options = array("insertBefore" => false); 
            $objWSSE->signSoapDoc($objKey, $options); 
    
            /* Add certificate (BinarySecurityToken) to the message */ 
            $token = $objWSSE->addBinaryToken(file_get_contents(__DIR__ ."/../localssl/". $this->crt_cert_file)); 
    
            /* Attach pointer to Signature */ 
            $objWSSE->attachTokentoSig($token); 
            $retVal = parent::__doRequest($objWSSE->saveXML(), $location, $saction, $version); 
    
            $doc = new \DOMDocument(); 
            $doc->loadXML($retVal); 
    
            $options = ["keys" => ["private" => ["key" => __DIR__ . $this->private_key_file, "isFile" => true, "isCert" => false]]]; 
            $objWSSE->decryptSoapDoc($doc, $options); 
    
    
            return $doc->saveXML(); 
    
        } 
    } 
    

    【讨论】:

    • 非常感谢 Ezequiel,您的解决方案非常有用 - 它生成整个 wsse 标头,这很棒,但 SOAP 服务器仍然拒绝整个消息“签名或解密无效”。我看到了一点不同 - 在由 WSSESoap 生成的 XML 中没有 inclusiveNamespace 标记,并且 binarySecurityToken 的 valueType 不同(docs.oasis-open.org/wss/2004/01/… 而不是docs.oasis-open.org/wss/2004/01/…),所以 token 的值也不同于 sopui
    • 我也用了soapui,token也是一样的。我完成了前面的示例,以防对您有所帮助。
    • 您好,感谢您的精彩回答!我在 Google 上搜索了您使用的类并找到了这个库:github.com/robrichards/wse-php 并使用 composer 安装了它,并使其与examples/soap-wsa-example.php 中的代码一起工作,我只需要稍微适应 PHP7。
    猜你喜欢
    • 2011-06-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 2010-12-16
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    相关资源
    最近更新 更多