|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The asymmetric DSA algorithm abstraction specification. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Abstractions\MessageEncryption; |
|
8
|
|
|
|
|
9
|
|
|
use CryptoManana\Core\Abstractions\MessageEncryption\AbstractAsymmetricEncryptionAlgorithm as AsymmetricAlgorithm; |
|
10
|
|
|
use CryptoManana\Core\Interfaces\MessageEncryption\DataSigningInterface as DigitalSignatureVerification; |
|
11
|
|
|
use CryptoManana\Core\Interfaces\MessageEncryption\SignatureDigestionInterface as SignatureDataDigestion; |
|
12
|
|
|
use CryptoManana\Core\Interfaces\MessageEncryption\ObjectSigningInterface as ObjectSigning; |
|
13
|
|
|
use CryptoManana\Core\Interfaces\MessageEncryption\FileSigningInterface as FileSigning; |
|
14
|
|
|
use CryptoManana\Core\Interfaces\MessageEncryption\SignatureDataFormatsInterface as SignatureDataFormatting; |
|
15
|
|
|
use CryptoManana\Core\Traits\MessageEncryption\SignatureDataFormatsTrait as SignatureDataFormats; |
|
16
|
|
|
use CryptoManana\Core\Traits\MessageEncryption\ObjectSigningTrait as SignAndVerifyObjects; |
|
17
|
|
|
use CryptoManana\Core\Traits\MessageEncryption\FileSigningTrait as SignAndVerifyFiles; |
|
18
|
|
|
use CryptoManana\Core\Traits\MessageEncryption\SignatureDigestionTrait as SignatureDigestionAlgorithms; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class AbstractDsaSignature - The DSA signature algorithm abstraction representation. |
|
22
|
|
|
* |
|
23
|
|
|
* @package CryptoManana\Core\Abstractions\MessageEncryption |
|
24
|
|
|
* |
|
25
|
|
|
* @mixin SignatureDigestionAlgorithms |
|
26
|
|
|
* @mixin SignAndVerifyObjects |
|
27
|
|
|
* @mixin SignAndVerifyFiles |
|
28
|
|
|
* @mixin SignatureDataFormats |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class AbstractDsaSignature extends AsymmetricAlgorithm implements |
|
31
|
|
|
DigitalSignatureVerification, |
|
32
|
|
|
SignatureDataDigestion, |
|
33
|
|
|
ObjectSigning, |
|
34
|
|
|
FileSigning, |
|
35
|
|
|
SignatureDataFormatting |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* Signature digestion algorithms. |
|
39
|
|
|
* |
|
40
|
|
|
* {@internal Reusable implementation of `SignatureDigestionInterface`. }} |
|
41
|
|
|
*/ |
|
42
|
|
|
use SignatureDigestionAlgorithms; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Generating signatures for objects and verifying them. |
|
46
|
|
|
* |
|
47
|
|
|
* {@internal Reusable implementation of `ObjectSigningInterface`. }} |
|
48
|
|
|
*/ |
|
49
|
|
|
use SignAndVerifyObjects; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Generating signatures for file content and verifying them. |
|
53
|
|
|
* |
|
54
|
|
|
* {@internal Reusable implementation of `FileSigningInterface`. }} |
|
55
|
|
|
*/ |
|
56
|
|
|
use SignAndVerifyFiles; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Signature data outputting formats. |
|
60
|
|
|
* |
|
61
|
|
|
* {@internal Reusable implementation of `SignatureDataFormatsInterface`. }} |
|
62
|
|
|
*/ |
|
63
|
|
|
use SignatureDataFormats; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The internal name of the algorithm. |
|
67
|
|
|
*/ |
|
68
|
|
|
const ALGORITHM_NAME = OPENSSL_KEYTYPE_DSA; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The signature's internal digestion algorithm property. |
|
72
|
|
|
* |
|
73
|
|
|
* @var int The digestion algorithm integer code value. |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $digestion = self::SHA2_384_SIGNING; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The output signature format property storage. |
|
79
|
|
|
* |
|
80
|
|
|
* @var int The output signature format integer code value. |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $signatureFormat = self::SIGNATURE_OUTPUT_HEX_UPPER; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Internal method for the validation of plain data used at encryption/signing operations. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $plainData The plain input string. |
|
88
|
|
|
* |
|
89
|
|
|
* @throws \Exception Validation errors. |
|
90
|
|
|
*/ |
|
91
|
81 |
|
protected function validatePlainData($plainData) |
|
92
|
|
|
{ |
|
93
|
81 |
|
if (!is_string($plainData)) { |
|
94
|
9 |
|
throw new \InvalidArgumentException('The data for signing must be a string or a binary string.'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Internal method for the validation of cipher/signature data used at decryption/verifying operations. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $cipherOrSignatureData The encrypted input string or a signature string. |
|
102
|
|
|
* |
|
103
|
|
|
* @throws \Exception Validation errors. |
|
104
|
|
|
*/ |
|
105
|
46 |
|
protected function validateCipherOrSignatureData($cipherOrSignatureData) |
|
106
|
|
|
{ |
|
107
|
46 |
|
if (!is_string($cipherOrSignatureData)) { |
|
108
|
4 |
|
throw new \InvalidArgumentException('The signature data must be a string or a binary string.'); |
|
109
|
42 |
|
} elseif ($cipherOrSignatureData === '') { |
|
110
|
4 |
|
throw new \InvalidArgumentException( |
|
111
|
4 |
|
'The signature string is empty and there is nothing to verify from it.' |
|
112
|
4 |
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* DSA asymmetric algorithm constructor. |
|
118
|
|
|
*/ |
|
119
|
152 |
|
public function __construct() |
|
120
|
|
|
{ |
|
121
|
152 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get debug information for the class instance. |
|
125
|
|
|
* |
|
126
|
|
|
* @return array Debug information. |
|
127
|
|
|
*/ |
|
128
|
4 |
|
public function __debugInfo() |
|
129
|
|
|
{ |
|
130
|
4 |
|
return [ |
|
131
|
4 |
|
'standard' => 'DSA/DSS', |
|
132
|
4 |
|
'type' => 'asymmetrical signing or digital signature algorithm', |
|
133
|
4 |
|
'key size in bits' => static::KEY_SIZE, |
|
134
|
4 |
|
'signature digestion algorithm' => $this->digestion, |
|
135
|
4 |
|
'private key' => $this->privateKey, |
|
136
|
4 |
|
'public key' => $this->publicKey, |
|
137
|
4 |
|
]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Generates a signature of the given plain data. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $plainData The plain input string. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string The signature data. |
|
146
|
|
|
* @throws \Exception Validation errors. |
|
147
|
|
|
*/ |
|
148
|
69 |
|
public function signData($plainData) |
|
149
|
|
|
{ |
|
150
|
69 |
|
$this->checkIfThePrivateKeyIsSet(); |
|
151
|
65 |
|
$this->validatePlainData($plainData); |
|
152
|
|
|
|
|
153
|
60 |
|
$privateKeyResource = openssl_pkey_get_private(base64_decode($this->privateKey)); |
|
154
|
|
|
|
|
155
|
|
|
// @codeCoverageIgnoreStart |
|
156
|
|
|
if ($privateKeyResource === false) { |
|
157
|
|
|
throw new \RuntimeException( |
|
158
|
|
|
'Failed to use the current private key, probably because of ' . |
|
159
|
|
|
'a misconfigured OpenSSL library or an invalid key.' |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
// @codeCoverageIgnoreEnd |
|
163
|
|
|
|
|
164
|
60 |
|
$signatureData = ''; |
|
165
|
60 |
|
openssl_sign($plainData, $signatureData, $privateKeyResource, $this->digestion); |
|
166
|
|
|
|
|
167
|
|
|
// Free the private key (resource cleanup) |
|
168
|
60 |
|
@openssl_free_key($privateKeyResource); |
|
169
|
60 |
|
$privateKeyResource = null; |
|
170
|
|
|
|
|
171
|
60 |
|
return $this->changeOutputFormat($signatureData, true); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Verifies that the signature is correct for the given plain data. |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $signatureData The signature input string. |
|
178
|
|
|
* @param string $plainData The plain input string. |
|
179
|
|
|
* |
|
180
|
|
|
* @return bool The verification result. |
|
181
|
|
|
* @throws \Exception Validation errors. |
|
182
|
|
|
*/ |
|
183
|
50 |
|
public function verifyDataSignature($signatureData, $plainData) |
|
184
|
|
|
{ |
|
185
|
50 |
|
$this->checkIfThePublicKeyIsSet(); |
|
186
|
50 |
|
$this->validatePlainData($plainData); |
|
187
|
46 |
|
$this->validateCipherOrSignatureData($signatureData); |
|
188
|
|
|
|
|
189
|
38 |
|
$signatureData = $this->changeOutputFormat($signatureData, false); |
|
190
|
38 |
|
$publicKeyResource = openssl_pkey_get_public(base64_decode($this->publicKey)); |
|
191
|
|
|
|
|
192
|
|
|
// @codeCoverageIgnoreStart |
|
193
|
|
|
if ($publicKeyResource === false) { |
|
194
|
|
|
throw new \RuntimeException( |
|
195
|
|
|
'Failed to use the current public key, probably because of ' . |
|
196
|
|
|
'a misconfigured OpenSSL library or an invalid key.' |
|
197
|
|
|
); |
|
198
|
|
|
} |
|
199
|
|
|
// @codeCoverageIgnoreEnd |
|
200
|
|
|
|
|
201
|
38 |
|
$verified = (openssl_verify($plainData, $signatureData, $publicKeyResource, $this->digestion) === 1); |
|
202
|
|
|
|
|
203
|
|
|
// Free the public key (resource cleanup) |
|
204
|
38 |
|
@openssl_free_key($publicKeyResource); |
|
205
|
38 |
|
$publicKeyResource = null; |
|
206
|
|
|
|
|
207
|
38 |
|
return $verified; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|