title: How can I convert the private key stored in HSM to SignedXml.SigningKey in C#
body: <p>I'm trying to implement some demo of XML signing with a certificate which stored in the HSM.</p>
  
  <p>I found some interesting example from this link:
  <a href="http://www.asptricks.net/2015/09/sign-xmldocument-with-x509certificate2.html" rel="nofollow noreferrer">Sign XML Document with X509Certificate2</a> and modified it to using certificate and key inside the HSM with PKCS11Interop wrapper.</p>
  
  <p>But anyone could give me a suggestion or example to <strong>convert ObjectHandle privateKey from HSM to SignedXML.SigningKey</strong></p>
  
  <pre><code>private static void SignXmlWithCertificate(XmlDocument xmlDoc, X509Certificate2 cert, Session session, String alias)
          {
              SignedXml signedXml = new SignedXml(xmlDoc);
  
              List&lt;ObjectAttribute&gt; template = new List&lt;ObjectAttribute&gt;();
              template.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));
              template.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_RSA));
              template.Add(new ObjectAttribute(CKA.CKA_LABEL, alias));
              List&lt;ObjectHandle&gt; foundObjects = session.FindAllObjects(template);
              ObjectHandle privateKey = foundObjects[0];
  
              signedXml.SigningKey = privateKey; //Here is where I stuck.
  </code></pre>
  
  <p>In the example from above external link. They using a certificate which combined private key. Then they can use like this.</p>
  
  <pre><code>signedXml.SigningKey = cert.PrivateKey;
  </code></pre>
  
  <p>But the certificate that I'm using haven't content of private key inside. Please give me some suggestion.</p>