timlegge

Untitled

Feb 4th, 2023
1,429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.27 KB | None | 0 0
  1. diff --git a/lib/XML/Sig.pm b/lib/XML/Sig.pm
  2. index adf608d..b8bd2cf 100644
  3. --- a/lib/XML/Sig.pm
  4. +++ b/lib/XML/Sig.pm
  5. @@ -346,9 +346,6 @@ sub sign {
  6.          # Get the XML note to sign base on the ID
  7.          my $xml = $self->_get_xml_to_sign($signid);
  8.  
  9. -        # Set the namespace but do not apply it to the XML
  10. -        $xml->setNamespace("http://www.w3.org/2000/09/xmldsig#", "dsig", 0);
  11. -
  12.          # Canonicalize the XML to http://www.w3.org/2001/10/xml-exc-c14n#
  13.          # TODO Change the Canonicalization method in the xml fragment from _signedinfo_xml
  14.          #    <dsig:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
  15. @@ -374,20 +371,25 @@ sub sign {
  16.          my $reference = $signid; #$self->{parser}->findvalue('//@ID', $xml);
  17.          print ("   Reference URI: $reference\n") if $DEBUG;
  18.  
  19. -        # Add the Signature to the xml being signed
  20. -        $xml->appendWellBalancedChunk($signature_xml, 'UTF-8');
  21. +        local $XML::LibXML::skipXMLDeclaration = $self->{ no_xml_declaration };
  22. +
  23. +        my $signature_dom = XML::LibXML->load_xml( string => $signature_xml );
  24. +
  25. +        my $xpath = XML::LibXML::XPathContext->new($signature_dom);
  26. +        $xpath->registerNs('dsig', 'http://www.w3.org/2000/09/xmldsig#');
  27. +        $xpath->registerNs('ec', 'http://www.w3.org/2001/10/xml-exc-c14n#');
  28.  
  29.          # Canonicalize the SignedInfo to http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments
  30.          # TODO Change the Canonicalization method in the xml fragment from _signedinfo_xml
  31.  
  32. -        my ($signature_node) = $xml->findnodes(
  33. -            './dsig:Signature', $xml);
  34. -        my ($signed_info_node) = $xml->findnodes(
  35. -            './dsig:Signature/dsig:SignedInfo',$xml);
  36. +        my ($signature_node) = $xpath->findnodes(
  37. +            '/dsig:Signature', $signature_xml);
  38. +        my ($signed_info_node) = $xpath->findnodes(
  39. +            '/dsig:Signature/dsig:SignedInfo',$signature_xml);
  40.  
  41.          # Add the digest value to the Signed info
  42. -        my ($digest_value_node) = $xml->findnodes(
  43. -            './dsig:Signature/dsig:SignedInfo/dsig:Reference/dsig:DigestValue', $signature_node);
  44. +        my ($digest_value_node) = $xpath->findnodes(
  45. +            '/dsig:Signature/dsig:SignedInfo/dsig:Reference/dsig:DigestValue', $signature_xml);
  46.          $digest_value_node->removeChildNodes();
  47.          $digest_value_node->appendText($digest);
  48.  
  49. @@ -412,11 +414,23 @@ sub sign {
  50.          }
  51.  
  52.          # Add the Signature to the SignatureValue
  53. -        my ($signature_value_node) = $xml->findnodes(
  54. -            './dsig:Signature/dsig:SignatureValue', $signature_node);
  55. +        my ($signature_value_node) = $xpath->findnodes(
  56. +            '/dsig:Signature/dsig:SignatureValue', $signature_xml);
  57.          $signature_value_node->removeChildNodes();
  58.          $signature_value_node->appendText($signature);
  59.  
  60. +        my $set = $xpath->findnodes('dsig:Signature');
  61. +
  62. +        my $node;
  63. +        if ($set->size) {
  64. +            $node = $set->get_node(1)->cloneNode( 1 );
  65. +        }
  66. +
  67. +        my $root = $dom->findnodes("//*[\@ID=\'$signid\']");
  68. +
  69. +        my $loc = $root->shift();
  70. +        $loc->addChild($node);
  71. +
  72.          print ("\n\n\n SignatureValue:\n" . $signature_value_node . "\n\n\n") if $DEBUG;
  73.      }
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment