|
13 | 13 |
|
14 | 14 | use Assert\Assertion; |
15 | 15 | use Base64Url\Base64Url; |
| 16 | +use Jose\KeyConverter\ECKey; |
16 | 17 | use Jose\KeyConverter\KeyConverter; |
17 | 18 | use Jose\KeyConverter\RSAKey; |
18 | 19 | use Jose\Object\JKUJWKSet; |
@@ -119,20 +120,38 @@ public static function createECKey(array $values) |
119 | 120 | { |
120 | 121 | Assertion::keyExists($values, 'crv', 'The curve is not set.'); |
121 | 122 | $curve = $values['crv']; |
122 | | - $curve_name = self::getNistName($curve); |
123 | | - $generator = CurveFactory::getGeneratorByName($curve_name); |
124 | | - $private_key = $generator->createPrivateKey(); |
125 | | - |
126 | | - $values = array_merge( |
127 | | - $values, |
128 | | - [ |
129 | | - 'kty' => 'EC', |
130 | | - 'crv' => $curve, |
131 | | - 'x' => self::encodeValue($private_key->getPublicKey()->getPoint()->getX()), |
132 | | - 'y' => self::encodeValue($private_key->getPublicKey()->getPoint()->getY()), |
133 | | - 'd' => self::encodeValue($private_key->getSecret()), |
134 | | - ] |
135 | | - ); |
| 123 | + if (function_exists('openssl_get_curve_names')) { |
| 124 | + $args = [ |
| 125 | + 'curve_name' => self::getOpensslName($curve), |
| 126 | + 'private_key_type' => OPENSSL_KEYTYPE_EC, |
| 127 | + ]; |
| 128 | + $key = openssl_pkey_new($args); |
| 129 | + $res = openssl_pkey_export($key, $out); |
| 130 | + Assertion::true($res, 'Unable to create the key'); |
| 131 | + |
| 132 | + $rsa = new ECKey($out); |
| 133 | + $values = array_merge( |
| 134 | + $values, |
| 135 | + $rsa->toArray() |
| 136 | + ); |
| 137 | + |
| 138 | + return new JWK($values); |
| 139 | + } else { |
| 140 | + $curve_name = self::getNistName($curve); |
| 141 | + $generator = CurveFactory::getGeneratorByName($curve_name); |
| 142 | + $private_key = $generator->createPrivateKey(); |
| 143 | + |
| 144 | + $values = array_merge( |
| 145 | + $values, |
| 146 | + [ |
| 147 | + 'kty' => 'EC', |
| 148 | + 'crv' => $curve, |
| 149 | + 'x' => self::encodeValue($private_key->getPublicKey()->getPoint()->getX()), |
| 150 | + 'y' => self::encodeValue($private_key->getPublicKey()->getPoint()->getY()), |
| 151 | + 'd' => self::encodeValue($private_key->getSecret()), |
| 152 | + ] |
| 153 | + ); |
| 154 | + } |
136 | 155 |
|
137 | 156 | return new JWK($values); |
138 | 157 | } |
@@ -233,6 +252,27 @@ private static function convertDecToBin($value) |
233 | 252 | return hex2bin($adapter->decHex($value)); |
234 | 253 | } |
235 | 254 |
|
| 255 | + /** |
| 256 | + * @param string $curve |
| 257 | + * |
| 258 | + * @throws \InvalidArgumentException |
| 259 | + * |
| 260 | + * @return string |
| 261 | + */ |
| 262 | + private static function getOpensslName($curve) |
| 263 | + { |
| 264 | + switch ($curve) { |
| 265 | + case 'P-256': |
| 266 | + return 'prime256v1'; |
| 267 | + case 'P-384': |
| 268 | + return 'secp384r1'; |
| 269 | + case 'P-521': |
| 270 | + return 'secp521r1'; |
| 271 | + default: |
| 272 | + throw new \InvalidArgumentException(sprintf('The curve "%s" is not supported.', $curve)); |
| 273 | + } |
| 274 | + } |
| 275 | + |
236 | 276 | /** |
237 | 277 | * @param string $curve |
238 | 278 | * |
|
0 commit comments