Skip to content

Commit

Permalink
return encrypted node from XMLSecEnc::encryptNode() when replace is s…
Browse files Browse the repository at this point in the history
…et to false. (Olav)

add test

git-svn-id: http://xmlseclibs.googlecode.com/svn/trunk@44 fc874575-5144-0410-81e8-bd400901c4fa
  • Loading branch information
cdatazone.org authored and Maks3w committed Dec 16, 2012
1 parent 9ccf053 commit 91ed949
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/xmlsec-encrypt-noreplace.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Encryption without modifying original data
--FILE--
<?php
require(dirname(__FILE__) . '/../xmlseclibs.php');

$dom = new DOMDocument();
$dom->load(dirname(__FILE__) . '/basic-doc.xml');

$origData = $dom->saveXML();

$objKey = new XMLSecurityKey(XMLSecurityKey::AES256_CBC);
$objKey->generateSessionKey();

$siteKey = new XMLSecurityKey(XMLSecurityKey::RSA_OAEP_MGF1P, array('type'=>'public'));
$siteKey->loadKey(dirname(__FILE__) . '/mycert.pem', TRUE, TRUE);

$enc = new XMLSecEnc();
$enc->setNode($dom->documentElement);
$enc->encryptKey($siteKey, $objKey);

$enc->type = XMLSecEnc::Element;
$encNode = $enc->encryptNode($objKey, FALSE);

$newData = $dom->saveXML();
if ($newData !== $origData) {
echo "Original data was modified.\n";
}

if ($encNode->namespaceURI !== XMLSecEnc::XMLENCNS || $encNode->localName !== 'EncryptedData') {
echo "Encrypted node wasn't a <xenc:EncryptedData>-element.\n";
}

?>
--EXPECTF--
9 changes: 9 additions & 0 deletions xmlseclibs.php
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,13 @@ public function setNode($node) {
$this->rawNode = $node;
}

/**
* Encrypt the selected node with the given key.
*
* @param XMLSecurityKey $objKey The encryption key and algorithm.
* @param bool $replace Whether the encrypted node should be replaced in the original tree. Default is TRUE.
* @return DOMElement The <xenc:EncryptedData>-element.
*/
public function encryptNode($objKey, $replace=TRUE) {
$data = '';
if (empty($this->rawNode)) {
Expand Down Expand Up @@ -1458,6 +1465,8 @@ public function encryptNode($objKey, $replace=TRUE) {
return $importEnc;
break;
}
} else {
return $this->encdoc->documentElement;
}
}

Expand Down

0 comments on commit 91ed949

Please sign in to comment.