web/lib/Zend/InfoCard/Xml/EncryptedKey.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: EncryptedKey.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * Zend_InfoCard_Xml_Element
       
    25  */
       
    26 require_once 'Zend/InfoCard/Xml/Element.php';
       
    27 
       
    28 /**
       
    29  * Zend_InfoCard_Xml_EncryptedKey
       
    30  */
       
    31 require_once 'Zend/InfoCard/Xml/EncryptedKey.php';
       
    32 
       
    33 /**
       
    34  * Zend_InfoCard_Xml_KeyInfo_Interface
       
    35  */
       
    36 require_once 'Zend/InfoCard/Xml/KeyInfo/Interface.php';
       
    37 
       
    38 /**
       
    39  * An object representing an Xml EncryptedKEy block
       
    40  *
       
    41  * @category   Zend
       
    42  * @package    Zend_InfoCard
       
    43  * @subpackage Zend_InfoCard_Xml
       
    44  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    45  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    46  */
       
    47 class Zend_InfoCard_Xml_EncryptedKey
       
    48     extends Zend_InfoCard_Xml_Element
       
    49     implements Zend_InfoCard_Xml_KeyInfo_Interface
       
    50 {
       
    51     /**
       
    52      * Return an instance of the object based on input XML Data
       
    53      *
       
    54      * @throws Zend_InfoCard_Xml_Exception
       
    55      * @param string $xmlData The EncryptedKey XML Block
       
    56      * @return Zend_InfoCard_Xml_EncryptedKey
       
    57      */
       
    58     static public function getInstance($xmlData)
       
    59     {
       
    60         if($xmlData instanceof Zend_InfoCard_Xml_Element) {
       
    61             $strXmlData = $xmlData->asXML();
       
    62         } else if (is_string($xmlData)) {
       
    63             $strXmlData = $xmlData;
       
    64         } else {
       
    65             throw new Zend_InfoCard_Xml_Exception("Invalid Data provided to create instance");
       
    66         }
       
    67 
       
    68         $sxe = simplexml_load_string($strXmlData);
       
    69 
       
    70         if($sxe->getName() != "EncryptedKey") {
       
    71             throw new Zend_InfoCard_Xml_Exception("Invalid XML Block provided for EncryptedKey");
       
    72         }
       
    73 
       
    74         return simplexml_load_string($strXmlData, "Zend_InfoCard_Xml_EncryptedKey");
       
    75     }
       
    76 
       
    77     /**
       
    78      * Returns the Encyption Method Algorithm URI of the block
       
    79      *
       
    80      * @throws Zend_InfoCard_Xml_Exception
       
    81      * @return string the Encryption method algorithm URI
       
    82      */
       
    83     public function getEncryptionMethod()
       
    84     {
       
    85 
       
    86         $this->registerXPathNamespace('e', 'http://www.w3.org/2001/04/xmlenc#');
       
    87         list($encryption_method) = $this->xpath("//e:EncryptionMethod");
       
    88 
       
    89         if(!($encryption_method instanceof Zend_InfoCard_Xml_Element)) {
       
    90             throw new Zend_InfoCard_Xml_Exception("Unable to find the e:EncryptionMethod KeyInfo encryption block");
       
    91         }
       
    92 
       
    93         $dom = self::convertToDOM($encryption_method);
       
    94 
       
    95         if(!$dom->hasAttribute('Algorithm')) {
       
    96             throw new Zend_InfoCard_Xml_Exception("Unable to determine the encryption algorithm in the Symmetric enc:EncryptionMethod XML block");
       
    97         }
       
    98 
       
    99         return $dom->getAttribute('Algorithm');
       
   100 
       
   101     }
       
   102 
       
   103     /**
       
   104      * Returns the Digest Method Algorithm URI used
       
   105      *
       
   106      * @throws Zend_InfoCard_Xml_Exception
       
   107      * @return string the Digest Method Algorithm URI
       
   108      */
       
   109     public function getDigestMethod()
       
   110     {
       
   111         $this->registerXPathNamespace('e', 'http://www.w3.org/2001/04/xmlenc#');
       
   112         list($encryption_method) = $this->xpath("//e:EncryptionMethod");
       
   113 
       
   114         if(!($encryption_method instanceof Zend_InfoCard_Xml_Element)) {
       
   115             throw new Zend_InfoCard_Xml_Exception("Unable to find the e:EncryptionMethod KeyInfo encryption block");
       
   116         }
       
   117 
       
   118         if(!($encryption_method->DigestMethod instanceof Zend_InfoCard_Xml_Element)) {
       
   119             throw new Zend_InfoCard_Xml_Exception("Unable to find the DigestMethod block");
       
   120         }
       
   121 
       
   122         $dom = self::convertToDOM($encryption_method->DigestMethod);
       
   123 
       
   124         if(!$dom->hasAttribute('Algorithm')) {
       
   125             throw new Zend_InfoCard_Xml_Exception("Unable to determine the digest algorithm for the symmetric Keyinfo");
       
   126         }
       
   127 
       
   128         return $dom->getAttribute('Algorithm');
       
   129 
       
   130     }
       
   131 
       
   132     /**
       
   133      * Returns the KeyInfo block object
       
   134      *
       
   135      * @throws Zend_InfoCard_Xml_Exception
       
   136      * @return Zend_InfoCard_Xml_KeyInfo_Abstract
       
   137      */
       
   138     public function getKeyInfo()
       
   139     {
       
   140 
       
   141         if(isset($this->KeyInfo)) {
       
   142             return Zend_InfoCard_Xml_KeyInfo::getInstance($this->KeyInfo);
       
   143         }
       
   144 
       
   145         throw new Zend_InfoCard_Xml_Exception("Unable to locate a KeyInfo block");
       
   146     }
       
   147 
       
   148     /**
       
   149      * Return the encrypted value of the block in base64 format
       
   150      *
       
   151      * @throws Zend_InfoCard_Xml_Exception
       
   152      * @return string The Value of the CipherValue block in base64 format
       
   153      */
       
   154     public function getCipherValue()
       
   155     {
       
   156 
       
   157         $this->registerXPathNamespace('e', 'http://www.w3.org/2001/04/xmlenc#');
       
   158 
       
   159         list($cipherdata) = $this->xpath("//e:CipherData");
       
   160 
       
   161         if(!($cipherdata instanceof Zend_InfoCard_Xml_Element)) {
       
   162             throw new Zend_InfoCard_Xml_Exception("Unable to find the e:CipherData block");
       
   163         }
       
   164 
       
   165         $cipherdata->registerXPathNameSpace('enc', 'http://www.w3.org/2001/04/xmlenc#');
       
   166         list($ciphervalue) = $cipherdata->xpath("//enc:CipherValue");
       
   167 
       
   168         if(!($ciphervalue instanceof Zend_InfoCard_Xml_Element)) {
       
   169             throw new Zend_InfoCard_Xml_Exception("Unable to fidn the enc:CipherValue block");
       
   170         }
       
   171 
       
   172         return (string)$ciphervalue;
       
   173     }
       
   174 }