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

Source for file RSAPrivateKey.php

Documentation is available at RSAPrivateKey.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.  * Class to manage private keys.
  29.  *
  30.  * @package phpPoA2
  31.  * @subpackage crypto
  32.  */
  33. class RSAPrivateKey {
  34.  
  35.     protected $pem;
  36.     protected $der;
  37.     protected $modulus;
  38.     protected $bits;
  39.     protected $public_exponent;
  40.     protected $private_exponent;
  41.     protected $prime1;
  42.     protected $prime2;
  43.     protected $exponent1;
  44.     protected $exponent2;
  45.     protected $coefficient;
  46.  
  47.     /**
  48.      *  Build a new private key from its PEM representation.
  49.      */
  50.     public function __construct($pem ''{
  51.         if (!empty($pem)) {
  52.             $this->fromPEM($pem);
  53.         }
  54.     }
  55.  
  56.     /**
  57.      * Build the private key from its DER representation.
  58.      */
  59.     public function fromDER($der{
  60.         $this->der = $der;
  61.  
  62.         // calculate pem
  63.         $this->pem = "-----BEGIN RSA PRIVATE KEY-----\n";
  64.         $this->pem .= wordwrap(base64_encode($der)64"\n"true)."\n";
  65.         $this->pem .= "-----END RSA PRIVATE KEY-----\n";
  66.  
  67.         // fill in the rest of the object
  68.         $this->decode();
  69.     }
  70.  
  71.     /**
  72.      * Build the private key from its PEM representation.
  73.      */
  74.     protected function fromPEM($pem{
  75.         $this->pem = $pem;
  76.  
  77.         // recover base64 encoded text from PEM
  78.         $lines explode("\n"$pem);
  79.         unset($lines[(count($lines-1)]);
  80.         unset($lines[0]);
  81.  
  82.         // decode text to get DER format
  83.         $this->der = base64_decode(implode($lines));
  84.  
  85.         // fill in the rest of the object
  86.         $this->decode();
  87.     }
  88.  
  89.     /**
  90.      * Extract private key details from its DER representation.
  91.      */
  92.     protected function decode({
  93.         $buffer $this->der;
  94.  
  95.         // decode root
  96.         $asn new ASN1();
  97.         $asn->decode($buffer);
  98.  
  99.         // get main sequence
  100.         $items $asn->getValues();
  101.  
  102.         // get the key values
  103.         $this->modulus = $items[1]->getValue();
  104.         $this->public_exponent = $items[2]->getInteger();
  105.         $this->private_exponent = $items[3]->getValue();
  106.         $this->prime1 = $items[4]->getValue();
  107.         $this->prime2 = $items[5]->getValue();
  108.         $this->exponent1 = $items[6]->getValue();
  109.         $this->exponent2 = $items[7]->getValue();
  110.         $this->coefficient = $items[8]->getValue();
  111.  
  112.         // compute bits
  113.         $this->bits = strlen($this->modulus8;
  114.     }
  115.  
  116.     /**
  117.      * Build a new private key represented by it DER and PEM formats
  118.      * from its modulus and public exponent.
  119.      */
  120.     public function encode({
  121.         // TODO
  122.     }
  123.  
  124.     /**
  125.      * Get the PEM representation of the key.
  126.      */
  127.     public function getPEM({
  128.         return $this->pem;
  129.     }
  130.  
  131.     /**
  132.      * Get the DER representation of the key.
  133.      */
  134.     public function getDER({
  135.         return $this->der;
  136.     }
  137.  
  138.     /**
  139.      * Get the modulus of the key.
  140.      */
  141.     public function getModulus({
  142.         return $this->modulus;
  143.     }
  144.  
  145.     /**
  146.      * Get the public exponent of the key.
  147.      */
  148.     public function getPublicExponent({
  149.         return $this->public_exponent;
  150.     }
  151.  
  152.     /**
  153.      * Get the private exponent of the key.
  154.      */
  155.     public function getPrivateExponent({
  156.         return $this->private_exponent;
  157.     }
  158.  
  159.     /**
  160.      * Get the length of the key in bits.
  161.      */
  162.     public function getBits({
  163.         return $this->bits;
  164.     }
  165.  
  166.     /**
  167.      * Get the prime 1 of the key.
  168.      */
  169.     public function getPrime1({
  170.         return $this->prime1;
  171.     }
  172.  
  173.     /**
  174.      * Get the prime 2 of the key.
  175.      */
  176.     public function getPrime2({
  177.         return $this->prime2;
  178.     }
  179.  
  180.     /**
  181.      * Get the exponent 1 of the key.
  182.      */
  183.     public function getExponent1({
  184.         return $this->exponent1;
  185.     }
  186.  
  187.     /**
  188.      * Get the exponent 2 of the key.
  189.      */
  190.     public function getExponent2({
  191.         return $this->exponent2;
  192.     }
  193.  
  194.     /**
  195.      * Get the coefficient of the key.
  196.      */
  197.     public function getCoefficient({
  198.         return $this->coefficient;
  199.     }
  200.  
  201.     /**
  202.      * Set the modulus of the key.
  203.      */
  204.     public function setModulus($modulus{
  205.         $this->modulus = $modulus;
  206.     }
  207.  
  208.     /**
  209.      * Set the public exponent of the key.
  210.      */
  211.     public function setPublicExponent($exponent{
  212.         $this->public_exponent = $exponent;
  213.     }
  214.  
  215.     /**
  216.      * Set the private exponent of the key.
  217.      */
  218.     public function setPrivateExponent($exponent{
  219.         $this->private_exponent = $exponent;
  220.     }
  221.  
  222.     /**
  223.      * Set the prime 1 of the key.
  224.      */
  225.     public function setPrime1($prime{
  226.         $this->prime1 = $prime;
  227.     }
  228.  
  229.     /**
  230.      * Set the prime 2 of the key.
  231.      */
  232.     public function setPrime2($prime{
  233.         $this->prime2 = $prime;
  234.     }
  235.  
  236.     /**
  237.      * Set the exponent 1 of the key.
  238.      */
  239.     public function setExponent1($exponent{
  240.         $this->exponent1 = $exponent;
  241.     }
  242.  
  243.     /**
  244.      * Set the exponent 2 of the key.
  245.      */
  246.     public function setExponent2($exponent{
  247.         $this->exponent2 = $exponent;
  248.     }
  249.  
  250.     /**
  251.      * Set the coefficient of the key.
  252.      */
  253.     public function setCoefficient($coefficient{
  254.         $this->coefficient = $coefficient;
  255.     }
  256.  
  257. }
  258.  
  259. ?>

Documentation generated on Tue, 14 Jun 2011 12:22:19 +0200 by phpDocumentor 1.4.3