SignableElementTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getBlacklistedAlgorithms() 0 4 1
A doSign() 0 40 1
A sign() 0 25 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Compat\ContainerSingleton;
10
use SimpleSAML\XML\DOMDocumentFactory;
11
use SimpleSAML\XMLSchema\Type\AnyURIValue;
12
use SimpleSAML\XMLSchema\Type\Base64BinaryValue;
13
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmInterface;
14
use SimpleSAML\XMLSecurity\Constants as C;
15
use SimpleSAML\XMLSecurity\Exception\RuntimeException;
16
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException;
17
use SimpleSAML\XMLSecurity\Utils\XML;
18
use SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod;
19
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
20
use SimpleSAML\XMLSecurity\XML\ds\Signature;
21
use SimpleSAML\XMLSecurity\XML\ds\SignatureMethod;
22
use SimpleSAML\XMLSecurity\XML\ds\SignatureValue;
23
use SimpleSAML\XMLSecurity\XML\ds\SignedInfo;
24
use SimpleSAML\XMLSecurity\XML\ds\Transform;
25
use SimpleSAML\XMLSecurity\XML\ds\Transforms;
26
use SimpleSAML\XMLSecurity\XML\SignableElementTrait as BaseSignableElementTrait;
27
28
use function base64_encode;
29
30
/**
31
 * Helper trait for processing signable elements.
32
 *
33
 * @package simplesamlphp/saml2
34
 */
35
trait SignableElementTrait
36
{
37
    use BaseSignableElementTrait;
38
39
40
    /**
41
     * Sign the current element.
42
     *
43
     * The signature will not be applied until toXML() is called.
44
     *
45
     * @param \SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmInterface $signer The actual signer implementation
46
     * to use.
47
     * @param string $canonicalizationAlg The identifier of the canonicalization algorithm to use.
48
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo A KeyInfo object to add to the signature.
49
     */
50
    public function sign(
51
        SignatureAlgorithmInterface $signer,
52
        string $canonicalizationAlg = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS,
53
        ?KeyInfo $keyInfo = null,
54
    ): void {
55
        /**
56
         * 5.4.2: SAML assertions and protocol messages MUST supply a value for the ID attribute
57
         * on the root element of the assertion or protocol message being signed.
58
         */
59
        Assert::notNull($this->getID(), "Signable element must have an ID set before it can be signed.");
60
61
        $this->signer = $signer;
62
        $this->keyInfo = $keyInfo;
63
        Assert::oneOf(
64
            $canonicalizationAlg,
65
            [
66
                C::C14N_INCLUSIVE_WITH_COMMENTS,
67
                C::C14N_INCLUSIVE_WITHOUT_COMMENTS,
68
                C::C14N_EXCLUSIVE_WITH_COMMENTS,
69
                C::C14N_EXCLUSIVE_WITHOUT_COMMENTS,
70
            ],
71
            'Unsupported canonicalization algorithm: %s',
72
            UnsupportedAlgorithmException::class,
73
        );
74
        $this->c14nAlg = $canonicalizationAlg;
75
    }
76
77
78
    /**
79
     * Do the actual signing of the document.
80
     *
81
     * Note that this method does not insert the signature in the returned \DOMElement. The signature will be available
82
     * in $this->signature as a \SimpleSAML\XMLSecurity\XML\ds\Signature object, which can then be converted to XML
83
     * calling toXML() on it, passing the \DOMElement value returned here as a parameter. The resulting \DOMElement
84
     * can then be inserted in the position desired.
85
     *
86
     * E.g.:
87
     *     $xml = // our XML to sign
88
     *     $signedXML = $this->doSign($xml);
89
     *     $signedXML->appendChild($this->signature->toXML($signedXML));
90
     *
91
     * @param \DOMElement $xml The element to sign.
92
     * @return \DOMElement The signed element, without the signature attached to it just yet.
93
     */
94
    protected function doSign(DOMElement $xml): DOMElement
95
    {
96
        Assert::notNull(
97
            $this->signer,
98
            'Cannot call toSignedXML() without calling sign() first.',
99
            RuntimeException::class,
100
        );
101
102
        $algorithm = $this->signer->getAlgorithmId();
0 ignored issues
show
Bug introduced by
The method getAlgorithmId() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
        /** @scrutinizer ignore-call */ 
103
        $algorithm = $this->signer->getAlgorithmId();

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.

Loading history...
103
        $digest = $this->signer->getDigest();
104
105
        $transforms = new Transforms([
106
            /**
107
             * 5.4.1: SAML assertions and protocols MUST use enveloped signatures when
108
             * signing assertions and protocol messages
109
             */
110
            new Transform(AnyURIValue::fromString(C::XMLDSIG_ENVELOPED)),
111
            new Transform(AnyURIValue::fromString($this->c14nAlg)),
112
        ]);
113
114
        $canonicalDocument = XML::processTransforms($transforms, $xml);
115
116
        $signedInfo = new SignedInfo(
117
            new CanonicalizationMethod(AnyURIValue::fromString($this->c14nAlg)),
118
            new SignatureMethod(AnyURIValue::fromString($algorithm)),
119
            [$this->getReference($digest, $transforms, $xml, $canonicalDocument)],
120
        );
121
122
        $signingData = $signedInfo->canonicalize($this->c14nAlg);
123
        $signedData = base64_encode($this->signer->sign($signingData));
0 ignored issues
show
Bug introduced by
It seems like $this->signer->sign($signingData) can also be of type false; however, parameter $string of base64_encode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
        $signedData = base64_encode(/** @scrutinizer ignore-type */ $this->signer->sign($signingData));
Loading history...
124
125
        $this->signature = new Signature(
126
            $signedInfo,
127
            new SignatureValue(
128
                Base64BinaryValue::fromString($signedData),
129
            ),
130
            $this->keyInfo,
131
        );
132
133
        return DOMDocumentFactory::fromString($canonicalDocument)->documentElement;
134
    }
135
136
137
    public function getBlacklistedAlgorithms(): ?array
138
    {
139
        $container = ContainerSingleton::getInstance();
140
        return $container->getBlacklistedEncryptionAlgorithms();
141
    }
142
}
143