diff --git a/src/XMLSecEnc.php b/src/XMLSecEnc.php index 39a1f3cb..36101928 100644 --- a/src/XMLSecEnc.php +++ b/src/XMLSecEnc.php @@ -70,16 +70,16 @@ class XMLSecEnc /** @var string */ public const XMLENCNS = 'http://www.w3.org/2001/04/xmlenc#'; - /** @var null|\DOMDocument */ + /** @var \DOMDocument|null */ private $encdoc = null; - /** @var null|\DOMNode */ + /** @var \DOMNode|null */ private $rawNode = null; - /** @var null|string */ + /** @var string|null */ public $type = null; - /** @var null|\DOMElement */ + /** @var \DOMElement|null */ public $encKey = null; /** @var array */ @@ -111,10 +111,6 @@ private function resetTemplate(): void */ public function addReference(string $name, DOMNode $node, string $type): void { - if (!($node instanceof DOMNode)) { - throw new Exception('$node is not of type DOMNode'); - } - $curencdoc = $this->encdoc; $this->resetTemplate(); $encdoc = $this->encdoc; @@ -143,24 +139,28 @@ public function setNode(DOMNode $node): void * @param bool $replace Whether the encrypted node should be replaced in the original tree. Default is true. * @throws \Exception * - * @return \DOMElement The -element. + * @return \DOMNode|false The -element. */ - public function encryptNode(XMLSecurityKey $objKey, bool $replace = true): DOMElement + public function encryptNode(XMLSecurityKey $objKey, bool $replace = true) { $data = ''; if (empty($this->rawNode)) { throw new Exception('Node to encrypt has not been set'); } + if (!($objKey instanceof XMLSecurityKey)) { throw new Exception('Invalid Key'); } + $doc = $this->rawNode->ownerDocument; $xPath = new DOMXPath($this->encdoc); $objList = $xPath->query('/xenc:EncryptedData/xenc:CipherData/xenc:CipherValue'); + $cipherValue = $objList->item(0); if ($cipherValue == null) { throw new Exception('Error locating CipherValue element within template'); } + switch ($this->type) { case (self::ELEMENT): $data = $doc->saveXML($this->rawNode); @@ -556,7 +556,7 @@ public static function staticLocateKeyInfo(XMLSecurityKey $objBaseKey = null, DO * @param \DOMNode|null $node * @return \RobRichards\XMLSecLibs\XMLSecurityKey|null */ - public function locateKeyInfo(XMLSecurityKey $objBaseKey = null, DOMNode $node = null): XMLSecurityKey + public function locateKeyInfo(XMLSecurityKey $objBaseKey = null, DOMNode $node = null): ?XMLSecurityKey { if (empty($node)) { $node = $this->rawNode; diff --git a/src/XMLSecurityDSig.php b/src/XMLSecurityDSig.php index 81f81284..4de3d431 100644 --- a/src/XMLSecurityDSig.php +++ b/src/XMLSecurityDSig.php @@ -305,7 +305,6 @@ private function canonicalizeData( if ( is_null($arXPath) - && ($node instanceof DOMNode) && ($node->ownerDocument !== null) && $node->isSameNode($node->ownerDocument->documentElement) ) { @@ -432,9 +431,9 @@ public function validateDigest(DOMNode $refNode, string $data): bool * @param \DOMNode $refNode * @param \DOMNode $objData * @param bool $includeCommentNodes - * @return string + * @return \DOMNode|string */ - public function processTransforms(DOMNode $refNode, DOMNode $objData, bool $includeCommentNodes = true): string + public function processTransforms(DOMNode $refNode, DOMNode $objData, bool $includeCommentNodes = true) { $data = $objData; $xpath = new DOMXPath($refNode->ownerDocument); @@ -822,7 +821,7 @@ public function addReferenceList( /** - * @param \DOMElement|string $data + * @param \DOMElement $data * @param string|null $mimetype * @param string|null $encoding * @return \DOMElement diff --git a/src/XMLSecurityKey.php b/src/XMLSecurityKey.php index 12db2d99..2ad9ca8b 100644 --- a/src/XMLSecurityKey.php +++ b/src/XMLSecurityKey.php @@ -842,9 +842,9 @@ public function serializeKey($parent): void * Will return the X509 certificate in PEM-format if this key represents * an X509 certificate. * - * @return string The X509 certificate or null if this key doesn't represent an X509-certificate. + * @return string|null The X509 certificate or null if this key doesn't represent an X509-certificate. */ - public function getX509Certificate(): string + public function getX509Certificate(): ?string { return $this->x509Certificate; } @@ -857,9 +857,9 @@ public function getX509Certificate(): string * The thumbprint as a lowercase 40-character hexadecimal number, or null * if this isn't a X509 certificate. * - * @return string Lowercase 40-character hexadecimal number of thumbprint + * @return string|null Lowercase 40-character hexadecimal number of thumbprint */ - public function getX509Thumbprint(): string + public function getX509Thumbprint(): ?string { return $this->X509Thumbprint; }