| Total Complexity | 144 |
| Total Lines | 1141 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Signature often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Signature, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Signature |
||
| 34 | { |
||
| 35 | /** @var array */ |
||
| 36 | public array $idNS = []; |
||
| 37 | |||
| 38 | /** @var array */ |
||
| 39 | public array $idKeys = []; |
||
| 40 | |||
| 41 | /** @var \SimpleSAML\XMLSecurity\Backend\SignatureBackend|null */ |
||
| 42 | protected ?SignatureBackend $backend = null; |
||
| 43 | |||
| 44 | /** @var \DOMElement */ |
||
| 45 | protected DOMElement $root; |
||
| 46 | |||
| 47 | /** @var \DOMElement|null */ |
||
| 48 | protected ?DOMElement $sigNode = null; |
||
| 49 | |||
| 50 | /** @var \DOMElement */ |
||
| 51 | protected DOMElement $sigMethodNode; |
||
| 52 | |||
| 53 | /** @var \DOMElement */ |
||
| 54 | protected DOMElement $c14nMethodNode; |
||
| 55 | |||
| 56 | /** @var \DOMElement */ |
||
| 57 | protected DOMElement $sigInfoNode; |
||
| 58 | |||
| 59 | /** @var \DOMElement|null */ |
||
| 60 | protected ?DOMElement $objectNode = null; |
||
| 61 | |||
| 62 | /** @var string */ |
||
| 63 | protected string $signfo; |
||
| 64 | |||
| 65 | /** @var string */ |
||
| 66 | protected string $sigAlg; |
||
| 67 | |||
| 68 | /** @var \DOMElement[] */ |
||
| 69 | protected array $verifiedElements = []; |
||
| 70 | |||
| 71 | /** @var string */ |
||
| 72 | protected string $c14nMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS; |
||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | protected string $nsPrefix = 'ds:'; |
||
| 76 | |||
| 77 | /** @var array */ |
||
| 78 | protected array $algBlacklist = [ |
||
| 79 | C::SIG_RSA_SHA1, |
||
| 80 | C::SIG_HMAC_SHA1, |
||
| 81 | ]; |
||
| 82 | |||
| 83 | /** @var array */ |
||
| 84 | protected array $references = []; |
||
| 85 | |||
| 86 | /** @var bool */ |
||
| 87 | protected bool $enveloping = false; |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * Signature constructor. |
||
| 92 | * |
||
| 93 | * @param \DOMElement|string $root The DOM element or a string of data we want to sign. |
||
| 94 | * @param \SimpleSAML\XMLSecurity\Backend\SignatureBackend|null $backend The backend to use to |
||
| 95 | * generate or verify signatures. See individual algorithms for defaults. |
||
| 96 | */ |
||
| 97 | public function __construct($root, SignatureBackend $backend = null) |
||
| 98 | { |
||
| 99 | $this->backend = $backend; |
||
| 100 | $this->initSignature(); |
||
| 101 | |||
| 102 | if (is_string($root)) { |
||
| 103 | $this->root = $this->addObject($root); |
||
| 104 | $this->enveloping = true; |
||
| 105 | } else { |
||
| 106 | $this->root = $root; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * Add an object element to the signature containing the given data. |
||
| 113 | * |
||
| 114 | * @param \DOMElement|string $data The data we want to envelope inside the signature. |
||
| 115 | * @param string|null $mimetype An optional mime type to specify. |
||
| 116 | * @param string|null $encoding An optional encoding to specify. |
||
| 117 | * |
||
| 118 | * @return \DOMElement The resulting object element added to the signature. |
||
| 119 | */ |
||
| 120 | public function addObject($data, ?string $mimetype = null, ?string $encoding = null): DOMElement |
||
| 121 | { |
||
| 122 | if ($this->objectNode === null) { |
||
| 123 | $this->objectNode = $this->createElement('Object'); |
||
| 124 | $this->sigNode->appendChild($this->objectNode); |
||
| 125 | } |
||
| 126 | |||
| 127 | if (is_string($mimetype) && !empty($mimetype)) { |
||
| 128 | $this->objectNode->setAttribute('MimeType', $mimetype); |
||
| 129 | } |
||
| 130 | |||
| 131 | if (is_string($encoding) && !empty($encoding)) { |
||
| 132 | $this->objectNode->setAttribute('Encoding', $encoding); |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($data instanceof DOMElement) { |
||
| 136 | $this->objectNode->appendChild($this->sigNode->ownerDocument->importNode($data, true)); |
||
| 137 | } else { |
||
| 138 | $this->objectNode->appendChild($this->sigNode->ownerDocument->createTextNode($data)); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $this->objectNode; |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Add a reference to a given node (an element or a document). |
||
| 147 | * |
||
| 148 | * @param \DOMNode $node A DOMElement that we want to sign, or a DOMDocument if we want to sign the entire document. |
||
| 149 | * @param string $alg The identifier of a supported digest algorithm to use when processing this reference. |
||
| 150 | * @param array $transforms An array containing a list of transforms that must be applied to the reference. |
||
| 151 | * Optional. |
||
| 152 | * @param array $options An array containing a set of options for this reference. Optional. Supported options are: |
||
| 153 | * - prefix (string): the XML prefix used in the element being referenced. Defaults to none (no prefix used). |
||
| 154 | * |
||
| 155 | * - prefix_ns (string): the namespace associated with the given prefix. Defaults to none (no prefix used). |
||
| 156 | * |
||
| 157 | * - id_name (string): the name of the "id" attribute in the referenced element. Defaults to "Id". |
||
| 158 | * |
||
| 159 | * - force_uri (boolean): Whether to explicitly add a URI attribute to the reference when referencing a |
||
| 160 | * DOMDocument or not. Defaults to true. If force_uri is false and $node is a DOMDocument, the URI attribute |
||
| 161 | * will be completely omitted. |
||
| 162 | * |
||
| 163 | * - overwrite (boolean): Whether to overwrite the identifier existing in the element referenced with a new, |
||
| 164 | * random one, or not. Defaults to true. |
||
| 165 | * |
||
| 166 | * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $node is not |
||
| 167 | * an instance of DOMDocument or DOMElement. |
||
| 168 | */ |
||
| 169 | public function addReference(DOMNode $node, string $alg, array $transforms = [], array $options = []): void |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Add a set of references to the signature. |
||
| 266 | * |
||
| 267 | * @param \DOMNode[] $nodes An array of DOMNode objects to be referred in the signature. |
||
| 268 | * @param string $alg The identifier of the digest algorithm to use. |
||
| 269 | * @param array $transforms An array of transforms to apply to each reference. |
||
| 270 | * @param array $options An array of options. |
||
| 271 | * |
||
| 272 | * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If any of the nodes in the $nodes |
||
| 273 | * array is not an instance of DOMDocument or DOMElement. |
||
| 274 | * |
||
| 275 | * @see addReference() |
||
| 276 | */ |
||
| 277 | public function addReferences(array $nodes, string $alg, array $transforms = [], $options = []): void |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * Attach one or more X509 certificates to the signature. |
||
| 287 | * |
||
| 288 | * @param \SimpleSAML\XMLSecurity\Key\X509Certificate|\SimpleSAML\XMLSecurity\Key\X509Certificate[] $certs |
||
| 289 | * An X509Certificate object or an array of them. |
||
| 290 | * @param boolean $addSubject Whether to add the subject of the certificate or not. |
||
| 291 | * @param string|false $digest A digest algorithm identifier if the digest of the certificate should be added. False |
||
| 292 | * otherwise. |
||
| 293 | * @param boolean $addIssuerSerial Whether to add the serial number of the issuer or not. |
||
| 294 | * |
||
| 295 | * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $certs is not a |
||
| 296 | * X509Certificate object or an array of them. |
||
| 297 | */ |
||
| 298 | public function addX509Certificates( |
||
| 299 | array $certs, |
||
| 300 | bool $addSubject = false, |
||
| 301 | $digest = false, |
||
| 302 | bool $addIssuerSerial = false |
||
| 303 | ): void { |
||
| 304 | Assert::allIsInstanceOf($certs, Key\X509Certificate::class); |
||
| 305 | |||
| 306 | $certData = []; |
||
| 307 | |||
| 308 | foreach ($certs as $cert) { |
||
| 309 | $details = $cert->getCertificateDetails(); |
||
| 310 | |||
| 311 | if ($addSubject && isset($details['subject'])) { |
||
| 312 | // add subject |
||
| 313 | $subjectNameValue = $details['issuer']; |
||
| 314 | if (is_array($details['subject'])) { |
||
| 315 | $parts = []; |
||
| 316 | foreach ($details['subject'] as $key => $value) { |
||
| 317 | if (is_array($value)) { |
||
| 318 | foreach ($value as $valueElement) { |
||
| 319 | array_unshift($parts, $key . '=' . $valueElement); |
||
| 320 | } |
||
| 321 | } else { |
||
| 322 | array_unshift($parts, $key . '=' . $value); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | $subjectNameValue = implode(',', $parts); |
||
| 326 | } |
||
| 327 | $certData[] = new X509SubjectName($subjectNameValue); |
||
| 328 | } |
||
| 329 | |||
| 330 | if ($digest !== false) { |
||
| 331 | // add certificate digest |
||
| 332 | $fingerprint = base64_encode(hex2bin($cert->getRawThumbprint($digest))); |
||
| 333 | $certData[] = new X509Digest($fingerprint, $digest); |
||
| 334 | } |
||
| 335 | |||
| 336 | if ($addIssuerSerial && isset($details['issuer']) && isset($details['serialNumber'])) { |
||
| 337 | $issuerName = CertificateUtils::parseIssuer($details['issuer']); |
||
| 338 | |||
| 339 | $certData[] = new X509IssuerSerial($issuerName, $details['serialNumber']); |
||
| 340 | } |
||
| 341 | |||
| 342 | $certData[] = new X509Certificate(CertificateUtils::stripHeaders($cert->getCertificate())); |
||
| 343 | } |
||
| 344 | |||
| 345 | $keyInfoNode = $this->createElement('KeyInfo'); |
||
| 346 | |||
| 347 | $certDataNode = new X509Data($certData); |
||
| 348 | $certDataNode->toXML($keyInfoNode); |
||
| 349 | |||
| 350 | if ($this->objectNode === null) { |
||
| 351 | $this->sigNode->appendChild($keyInfoNode); |
||
| 352 | } else { |
||
| 353 | $this->sigNode->insertBefore($keyInfoNode, $this->objectNode); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * Append a signature as the last child of the signed element. |
||
| 360 | * |
||
| 361 | * @return \DOMNode The appended signature. |
||
| 362 | */ |
||
| 363 | public function append(): DOMNode |
||
| 364 | { |
||
| 365 | return $this->insert(); |
||
| 366 | } |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * Use this signature as an enveloping signature, effectively adding the signed data to a ds:Object element. |
||
| 371 | * |
||
| 372 | * @param string|null $mimetype The mime type corresponding to the signed data. |
||
| 373 | * @param string|null $encoding The encoding corresponding to the signed data. |
||
| 374 | */ |
||
| 375 | public function envelop(string $mimetype = null, string $encoding = null): void |
||
| 378 | } |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * Build a new XML digital signature from a given document or node. |
||
| 383 | * |
||
| 384 | * @param \DOMNode $node The DOMDocument or DOMElement that contains the signature. |
||
| 385 | * |
||
| 386 | * @return Signature A Signature object corresponding to the signature present in the given DOM document or element. |
||
| 387 | * |
||
| 388 | * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $node is not |
||
| 389 | * an instance of DOMDocument or DOMElement. |
||
| 390 | * @throws \SimpleSAML\XMLSecurity\Exception\NoSignatureFound If there is no signature in the $node. |
||
| 391 | */ |
||
| 392 | public static function fromXML(DOMNode $node): Signature |
||
| 393 | { |
||
| 394 | Assert::isInstanceOfAny( |
||
| 395 | $node, |
||
| 396 | [DOMDocument::class, DOMElement::class], |
||
| 397 | 'Signatures can only be created from DOM documents or elements' |
||
| 398 | ); |
||
| 399 | |||
| 400 | $signature = self::findSignature($node); |
||
| 401 | if ($node instanceof DOMDocument) { |
||
| 402 | $node = $node->documentElement; |
||
| 403 | } |
||
| 404 | $dsig = new self($node); |
||
| 405 | $dsig->setSignatureElement($signature); |
||
| 406 | return $dsig; |
||
| 407 | } |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * Obtain the list of currently blacklisted algorithms. |
||
| 412 | * |
||
| 413 | * Signatures using blacklisted algorithms cannot be created or verified. |
||
| 414 | * |
||
| 415 | * @return array An array containing the identifiers of the algorithms blacklisted currently. |
||
| 416 | */ |
||
| 417 | public function getBlacklistedAlgorithms(): array |
||
| 418 | { |
||
| 419 | return $this->algBlacklist; |
||
| 420 | } |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * Get the list of namespaces to designate ID attributes. |
||
| 425 | * |
||
| 426 | * @return array An array of strings with the namespaces used in ID attributes. |
||
| 427 | */ |
||
| 428 | public function getIdNamespaces(): array |
||
| 429 | { |
||
| 430 | return $this->idNS; |
||
| 431 | } |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * Get a list of attributes used as an ID. |
||
| 436 | * |
||
| 437 | * @return array An array of strings with the attributes used as an ID. |
||
| 438 | */ |
||
| 439 | public function getIdAttributes(): array |
||
| 440 | { |
||
| 441 | return $this->idKeys; |
||
| 442 | } |
||
| 443 | |||
| 444 | |||
| 445 | /** |
||
| 446 | * Get the root configured for this signature. |
||
| 447 | * |
||
| 448 | * This will be the signed element, whether that's a user-provided XML element or a ds:Object element containing |
||
| 449 | * the actual data (which can in turn be either XML or not). |
||
| 450 | * |
||
| 451 | * @return \DOMElement The root element for this signature. |
||
| 452 | */ |
||
| 453 | public function getRoot(): DOMElement |
||
| 454 | { |
||
| 455 | return $this->root; |
||
| 456 | } |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * Get the identifier of the algorithm used in this signature. |
||
| 461 | * |
||
| 462 | * @return string The identifier of the algorithm used in this signature. |
||
| 463 | */ |
||
| 464 | public function getSignatureMethod(): string |
||
| 465 | { |
||
| 466 | return $this->sigAlg; |
||
| 467 | } |
||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * Get a list of elements verified by this signature. |
||
| 472 | * |
||
| 473 | * The elements in this list are referenced by the signature and the references verified to be correct. However, |
||
| 474 | * this doesn't mean the signature is valid, only that the references were so. |
||
| 475 | * |
||
| 476 | * Note that the list returned will be empty unless verify() has been called before. |
||
| 477 | * |
||
| 478 | * @return \DOMElement[] A list of elements correctly referenced by this signature. An empty list of verify() has |
||
| 479 | * not been called yet, or if the references couldn't be verified. |
||
| 480 | */ |
||
| 481 | public function getVerifiedElements(): array |
||
| 482 | { |
||
| 483 | return $this->verifiedElements; |
||
| 484 | } |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * Insert a signature as a child of the signed element, optionally before a given element. |
||
| 489 | * |
||
| 490 | * @param \DOMElement|false $before An optional DOM element the signature should be prepended to. |
||
| 491 | * |
||
| 492 | * @return \DOMNode The inserted signature. |
||
| 493 | * |
||
| 494 | * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If this signature is in enveloping mode. |
||
| 495 | */ |
||
| 496 | public function insert($before = false): DOMNode |
||
| 497 | { |
||
| 498 | Assert::false( |
||
| 499 | $this->enveloping, |
||
| 500 | 'Cannot insert the signature in the object it is enveloping.', |
||
| 501 | RuntimeException::class |
||
| 502 | ); |
||
| 503 | |||
| 504 | $signature = $this->root->ownerDocument->importNode($this->sigNode, true); |
||
| 505 | |||
| 506 | if ($before instanceof DOMElement) { |
||
| 507 | return $this->root->insertBefore($signature, $before); |
||
| 508 | } |
||
| 509 | return $this->root->insertBefore($signature); |
||
| 510 | } |
||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * Prepend a signature as the first child of the signed element. |
||
| 515 | * |
||
| 516 | * @return \DOMNode The prepended signature. |
||
| 517 | */ |
||
| 518 | public function prepend(): DOMNode |
||
| 527 | } |
||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * Set the backend to create or verify signatures. |
||
| 532 | * |
||
| 533 | * @param SignatureBackend $backend The SignatureBackend implementation to use. See individual algorithms for |
||
| 534 | * details about the default backends used. |
||
| 535 | */ |
||
| 536 | public function setBackend(SignatureBackend $backend): void |
||
| 537 | { |
||
| 538 | $this->backend = $backend; |
||
| 539 | } |
||
| 540 | |||
| 541 | |||
| 542 | /** |
||
| 543 | * Set the list of currently blacklisted algorithms. |
||
| 544 | * |
||
| 545 | * Signatures using blacklisted algorithms cannot be created or verified. |
||
| 546 | * |
||
| 547 | * @param array $algs An array containing the identifiers of the algorithms to blacklist. |
||
| 548 | */ |
||
| 549 | public function setBlacklistedAlgorithms(array $algs): void |
||
| 550 | { |
||
| 551 | $this->algBlacklist = $algs; |
||
| 552 | } |
||
| 553 | |||
| 554 | |||
| 555 | /** |
||
| 556 | * Set the canonicalization method used in this signature. |
||
| 557 | * |
||
| 558 | * Note that exclusive canonicalization without comments is used by default, so it's not necessary to call |
||
| 559 | * setCanonicalizationMethod() if that canonicalization method is desired. |
||
| 560 | * |
||
| 561 | * @param string $method The identifier of the canonicalization method to use. |
||
| 562 | * |
||
| 563 | * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $method is not a valid |
||
| 564 | * identifier of a supported canonicalization method. |
||
| 565 | */ |
||
| 566 | public function setCanonicalizationMethod(string $method): void |
||
| 567 | { |
||
| 568 | Assert::oneOf( |
||
| 569 | $method, |
||
| 570 | [ |
||
| 571 | C::C14N_EXCLUSIVE_WITH_COMMENTS, |
||
| 572 | C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, |
||
| 573 | C::C14N_INCLUSIVE_WITH_COMMENTS, |
||
| 574 | C::C14N_INCLUSIVE_WITHOUT_COMMENTS |
||
| 575 | ], |
||
| 576 | 'Invalid canonicalization method', |
||
| 577 | InvalidArgumentException::class |
||
| 578 | ); |
||
| 579 | |||
| 580 | $this->c14nMethod = $method; |
||
| 581 | $this->c14nMethodNode->setAttribute('Algorithm', $method); |
||
| 582 | } |
||
| 583 | |||
| 584 | |||
| 585 | /** |
||
| 586 | * Set the encoding for the signed contents in an enveloping signature. |
||
| 587 | * |
||
| 588 | * @param string $encoding The encoding used in the signed contents. |
||
| 589 | * |
||
| 590 | * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If this is not an enveloping signature. |
||
| 591 | */ |
||
| 592 | public function setEncoding(string $encoding): void |
||
| 593 | { |
||
| 594 | Assert::true( |
||
| 595 | $this->enveloping, |
||
| 596 | 'Cannot set the encoding for non-enveloping signatures.', |
||
| 597 | RuntimeException::class |
||
| 598 | ); |
||
| 599 | |||
| 600 | $this->root->setAttribute('Encoding', $encoding); |
||
| 601 | } |
||
| 602 | |||
| 603 | |||
| 604 | /** |
||
| 605 | * Set a list of attributes used as an ID. |
||
| 606 | * |
||
| 607 | * @param array $keys An array of strings with the attributes used as an ID. |
||
| 608 | */ |
||
| 609 | public function setIdAttributes(array $keys): void |
||
| 610 | { |
||
| 611 | $this->idKeys = $keys; |
||
| 612 | } |
||
| 613 | |||
| 614 | |||
| 615 | /** |
||
| 616 | * Set the list of namespaces to designate ID attributes. |
||
| 617 | * |
||
| 618 | * @param array $namespaces An array of strings with the namespaces used in ID attributes. |
||
| 619 | */ |
||
| 620 | public function setIdNamespaces(array $namespaces): void |
||
| 621 | { |
||
| 622 | $this->idNS = $namespaces; |
||
| 623 | } |
||
| 624 | |||
| 625 | |||
| 626 | /** |
||
| 627 | * Set the mime type for the signed contents in an enveloping signature. |
||
| 628 | * |
||
| 629 | * @param string $mimetype The mime type of the signed contents. |
||
| 630 | * |
||
| 631 | * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If this is not an enveloping signature. |
||
| 632 | */ |
||
| 633 | public function setMimeType(string $mimetype): void |
||
| 634 | { |
||
| 635 | Assert::true( |
||
| 636 | $this->enveloping, |
||
| 637 | 'Cannot set the mime type for non-enveloping signatures.', |
||
| 638 | RuntimeException::class |
||
| 639 | ); |
||
| 640 | |||
| 641 | $this->root->setAttribute('MimeType', $mimetype); |
||
| 642 | } |
||
| 643 | |||
| 644 | |||
| 645 | /** |
||
| 646 | * Set the signature element to a given one, and initialize the signature from there. |
||
| 647 | * |
||
| 648 | * @param \DOMElement $element A DOM element containing an XML signature. |
||
| 649 | * |
||
| 650 | * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException If the element does not correspond to an XML |
||
| 651 | * signature or it is malformed (e.g. there are missing mandatory elements or attributes). |
||
| 652 | */ |
||
| 653 | public function setSignatureElement(DOMElement $element): void |
||
| 654 | { |
||
| 655 | Assert::same($element->localName, 'Signature', InvalidDOMElementException::class); |
||
| 656 | Assert::same($element->namespaceURI, Sig::NS, InvalidDOMElementException::class); |
||
| 657 | |||
| 658 | $this->sigNode = $element; |
||
| 659 | |||
| 660 | $xp = XP::getXPath($this->sigNode->ownerDocument); |
||
| 661 | |||
| 662 | $signedInfoNodes = $xp->query('./ds:SignedInfo', $this->sigNode); |
||
| 663 | |||
| 664 | Assert::minCount( |
||
| 665 | $signedInfoNodes, |
||
| 666 | 1, |
||
| 667 | 'There is no SignedInfo element in the signature', |
||
| 668 | RuntimeException::class |
||
| 669 | ); |
||
| 670 | $this->sigInfoNode = $signedInfoNodes->item(0); |
||
| 671 | |||
| 672 | |||
| 673 | $this->sigAlg = $xp->evaluate('string(./ds:SignedInfo/ds:SignatureMethod/@Algorithm)', $this->sigNode); |
||
| 674 | Assert::stringNotEmpty($this->sigAlg, 'Unable to determine SignatureMethod', RuntimeException::class); |
||
| 675 | |||
| 676 | $c14nMethodNodes = $xp->query('./ds:CanonicalizationMethod', $this->sigInfoNode); |
||
| 677 | Assert::minCount( |
||
| 678 | $c14nMethodNodes, |
||
| 679 | 1, |
||
| 680 | 'There is no CanonicalizationMethod in the signature', |
||
| 681 | RuntimeException::class |
||
| 682 | ); |
||
| 683 | |||
| 684 | $this->c14nMethodNode = $c14nMethodNodes->item(0); |
||
| 685 | if (!$this->c14nMethodNode->hasAttribute('Algorithm')) { |
||
| 686 | throw new RuntimeException('CanonicalizationMethod missing required Algorithm attribute'); |
||
| 687 | } |
||
| 688 | $this->c14nMethod = $this->c14nMethodNode->getAttribute('Algorithm'); |
||
| 689 | } |
||
| 690 | |||
| 691 | |||
| 692 | /** |
||
| 693 | * Sign the document or element. |
||
| 694 | * |
||
| 695 | * This method will finish the signature process. It will create an XML signature valid for document or elements |
||
| 696 | * specified previously with addReference() or addReferences(). If none of those methods have been called previous |
||
| 697 | * to calling sign() (there are no references in the signature), the $root passed during construction of the |
||
| 698 | * Signature object will be referenced automatically. |
||
| 699 | * |
||
| 700 | * @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The key to use for signing. Bear in mind that the type of |
||
| 701 | * this key must be compatible with the types of key accepted by the algorithm specified in $alg. |
||
| 702 | * @param string $alg The identifier of the signature algorithm to use. See \SimpleSAML\XMLSecurity\Constants. |
||
| 703 | * @param bool $appendToNode Whether to append the signature as the last child of the root element or not. |
||
| 704 | * |
||
| 705 | * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $appendToNode is true and |
||
| 706 | * this is an enveloping signature. |
||
| 707 | */ |
||
| 708 | public function sign(Key\AbstractKey $key, string $alg, bool $appendToNode = false): void |
||
| 709 | { |
||
| 710 | Assert::false( |
||
| 711 | ($this->enveloping && $appendToNode), |
||
| 712 | 'Cannot append the signature, we are in enveloping mode.', |
||
| 713 | InvalidArgumentException::class |
||
| 714 | ); |
||
| 715 | |||
| 716 | $this->sigMethodNode->setAttribute('Algorithm', $alg); |
||
| 717 | $factory = new SignatureAlgorithmFactory($this->algBlacklist); |
||
| 718 | $signer = $factory->getAlgorithm($alg, $key); |
||
| 719 | if ($this->backend !== null) { |
||
| 720 | $signer->setBackend($this->backend); |
||
| 721 | } |
||
| 722 | |||
| 723 | if (empty($this->references)) { |
||
| 724 | // no references have been added, ref root |
||
| 725 | $transforms = []; |
||
| 726 | if (!$this->enveloping) { |
||
| 727 | $transforms[] = C::XMLDSIG_ENVELOPED; |
||
| 728 | } |
||
| 729 | $this->addReference($this->root->ownerDocument, $signer->getDigest(), $transforms, []); |
||
| 730 | } |
||
| 731 | |||
| 732 | if ($appendToNode) { |
||
| 733 | $this->sigNode = $this->append(); |
||
| 734 | } elseif (in_array($this->c14nMethod, [C::C14N_INCLUSIVE_WITHOUT_COMMENTS, C::C14N_INCLUSIVE_WITH_COMMENTS])) { |
||
| 735 | // append Signature to root node for inclusive canonicalization |
||
| 736 | $restoreSigNode = $this->sigNode; |
||
| 737 | $this->sigNode = $this->prepend(); |
||
| 738 | } |
||
| 739 | |||
| 740 | $sigValue = base64_encode($signer->sign($this->canonicalizeData($this->sigInfoNode, $this->c14nMethod))); |
||
| 741 | |||
| 742 | // remove Signature from node if we added it for c14n |
||
| 743 | if ( |
||
| 744 | !$appendToNode && |
||
| 745 | in_array($this->c14nMethod, [C::C14N_INCLUSIVE_WITHOUT_COMMENTS, C::C14N_INCLUSIVE_WITH_COMMENTS]) |
||
| 746 | ) { // remove from root in case we added it for inclusive canonicalization |
||
| 747 | $this->root->removeChild($this->root->lastChild); |
||
| 748 | /** @var \DOMElement $restoreSigNode */ |
||
| 749 | $this->sigNode = $restoreSigNode; |
||
| 750 | } |
||
| 751 | |||
| 752 | $sigValueNode = $this->createElement('SignatureValue', $sigValue); |
||
| 753 | if ($this->sigInfoNode->nextSibling) { |
||
| 754 | $this->sigInfoNode->nextSibling->parentNode->insertBefore($sigValueNode, $this->sigInfoNode->nextSibling); |
||
| 755 | } else { |
||
| 756 | $this->sigNode->appendChild($sigValueNode); |
||
| 757 | } |
||
| 758 | } |
||
| 759 | |||
| 760 | |||
| 761 | /** |
||
| 762 | * Verify this signature with a given key. |
||
| 763 | * |
||
| 764 | * @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The key to use to verify this signature. |
||
| 765 | * |
||
| 766 | * @return bool True if the signature can be verified with $key, false otherwise. |
||
| 767 | * |
||
| 768 | * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If there is no SignatureValue in |
||
| 769 | * the signature, or we couldn't verify all the references. |
||
| 770 | */ |
||
| 771 | public function verify(Key\AbstractKey $key): bool |
||
| 772 | { |
||
| 773 | $xp = XP::getXPath($this->sigNode->ownerDocument); |
||
| 774 | $sigval = $xp->evaluate('string(./ds:SignatureValue)', $this->sigNode); |
||
| 775 | if (empty($sigval)) { |
||
| 776 | throw new RuntimeException('Unable to locate SignatureValue'); |
||
| 777 | } |
||
| 778 | |||
| 779 | $siginfo = $this->canonicalizeData($this->sigInfoNode, $this->c14nMethod); |
||
| 780 | if (!$this->validateReferences()) { |
||
| 781 | throw new RuntimeException('Unable to verify all references'); |
||
| 782 | } |
||
| 783 | |||
| 784 | $factory = new SignatureAlgorithmFactory($this->algBlacklist); |
||
| 785 | $alg = $factory->getAlgorithm($this->sigAlg, $key); |
||
| 786 | if ($this->backend !== null) { |
||
| 787 | $alg->setBackend($this->backend); |
||
| 788 | } |
||
| 789 | return $alg->verify($siginfo, base64_decode($sigval)); |
||
| 790 | } |
||
| 791 | |||
| 792 | |||
| 793 | /** |
||
| 794 | * Canonicalize any given node. |
||
| 795 | * |
||
| 796 | * @param \DOMNode $node The DOM node that needs canonicalization. |
||
| 797 | * @param string $c14nMethod The identifier of the canonicalization algorithm to use. |
||
| 798 | * See \SimpleSAML\XMLSecurity\Constants. |
||
| 799 | * @param array|null $xpaths An array of xpaths to filter the nodes by. Defaults to null (no filters). |
||
| 800 | * @param array|null $prefixes An array of namespace prefixes to filter the nodes by. Defaults to null (no filters). |
||
| 801 | * |
||
| 802 | * @return string The canonical representation of the given DOM node, according to the algorithm requested. |
||
| 803 | */ |
||
| 804 | protected function canonicalizeData( |
||
| 805 | DOMNode $node, |
||
| 806 | string $c14nMethod, |
||
| 807 | array $xpaths = null, |
||
| 808 | array $prefixes = null |
||
| 809 | ): string { |
||
| 810 | $exclusive = false; |
||
| 811 | $withComments = false; |
||
| 812 | switch ($c14nMethod) { |
||
| 813 | case C::C14N_EXCLUSIVE_WITH_COMMENTS: |
||
| 814 | case C::C14N_INCLUSIVE_WITH_COMMENTS: |
||
| 815 | $withComments = true; |
||
| 816 | } |
||
| 817 | switch ($c14nMethod) { |
||
| 818 | case C::C14N_EXCLUSIVE_WITH_COMMENTS: |
||
| 819 | case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS: |
||
| 820 | $exclusive = true; |
||
| 821 | } |
||
| 822 | |||
| 823 | if ( |
||
| 824 | is_null($xpaths) |
||
| 825 | && ($node->ownerDocument !== null) |
||
| 826 | && $node->isSameNode($node->ownerDocument->documentElement) |
||
| 827 | ) { |
||
| 828 | // check for any PI or comments as they would have been excluded |
||
| 829 | $element = $node; |
||
| 830 | while ($refNode = $element->previousSibling) { |
||
| 831 | if ( |
||
| 832 | (($refNode->nodeType === XML_COMMENT_NODE) && $withComments) |
||
| 833 | || $refNode->nodeType === XML_PI_NODE |
||
| 834 | ) { |
||
| 835 | break; |
||
| 836 | } |
||
| 837 | $element = $refNode; |
||
| 838 | } |
||
| 839 | if ($refNode == null) { |
||
| 840 | $node = $node->ownerDocument; |
||
| 841 | } |
||
| 842 | } |
||
| 843 | |||
| 844 | return $node->C14N($exclusive, $withComments, $xpaths, $prefixes); |
||
| 845 | } |
||
| 846 | |||
| 847 | |||
| 848 | /** |
||
| 849 | * Create a new element in this signature. |
||
| 850 | * |
||
| 851 | * @param string $name The name of this element. |
||
| 852 | * @param string|null $content The text contents of the element, or null if it is not supposed to have any text |
||
| 853 | * contents. Defaults to null. |
||
| 854 | * @param string $ns The namespace the new element must be created under. Defaults to the standard XMLDSIG |
||
| 855 | * namespace. |
||
| 856 | * |
||
| 857 | * @return \DOMElement A new DOM element with the given name. |
||
| 858 | */ |
||
| 859 | protected function createElement( |
||
| 860 | string $name, |
||
| 861 | string $content = null, |
||
| 862 | string $ns = C::XMLDSIGNS |
||
| 863 | ): DOMElement { |
||
| 864 | if ($this->sigNode === null) { |
||
| 865 | // initialize signature |
||
| 866 | $doc = DOMDocumentFactory::create(); |
||
| 867 | } else { |
||
| 868 | $doc = $this->sigNode->ownerDocument; |
||
| 869 | } |
||
| 870 | |||
| 871 | $nsPrefix = $this->nsPrefix; |
||
| 872 | |||
| 873 | if ($content !== null) { |
||
| 874 | return $doc->createElementNS($ns, $nsPrefix . $name, $content); |
||
| 875 | } |
||
| 876 | |||
| 877 | return $doc->createElementNS($ns, $nsPrefix . $name); |
||
| 878 | } |
||
| 879 | |||
| 880 | |||
| 881 | /** |
||
| 882 | * Find a signature from a given node. |
||
| 883 | * |
||
| 884 | * @param \DOMNode $node A DOMElement node where a signature is expected as a child (enveloped) or a DOMDocument |
||
| 885 | * node to search for document signatures (one single reference with an empty URI). |
||
| 886 | * |
||
| 887 | * @return \DOMElement The signature element. |
||
| 888 | * |
||
| 889 | * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If there is no DOMDocument element available. |
||
| 890 | * @throws \SimpleSAML\XMLSecurity\Exception\NoSignatureFound If no signature is found. |
||
| 891 | */ |
||
| 892 | protected static function findSignature(DOMNode $node): DOMElement |
||
| 905 | } |
||
| 906 | |||
| 907 | |||
| 908 | /** |
||
| 909 | * Compute the hash for some data with a given algorithm. |
||
| 910 | * |
||
| 911 | * @param string $alg The identifier of the algorithm to use. |
||
| 912 | * @param string $data The data to digest. |
||
| 913 | * @param bool $encode Whether to bas64-encode the result or not. Defaults to true. |
||
| 914 | * |
||
| 915 | * @return string The (binary or base64-encoded) digest corresponding to the given data. |
||
| 916 | * |
||
| 917 | * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If $alg is not a valid |
||
| 918 | * identifier of a supported digest algorithm. |
||
| 919 | */ |
||
| 920 | protected function hash(string $alg, string $data, bool $encode = true): string |
||
| 921 | { |
||
| 922 | Assert::keyExists( |
||
| 923 | C::$DIGEST_ALGORITHMS, |
||
| 924 | $alg, |
||
| 925 | 'Unsupported digest method "%s"', |
||
| 926 | InvalidArgumentException::class |
||
| 927 | ); |
||
| 928 | |||
| 929 | $digest = hash(C::$DIGEST_ALGORITHMS[$alg], $data, true); |
||
| 930 | return $encode ? base64_encode($digest) : $digest; |
||
| 931 | } |
||
| 932 | |||
| 933 | |||
| 934 | /** |
||
| 935 | * Initialize the basic structure of a signature from scratch. |
||
| 936 | * |
||
| 937 | */ |
||
| 938 | protected function initSignature(): void |
||
| 950 | } |
||
| 951 | |||
| 952 | |||
| 953 | /** |
||
| 954 | * Process a given reference, by looking for it, processing the specified transforms, canonicalizing the result |
||
| 955 | * and comparing its corresponding digest. |
||
| 956 | * |
||
| 957 | * Verified references will be stored in the "verifiedElements" property. |
||
| 958 | * |
||
| 959 | * @param \DOMElement $ref The ds:Reference element to process. |
||
| 960 | * |
||
| 961 | * @return bool True if the digest of the processed reference matches the one given, false otherwise. |
||
| 962 | * |
||
| 963 | * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If the referenced element is missing or |
||
| 964 | * the reference points to an external document. |
||
| 965 | * |
||
| 966 | * @see http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel |
||
| 967 | */ |
||
| 968 | protected function processReference(DOMElement $ref): bool |
||
| 969 | { |
||
| 970 | /* |
||
| 971 | * Depending on the URI, we may need to remove comments during canonicalization. |
||
| 972 | * See: http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel |
||
| 973 | */ |
||
| 974 | $includeCommentNodes = true; |
||
| 975 | $dataObject = $ref->ownerDocument; |
||
| 976 | if ($ref->hasAttribute("URI")) { |
||
| 977 | $uri = $ref->getAttribute('URI'); |
||
| 978 | if (empty($uri)) { |
||
| 979 | // this reference identifies the enclosing XML, it should not include comments |
||
| 980 | $includeCommentNodes = false; |
||
| 981 | } |
||
| 982 | $arUrl = parse_url($uri); |
||
| 983 | if (empty($arUrl['path'])) { |
||
| 984 | if ($identifier = @$arUrl['fragment']) { |
||
| 985 | /* |
||
| 986 | * This reference identifies a node with the given ID by using a URI on the form '#identifier'. |
||
| 987 | * This should not include comments. |
||
| 988 | */ |
||
| 989 | $includeCommentNodes = false; |
||
| 990 | |||
| 991 | $xp = XP::getXPath($ref->ownerDocument); |
||
| 992 | if ($this->idNS && is_array($this->idNS)) { |
||
| 993 | foreach ($this->idNS as $nspf => $ns) { |
||
| 994 | $xp->registerNamespace($nspf, $ns); |
||
| 995 | } |
||
| 996 | } |
||
| 997 | $iDlist = '@Id="' . $identifier . '"'; |
||
| 998 | if (is_array($this->idKeys)) { |
||
| 999 | foreach ($this->idKeys as $idKey) { |
||
| 1000 | $iDlist .= " or @$idKey='$identifier'"; |
||
| 1001 | } |
||
| 1002 | } |
||
| 1003 | $query = '//*[' . $iDlist . ']'; |
||
| 1004 | $dataObject = $xp->query($query)->item(0); |
||
| 1005 | if ($dataObject === null) { |
||
| 1006 | throw new RuntimeException('Reference not found'); |
||
| 1007 | } |
||
| 1008 | } |
||
| 1009 | } else { |
||
| 1010 | throw new RuntimeException('Processing of external documents is not supported'); |
||
| 1011 | } |
||
| 1012 | } else { |
||
| 1013 | // this reference identifies the root node with an empty URI, it should not include comments |
||
| 1014 | $includeCommentNodes = false; |
||
| 1015 | } |
||
| 1016 | |||
| 1017 | $data = $this->processTransforms($ref, $dataObject, $includeCommentNodes); |
||
| 1018 | if (!$this->validateDigest($ref, $data)) { |
||
| 1019 | return false; |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | // parse the canonicalized reference... |
||
| 1023 | $doc = DOMDocumentFactory::create(); |
||
| 1024 | $doc->loadXML($data); |
||
| 1025 | $dataObject = $doc->documentElement; |
||
| 1026 | |||
| 1027 | // ... and add it to the list of verified elements |
||
| 1028 | if (!empty($identifier)) { |
||
| 1029 | $this->verifiedElements[$identifier] = $dataObject; |
||
| 1030 | } else { |
||
| 1031 | $this->verifiedElements[] = $dataObject; |
||
| 1032 | } |
||
| 1033 | |||
| 1034 | return true; |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Process all transforms specified by a given Reference element. |
||
| 1040 | * |
||
| 1041 | * @param \DOMElement $ref The Reference element. |
||
| 1042 | * @param mixed $data The data referenced. |
||
| 1043 | * @param bool $includeCommentNodes Whether to allow canonicalization with comments or not. |
||
| 1044 | * |
||
| 1045 | * @return string The canonicalized data after applying all transforms specified by $ref. |
||
| 1046 | * |
||
| 1047 | * @see http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel |
||
| 1048 | */ |
||
| 1049 | protected function processTransforms(DOMElement $ref, $data, bool $includeCommentNodes = false): string |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | |||
| 1129 | /** |
||
| 1130 | * Compute and compare the digest corresponding to some data given to the one specified by a reference. |
||
| 1131 | * |
||
| 1132 | * @param \DOMElement $ref The ds:Reference element containing the digest. |
||
| 1133 | * @param string $data The referenced element, canonicalized, to digest and compare. |
||
| 1134 | * |
||
| 1135 | * @return bool True if the resulting digest matches the one in the reference, false otherwise. |
||
| 1136 | */ |
||
| 1137 | protected function validateDigest(DOMElement $ref, string $data): bool |
||
| 1138 | { |
||
| 1139 | $xp = XP::getXPath($ref->ownerDocument); |
||
| 1140 | $alg = $xp->evaluate('string(./ds:DigestMethod/@Algorithm)', $ref); |
||
| 1141 | $computed = $this->hash($alg, $data, false); |
||
| 1142 | $evaluated = base64_decode($xp->evaluate('string(./ds:DigestValue)', $ref)); |
||
| 1143 | return Sec::compareStrings($computed, $evaluated); |
||
| 1144 | } |
||
| 1145 | |||
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Iterate over the references specified by the signature, apply their transforms, and validate their digests |
||
| 1149 | * against the referenced elements. |
||
| 1150 | * |
||
| 1151 | * @return boolean True if all references could be verified, false otherwise. |
||
| 1152 | * |
||
| 1153 | * @throws \SimpleSAML\XMLSecurity\Exception\RuntimeException If there are no references. |
||
| 1154 | */ |
||
| 1155 | protected function validateReferences(): bool |
||
| 1174 | } |
||
| 1175 | } |
||
| 1176 |
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