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