Reputation: 37632
I am trying to sign XML with a custom generated certificate which is acessible via this code
private void buttonSelectCertificate_Click(object sender, EventArgs e)
{
CertStoreLocation = (StoreLocation)cboStoreLocation.SelectedItem;
CertStoreName = (StoreName)cboStoreName.SelectedItem;
X509Store store = new X509Store(CertStoreName, CertStoreLocation);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(store.Certificates, "Certificate Select", "Select a certificate from the following list to get information on that certificate", System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);
}
What I cannot get how to use properly X509Certificate2Collection scollection
in order to fill out following properties.
Also I am not getting following idea:
If certificate is stored here My = 5
then have I use
CspProviderFlags.UseMachineKeyStore
?
How can I get the KeyContainerName
from X509Certificate2Collection scollection
?
And finally maybe I am completely wrong in the way to get certificate attributes I need in order to populate CspParameters
class, any clue?
Thank you for help!
// Get the key pair from the key store.
CspParameters parms = new CspParameters(1); // PROV_RSA_FULL
parms.Flags = ??? CspProviderFlags.UseMachineKeyStore; // Use Machine store
parms.KeyContainerName = ???; //
parms.KeyNumber = 2; // AT_SIGNATURE
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(parms);
Upvotes: 1
Views: 3596
Reputation: 37632
Here the answer
private void buttonSelectCertificate_Click(object sender, EventArgs e)
{
CertStoreLocation = (StoreLocation)cboStoreLocation.SelectedItem;
CertStoreName = (StoreName)cboStoreName.SelectedItem;
X509Store store = new X509Store(CertStoreName, CertStoreLocation);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(store.Certificates, "Certificate Select", "Select a certificate from the following list to get information on that certificate", System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);
foreach (X509Certificate2 cert in scollection)
{
var rsa = cert.PrivateKey as RSACryptoServiceProvider;
if (rsa == null) continue; // not smart card cert again
if (!string.IsNullOrEmpty(rsa.CspKeyContainerInfo.KeyContainerName))
{
// This is how we can get it! :)
var keyContainerName = rsa.CspKeyContainerInfo.KeyContainerName;
}
}
}
And also we have use normally CspProviderFlags.UseMachineKeyStore
CspParameters parms = new CspParameters(1); // PROV_RSA_FULL
parms.Flags = CspProviderFlags.UseMachineKeyStore; // Use Machine store
Upvotes: 1