|
4 | 4 |
|
5 | 5 | namespace Jose\Component\Core\Util; |
6 | 6 |
|
| 7 | +use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 8 | +use SpomkyLabs\Pki\ASN1\Type\Primitive\BitString; |
| 9 | +use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer; |
| 10 | +use SpomkyLabs\Pki\ASN1\Type\Primitive\NullType; |
| 11 | +use SpomkyLabs\Pki\ASN1\Type\Primitive\ObjectIdentifier; |
| 12 | +use SpomkyLabs\Pki\ASN1\Type\Primitive\OctetString; |
| 13 | +use SpomkyLabs\Pki\CryptoEncoding\PEM; |
| 14 | +use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Asymmetric\RSAEncryptionAlgorithmIdentifier; |
7 | 15 | use function array_key_exists; |
8 | 16 | use function count; |
9 | 17 | use InvalidArgumentException; |
|
19 | 27 | */ |
20 | 28 | final class RSAKey |
21 | 29 | { |
22 | | - private null|RSAPrivateKey|RSAPublicKey $sequence = null; |
| 30 | + private null|Sequence $sequence = null; |
23 | 31 |
|
24 | 32 | private readonly array $values; |
25 | 33 |
|
@@ -131,27 +139,37 @@ public function toArray(): array |
131 | 139 | public function toPEM(): string |
132 | 140 | { |
133 | 141 | if (array_key_exists('d', $this->values)) { |
134 | | - $this->sequence = RSAPrivateKey::create( |
135 | | - $this->fromBase64ToInteger($this->values['n']), |
136 | | - $this->fromBase64ToInteger($this->values['e']), |
137 | | - $this->fromBase64ToInteger($this->values['d']), |
138 | | - isset($this->values['p']) ? $this->fromBase64ToInteger($this->values['p']) : '0', |
139 | | - isset($this->values['q']) ? $this->fromBase64ToInteger($this->values['q']) : '0', |
140 | | - isset($this->values['dp']) ? $this->fromBase64ToInteger($this->values['dp']) : '0', |
141 | | - isset($this->values['dq']) ? $this->fromBase64ToInteger($this->values['dq']) : '0', |
142 | | - isset($this->values['qi']) ? $this->fromBase64ToInteger($this->values['qi']) : '0', |
| 142 | + $this->sequence = Sequence::create( |
| 143 | + Integer::create(0), |
| 144 | + RSAEncryptionAlgorithmIdentifier::create()->toASN1(), |
| 145 | + OctetString::create( |
| 146 | + RSAPrivateKey::create( |
| 147 | + $this->fromBase64ToInteger($this->values['n']), |
| 148 | + $this->fromBase64ToInteger($this->values['e']), |
| 149 | + $this->fromBase64ToInteger($this->values['d']), |
| 150 | + isset($this->values['p']) ? $this->fromBase64ToInteger($this->values['p']) : '0', |
| 151 | + isset($this->values['q']) ? $this->fromBase64ToInteger($this->values['q']) : '0', |
| 152 | + isset($this->values['dp']) ? $this->fromBase64ToInteger($this->values['dp']) : '0', |
| 153 | + isset($this->values['dq']) ? $this->fromBase64ToInteger($this->values['dq']) : '0', |
| 154 | + isset($this->values['qi']) ? $this->fromBase64ToInteger($this->values['qi']) : '0', |
| 155 | + )->toDER() |
| 156 | + ) |
143 | 157 | ); |
144 | | - } else { |
145 | | - $this->sequence = RSAPublicKey::create( |
146 | | - $this->fromBase64ToInteger($this->values['n']), |
147 | | - $this->fromBase64ToInteger($this->values['e']) |
148 | | - ); |
149 | | - } |
150 | | - if ($this->sequence === null) { |
151 | | - throw new RuntimeException(); |
152 | | - } |
153 | 158 |
|
154 | | - return $this->sequence->toPEM() |
| 159 | + return PEM::create(PEM::TYPE_RSA_PRIVATE_KEY, $this->sequence->toDER()) |
| 160 | + ->string(); |
| 161 | + } |
| 162 | + $this->sequence = Sequence::create( |
| 163 | + RSAEncryptionAlgorithmIdentifier::create()->toASN1(), |
| 164 | + BitString::create( |
| 165 | + RSAPublicKey::create( |
| 166 | + $this->fromBase64ToInteger($this->values['n']), |
| 167 | + $this->fromBase64ToInteger($this->values['e']) |
| 168 | + )->toDER() |
| 169 | + ) |
| 170 | + ); |
| 171 | + |
| 172 | + return PEM::create(PEM::TYPE_RSA_PUBLIC_KEY, $this->sequence->toDER()) |
155 | 173 | ->string(); |
156 | 174 | } |
157 | 175 |
|
|
0 commit comments