PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/typo3/sysext/rsaauth/sv1/backends/class.tx_rsaauth_cmdline_backend.php

https://github.com/foxsoft/typo3v4core
PHP | 178 lines | 66 code | 23 blank | 89 comment | 9 complexity | a257b92b6077b00cc97a95e84afc1298 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2009-2010 Dmitry Dulepov <dmitry@typo3.org>
  6. * All rights reserved
  7. *
  8. * This script is part of the TYPO3 project. The TYPO3 project is
  9. * free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * The GNU General Public License can be found at
  15. * http://www.gnu.org/copyleft/gpl.html.
  16. *
  17. * This script is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * This copyright notice MUST APPEAR in all copies of the script!
  23. ***************************************************************/
  24. /**
  25. * [CLASS/FUNCTION INDEX of SCRIPT]
  26. *
  27. * $Id$
  28. */
  29. require_once(t3lib_extMgm::extPath('rsaauth', 'sv1/backends/class.tx_rsaauth_abstract_backend.php'));
  30. /**
  31. * This class contains a OpenSSL backend for the TYPO3 RSA authentication
  32. * service. It uses shell version of OpenSSL to perform tasks. See class
  33. * tx_rsaauth_abstract_backend for the information on using backends.
  34. *
  35. * @author Dmitry Dulepov <dmitry@typo3.org>
  36. * @package TYPO3
  37. * @subpackage tx_rsaauth
  38. */
  39. class tx_rsaauth_cmdline_backend extends tx_rsaauth_abstract_backend {
  40. /**
  41. * A path to the openssl binary or false if the binary does not exist
  42. *
  43. * @var mixed
  44. */
  45. protected $opensslPath;
  46. /**
  47. * Temporary directory. It is best of it is outside of the web site root and
  48. * not publically readable.
  49. * For now we use typo3temp/.
  50. *
  51. * @var string
  52. */
  53. protected $temporaryDirectory;
  54. /**
  55. * Creates an instance of this class. It obtains a path to the OpenSSL
  56. * binary.
  57. *
  58. * @return void
  59. */
  60. public function __construct() {
  61. $this->opensslPath = t3lib_exec::getCommand('openssl');
  62. $this->temporaryDirectory = PATH_site . 'typo3temp';
  63. // Get temporary directory from the configuration
  64. $extconf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['rsaauth']);
  65. if ($extconf['temporaryDirectory'] != '' &&
  66. $extconf['temporaryDirectory']{0} == '/' &&
  67. @is_dir($extconf['temporaryDirectory']) &&
  68. is_writable($extconf['temporaryDirectory'])) {
  69. $this->temporaryDirectory = $extconf['temporaryDirectory'];
  70. }
  71. }
  72. /**
  73. *
  74. * @return tx_rsaauth_keypair A new key pair or null in case of error
  75. * @see tx_rsaauth_abstract_backend::createNewKeyPair()
  76. */
  77. public function createNewKeyPair() {
  78. $result = null;
  79. // Create a temporary file. Security: tempnam() sets permissions to 0600
  80. $privateKeyFile = tempnam($this->temporaryDirectory, uniqid());
  81. // Generate the private key.
  82. //
  83. // PHP generates 1024 bit key files. We force command line version
  84. // to do the same and use the F4 (0x10001) exponent. This is the most
  85. // secure.
  86. $command = $this->opensslPath . ' genrsa -out ' .
  87. escapeshellarg($privateKeyFile) . ' 1024';
  88. exec($command);
  89. // Test that we got a private key
  90. $privateKey = file_get_contents($privateKeyFile);
  91. if (false !== strpos($privateKey, 'BEGIN RSA PRIVATE KEY')) {
  92. // Ok, we got the private key. Get the modulus.
  93. $command = $this->opensslPath . ' rsa -noout -modulus -in ' .
  94. escapeshellarg($privateKeyFile);
  95. $value = exec($command);
  96. if (substr($value, 0, 8) === 'Modulus=') {
  97. $publicKey = substr($value, 8);
  98. // Create a result object
  99. $result = t3lib_div::makeInstance('tx_rsaauth_keypair');
  100. /* @var $result tx_rsa_keypair */
  101. $result->setExponent(0x10001);
  102. $result->setPrivateKey($privateKey);
  103. $result->setPublicKey($publicKey);
  104. }
  105. }
  106. @unlink($privateKeyFile);
  107. return $result;
  108. }
  109. /**
  110. *
  111. * @param string $privateKey The private key (obtained from a call to createNewKeyPair())
  112. * @param string $data Data to decrypt (base64-encoded)
  113. * @return string Decrypted data or null in case of a error
  114. * @see tx_rsaauth_abstract_backend::decrypt()
  115. */
  116. public function decrypt($privateKey, $data) {
  117. // Key must be put to the file
  118. $privateKeyFile = tempnam($this->temporaryDirectory, uniqid());
  119. file_put_contents($privateKeyFile, $privateKey);
  120. $dataFile = tempnam($this->temporaryDirectory, uniqid());
  121. file_put_contents($dataFile, base64_decode($data));
  122. // Prepare the command
  123. $command = $this->opensslPath . ' rsautl -inkey ' .
  124. escapeshellarg($privateKeyFile) . ' -in ' .
  125. escapeshellarg($dataFile) .
  126. ' -decrypt';
  127. // Execute the command and capture the result
  128. $output = array();
  129. exec($command, $output);
  130. // Remove the file
  131. @unlink($privateKeyFile);
  132. @unlink($dataFile);
  133. return implode(LF, $output);
  134. }
  135. /**
  136. * Checks if command line version of the OpenSSL is available and can be
  137. * executed successfully.
  138. *
  139. * @return void
  140. * @see tx_rsaauth_abstract_backend::isAvailable()
  141. */
  142. public function isAvailable() {
  143. $result = false;
  144. if ($this->opensslPath) {
  145. // If path exists, test that command runs and can produce output
  146. $test = exec($this->opensslPath . ' version');
  147. $result = (substr($test, 0, 8) == 'OpenSSL ');
  148. }
  149. return $result;
  150. }
  151. }
  152. if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rsaauth/sv1/backends/class.tx_rsaauth_cmdline_backend.php']) {
  153. include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rsaauth/sv1/backends/class.tx_rsaauth_cmdline_backend.php']);
  154. }
  155. ?>