Re: Help : XML Signature with private key in smart card

From: J-P Meunier (fuimens_at_yahoo.fr)
Date: 05/07/03

  • Next message: Doug Bratton: "Re: FileIO Security"
    Date: Wed, 7 May 2003 19:45:19 +0200
    
    

    Ivan,

    Thank you for your post.

    I have 2 remarks:
    1) the instruction "RSACryptoServiceProvider rsa = new
    RSACryptoServiceProvider(csp);", used with the same flags and class
    properties sometimes generates a key in the card and sometimes seems to use
    the key already located in the card.
    Is this the normal behaviour ? Have I missed a flag or property !?
    Any explanation about this will be very helpful.

    2) rsa.SignData() doesn't support XML Digital Signature while my need is to
    perform XML D-Signature using the private key in a smart card.

    Regards,

    J-P

    "Ivan Medvedev [MS]" <ivanmed@online.microsoft.com> a écrit dans le message
    de news: eLfuiLLFDHA.2396@TK2MSFTNGP12.phx.gbl...
    > J-P -
    > I threw a little sample together that shows how to create an RSA object
    tied
    > to a smart card and sign with it. In your code you can drop the X509 part
    > and take the code from the sample that creates an RSA object wich you
    would
    > set to SignedXml.SigningKey.
    > You may need to change the provider name in the CspParameters constructor
    to
    > match the particular smartcard you are using.
    > Hope this helps.
    > --Ivan
    >
    > -------------------------------------------------------
    > cut ------------------------------------------------------
    > using System;
    > using System.Security.Cryptography;
    >
    > class SCSign
    > {
    > [STAThread]
    > static void Main(string[] args)
    > {
    > // the 1st parameter comes from
    > HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types
    > // the 2nd parameter comes from
    > HKLM\Software\Microsoft\Cryptography\Defaults\Provider
    > CspParameters csp = new CspParameters(1, "Schlumberger Cryptographic
    > Service Provider");
    > csp.Flags = CspProviderFlags.UseDefaultKeyContainer;
    >
    > RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
    >
    > byte[] data = new byte[] {0,1,2,3,4,5,6,7};
    >
    > Console.WriteLine("Data : " + BitConverter.ToString(data));
    >
    > byte[] sig = rsa.SignData(data, "SHA1");
    >
    > Console.WriteLine("Signature : " + BitConverter.ToString(sig));
    >
    > bool verified = rsa.VerifyData(data, "SHA1", sig);
    >
    > Console.WriteLine("Verified : " + verified);
    > }
    > }
    >
    > -------------------------------------------------------
    > cut ------------------------------------------------------
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    rights.
    > Use of included script samples are subject to the terms specified at
    > http://www.microsoft.com/info/cpyright.htm
    >
    >
    >
    > "J-P Meunier" <fuimens@yahoo.fr> wrote in message
    > news:#W4aw#9EDHA.1552@TK2MSFTNGP12.phx.gbl...
    > > hi all,
    > >
    > > I want to sign a local file with XML Signature using a private key
    located
    > > in a smart card.
    > >
    > > I am able to sign with private keys located in local registry with the
    > > following testing code:
    > >
    > >
    > >
    > > using FCLX509 = System.Security.Cryptography.X509Certificates;
    > > using WSEX509 = Microsoft.Web.Services.Security.X509;
    > > string subjectname = "CN=Admin";
    > > string storename ="MY";
    > > WSEX509.X509CertificateStore.StoreLocation location =
    > > WSEX509.X509CertificateStore.StoreLocation.CurrentUser;
    > > WSEX509.X509CertificateStore.StoreProvider provider =
    > > WSEX509.X509CertificateStore.StoreProvider.System;
    > > WSEX509.X509CertificateStore store = new
    > > WSEX509.X509CertificateStore(provider, location, storename);
    > > bool fopen = store.OpenRead();
    > > WSEX509.X509CertificateCollection cers =
    > > store.FindCertificateBySubjectName(subjectname);
    > > foreach ( FCLX509.X509Certificate FCLcer in cers)
    > > {
    > > WSEX509.X509Certificate cer = new
    > > WSEX509.X509Certificate(FCLcer.GetRawCertData());
    > > AsymmetricAlgorithm private_key = cer.Key;
    > >
    > > // getting private key
    > > RSA rsaKeyPair = cer.Key;
    > >
    > > SignedXml signedXml = new SignedXml();
    > > signedXml.SigningKey = rsaKeyPair;
    > >
    > > XmlDocument objdocument = new XmlDocument();
    > > objdocument.Load("CreditCardInfo.xml");
    > >
    > > DataObject dataObject = new DataObject();
    > > dataObject.Data = objdocument.ChildNodes;
    > > dataObject.Id = "CreditCardInfo";
    > > signedXml.AddObject(dataObject);
    > >
    > > Reference reference = new Reference();
    > > reference.Uri = "#CreditCardInfo";
    > > signedXml.AddReference(reference);
    > >
    > > signedXml.ComputeSignature();
    > > }
    > >
    > >
    > > But when the private key associated to the certificate "cer", the
    > > instruction "RSA rsaKeyPair = cer.Key" fails, which is expected because
    > > smart cards generally don't allow private key exporting.
    > >
    > >
    > > So I tried the following code to "retrieve" the private key in the smart
    > > card:
    > >
    > > CspParameters cp = new CspParameters();
    > > cp.ProviderName = "Gemplus GemSAFE Card CSP";
    > > cp.Flags = CspProviderFlags.UseMachineKeyStore;
    > > RSACryptoServiceProvider rsaKeyPair = new
    > RSACryptoServiceProvider(cp);
    > >
    > > But this generates private key on the smart card !?
    > >
    > > So how can I use the smart card CSP (as Gemplus GemSAFE Card CSP) to
    sign
    > > with the private key in the smart card ?
    > >
    > > Thanks
    > >
    > >
    >
    >


  • Next message: Doug Bratton: "Re: FileIO Security"

    Relevant Pages

    • Re: Certificates
      ... RSA encryption. ... I wrote that the RSA digital signature _algorithm_ superficially ... looks like a RSA encryption _algorithm_. ... reason why you talk about "encrypting with the private key" and I point ...
      (sci.crypt)
    • Re: Problem with RSA.ImportParameters() under ASP .NET
      ... access for writing that temporary file is denied. ... file (in Machine CU store) since you don't specify a persistence. ... You could look for a transient (private key file) created .. ... RSA rsa = RSA.Create; ...
      (microsoft.public.dotnet.security)
    • Re: RSA .crt and .key file formats?
      ... > and private key file (actually, the private key file, from what I can ... Basically, for RSA signatures, and for RSA decryption, you need to ... needs only the modulus n and the private exponent d. ... setting is to choose a random binary value K, encrypt it ...
      (sci.crypt)
    • Re: RSA breaking vs. factoring
      ... private key but other activities like decrypting texts without ... Breaking RSA _encryption_ means being able to decrypt a message without ... The public operation is then elevating to the e power modulo n. ... You may compare this with the extraction of square roots. ...
      (sci.crypt)
    • Re: Problem with RSA.ImportParameters() under ASP .NET
      ... I'm pretty sure what you are doing is creating a transient RSA private key ... file since you don't specify a persistence. ... You could look for a transient (private key file) created .. ... I'm not trying to access a private key or certificate in a specific store. ...
      (microsoft.public.dotnet.security)