Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/KeyManagement/KeyConverter/ECKey.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\KeyManagement\KeyConverter;
6
7 use InvalidArgumentException;
8 use Jose\Component\Core\Util\Base64UrlSafe;
9 use SpomkyLabs\Pki\CryptoEncoding\PEM;
10 use SpomkyLabs\Pki\CryptoTypes\Asymmetric\EC\ECPrivateKey;
11 use SpomkyLabs\Pki\CryptoTypes\Asymmetric\EC\ECPublicKey;
12 use Throwable;
13 use function array_key_exists;
14 use function is_string;
15
16 /**
17 * @internal
18 */
19 final class ECKey
20 {
21 private array $values = [];
22
23 private function __construct(array $data)
24 {
25 $this->loadJWK($data);
26 }
27
28 public static function createFromPEM(string $pem): self
29 {
30 $data = self::loadPEM($pem);
31
32 return new self($data);
33 }
34
35 public static function toPublic(self $private): self
36 {
37 $data = $private->toArray();
38 if (array_key_exists('d', $data)) {
39 unset($data['d']);
40 }
41
42 return new self($data);
43 }
44
45 /**
46 * @return array
47 */
48 public function toArray()
49 {
50 return $this->values;
51 }
52
53 private static function loadPEM(string $data): array
54 {
55 $pem = PEM::fromString($data);
56 try {
57 $key = ECPrivateKey::fromPEM($pem);
58
59 return [
60 'kty' => 'EC',
61 'crv' => self::getCurve($key->namedCurve()),
62 'd' => Base64UrlSafe::encodeUnpadded($key->privateKeyOctets()),
63 'x' => Base64UrlSafe::encodeUnpadded($key->publicKey()->curvePointOctets()[0]),
64 'y' => Base64UrlSafe::encodeUnpadded($key->publicKey()->curvePointOctets()[1]),
65 ];
66 } catch (Throwable) {
67 }
68 try {
69 $key = ECPublicKey::fromPEM($pem);
70 return [
71 'kty' => 'EC',
72 'crv' => self::getCurve($key->namedCurve()),
73 'x' => Base64UrlSafe::encodeUnpadded($key->curvePointOctets()[0]),
74 'y' => Base64UrlSafe::encodeUnpadded($key->curvePointOctets()[1]),
75 ];
76 } catch (Throwable) {
77 }
78 throw new InvalidArgumentException('Unable to load the key.');
79 }
80
81 private static function getCurve(string $oid): string
82 {
83 $curves = self::getSupportedCurves();
84 $curve = array_search($oid, $curves, true);
85 if (! is_string($curve)) {
86 throw new InvalidArgumentException('Unsupported OID.');
87 }
88
89 return $curve;
90 }
91
92 private static function getSupportedCurves(): array
93 {
94 return [
95 'P-256' => '1.2.840.10045.3.1.7',
96 'P-384' => '1.3.132.0.34',
97 'P-521' => '1.3.132.0.35',
98 ];
99 }
100
101 private function loadJWK(array $jwk): void
102 {
103 $keys = [
104 'kty' => 'The key parameter "kty" is missing.',
105 'crv' => 'Curve parameter is missing',
106 'x' => 'Point parameters are missing.',
107 'y' => 'Point parameters are missing.',
108 ];
109 foreach ($keys as $k => $v) {
110 if (! array_key_exists($k, $jwk)) {
111 throw new InvalidArgumentException($v);
112 }
113 }
114
115 if ($jwk['kty'] !== 'EC') {
116 throw new InvalidArgumentException('JWK is not an Elliptic Curve key.');
117 }
118 $this->values = $jwk;
119 }
120 }