|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\CryptoTypes\Asymmetric\RSA; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
|
8
|
|
|
use Sop\ASN1\Type\Primitive\Integer; |
|
9
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
|
10
|
|
|
use Sop\CryptoEncoding\PEM; |
|
11
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier; |
|
12
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Asymmetric\RSAEncryptionAlgorithmIdentifier; |
|
13
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType; |
|
14
|
|
|
use Sop\CryptoTypes\Asymmetric\PublicKey; |
|
15
|
|
|
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Implements PKCS #1 RSAPublicKey ASN.1 type. |
|
19
|
|
|
* |
|
20
|
|
|
* @see https://tools.ietf.org/html/rfc2437#section-11.1.1 |
|
21
|
|
|
*/ |
|
22
|
|
|
class RSAPublicKey extends PublicKey |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Modulus as a base 10 integer. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $_modulus; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Public exponent as a base 10 integer. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_publicExponent; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param int|string $n Modulus |
|
42
|
|
|
* @param int|string $e Public exponent |
|
43
|
|
|
*/ |
|
44
|
10 |
|
public function __construct($n, $e) |
|
45
|
|
|
{ |
|
46
|
10 |
|
$this->_modulus = strval($n); |
|
47
|
10 |
|
$this->_publicExponent = strval($e); |
|
48
|
10 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Initialize from ASN.1. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Sequence $seq |
|
54
|
|
|
* |
|
55
|
|
|
* @return self |
|
56
|
|
|
*/ |
|
57
|
9 |
|
public static function fromASN1(Sequence $seq): RSAPublicKey |
|
58
|
|
|
{ |
|
59
|
9 |
|
$n = $seq->at(0)->asInteger()->number(); |
|
60
|
9 |
|
$e = $seq->at(1)->asInteger()->number(); |
|
61
|
9 |
|
return new self($n, $e); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Initialize from DER data. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $data |
|
68
|
|
|
* |
|
69
|
|
|
* @return self |
|
70
|
|
|
*/ |
|
71
|
9 |
|
public static function fromDER(string $data): RSAPublicKey |
|
72
|
|
|
{ |
|
73
|
9 |
|
return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @see PublicKey::fromPEM() |
|
78
|
|
|
* |
|
79
|
|
|
* @param PEM $pem |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \UnexpectedValueException |
|
82
|
|
|
* |
|
83
|
|
|
* @return self |
|
84
|
|
|
*/ |
|
85
|
5 |
|
public static function fromPEM(PEM $pem): RSAPublicKey |
|
86
|
|
|
{ |
|
87
|
5 |
|
switch ($pem->type()) { |
|
88
|
5 |
|
case PEM::TYPE_RSA_PUBLIC_KEY: |
|
89
|
2 |
|
return self::fromDER($pem->data()); |
|
90
|
3 |
|
case PEM::TYPE_PUBLIC_KEY: |
|
91
|
2 |
|
$pki = PublicKeyInfo::fromDER($pem->data()); |
|
92
|
|
|
if (AlgorithmIdentifier::OID_RSA_ENCRYPTION !== |
|
93
|
2 |
|
$pki->algorithmIdentifier()->oid()) { |
|
94
|
1 |
|
throw new \UnexpectedValueException('Not an RSA public key.'); |
|
95
|
|
|
} |
|
96
|
1 |
|
return self::fromDER($pki->publicKeyData()); |
|
97
|
|
|
} |
|
98
|
1 |
|
throw new \UnexpectedValueException('Invalid PEM type ' . $pem->type()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get modulus. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string Base 10 integer |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function modulus(): string |
|
107
|
|
|
{ |
|
108
|
1 |
|
return $this->_modulus; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get public exponent. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string Base 10 integer |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function publicExponent(): string |
|
117
|
|
|
{ |
|
118
|
1 |
|
return $this->_publicExponent; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
3 |
|
public function algorithmIdentifier(): AlgorithmIdentifierType |
|
125
|
|
|
{ |
|
126
|
3 |
|
return new RSAEncryptionAlgorithmIdentifier(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Generate ASN.1 structure. |
|
131
|
|
|
* |
|
132
|
|
|
* @return Sequence |
|
133
|
|
|
*/ |
|
134
|
4 |
|
public function toASN1(): Sequence |
|
135
|
|
|
{ |
|
136
|
4 |
|
return new Sequence(new Integer($this->_modulus), |
|
137
|
4 |
|
new Integer($this->_publicExponent)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
4 |
|
public function toDER(): string |
|
144
|
|
|
{ |
|
145
|
4 |
|
return $this->toASN1()->toDER(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Generate PEM. |
|
150
|
|
|
* |
|
151
|
|
|
* @return PEM |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function toPEM(): PEM |
|
154
|
|
|
{ |
|
155
|
1 |
|
return new PEM(PEM::TYPE_RSA_PUBLIC_KEY, $this->toDER()); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|