|
7 | 7 | use function array_key_exists; |
8 | 8 | use function count; |
9 | 9 | use function extension_loaded; |
| 10 | +use FG\ASN1\Universal\BitString; |
| 11 | +use FG\ASN1\Universal\Integer; |
| 12 | +use FG\ASN1\Universal\ObjectIdentifier; |
| 13 | +use FG\ASN1\Universal\OctetString; |
| 14 | +use FG\ASN1\Universal\Sequence; |
10 | 15 | use InvalidArgumentException; |
11 | 16 | use function is_array; |
12 | 17 | use function is_string; |
13 | 18 | use const OPENSSL_KEYTYPE_EC; |
14 | 19 | use const OPENSSL_KEYTYPE_RSA; |
15 | 20 | use const OPENSSL_RAW_DATA; |
16 | 21 | use OpenSSLCertificate; |
| 22 | +use ParagonIE\ConstantTime\Base64; |
17 | 23 | use ParagonIE\ConstantTime\Base64UrlSafe; |
18 | 24 | use const PHP_EOL; |
19 | 25 | use const PREG_PATTERN_ORDER; |
@@ -185,20 +191,131 @@ private static function loadKeyFromPEM(string $pem, ?string $password = null): a |
185 | 191 | throw new InvalidArgumentException('Unable to get details of the key'); |
186 | 192 | } |
187 | 193 |
|
188 | | - switch ($details['type']) { |
189 | | - case OPENSSL_KEYTYPE_EC: |
190 | | - $ec_key = ECKey::createFromPEM($pem); |
| 194 | + return match ($details['type']) { |
| 195 | + OPENSSL_KEYTYPE_EC => ECKey::createFromPEM($pem)->toArray(), |
| 196 | + OPENSSL_KEYTYPE_RSA => RSAKey::createFromPEM($pem)->toArray(), |
| 197 | + -1 => self::tryToLoadOtherKeyTypes($pem), |
| 198 | + default => throw new InvalidArgumentException('Unsupported key type'), |
| 199 | + }; |
| 200 | + } |
191 | 201 |
|
192 | | - return $ec_key->toArray(); |
| 202 | + /** |
| 203 | + * This method tries to load Ed448, X488, Ed25519 and X25519 keys. |
| 204 | + */ |
| 205 | + private static function tryToLoadOtherKeyTypes(string $pem): array |
| 206 | + { |
| 207 | + try { |
| 208 | + preg_match_all('#(-.*-)#', $pem, $matches, PREG_PATTERN_ORDER); |
| 209 | + $data = preg_replace('#-.*-|\r|\n| #', '', $pem); |
| 210 | + if (! is_string($data)) { |
| 211 | + throw new InvalidArgumentException('Unsupported key type'); |
| 212 | + } |
| 213 | + $der = Base64::decode($data); |
| 214 | + $sequence = Sequence::fromBinary($der); |
| 215 | + if (! $sequence instanceof Sequence) { |
| 216 | + throw new InvalidArgumentException('Unsupported key type'); |
| 217 | + } |
193 | 218 |
|
194 | | - case OPENSSL_KEYTYPE_RSA: |
195 | | - $rsa_key = RSAKey::createFromPEM($pem); |
| 219 | + return match ($sequence->count()) { |
| 220 | + 2 => self::tryToLoadPublicKeyTypes($sequence), |
| 221 | + 3 => self::tryToLoadPrivateKeyTypes($sequence), |
| 222 | + default => throw new InvalidArgumentException('Unsupported key type'), |
| 223 | + }; |
| 224 | + } catch (Throwable $e) { |
| 225 | + throw new InvalidArgumentException('Unsupported key type', 0, $e); |
| 226 | + } |
| 227 | + } |
196 | 228 |
|
197 | | - return $rsa_key->toArray(); |
| 229 | + /** |
| 230 | + * This method tries to load Ed448 or Ed25519 keys. |
| 231 | + */ |
| 232 | + private static function tryToLoadPublicKeyTypes(Sequence $sequence): array |
| 233 | + { |
| 234 | + [$curveId, $x] = $sequence; |
| 235 | + if (! $curveId instanceof Sequence || $curveId->count() === 0) { |
| 236 | + throw new InvalidArgumentException('Unsupported key type'); |
| 237 | + } |
| 238 | + if (! $x instanceof BitString) { |
| 239 | + throw new InvalidArgumentException('Unsupported key type'); |
| 240 | + } |
| 241 | + $oid = $curveId[0]; |
| 242 | + if (! $oid instanceof ObjectIdentifier) { |
| 243 | + throw new InvalidArgumentException('Unsupported key type'); |
| 244 | + } |
| 245 | + $curve = $oid->getContent(); |
| 246 | + if (! is_string($curve)) { |
| 247 | + throw new InvalidArgumentException('Unsupported key type'); |
| 248 | + } |
198 | 249 |
|
199 | | - default: |
200 | | - throw new InvalidArgumentException('Unsupported key type'); |
| 250 | + return [ |
| 251 | + 'kty' => 'OKP', |
| 252 | + 'crv' => self::getCurve($curve), |
| 253 | + 'x' => Base64UrlSafe::encodeUnpadded($x->getBinaryContent()), |
| 254 | + ]; |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * This method tries to load X448 or X25519 keys. |
| 259 | + */ |
| 260 | + private static function tryToLoadPrivateKeyTypes(Sequence $sequence): array |
| 261 | + { |
| 262 | + [$version, $curveId, $octetD] = $sequence; |
| 263 | + if ($version instanceof Integer && $version->getContent() !== '0') { |
| 264 | + throw new InvalidArgumentException('Unsupported key type'); |
| 265 | + } |
| 266 | + if (! $curveId instanceof Sequence || $curveId->count() === 0) { |
| 267 | + throw new InvalidArgumentException('Unsupported key type'); |
| 268 | + } |
| 269 | + if (! $octetD instanceof OctetString) { |
| 270 | + throw new InvalidArgumentException('Unsupported key type'); |
| 271 | + } |
| 272 | + $oid = $curveId[0]; |
| 273 | + if (! $oid instanceof ObjectIdentifier) { |
| 274 | + throw new InvalidArgumentException('Unsupported key type'); |
| 275 | + } |
| 276 | + $curve = $oid->getContent(); |
| 277 | + if (! is_string($curve)) { |
| 278 | + throw new InvalidArgumentException('Unsupported key type'); |
| 279 | + } |
| 280 | + $crv = self::getCurve($curve); |
| 281 | + $binOctetdD = $octetD->getBinaryContent(); |
| 282 | + $d = OctetString::fromBinary($binOctetdD); |
| 283 | + $d = $d->getContent(); |
| 284 | + if (! is_string($d)) { |
| 285 | + throw new InvalidArgumentException('Unsupported key type'); |
| 286 | + } |
| 287 | + $dBin = hex2bin($d); |
| 288 | + if (! is_string($dBin)) { |
| 289 | + throw new InvalidArgumentException('Unsupported key type'); |
201 | 290 | } |
| 291 | + |
| 292 | + $data = [ |
| 293 | + 'kty' => 'OKP', |
| 294 | + 'crv' => $crv, |
| 295 | + 'd' => Base64UrlSafe::encodeUnpadded($dBin), |
| 296 | + ]; |
| 297 | + |
| 298 | + if (($crv === 'Ed25519' || $crv === 'X25519') && extension_loaded('sodium')) { |
| 299 | + $data['x'] = Base64UrlSafe::encodeUnpadded(sodium_crypto_sign_publickey_from_secretkey($d)); |
| 300 | + } |
| 301 | + |
| 302 | + return $data; |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * This method modifies the PEM to get 64 char lines and fix bug with old OpenSSL versions. |
| 307 | + */ |
| 308 | + private static function getCurve(string $oid): string |
| 309 | + { |
| 310 | + return match ($oid) { |
| 311 | + '1.3.101.115' => 'Ed448ph', |
| 312 | + '1.3.101.114' => 'Ed25519ph', |
| 313 | + '1.3.101.113' => 'Ed448', |
| 314 | + '1.3.101.112' => 'Ed25519', |
| 315 | + '1.3.101.111' => 'X448', |
| 316 | + '1.3.101.110' => 'X25519', |
| 317 | + default => throw new InvalidArgumentException('Unsupported key type.'), |
| 318 | + }; |
202 | 319 | } |
203 | 320 |
|
204 | 321 | /** |
|
0 commit comments