Debian Codegraph @

lib/3rdParty/composer/web-auth/cose-lib/src/Key/Ec2Key.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Cose\Key;
6
7 use function array_key_exists;
8 use function in_array;
9 use InvalidArgumentException;
10 use function is_int;
11 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
12 use SpomkyLabs\Pki\ASN1\Type\Primitive\BitString;
13 use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer;
14 use SpomkyLabs\Pki\ASN1\Type\Primitive\ObjectIdentifier;
15 use SpomkyLabs\Pki\ASN1\Type\Primitive\OctetString;
16 use SpomkyLabs\Pki\ASN1\Type\Tagged\ExplicitlyTaggedType;
17 use function sprintf;
18 use function strlen;
19
20 /**
21 * @final
22 * @see \Cose\Tests\Key\Ec2KeyTest
23 */
24 class Ec2Key extends Key
25 {
26 final public const CURVE_P256 = 1;
27
28 final public const CURVE_P256K = 8;
29
30 final public const CURVE_P384 = 2;
31
32 final public const CURVE_P521 = 3;
33
34 final public const CURVE_NAME_P256 = 'P-256';
35
36 final public const CURVE_NAME_P256K = 'P-256K';
37
38 final public const CURVE_NAME_P384 = 'P-384';
39
40 final public const CURVE_NAME_P521 = 'P-521';
41
42 final public const DATA_CURVE = -1;
43
44 final public const DATA_X = -2;
45
46 final public const DATA_Y = -3;
47
48 final public const DATA_D = -4;
49
50 private const SUPPORTED_CURVES_INT = [self::CURVE_P256, self::CURVE_P256K, self::CURVE_P384, self::CURVE_P521];
51
52 private const SUPPORTED_CURVES_NAMES = [
53 self::CURVE_NAME_P256,
54 self::CURVE_NAME_P256K,
55 self::CURVE_NAME_P384,
56 self::CURVE_NAME_P521,
57 ];
58
59 private const NAMED_CURVE_OID = [
60 self::CURVE_P256 => '1.2.840.10045.3.1.7',
61 // NIST P-256 / secp256r1
62 self::CURVE_P256K => '1.3.132.0.10',
63 // NIST P-256K / secp256k1
64 self::CURVE_P384 => '1.3.132.0.34',
65 // NIST P-384 / secp384r1
66 self::CURVE_P521 => '1.3.132.0.35',
67 // NIST P-521 / secp521r1
68 ];
69
70 private const CURVE_KEY_LENGTH = [
71 self::CURVE_P256 => 32,
72 self::CURVE_P256K => 32,
73 self::CURVE_P384 => 48,
74 self::CURVE_P521 => 66,
75 self::CURVE_NAME_P256 => 32,
76 self::CURVE_NAME_P256K => 32,
77 self::CURVE_NAME_P384 => 48,
78 self::CURVE_NAME_P521 => 66,
79 ];
80
81 /**
82 * @param array<int|string, mixed> $data
83 */
84 public function __construct(array $data)
85 {
86 foreach ([self::DATA_CURVE, self::TYPE] as $key) {
87 if (is_numeric($data[$key])) {
88 $data[$key] = (int) $data[$key];
89 }
90 }
91 parent::__construct($data);
92 if ($data[self::TYPE] !== self::TYPE_EC2 && $data[self::TYPE] !== self::TYPE_NAME_EC2) {
93 throw new InvalidArgumentException('Invalid EC2 key. The key type does not correspond to an EC2 key');
94 }
95 if (! isset($data[self::DATA_CURVE], $data[self::DATA_X], $data[self::DATA_Y])) {
96 throw new InvalidArgumentException('Invalid EC2 key. The curve or the "x/y" coordinates are missing');
97 }
98 if (strlen((string) $data[self::DATA_X]) !== self::CURVE_KEY_LENGTH[$data[self::DATA_CURVE]]) {
99 throw new InvalidArgumentException('Invalid length for x coordinate');
100 }
101 if (strlen((string) $data[self::DATA_Y]) !== self::CURVE_KEY_LENGTH[$data[self::DATA_CURVE]]) {
102 throw new InvalidArgumentException('Invalid length for y coordinate');
103 }
104 if (is_int($data[self::DATA_CURVE])) {
105 if (! in_array($data[self::DATA_CURVE], self::SUPPORTED_CURVES_INT, true)) {
106 throw new InvalidArgumentException('The curve is not supported');
107 }
108 } elseif (! in_array($data[self::DATA_CURVE], self::SUPPORTED_CURVES_NAMES, true)) {
109 throw new InvalidArgumentException('The curve is not supported');
110 }
111 }
112
113 /**
114 * @param array<int|string, mixed> $data
115 */
116 public static function create(array $data): self
117 {
118 return new self($data);
119 }
120
121 public function toPublic(): self
122 {
123 $data = $this->getData();
124 unset($data[self::DATA_D]);
125
126 return new self($data);
127 }
128
129 public function x(): string
130 {
131 return $this->get(self::DATA_X);
132 }
133
134 public function y(): string
135 {
136 return $this->get(self::DATA_Y);
137 }
138
139 public function isPrivate(): bool
140 {
141 return array_key_exists(self::DATA_D, $this->getData());
142 }
143
144 public function d(): string
145 {
146 if (! $this->isPrivate()) {
147 throw new InvalidArgumentException('The key is not private.');
148 }
149 return $this->get(self::DATA_D);
150 }
151
152 public function curve(): int|string
153 {
154 return $this->get(self::DATA_CURVE);
155 }
156
157 public function asPEM(): string
158 {
159 if ($this->isPrivate()) {
160 $der = Sequence::create(
161 Integer::create(1),
162 OctetString::create($this->d()),
163 ExplicitlyTaggedType::create(0, ObjectIdentifier::create($this->getCurveOid())),
164 ExplicitlyTaggedType::create(1, BitString::create($this->getUncompressedCoordinates())),
165 );
166
167 return $this->pem('EC PRIVATE KEY', $der->toDER());
168 }
169
170 $der = Sequence::create(
171 Sequence::create(
172 ObjectIdentifier::create('1.2.840.10045.2.1'),
173 ObjectIdentifier::create($this->getCurveOid())
174 ),
175 BitString::create($this->getUncompressedCoordinates())
176 );
177
178 return $this->pem('PUBLIC KEY', $der->toDER());
179 }
180
181 public function getUncompressedCoordinates(): string
182 {
183 return "\x04" . $this->x() . $this->y();
184 }
185
186 private function getCurveOid(): string
187 {
188 return self::NAMED_CURVE_OID[$this->curve()];
189 }
190
191 private function pem(string $type, string $der): string
192 {
193 return sprintf("-----BEGIN %s-----\n", strtoupper($type)) .
194 chunk_split(base64_encode($der), 64, "\n") .
195 sprintf("-----END %s-----\n", strtoupper($type));
196 }
197 }