simplesamlphp /
xml-security
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | declare(strict_types=1); |
||||||
| 4 | |||||||
| 5 | namespace SimpleSAML\XMLSecurity\XML; |
||||||
| 6 | |||||||
| 7 | use DOMElement; |
||||||
| 8 | use SimpleSAML\Assert\Assert; |
||||||
| 9 | use SimpleSAML\XML\DOMDocumentFactory; |
||||||
| 10 | use SimpleSAML\XMLSchema\Type\AnyURIValue; |
||||||
| 11 | use SimpleSAML\XMLSchema\Type\Base64BinaryValue; |
||||||
| 12 | use SimpleSAML\XMLSchema\Type\IDValue; |
||||||
| 13 | use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmInterface; |
||||||
| 14 | use SimpleSAML\XMLSecurity\Constants as C; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 15 | use SimpleSAML\XMLSecurity\Exception\RuntimeException; |
||||||
| 16 | use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException; |
||||||
| 17 | use SimpleSAML\XMLSecurity\Type\DigestValue as DigestValueType; |
||||||
| 18 | use SimpleSAML\XMLSecurity\Utils\XML; |
||||||
| 19 | use SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod; |
||||||
| 20 | use SimpleSAML\XMLSecurity\XML\ds\DigestMethod; |
||||||
|
0 ignored issues
–
show
The type
SimpleSAML\XMLSecurity\XML\ds\DigestMethod 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 21 | use SimpleSAML\XMLSecurity\XML\ds\DigestValue; |
||||||
|
0 ignored issues
–
show
The type
SimpleSAML\XMLSecurity\XML\ds\DigestValue 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 22 | use SimpleSAML\XMLSecurity\XML\ds\KeyInfo; |
||||||
| 23 | use SimpleSAML\XMLSecurity\XML\ds\Reference; |
||||||
| 24 | use SimpleSAML\XMLSecurity\XML\ds\Signature; |
||||||
| 25 | use SimpleSAML\XMLSecurity\XML\ds\SignatureMethod; |
||||||
|
0 ignored issues
–
show
The type
SimpleSAML\XMLSecurity\XML\ds\SignatureMethod 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 26 | use SimpleSAML\XMLSecurity\XML\ds\SignatureValue; |
||||||
| 27 | use SimpleSAML\XMLSecurity\XML\ds\SignedInfo; |
||||||
| 28 | use SimpleSAML\XMLSecurity\XML\ds\Transform; |
||||||
| 29 | use SimpleSAML\XMLSecurity\XML\ds\Transforms; |
||||||
| 30 | |||||||
| 31 | use function base64_encode; |
||||||
| 32 | use function hash; |
||||||
| 33 | use function in_array; |
||||||
| 34 | |||||||
| 35 | /** |
||||||
| 36 | * Trait SignableElementTrait |
||||||
| 37 | * |
||||||
| 38 | * @package simplesamlphp/xml-security |
||||||
| 39 | * @phpstan-ignore trait.unused |
||||||
| 40 | */ |
||||||
| 41 | trait SignableElementTrait |
||||||
| 42 | { |
||||||
| 43 | use CanonicalizableElementTrait; |
||||||
| 44 | |||||||
| 45 | |||||||
| 46 | /** @var \SimpleSAML\XMLSecurity\XML\ds\Signature|null */ |
||||||
| 47 | protected ?Signature $signature = null; |
||||||
| 48 | |||||||
| 49 | private string $c14nAlg = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS; |
||||||
| 50 | |||||||
| 51 | /** @var \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null */ |
||||||
| 52 | private ?KeyInfo $keyInfo = null; |
||||||
| 53 | |||||||
| 54 | /** @var \SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmInterface|null */ |
||||||
| 55 | protected ?SignatureAlgorithmInterface $signer = null; |
||||||
| 56 | |||||||
| 57 | |||||||
| 58 | /** |
||||||
| 59 | * Get the ID of this element. |
||||||
| 60 | * |
||||||
| 61 | * When this method returns null, the signature created for this object will reference the entire document. |
||||||
| 62 | * |
||||||
| 63 | * @return \SimpleSAML\XML\Type\IDValue|null The ID of this element, or null if we don't have one. |
||||||
| 64 | */ |
||||||
| 65 | abstract public function getId(): ?IDValue; |
||||||
| 66 | |||||||
| 67 | |||||||
| 68 | /** |
||||||
| 69 | * Sign the current element. |
||||||
| 70 | * |
||||||
| 71 | * The signature will not be applied until toXML() is called. |
||||||
| 72 | * |
||||||
| 73 | * @param \SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmInterface $signer The actual signer implementation |
||||||
| 74 | * to use. |
||||||
| 75 | * @param string $canonicalizationAlg The identifier of the canonicalization algorithm to use. |
||||||
| 76 | * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo A KeyInfo object to add to the signature. |
||||||
| 77 | */ |
||||||
| 78 | public function sign( |
||||||
| 79 | SignatureAlgorithmInterface $signer, |
||||||
| 80 | string $canonicalizationAlg = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, |
||||||
| 81 | ?KeyInfo $keyInfo = null, |
||||||
| 82 | ): void { |
||||||
| 83 | $this->signer = $signer; |
||||||
| 84 | $this->keyInfo = $keyInfo; |
||||||
| 85 | Assert::oneOf( |
||||||
| 86 | $canonicalizationAlg, |
||||||
| 87 | [ |
||||||
| 88 | C::C14N_INCLUSIVE_WITH_COMMENTS, |
||||||
| 89 | C::C14N_INCLUSIVE_WITHOUT_COMMENTS, |
||||||
| 90 | C::C14N_EXCLUSIVE_WITH_COMMENTS, |
||||||
| 91 | C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, |
||||||
| 92 | ], |
||||||
| 93 | 'Unsupported canonicalization algorithm: %s', |
||||||
| 94 | UnsupportedAlgorithmException::class, |
||||||
| 95 | ); |
||||||
| 96 | $this->c14nAlg = $canonicalizationAlg; |
||||||
| 97 | } |
||||||
| 98 | |||||||
| 99 | |||||||
| 100 | /** |
||||||
| 101 | * Get a ds:Reference pointing to this object. |
||||||
| 102 | * |
||||||
| 103 | * @param string $digestAlg The digest algorithm to use. |
||||||
| 104 | * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms $transforms The transforms to apply to the object. |
||||||
| 105 | */ |
||||||
| 106 | #[\NoDiscard] |
||||||
| 107 | private function getReference( |
||||||
| 108 | string $digestAlg, |
||||||
| 109 | Transforms $transforms, |
||||||
| 110 | DOMElement $xml, |
||||||
| 111 | string $canonicalDocument, |
||||||
| 112 | ): Reference { |
||||||
| 113 | $id = $this->getId(); |
||||||
| 114 | $uri = null; |
||||||
| 115 | if (empty($id)) { // document reference |
||||||
| 116 | Assert::notNull( |
||||||
| 117 | $xml->ownerDocument->documentElement, |
||||||
| 118 | 'Cannot create a document reference without a root element in the document.', |
||||||
| 119 | RuntimeException::class, |
||||||
| 120 | ); |
||||||
| 121 | Assert::true( |
||||||
| 122 | $xml->isSameNode($xml->ownerDocument->documentElement), |
||||||
| 123 | 'Cannot create a document reference when signing an object that is not the root of the document. ' . |
||||||
| 124 | 'Please give your object an identifier.', |
||||||
| 125 | RuntimeException::class, |
||||||
| 126 | ); |
||||||
| 127 | if (in_array($this->c14nAlg, [C::C14N_INCLUSIVE_WITH_COMMENTS, C::C14N_EXCLUSIVE_WITH_COMMENTS])) { |
||||||
| 128 | $uri = '#xpointer(/)'; |
||||||
| 129 | } |
||||||
| 130 | } elseif (in_array($this->c14nAlg, [C::C14N_INCLUSIVE_WITH_COMMENTS, C::C14N_EXCLUSIVE_WITH_COMMENTS])) { |
||||||
| 131 | // regular reference, but must retain comments |
||||||
| 132 | $uri = '#xpointer(id(' . $id . '))'; |
||||||
| 133 | } else { // regular reference, can ignore comments |
||||||
| 134 | $uri = '#' . $id; |
||||||
| 135 | } |
||||||
| 136 | |||||||
| 137 | return new Reference( |
||||||
| 138 | new DigestMethod( |
||||||
| 139 | AnyURIValue::fromString($digestAlg), |
||||||
| 140 | ), |
||||||
| 141 | new DigestValue( |
||||||
| 142 | DigestValueType::fromString( |
||||||
| 143 | base64_encode(hash(C::$DIGEST_ALGORITHMS[$digestAlg], $canonicalDocument, true)), |
||||||
| 144 | ), |
||||||
| 145 | ), |
||||||
| 146 | $transforms, |
||||||
| 147 | null, |
||||||
| 148 | null, |
||||||
| 149 | ($uri !== null) ? AnyURIValue::fromString($uri) : null, |
||||||
| 150 | ); |
||||||
| 151 | } |
||||||
| 152 | |||||||
| 153 | |||||||
| 154 | /** |
||||||
| 155 | * Do the actual signing of the document. |
||||||
| 156 | * |
||||||
| 157 | * Note that this method does not insert the signature in the returned \DOMElement. The signature will be available |
||||||
| 158 | * in $this->signature as a \SimpleSAML\XMLSecurity\XML\ds\Signature object, which can then be converted to XML |
||||||
| 159 | * calling toXML() on it, passing the \DOMElement value returned here as a parameter. The resulting \DOMElement |
||||||
| 160 | * can then be inserted in the position desired. |
||||||
| 161 | * |
||||||
| 162 | * E.g.: |
||||||
| 163 | * $xml = // our XML to sign |
||||||
| 164 | * $signedXML = $this->doSign($xml); |
||||||
| 165 | * $signedXML->appendChild($this->signature->toXML($signedXML)); |
||||||
| 166 | * |
||||||
| 167 | * @param \DOMElement $xml The element to sign. |
||||||
| 168 | * @return \DOMElement The signed element, without the signature attached to it just yet. |
||||||
| 169 | */ |
||||||
| 170 | #[\NoDiscard] |
||||||
| 171 | protected function doSign(DOMElement $xml): DOMElement |
||||||
| 172 | { |
||||||
| 173 | Assert::notNull( |
||||||
| 174 | $this->signer, |
||||||
| 175 | 'Cannot call toSignedXML() without calling sign() first.', |
||||||
| 176 | RuntimeException::class, |
||||||
| 177 | ); |
||||||
| 178 | |||||||
| 179 | $algorithm = $this->signer->getAlgorithmId(); |
||||||
|
0 ignored issues
–
show
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
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...
|
|||||||
| 180 | $digest = $this->signer->getDigest(); |
||||||
| 181 | |||||||
| 182 | $transforms = new Transforms([ |
||||||
| 183 | new Transform( |
||||||
| 184 | AnyURIValue::fromString(C::XMLDSIG_ENVELOPED), |
||||||
| 185 | ), |
||||||
| 186 | new Transform( |
||||||
| 187 | AnyURIValue::fromString($this->c14nAlg), |
||||||
| 188 | ), |
||||||
| 189 | ]); |
||||||
| 190 | |||||||
| 191 | $canonicalDocument = XML::processTransforms($transforms, $xml); |
||||||
| 192 | |||||||
| 193 | $signedInfo = new SignedInfo( |
||||||
| 194 | new CanonicalizationMethod( |
||||||
| 195 | AnyURIValue::fromString($this->c14nAlg), |
||||||
| 196 | ), |
||||||
| 197 | new SignatureMethod( |
||||||
| 198 | AnyURIValue::fromString($algorithm), |
||||||
| 199 | ), |
||||||
| 200 | [$this->getReference($digest, $transforms, $xml, $canonicalDocument)], |
||||||
| 201 | ); |
||||||
| 202 | |||||||
| 203 | $signingData = $signedInfo->canonicalize($this->c14nAlg); |
||||||
| 204 | $signedData = base64_encode($this->signer->sign($signingData)); |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 205 | |||||||
| 206 | $this->setSignature( |
||||||
|
0 ignored issues
–
show
It seems like
setSignature() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 207 | new Signature( |
||||||
| 208 | $signedInfo, |
||||||
| 209 | new SignatureValue( |
||||||
| 210 | Base64BinaryValue::fromString($signedData), |
||||||
| 211 | ), |
||||||
| 212 | $this->keyInfo, |
||||||
| 213 | ), |
||||||
| 214 | ); |
||||||
| 215 | return DOMDocumentFactory::fromString($canonicalDocument)->documentElement; |
||||||
| 216 | } |
||||||
| 217 | |||||||
| 218 | |||||||
| 219 | /** |
||||||
| 220 | * Get the list of algorithms that are blacklisted for any signing operation. |
||||||
| 221 | * |
||||||
| 222 | * @return string[]|null An array with all algorithm identifiers that are blacklisted, or null to use this |
||||||
| 223 | * libraries default. |
||||||
| 224 | */ |
||||||
| 225 | abstract public function getBlacklistedAlgorithms(): ?array; |
||||||
| 226 | } |
||||||
| 227 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths