CAPICOM problem in .NET 2.0

CAPICOM problem in .NET 2.0

Secure Home | Search | About
 Microsoft Applications Security    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content add this group's latest topics to your Google content
Subject Author Date
CAPICOM problem in .NET 2.0 efrolov 02-28-2007
Posted by on February 28, 2007, 10:02 am
If you were  Registered and logged in, you could reply and use other advanced thread options
RSACryptoProvider creation problem:
I used some code for RSACryptoProvider creation with smart card and
CAPICOM COM object
The code worked well in Framework 1.1
but when I tried the same code in Framework 2.0
I got an following Cryptographic exception:
"Unable to open the access token of the current thread"


string url = string.Empty;
rsa = null;
certificate = null;
RSACryptoServiceProvider.UseMachineKeyStore = true;

CspParameters csp = new CspParameters();
csp.Flags = CspProviderFlags.UseMachineKeyStore;
ICertificate2 selectedCert = null;


//open the ceritificate store
Store st = new Store();

st.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE,
"My",
CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED);
//search for the certificate with the digital signature
enabled
foreach (ICertificate2 currCert in st.Certificates)
{

if (currCert.HasPrivateKey())
{
selectedCert = currCert;
break;
}
}
//check if the certificate was found or not
if (selectedCert == null || selectedCert.HasPrivateKey()
== false ||

selectedCert.PublicKey().Algorithm.FriendlyName != "RSA")
{
StringBuilder sb = new StringBuilder();
sb.Append("Can't find a vaild certificate!\n\n");
sb.Append("Valid Certificate Authorities are :\n");
System.Collections.IEnumerator myEnum =
issuerlist.GetEnumerator();
while (myEnum.MoveNext())
sb.Append(myEnum.Current).Append("\n");
throw new Exception(sb.ToString());
}

certificate = selectedCert;

//create cerificate provider
csp.KeyContainerName =
selectedCert.PrivateKey.ContainerName;
csp.ProviderName = selectedCert.PrivateKey.ProviderName;
csp.ProviderType =
Convert.ToInt32(selectedCert.PrivateKey.ProviderType);

//the certificate key number by the key specifications
switch (selectedCert.PrivateKey.KeySpec)
{
case CAPICOM_KEY_SPEC.CAPICOM_KEY_SPEC_KEYEXCHANGE:
csp.KeyNumber = 1;
break;

case CAPICOM_KEY_SPEC.CAPICOM_KEY_SPEC_SIGNATURE:
csp.KeyNumber = 2;
break;
}

//for machine keys use the local store of the machine
if (selectedCert.PrivateKey.IsMachineKeyset())
{
csp.Flags = CspProviderFlags.UseMachineKeyStore;
}
//create rsa cryptogragic provider based on the CSP
try
{
rsa = new RSACryptoServiceProvider(csp);
}
catch
{ }


Similar ThreadsPosted
CAPICOM and CSR or PKCS#10 March 5, 2008, 8:52 am
Capicom: sign error June 23, 2005, 3:04 pm
Capicom Security Alert November 16, 2006, 10:35 am
Need help with CAPICOM Security APIs August 8, 2007, 9:59 am
Re: CAPICOM.EnvelopedData decrypt: where to put certificate? June 22, 2005, 3:24 pm
CAPICOM: Is it possible to sign multiple files... August 22, 2005, 12:48 pm
Encryption on Windows 2000 with capicom December 1, 2005, 7:17 pm
SUS Problem July 4, 2005, 6:29 am
A Problem July 10, 2005, 11:05 pm
Re: I'm having the same problem... can someone help me too? May 26, 2005, 1:45 am

The site map in XML format XML site map

Contact Us | Privacy Policy