Complex classes like ECDHES 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 ECDHES, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | final class ECDHES implements KeyAgreement |
||
| 29 | { |
||
| 30 | public function allowedKeyTypes(): array |
||
| 31 | { |
||
| 32 | return ['EC', 'OKP']; |
||
| 33 | } |
||
| 34 | |||
| 35 | public function getAgreementKey(int $encryptionKeyLength, string $algorithm, JWK $recipientKey, ?JWK $senderKey, array $complete_header = [], array &$additional_header_values = []): string |
||
| 36 | { |
||
| 37 | if ($recipientKey->has('d')) { |
||
| 38 | list($public_key, $private_key) = $this->getKeysFromPrivateKeyAndHeader($recipientKey, $complete_header); |
||
| 39 | } else { |
||
| 40 | list($public_key, $private_key) = $this->getKeysFromPublicKey($recipientKey, $additional_header_values); |
||
| 41 | } |
||
| 42 | |||
| 43 | $agreed_key = $this->calculateAgreementKey($private_key, $public_key); |
||
| 44 | |||
| 45 | $apu = \array_key_exists('apu', $complete_header) ? $complete_header['apu'] : ''; |
||
| 46 | $apv = \array_key_exists('apv', $complete_header) ? $complete_header['apv'] : ''; |
||
| 47 | |||
| 48 | return ConcatKDF::generate($agreed_key, $algorithm, $encryptionKeyLength, $apu, $apv); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function calculateAgreementKey(JWK $private_key, JWK $public_key): string |
||
| 52 | { |
||
| 53 | switch ($public_key->get('crv')) { |
||
| 54 | case 'P-256': |
||
| 55 | case 'P-384': |
||
| 56 | case 'P-521': |
||
| 57 | $curve = $this->getCurve($public_key->get('crv')); |
||
| 58 | if (\function_exists('openssl_pkey_derive')) { |
||
| 59 | try { |
||
| 60 | $publicPem = ECKey::convertPublicKeyToPEM($public_key); |
||
| 61 | $privatePem = ECKey::convertPrivateKeyToPEM($private_key); |
||
| 62 | |||
| 63 | return openssl_pkey_derive($publicPem, $privatePem, $curve->getSize()); |
||
| 64 | } catch (\Throwable $throwable) { |
||
| 65 | //Does nothing. Will fallback to the pure PHP function |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | $rec_x = $this->convertBase64ToGmp($public_key->get('x')); |
||
| 70 | $rec_y = $this->convertBase64ToGmp($public_key->get('y')); |
||
| 71 | $sen_d = $this->convertBase64ToGmp($private_key->get('d')); |
||
| 72 | |||
| 73 | $priv_key = PrivateKey::create($sen_d); |
||
| 74 | $pub_key = $curve->getPublicKeyFrom($rec_x, $rec_y); |
||
| 75 | |||
| 76 | return $this->convertDecToBin(EcDH::computeSharedKey($curve, $pub_key, $priv_key)); |
||
| 77 | case 'X25519': |
||
| 78 | $sKey = Base64Url::decode($private_key->get('d')); |
||
| 79 | $recipientPublickey = Base64Url::decode($public_key->get('x')); |
||
| 80 | |||
| 81 | return sodium_crypto_scalarmult($sKey, $recipientPublickey); |
||
| 82 | default: |
||
| 83 | throw new InvalidArgumentException(sprintf('The curve "%s" is not supported', $public_key->get('crv'))); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | public function name(): string |
||
| 88 | { |
||
| 89 | return 'ECDH-ES'; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function getKeyManagementMode(): string |
||
| 93 | { |
||
| 94 | return self::MODE_AGREEMENT; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return JWK[] |
||
| 99 | */ |
||
| 100 | private function getKeysFromPublicKey(JWK $recipient_key, array &$additional_header_values): array |
||
| 101 | { |
||
| 102 | $this->checkKey($recipient_key, false); |
||
| 103 | $public_key = $recipient_key; |
||
| 104 | switch ($public_key->get('crv')) { |
||
| 105 | case 'P-256': |
||
| 106 | case 'P-384': |
||
| 107 | case 'P-521': |
||
| 108 | $private_key = ECKey::createECKey($public_key->get('crv')); |
||
| 109 | |||
| 110 | break; |
||
| 111 | case 'X25519': |
||
| 112 | $this->checkSodiumExtensionIsAvailable(); |
||
| 113 | $private_key = $this->createOKPKey('X25519'); |
||
| 114 | |||
| 115 | break; |
||
| 116 | default: |
||
| 117 | throw new InvalidArgumentException(sprintf('The curve "%s" is not supported', $public_key->get('crv'))); |
||
| 118 | } |
||
| 119 | $epk = $private_key->toPublic()->all(); |
||
| 120 | $additional_header_values['epk'] = $epk; |
||
| 121 | |||
| 122 | return [$public_key, $private_key]; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return JWK[] |
||
| 127 | */ |
||
| 128 | private function getKeysFromPrivateKeyAndHeader(JWK $recipient_key, array $complete_header): array |
||
| 129 | { |
||
| 130 | $this->checkKey($recipient_key, true); |
||
| 131 | $private_key = $recipient_key; |
||
| 132 | $public_key = $this->getPublicKey($complete_header); |
||
| 133 | if ($private_key->get('crv') !== $public_key->get('crv')) { |
||
| 134 | throw new InvalidArgumentException('Curves are different'); |
||
| 135 | } |
||
| 136 | |||
| 137 | return [$public_key, $private_key]; |
||
| 138 | } |
||
| 139 | |||
| 140 | private function getPublicKey(array $complete_header): JWK |
||
| 141 | { |
||
| 142 | if (!isset($complete_header['epk'])) { |
||
| 143 | throw new InvalidArgumentException('The header parameter "epk" is missing.'); |
||
| 144 | } |
||
| 145 | if (!\is_array($complete_header['epk'])) { |
||
| 146 | throw new InvalidArgumentException('The header parameter "epk" is not an array of parameters'); |
||
| 147 | } |
||
| 148 | $public_key = new JWK($complete_header['epk']); |
||
| 149 | $this->checkKey($public_key, false); |
||
| 150 | |||
| 151 | return $public_key; |
||
| 152 | } |
||
| 153 | |||
| 154 | private function checkKey(JWK $key, bool $is_private): void |
||
| 155 | { |
||
| 156 | if (!\in_array($key->get('kty'), $this->allowedKeyTypes(), true)) { |
||
| 157 | throw new InvalidArgumentException('Wrong key type.'); |
||
| 158 | } |
||
| 159 | foreach (['x', 'crv'] as $k) { |
||
| 160 | if (!$key->has($k)) { |
||
| 161 | throw new InvalidArgumentException(sprintf('The key parameter "%s" is missing.', $k)); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | switch ($key->get('crv')) { |
||
| 166 | case 'P-256': |
||
| 167 | case 'P-384': |
||
| 168 | case 'P-521': |
||
| 169 | if (!$key->has('y')) { |
||
| 170 | throw new InvalidArgumentException('The key parameter "y" is missing.'); |
||
| 171 | } |
||
| 172 | |||
| 173 | break; |
||
| 174 | case 'X25519': |
||
| 175 | break; |
||
| 176 | default: |
||
| 177 | throw new InvalidArgumentException(sprintf('The curve "%s" is not supported', $key->get('crv'))); |
||
| 178 | } |
||
| 179 | if (true === $is_private && !$key->has('d')) { |
||
| 180 | throw new InvalidArgumentException('The key parameter "d" is missing.'); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | private function getCurve(string $crv): Curve |
||
| 197 | |||
| 198 | private function convertBase64ToGmp(string $value): GMP |
||
| 204 | |||
| 205 | private function convertDecToBin(GMP $dec): string |
||
| 206 | { |
||
| 207 | if (gmp_cmp($dec, 0) < 0) { |
||
| 208 | throw new \InvalidArgumentException('Unable to convert negative integer to string'); |
||
| 209 | } |
||
| 210 | $hex = gmp_strval($dec, 16); |
||
| 211 | |||
| 212 | if (0 !== mb_strlen($hex, '8bit') % 2) { |
||
| 213 | $hex = '0'.$hex; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param string $curve The curve |
||
| 221 | */ |
||
| 222 | private function createOKPKey(string $curve): JWK |
||
| 249 | |||
| 250 | private function checkSodiumExtensionIsAvailable(): void |
||
| 256 | } |
||
| 257 |