HighTechTalks DotNet Forums  

CAPICOM problem with .NET 2.0

Dotnet Security microsoft.public.dotnet.security


Discuss CAPICOM problem with .NET 2.0 in the Dotnet Security forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
efrolov@gmail.com
 
Posts: n/a

Default CAPICOM problem with .NET 2.0 - 02-28-2007 , 10:04 AM






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_USE R_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.ProviderTy pe);

//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);//Here i get
the Exception
}
catch
{ }


Reply With Quote
  #2  
Old   
Joe Kaplan
 
Posts: n/a

Default Re: CAPICOM problem with .NET 2.0 - 02-28-2007 , 01:50 PM






I'm not sure what the problem with CAPICOM is, but did you consider
converting your code to the new .NET 2.0 X509 stuff? Everything you are
doing below is now supported directly by the framework and might be easier
to integrate.

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
<efrolov (AT) gmail (DOT) com> wrote

Quote:
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_USE R_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.ProviderTy pe);

//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);//Here i get
the Exception
}
catch
{ }




Reply With Quote
  #3  
Old   
Dominick Baier
 
Posts: n/a

Default Re: CAPICOM problem with .NET 2.0 - 03-01-2007 , 05:39 AM



Have you used X509Certificate2 ?

If you can't access the PrivateKey property - then you most probably don't
have one - can you open the cert in explorer - does it say "you have a corresponding
private key" ?


-----
Dominick Baier (http://www.leastprivilege.com)

Developing More Secure Microsoft ASP.NET 2.0 Applications (http://www.microsoft.com/mspress/books/9989.asp)

Quote:
On Feb 28, 8:50 pm, "Joe Kaplan"
joseph.e.kap... (AT) removethis (DOT) accenture.com> wrote:
I'm not sure what the problem withCAPICOMis, but did you consider
converting your code to the new .NET 2.0 X509 stuff? Everything you
are doing below is now supported directly by the framework and might
be easier to integrate.

Joe K.

--

Joe Kaplan-MS MVP Directory Services Programming

Co-author of "The .NET Developer's Guide to Directory Services
Programming"http://www.directoryprogramming.net

--<efro... (AT) gmail (DOT) com> wrote in message

news:1172675045.093914.251350 (AT) j27g2000cwj (DOT) googlegroups.com...

I tried this namespace but there I had similar problem I could not get
PrivateKey property of the certificate




Reply With Quote
  #4  
Old   
efrolov@gmail.com
 
Posts: n/a

Default Re: CAPICOM problem with .NET 2.0 - 03-01-2007 , 06:15 AM



On Feb 28, 8:50 pm, "Joe Kaplan"
<joseph.e.kap... (AT) removethis (DOT) accenture.com> wrote:
Quote:
I'm not sure what the problem withCAPICOMis, but did you consider
converting your code to the new .NET 2.0 X509 stuff? Everything you are
doing below is now supported directly by the framework and might be easier
to integrate.

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"http://www.directoryprogramming.net
--<efro... (AT) gmail (DOT) com> wrote in message

news:1172675045.093914.251350 (AT) j27g2000cwj (DOT) googlegroups.com...


I tried this namespace but there I had similar problem I could not get
PrivateKey property of the certificate



Reply With Quote
  #5  
Old   
efrolov@gmail.com
 
Posts: n/a

Default Re: CAPICOM problem with .NET 2.0 - 03-01-2007 , 10:05 AM



Yes I used an X509Certificate2 and he has property called
HasPrivateKey and its value is true, and yes I checked that
certificate in explorer and I saw that there is private key. I have to
repeat that code works great in .NET 1.1


Reply With Quote
  #6  
Old   
Joe Kaplan
 
Posts: n/a

Default Re: CAPICOM problem with .NET 2.0 - 03-01-2007 , 03:18 PM



Is it possible there is a permissions problem? Perhaps the account in use
doesn't actually have proper file system ACL rights to the private key in
one context but does in the other?

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
<efrolov (AT) gmail (DOT) com> wrote

Quote:
Yes I used an X509Certificate2 and he has property called
HasPrivateKey and its value is true, and yes I checked that
certificate in explorer and I saw that there is private key. I have to
repeat that code works great in .NET 1.1




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.