[TASK] Use autoloader in rsaauth system extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / sv1 / backends / class.tx_rsaauth_cmdline_backend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 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
26 /**
27 * This class contains a OpenSSL backend for the TYPO3 RSA authentication
28 * service. It uses shell version of OpenSSL to perform tasks. See class
29 * tx_rsaauth_abstract_backend for the information on using backends.
30 *
31 * @author Dmitry Dulepov <dmitry@typo3.org>
32 * @package TYPO3
33 * @subpackage tx_rsaauth
34 */
35 class tx_rsaauth_cmdline_backend extends tx_rsaauth_abstract_backend {
36
37 /**
38 * A path to the openssl binary or FALSE if the binary does not exist
39 *
40 * @var mixed
41 */
42 protected $opensslPath;
43
44 /**
45 * Temporary directory. It is best of it is outside of the web site root and
46 * not publically readable.
47 * For now we use typo3temp/.
48 *
49 * @var string
50 */
51 protected $temporaryDirectory;
52
53 /**
54 * Creates an instance of this class. It obtains a path to the OpenSSL
55 * binary.
56 *
57 * @return void
58 */
59 public function __construct() {
60 $this->opensslPath = t3lib_exec::getCommand('openssl');
61 $this->temporaryDirectory = PATH_site . 'typo3temp';
62
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 *
75 * @return tx_rsaauth_keypair A new key pair or NULL in case of error
76 * @see tx_rsaauth_abstract_backend::createNewKeyPair()
77 */
78 public function createNewKeyPair() {
79 $result = NULL;
80
81 // Create a temporary file. Security: tempnam() sets permissions to 0600
82 $privateKeyFile = tempnam($this->temporaryDirectory, uniqid());
83
84 // Generate the private key.
85 //
86 // PHP generates 1024 bit key files. We force command line version
87 // to do the same and use the F4 (0x10001) exponent. This is the most
88 // secure.
89 $command = $this->opensslPath . ' genrsa -out ' .
90 escapeshellarg($privateKeyFile) . ' 1024';
91 t3lib_utility_Command::exec($command);
92
93 // Test that we got a private key
94 $privateKey = file_get_contents($privateKeyFile);
95 if (FALSE !== strpos($privateKey, 'BEGIN RSA PRIVATE KEY')) {
96 // Ok, we got the private key. Get the modulus.
97 $command = $this->opensslPath . ' rsa -noout -modulus -in ' .
98 escapeshellarg($privateKeyFile);
99 $value = t3lib_utility_Command::exec($command);
100 if (substr($value, 0, 8) === 'Modulus=') {
101 $publicKey = substr($value, 8);
102
103 // Create a result object
104 $result = t3lib_div::makeInstance('tx_rsaauth_keypair');
105 /* @var $result tx_rsa_keypair */
106 $result->setExponent(0x10001);
107 $result->setPrivateKey($privateKey);
108 $result->setPublicKey($publicKey);
109 }
110 }
111
112 @unlink($privateKeyFile);
113
114 return $result;
115 }
116
117 /**
118 *
119 * @param string $privateKey The private key (obtained from a call to createNewKeyPair())
120 * @param string $data Data to decrypt (base64-encoded)
121 * @return string Decrypted data or NULL in case of a error
122 * @see tx_rsaauth_abstract_backend::decrypt()
123 */
124 public function decrypt($privateKey, $data) {
125 // Key must be put to the file
126 $privateKeyFile = tempnam($this->temporaryDirectory, uniqid());
127 file_put_contents($privateKeyFile, $privateKey);
128
129 $dataFile = tempnam($this->temporaryDirectory, uniqid());
130 file_put_contents($dataFile, base64_decode($data));
131
132 // Prepare the command
133 $command = $this->opensslPath . ' rsautl -inkey ' .
134 escapeshellarg($privateKeyFile) . ' -in ' .
135 escapeshellarg($dataFile) .
136 ' -decrypt';
137
138 // Execute the command and capture the result
139 $output = array();
140 t3lib_utility_Command::exec($command, $output);
141
142 // Remove the file
143 @unlink($privateKeyFile);
144 @unlink($dataFile);
145
146 return implode(LF, $output);
147 }
148
149 /**
150 * Checks if command line version of the OpenSSL is available and can be
151 * executed successfully.
152 *
153 * @return void
154 * @see tx_rsaauth_abstract_backend::isAvailable()
155 */
156 public function isAvailable() {
157 $result = FALSE;
158 if ($this->opensslPath) {
159 // If path exists, test that command runs and can produce output
160 $test = t3lib_utility_Command::exec($this->opensslPath . ' version');
161 $result = (substr($test, 0, 8) == 'OpenSSL ');
162 }
163 return $result;
164 }
165 }
166
167 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rsaauth/sv1/backends/class.tx_rsaauth_cmdline_backend.php'])) {
168 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rsaauth/sv1/backends/class.tx_rsaauth_cmdline_backend.php']);
169 }
170
171 ?>