| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Cose\Key; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use Brick\Math\BigInteger; |
| 9 |
use function in_array; |
| 10 |
use InvalidArgumentException; |
| 11 |
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PublicKeyInfo; |
| 12 |
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\RSA\RSAPrivateKey; |
| 13 |
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\RSA\RSAPublicKey; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class RsaKey extends Key |
| 20 |
{ |
| 21 |
final public const DATA_N = -1; |
| 22 |
|
| 23 |
final public const DATA_E = -2; |
| 24 |
|
| 25 |
final public const DATA_D = -3; |
| 26 |
|
| 27 |
final public const DATA_P = -4; |
| 28 |
|
| 29 |
final public const DATA_Q = -5; |
| 30 |
|
| 31 |
final public const DATA_DP = -6; |
| 32 |
|
| 33 |
final public const DATA_DQ = -7; |
| 34 |
|
| 35 |
final public const DATA_QI = -8; |
| 36 |
|
| 37 |
final public const DATA_OTHER = -9; |
| 38 |
|
| 39 |
final public const DATA_RI = -10; |
| 40 |
|
| 41 |
final public const DATA_DI = -11; |
| 42 |
|
| 43 |
final public const DATA_TI = -12; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function __construct(array $data) |
| 49 |
{ |
| 50 |
foreach ([self::TYPE] as $key) { |
| 51 |
if (is_numeric($data[$key])) { |
| 52 |
$data[$key] = (int) $data[$key]; |
| 53 |
} |
| 54 |
} |
| 55 |
parent::__construct($data); |
| 56 |
if ($data[self::TYPE] !== self::TYPE_RSA && $data[self::TYPE] !== self::TYPE_NAME_RSA) { |
| 57 |
throw new InvalidArgumentException('Invalid RSA key. The key type does not correspond to a RSA key'); |
| 58 |
} |
| 59 |
if (! isset($data[self::DATA_N], $data[self::DATA_E])) { |
| 60 |
throw new InvalidArgumentException('Invalid RSA key. The modulus or the exponent is missing'); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static function create(array $data): self |
| 68 |
{ |
| 69 |
return new self($data); |
| 70 |
} |
| 71 |
|
| 72 |
public function n(): string |
| 73 |
{ |
| 74 |
return $this->get(self::DATA_N); |
| 75 |
} |
| 76 |
|
| 77 |
public function e(): string |
| 78 |
{ |
| 79 |
return $this->get(self::DATA_E); |
| 80 |
} |
| 81 |
|
| 82 |
public function d(): string |
| 83 |
{ |
| 84 |
$this->checkKeyIsPrivate(); |
| 85 |
|
| 86 |
return $this->get(self::DATA_D); |
| 87 |
} |
| 88 |
|
| 89 |
public function p(): string |
| 90 |
{ |
| 91 |
$this->checkKeyIsPrivate(); |
| 92 |
|
| 93 |
return $this->get(self::DATA_P); |
| 94 |
} |
| 95 |
|
| 96 |
public function q(): string |
| 97 |
{ |
| 98 |
$this->checkKeyIsPrivate(); |
| 99 |
|
| 100 |
return $this->get(self::DATA_Q); |
| 101 |
} |
| 102 |
|
| 103 |
public function dP(): string |
| 104 |
{ |
| 105 |
$this->checkKeyIsPrivate(); |
| 106 |
|
| 107 |
return $this->get(self::DATA_DP); |
| 108 |
} |
| 109 |
|
| 110 |
public function dQ(): string |
| 111 |
{ |
| 112 |
$this->checkKeyIsPrivate(); |
| 113 |
|
| 114 |
return $this->get(self::DATA_DQ); |
| 115 |
} |
| 116 |
|
| 117 |
public function QInv(): string |
| 118 |
{ |
| 119 |
$this->checkKeyIsPrivate(); |
| 120 |
|
| 121 |
return $this->get(self::DATA_QI); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function other(): array |
| 128 |
{ |
| 129 |
$this->checkKeyIsPrivate(); |
| 130 |
|
| 131 |
return $this->get(self::DATA_OTHER); |
| 132 |
} |
| 133 |
|
| 134 |
public function rI(): string |
| 135 |
{ |
| 136 |
$this->checkKeyIsPrivate(); |
| 137 |
|
| 138 |
return $this->get(self::DATA_RI); |
| 139 |
} |
| 140 |
|
| 141 |
public function dI(): string |
| 142 |
{ |
| 143 |
$this->checkKeyIsPrivate(); |
| 144 |
|
| 145 |
return $this->get(self::DATA_DI); |
| 146 |
} |
| 147 |
|
| 148 |
public function tI(): string |
| 149 |
{ |
| 150 |
$this->checkKeyIsPrivate(); |
| 151 |
|
| 152 |
return $this->get(self::DATA_TI); |
| 153 |
} |
| 154 |
|
| 155 |
public function hasPrimes(): bool |
| 156 |
{ |
| 157 |
return $this->has(self::DATA_P) && $this->has(self::DATA_Q); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function primes(): array |
| 164 |
{ |
| 165 |
return [$this->p(), $this->q()]; |
| 166 |
} |
| 167 |
|
| 168 |
public function hasExponents(): bool |
| 169 |
{ |
| 170 |
return $this->has(self::DATA_DP) && $this->has(self::DATA_DQ); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function exponents(): array |
| 177 |
{ |
| 178 |
return [$this->dP(), $this->dQ()]; |
| 179 |
} |
| 180 |
|
| 181 |
public function hasCoefficient(): bool |
| 182 |
{ |
| 183 |
return $this->has(self::DATA_QI); |
| 184 |
} |
| 185 |
|
| 186 |
public function isPublic(): bool |
| 187 |
{ |
| 188 |
return ! $this->isPrivate(); |
| 189 |
} |
| 190 |
|
| 191 |
public function isPrivate(): bool |
| 192 |
{ |
| 193 |
return array_key_exists(self::DATA_D, $this->getData()); |
| 194 |
} |
| 195 |
|
| 196 |
public function asPem(): string |
| 197 |
{ |
| 198 |
if ($this->isPrivate()) { |
| 199 |
$privateKey = RSAPrivateKey::create( |
| 200 |
$this->binaryToBigInteger($this->n()), |
| 201 |
$this->binaryToBigInteger($this->e()), |
| 202 |
$this->binaryToBigInteger($this->d()), |
| 203 |
$this->binaryToBigInteger($this->p()), |
| 204 |
$this->binaryToBigInteger($this->q()), |
| 205 |
$this->binaryToBigInteger($this->dP()), |
| 206 |
$this->binaryToBigInteger($this->dQ()), |
| 207 |
$this->binaryToBigInteger($this->QInv()) |
| 208 |
); |
| 209 |
|
| 210 |
return $privateKey->toPEM() |
| 211 |
->string(); |
| 212 |
} |
| 213 |
|
| 214 |
$publicKey = RSAPublicKey::create( |
| 215 |
$this->binaryToBigInteger($this->n()), |
| 216 |
$this->binaryToBigInteger($this->e()) |
| 217 |
); |
| 218 |
$rsaKey = PublicKeyInfo::fromPublicKey($publicKey); |
| 219 |
|
| 220 |
return $rsaKey->toPEM() |
| 221 |
->string(); |
| 222 |
} |
| 223 |
|
| 224 |
public function toPublic(): static |
| 225 |
{ |
| 226 |
$toBeRemoved = [ |
| 227 |
self::DATA_D, |
| 228 |
self::DATA_P, |
| 229 |
self::DATA_Q, |
| 230 |
self::DATA_DP, |
| 231 |
self::DATA_DQ, |
| 232 |
self::DATA_QI, |
| 233 |
self::DATA_OTHER, |
| 234 |
self::DATA_RI, |
| 235 |
self::DATA_DI, |
| 236 |
self::DATA_TI, |
| 237 |
]; |
| 238 |
$data = $this->getData(); |
| 239 |
foreach ($data as $k => $v) { |
| 240 |
if (in_array($k, $toBeRemoved, true)) { |
| 241 |
unset($data[$k]); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
return new static($data); |
| 246 |
} |
| 247 |
|
| 248 |
private function checkKeyIsPrivate(): void |
| 249 |
{ |
| 250 |
if (! $this->isPrivate()) { |
| 251 |
throw new InvalidArgumentException('The key is not private.'); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
private function binaryToBigInteger(string $data): string |
| 256 |
{ |
| 257 |
$res = unpack('H*', $data); |
| 258 |
|
| 259 |
$res = current($res); |
| 260 |
|
| 261 |
return BigInteger::fromBase($res, 16)->toBase(10); |
| 262 |
} |
| 263 |
} |