phpPoA2
[ class tree: phpPoA2 ] [ index: phpPoA2 ] [ all elements ]

Source for file RSAPublicKey.php

Documentation is available at RSAPublicKey.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2005-2011 RedIRIS, http://www.rediris.es/
  4.  *
  5.  *  This file is part of phpPoA2.
  6.  *
  7.  *  phpPoA2 is free software: you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation, either version 3 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  phpPoA2 is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with phpPoA2. If not, see <http://www.gnu.org/licenses/>.
  19.  *
  20.  * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  21.  * @version 2.0
  22.  * @package phpPoA2
  23.  * @author Jaime Perez <jaime.perez@rediris.es>
  24.  * @filesource
  25.  */
  26.  
  27. /**
  28.  * The OID for public keys.
  29.  */
  30. define("PUBLIC_KEY_OID""1.2.840.113549.1.1.1");
  31.  
  32. /**
  33.  * Class to manage public keys.
  34.  *
  35.  * @package phpPoA2
  36.  * @subpackage crypto
  37.  */
  38. class RSAPublicKey {
  39.  
  40.     protected $pem;
  41.     protected $der;
  42.     protected $modulus;
  43.     protected $exponent;
  44.     protected $bits;
  45.  
  46.     /**
  47.      *  Build a new public key from its PEM representation.
  48.      */
  49.     public function __construct($pem ''{
  50.         if (!empty($pem)) {
  51.             $this->fromPEM($pem);
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Build the public key from its DER representation.
  57.      */
  58.     public function fromDER($der{
  59.         $this->der = $der;
  60.  
  61.         // calculate pem
  62.         $this->pem = "-----BEGIN PUBLIC KEY-----\n";
  63.         $this->pem .= wordwrap(base64_encode($der)64"\n"true)."\n";
  64.         $this->pem .= "-----END PUBLIC KEY-----\n";
  65.  
  66.         // fill in the rest of the object
  67.         $this->decode();
  68.     }
  69.  
  70.     /**
  71.      * Build the public key from its PEM representation.
  72.      */
  73.     protected function fromPEM($pem{
  74.         $this->pem = $pem;
  75.  
  76.         // recover base64 encoded text from PEM
  77.         $lines explode("\n"$pem);
  78.         unset($lines[(count($lines-1)]);
  79.         unset($lines[0]);
  80.  
  81.         // decode text to get DER format
  82.         $this->der = base64_decode(implode($lines));
  83.  
  84.         // fill in the rest of the object
  85.         $this->decode();
  86.     }
  87.  
  88.     /**
  89.      * Extract public key details from its DER representation.
  90.      */
  91.     protected function decode({
  92.         $buffer $this->der;
  93.  
  94.         // decode root
  95.         $asn new ASN1();
  96.         $asn->decode($buffer);
  97.  
  98.         // get main sequence
  99.         $items $asn->getValues();
  100.  
  101.         // get the key info
  102.         $keydata $items[1]->getValue();
  103.         $key new ASN1();
  104.         $key->decode($keydata);
  105.         $data $key->getValues();
  106.  
  107.         // get modulus
  108.         $this->modulus = $data[0]->getValue();
  109.  
  110.         // get public exponent
  111.         $this->exponent = $data[1]->getValue();
  112.  
  113.         // compute bits
  114.         $this->bits = strlen($this->modulus8;
  115.     }
  116.  
  117.     /**
  118.      * Build a new public key represented by it DER and PEM formats
  119.      * from its modulus and public exponent.
  120.      */
  121.     public function encode({
  122.         // TODO
  123.     }
  124.  
  125.     /**
  126.      * Get the PEM representation of the key.
  127.      */
  128.     public function getPEM({
  129.         return $this->pem;
  130.     }
  131.  
  132.     /**
  133.      * Get the DER representation of the key.
  134.      */
  135.     public function getDER({
  136.         return $this->der;
  137.     }
  138.  
  139.     /**
  140.      * Get the modulus of the key.
  141.      */
  142.     public function getModulus({
  143.         return $this->modulus;
  144.     }
  145.  
  146.     /**
  147.      * Get the exponent of the key.
  148.      */
  149.     public function getExponent({
  150.         return $this->exponent;
  151.     }
  152.  
  153.     /**
  154.      * Get the length of the key in bits.
  155.      */
  156.     public function getBits({
  157.         return $this->bits;
  158.     }
  159.  
  160.     /**
  161.      * Set the modulus of the key.
  162.      */
  163.     public function setModulus($modulus{
  164.         $this->modulus = $modulus;
  165.     }
  166.  
  167.     /**
  168.      * Set the public exponent of the key.
  169.      */
  170.     public function setPublicExponent($exponent{
  171.         $this->exponent = $exponent;
  172.     }
  173. }
  174.  
  175. ?>

Documentation generated on Mon, 20 Feb 2012 12:07:18 +0100 by phpDocumentor 1.4.3