<?php
namespace PHPCadesSOAP;
use SoapClient;
use PHPCadesSOAP\Logger;
use DOMDocument;
use PHPCadesSOAP\CryptoPro;
use CPSigner;
use CPSignedXml;
class MySoapClient extends SoapClient {
private $certSubjectName = 'test certificate';
private $IPS_id = '********-****-****-****-************';
function __doRequest($request, $location, $saction, $version, $one_way = NULL) {
Logger::debug('Неподписанный запрос от MySoapClient - ' . $request);
$cert = CryptoPro::SetupCertificate(CURRENT_USER_STORE, "My", STORE_OPEN_READ_ONLY, CERTIFICATE_FIND_SUBJECT_NAME, $this->certSubjectName, 0, 1);
$certData = preg_replace('/\n+/', '', $cert->export(0));
$xml = new DOMDocument();
$xml->loadXML($request);
$Envelope = $xml->getElementsByTagName('Envelope')->item(0);
$Body = $Envelope->getElementsByTagName('Body')->item(0);
$prefix = $Envelope->prefix;
$Envelope->setAttribute('xmlns:wsse', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
$Envelope->setAttribute('xmlns:wsu', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd');
$Envelope->setAttribute('xmlns:ds', 'http://www.w3.org/2000/09/xmldsig#');
$Envelope->setAttribute('xmlns:a', 'http://www.w3.org/2005/08/addressing');
$Header = $xml->createElement($prefix . ':Header');
$To = $xml->createElement('a:To', 'https://**********************');
$Action = $xml->createElement('a:Action', 'sendResponse');
$ReplyTo = $xml->createElement('a:ReplyTo');
$ReplyToAddress = $xml->createElement('a:Address', 'http://www.w3.org/2005/08/addressing/anonymous');
$FaultTo = $xml->createElement('a:FaultTo');
$FaultToAddress = $xml->createElement('a:Address', 'http://www.w3.org/2005/08/addressing/anonymous');
$MessageID = $xml->createElement('a:MessageID', CryptoPro::createMessageID());
$Security = $xml->createElement('wsse:Security');
$BinarySecurityToken = $xml->createElement('wsse:BinarySecurityToken', $certData);
$BinarySecurityToken->setAttribute('EncodingType', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary');
$BinarySecurityToken->setAttribute('ValueType', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3');
$bsd_id = uniqid('x509-');
$BinarySecurityToken->setAttribute('wsu:Id', $bsd_id);
$Signature = $xml->createElement('ds:Signature');
$SignedInfo = $xml->createElement('ds:SignedInfo');
$CanonicalizationMethod = $xml->createElement('ds:CanonicalizationMethod');
$CanonicalizationMethod->setAttribute('Algorithm', 'http://www.w3.org/2001/10/xml-exc-c14n#');
$SignatureMethod = $xml->createElement('ds:SignatureMethod');
$SignatureMethod->setAttribute('Algorithm', 'urn:ietf:params:xml:ns:cpxmlsec:algorithms:gostr34102012-gostr34112012-256');
$Reference_of_body = $xml->createElement('ds:Reference');
$uri_on_body = uniqid('body-');
$Reference_of_body->setAttribute('URI', '#' . $uri_on_body);
$Body->setAttribute('wsu:Id', $uri_on_body);
$Reference_of_body_Transforms = $xml->createElement('ds:Transforms');
$Reference_of_body_Transforms_Transform = $xml->createElement('ds:Transform');
$Reference_of_body_Transforms_Transform->setAttribute('Algorithm', 'http://www.w3.org/2001/10/xml-exc-c14n#');
$Reference_of_body_DigestMethod = $xml->createElement('ds:DigestMethod');
$Reference_of_body_DigestMethod->setAttribute('Algorithm', 'urn:ietf:params:xml:ns:cpxmlsec:algorithms:gostr34112012-256');
$Reference_of_body_DigestValue = $xml->createElement('ds:DigestValue');
$SignatureValue = $xml->createElement('ds:SignatureValue');
$KeyInfo = $xml->createElement('ds:KeyInfo');
$SecurityTokenReference = $xml->createElement('wsse:SecurityTokenReference');
$SecurityTokenReference_Reference = $xml->createElement('wsse:Reference');
$SecurityTokenReference_Reference->setAttribute('ValueType', "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
$SecurityTokenReference_Reference->setAttribute('URI', '#' . $bsd_id);
$Envelope->insertBefore($Header, $Body);
$Header->appendChild($To);
$Header->appendChild($Action);
$Header->appendChild($ReplyTo);
$ReplyTo->appendChild($ReplyToAddress);
$Header->appendChild($FaultTo);
$FaultTo->appendChild($FaultToAddress);
$Header->appendChild($MessageID);
$Header->appendChild($Security);
$Security->appendChild($BinarySecurityToken);
$Security->appendChild($Signature);
$Signature->appendChild($SignedInfo);
$SignedInfo->appendChild($CanonicalizationMethod);
$SignedInfo->appendChild($SignatureMethod);
$SignedInfo->appendChild($Reference_of_body);
$Reference_of_body->appendChild($Reference_of_body_Transforms);
$Reference_of_body_Transforms->appendChild($Reference_of_body_Transforms_Transform);
$Reference_of_body->appendChild($Reference_of_body_DigestMethod);
$Reference_of_body->appendChild($Reference_of_body_DigestValue);
$Signature->appendChild($SignatureValue);
$Signature->appendChild($KeyInfo);
$KeyInfo->appendChild($SecurityTokenReference);
$SecurityTokenReference->appendChild($SecurityTokenReference_Reference);
$request = $xml->saveXML();
$signer = new CPSigner();
$signer->set_Certificate($cert);
$signer->set_Options(2);
$sd = new CPSignedXml();
$sd->set_SignatureType(2);
$sd->set_Content($request);
$sd->set_DigestMethod('urn:ietf:params:xml:ns:cpxmlsec:algorithms:gostr34112012-512');
$sd->set_SignatureMethod('urn:ietf:params:xml:ns:cpxmlsec:algorithms:gostr34102012-gostr34112012-512');
$signedXml = $sd->Sign($signer, "//*[local-name()='Signature']");
Logger::debug('подписанный запрос от MySoapClient - ' . $signedXml);
return parent::__doRequest($signedXml, $location, $saction, $version);
}
}