|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Cryptographic digital signature protocol. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CryptoManana\CryptographicProtocol; |
|
8
|
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Abstractions\Containers\AbstractCryptographicProtocol as CryptographicProtocol; |
|
10
|
|
|
use \CryptoManana\Core\Abstractions\MessageEncryption\AbstractAsymmetricEncryptionAlgorithm as AsymmetricCipher; |
|
11
|
|
|
use \CryptoManana\Core\Interfaces\MessageEncryption\DataSigningInterface as DataSigning; |
|
12
|
|
|
use \CryptoManana\Core\Interfaces\Containers\SignatureStandardInjectableInterface as SignatureStandardSetter; |
|
13
|
|
|
use \CryptoManana\Core\Interfaces\Containers\SignedDataInterface as SignedDataObjectProcessing; |
|
14
|
|
|
use \CryptoManana\Core\Traits\Containers\SignatureStandardInjectableTrait as SignatureStandardSetterImplementation; |
|
15
|
|
|
use \CryptoManana\DataStructures\SignedData as SignedDataStructure; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class DigitalSignature - The digital signature cryptographic protocol object. |
|
19
|
|
|
* |
|
20
|
|
|
* @package CryptoManana\CryptographicProtocol |
|
21
|
|
|
* |
|
22
|
|
|
* @mixin SignatureStandardSetterImplementation |
|
23
|
|
|
*/ |
|
24
|
|
|
class DigitalSignature extends CryptographicProtocol implements SignatureStandardSetter, SignedDataObjectProcessing |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The digital signature service dependency injection via a setter method implementation. |
|
28
|
|
|
* |
|
29
|
|
|
* {@internal Reusable implementation of `SignatureStandardInjectableInterface`. }} |
|
30
|
|
|
*/ |
|
31
|
|
|
use SignatureStandardSetterImplementation; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The digital signature service property storage. |
|
35
|
|
|
* |
|
36
|
|
|
* @var AsymmetricCipher|DataSigning|null The digital signature service. |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $signatureSource = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Container constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param AsymmetricCipher|DataSigning|null $signatureAlgorithm The digital signature service. |
|
44
|
|
|
* |
|
45
|
|
|
* @throws \Exception Initialization validation. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(AsymmetricCipher $signatureAlgorithm = null) |
|
48
|
|
|
{ |
|
49
|
|
|
if ($signatureAlgorithm instanceof DataSigning) { |
|
50
|
|
|
$this->signatureSource = $signatureAlgorithm; |
|
51
|
|
|
} else { |
|
52
|
|
|
throw new \RuntimeException('No digital signature standard service has been set.'); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Container destructor. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __destruct() |
|
60
|
|
|
{ |
|
61
|
|
|
unset($this->signatureSource); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Container cloning via deep copy. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __clone() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->signatureSource = clone $this->signatureSource; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Creates a signed data object for the given input data. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $plainData The plain input string. |
|
76
|
|
|
* |
|
77
|
|
|
* @return SignedDataStructure The signed data object. |
|
78
|
|
|
* @throws \Exception Validation errors. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function createSignedData($plainData) |
|
81
|
|
|
{ |
|
82
|
|
|
$signatureData = $this->signatureSource->signData($plainData); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
return new SignedDataStructure($plainData, $signatureData); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Verifies and extracts the plain data from a signed data object. |
|
89
|
|
|
* |
|
90
|
|
|
* @param SignedDataStructure $signedData The signed data object. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string The verified plain information. |
|
93
|
|
|
* @throws \Exception Validation errors. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function extractVerifiedData(SignedDataStructure $signedData) |
|
96
|
|
|
{ |
|
97
|
|
|
if (!$this->signatureSource->verifyDataSignature($signedData->signature, $signedData->data)) { |
|
|
|
|
|
|
98
|
|
|
throw new \RuntimeException('Wrong signature, the data has been tampered with or defected.'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $signedData->data; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.