Complex classes like Crypt often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Crypt, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
56 | class Crypt { |
||
57 | |||
58 | const DEFAULT_CIPHER = 'AES-256-CTR'; |
||
59 | // default cipher from old Nextcloud versions |
||
60 | const LEGACY_CIPHER = 'AES-128-CFB'; |
||
61 | |||
62 | // default key format, old Nextcloud version encrypted the private key directly |
||
63 | // with the user password |
||
64 | const LEGACY_KEY_FORMAT = 'password'; |
||
65 | |||
66 | const HEADER_START = 'HBEGIN'; |
||
67 | const HEADER_END = 'HEND'; |
||
68 | |||
69 | /** @var ILogger */ |
||
70 | private $logger; |
||
71 | |||
72 | /** @var string */ |
||
73 | private $user; |
||
74 | |||
75 | /** @var IConfig */ |
||
76 | private $config; |
||
77 | |||
78 | /** @var array */ |
||
79 | private $supportedKeyFormats; |
||
80 | |||
81 | /** @var IL10N */ |
||
82 | private $l; |
||
83 | |||
84 | /** @var array */ |
||
85 | private $supportedCiphersAndKeySize = [ |
||
86 | 'AES-256-CTR' => 32, |
||
87 | 'AES-128-CTR' => 16, |
||
88 | 'AES-256-CFB' => 32, |
||
89 | 'AES-128-CFB' => 16, |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * @param ILogger $logger |
||
94 | * @param IUserSession $userSession |
||
95 | * @param IConfig $config |
||
96 | * @param IL10N $l |
||
97 | */ |
||
98 | public function __construct(ILogger $logger, IUserSession $userSession, IConfig $config, IL10N $l) { |
||
99 | $this->logger = $logger; |
||
100 | $this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : '"no user given"'; |
||
101 | $this->config = $config; |
||
102 | $this->l = $l; |
||
103 | $this->supportedKeyFormats = ['hash', 'password']; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * create new private/public key-pair for user |
||
108 | * |
||
109 | * @return array|bool |
||
110 | */ |
||
111 | public function createKeyPair() { |
||
112 | |||
113 | $log = $this->logger; |
||
114 | $res = $this->getOpenSSLPKey(); |
||
115 | |||
116 | if (!$res) { |
||
117 | $log->error("Encryption Library couldn't generate users key-pair for {$this->user}", |
||
118 | ['app' => 'encryption']); |
||
119 | |||
120 | if (openssl_error_string()) { |
||
121 | $log->error('Encryption library openssl_pkey_new() fails: ' . openssl_error_string(), |
||
122 | ['app' => 'encryption']); |
||
123 | } |
||
124 | } elseif (openssl_pkey_export($res, |
||
125 | $privateKey, |
||
126 | null, |
||
127 | $this->getOpenSSLConfig())) { |
||
128 | $keyDetails = openssl_pkey_get_details($res); |
||
129 | $publicKey = $keyDetails['key']; |
||
130 | |||
131 | return [ |
||
132 | 'publicKey' => $publicKey, |
||
133 | 'privateKey' => $privateKey |
||
134 | ]; |
||
135 | } |
||
136 | $log->error('Encryption library couldn\'t export users private key, please check your servers OpenSSL configuration.' . $this->user, |
||
137 | ['app' => 'encryption']); |
||
138 | if (openssl_error_string()) { |
||
139 | $log->error('Encryption Library:' . openssl_error_string(), |
||
140 | ['app' => 'encryption']); |
||
141 | } |
||
142 | |||
143 | return false; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Generates a new private key |
||
148 | * |
||
149 | * @return resource |
||
150 | */ |
||
151 | public function getOpenSSLPKey() { |
||
152 | $config = $this->getOpenSSLConfig(); |
||
153 | return openssl_pkey_new($config); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * get openSSL Config |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | private function getOpenSSLConfig() { |
||
162 | $config = ['private_key_bits' => 4096]; |
||
163 | $config = array_merge( |
||
164 | $config, |
||
165 | $this->config->getSystemValue('openssl', []) |
||
166 | ); |
||
167 | return $config; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param string $plainContent |
||
172 | * @param string $passPhrase |
||
173 | * @param int $version |
||
174 | * @param int $position |
||
175 | * @return false|string |
||
176 | * @throws EncryptionFailedException |
||
177 | */ |
||
178 | public function symmetricEncryptFileContent($plainContent, $passPhrase, $version, $position) { |
||
179 | |||
180 | if (!$plainContent) { |
||
181 | $this->logger->error('Encryption Library, symmetrical encryption failed no content given', |
||
182 | ['app' => 'encryption']); |
||
183 | return false; |
||
184 | } |
||
185 | |||
186 | $iv = $this->generateIv(); |
||
187 | |||
188 | $encryptedContent = $this->encrypt($plainContent, |
||
189 | $iv, |
||
190 | $passPhrase, |
||
191 | $this->getCipher()); |
||
192 | |||
193 | // Create a signature based on the key as well as the current version |
||
194 | $sig = $this->createSignature($encryptedContent, $passPhrase.$version.$position); |
||
195 | |||
196 | // combine content to encrypt the IV identifier and actual IV |
||
197 | $catFile = $this->concatIV($encryptedContent, $iv); |
||
198 | $catFile = $this->concatSig($catFile, $sig); |
||
199 | return $this->addPadding($catFile); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * generate header for encrypted file |
||
204 | * |
||
205 | * @param string $keyFormat (can be 'hash' or 'password') |
||
206 | * @return string |
||
207 | * @throws \InvalidArgumentException |
||
208 | */ |
||
209 | public function generateHeader($keyFormat = 'hash') { |
||
224 | |||
225 | /** |
||
226 | * @param string $plainContent |
||
227 | * @param string $iv |
||
228 | * @param string $passPhrase |
||
229 | * @param string $cipher |
||
230 | * @return string |
||
231 | * @throws EncryptionFailedException |
||
232 | */ |
||
233 | private function encrypt($plainContent, $iv, $passPhrase = '', $cipher = self::DEFAULT_CIPHER) { |
||
249 | |||
250 | /** |
||
251 | * return Cipher either from config.php or the default cipher defined in |
||
252 | * this class |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | public function getCipher() { |
||
278 | |||
279 | /** |
||
280 | * get key size depending on the cipher |
||
281 | * |
||
282 | * @param string $cipher |
||
283 | * @return int |
||
284 | * @throws \InvalidArgumentException |
||
285 | */ |
||
286 | protected function getKeySize($cipher) { |
||
298 | |||
299 | /** |
||
300 | * get legacy cipher |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | public function getLegacyCipher() { |
||
307 | |||
308 | /** |
||
309 | * @param string $encryptedContent |
||
310 | * @param string $iv |
||
311 | * @return string |
||
312 | */ |
||
313 | private function concatIV($encryptedContent, $iv) { |
||
316 | |||
317 | /** |
||
318 | * @param string $encryptedContent |
||
319 | * @param string $signature |
||
320 | * @return string |
||
321 | */ |
||
322 | private function concatSig($encryptedContent, $signature) { |
||
325 | |||
326 | /** |
||
327 | * Note: This is _NOT_ a padding used for encryption purposes. It is solely |
||
328 | * used to achieve the PHP stream size. It has _NOTHING_ to do with the |
||
329 | * encrypted content and is not used in any crypto primitive. |
||
330 | * |
||
331 | * @param string $data |
||
332 | * @return string |
||
333 | */ |
||
334 | private function addPadding($data) { |
||
337 | |||
338 | /** |
||
339 | * generate password hash used to encrypt the users private key |
||
340 | * |
||
341 | * @param string $password |
||
342 | * @param string $cipher |
||
343 | * @param string $uid only used for user keys |
||
344 | * @return string |
||
345 | */ |
||
346 | protected function generatePasswordHash($password, $cipher, $uid = '') { |
||
363 | |||
364 | /** |
||
365 | * encrypt private key |
||
366 | * |
||
367 | * @param string $privateKey |
||
368 | * @param string $password |
||
369 | * @param string $uid for regular users, empty for system keys |
||
370 | * @return false|string |
||
371 | */ |
||
372 | public function encryptPrivateKey($privateKey, $password, $uid = '') { |
||
384 | |||
385 | /** |
||
386 | * @param string $privateKey |
||
387 | * @param string $password |
||
388 | * @param string $uid for regular users, empty for system keys |
||
389 | * @return false|string |
||
390 | */ |
||
391 | public function decryptPrivateKey($privateKey, $password = '', $uid = '') { |
||
431 | |||
432 | /** |
||
433 | * check if it is a valid private key |
||
434 | * |
||
435 | * @param string $plainKey |
||
436 | * @return bool |
||
437 | */ |
||
438 | protected function isValidPrivateKey($plainKey) { |
||
449 | |||
450 | /** |
||
451 | * @param string $keyFileContents |
||
452 | * @param string $passPhrase |
||
453 | * @param string $cipher |
||
454 | * @param int $version |
||
455 | * @param int $position |
||
456 | * @return string |
||
457 | * @throws DecryptionFailedException |
||
458 | */ |
||
459 | public function symmetricDecryptFileContent($keyFileContents, $passPhrase, $cipher = self::DEFAULT_CIPHER, $version = 0, $position = 0) { |
||
471 | |||
472 | /** |
||
473 | * check for valid signature |
||
474 | * |
||
475 | * @param string $data |
||
476 | * @param string $passPhrase |
||
477 | * @param string $expectedSignature |
||
478 | * @throws GenericEncryptionException |
||
479 | */ |
||
480 | private function checkSignature($data, $passPhrase, $expectedSignature) { |
||
486 | |||
487 | /** |
||
488 | * create signature |
||
489 | * |
||
490 | * @param string $data |
||
491 | * @param string $passPhrase |
||
492 | * @return string |
||
493 | */ |
||
494 | private function createSignature($data, $passPhrase) { |
||
498 | |||
499 | |||
500 | /** |
||
501 | * remove padding |
||
502 | * |
||
503 | * @param string $padded |
||
504 | * @param bool $hasSignature did the block contain a signature, in this case we use a different padding |
||
505 | * @return string|false |
||
506 | */ |
||
507 | private function removePadding($padded, $hasSignature = false) { |
||
515 | |||
516 | /** |
||
517 | * split meta data from encrypted file |
||
518 | * Note: for now, we assume that the meta data always start with the iv |
||
519 | * followed by the signature, if available |
||
520 | * |
||
521 | * @param string $catFile |
||
522 | * @param string $cipher |
||
523 | * @return array |
||
524 | */ |
||
525 | private function splitMetaData($catFile, $cipher) { |
||
546 | |||
547 | /** |
||
548 | * check if encrypted block is signed |
||
549 | * |
||
550 | * @param string $catFile |
||
551 | * @param string $cipher |
||
552 | * @return bool |
||
553 | * @throws GenericEncryptionException |
||
554 | */ |
||
555 | private function hasSignature($catFile, $cipher) { |
||
566 | |||
567 | |||
568 | /** |
||
569 | * @param string $encryptedContent |
||
570 | * @param string $iv |
||
571 | * @param string $passPhrase |
||
572 | * @param string $cipher |
||
573 | * @return string |
||
574 | * @throws DecryptionFailedException |
||
575 | */ |
||
576 | private function decrypt($encryptedContent, $iv, $passPhrase = '', $cipher = self::DEFAULT_CIPHER) { |
||
589 | |||
590 | /** |
||
591 | * @param string $data |
||
592 | * @return array |
||
593 | */ |
||
594 | protected function parseHeader($data) { |
||
615 | |||
616 | /** |
||
617 | * generate initialization vector |
||
618 | * |
||
619 | * @return string |
||
620 | * @throws GenericEncryptionException |
||
621 | */ |
||
622 | private function generateIv() { |
||
625 | |||
626 | /** |
||
627 | * Generate a cryptographically secure pseudo-random 256-bit ASCII key, used |
||
628 | * as file key |
||
629 | * |
||
630 | * @return string |
||
631 | * @throws \Exception |
||
632 | */ |
||
633 | public function generateFileKey() { |
||
636 | |||
637 | /** |
||
638 | * @param $encKeyFile |
||
639 | * @param $shareKey |
||
640 | * @param $privateKey |
||
641 | * @return string |
||
642 | * @throws MultiKeyDecryptException |
||
643 | */ |
||
644 | public function multiKeyDecrypt($encKeyFile, $shareKey, $privateKey) { |
||
655 | |||
656 | /** |
||
657 | * @param string $plainContent |
||
658 | * @param array $keyFiles |
||
659 | * @return array |
||
660 | * @throws MultiKeyEncryptException |
||
661 | */ |
||
662 | public function multiKeyEncrypt($plainContent, array $keyFiles) { |
||
691 | } |
||
692 | |||
693 |