RobRichards\WsePhp\WSSESoap::decryptSoapDoc PHP Method

decryptSoapDoc() public method

public decryptSoapDoc ( $doc, $options )
    public function decryptSoapDoc($doc, $options)
    {
        $privKey = null;
        $privKey_isFile = false;
        $privKey_isCert = false;
        if (is_array($options)) {
            $privKey = !empty($options['keys']['private']['key']) ? $options['keys']['private']['key'] : null;
            $privKey_isFile = !empty($options['keys']['private']['isFile']) ? true : false;
            $privKey_isCert = !empty($options['keys']['private']['isCert']) ? true : false;
        }
        $objenc = new XMLSecEnc();
        $xpath = new DOMXPath($doc);
        $envns = $doc->documentElement->namespaceURI;
        $xpath->registerNamespace('soapns', $envns);
        $xpath->registerNamespace('soapenc', 'http://www.w3.org/2001/04/xmlenc#');
        $nodes = $xpath->query('/soapns:Envelope/soapns:Header/*[local-name()="Security"]/soapenc:EncryptedKey');
        $references = array();
        if ($node = $nodes->item(0)) {
            $objenc = new XMLSecEnc();
            $objenc->setNode($node);
            if (!($objKey = $objenc->locateKey())) {
                throw new Exception('Unable to locate algorithm for this Encrypted Key');
            }
            $objKey->isEncrypted = true;
            $objKey->encryptedCtx = $objenc;
            XMLSecEnc::staticLocateKeyInfo($objKey, $node);
            if ($objKey && $objKey->isEncrypted) {
                $objencKey = $objKey->encryptedCtx;
                $objKey->loadKey($privKey, $privKey_isFile, $privKey_isCert);
                $key = $objencKey->decryptKey($objKey);
                $objKey->loadKey($key);
            }
            $refnodes = $xpath->query('./soapenc:ReferenceList/soapenc:DataReference/@URI', $node);
            foreach ($refnodes as $reference) {
                $references[] = $reference->nodeValue;
            }
        }
        foreach ($references as $reference) {
            $arUrl = parse_url($reference);
            $reference = $arUrl['fragment'];
            $query = '//*[@Id="' . $reference . '"]';
            $nodes = $xpath->query($query);
            $encData = $nodes->item(0);
            if ($algo = $xpath->evaluate('string(./soapenc:EncryptionMethod/@Algorithm)', $encData)) {
                $objKey = new XMLSecurityKey($algo);
                $objKey->loadKey($key);
            }
            $objenc->setNode($encData);
            $objenc->type = $encData->getAttribute('Type');
            $decrypt = $objenc->decryptNode($objKey, true);
        }
        return true;
    }

Usage Example

 public function __doRequest($request, $location, $saction, $version)
 {
     $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 AES256_CBC and type is private key */
     $objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type' => 'private'));
     /* load the private key from file - last arg is bool if key in file (true) or is string (false) */
     $objKey->loadKey(PRIVATE_KEY, true);
     /* 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(CERT_FILE));
     /* Attach pointer to Signature */
     $objWSSE->attachTokentoSig($token);
     $objKey = new XMLSecurityKey(XMLSecurityKey::AES256_CBC);
     $objKey->generateSessionKey();
     $siteKey = new XMLSecurityKey(XMLSecurityKey::RSA_OAEP_MGF1P, array('type' => 'public'));
     $siteKey->loadKey(SERVICE_CERT, true, true);
     $options = array("KeyInfo" => array("X509SubjectKeyIdentifier" => true));
     $objWSSE->encryptSoapDoc($siteKey, $objKey, $options);
     $retVal = parent::__doRequest($objWSSE->saveXML(), $location, $saction, $version);
     $doc = new DOMDocument();
     $doc->loadXML($retVal);
     $options = array("keys" => array("private" => array("key" => PRIVATE_KEY, "isFile" => true, "isCert" => false)));
     $objWSSE->decryptSoapDoc($doc, $options);
     return $doc->saveXML();
 }