Skip to content

Commit

Permalink
Adds support for signing a specific node identified by id
Browse files Browse the repository at this point in the history
  • Loading branch information
bseddon committed Feb 26, 2022
1 parent 48a37a7 commit 1f6fb68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/InputResourceInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class InputResourceInfo extends BaseInputResourceInfo
*/
public $hugeFile = false;

/**
* The value of an id to select when signing
*
* @var string
*/
public $uri = null;

/**
* Returns true if the transforms contains one that is enveloped
*
Expand Down
18 changes: 16 additions & 2 deletions src/XAdES.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,23 @@ public function signXAdESFile( $xmlResource, $certificateResource, $keyResource
)
);

// Check if a URI has been given that might be an Id
$node = $doc;
if ( ! $xmlResource->detached && $xmlResource->uri && ! filter_var( $xmlResource->uri, FILTER_VALIDATE_URL ) )
{
$node = false;
$xpath = new \DOMXPath( $doc );
$nodes = $xpath->query( "//*[@Id='{$xmlResource->uri}']" );
if ( $nodes && $nodes->count() )
{
$node = $nodes[0];
$xmlResource->uri = '#' . $xmlResource->uri;
}
}

// Sign using SHA-256
$this->addReference(
$doc, // Content
$node, // Content
XMLSecurityDSig::SHA256, // Algorithm
$xmlResource->convertTransforms( ! $xmlResource->detached ), // Transforms
array( // Options
Expand All @@ -486,7 +500,7 @@ public function signXAdESFile( $xmlResource, $certificateResource, $keyResource
? XMLSecurityDSig::encodedUrl( parse_url( $xmlResource->resource ) )
: basename( $xmlResource->resource )
)
: true,
: $xmlResource->uri ?? true,
'id' => $referenceId,
)
);
Expand Down

0 comments on commit 1f6fb68

Please sign in to comment.