EncryptedKey.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_InfoCard
  17. * @subpackage Zend_InfoCard_Xml
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_InfoCard_Xml_Element
  24. */
  25. require_once 'Zend/InfoCard/Xml/Element.php';
  26. /**
  27. * Zend_InfoCard_Xml_EncryptedKey
  28. */
  29. require_once 'Zend/InfoCard/Xml/EncryptedKey.php';
  30. /**
  31. * Zend_InfoCard_Xml_KeyInfo_Interface
  32. */
  33. require_once 'Zend/InfoCard/Xml/KeyInfo/Interface.php';
  34. /**
  35. * An object representing an Xml EncryptedKEy block
  36. *
  37. * @category Zend
  38. * @package Zend_InfoCard
  39. * @subpackage Zend_InfoCard_Xml
  40. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_InfoCard_Xml_EncryptedKey
  44. extends Zend_InfoCard_Xml_Element
  45. implements Zend_InfoCard_Xml_KeyInfo_Interface
  46. {
  47. /**
  48. * Return an instance of the object based on input XML Data
  49. *
  50. * @throws Zend_InfoCard_Xml_Exception
  51. * @param string $xmlData The EncryptedKey XML Block
  52. * @return Zend_InfoCard_Xml_EncryptedKey
  53. */
  54. static public function getInstance($xmlData)
  55. {
  56. if($xmlData instanceof Zend_InfoCard_Xml_Element) {
  57. $strXmlData = $xmlData->asXML();
  58. } else if (is_string($xmlData)) {
  59. $strXmlData = $xmlData;
  60. } else {
  61. throw new Zend_InfoCard_Xml_Exception("Invalid Data provided to create instance");
  62. }
  63. $sxe = simplexml_load_string($strXmlData);
  64. if($sxe->getName() != "EncryptedKey") {
  65. throw new Zend_InfoCard_Xml_Exception("Invalid XML Block provided for EncryptedKey");
  66. }
  67. return simplexml_load_string($strXmlData, "Zend_InfoCard_Xml_EncryptedKey");
  68. }
  69. /**
  70. * Returns the Encyption Method Algorithm URI of the block
  71. *
  72. * @throws Zend_InfoCard_Xml_Exception
  73. * @return string the Encryption method algorithm URI
  74. */
  75. public function getEncryptionMethod()
  76. {
  77. $this->registerXPathNamespace('e', 'http://www.w3.org/2001/04/xmlenc#');
  78. list($encryption_method) = $this->xpath("//e:EncryptionMethod");
  79. if(!($encryption_method instanceof Zend_InfoCard_Xml_Element)) {
  80. throw new Zend_InfoCard_Xml_Exception("Unable to find the e:EncryptionMethod KeyInfo encryption block");
  81. }
  82. $dom = self::convertToDOM($encryption_method);
  83. if(!$dom->hasAttribute('Algorithm')) {
  84. throw new Zend_InfoCard_Xml_Exception("Unable to determine the encryption algorithm in the Symmetric enc:EncryptionMethod XML block");
  85. }
  86. return $dom->getAttribute('Algorithm');
  87. }
  88. /**
  89. * Returns the Digest Method Algorithm URI used
  90. *
  91. * @throws Zend_InfoCard_Xml_Exception
  92. * @return string the Digest Method Algorithm URI
  93. */
  94. public function getDigestMethod()
  95. {
  96. $this->registerXPathNamespace('e', 'http://www.w3.org/2001/04/xmlenc#');
  97. list($encryption_method) = $this->xpath("//e:EncryptionMethod");
  98. if(!($encryption_method instanceof Zend_InfoCard_Xml_Element)) {
  99. throw new Zend_InfoCard_Xml_Exception("Unable to find the e:EncryptionMethod KeyInfo encryption block");
  100. }
  101. if(!($encryption_method->DigestMethod instanceof Zend_InfoCard_Xml_Element)) {
  102. throw new Zend_InfoCard_Xml_Exception("Unable to find the DigestMethod block");
  103. }
  104. $dom = self::convertToDOM($encryption_method->DigestMethod);
  105. if(!$dom->hasAttribute('Algorithm')) {
  106. throw new Zend_InfoCard_Xml_Exception("Unable to determine the digest algorithm for the symmetric Keyinfo");
  107. }
  108. return $dom->getAttribute('Algorithm');
  109. }
  110. /**
  111. * Returns the KeyInfo block object
  112. *
  113. * @throws Zend_InfoCard_Xml_Exception
  114. * @return Zend_InfoCard_Xml_KeyInfo_Abstract
  115. */
  116. public function getKeyInfo()
  117. {
  118. if(isset($this->KeyInfo)) {
  119. return Zend_InfoCard_Xml_KeyInfo::getInstance($this->KeyInfo);
  120. }
  121. throw new Zend_InfoCard_Xml_Exception("Unable to locate a KeyInfo block");
  122. }
  123. /**
  124. * Return the encrypted value of the block in base64 format
  125. *
  126. * @throws Zend_InfoCard_Xml_Exception
  127. * @return string The Value of the CipherValue block in base64 format
  128. */
  129. public function getCipherValue()
  130. {
  131. $this->registerXPathNamespace('e', 'http://www.w3.org/2001/04/xmlenc#');
  132. list($cipherdata) = $this->xpath("//e:CipherData");
  133. if(!($cipherdata instanceof Zend_InfoCard_Xml_Element)) {
  134. throw new Zend_InfoCard_Xml_Exception("Unable to find the e:CipherData block");
  135. }
  136. $cipherdata->registerXPathNameSpace('enc', 'http://www.w3.org/2001/04/xmlenc#');
  137. list($ciphervalue) = $cipherdata->xpath("//enc:CipherValue");
  138. if(!($ciphervalue instanceof Zend_InfoCard_Xml_Element)) {
  139. throw new Zend_InfoCard_Xml_Exception("Unable to fidn the enc:CipherValue block");
  140. }
  141. return (string)$ciphervalue;
  142. }
  143. }