|
11 | 11 |
|
12 | 12 | namespace AcmePhp\Ssl\Generator; |
13 | 13 |
|
| 14 | +use AcmePhp\Ssl\Exception\KeyGenerationException; |
14 | 15 | use AcmePhp\Ssl\Exception\KeyPairGenerationException; |
| 16 | +use AcmePhp\Ssl\Generator\EcKey\EcKeyGenerator; |
| 17 | +use AcmePhp\Ssl\Generator\RsaKey\RsaKeyGenerator; |
| 18 | +use AcmePhp\Ssl\Generator\RsaKey\RsaKeyOption; |
15 | 19 | use AcmePhp\Ssl\KeyPair; |
16 | | -use AcmePhp\Ssl\PrivateKey; |
17 | | -use AcmePhp\Ssl\PublicKey; |
18 | 20 | use Webmozart\Assert\Assert; |
19 | 21 |
|
20 | 22 | /** |
|
24 | 26 | */ |
25 | 27 | class KeyPairGenerator |
26 | 28 | { |
| 29 | + private $generator; |
| 30 | + |
| 31 | + public function __construct(PrivateKeyGeneratorInterface $generator = null) |
| 32 | + { |
| 33 | + $this->generator = $generator ?: new ChainPrivateKeyGenerator( |
| 34 | + [ |
| 35 | + new RsaKeyGenerator(), |
| 36 | + new EcKeyGenerator(), |
| 37 | + ] |
| 38 | + ); |
| 39 | + } |
| 40 | + |
27 | 41 | /** |
28 | 42 | * Generate KeyPair. |
29 | 43 | * |
30 | | - * @param int $keySize size of the key |
| 44 | + * @param KeyOption $keyOption configuration of the key to generate |
31 | 45 | * |
32 | 46 | * @throws KeyPairGenerationException when OpenSSL failed to generate keys |
33 | 47 | * |
34 | 48 | * @return KeyPair |
35 | 49 | */ |
36 | | - public function generateKeyPair($keySize = 4096) |
| 50 | + public function generateKeyPair($keyOption = null) |
37 | 51 | { |
38 | | - Assert::integer($keySize, __METHOD__.'::$keySize should be an integer. Got: %s'); |
39 | | - |
40 | | - $key = openssl_pkey_new( |
41 | | - [ |
42 | | - 'private_key_type' => OPENSSL_KEYTYPE_RSA, |
43 | | - 'private_key_bits' => $keySize, |
44 | | - ] |
45 | | - ); |
46 | | - |
47 | | - if (!$key) { |
48 | | - throw new KeyPairGenerationException( |
49 | | - sprintf( |
50 | | - 'OpenSSL key creation failed during generation with error: %s', |
51 | | - openssl_error_string() |
52 | | - ) |
53 | | - ); |
| 52 | + if (null === $keyOption) { |
| 53 | + $keyOption = new RsaKeyOption(); |
54 | 54 | } |
55 | | - |
56 | | - if (!openssl_pkey_export($key, $privateKey)) { |
57 | | - throw new KeyPairGenerationException( |
58 | | - sprintf( |
59 | | - 'OpenSSL key export failed during generation with error: %s', |
60 | | - openssl_error_string() |
61 | | - ) |
62 | | - ); |
| 55 | + if (\is_int($keyOption)) { |
| 56 | + @trigger_error('Passing a keySize to "generateKeyPair" is deprecated since version 1.1 and will be removed in 2.0. Pass an instance of KeyOption instead', E_USER_DEPRECATED); |
| 57 | + $keyOption = new RsaKeyOption($keyOption); |
63 | 58 | } |
| 59 | + Assert::isInstanceOf($keyOption, KeyOption::class); |
64 | 60 |
|
65 | | - $details = openssl_pkey_get_details($key); |
66 | | - |
67 | | - if (!\is_array($details)) { |
68 | | - throw new KeyPairGenerationException( |
69 | | - sprintf( |
70 | | - 'OpenSSL key parsing failed during generation with error: %s', |
71 | | - openssl_error_string() |
72 | | - ) |
73 | | - ); |
| 61 | + try { |
| 62 | + $privateKey = $this->generator->generatePrivateKey($keyOption); |
| 63 | + } catch (KeyGenerationException $e) { |
| 64 | + throw new KeyPairGenerationException('Fail to generate a KeyPair with the given options', 0, $e); |
74 | 65 | } |
75 | 66 |
|
76 | 67 | return new KeyPair( |
77 | | - new PublicKey($details['key']), |
78 | | - new PrivateKey($privateKey) |
| 68 | + $privateKey->getPublicKey(), |
| 69 | + $privateKey |
79 | 70 | ); |
80 | 71 | } |
81 | 72 | } |
0 commit comments