duanbu1421 2016-04-21 15:17
浏览 46

如何在php中签署SOAP消息

I'm using php (yii2) and I'd like to implement SOAP communication with server. I have following guide to SOAP:

The Customer’s system uses the Customer’s private key for issuing digital signatures. Both the application request (ApplicationRequest) and the SOAP message must be signed separately in the WSC. The signature is performed with the private key. The signing system must include in the signature also the certificate. This certificate contains the public key corresponding to the private key used in the signing. The receiver uses the public key to authenticate the signature.

and:

Next step: Digitally sign (detached type XML Digital Signature) the whole SOAP message with the Private Key of Sender Certificate and put the signature into SOAP-header

So, I have own private.key, public.key and certificate.cer

My code looks like

    $client = new SoapClient($wdsl, ['trace' => true]);
    $arguments = ['DownloadFileListRequest' => $dflr];
    $appResponse = $client->__call('downloadFileList', $arguments);

But I get the expected error:

SOAP signature error

What I have to do and how to sign this SOAP?

  • 写回答

1条回答 默认 最新

  • doupu1727 2017-03-12 11:30
    关注

    XMLSecurityDSig helped (https://github.com/robrichards/xmlseclibs)

    $dom = new DOMDocument('1.0', 'UTF-8');
    $ar = $dom->createElementNS('http://bxd.fi/xmldata/', 'ApplicationRequest');
    $dom->appendChild($ar);
    $ar->appendChild($dom->createElement('CustomerId', $this->userID));
    ...
    $ar->appendChild($dom->createElement('Content', $contentBase64));
    
    $objDSig = new XMLSecurityDSig();
    $objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
    $objDSig->addReference(
                $dom,
                XMLSecurityDSig::SHA256,
                ['http://www.w3.org/2000/09/xmldsig#enveloped-signature'],
                ['force_uri' => true]
            );
    $objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, ['type'=>'private']);
    $objKey->loadKey($this->privateKeyPath, true);
    $objDSig->sign($objKey);
    $objDSig->add509Cert(base64_encode(file_get_contents($this->certificatePath)), false);
    $objDSig->appendSignature($dom->documentElement);
    
    $xmlRaw = $dom->saveXML();
    
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?