@@ -50,213 +50,213 @@ |
||
50 | 50 | |
51 | 51 | class Application extends \OCP\AppFramework\App { |
52 | 52 | |
53 | - /** @var IManager */ |
|
54 | - private $encryptionManager; |
|
55 | - /** @var IConfig */ |
|
56 | - private $config; |
|
53 | + /** @var IManager */ |
|
54 | + private $encryptionManager; |
|
55 | + /** @var IConfig */ |
|
56 | + private $config; |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param array $urlParams |
|
60 | - */ |
|
61 | - public function __construct($urlParams = []) { |
|
62 | - parent::__construct('encryption', $urlParams); |
|
63 | - $this->encryptionManager = \OC::$server->getEncryptionManager(); |
|
64 | - $this->config = \OC::$server->getConfig(); |
|
65 | - $this->registerServices(); |
|
66 | - } |
|
58 | + /** |
|
59 | + * @param array $urlParams |
|
60 | + */ |
|
61 | + public function __construct($urlParams = []) { |
|
62 | + parent::__construct('encryption', $urlParams); |
|
63 | + $this->encryptionManager = \OC::$server->getEncryptionManager(); |
|
64 | + $this->config = \OC::$server->getConfig(); |
|
65 | + $this->registerServices(); |
|
66 | + } |
|
67 | 67 | |
68 | - public function setUp() { |
|
69 | - if ($this->encryptionManager->isEnabled()) { |
|
70 | - /** @var Setup $setup */ |
|
71 | - $setup = $this->getContainer()->query('UserSetup'); |
|
72 | - $setup->setupSystem(); |
|
73 | - } |
|
74 | - } |
|
68 | + public function setUp() { |
|
69 | + if ($this->encryptionManager->isEnabled()) { |
|
70 | + /** @var Setup $setup */ |
|
71 | + $setup = $this->getContainer()->query('UserSetup'); |
|
72 | + $setup->setupSystem(); |
|
73 | + } |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * register hooks |
|
78 | - */ |
|
79 | - public function registerHooks() { |
|
80 | - if (!$this->config->getSystemValueBool('maintenance')) { |
|
81 | - $container = $this->getContainer(); |
|
82 | - $server = $container->getServer(); |
|
83 | - // Register our hooks and fire them. |
|
84 | - $hookManager = new HookManager(); |
|
76 | + /** |
|
77 | + * register hooks |
|
78 | + */ |
|
79 | + public function registerHooks() { |
|
80 | + if (!$this->config->getSystemValueBool('maintenance')) { |
|
81 | + $container = $this->getContainer(); |
|
82 | + $server = $container->getServer(); |
|
83 | + // Register our hooks and fire them. |
|
84 | + $hookManager = new HookManager(); |
|
85 | 85 | |
86 | - $hookManager->registerHook([ |
|
87 | - new UserHooks($container->query('KeyManager'), |
|
88 | - $server->getUserManager(), |
|
89 | - $server->getLogger(), |
|
90 | - $container->query('UserSetup'), |
|
91 | - $server->getUserSession(), |
|
92 | - $container->query('Util'), |
|
93 | - $container->query('Session'), |
|
94 | - $container->query('Crypt'), |
|
95 | - $container->query('Recovery')) |
|
96 | - ]); |
|
86 | + $hookManager->registerHook([ |
|
87 | + new UserHooks($container->query('KeyManager'), |
|
88 | + $server->getUserManager(), |
|
89 | + $server->getLogger(), |
|
90 | + $container->query('UserSetup'), |
|
91 | + $server->getUserSession(), |
|
92 | + $container->query('Util'), |
|
93 | + $container->query('Session'), |
|
94 | + $container->query('Crypt'), |
|
95 | + $container->query('Recovery')) |
|
96 | + ]); |
|
97 | 97 | |
98 | - $hookManager->fireHooks(); |
|
99 | - } else { |
|
100 | - // Logout user if we are in maintenance to force re-login |
|
101 | - $this->getContainer()->getServer()->getUserSession()->logout(); |
|
102 | - } |
|
103 | - } |
|
98 | + $hookManager->fireHooks(); |
|
99 | + } else { |
|
100 | + // Logout user if we are in maintenance to force re-login |
|
101 | + $this->getContainer()->getServer()->getUserSession()->logout(); |
|
102 | + } |
|
103 | + } |
|
104 | 104 | |
105 | - public function registerEncryptionModule() { |
|
106 | - $container = $this->getContainer(); |
|
105 | + public function registerEncryptionModule() { |
|
106 | + $container = $this->getContainer(); |
|
107 | 107 | |
108 | 108 | |
109 | - $this->encryptionManager->registerEncryptionModule( |
|
110 | - Encryption::ID, |
|
111 | - Encryption::DISPLAY_NAME, |
|
112 | - function () use ($container) { |
|
113 | - return new Encryption( |
|
114 | - $container->query('Crypt'), |
|
115 | - $container->query('KeyManager'), |
|
116 | - $container->query('Util'), |
|
117 | - $container->query('Session'), |
|
118 | - $container->query('EncryptAll'), |
|
119 | - $container->query('DecryptAll'), |
|
120 | - $container->getServer()->getLogger(), |
|
121 | - $container->getServer()->getL10N($container->getAppName()) |
|
122 | - ); |
|
123 | - }); |
|
124 | - } |
|
109 | + $this->encryptionManager->registerEncryptionModule( |
|
110 | + Encryption::ID, |
|
111 | + Encryption::DISPLAY_NAME, |
|
112 | + function () use ($container) { |
|
113 | + return new Encryption( |
|
114 | + $container->query('Crypt'), |
|
115 | + $container->query('KeyManager'), |
|
116 | + $container->query('Util'), |
|
117 | + $container->query('Session'), |
|
118 | + $container->query('EncryptAll'), |
|
119 | + $container->query('DecryptAll'), |
|
120 | + $container->getServer()->getLogger(), |
|
121 | + $container->getServer()->getL10N($container->getAppName()) |
|
122 | + ); |
|
123 | + }); |
|
124 | + } |
|
125 | 125 | |
126 | - public function registerServices() { |
|
127 | - $container = $this->getContainer(); |
|
126 | + public function registerServices() { |
|
127 | + $container = $this->getContainer(); |
|
128 | 128 | |
129 | - $container->registerService('Crypt', |
|
130 | - function (IAppContainer $c) { |
|
131 | - $server = $c->getServer(); |
|
132 | - return new Crypt($server->getLogger(), |
|
133 | - $server->getUserSession(), |
|
134 | - $server->getConfig(), |
|
135 | - $server->getL10N($c->getAppName())); |
|
136 | - }); |
|
129 | + $container->registerService('Crypt', |
|
130 | + function (IAppContainer $c) { |
|
131 | + $server = $c->getServer(); |
|
132 | + return new Crypt($server->getLogger(), |
|
133 | + $server->getUserSession(), |
|
134 | + $server->getConfig(), |
|
135 | + $server->getL10N($c->getAppName())); |
|
136 | + }); |
|
137 | 137 | |
138 | - $container->registerService('Session', |
|
139 | - function (IAppContainer $c) { |
|
140 | - $server = $c->getServer(); |
|
141 | - return new Session($server->getSession()); |
|
142 | - } |
|
143 | - ); |
|
138 | + $container->registerService('Session', |
|
139 | + function (IAppContainer $c) { |
|
140 | + $server = $c->getServer(); |
|
141 | + return new Session($server->getSession()); |
|
142 | + } |
|
143 | + ); |
|
144 | 144 | |
145 | - $container->registerService('KeyManager', |
|
146 | - function (IAppContainer $c) { |
|
147 | - $server = $c->getServer(); |
|
145 | + $container->registerService('KeyManager', |
|
146 | + function (IAppContainer $c) { |
|
147 | + $server = $c->getServer(); |
|
148 | 148 | |
149 | - return new KeyManager($server->getEncryptionKeyStorage(), |
|
150 | - $c->query('Crypt'), |
|
151 | - $server->getConfig(), |
|
152 | - $server->getUserSession(), |
|
153 | - new Session($server->getSession()), |
|
154 | - $server->getLogger(), |
|
155 | - $c->query('Util'), |
|
156 | - $server->getLockingProvider() |
|
157 | - ); |
|
158 | - }); |
|
149 | + return new KeyManager($server->getEncryptionKeyStorage(), |
|
150 | + $c->query('Crypt'), |
|
151 | + $server->getConfig(), |
|
152 | + $server->getUserSession(), |
|
153 | + new Session($server->getSession()), |
|
154 | + $server->getLogger(), |
|
155 | + $c->query('Util'), |
|
156 | + $server->getLockingProvider() |
|
157 | + ); |
|
158 | + }); |
|
159 | 159 | |
160 | - $container->registerService('Recovery', |
|
161 | - function (IAppContainer $c) { |
|
162 | - $server = $c->getServer(); |
|
160 | + $container->registerService('Recovery', |
|
161 | + function (IAppContainer $c) { |
|
162 | + $server = $c->getServer(); |
|
163 | 163 | |
164 | - return new Recovery( |
|
165 | - $server->getUserSession(), |
|
166 | - $c->query('Crypt'), |
|
167 | - $c->query('KeyManager'), |
|
168 | - $server->getConfig(), |
|
169 | - $server->getEncryptionFilesHelper(), |
|
170 | - new View()); |
|
171 | - }); |
|
164 | + return new Recovery( |
|
165 | + $server->getUserSession(), |
|
166 | + $c->query('Crypt'), |
|
167 | + $c->query('KeyManager'), |
|
168 | + $server->getConfig(), |
|
169 | + $server->getEncryptionFilesHelper(), |
|
170 | + new View()); |
|
171 | + }); |
|
172 | 172 | |
173 | - $container->registerService('RecoveryController', function (IAppContainer $c) { |
|
174 | - $server = $c->getServer(); |
|
175 | - return new RecoveryController( |
|
176 | - $c->getAppName(), |
|
177 | - $server->getRequest(), |
|
178 | - $server->getConfig(), |
|
179 | - $server->getL10N($c->getAppName()), |
|
180 | - $c->query('Recovery')); |
|
181 | - }); |
|
173 | + $container->registerService('RecoveryController', function (IAppContainer $c) { |
|
174 | + $server = $c->getServer(); |
|
175 | + return new RecoveryController( |
|
176 | + $c->getAppName(), |
|
177 | + $server->getRequest(), |
|
178 | + $server->getConfig(), |
|
179 | + $server->getL10N($c->getAppName()), |
|
180 | + $c->query('Recovery')); |
|
181 | + }); |
|
182 | 182 | |
183 | - $container->registerService('StatusController', function (IAppContainer $c) { |
|
184 | - $server = $c->getServer(); |
|
185 | - return new StatusController( |
|
186 | - $c->getAppName(), |
|
187 | - $server->getRequest(), |
|
188 | - $server->getL10N($c->getAppName()), |
|
189 | - $c->query('Session'), |
|
190 | - $server->getEncryptionManager() |
|
191 | - ); |
|
192 | - }); |
|
183 | + $container->registerService('StatusController', function (IAppContainer $c) { |
|
184 | + $server = $c->getServer(); |
|
185 | + return new StatusController( |
|
186 | + $c->getAppName(), |
|
187 | + $server->getRequest(), |
|
188 | + $server->getL10N($c->getAppName()), |
|
189 | + $c->query('Session'), |
|
190 | + $server->getEncryptionManager() |
|
191 | + ); |
|
192 | + }); |
|
193 | 193 | |
194 | - $container->registerService('SettingsController', function (IAppContainer $c) { |
|
195 | - $server = $c->getServer(); |
|
196 | - return new SettingsController( |
|
197 | - $c->getAppName(), |
|
198 | - $server->getRequest(), |
|
199 | - $server->getL10N($c->getAppName()), |
|
200 | - $server->getUserManager(), |
|
201 | - $server->getUserSession(), |
|
202 | - $c->query('KeyManager'), |
|
203 | - $c->query('Crypt'), |
|
204 | - $c->query('Session'), |
|
205 | - $server->getSession(), |
|
206 | - $c->query('Util') |
|
207 | - ); |
|
208 | - }); |
|
194 | + $container->registerService('SettingsController', function (IAppContainer $c) { |
|
195 | + $server = $c->getServer(); |
|
196 | + return new SettingsController( |
|
197 | + $c->getAppName(), |
|
198 | + $server->getRequest(), |
|
199 | + $server->getL10N($c->getAppName()), |
|
200 | + $server->getUserManager(), |
|
201 | + $server->getUserSession(), |
|
202 | + $c->query('KeyManager'), |
|
203 | + $c->query('Crypt'), |
|
204 | + $c->query('Session'), |
|
205 | + $server->getSession(), |
|
206 | + $c->query('Util') |
|
207 | + ); |
|
208 | + }); |
|
209 | 209 | |
210 | - $container->registerService('UserSetup', |
|
211 | - function (IAppContainer $c) { |
|
212 | - $server = $c->getServer(); |
|
213 | - return new Setup($server->getLogger(), |
|
214 | - $server->getUserSession(), |
|
215 | - $c->query('Crypt'), |
|
216 | - $c->query('KeyManager')); |
|
217 | - }); |
|
210 | + $container->registerService('UserSetup', |
|
211 | + function (IAppContainer $c) { |
|
212 | + $server = $c->getServer(); |
|
213 | + return new Setup($server->getLogger(), |
|
214 | + $server->getUserSession(), |
|
215 | + $c->query('Crypt'), |
|
216 | + $c->query('KeyManager')); |
|
217 | + }); |
|
218 | 218 | |
219 | - $container->registerService('Util', |
|
220 | - function (IAppContainer $c) { |
|
221 | - $server = $c->getServer(); |
|
219 | + $container->registerService('Util', |
|
220 | + function (IAppContainer $c) { |
|
221 | + $server = $c->getServer(); |
|
222 | 222 | |
223 | - return new Util( |
|
224 | - new View(), |
|
225 | - $c->query('Crypt'), |
|
226 | - $server->getLogger(), |
|
227 | - $server->getUserSession(), |
|
228 | - $server->getConfig(), |
|
229 | - $server->getUserManager()); |
|
230 | - }); |
|
223 | + return new Util( |
|
224 | + new View(), |
|
225 | + $c->query('Crypt'), |
|
226 | + $server->getLogger(), |
|
227 | + $server->getUserSession(), |
|
228 | + $server->getConfig(), |
|
229 | + $server->getUserManager()); |
|
230 | + }); |
|
231 | 231 | |
232 | - $container->registerService('EncryptAll', |
|
233 | - function (IAppContainer $c) { |
|
234 | - $server = $c->getServer(); |
|
235 | - return new EncryptAll( |
|
236 | - $c->query('UserSetup'), |
|
237 | - $c->getServer()->getUserManager(), |
|
238 | - new View(), |
|
239 | - $c->query('KeyManager'), |
|
240 | - $c->query('Util'), |
|
241 | - $server->getConfig(), |
|
242 | - $server->getMailer(), |
|
243 | - $server->getL10N('encryption'), |
|
244 | - new QuestionHelper(), |
|
245 | - $server->getSecureRandom() |
|
246 | - ); |
|
247 | - } |
|
248 | - ); |
|
232 | + $container->registerService('EncryptAll', |
|
233 | + function (IAppContainer $c) { |
|
234 | + $server = $c->getServer(); |
|
235 | + return new EncryptAll( |
|
236 | + $c->query('UserSetup'), |
|
237 | + $c->getServer()->getUserManager(), |
|
238 | + new View(), |
|
239 | + $c->query('KeyManager'), |
|
240 | + $c->query('Util'), |
|
241 | + $server->getConfig(), |
|
242 | + $server->getMailer(), |
|
243 | + $server->getL10N('encryption'), |
|
244 | + new QuestionHelper(), |
|
245 | + $server->getSecureRandom() |
|
246 | + ); |
|
247 | + } |
|
248 | + ); |
|
249 | 249 | |
250 | - $container->registerService('DecryptAll', |
|
251 | - function (IAppContainer $c) { |
|
252 | - return new DecryptAll( |
|
253 | - $c->query('Util'), |
|
254 | - $c->query('KeyManager'), |
|
255 | - $c->query('Crypt'), |
|
256 | - $c->query('Session'), |
|
257 | - new QuestionHelper() |
|
258 | - ); |
|
259 | - } |
|
260 | - ); |
|
261 | - } |
|
250 | + $container->registerService('DecryptAll', |
|
251 | + function (IAppContainer $c) { |
|
252 | + return new DecryptAll( |
|
253 | + $c->query('Util'), |
|
254 | + $c->query('KeyManager'), |
|
255 | + $c->query('Crypt'), |
|
256 | + $c->query('Session'), |
|
257 | + new QuestionHelper() |
|
258 | + ); |
|
259 | + } |
|
260 | + ); |
|
261 | + } |
|
262 | 262 | } |
@@ -32,58 +32,58 @@ |
||
32 | 32 | use OCP\IUserSession; |
33 | 33 | |
34 | 34 | class Setup { |
35 | - /** |
|
36 | - * @var Crypt |
|
37 | - */ |
|
38 | - private $crypt; |
|
39 | - /** |
|
40 | - * @var KeyManager |
|
41 | - */ |
|
42 | - private $keyManager; |
|
43 | - /** |
|
44 | - * @var ILogger |
|
45 | - */ |
|
46 | - private $logger; |
|
47 | - /** |
|
48 | - * @var bool|string |
|
49 | - */ |
|
50 | - private $user; |
|
35 | + /** |
|
36 | + * @var Crypt |
|
37 | + */ |
|
38 | + private $crypt; |
|
39 | + /** |
|
40 | + * @var KeyManager |
|
41 | + */ |
|
42 | + private $keyManager; |
|
43 | + /** |
|
44 | + * @var ILogger |
|
45 | + */ |
|
46 | + private $logger; |
|
47 | + /** |
|
48 | + * @var bool|string |
|
49 | + */ |
|
50 | + private $user; |
|
51 | 51 | |
52 | 52 | |
53 | - /** |
|
54 | - * @param ILogger $logger |
|
55 | - * @param IUserSession $userSession |
|
56 | - * @param Crypt $crypt |
|
57 | - * @param KeyManager $keyManager |
|
58 | - */ |
|
59 | - public function __construct(ILogger $logger, |
|
60 | - IUserSession $userSession, |
|
61 | - Crypt $crypt, |
|
62 | - KeyManager $keyManager) { |
|
63 | - $this->logger = $logger; |
|
64 | - $this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false; |
|
65 | - $this->crypt = $crypt; |
|
66 | - $this->keyManager = $keyManager; |
|
67 | - } |
|
53 | + /** |
|
54 | + * @param ILogger $logger |
|
55 | + * @param IUserSession $userSession |
|
56 | + * @param Crypt $crypt |
|
57 | + * @param KeyManager $keyManager |
|
58 | + */ |
|
59 | + public function __construct(ILogger $logger, |
|
60 | + IUserSession $userSession, |
|
61 | + Crypt $crypt, |
|
62 | + KeyManager $keyManager) { |
|
63 | + $this->logger = $logger; |
|
64 | + $this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false; |
|
65 | + $this->crypt = $crypt; |
|
66 | + $this->keyManager = $keyManager; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @param string $uid user id |
|
71 | - * @param string $password user password |
|
72 | - * @return bool |
|
73 | - */ |
|
74 | - public function setupUser($uid, $password) { |
|
75 | - if (!$this->keyManager->userHasKeys($uid)) { |
|
76 | - $keyPair = $this->crypt->createKeyPair(); |
|
77 | - return is_array($keyPair) ? $this->keyManager->storeKeyPair($uid, $password, $keyPair) : false; |
|
78 | - } |
|
79 | - return true; |
|
80 | - } |
|
69 | + /** |
|
70 | + * @param string $uid user id |
|
71 | + * @param string $password user password |
|
72 | + * @return bool |
|
73 | + */ |
|
74 | + public function setupUser($uid, $password) { |
|
75 | + if (!$this->keyManager->userHasKeys($uid)) { |
|
76 | + $keyPair = $this->crypt->createKeyPair(); |
|
77 | + return is_array($keyPair) ? $this->keyManager->storeKeyPair($uid, $password, $keyPair) : false; |
|
78 | + } |
|
79 | + return true; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * make sure that all system keys exists |
|
84 | - */ |
|
85 | - public function setupSystem() { |
|
86 | - $this->keyManager->validateShareKey(); |
|
87 | - $this->keyManager->validateMasterKey(); |
|
88 | - } |
|
82 | + /** |
|
83 | + * make sure that all system keys exists |
|
84 | + */ |
|
85 | + public function setupSystem() { |
|
86 | + $this->keyManager->validateShareKey(); |
|
87 | + $this->keyManager->validateMasterKey(); |
|
88 | + } |
|
89 | 89 | } |
@@ -45,721 +45,721 @@ |
||
45 | 45 | |
46 | 46 | class KeyManager { |
47 | 47 | |
48 | - /** |
|
49 | - * @var Session |
|
50 | - */ |
|
51 | - protected $session; |
|
52 | - /** |
|
53 | - * @var IStorage |
|
54 | - */ |
|
55 | - private $keyStorage; |
|
56 | - /** |
|
57 | - * @var Crypt |
|
58 | - */ |
|
59 | - private $crypt; |
|
60 | - /** |
|
61 | - * @var string |
|
62 | - */ |
|
63 | - private $recoveryKeyId; |
|
64 | - /** |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - private $publicShareKeyId; |
|
68 | - /** |
|
69 | - * @var string |
|
70 | - */ |
|
71 | - private $masterKeyId; |
|
72 | - /** |
|
73 | - * @var string UserID |
|
74 | - */ |
|
75 | - private $keyId; |
|
76 | - /** |
|
77 | - * @var string |
|
78 | - */ |
|
79 | - private $publicKeyId = 'publicKey'; |
|
80 | - /** |
|
81 | - * @var string |
|
82 | - */ |
|
83 | - private $privateKeyId = 'privateKey'; |
|
84 | - |
|
85 | - /** |
|
86 | - * @var string |
|
87 | - */ |
|
88 | - private $shareKeyId = 'shareKey'; |
|
89 | - |
|
90 | - /** |
|
91 | - * @var string |
|
92 | - */ |
|
93 | - private $fileKeyId = 'fileKey'; |
|
94 | - /** |
|
95 | - * @var IConfig |
|
96 | - */ |
|
97 | - private $config; |
|
98 | - /** |
|
99 | - * @var ILogger |
|
100 | - */ |
|
101 | - private $log; |
|
102 | - /** |
|
103 | - * @var Util |
|
104 | - */ |
|
105 | - private $util; |
|
106 | - |
|
107 | - /** |
|
108 | - * @var ILockingProvider |
|
109 | - */ |
|
110 | - private $lockingProvider; |
|
111 | - |
|
112 | - /** |
|
113 | - * @param IStorage $keyStorage |
|
114 | - * @param Crypt $crypt |
|
115 | - * @param IConfig $config |
|
116 | - * @param IUserSession $userSession |
|
117 | - * @param Session $session |
|
118 | - * @param ILogger $log |
|
119 | - * @param Util $util |
|
120 | - */ |
|
121 | - public function __construct( |
|
122 | - IStorage $keyStorage, |
|
123 | - Crypt $crypt, |
|
124 | - IConfig $config, |
|
125 | - IUserSession $userSession, |
|
126 | - Session $session, |
|
127 | - ILogger $log, |
|
128 | - Util $util, |
|
129 | - ILockingProvider $lockingProvider |
|
130 | - ) { |
|
131 | - $this->util = $util; |
|
132 | - $this->session = $session; |
|
133 | - $this->keyStorage = $keyStorage; |
|
134 | - $this->crypt = $crypt; |
|
135 | - $this->config = $config; |
|
136 | - $this->log = $log; |
|
137 | - $this->lockingProvider = $lockingProvider; |
|
138 | - |
|
139 | - $this->recoveryKeyId = $this->config->getAppValue('encryption', |
|
140 | - 'recoveryKeyId'); |
|
141 | - if (empty($this->recoveryKeyId)) { |
|
142 | - $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8); |
|
143 | - $this->config->setAppValue('encryption', |
|
144 | - 'recoveryKeyId', |
|
145 | - $this->recoveryKeyId); |
|
146 | - } |
|
147 | - |
|
148 | - $this->publicShareKeyId = $this->config->getAppValue('encryption', |
|
149 | - 'publicShareKeyId'); |
|
150 | - if (empty($this->publicShareKeyId)) { |
|
151 | - $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8); |
|
152 | - $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId); |
|
153 | - } |
|
154 | - |
|
155 | - $this->masterKeyId = $this->config->getAppValue('encryption', |
|
156 | - 'masterKeyId'); |
|
157 | - if (empty($this->masterKeyId)) { |
|
158 | - $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8); |
|
159 | - $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId); |
|
160 | - } |
|
161 | - |
|
162 | - $this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false; |
|
163 | - $this->log = $log; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * check if key pair for public link shares exists, if not we create one |
|
168 | - */ |
|
169 | - public function validateShareKey() { |
|
170 | - $shareKey = $this->getPublicShareKey(); |
|
171 | - if (empty($shareKey)) { |
|
172 | - $this->lockingProvider->acquireLock('encryption-generateSharedKey', ILockingProvider::LOCK_EXCLUSIVE, 'Encryption: shared key generation'); |
|
173 | - try { |
|
174 | - $keyPair = $this->crypt->createKeyPair(); |
|
175 | - |
|
176 | - // Save public key |
|
177 | - $this->keyStorage->setSystemUserKey( |
|
178 | - $this->publicShareKeyId . '.' . $this->publicKeyId, $keyPair['publicKey'], |
|
179 | - Encryption::ID); |
|
180 | - |
|
181 | - // Encrypt private key empty passphrase |
|
182 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], ''); |
|
183 | - $header = $this->crypt->generateHeader(); |
|
184 | - $this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey); |
|
185 | - } catch (\Throwable $e) { |
|
186 | - $this->lockingProvider->releaseLock('encryption-generateSharedKey', ILockingProvider::LOCK_EXCLUSIVE); |
|
187 | - throw $e; |
|
188 | - } |
|
189 | - $this->lockingProvider->releaseLock('encryption-generateSharedKey', ILockingProvider::LOCK_EXCLUSIVE); |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * check if a key pair for the master key exists, if not we create one |
|
195 | - */ |
|
196 | - public function validateMasterKey() { |
|
197 | - if ($this->util->isMasterKeyEnabled() === false) { |
|
198 | - return; |
|
199 | - } |
|
200 | - |
|
201 | - $publicMasterKey = $this->getPublicMasterKey(); |
|
202 | - $privateMasterKey = $this->getPrivateMasterKey(); |
|
203 | - |
|
204 | - if (empty($publicMasterKey) && empty($privateMasterKey)) { |
|
205 | - // There could be a race condition here if two requests would trigger |
|
206 | - // the generation the second one would enter the key generation as long |
|
207 | - // as the first one didn't write the key to the keystorage yet |
|
208 | - $this->lockingProvider->acquireLock('encryption-generateMasterKey', ILockingProvider::LOCK_EXCLUSIVE, 'Encryption: master key generation'); |
|
209 | - try { |
|
210 | - $keyPair = $this->crypt->createKeyPair(); |
|
211 | - |
|
212 | - // Save public key |
|
213 | - $this->keyStorage->setSystemUserKey( |
|
214 | - $this->masterKeyId . '.' . $this->publicKeyId, $keyPair['publicKey'], |
|
215 | - Encryption::ID); |
|
216 | - |
|
217 | - // Encrypt private key with system password |
|
218 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId); |
|
219 | - $header = $this->crypt->generateHeader(); |
|
220 | - $this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey); |
|
221 | - } catch (\Throwable $e) { |
|
222 | - $this->lockingProvider->releaseLock('encryption-generateMasterKey', ILockingProvider::LOCK_EXCLUSIVE); |
|
223 | - throw $e; |
|
224 | - } |
|
225 | - $this->lockingProvider->releaseLock('encryption-generateMasterKey', ILockingProvider::LOCK_EXCLUSIVE); |
|
226 | - } elseif (empty($publicMasterKey)) { |
|
227 | - $this->log->error('A private master key is available but the public key could not be found. This should never happen.'); |
|
228 | - return; |
|
229 | - } elseif (empty($privateMasterKey)) { |
|
230 | - $this->log->error('A public master key is available but the private key could not be found. This should never happen.'); |
|
231 | - return; |
|
232 | - } |
|
233 | - |
|
234 | - if (!$this->session->isPrivateKeySet()) { |
|
235 | - $masterKey = $this->getSystemPrivateKey($this->masterKeyId); |
|
236 | - $decryptedMasterKey = $this->crypt->decryptPrivateKey($masterKey, $this->getMasterKeyPassword(), $this->masterKeyId); |
|
237 | - $this->session->setPrivateKey($decryptedMasterKey); |
|
238 | - } |
|
239 | - |
|
240 | - // after the encryption key is available we are ready to go |
|
241 | - $this->session->setStatus(Session::INIT_SUCCESSFUL); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @return bool |
|
246 | - */ |
|
247 | - public function recoveryKeyExists() { |
|
248 | - $key = $this->getRecoveryKey(); |
|
249 | - return !empty($key); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * get recovery key |
|
254 | - * |
|
255 | - * @return string |
|
256 | - */ |
|
257 | - public function getRecoveryKey() { |
|
258 | - return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.' . $this->publicKeyId, Encryption::ID); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * get recovery key ID |
|
263 | - * |
|
264 | - * @return string |
|
265 | - */ |
|
266 | - public function getRecoveryKeyId() { |
|
267 | - return $this->recoveryKeyId; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * @param string $password |
|
272 | - * @return bool |
|
273 | - */ |
|
274 | - public function checkRecoveryPassword($password) { |
|
275 | - $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.' . $this->privateKeyId, Encryption::ID); |
|
276 | - $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password); |
|
277 | - |
|
278 | - if ($decryptedRecoveryKey) { |
|
279 | - return true; |
|
280 | - } |
|
281 | - return false; |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * @param string $uid |
|
286 | - * @param string $password |
|
287 | - * @param array $keyPair |
|
288 | - * @return bool |
|
289 | - */ |
|
290 | - public function storeKeyPair($uid, $password, $keyPair) { |
|
291 | - // Save Public Key |
|
292 | - $this->setPublicKey($uid, $keyPair['publicKey']); |
|
293 | - |
|
294 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password, $uid); |
|
295 | - |
|
296 | - $header = $this->crypt->generateHeader(); |
|
297 | - |
|
298 | - if ($encryptedKey) { |
|
299 | - $this->setPrivateKey($uid, $header . $encryptedKey); |
|
300 | - return true; |
|
301 | - } |
|
302 | - return false; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * @param string $password |
|
307 | - * @param array $keyPair |
|
308 | - * @return bool |
|
309 | - */ |
|
310 | - public function setRecoveryKey($password, $keyPair) { |
|
311 | - // Save Public Key |
|
312 | - $this->keyStorage->setSystemUserKey($this->getRecoveryKeyId(). |
|
313 | - '.' . $this->publicKeyId, |
|
314 | - $keyPair['publicKey'], |
|
315 | - Encryption::ID); |
|
316 | - |
|
317 | - $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password); |
|
318 | - $header = $this->crypt->generateHeader(); |
|
319 | - |
|
320 | - if ($encryptedKey) { |
|
321 | - $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey); |
|
322 | - return true; |
|
323 | - } |
|
324 | - return false; |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * @param $userId |
|
329 | - * @param $key |
|
330 | - * @return bool |
|
331 | - */ |
|
332 | - public function setPublicKey($userId, $key) { |
|
333 | - return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID); |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * @param $userId |
|
338 | - * @param string $key |
|
339 | - * @return bool |
|
340 | - */ |
|
341 | - public function setPrivateKey($userId, $key) { |
|
342 | - return $this->keyStorage->setUserKey($userId, |
|
343 | - $this->privateKeyId, |
|
344 | - $key, |
|
345 | - Encryption::ID); |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * write file key to key storage |
|
350 | - * |
|
351 | - * @param string $path |
|
352 | - * @param string $key |
|
353 | - * @return boolean |
|
354 | - */ |
|
355 | - public function setFileKey($path, $key) { |
|
356 | - return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID); |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * set all file keys (the file key and the corresponding share keys) |
|
361 | - * |
|
362 | - * @param string $path |
|
363 | - * @param array $keys |
|
364 | - */ |
|
365 | - public function setAllFileKeys($path, $keys) { |
|
366 | - $this->setFileKey($path, $keys['data']); |
|
367 | - foreach ($keys['keys'] as $uid => $keyFile) { |
|
368 | - $this->setShareKey($path, $uid, $keyFile); |
|
369 | - } |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * write share key to the key storage |
|
374 | - * |
|
375 | - * @param string $path |
|
376 | - * @param string $uid |
|
377 | - * @param string $key |
|
378 | - * @return boolean |
|
379 | - */ |
|
380 | - public function setShareKey($path, $uid, $key) { |
|
381 | - $keyId = $uid . '.' . $this->shareKeyId; |
|
382 | - return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID); |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Decrypt private key and store it |
|
387 | - * |
|
388 | - * @param string $uid user id |
|
389 | - * @param string $passPhrase users password |
|
390 | - * @return boolean |
|
391 | - */ |
|
392 | - public function init($uid, $passPhrase) { |
|
393 | - $this->session->setStatus(Session::INIT_EXECUTED); |
|
394 | - |
|
395 | - try { |
|
396 | - if ($this->util->isMasterKeyEnabled()) { |
|
397 | - $uid = $this->getMasterKeyId(); |
|
398 | - $passPhrase = $this->getMasterKeyPassword(); |
|
399 | - $privateKey = $this->getSystemPrivateKey($uid); |
|
400 | - } else { |
|
401 | - $privateKey = $this->getPrivateKey($uid); |
|
402 | - } |
|
403 | - $privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid); |
|
404 | - } catch (PrivateKeyMissingException $e) { |
|
405 | - return false; |
|
406 | - } catch (DecryptionFailedException $e) { |
|
407 | - return false; |
|
408 | - } catch (\Exception $e) { |
|
409 | - $this->log->logException($e, [ |
|
410 | - 'message' => 'Could not decrypt the private key from user "' . $uid . '"" during login. Assume password change on the user back-end.', |
|
411 | - 'level' => ILogger::WARN, |
|
412 | - 'app' => 'encryption', |
|
413 | - ]); |
|
414 | - return false; |
|
415 | - } |
|
416 | - |
|
417 | - if ($privateKey) { |
|
418 | - $this->session->setPrivateKey($privateKey); |
|
419 | - $this->session->setStatus(Session::INIT_SUCCESSFUL); |
|
420 | - return true; |
|
421 | - } |
|
422 | - |
|
423 | - return false; |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * @param $userId |
|
428 | - * @return string |
|
429 | - * @throws PrivateKeyMissingException |
|
430 | - */ |
|
431 | - public function getPrivateKey($userId) { |
|
432 | - $privateKey = $this->keyStorage->getUserKey($userId, |
|
433 | - $this->privateKeyId, Encryption::ID); |
|
434 | - |
|
435 | - if (strlen($privateKey) !== 0) { |
|
436 | - return $privateKey; |
|
437 | - } |
|
438 | - throw new PrivateKeyMissingException($userId); |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * @param string $path |
|
443 | - * @param $uid |
|
444 | - * @return string |
|
445 | - */ |
|
446 | - public function getFileKey($path, $uid) { |
|
447 | - if ($uid === '') { |
|
448 | - $uid = null; |
|
449 | - } |
|
450 | - $publicAccess = is_null($uid); |
|
451 | - $encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID); |
|
452 | - |
|
453 | - if (empty($encryptedFileKey)) { |
|
454 | - return ''; |
|
455 | - } |
|
456 | - |
|
457 | - if ($this->util->isMasterKeyEnabled()) { |
|
458 | - $uid = $this->getMasterKeyId(); |
|
459 | - $shareKey = $this->getShareKey($path, $uid); |
|
460 | - if ($publicAccess) { |
|
461 | - $privateKey = $this->getSystemPrivateKey($uid); |
|
462 | - $privateKey = $this->crypt->decryptPrivateKey($privateKey, $this->getMasterKeyPassword(), $uid); |
|
463 | - } else { |
|
464 | - // when logged in, the master key is already decrypted in the session |
|
465 | - $privateKey = $this->session->getPrivateKey(); |
|
466 | - } |
|
467 | - } elseif ($publicAccess) { |
|
468 | - // use public share key for public links |
|
469 | - $uid = $this->getPublicShareKeyId(); |
|
470 | - $shareKey = $this->getShareKey($path, $uid); |
|
471 | - $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.' . $this->privateKeyId, Encryption::ID); |
|
472 | - $privateKey = $this->crypt->decryptPrivateKey($privateKey); |
|
473 | - } else { |
|
474 | - $shareKey = $this->getShareKey($path, $uid); |
|
475 | - $privateKey = $this->session->getPrivateKey(); |
|
476 | - } |
|
477 | - |
|
478 | - if ($encryptedFileKey && $shareKey && $privateKey) { |
|
479 | - return $this->crypt->multiKeyDecrypt($encryptedFileKey, |
|
480 | - $shareKey, |
|
481 | - $privateKey); |
|
482 | - } |
|
483 | - |
|
484 | - return ''; |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * Get the current version of a file |
|
489 | - * |
|
490 | - * @param string $path |
|
491 | - * @param View $view |
|
492 | - * @return int |
|
493 | - */ |
|
494 | - public function getVersion($path, View $view) { |
|
495 | - $fileInfo = $view->getFileInfo($path); |
|
496 | - if ($fileInfo === false) { |
|
497 | - return 0; |
|
498 | - } |
|
499 | - return $fileInfo->getEncryptedVersion(); |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * Set the current version of a file |
|
504 | - * |
|
505 | - * @param string $path |
|
506 | - * @param int $version |
|
507 | - * @param View $view |
|
508 | - */ |
|
509 | - public function setVersion($path, $version, View $view) { |
|
510 | - $fileInfo= $view->getFileInfo($path); |
|
511 | - |
|
512 | - if ($fileInfo !== false) { |
|
513 | - $cache = $fileInfo->getStorage()->getCache(); |
|
514 | - $cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]); |
|
515 | - } |
|
516 | - } |
|
517 | - |
|
518 | - /** |
|
519 | - * get the encrypted file key |
|
520 | - * |
|
521 | - * @param string $path |
|
522 | - * @return string |
|
523 | - */ |
|
524 | - public function getEncryptedFileKey($path) { |
|
525 | - $encryptedFileKey = $this->keyStorage->getFileKey($path, |
|
526 | - $this->fileKeyId, Encryption::ID); |
|
527 | - |
|
528 | - return $encryptedFileKey; |
|
529 | - } |
|
530 | - |
|
531 | - /** |
|
532 | - * delete share key |
|
533 | - * |
|
534 | - * @param string $path |
|
535 | - * @param string $keyId |
|
536 | - * @return boolean |
|
537 | - */ |
|
538 | - public function deleteShareKey($path, $keyId) { |
|
539 | - return $this->keyStorage->deleteFileKey( |
|
540 | - $path, |
|
541 | - $keyId . '.' . $this->shareKeyId, |
|
542 | - Encryption::ID); |
|
543 | - } |
|
544 | - |
|
545 | - |
|
546 | - /** |
|
547 | - * @param $path |
|
548 | - * @param $uid |
|
549 | - * @return mixed |
|
550 | - */ |
|
551 | - public function getShareKey($path, $uid) { |
|
552 | - $keyId = $uid . '.' . $this->shareKeyId; |
|
553 | - return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID); |
|
554 | - } |
|
555 | - |
|
556 | - /** |
|
557 | - * check if user has a private and a public key |
|
558 | - * |
|
559 | - * @param string $userId |
|
560 | - * @return bool |
|
561 | - * @throws PrivateKeyMissingException |
|
562 | - * @throws PublicKeyMissingException |
|
563 | - */ |
|
564 | - public function userHasKeys($userId) { |
|
565 | - $privateKey = $publicKey = true; |
|
566 | - $exception = null; |
|
567 | - |
|
568 | - try { |
|
569 | - $this->getPrivateKey($userId); |
|
570 | - } catch (PrivateKeyMissingException $e) { |
|
571 | - $privateKey = false; |
|
572 | - $exception = $e; |
|
573 | - } |
|
574 | - try { |
|
575 | - $this->getPublicKey($userId); |
|
576 | - } catch (PublicKeyMissingException $e) { |
|
577 | - $publicKey = false; |
|
578 | - $exception = $e; |
|
579 | - } |
|
580 | - |
|
581 | - if ($privateKey && $publicKey) { |
|
582 | - return true; |
|
583 | - } elseif (!$privateKey && !$publicKey) { |
|
584 | - return false; |
|
585 | - } else { |
|
586 | - throw $exception; |
|
587 | - } |
|
588 | - } |
|
589 | - |
|
590 | - /** |
|
591 | - * @param $userId |
|
592 | - * @return mixed |
|
593 | - * @throws PublicKeyMissingException |
|
594 | - */ |
|
595 | - public function getPublicKey($userId) { |
|
596 | - $publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID); |
|
597 | - |
|
598 | - if (strlen($publicKey) !== 0) { |
|
599 | - return $publicKey; |
|
600 | - } |
|
601 | - throw new PublicKeyMissingException($userId); |
|
602 | - } |
|
603 | - |
|
604 | - public function getPublicShareKeyId() { |
|
605 | - return $this->publicShareKeyId; |
|
606 | - } |
|
607 | - |
|
608 | - /** |
|
609 | - * get public key for public link shares |
|
610 | - * |
|
611 | - * @return string |
|
612 | - */ |
|
613 | - public function getPublicShareKey() { |
|
614 | - return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.' . $this->publicKeyId, Encryption::ID); |
|
615 | - } |
|
616 | - |
|
617 | - /** |
|
618 | - * @param string $purpose |
|
619 | - * @param string $uid |
|
620 | - */ |
|
621 | - public function backupUserKeys($purpose, $uid) { |
|
622 | - $this->keyStorage->backupUserKeys(Encryption::ID, $purpose, $uid); |
|
623 | - } |
|
624 | - |
|
625 | - /** |
|
626 | - * creat a backup of the users private and public key and then delete it |
|
627 | - * |
|
628 | - * @param string $uid |
|
629 | - */ |
|
630 | - public function deleteUserKeys($uid) { |
|
631 | - $this->deletePublicKey($uid); |
|
632 | - $this->deletePrivateKey($uid); |
|
633 | - } |
|
634 | - |
|
635 | - /** |
|
636 | - * @param $uid |
|
637 | - * @return bool |
|
638 | - */ |
|
639 | - public function deletePublicKey($uid) { |
|
640 | - return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID); |
|
641 | - } |
|
642 | - |
|
643 | - /** |
|
644 | - * @param string $uid |
|
645 | - * @return bool |
|
646 | - */ |
|
647 | - private function deletePrivateKey($uid) { |
|
648 | - return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID); |
|
649 | - } |
|
650 | - |
|
651 | - /** |
|
652 | - * @param string $path |
|
653 | - * @return bool |
|
654 | - */ |
|
655 | - public function deleteAllFileKeys($path) { |
|
656 | - return $this->keyStorage->deleteAllFileKeys($path); |
|
657 | - } |
|
658 | - |
|
659 | - /** |
|
660 | - * @param array $userIds |
|
661 | - * @return array |
|
662 | - * @throws PublicKeyMissingException |
|
663 | - */ |
|
664 | - public function getPublicKeys(array $userIds) { |
|
665 | - $keys = []; |
|
666 | - |
|
667 | - foreach ($userIds as $userId) { |
|
668 | - try { |
|
669 | - $keys[$userId] = $this->getPublicKey($userId); |
|
670 | - } catch (PublicKeyMissingException $e) { |
|
671 | - continue; |
|
672 | - } |
|
673 | - } |
|
674 | - |
|
675 | - return $keys; |
|
676 | - } |
|
677 | - |
|
678 | - /** |
|
679 | - * @param string $keyId |
|
680 | - * @return string returns openssl key |
|
681 | - */ |
|
682 | - public function getSystemPrivateKey($keyId) { |
|
683 | - return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID); |
|
684 | - } |
|
685 | - |
|
686 | - /** |
|
687 | - * @param string $keyId |
|
688 | - * @param string $key |
|
689 | - * @return string returns openssl key |
|
690 | - */ |
|
691 | - public function setSystemPrivateKey($keyId, $key) { |
|
692 | - return $this->keyStorage->setSystemUserKey( |
|
693 | - $keyId . '.' . $this->privateKeyId, |
|
694 | - $key, |
|
695 | - Encryption::ID); |
|
696 | - } |
|
697 | - |
|
698 | - /** |
|
699 | - * add system keys such as the public share key and the recovery key |
|
700 | - * |
|
701 | - * @param array $accessList |
|
702 | - * @param array $publicKeys |
|
703 | - * @param string $uid |
|
704 | - * @return array |
|
705 | - * @throws PublicKeyMissingException |
|
706 | - */ |
|
707 | - public function addSystemKeys(array $accessList, array $publicKeys, $uid) { |
|
708 | - if (!empty($accessList['public'])) { |
|
709 | - $publicShareKey = $this->getPublicShareKey(); |
|
710 | - if (empty($publicShareKey)) { |
|
711 | - throw new PublicKeyMissingException($this->getPublicShareKeyId()); |
|
712 | - } |
|
713 | - $publicKeys[$this->getPublicShareKeyId()] = $publicShareKey; |
|
714 | - } |
|
715 | - |
|
716 | - if ($this->recoveryKeyExists() && |
|
717 | - $this->util->isRecoveryEnabledForUser($uid)) { |
|
718 | - $publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey(); |
|
719 | - } |
|
720 | - |
|
721 | - return $publicKeys; |
|
722 | - } |
|
723 | - |
|
724 | - /** |
|
725 | - * get master key password |
|
726 | - * |
|
727 | - * @return string |
|
728 | - * @throws \Exception |
|
729 | - */ |
|
730 | - public function getMasterKeyPassword() { |
|
731 | - $password = $this->config->getSystemValue('secret'); |
|
732 | - if (empty($password)) { |
|
733 | - throw new \Exception('Can not get secret from Nextcloud instance'); |
|
734 | - } |
|
735 | - |
|
736 | - return $password; |
|
737 | - } |
|
738 | - |
|
739 | - /** |
|
740 | - * return master key id |
|
741 | - * |
|
742 | - * @return string |
|
743 | - */ |
|
744 | - public function getMasterKeyId() { |
|
745 | - return $this->masterKeyId; |
|
746 | - } |
|
747 | - |
|
748 | - /** |
|
749 | - * get public master key |
|
750 | - * |
|
751 | - * @return string |
|
752 | - */ |
|
753 | - public function getPublicMasterKey() { |
|
754 | - return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.' . $this->publicKeyId, Encryption::ID); |
|
755 | - } |
|
756 | - |
|
757 | - /** |
|
758 | - * get public master key |
|
759 | - * |
|
760 | - * @return string |
|
761 | - */ |
|
762 | - public function getPrivateMasterKey() { |
|
763 | - return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.' . $this->privateKeyId, Encryption::ID); |
|
764 | - } |
|
48 | + /** |
|
49 | + * @var Session |
|
50 | + */ |
|
51 | + protected $session; |
|
52 | + /** |
|
53 | + * @var IStorage |
|
54 | + */ |
|
55 | + private $keyStorage; |
|
56 | + /** |
|
57 | + * @var Crypt |
|
58 | + */ |
|
59 | + private $crypt; |
|
60 | + /** |
|
61 | + * @var string |
|
62 | + */ |
|
63 | + private $recoveryKeyId; |
|
64 | + /** |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + private $publicShareKeyId; |
|
68 | + /** |
|
69 | + * @var string |
|
70 | + */ |
|
71 | + private $masterKeyId; |
|
72 | + /** |
|
73 | + * @var string UserID |
|
74 | + */ |
|
75 | + private $keyId; |
|
76 | + /** |
|
77 | + * @var string |
|
78 | + */ |
|
79 | + private $publicKeyId = 'publicKey'; |
|
80 | + /** |
|
81 | + * @var string |
|
82 | + */ |
|
83 | + private $privateKeyId = 'privateKey'; |
|
84 | + |
|
85 | + /** |
|
86 | + * @var string |
|
87 | + */ |
|
88 | + private $shareKeyId = 'shareKey'; |
|
89 | + |
|
90 | + /** |
|
91 | + * @var string |
|
92 | + */ |
|
93 | + private $fileKeyId = 'fileKey'; |
|
94 | + /** |
|
95 | + * @var IConfig |
|
96 | + */ |
|
97 | + private $config; |
|
98 | + /** |
|
99 | + * @var ILogger |
|
100 | + */ |
|
101 | + private $log; |
|
102 | + /** |
|
103 | + * @var Util |
|
104 | + */ |
|
105 | + private $util; |
|
106 | + |
|
107 | + /** |
|
108 | + * @var ILockingProvider |
|
109 | + */ |
|
110 | + private $lockingProvider; |
|
111 | + |
|
112 | + /** |
|
113 | + * @param IStorage $keyStorage |
|
114 | + * @param Crypt $crypt |
|
115 | + * @param IConfig $config |
|
116 | + * @param IUserSession $userSession |
|
117 | + * @param Session $session |
|
118 | + * @param ILogger $log |
|
119 | + * @param Util $util |
|
120 | + */ |
|
121 | + public function __construct( |
|
122 | + IStorage $keyStorage, |
|
123 | + Crypt $crypt, |
|
124 | + IConfig $config, |
|
125 | + IUserSession $userSession, |
|
126 | + Session $session, |
|
127 | + ILogger $log, |
|
128 | + Util $util, |
|
129 | + ILockingProvider $lockingProvider |
|
130 | + ) { |
|
131 | + $this->util = $util; |
|
132 | + $this->session = $session; |
|
133 | + $this->keyStorage = $keyStorage; |
|
134 | + $this->crypt = $crypt; |
|
135 | + $this->config = $config; |
|
136 | + $this->log = $log; |
|
137 | + $this->lockingProvider = $lockingProvider; |
|
138 | + |
|
139 | + $this->recoveryKeyId = $this->config->getAppValue('encryption', |
|
140 | + 'recoveryKeyId'); |
|
141 | + if (empty($this->recoveryKeyId)) { |
|
142 | + $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8); |
|
143 | + $this->config->setAppValue('encryption', |
|
144 | + 'recoveryKeyId', |
|
145 | + $this->recoveryKeyId); |
|
146 | + } |
|
147 | + |
|
148 | + $this->publicShareKeyId = $this->config->getAppValue('encryption', |
|
149 | + 'publicShareKeyId'); |
|
150 | + if (empty($this->publicShareKeyId)) { |
|
151 | + $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8); |
|
152 | + $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId); |
|
153 | + } |
|
154 | + |
|
155 | + $this->masterKeyId = $this->config->getAppValue('encryption', |
|
156 | + 'masterKeyId'); |
|
157 | + if (empty($this->masterKeyId)) { |
|
158 | + $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8); |
|
159 | + $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId); |
|
160 | + } |
|
161 | + |
|
162 | + $this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false; |
|
163 | + $this->log = $log; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * check if key pair for public link shares exists, if not we create one |
|
168 | + */ |
|
169 | + public function validateShareKey() { |
|
170 | + $shareKey = $this->getPublicShareKey(); |
|
171 | + if (empty($shareKey)) { |
|
172 | + $this->lockingProvider->acquireLock('encryption-generateSharedKey', ILockingProvider::LOCK_EXCLUSIVE, 'Encryption: shared key generation'); |
|
173 | + try { |
|
174 | + $keyPair = $this->crypt->createKeyPair(); |
|
175 | + |
|
176 | + // Save public key |
|
177 | + $this->keyStorage->setSystemUserKey( |
|
178 | + $this->publicShareKeyId . '.' . $this->publicKeyId, $keyPair['publicKey'], |
|
179 | + Encryption::ID); |
|
180 | + |
|
181 | + // Encrypt private key empty passphrase |
|
182 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], ''); |
|
183 | + $header = $this->crypt->generateHeader(); |
|
184 | + $this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey); |
|
185 | + } catch (\Throwable $e) { |
|
186 | + $this->lockingProvider->releaseLock('encryption-generateSharedKey', ILockingProvider::LOCK_EXCLUSIVE); |
|
187 | + throw $e; |
|
188 | + } |
|
189 | + $this->lockingProvider->releaseLock('encryption-generateSharedKey', ILockingProvider::LOCK_EXCLUSIVE); |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * check if a key pair for the master key exists, if not we create one |
|
195 | + */ |
|
196 | + public function validateMasterKey() { |
|
197 | + if ($this->util->isMasterKeyEnabled() === false) { |
|
198 | + return; |
|
199 | + } |
|
200 | + |
|
201 | + $publicMasterKey = $this->getPublicMasterKey(); |
|
202 | + $privateMasterKey = $this->getPrivateMasterKey(); |
|
203 | + |
|
204 | + if (empty($publicMasterKey) && empty($privateMasterKey)) { |
|
205 | + // There could be a race condition here if two requests would trigger |
|
206 | + // the generation the second one would enter the key generation as long |
|
207 | + // as the first one didn't write the key to the keystorage yet |
|
208 | + $this->lockingProvider->acquireLock('encryption-generateMasterKey', ILockingProvider::LOCK_EXCLUSIVE, 'Encryption: master key generation'); |
|
209 | + try { |
|
210 | + $keyPair = $this->crypt->createKeyPair(); |
|
211 | + |
|
212 | + // Save public key |
|
213 | + $this->keyStorage->setSystemUserKey( |
|
214 | + $this->masterKeyId . '.' . $this->publicKeyId, $keyPair['publicKey'], |
|
215 | + Encryption::ID); |
|
216 | + |
|
217 | + // Encrypt private key with system password |
|
218 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId); |
|
219 | + $header = $this->crypt->generateHeader(); |
|
220 | + $this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey); |
|
221 | + } catch (\Throwable $e) { |
|
222 | + $this->lockingProvider->releaseLock('encryption-generateMasterKey', ILockingProvider::LOCK_EXCLUSIVE); |
|
223 | + throw $e; |
|
224 | + } |
|
225 | + $this->lockingProvider->releaseLock('encryption-generateMasterKey', ILockingProvider::LOCK_EXCLUSIVE); |
|
226 | + } elseif (empty($publicMasterKey)) { |
|
227 | + $this->log->error('A private master key is available but the public key could not be found. This should never happen.'); |
|
228 | + return; |
|
229 | + } elseif (empty($privateMasterKey)) { |
|
230 | + $this->log->error('A public master key is available but the private key could not be found. This should never happen.'); |
|
231 | + return; |
|
232 | + } |
|
233 | + |
|
234 | + if (!$this->session->isPrivateKeySet()) { |
|
235 | + $masterKey = $this->getSystemPrivateKey($this->masterKeyId); |
|
236 | + $decryptedMasterKey = $this->crypt->decryptPrivateKey($masterKey, $this->getMasterKeyPassword(), $this->masterKeyId); |
|
237 | + $this->session->setPrivateKey($decryptedMasterKey); |
|
238 | + } |
|
239 | + |
|
240 | + // after the encryption key is available we are ready to go |
|
241 | + $this->session->setStatus(Session::INIT_SUCCESSFUL); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @return bool |
|
246 | + */ |
|
247 | + public function recoveryKeyExists() { |
|
248 | + $key = $this->getRecoveryKey(); |
|
249 | + return !empty($key); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * get recovery key |
|
254 | + * |
|
255 | + * @return string |
|
256 | + */ |
|
257 | + public function getRecoveryKey() { |
|
258 | + return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.' . $this->publicKeyId, Encryption::ID); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * get recovery key ID |
|
263 | + * |
|
264 | + * @return string |
|
265 | + */ |
|
266 | + public function getRecoveryKeyId() { |
|
267 | + return $this->recoveryKeyId; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * @param string $password |
|
272 | + * @return bool |
|
273 | + */ |
|
274 | + public function checkRecoveryPassword($password) { |
|
275 | + $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.' . $this->privateKeyId, Encryption::ID); |
|
276 | + $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password); |
|
277 | + |
|
278 | + if ($decryptedRecoveryKey) { |
|
279 | + return true; |
|
280 | + } |
|
281 | + return false; |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * @param string $uid |
|
286 | + * @param string $password |
|
287 | + * @param array $keyPair |
|
288 | + * @return bool |
|
289 | + */ |
|
290 | + public function storeKeyPair($uid, $password, $keyPair) { |
|
291 | + // Save Public Key |
|
292 | + $this->setPublicKey($uid, $keyPair['publicKey']); |
|
293 | + |
|
294 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password, $uid); |
|
295 | + |
|
296 | + $header = $this->crypt->generateHeader(); |
|
297 | + |
|
298 | + if ($encryptedKey) { |
|
299 | + $this->setPrivateKey($uid, $header . $encryptedKey); |
|
300 | + return true; |
|
301 | + } |
|
302 | + return false; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * @param string $password |
|
307 | + * @param array $keyPair |
|
308 | + * @return bool |
|
309 | + */ |
|
310 | + public function setRecoveryKey($password, $keyPair) { |
|
311 | + // Save Public Key |
|
312 | + $this->keyStorage->setSystemUserKey($this->getRecoveryKeyId(). |
|
313 | + '.' . $this->publicKeyId, |
|
314 | + $keyPair['publicKey'], |
|
315 | + Encryption::ID); |
|
316 | + |
|
317 | + $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password); |
|
318 | + $header = $this->crypt->generateHeader(); |
|
319 | + |
|
320 | + if ($encryptedKey) { |
|
321 | + $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey); |
|
322 | + return true; |
|
323 | + } |
|
324 | + return false; |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * @param $userId |
|
329 | + * @param $key |
|
330 | + * @return bool |
|
331 | + */ |
|
332 | + public function setPublicKey($userId, $key) { |
|
333 | + return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID); |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * @param $userId |
|
338 | + * @param string $key |
|
339 | + * @return bool |
|
340 | + */ |
|
341 | + public function setPrivateKey($userId, $key) { |
|
342 | + return $this->keyStorage->setUserKey($userId, |
|
343 | + $this->privateKeyId, |
|
344 | + $key, |
|
345 | + Encryption::ID); |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * write file key to key storage |
|
350 | + * |
|
351 | + * @param string $path |
|
352 | + * @param string $key |
|
353 | + * @return boolean |
|
354 | + */ |
|
355 | + public function setFileKey($path, $key) { |
|
356 | + return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID); |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * set all file keys (the file key and the corresponding share keys) |
|
361 | + * |
|
362 | + * @param string $path |
|
363 | + * @param array $keys |
|
364 | + */ |
|
365 | + public function setAllFileKeys($path, $keys) { |
|
366 | + $this->setFileKey($path, $keys['data']); |
|
367 | + foreach ($keys['keys'] as $uid => $keyFile) { |
|
368 | + $this->setShareKey($path, $uid, $keyFile); |
|
369 | + } |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * write share key to the key storage |
|
374 | + * |
|
375 | + * @param string $path |
|
376 | + * @param string $uid |
|
377 | + * @param string $key |
|
378 | + * @return boolean |
|
379 | + */ |
|
380 | + public function setShareKey($path, $uid, $key) { |
|
381 | + $keyId = $uid . '.' . $this->shareKeyId; |
|
382 | + return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID); |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Decrypt private key and store it |
|
387 | + * |
|
388 | + * @param string $uid user id |
|
389 | + * @param string $passPhrase users password |
|
390 | + * @return boolean |
|
391 | + */ |
|
392 | + public function init($uid, $passPhrase) { |
|
393 | + $this->session->setStatus(Session::INIT_EXECUTED); |
|
394 | + |
|
395 | + try { |
|
396 | + if ($this->util->isMasterKeyEnabled()) { |
|
397 | + $uid = $this->getMasterKeyId(); |
|
398 | + $passPhrase = $this->getMasterKeyPassword(); |
|
399 | + $privateKey = $this->getSystemPrivateKey($uid); |
|
400 | + } else { |
|
401 | + $privateKey = $this->getPrivateKey($uid); |
|
402 | + } |
|
403 | + $privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid); |
|
404 | + } catch (PrivateKeyMissingException $e) { |
|
405 | + return false; |
|
406 | + } catch (DecryptionFailedException $e) { |
|
407 | + return false; |
|
408 | + } catch (\Exception $e) { |
|
409 | + $this->log->logException($e, [ |
|
410 | + 'message' => 'Could not decrypt the private key from user "' . $uid . '"" during login. Assume password change on the user back-end.', |
|
411 | + 'level' => ILogger::WARN, |
|
412 | + 'app' => 'encryption', |
|
413 | + ]); |
|
414 | + return false; |
|
415 | + } |
|
416 | + |
|
417 | + if ($privateKey) { |
|
418 | + $this->session->setPrivateKey($privateKey); |
|
419 | + $this->session->setStatus(Session::INIT_SUCCESSFUL); |
|
420 | + return true; |
|
421 | + } |
|
422 | + |
|
423 | + return false; |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * @param $userId |
|
428 | + * @return string |
|
429 | + * @throws PrivateKeyMissingException |
|
430 | + */ |
|
431 | + public function getPrivateKey($userId) { |
|
432 | + $privateKey = $this->keyStorage->getUserKey($userId, |
|
433 | + $this->privateKeyId, Encryption::ID); |
|
434 | + |
|
435 | + if (strlen($privateKey) !== 0) { |
|
436 | + return $privateKey; |
|
437 | + } |
|
438 | + throw new PrivateKeyMissingException($userId); |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * @param string $path |
|
443 | + * @param $uid |
|
444 | + * @return string |
|
445 | + */ |
|
446 | + public function getFileKey($path, $uid) { |
|
447 | + if ($uid === '') { |
|
448 | + $uid = null; |
|
449 | + } |
|
450 | + $publicAccess = is_null($uid); |
|
451 | + $encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID); |
|
452 | + |
|
453 | + if (empty($encryptedFileKey)) { |
|
454 | + return ''; |
|
455 | + } |
|
456 | + |
|
457 | + if ($this->util->isMasterKeyEnabled()) { |
|
458 | + $uid = $this->getMasterKeyId(); |
|
459 | + $shareKey = $this->getShareKey($path, $uid); |
|
460 | + if ($publicAccess) { |
|
461 | + $privateKey = $this->getSystemPrivateKey($uid); |
|
462 | + $privateKey = $this->crypt->decryptPrivateKey($privateKey, $this->getMasterKeyPassword(), $uid); |
|
463 | + } else { |
|
464 | + // when logged in, the master key is already decrypted in the session |
|
465 | + $privateKey = $this->session->getPrivateKey(); |
|
466 | + } |
|
467 | + } elseif ($publicAccess) { |
|
468 | + // use public share key for public links |
|
469 | + $uid = $this->getPublicShareKeyId(); |
|
470 | + $shareKey = $this->getShareKey($path, $uid); |
|
471 | + $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.' . $this->privateKeyId, Encryption::ID); |
|
472 | + $privateKey = $this->crypt->decryptPrivateKey($privateKey); |
|
473 | + } else { |
|
474 | + $shareKey = $this->getShareKey($path, $uid); |
|
475 | + $privateKey = $this->session->getPrivateKey(); |
|
476 | + } |
|
477 | + |
|
478 | + if ($encryptedFileKey && $shareKey && $privateKey) { |
|
479 | + return $this->crypt->multiKeyDecrypt($encryptedFileKey, |
|
480 | + $shareKey, |
|
481 | + $privateKey); |
|
482 | + } |
|
483 | + |
|
484 | + return ''; |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * Get the current version of a file |
|
489 | + * |
|
490 | + * @param string $path |
|
491 | + * @param View $view |
|
492 | + * @return int |
|
493 | + */ |
|
494 | + public function getVersion($path, View $view) { |
|
495 | + $fileInfo = $view->getFileInfo($path); |
|
496 | + if ($fileInfo === false) { |
|
497 | + return 0; |
|
498 | + } |
|
499 | + return $fileInfo->getEncryptedVersion(); |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * Set the current version of a file |
|
504 | + * |
|
505 | + * @param string $path |
|
506 | + * @param int $version |
|
507 | + * @param View $view |
|
508 | + */ |
|
509 | + public function setVersion($path, $version, View $view) { |
|
510 | + $fileInfo= $view->getFileInfo($path); |
|
511 | + |
|
512 | + if ($fileInfo !== false) { |
|
513 | + $cache = $fileInfo->getStorage()->getCache(); |
|
514 | + $cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]); |
|
515 | + } |
|
516 | + } |
|
517 | + |
|
518 | + /** |
|
519 | + * get the encrypted file key |
|
520 | + * |
|
521 | + * @param string $path |
|
522 | + * @return string |
|
523 | + */ |
|
524 | + public function getEncryptedFileKey($path) { |
|
525 | + $encryptedFileKey = $this->keyStorage->getFileKey($path, |
|
526 | + $this->fileKeyId, Encryption::ID); |
|
527 | + |
|
528 | + return $encryptedFileKey; |
|
529 | + } |
|
530 | + |
|
531 | + /** |
|
532 | + * delete share key |
|
533 | + * |
|
534 | + * @param string $path |
|
535 | + * @param string $keyId |
|
536 | + * @return boolean |
|
537 | + */ |
|
538 | + public function deleteShareKey($path, $keyId) { |
|
539 | + return $this->keyStorage->deleteFileKey( |
|
540 | + $path, |
|
541 | + $keyId . '.' . $this->shareKeyId, |
|
542 | + Encryption::ID); |
|
543 | + } |
|
544 | + |
|
545 | + |
|
546 | + /** |
|
547 | + * @param $path |
|
548 | + * @param $uid |
|
549 | + * @return mixed |
|
550 | + */ |
|
551 | + public function getShareKey($path, $uid) { |
|
552 | + $keyId = $uid . '.' . $this->shareKeyId; |
|
553 | + return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID); |
|
554 | + } |
|
555 | + |
|
556 | + /** |
|
557 | + * check if user has a private and a public key |
|
558 | + * |
|
559 | + * @param string $userId |
|
560 | + * @return bool |
|
561 | + * @throws PrivateKeyMissingException |
|
562 | + * @throws PublicKeyMissingException |
|
563 | + */ |
|
564 | + public function userHasKeys($userId) { |
|
565 | + $privateKey = $publicKey = true; |
|
566 | + $exception = null; |
|
567 | + |
|
568 | + try { |
|
569 | + $this->getPrivateKey($userId); |
|
570 | + } catch (PrivateKeyMissingException $e) { |
|
571 | + $privateKey = false; |
|
572 | + $exception = $e; |
|
573 | + } |
|
574 | + try { |
|
575 | + $this->getPublicKey($userId); |
|
576 | + } catch (PublicKeyMissingException $e) { |
|
577 | + $publicKey = false; |
|
578 | + $exception = $e; |
|
579 | + } |
|
580 | + |
|
581 | + if ($privateKey && $publicKey) { |
|
582 | + return true; |
|
583 | + } elseif (!$privateKey && !$publicKey) { |
|
584 | + return false; |
|
585 | + } else { |
|
586 | + throw $exception; |
|
587 | + } |
|
588 | + } |
|
589 | + |
|
590 | + /** |
|
591 | + * @param $userId |
|
592 | + * @return mixed |
|
593 | + * @throws PublicKeyMissingException |
|
594 | + */ |
|
595 | + public function getPublicKey($userId) { |
|
596 | + $publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID); |
|
597 | + |
|
598 | + if (strlen($publicKey) !== 0) { |
|
599 | + return $publicKey; |
|
600 | + } |
|
601 | + throw new PublicKeyMissingException($userId); |
|
602 | + } |
|
603 | + |
|
604 | + public function getPublicShareKeyId() { |
|
605 | + return $this->publicShareKeyId; |
|
606 | + } |
|
607 | + |
|
608 | + /** |
|
609 | + * get public key for public link shares |
|
610 | + * |
|
611 | + * @return string |
|
612 | + */ |
|
613 | + public function getPublicShareKey() { |
|
614 | + return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.' . $this->publicKeyId, Encryption::ID); |
|
615 | + } |
|
616 | + |
|
617 | + /** |
|
618 | + * @param string $purpose |
|
619 | + * @param string $uid |
|
620 | + */ |
|
621 | + public function backupUserKeys($purpose, $uid) { |
|
622 | + $this->keyStorage->backupUserKeys(Encryption::ID, $purpose, $uid); |
|
623 | + } |
|
624 | + |
|
625 | + /** |
|
626 | + * creat a backup of the users private and public key and then delete it |
|
627 | + * |
|
628 | + * @param string $uid |
|
629 | + */ |
|
630 | + public function deleteUserKeys($uid) { |
|
631 | + $this->deletePublicKey($uid); |
|
632 | + $this->deletePrivateKey($uid); |
|
633 | + } |
|
634 | + |
|
635 | + /** |
|
636 | + * @param $uid |
|
637 | + * @return bool |
|
638 | + */ |
|
639 | + public function deletePublicKey($uid) { |
|
640 | + return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID); |
|
641 | + } |
|
642 | + |
|
643 | + /** |
|
644 | + * @param string $uid |
|
645 | + * @return bool |
|
646 | + */ |
|
647 | + private function deletePrivateKey($uid) { |
|
648 | + return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID); |
|
649 | + } |
|
650 | + |
|
651 | + /** |
|
652 | + * @param string $path |
|
653 | + * @return bool |
|
654 | + */ |
|
655 | + public function deleteAllFileKeys($path) { |
|
656 | + return $this->keyStorage->deleteAllFileKeys($path); |
|
657 | + } |
|
658 | + |
|
659 | + /** |
|
660 | + * @param array $userIds |
|
661 | + * @return array |
|
662 | + * @throws PublicKeyMissingException |
|
663 | + */ |
|
664 | + public function getPublicKeys(array $userIds) { |
|
665 | + $keys = []; |
|
666 | + |
|
667 | + foreach ($userIds as $userId) { |
|
668 | + try { |
|
669 | + $keys[$userId] = $this->getPublicKey($userId); |
|
670 | + } catch (PublicKeyMissingException $e) { |
|
671 | + continue; |
|
672 | + } |
|
673 | + } |
|
674 | + |
|
675 | + return $keys; |
|
676 | + } |
|
677 | + |
|
678 | + /** |
|
679 | + * @param string $keyId |
|
680 | + * @return string returns openssl key |
|
681 | + */ |
|
682 | + public function getSystemPrivateKey($keyId) { |
|
683 | + return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID); |
|
684 | + } |
|
685 | + |
|
686 | + /** |
|
687 | + * @param string $keyId |
|
688 | + * @param string $key |
|
689 | + * @return string returns openssl key |
|
690 | + */ |
|
691 | + public function setSystemPrivateKey($keyId, $key) { |
|
692 | + return $this->keyStorage->setSystemUserKey( |
|
693 | + $keyId . '.' . $this->privateKeyId, |
|
694 | + $key, |
|
695 | + Encryption::ID); |
|
696 | + } |
|
697 | + |
|
698 | + /** |
|
699 | + * add system keys such as the public share key and the recovery key |
|
700 | + * |
|
701 | + * @param array $accessList |
|
702 | + * @param array $publicKeys |
|
703 | + * @param string $uid |
|
704 | + * @return array |
|
705 | + * @throws PublicKeyMissingException |
|
706 | + */ |
|
707 | + public function addSystemKeys(array $accessList, array $publicKeys, $uid) { |
|
708 | + if (!empty($accessList['public'])) { |
|
709 | + $publicShareKey = $this->getPublicShareKey(); |
|
710 | + if (empty($publicShareKey)) { |
|
711 | + throw new PublicKeyMissingException($this->getPublicShareKeyId()); |
|
712 | + } |
|
713 | + $publicKeys[$this->getPublicShareKeyId()] = $publicShareKey; |
|
714 | + } |
|
715 | + |
|
716 | + if ($this->recoveryKeyExists() && |
|
717 | + $this->util->isRecoveryEnabledForUser($uid)) { |
|
718 | + $publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey(); |
|
719 | + } |
|
720 | + |
|
721 | + return $publicKeys; |
|
722 | + } |
|
723 | + |
|
724 | + /** |
|
725 | + * get master key password |
|
726 | + * |
|
727 | + * @return string |
|
728 | + * @throws \Exception |
|
729 | + */ |
|
730 | + public function getMasterKeyPassword() { |
|
731 | + $password = $this->config->getSystemValue('secret'); |
|
732 | + if (empty($password)) { |
|
733 | + throw new \Exception('Can not get secret from Nextcloud instance'); |
|
734 | + } |
|
735 | + |
|
736 | + return $password; |
|
737 | + } |
|
738 | + |
|
739 | + /** |
|
740 | + * return master key id |
|
741 | + * |
|
742 | + * @return string |
|
743 | + */ |
|
744 | + public function getMasterKeyId() { |
|
745 | + return $this->masterKeyId; |
|
746 | + } |
|
747 | + |
|
748 | + /** |
|
749 | + * get public master key |
|
750 | + * |
|
751 | + * @return string |
|
752 | + */ |
|
753 | + public function getPublicMasterKey() { |
|
754 | + return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.' . $this->publicKeyId, Encryption::ID); |
|
755 | + } |
|
756 | + |
|
757 | + /** |
|
758 | + * get public master key |
|
759 | + * |
|
760 | + * @return string |
|
761 | + */ |
|
762 | + public function getPrivateMasterKey() { |
|
763 | + return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.' . $this->privateKeyId, Encryption::ID); |
|
764 | + } |
|
765 | 765 | } |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | $this->recoveryKeyId = $this->config->getAppValue('encryption', |
140 | 140 | 'recoveryKeyId'); |
141 | 141 | if (empty($this->recoveryKeyId)) { |
142 | - $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8); |
|
142 | + $this->recoveryKeyId = 'recoveryKey_'.substr(md5(time()), 0, 8); |
|
143 | 143 | $this->config->setAppValue('encryption', |
144 | 144 | 'recoveryKeyId', |
145 | 145 | $this->recoveryKeyId); |
@@ -148,14 +148,14 @@ discard block |
||
148 | 148 | $this->publicShareKeyId = $this->config->getAppValue('encryption', |
149 | 149 | 'publicShareKeyId'); |
150 | 150 | if (empty($this->publicShareKeyId)) { |
151 | - $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8); |
|
151 | + $this->publicShareKeyId = 'pubShare_'.substr(md5(time()), 0, 8); |
|
152 | 152 | $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId); |
153 | 153 | } |
154 | 154 | |
155 | 155 | $this->masterKeyId = $this->config->getAppValue('encryption', |
156 | 156 | 'masterKeyId'); |
157 | 157 | if (empty($this->masterKeyId)) { |
158 | - $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8); |
|
158 | + $this->masterKeyId = 'master_'.substr(md5(time()), 0, 8); |
|
159 | 159 | $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId); |
160 | 160 | } |
161 | 161 | |
@@ -175,13 +175,13 @@ discard block |
||
175 | 175 | |
176 | 176 | // Save public key |
177 | 177 | $this->keyStorage->setSystemUserKey( |
178 | - $this->publicShareKeyId . '.' . $this->publicKeyId, $keyPair['publicKey'], |
|
178 | + $this->publicShareKeyId.'.'.$this->publicKeyId, $keyPair['publicKey'], |
|
179 | 179 | Encryption::ID); |
180 | 180 | |
181 | 181 | // Encrypt private key empty passphrase |
182 | 182 | $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], ''); |
183 | 183 | $header = $this->crypt->generateHeader(); |
184 | - $this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey); |
|
184 | + $this->setSystemPrivateKey($this->publicShareKeyId, $header.$encryptedKey); |
|
185 | 185 | } catch (\Throwable $e) { |
186 | 186 | $this->lockingProvider->releaseLock('encryption-generateSharedKey', ILockingProvider::LOCK_EXCLUSIVE); |
187 | 187 | throw $e; |
@@ -211,13 +211,13 @@ discard block |
||
211 | 211 | |
212 | 212 | // Save public key |
213 | 213 | $this->keyStorage->setSystemUserKey( |
214 | - $this->masterKeyId . '.' . $this->publicKeyId, $keyPair['publicKey'], |
|
214 | + $this->masterKeyId.'.'.$this->publicKeyId, $keyPair['publicKey'], |
|
215 | 215 | Encryption::ID); |
216 | 216 | |
217 | 217 | // Encrypt private key with system password |
218 | 218 | $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId); |
219 | 219 | $header = $this->crypt->generateHeader(); |
220 | - $this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey); |
|
220 | + $this->setSystemPrivateKey($this->masterKeyId, $header.$encryptedKey); |
|
221 | 221 | } catch (\Throwable $e) { |
222 | 222 | $this->lockingProvider->releaseLock('encryption-generateMasterKey', ILockingProvider::LOCK_EXCLUSIVE); |
223 | 223 | throw $e; |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | * @return string |
256 | 256 | */ |
257 | 257 | public function getRecoveryKey() { |
258 | - return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.' . $this->publicKeyId, Encryption::ID); |
|
258 | + return $this->keyStorage->getSystemUserKey($this->recoveryKeyId.'.'.$this->publicKeyId, Encryption::ID); |
|
259 | 259 | } |
260 | 260 | |
261 | 261 | /** |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | * @return bool |
273 | 273 | */ |
274 | 274 | public function checkRecoveryPassword($password) { |
275 | - $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.' . $this->privateKeyId, Encryption::ID); |
|
275 | + $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId.'.'.$this->privateKeyId, Encryption::ID); |
|
276 | 276 | $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password); |
277 | 277 | |
278 | 278 | if ($decryptedRecoveryKey) { |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | $header = $this->crypt->generateHeader(); |
297 | 297 | |
298 | 298 | if ($encryptedKey) { |
299 | - $this->setPrivateKey($uid, $header . $encryptedKey); |
|
299 | + $this->setPrivateKey($uid, $header.$encryptedKey); |
|
300 | 300 | return true; |
301 | 301 | } |
302 | 302 | return false; |
@@ -310,7 +310,7 @@ discard block |
||
310 | 310 | public function setRecoveryKey($password, $keyPair) { |
311 | 311 | // Save Public Key |
312 | 312 | $this->keyStorage->setSystemUserKey($this->getRecoveryKeyId(). |
313 | - '.' . $this->publicKeyId, |
|
313 | + '.'.$this->publicKeyId, |
|
314 | 314 | $keyPair['publicKey'], |
315 | 315 | Encryption::ID); |
316 | 316 | |
@@ -318,7 +318,7 @@ discard block |
||
318 | 318 | $header = $this->crypt->generateHeader(); |
319 | 319 | |
320 | 320 | if ($encryptedKey) { |
321 | - $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey); |
|
321 | + $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header.$encryptedKey); |
|
322 | 322 | return true; |
323 | 323 | } |
324 | 324 | return false; |
@@ -378,7 +378,7 @@ discard block |
||
378 | 378 | * @return boolean |
379 | 379 | */ |
380 | 380 | public function setShareKey($path, $uid, $key) { |
381 | - $keyId = $uid . '.' . $this->shareKeyId; |
|
381 | + $keyId = $uid.'.'.$this->shareKeyId; |
|
382 | 382 | return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID); |
383 | 383 | } |
384 | 384 | |
@@ -407,7 +407,7 @@ discard block |
||
407 | 407 | return false; |
408 | 408 | } catch (\Exception $e) { |
409 | 409 | $this->log->logException($e, [ |
410 | - 'message' => 'Could not decrypt the private key from user "' . $uid . '"" during login. Assume password change on the user back-end.', |
|
410 | + 'message' => 'Could not decrypt the private key from user "'.$uid.'"" during login. Assume password change on the user back-end.', |
|
411 | 411 | 'level' => ILogger::WARN, |
412 | 412 | 'app' => 'encryption', |
413 | 413 | ]); |
@@ -468,7 +468,7 @@ discard block |
||
468 | 468 | // use public share key for public links |
469 | 469 | $uid = $this->getPublicShareKeyId(); |
470 | 470 | $shareKey = $this->getShareKey($path, $uid); |
471 | - $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.' . $this->privateKeyId, Encryption::ID); |
|
471 | + $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId.'.'.$this->privateKeyId, Encryption::ID); |
|
472 | 472 | $privateKey = $this->crypt->decryptPrivateKey($privateKey); |
473 | 473 | } else { |
474 | 474 | $shareKey = $this->getShareKey($path, $uid); |
@@ -507,7 +507,7 @@ discard block |
||
507 | 507 | * @param View $view |
508 | 508 | */ |
509 | 509 | public function setVersion($path, $version, View $view) { |
510 | - $fileInfo= $view->getFileInfo($path); |
|
510 | + $fileInfo = $view->getFileInfo($path); |
|
511 | 511 | |
512 | 512 | if ($fileInfo !== false) { |
513 | 513 | $cache = $fileInfo->getStorage()->getCache(); |
@@ -538,7 +538,7 @@ discard block |
||
538 | 538 | public function deleteShareKey($path, $keyId) { |
539 | 539 | return $this->keyStorage->deleteFileKey( |
540 | 540 | $path, |
541 | - $keyId . '.' . $this->shareKeyId, |
|
541 | + $keyId.'.'.$this->shareKeyId, |
|
542 | 542 | Encryption::ID); |
543 | 543 | } |
544 | 544 | |
@@ -549,7 +549,7 @@ discard block |
||
549 | 549 | * @return mixed |
550 | 550 | */ |
551 | 551 | public function getShareKey($path, $uid) { |
552 | - $keyId = $uid . '.' . $this->shareKeyId; |
|
552 | + $keyId = $uid.'.'.$this->shareKeyId; |
|
553 | 553 | return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID); |
554 | 554 | } |
555 | 555 | |
@@ -611,7 +611,7 @@ discard block |
||
611 | 611 | * @return string |
612 | 612 | */ |
613 | 613 | public function getPublicShareKey() { |
614 | - return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.' . $this->publicKeyId, Encryption::ID); |
|
614 | + return $this->keyStorage->getSystemUserKey($this->publicShareKeyId.'.'.$this->publicKeyId, Encryption::ID); |
|
615 | 615 | } |
616 | 616 | |
617 | 617 | /** |
@@ -680,7 +680,7 @@ discard block |
||
680 | 680 | * @return string returns openssl key |
681 | 681 | */ |
682 | 682 | public function getSystemPrivateKey($keyId) { |
683 | - return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID); |
|
683 | + return $this->keyStorage->getSystemUserKey($keyId.'.'.$this->privateKeyId, Encryption::ID); |
|
684 | 684 | } |
685 | 685 | |
686 | 686 | /** |
@@ -690,7 +690,7 @@ discard block |
||
690 | 690 | */ |
691 | 691 | public function setSystemPrivateKey($keyId, $key) { |
692 | 692 | return $this->keyStorage->setSystemUserKey( |
693 | - $keyId . '.' . $this->privateKeyId, |
|
693 | + $keyId.'.'.$this->privateKeyId, |
|
694 | 694 | $key, |
695 | 695 | Encryption::ID); |
696 | 696 | } |
@@ -751,7 +751,7 @@ discard block |
||
751 | 751 | * @return string |
752 | 752 | */ |
753 | 753 | public function getPublicMasterKey() { |
754 | - return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.' . $this->publicKeyId, Encryption::ID); |
|
754 | + return $this->keyStorage->getSystemUserKey($this->masterKeyId.'.'.$this->publicKeyId, Encryption::ID); |
|
755 | 755 | } |
756 | 756 | |
757 | 757 | /** |
@@ -760,6 +760,6 @@ discard block |
||
760 | 760 | * @return string |
761 | 761 | */ |
762 | 762 | public function getPrivateMasterKey() { |
763 | - return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.' . $this->privateKeyId, Encryption::ID); |
|
763 | + return $this->keyStorage->getSystemUserKey($this->masterKeyId.'.'.$this->privateKeyId, Encryption::ID); |
|
764 | 764 | } |
765 | 765 | } |
@@ -47,234 +47,234 @@ |
||
47 | 47 | |
48 | 48 | class ChangePasswordController extends Controller { |
49 | 49 | |
50 | - /** @var string */ |
|
51 | - private $userId; |
|
50 | + /** @var string */ |
|
51 | + private $userId; |
|
52 | 52 | |
53 | - /** @var IUserManager */ |
|
54 | - private $userManager; |
|
53 | + /** @var IUserManager */ |
|
54 | + private $userManager; |
|
55 | 55 | |
56 | - /** @var IL10N */ |
|
57 | - private $l; |
|
56 | + /** @var IL10N */ |
|
57 | + private $l; |
|
58 | 58 | |
59 | - /** @var IGroupManager */ |
|
60 | - private $groupManager; |
|
59 | + /** @var IGroupManager */ |
|
60 | + private $groupManager; |
|
61 | 61 | |
62 | - /** @var Session */ |
|
63 | - private $userSession; |
|
62 | + /** @var Session */ |
|
63 | + private $userSession; |
|
64 | 64 | |
65 | - /** @var IAppManager */ |
|
66 | - private $appManager; |
|
65 | + /** @var IAppManager */ |
|
66 | + private $appManager; |
|
67 | 67 | |
68 | - public function __construct(string $appName, |
|
69 | - IRequest $request, |
|
70 | - string $userId, |
|
71 | - IUserManager $userManager, |
|
72 | - IUserSession $userSession, |
|
73 | - IGroupManager $groupManager, |
|
74 | - IAppManager $appManager, |
|
75 | - IL10N $l) { |
|
76 | - parent::__construct($appName, $request); |
|
68 | + public function __construct(string $appName, |
|
69 | + IRequest $request, |
|
70 | + string $userId, |
|
71 | + IUserManager $userManager, |
|
72 | + IUserSession $userSession, |
|
73 | + IGroupManager $groupManager, |
|
74 | + IAppManager $appManager, |
|
75 | + IL10N $l) { |
|
76 | + parent::__construct($appName, $request); |
|
77 | 77 | |
78 | - $this->userId = $userId; |
|
79 | - $this->userManager = $userManager; |
|
80 | - $this->userSession = $userSession; |
|
81 | - $this->groupManager = $groupManager; |
|
82 | - $this->appManager = $appManager; |
|
83 | - $this->l = $l; |
|
84 | - } |
|
78 | + $this->userId = $userId; |
|
79 | + $this->userManager = $userManager; |
|
80 | + $this->userSession = $userSession; |
|
81 | + $this->groupManager = $groupManager; |
|
82 | + $this->appManager = $appManager; |
|
83 | + $this->l = $l; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * @NoAdminRequired |
|
88 | - * @NoSubAdminRequired |
|
89 | - * @BruteForceProtection(action=changePersonalPassword) |
|
90 | - */ |
|
91 | - public function changePersonalPassword(string $oldpassword = '', string $newpassword = null): JSONResponse { |
|
92 | - $loginName = $this->userSession->getLoginName(); |
|
93 | - /** @var IUser $user */ |
|
94 | - $user = $this->userManager->checkPassword($loginName, $oldpassword); |
|
95 | - if ($user === false) { |
|
96 | - $response = new JSONResponse([ |
|
97 | - 'status' => 'error', |
|
98 | - 'data' => [ |
|
99 | - 'message' => $this->l->t('Wrong password'), |
|
100 | - ], |
|
101 | - ]); |
|
102 | - $response->throttle(); |
|
103 | - return $response; |
|
104 | - } |
|
86 | + /** |
|
87 | + * @NoAdminRequired |
|
88 | + * @NoSubAdminRequired |
|
89 | + * @BruteForceProtection(action=changePersonalPassword) |
|
90 | + */ |
|
91 | + public function changePersonalPassword(string $oldpassword = '', string $newpassword = null): JSONResponse { |
|
92 | + $loginName = $this->userSession->getLoginName(); |
|
93 | + /** @var IUser $user */ |
|
94 | + $user = $this->userManager->checkPassword($loginName, $oldpassword); |
|
95 | + if ($user === false) { |
|
96 | + $response = new JSONResponse([ |
|
97 | + 'status' => 'error', |
|
98 | + 'data' => [ |
|
99 | + 'message' => $this->l->t('Wrong password'), |
|
100 | + ], |
|
101 | + ]); |
|
102 | + $response->throttle(); |
|
103 | + return $response; |
|
104 | + } |
|
105 | 105 | |
106 | - try { |
|
107 | - if ($newpassword === null || $user->setPassword($newpassword) === false) { |
|
108 | - return new JSONResponse([ |
|
109 | - 'status' => 'error' |
|
110 | - ]); |
|
111 | - } |
|
112 | - // password policy app throws exception |
|
113 | - } catch (HintException $e) { |
|
114 | - return new JSONResponse([ |
|
115 | - 'status' => 'error', |
|
116 | - 'data' => [ |
|
117 | - 'message' => $e->getHint(), |
|
118 | - ], |
|
119 | - ]); |
|
120 | - } |
|
106 | + try { |
|
107 | + if ($newpassword === null || $user->setPassword($newpassword) === false) { |
|
108 | + return new JSONResponse([ |
|
109 | + 'status' => 'error' |
|
110 | + ]); |
|
111 | + } |
|
112 | + // password policy app throws exception |
|
113 | + } catch (HintException $e) { |
|
114 | + return new JSONResponse([ |
|
115 | + 'status' => 'error', |
|
116 | + 'data' => [ |
|
117 | + 'message' => $e->getHint(), |
|
118 | + ], |
|
119 | + ]); |
|
120 | + } |
|
121 | 121 | |
122 | - $this->userSession->updateSessionTokenPassword($newpassword); |
|
122 | + $this->userSession->updateSessionTokenPassword($newpassword); |
|
123 | 123 | |
124 | - return new JSONResponse([ |
|
125 | - 'status' => 'success', |
|
126 | - 'data' => [ |
|
127 | - 'message' => $this->l->t('Saved'), |
|
128 | - ], |
|
129 | - ]); |
|
130 | - } |
|
124 | + return new JSONResponse([ |
|
125 | + 'status' => 'success', |
|
126 | + 'data' => [ |
|
127 | + 'message' => $this->l->t('Saved'), |
|
128 | + ], |
|
129 | + ]); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * @NoAdminRequired |
|
134 | - * @PasswordConfirmationRequired |
|
135 | - */ |
|
136 | - public function changeUserPassword(string $username = null, string $password = null, string $recoveryPassword = null): JSONResponse { |
|
137 | - if ($username === null) { |
|
138 | - return new JSONResponse([ |
|
139 | - 'status' => 'error', |
|
140 | - 'data' => [ |
|
141 | - 'message' => $this->l->t('No user supplied'), |
|
142 | - ], |
|
143 | - ]); |
|
144 | - } |
|
132 | + /** |
|
133 | + * @NoAdminRequired |
|
134 | + * @PasswordConfirmationRequired |
|
135 | + */ |
|
136 | + public function changeUserPassword(string $username = null, string $password = null, string $recoveryPassword = null): JSONResponse { |
|
137 | + if ($username === null) { |
|
138 | + return new JSONResponse([ |
|
139 | + 'status' => 'error', |
|
140 | + 'data' => [ |
|
141 | + 'message' => $this->l->t('No user supplied'), |
|
142 | + ], |
|
143 | + ]); |
|
144 | + } |
|
145 | 145 | |
146 | - if ($password === null) { |
|
147 | - return new JSONResponse([ |
|
148 | - 'status' => 'error', |
|
149 | - 'data' => [ |
|
150 | - 'message' => $this->l->t('Unable to change password'), |
|
151 | - ], |
|
152 | - ]); |
|
153 | - } |
|
146 | + if ($password === null) { |
|
147 | + return new JSONResponse([ |
|
148 | + 'status' => 'error', |
|
149 | + 'data' => [ |
|
150 | + 'message' => $this->l->t('Unable to change password'), |
|
151 | + ], |
|
152 | + ]); |
|
153 | + } |
|
154 | 154 | |
155 | - $currentUser = $this->userSession->getUser(); |
|
156 | - $targetUser = $this->userManager->get($username); |
|
157 | - if ($currentUser === null || $targetUser === null || |
|
158 | - !($this->groupManager->isAdmin($this->userId) || |
|
159 | - $this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $targetUser)) |
|
160 | - ) { |
|
161 | - return new JSONResponse([ |
|
162 | - 'status' => 'error', |
|
163 | - 'data' => [ |
|
164 | - 'message' => $this->l->t('Authentication error'), |
|
165 | - ], |
|
166 | - ]); |
|
167 | - } |
|
155 | + $currentUser = $this->userSession->getUser(); |
|
156 | + $targetUser = $this->userManager->get($username); |
|
157 | + if ($currentUser === null || $targetUser === null || |
|
158 | + !($this->groupManager->isAdmin($this->userId) || |
|
159 | + $this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $targetUser)) |
|
160 | + ) { |
|
161 | + return new JSONResponse([ |
|
162 | + 'status' => 'error', |
|
163 | + 'data' => [ |
|
164 | + 'message' => $this->l->t('Authentication error'), |
|
165 | + ], |
|
166 | + ]); |
|
167 | + } |
|
168 | 168 | |
169 | - if ($this->appManager->isEnabledForUser('encryption')) { |
|
170 | - //handle the recovery case |
|
171 | - $crypt = new \OCA\Encryption\Crypto\Crypt( |
|
172 | - \OC::$server->getLogger(), |
|
173 | - \OC::$server->getUserSession(), |
|
174 | - \OC::$server->getConfig(), |
|
175 | - \OC::$server->getL10N('encryption')); |
|
176 | - $keyStorage = \OC::$server->getEncryptionKeyStorage(); |
|
177 | - $util = new \OCA\Encryption\Util( |
|
178 | - new \OC\Files\View(), |
|
179 | - $crypt, |
|
180 | - \OC::$server->getLogger(), |
|
181 | - \OC::$server->getUserSession(), |
|
182 | - \OC::$server->getConfig(), |
|
183 | - \OC::$server->getUserManager()); |
|
184 | - $keyManager = new \OCA\Encryption\KeyManager( |
|
185 | - $keyStorage, |
|
186 | - $crypt, |
|
187 | - \OC::$server->getConfig(), |
|
188 | - \OC::$server->getUserSession(), |
|
189 | - new \OCA\Encryption\Session(\OC::$server->getSession()), |
|
190 | - \OC::$server->getLogger(), |
|
191 | - $util, |
|
192 | - \OC::$server->getLockingProvider() |
|
193 | - ); |
|
194 | - $recovery = new \OCA\Encryption\Recovery( |
|
195 | - \OC::$server->getUserSession(), |
|
196 | - $crypt, |
|
197 | - $keyManager, |
|
198 | - \OC::$server->getConfig(), |
|
199 | - \OC::$server->getEncryptionFilesHelper(), |
|
200 | - new \OC\Files\View()); |
|
201 | - $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled(); |
|
169 | + if ($this->appManager->isEnabledForUser('encryption')) { |
|
170 | + //handle the recovery case |
|
171 | + $crypt = new \OCA\Encryption\Crypto\Crypt( |
|
172 | + \OC::$server->getLogger(), |
|
173 | + \OC::$server->getUserSession(), |
|
174 | + \OC::$server->getConfig(), |
|
175 | + \OC::$server->getL10N('encryption')); |
|
176 | + $keyStorage = \OC::$server->getEncryptionKeyStorage(); |
|
177 | + $util = new \OCA\Encryption\Util( |
|
178 | + new \OC\Files\View(), |
|
179 | + $crypt, |
|
180 | + \OC::$server->getLogger(), |
|
181 | + \OC::$server->getUserSession(), |
|
182 | + \OC::$server->getConfig(), |
|
183 | + \OC::$server->getUserManager()); |
|
184 | + $keyManager = new \OCA\Encryption\KeyManager( |
|
185 | + $keyStorage, |
|
186 | + $crypt, |
|
187 | + \OC::$server->getConfig(), |
|
188 | + \OC::$server->getUserSession(), |
|
189 | + new \OCA\Encryption\Session(\OC::$server->getSession()), |
|
190 | + \OC::$server->getLogger(), |
|
191 | + $util, |
|
192 | + \OC::$server->getLockingProvider() |
|
193 | + ); |
|
194 | + $recovery = new \OCA\Encryption\Recovery( |
|
195 | + \OC::$server->getUserSession(), |
|
196 | + $crypt, |
|
197 | + $keyManager, |
|
198 | + \OC::$server->getConfig(), |
|
199 | + \OC::$server->getEncryptionFilesHelper(), |
|
200 | + new \OC\Files\View()); |
|
201 | + $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled(); |
|
202 | 202 | |
203 | - $validRecoveryPassword = false; |
|
204 | - $recoveryEnabledForUser = false; |
|
205 | - if ($recoveryAdminEnabled) { |
|
206 | - $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword); |
|
207 | - $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username); |
|
208 | - } |
|
203 | + $validRecoveryPassword = false; |
|
204 | + $recoveryEnabledForUser = false; |
|
205 | + if ($recoveryAdminEnabled) { |
|
206 | + $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword); |
|
207 | + $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username); |
|
208 | + } |
|
209 | 209 | |
210 | - if ($recoveryEnabledForUser && $recoveryPassword === '') { |
|
211 | - return new JSONResponse([ |
|
212 | - 'status' => 'error', |
|
213 | - 'data' => [ |
|
214 | - 'message' => $this->l->t('Please provide an admin recovery password; otherwise, all user data will be lost.'), |
|
215 | - ] |
|
216 | - ]); |
|
217 | - } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) { |
|
218 | - return new JSONResponse([ |
|
219 | - 'status' => 'error', |
|
220 | - 'data' => [ |
|
221 | - 'message' => $this->l->t('Wrong admin recovery password. Please check the password and try again.'), |
|
222 | - ] |
|
223 | - ]); |
|
224 | - } else { // now we know that everything is fine regarding the recovery password, let's try to change the password |
|
225 | - try { |
|
226 | - $result = $targetUser->setPassword($password, $recoveryPassword); |
|
227 | - // password policy app throws exception |
|
228 | - } catch (HintException $e) { |
|
229 | - return new JSONResponse([ |
|
230 | - 'status' => 'error', |
|
231 | - 'data' => [ |
|
232 | - 'message' => $e->getHint(), |
|
233 | - ], |
|
234 | - ]); |
|
235 | - } |
|
236 | - if (!$result && $recoveryEnabledForUser) { |
|
237 | - return new JSONResponse([ |
|
238 | - 'status' => 'error', |
|
239 | - 'data' => [ |
|
240 | - 'message' => $this->l->t('Backend doesn\'t support password change, but the user\'s encryption key was updated.'), |
|
241 | - ] |
|
242 | - ]); |
|
243 | - } elseif (!$result && !$recoveryEnabledForUser) { |
|
244 | - return new JSONResponse([ |
|
245 | - 'status' => 'error', |
|
246 | - 'data' => [ |
|
247 | - 'message' => $this->l->t('Unable to change password'), |
|
248 | - ] |
|
249 | - ]); |
|
250 | - } |
|
251 | - } |
|
252 | - } else { |
|
253 | - try { |
|
254 | - if ($targetUser->setPassword($password) === false) { |
|
255 | - return new JSONResponse([ |
|
256 | - 'status' => 'error', |
|
257 | - 'data' => [ |
|
258 | - 'message' => $this->l->t('Unable to change password'), |
|
259 | - ], |
|
260 | - ]); |
|
261 | - } |
|
262 | - // password policy app throws exception |
|
263 | - } catch (HintException $e) { |
|
264 | - return new JSONResponse([ |
|
265 | - 'status' => 'error', |
|
266 | - 'data' => [ |
|
267 | - 'message' => $e->getHint(), |
|
268 | - ], |
|
269 | - ]); |
|
270 | - } |
|
271 | - } |
|
210 | + if ($recoveryEnabledForUser && $recoveryPassword === '') { |
|
211 | + return new JSONResponse([ |
|
212 | + 'status' => 'error', |
|
213 | + 'data' => [ |
|
214 | + 'message' => $this->l->t('Please provide an admin recovery password; otherwise, all user data will be lost.'), |
|
215 | + ] |
|
216 | + ]); |
|
217 | + } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) { |
|
218 | + return new JSONResponse([ |
|
219 | + 'status' => 'error', |
|
220 | + 'data' => [ |
|
221 | + 'message' => $this->l->t('Wrong admin recovery password. Please check the password and try again.'), |
|
222 | + ] |
|
223 | + ]); |
|
224 | + } else { // now we know that everything is fine regarding the recovery password, let's try to change the password |
|
225 | + try { |
|
226 | + $result = $targetUser->setPassword($password, $recoveryPassword); |
|
227 | + // password policy app throws exception |
|
228 | + } catch (HintException $e) { |
|
229 | + return new JSONResponse([ |
|
230 | + 'status' => 'error', |
|
231 | + 'data' => [ |
|
232 | + 'message' => $e->getHint(), |
|
233 | + ], |
|
234 | + ]); |
|
235 | + } |
|
236 | + if (!$result && $recoveryEnabledForUser) { |
|
237 | + return new JSONResponse([ |
|
238 | + 'status' => 'error', |
|
239 | + 'data' => [ |
|
240 | + 'message' => $this->l->t('Backend doesn\'t support password change, but the user\'s encryption key was updated.'), |
|
241 | + ] |
|
242 | + ]); |
|
243 | + } elseif (!$result && !$recoveryEnabledForUser) { |
|
244 | + return new JSONResponse([ |
|
245 | + 'status' => 'error', |
|
246 | + 'data' => [ |
|
247 | + 'message' => $this->l->t('Unable to change password'), |
|
248 | + ] |
|
249 | + ]); |
|
250 | + } |
|
251 | + } |
|
252 | + } else { |
|
253 | + try { |
|
254 | + if ($targetUser->setPassword($password) === false) { |
|
255 | + return new JSONResponse([ |
|
256 | + 'status' => 'error', |
|
257 | + 'data' => [ |
|
258 | + 'message' => $this->l->t('Unable to change password'), |
|
259 | + ], |
|
260 | + ]); |
|
261 | + } |
|
262 | + // password policy app throws exception |
|
263 | + } catch (HintException $e) { |
|
264 | + return new JSONResponse([ |
|
265 | + 'status' => 'error', |
|
266 | + 'data' => [ |
|
267 | + 'message' => $e->getHint(), |
|
268 | + ], |
|
269 | + ]); |
|
270 | + } |
|
271 | + } |
|
272 | 272 | |
273 | - return new JSONResponse([ |
|
274 | - 'status' => 'success', |
|
275 | - 'data' => [ |
|
276 | - 'username' => $username, |
|
277 | - ], |
|
278 | - ]); |
|
279 | - } |
|
273 | + return new JSONResponse([ |
|
274 | + 'status' => 'success', |
|
275 | + 'data' => [ |
|
276 | + 'username' => $username, |
|
277 | + ], |
|
278 | + ]); |
|
279 | + } |
|
280 | 280 | } |