1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\XML; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Signature; |
10
|
|
|
use SimpleSAML\XMLSecurity\XML\SignedElementInterface; |
11
|
|
|
use SimpleSAML\XMLSecurity\XMLSecurityKey; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Helper trait for processing signed elements. |
15
|
|
|
* |
16
|
|
|
* @package simplesamlphp/xml-security |
17
|
|
|
*/ |
18
|
|
|
trait SignedElementTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The signature of this element. |
22
|
|
|
* |
23
|
|
|
* @var \SimpleSAML\XMLSecurity\XML\ds\Signature $signature |
24
|
|
|
*/ |
25
|
|
|
protected Signature $signature; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the signature element of this object. |
30
|
|
|
* |
31
|
|
|
* @return \SimpleSAML\XMLSecurity\XML\ds\Signature |
32
|
|
|
*/ |
33
|
|
|
public function getSignature(): ?Signature |
34
|
|
|
{ |
35
|
|
|
return $this->signature; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Initialize a signed element from XML. |
41
|
|
|
* |
42
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\Signature $signature The ds:Signature object |
43
|
|
|
*/ |
44
|
|
|
protected function setSignature(Signature $signature): void |
45
|
|
|
{ |
46
|
|
|
$this->signature = $signature; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Validate this element against a public key. |
52
|
|
|
* |
53
|
|
|
* true is returned on success, false is returned if we don't have any |
54
|
|
|
* signature we can validate. An exception is thrown if the signature |
55
|
|
|
* validation fails. |
56
|
|
|
* |
57
|
|
|
* @param \SimpleSAML\XMLSecurity\XMLSecurityKey $key The key we should check against. |
58
|
|
|
* @return \SimpleSAML\XMLSecurity\XML\SignedElementInterface The Signed element if it was validated. |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
public function validate(XMLSecurityKey $key): SignedElementInterface |
62
|
|
|
{ |
63
|
|
|
if ($this->signature === null) { |
64
|
|
|
throw new Exception("Unsigned element"); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
Assert::eq( |
68
|
|
|
$key->getAlgorithm(), |
69
|
|
|
$this->signature->getAlgorithm(), |
|
|
|
|
70
|
|
|
'Algorithm provided in key does not match algorithm used in signature.' |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
// check the signature |
74
|
|
|
$signer = $this->signature->getSigner(); |
|
|
|
|
75
|
|
|
if ($signer->verify($key) === 1) { |
76
|
|
|
return $this->getElement(); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
throw new Exception("Unable to validate Signature"); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieve certificates that sign this element. |
85
|
|
|
* |
86
|
|
|
* @return array Array with certificates. |
87
|
|
|
* @throws \Exception if an error occurs while trying to extract the public key from a certificate. |
88
|
|
|
*/ |
89
|
|
|
public function getValidatingCertificates(): array |
90
|
|
|
{ |
91
|
|
|
$ret = []; |
92
|
|
|
foreach ($this->signature->getCertificates() as $cert) { |
|
|
|
|
93
|
|
|
// extract the public key from the certificate for validation. |
94
|
|
|
$key = new XMLSecurityKey($this->signature->getAlgorithm(), ['type' => 'public']); |
95
|
|
|
$key->loadKey($cert); |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
// check the signature. |
99
|
|
|
if ($this->validate($key)) { |
100
|
|
|
$ret[] = $cert; |
101
|
|
|
} |
102
|
|
|
} catch (Exception $e) { |
103
|
|
|
// this certificate does not sign this element. |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $ret; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.