PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.5.1
Defender Security – Malware Scanner, Login Security & Firewall v5.5.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / web-auth / cose-lib / src / Key / Ec2Key.php

Ec2Key.php in Defender Security – Malware Scanner, Login Security & Firewall 5.5.1, at vendor/web-auth/cose-lib/src/Key/Ec2Key.php

146 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 /*
6 * The MIT License (MIT)
7 *
8 * Copyright (c) 2014-2021 Spomky-Labs
9 *
10 * This software may be modified and distributed under the terms
11 * of the MIT license. See the LICENSE file for details.
12 */
13
14 namespace Cose\Key;
15
16 use function array_key_exists;
17 use Assert\Assertion;
18 use FG\ASN1\ExplicitlyTaggedObject;
19 use FG\ASN1\Universal\BitString;
20 use FG\ASN1\Universal\Integer;
21 use FG\ASN1\Universal\ObjectIdentifier;
22 use FG\ASN1\Universal\OctetString;
23 use FG\ASN1\Universal\Sequence;
24
25 class Ec2Key extends Key
26 {
27 public const CURVE_P256 = 1;
28 public const CURVE_P256K = 8;
29 public const CURVE_P384 = 2;
30 public const CURVE_P521 = 3;
31
32 public const DATA_CURVE = -1;
33 public const DATA_X = -2;
34 public const DATA_Y = -3;
35 public const DATA_D = -4;
36
37 private const SUPPORTED_CURVES = [
38 self::CURVE_P256,
39 self::CURVE_P256K,
40 self::CURVE_P384,
41 self::CURVE_P521,
42 ];
43
44 private const NAMED_CURVE_OID = [
45 self::CURVE_P256 => '1.2.840.10045.3.1.7', // NIST P-256 / secp256r1
46 self::CURVE_P256K => '1.3.132.0.10', // NIST P-256K / secp256k1
47 self::CURVE_P384 => '1.3.132.0.34', // NIST P-384 / secp384r1
48 self::CURVE_P521 => '1.3.132.0.35', // NIST P-521 / secp521r1
49 ];
50
51 private const CURVE_KEY_LENGTH = [
52 self::CURVE_P256 => 32,
53 self::CURVE_P256K => 32,
54 self::CURVE_P384 => 48,
55 self::CURVE_P521 => 66,
56 ];
57
58 public function __construct(array $data)
59 {
60 parent::__construct($data);
61 Assertion::eq($data[self::TYPE], self::TYPE_EC2, 'Invalid EC2 key. The key type does not correspond to an EC2 key');
62 Assertion::keyExists($data, self::DATA_CURVE, 'Invalid EC2 key. The curve is missing');
63 Assertion::keyExists($data, self::DATA_X, 'Invalid EC2 key. The x coordinate is missing');
64 Assertion::keyExists($data, self::DATA_Y, 'Invalid EC2 key. The y coordinate is missing');
65 Assertion::length($data[self::DATA_X], self::CURVE_KEY_LENGTH[$data[self::DATA_CURVE]], 'Invalid length for x coordinate', null, '8bit');
66 Assertion::length($data[self::DATA_Y], self::CURVE_KEY_LENGTH[$data[self::DATA_CURVE]], 'Invalid length for y coordinate', null, '8bit');
67 Assertion::inArray((int) $data[self::DATA_CURVE], self::SUPPORTED_CURVES, 'The curve is not supported');
68 }
69
70 public function toPublic(): self
71 {
72 $data = $this->getData();
73 unset($data[self::DATA_D]);
74
75 return new self($data);
76 }
77
78 public function x(): string
79 {
80 return $this->get(self::DATA_X);
81 }
82
83 public function y(): string
84 {
85 return $this->get(self::DATA_Y);
86 }
87
88 public function isPrivate(): bool
89 {
90 return array_key_exists(self::DATA_D, $this->getData());
91 }
92
93 public function d(): string
94 {
95 Assertion::true($this->isPrivate(), 'The key is not private');
96
97 return $this->get(self::DATA_D);
98 }
99
100 public function curve(): int
101 {
102 return (int) $this->get(self::DATA_CURVE);
103 }
104
105 public function asPEM(): string
106 {
107 if ($this->isPrivate()) {
108 $der = new Sequence(
109 new Integer(1),
110 new OctetString(bin2hex($this->d())),
111 new ExplicitlyTaggedObject(0, new ObjectIdentifier($this->getCurveOid())),
112 new ExplicitlyTaggedObject(1, new BitString(bin2hex($this->getUncompressedCoordinates())))
113 );
114
115 return $this->pem('EC PRIVATE KEY', $der->getBinary());
116 }
117
118 $der = new Sequence(
119 new Sequence(
120 new ObjectIdentifier('1.2.840.10045.2.1'),
121 new ObjectIdentifier($this->getCurveOid())
122 ),
123 new BitString(bin2hex($this->getUncompressedCoordinates()))
124 );
125
126 return $this->pem('PUBLIC KEY', $der->getBinary());
127 }
128
129 public function getUncompressedCoordinates(): string
130 {
131 return "\x04".$this->x().$this->y();
132 }
133
134 private function getCurveOid(): string
135 {
136 return self::NAMED_CURVE_OID[$this->curve()];
137 }
138
139 private function pem(string $type, string $der): string
140 {
141 return sprintf("-----BEGIN %s-----\n", mb_strtoupper($type)).
142 chunk_split(base64_encode($der), 64, "\n").
143 sprintf("-----END %s-----\n", mb_strtoupper($type));
144 }
145 }
146