title: XML Signature - Different signature value in PHP?
body: <p>I want to implement an XML digital signature in PHP. I'm testing the correctness of the signature at <a href="https://www.aleksey.com/xmlsec/xmldsig-verifier.html" rel="nofollow noreferrer">this verifier</a>.</p>
  
  <p>I'm getting the wrong signature value, so I'm going to explain step by step what I'm doing and please correct what I'm doing wrong.</p>
  
  <p>The XML I want to sign <em>(without new lines)</em>:</p>
  
  <pre><code>&lt;root&gt;
      &lt;node1&gt;test&lt;/node1&gt;
      &lt;node2 attr="value" /&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p><strong>First</strong>, I canonicalize the XML and then hash it using sha256 which yields the correct digest value.</p>
  
  <p><strong>Second</strong>, creation of SignedInfo XML element and canonicalizing it <em>(without new lines)</em>:</p>
  
  <pre><code>&lt;SignedInfo&gt;
      &lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"&gt;&lt;/CanonicalizationMethod&gt;
      &lt;SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"&gt;&lt;/SignatureMethod&gt;
      &lt;Reference URI=""&gt;
          &lt;Transforms&gt;
              &lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"&gt;&lt;/Transform&gt;
          &lt;/Transforms&gt;
          &lt;DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"&gt;&lt;/DigestMethod&gt;
          &lt;DigestValue&gt;EdtGJKqTvnmb0Z72M8ZxTO6Jv2jyXOohMgxVKe2Gkbo=&lt;/DigestValue&gt;
      &lt;/Reference&gt;
  &lt;/SignedInfo&gt;
  </code></pre>
  
  <p><strong>Finally</strong>, signature of SignedInfo element using RSA-SHA256. This is the part that yields an incorrect result.</p>
  
  <p>Signature is calculated using the following lines of code:</p>
  
  <pre><code>openssl_sign($c14nSignedInfo,$signature,$privateKey,"sha256WithRSAEncryption");
  $signature = base64_encode($signature);
  </code></pre>
  
  <p>But it yields an incorrect signature value, as stated by the <a href="https://www.aleksey.com/xmlsec/xmldsig-verifier.html" rel="nofollow noreferrer">verifier</a>. If I sign the same XML in C# using the same private key, I get the correct signature. So, what am I doing wrong? What am I missing? Clearly, I am signing the wrong thing, but what should I sign then? Should I hash the SignedInfo element before signing it?</p>
  
  <p>This is my final result <em>(which is incorrect)</em>:</p>
  
  <pre><code>&lt;root&gt;&lt;node1&gt;test&lt;/node1&gt;&lt;node2 attr="value" /&gt;&lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;&lt;SignedInfo&gt;&lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /&gt;&lt;SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /&gt;&lt;Reference URI=""&gt;&lt;Transforms&gt;&lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /&gt;&lt;/Transforms&gt;&lt;DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /&gt;&lt;DigestValue&gt;EdtGJKqTvnmb0Z72M8ZxTO6Jv2jyXOohMgxVKe2Gkbo=&lt;/DigestValue&gt;&lt;/Reference&gt;&lt;/SignedInfo&gt;&lt;SignatureValue&gt;cqkvNxWf8Z03K01SQFk/J5Z7F/l8scSFpRECCknTH840rUm2L9lKHC8UF/SGKJ4LoVxOmYKJHyq0Dx8j6shTXTK1Abm0a3Ty3IP/V0Roj+3EApq4Hwr7VOpvZjcToQj1snuUtgPZFJ6pxPWdYJ5hZhxm0C+mDMlOCgcTuWP7UIDNQ3CSC1GMcKESEkxsfEAzIXNh9wHoIY2e2HnedceFzOsJLPaLnltd1qhewJvqYCq7M1gD8e+Hv5Lyo+wG7ipvxvhAQ4Ui+BAOD9mROzSQaiirrxg6nC/dMJyWketTjKwEprZDm5BOoMoJC+kb2PvShfXrdRgA/ezWZHIaT0mLGQ==&lt;/SignatureValue&gt;&lt;KeyInfo&gt;&lt;KeyValue&gt;&lt;RSAKeyValue&gt;&lt;Modulus&gt;nENZ+u9FZ5DSDlFRZ+0f0tZ2Kvl6QY+cjUzSc89fhixKFuexGJQcEvCrs67x8QcgwxZCuoWHk/Cdh8qd3x0EZaC+ZoZ+AF7ofoxWPHioZ86CIuxhI4Zgk0bHWibSKx8Z7EIXVrQ1nM2OvX9CdM9iVjM0yfn1ohdd1o4EKmRBbJQf6kCZMTbCdOdr8UI0xGUMjjaZN6+vGj3VYoxlQXXi11NMHDlxsCyyyjBjyCvewnTerkXAomwf92xV77siOn1VZD2/YwWarv1Hk/0WtW1c6QGj0VNd4EbzUqvMtnkzY3301hz5MRqvPiPNez9tWMaawDzbMrGGkwbF2ivVAW3LHQ==&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;/RSAKeyValue&gt;&lt;/KeyValue&gt;&lt;/KeyInfo&gt;&lt;/Signature&gt;&lt;/root&gt;
  </code></pre>
  
  <p>And this is the correct result <em>(everything is the same, except signature value)</em>:</p>
  
  <pre><code>&lt;root&gt;&lt;node1&gt;test&lt;/node1&gt;&lt;node2 attr="value" /&gt;&lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;&lt;SignedInfo&gt;&lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /&gt;&lt;SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /&gt;&lt;Reference URI=""&gt;&lt;Transforms&gt;&lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /&gt;&lt;/Transforms&gt;&lt;DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /&gt;&lt;DigestValue&gt;EdtGJKqTvnmb0Z72M8ZxTO6Jv2jyXOohMgxVKe2Gkbo=&lt;/DigestValue&gt;&lt;/Reference&gt;&lt;/SignedInfo&gt;&lt;SignatureValue&gt;OzQtkphZ+eg8JnVHzf8BVZpUZ/xMFQJhkmw5j4h2+eu8PAJh8Z9mRZH5uN/hfPFgkByBTshS/VWDNyWyF6TclQRSc8m89L8sCBQhKqGxZKjCd7V8XUIXgRgh2+Zl1JZQ/hD36XESEPazFyQ26KWq+T+m1Tc541Rv3mklfOKv2qBOqZLd/n/nRnGhFJYMp6PtPMNk/BezosGaQFUu/IhSI5tiud5ia4qETk/1G1eXAXmE4RbnVMefkysarTjizJYkGRqW10f0cF0trGxbCPyohMMfb2msnYiQfZXZd0tW41mMpH0R0AHFeC7RPxK2GzxRMRJCkNeWe65brneUtUSHzA==&lt;/SignatureValue&gt;&lt;KeyInfo&gt;&lt;KeyValue&gt;&lt;RSAKeyValue&gt;&lt;Modulus&gt;nENZ+u9FZ5DSDlFRZ+0f0tZ2Kvl6QY+cjUzSc89fhixKFuexGJQcEvCrs67x8QcgwxZCuoWHk/Cdh8qd3x0EZaC+ZoZ+AF7ofoxWPHioZ86CIuxhI4Zgk0bHWibSKx8Z7EIXVrQ1nM2OvX9CdM9iVjM0yfn1ohdd1o4EKmRBbJQf6kCZMTbCdOdr8UI0xGUMjjaZN6+vGj3VYoxlQXXi11NMHDlxsCyyyjBjyCvewnTerkXAomwf92xV77siOn1VZD2/YwWarv1Hk/0WtW1c6QGj0VNd4EbzUqvMtnkzY3301hz5MRqvPiPNez9tWMaawDzbMrGGkwbF2ivVAW3LHQ==&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;/RSAKeyValue&gt;&lt;/KeyValue&gt;&lt;/KeyInfo&gt;&lt;/Signature&gt;&lt;/root&gt;
  </code></pre>
  
  <p>To test both results or for more details, just copy them <a href="https://www.aleksey.com/xmlsec/xmldsig-verifier.html" rel="nofollow noreferrer">here</a> and click "Verify Signature".</p>