validateElementWithKeys()   A
last analyzed

Complexity

Conditions 5
Paths 11

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 11
nop 2
dl 0
loc 37
rs 9.2568
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Signature;
6
7
use Exception;
8
use Psr\Log\LoggerInterface;
9
use SimpleSAML\SAML2\Utilities\ArrayCollection;
10
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\A...gnatureAlgorithmFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM;
12
use SimpleSAML\XMLSecurity\Key\X509Certificate as X509;
13
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
14
15
use function sprintf;
16
17
abstract class AbstractChainedValidator implements ChainedValidator
18
{
19
    /**
20
     * Constructor for AbstractChainedValidator
21
     *
22
     * @param \Psr\Log\LoggerInterface $logger
23
     */
24
    public function __construct(
25
        protected LoggerInterface $logger,
26
    ) {
27
    }
28
29
30
    /**
31
     * BC compatible version of the signature check
32
     *
33
     * @param \SimpleSAML\XMLSecurity\XML\SignedElementInterface $element
34
     * @param \SimpleSAML\SAML2\Utilities\ArrayCollection $pemCandidates
35
     *
36
     * @throws \Exception
37
     */
38
    protected function validateElementWithKeys(
39
        SignedElementInterface $element,
40
        ArrayCollection $pemCandidates,
41
    ): bool {
42
        $lastException = null;
43
        foreach ($pemCandidates as $index => $candidateKey) {
44
            $cert = new X509(PEM::fromString($candidateKey->getCertificate()));
45
            $verifier = (new SignatureAlgorithmFactory([]))->getAlgorithm(
46
                $element->getSignature()?->getSignedInfo()->getSignatureMethod()->getAlgorithm()->getValue(),
47
                $cert->getPublicKey(),
48
            );
49
50
            try {
51
                /*
52
                 * Make sure that we have a valid signature on either the response or the assertion.
53
                 */
54
                $result = $element->verify($verifier);
55
                if ($result) {
56
                    $this->logger->debug(sprintf('Validation with key "#%d" succeeded', $index));
57
                    return true;
58
                }
59
                $this->logger->debug(sprintf('Validation with key "#%d" failed without exception.', $index));
60
            } catch (Exception $e) {
61
                $this->logger->debug(sprintf(
62
                    'Validation with key "#%d" failed with exception: %s',
63
                    $index,
64
                    $e->getMessage(),
65
                ));
66
67
                $lastException = $e;
68
            }
69
        }
70
71
        if ($lastException !== null) {
72
            throw $lastException;
73
        } else {
74
            return false;
75
        }
76
    }
77
}
78