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