| 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 Brick\Math\BigInteger; |
| 19 |
use FG\ASN1\Universal\BitString; |
| 20 |
use FG\ASN1\Universal\Integer; |
| 21 |
use FG\ASN1\Universal\NullObject; |
| 22 |
use FG\ASN1\Universal\ObjectIdentifier; |
| 23 |
use FG\ASN1\Universal\Sequence; |
| 24 |
use InvalidArgumentException; |
| 25 |
|
| 26 |
class RsaKey extends Key |
| 27 |
{ |
| 28 |
public const DATA_N = -1; |
| 29 |
public const DATA_E = -2; |
| 30 |
public const DATA_D = -3; |
| 31 |
public const DATA_P = -4; |
| 32 |
public const DATA_Q = -5; |
| 33 |
public const DATA_DP = -6; |
| 34 |
public const DATA_DQ = -7; |
| 35 |
public const DATA_QI = -8; |
| 36 |
public const DATA_OTHER = -9; |
| 37 |
public const DATA_RI = -10; |
| 38 |
public const DATA_DI = -11; |
| 39 |
public const DATA_TI = -12; |
| 40 |
|
| 41 |
public function __construct(array $data) |
| 42 |
{ |
| 43 |
parent::__construct($data); |
| 44 |
Assertion::eq($data[self::TYPE], self::TYPE_RSA, 'Invalid RSA key. The key type does not correspond to a RSA key'); |
| 45 |
Assertion::keyExists($data, self::DATA_N, 'Invalid RSA key. The modulus is missing'); |
| 46 |
Assertion::keyExists($data, self::DATA_E, 'Invalid RSA key. The exponent is missing'); |
| 47 |
} |
| 48 |
|
| 49 |
public function n(): string |
| 50 |
{ |
| 51 |
return $this->get(self::DATA_N); |
| 52 |
} |
| 53 |
|
| 54 |
public function e(): string |
| 55 |
{ |
| 56 |
return $this->get(self::DATA_E); |
| 57 |
} |
| 58 |
|
| 59 |
public function d(): string |
| 60 |
{ |
| 61 |
Assertion::true($this->isPrivate(), 'The key is not private.'); |
| 62 |
|
| 63 |
return $this->get(self::DATA_D); |
| 64 |
} |
| 65 |
|
| 66 |
public function p(): string |
| 67 |
{ |
| 68 |
Assertion::true($this->isPrivate(), 'The key is not private.'); |
| 69 |
|
| 70 |
return $this->get(self::DATA_P); |
| 71 |
} |
| 72 |
|
| 73 |
public function q(): string |
| 74 |
{ |
| 75 |
Assertion::true($this->isPrivate(), 'The key is not private.'); |
| 76 |
|
| 77 |
return $this->get(self::DATA_Q); |
| 78 |
} |
| 79 |
|
| 80 |
public function dP(): string |
| 81 |
{ |
| 82 |
Assertion::true($this->isPrivate(), 'The key is not private.'); |
| 83 |
|
| 84 |
return $this->get(self::DATA_DP); |
| 85 |
} |
| 86 |
|
| 87 |
public function dQ(): string |
| 88 |
{ |
| 89 |
Assertion::true($this->isPrivate(), 'The key is not private.'); |
| 90 |
|
| 91 |
return $this->get(self::DATA_DQ); |
| 92 |
} |
| 93 |
|
| 94 |
public function QInv(): string |
| 95 |
{ |
| 96 |
Assertion::true($this->isPrivate(), 'The key is not private.'); |
| 97 |
|
| 98 |
return $this->get(self::DATA_QI); |
| 99 |
} |
| 100 |
|
| 101 |
public function other(): array |
| 102 |
{ |
| 103 |
Assertion::true($this->isPrivate(), 'The key is not private.'); |
| 104 |
|
| 105 |
return $this->get(self::DATA_OTHER); |
| 106 |
} |
| 107 |
|
| 108 |
public function rI(): string |
| 109 |
{ |
| 110 |
Assertion::true($this->isPrivate(), 'The key is not private.'); |
| 111 |
|
| 112 |
return $this->get(self::DATA_RI); |
| 113 |
} |
| 114 |
|
| 115 |
public function dI(): string |
| 116 |
{ |
| 117 |
Assertion::true($this->isPrivate(), 'The key is not private.'); |
| 118 |
|
| 119 |
return $this->get(self::DATA_DI); |
| 120 |
} |
| 121 |
|
| 122 |
public function tI(): string |
| 123 |
{ |
| 124 |
Assertion::true($this->isPrivate(), 'The key is not private.'); |
| 125 |
|
| 126 |
return $this->get(self::DATA_TI); |
| 127 |
} |
| 128 |
|
| 129 |
public function hasPrimes(): bool |
| 130 |
{ |
| 131 |
return $this->has(self::DATA_P) && $this->has(self::DATA_Q); |
| 132 |
} |
| 133 |
|
| 134 |
public function primes(): array |
| 135 |
{ |
| 136 |
return [ |
| 137 |
$this->p(), |
| 138 |
$this->q(), |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
public function hasExponents(): bool |
| 143 |
{ |
| 144 |
return $this->has(self::DATA_DP) && $this->has(self::DATA_DQ); |
| 145 |
} |
| 146 |
|
| 147 |
public function exponents(): array |
| 148 |
{ |
| 149 |
return [ |
| 150 |
$this->dP(), |
| 151 |
$this->dQ(), |
| 152 |
]; |
| 153 |
} |
| 154 |
|
| 155 |
public function hasCoefficient(): bool |
| 156 |
{ |
| 157 |
return $this->has(self::DATA_QI); |
| 158 |
} |
| 159 |
|
| 160 |
public function isPublic(): bool |
| 161 |
{ |
| 162 |
return !$this->isPrivate(); |
| 163 |
} |
| 164 |
|
| 165 |
public function isPrivate(): bool |
| 166 |
{ |
| 167 |
return array_key_exists(self::DATA_D, $this->getData()); |
| 168 |
} |
| 169 |
|
| 170 |
public function asPem(): string |
| 171 |
{ |
| 172 |
Assertion::false($this->isPrivate(), 'Unsupported for private keys.'); |
| 173 |
$bitSring = new Sequence( |
| 174 |
new Integer($this->fromBase64ToInteger($this->n())), |
| 175 |
new Integer($this->fromBase64ToInteger($this->e())) |
| 176 |
); |
| 177 |
|
| 178 |
$der = new Sequence( |
| 179 |
new Sequence( |
| 180 |
new ObjectIdentifier('1.2.840.113549.1.1.1'), |
| 181 |
new NullObject() |
| 182 |
), |
| 183 |
new BitString(bin2hex($bitSring->getBinary())) |
| 184 |
); |
| 185 |
|
| 186 |
return $this->pem('PUBLIC KEY', $der->getBinary()); |
| 187 |
} |
| 188 |
|
| 189 |
private function fromBase64ToInteger(string $value): string |
| 190 |
{ |
| 191 |
$data = unpack('H*', $value); |
| 192 |
if (false === $data) { |
| 193 |
throw new InvalidArgumentException('Unable to convert to an integer'); |
| 194 |
} |
| 195 |
|
| 196 |
$hex = current($data); |
| 197 |
|
| 198 |
return BigInteger::fromBase($hex, 16)->toBase(10); |
| 199 |
} |
| 200 |
|
| 201 |
private function pem(string $type, string $der): string |
| 202 |
{ |
| 203 |
return sprintf("-----BEGIN %s-----\n", mb_strtoupper($type)). |
| 204 |
chunk_split(base64_encode($der), 64, "\n"). |
| 205 |
sprintf("-----END %s-----\n", mb_strtoupper($type)); |
| 206 |
} |
| 207 |
} |
| 208 |
|