TYPO3 API  SVNRelease
class.tx_rsaauth_php_backend.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009-2011 Dmitry Dulepov <dmitry@typo3.org>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00025 /**
00026  * [CLASS/FUNCTION INDEX of SCRIPT]
00027  *
00028  * $Id: class.tx_rsaauth_php_backend.php 10120 2011-01-18 20:03:36Z ohader $
00029  */
00030 
00031 require_once(t3lib_extMgm::extPath('rsaauth', 'sv1/backends/class.tx_rsaauth_abstract_backend.php'));
00032 
00033 /**
00034  * This class contains a PHP OpenSSL backend for the TYPO3 RSA authentication
00035  * service. See class tx_rsaauth_abstract_backend for the information on using
00036  * backends.
00037  *
00038  * @author  Dmitry Dulepov <dmitry@typo3.org>
00039  * @package TYPO3
00040  * @subpackage  tx_rsaauth
00041  */
00042 class tx_rsaauth_php_backend extends tx_rsaauth_abstract_backend {
00043 
00044     /**
00045      * Creates a new public/private key pair using PHP OpenSSL extension.
00046      *
00047      * @return tx_rsaauth_keypair   A new key pair or null in case of error
00048      * @see tx_rsaauth_abstract_backend::createNewKeyPair()
00049      */
00050     public function createNewKeyPair() {
00051         $result = null;
00052         $privateKey = @openssl_pkey_new();
00053         if ($privateKey) {
00054             // Create private key as string
00055             $privateKeyStr = '';
00056             openssl_pkey_export($privateKey, $privateKeyStr);
00057 
00058             // Prepare public key information
00059             $exportedData = '';
00060             $csr = openssl_csr_new(array(), $privateKey);
00061             openssl_csr_export($csr, $exportedData, false);
00062 
00063             // Get public key (in fact modulus) and exponent
00064             $publicKey = $this->extractPublicKeyModulus($exportedData);
00065             $exponent = $this->extractExponent($exportedData);
00066 
00067             // Create result object
00068             $result = t3lib_div::makeInstance('tx_rsaauth_keypair');
00069             /* @var $result tx_rsaauth_keypair */
00070             $result->setExponent($exponent);
00071             $result->setPrivateKey($privateKeyStr);
00072             $result->setPublicKey($publicKey);
00073 
00074             // Clean up all resources
00075             openssl_free_key($privateKey);
00076         }
00077         return $result;
00078     }
00079 
00080     /**
00081      * Decrypts data using the private key. This implementation uses PHP OpenSSL
00082      * extension.
00083      *
00084      * @param string    $privateKey The private key (obtained from a call to createNewKeyPair())
00085      * @param string    $data   Data to decrypt (base64-encoded)
00086      * @return string   Decrypted data or null in case of a error
00087      * @see tx_rsaauth_abstract_backend::decrypt()
00088      */
00089     public function decrypt($privateKey, $data) {
00090         $result = '';
00091         if (!@openssl_private_decrypt(base64_decode($data), $result, $privateKey)) {
00092             $result = null;
00093         }
00094         return $result;
00095     }
00096 
00097     /**
00098      * Checks if this backend is available for calling. In particular checks if
00099      * PHP OpenSSl extension is installed and functional.
00100      *
00101      * @return void
00102      * @see tx_rsaauth_abstract_backend::isAvailable()
00103      */
00104     public function isAvailable() {
00105         $result = false;
00106         if (is_callable('openssl_pkey_new')) {
00107             if (TYPO3_OS !== 'WIN') {
00108                 // If the server does not run Windows, we can be sure than
00109                 // OpenSSL will work
00110                 $result = true;
00111             }
00112             else {
00113                 // On Windows PHP extension has to be configured properly. It
00114                 // can be installed and available but will not work unless
00115                 // configured. So we check if it works.
00116                 $testKey = @openssl_pkey_new();
00117                 if ($testKey) {
00118                     openssl_free_key($testKey);
00119                     $result = true;
00120                 }
00121             }
00122         }
00123         return $result;
00124     }
00125 
00126     /**
00127      * Extracts the exponent from the OpenSSL CSR
00128      *
00129      * @param   string  $data   The result of openssl_csr_export()
00130      * @return  int The exponent as a number
00131      */
00132     protected function extractExponent($data) {
00133         $index = strpos($data, 'Exponent: ');
00134         // We do not check for '$index === false' because the exponent is
00135         // always there!
00136         return intval(substr($data, $index + 10));
00137     }
00138 
00139     /**
00140      * Extracts public key modulus from the OpenSSL CSR.
00141      *
00142      * @param   string  $data   The result of openssl_csr_export()
00143      * @return  string  Modulus as uppercase hex string
00144      */
00145     protected function extractPublicKeyModulus($data) {
00146         $fragment = preg_replace('/.*Modulus.*?\n(.*)Exponent:.*/ms', '\1', $data);
00147         $fragment = preg_replace('/[\s\n\r:]/', '', $fragment);
00148         $result = trim(strtoupper(substr($fragment, 2)));
00149 
00150         return $result;
00151     }
00152 }
00153 
00154 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rsaauth/sv1/backends/class.tx_rsaauth_php_backend.php'])) {
00155     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rsaauth/sv1/backends/class.tx_rsaauth_php_backend.php']);
00156 }
00157 
00158 ?>