title: Microsoft.Xades Open Source Dll Using PKCS11
body: <p>XADES. I stuck at one point please help me out. iam using CSP to select the certificate from the store but i don't need an action from the user i only need to use pkcs11 wrapper to extract the private key from the smart-card [token] and use it for signature . iam using pkcs11 interop .net wrappr 
  below is the code i use to extract the certificate from the store</p>
  
  <pre><code>  X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                  store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
              X509Certificate2Collection collection = (X509Certificate2Collection) store.Certificates;
              X509Certificate2Collection fcollection =
                  (X509Certificate2Collection) collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
              X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection,
                  "XAdES sample", "Choose a certificate", X509SelectionFlag.SingleSelection);
  
              if (scollection != null &amp;&amp; scollection.Count == 1)
              {
                  cert = scollection[0];
  
                  if (cert.HasPrivateKey == false)
                  {
                      MessageBox.Show("This certificate does not have a private key associated with it");
                      cert = null;
                  }
              }
  
              store.Close();
  </code></pre>
  
  <p>and the code that i use to extract the private key from the smart card as below </p>
  
  <pre><code>      Pkcs11 pkcs11 = new Pkcs11(_pkcs11LibraryPath, false);
          //Get Info
          LibraryInfo libraryInfo = pkcs11.GetInfo();
  
          // Get list of available slots
          List&lt;Slot&gt; slots = pkcs11.GetSlotList(false);
  
          //Login to business slot 
          Slot paciSlot = slots[2];
          // Open RO session
          using (Session session = paciSlot.OpenSession(false))
          {
              //Sign and verify with certificate
  
              //Get Private Key
              List&lt;ObjectAttribute&gt; objectAttributes = new List&lt;ObjectAttribute&gt;();
              objectAttributes = new List&lt;ObjectAttribute&gt;();
  
              objectAttributes = new List&lt;ObjectAttribute&gt;();
              objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));
              objectAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_RSA));
  
              session.FindObjectsInit(objectAttributes);
              var oObjCollection = session.FindObjects(1);
  
  
              ObjectHandle oPrivKeyObjectHandle = new ObjectHandle();
              if (oObjCollection.Count &gt; 0)
              {
                  oPrivKeyObjectHandle = oObjCollection[0];
              }}
  </code></pre>
  
  <p>so how I use the private key from the smart card with Microsoft.xades dll .</p>
  
  <p>edit to the question :-
  below is the source code for signing using csp  </p>
  
  <pre><code>       this.BuildDigestedReferences();
          AsymmetricAlgorithm signingKey = this.SigningKey;  //Certificate Private Key
          if (signingKey == null)
          {
              throw new CryptographicException("Cryptography_Xml_LoadKeyFailed");
          }
          if (this.SignedInfo.SignatureMethod == null)
          {
              if (!(signingKey is DSA))
              {
                  if (!(signingKey is RSA))
                  {
                      throw new CryptographicException("Cryptography_Xml_CreatedKeyFailed");
                  }
                  if (this.SignedInfo.SignatureMethod == null)
                  {
                      this.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
                  }
              }
              else
              {
                  this.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
              }
          }
          SignatureDescription description = CryptoConfig.CreateFromName(this.SignedInfo.SignatureMethod) as SignatureDescription;
          if (description == null)
          {
              throw new CryptographicException("Cryptography_Xml_SignatureDescriptionNotCreated");
          }
          HashAlgorithm hash = new SHA1Managed();
          if (hash == null)
          {
              throw new CryptographicException("Cryptography_Xml_CreateHashAlgorithmFailed");
          }
          //this.GetC14NDigest(hash);
          this.GetC14NDigest(hash, "ds");
  
          this.m_signature.SignatureValue = description.CreateFormatter(signingKey).CreateSignature(hash);
  </code></pre>
  
  <p>and using pkcs11 library </p>
  
  <pre><code>        Pkcs11 pkcs11 = new Pkcs11(_pkcs11LibraryPath, false);
          //Get Info
          LibraryInfo libraryInfo = pkcs11.GetInfo();
  
          // Get list of available slots
          List&lt;Slot&gt; slots = pkcs11.GetSlotList(false);
  
          //Login to business slot 
          Slot paciSlot = slots[2];
          // Open RO session
          using (Session session = paciSlot.OpenSession(false))
          {
              //Sign and verify with certificate
  
              //Get Private Key
              var objectAttributes = new List&lt;ObjectAttribute&gt;
              {
                  new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY),
                  new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_RSA)
              };
  
              session.FindObjectsInit(objectAttributes);
              var oObjCollection = session.FindObjects(1);
  
  
              ObjectHandle oPrivKeyObjectHandle = new ObjectHandle();
              if (oObjCollection.Count &gt; 0)
              {
  
                  oPrivKeyObjectHandle = oObjCollection[0];
  
              }
  
              // Specify signing mechanism
              Mechanism mechanism = new Mechanism(CKM.CKM_SHA1_RSA_PKCS);
  
              byte[] signature = session.Sign(mechanism, oPrivKeyObjectHandle, hashedata);
  </code></pre>
  
  <p>the signature using csp is different than using pkcs11 how ever iam using the same private key .</p>
  
  <p>thank you</p>