SignerSign error - No provider was specified for the store or object

1

I am developing a c# .net 3.5 application (compiled to anycpu)

I need to sign an executable. I tried to use the code in here with some modifications. I didn't search for the certificate in the store, instead I loaded it form a pfx file:

  var cert = new X509Certificate2("myCert.pfx", "pass");

And change method sign like this:

  public static void Sign(string appPath, X509Certificate2 cert)
    {
        var pSignerCert = IntPtr.Zero;
        var pSubjectInfo = IntPtr.Zero;
        var pSignatureInfo = IntPtr.Zero;
        var pProviderInfo = IntPtr.Zero;


        pSignerCert = CreateSignerCert(cert);
        pSubjectInfo = CreateSignerSubjectInfo(appPath);
        pSignatureInfo = CreateSignerSignatureInfo();
        pProviderInfo = GetProviderInfo(cert);

           var hResult = NativeMethods.SignerSign(
                pSubjectInfo,
                pSignerCert,
                pSignatureInfo,
                pProviderInfo,
                null,
                IntPtr.Zero,
                IntPtr.Zero
                );

        Console.WriteLine(hResult);
    }

and then I am getting the provider info like this:

  private static IntPtr GetProviderInfo(X509Certificate2 cert)
    {
        if (cert == null || !cert.HasPrivateKey)
        {
            return IntPtr.Zero;
        }

        var key = cert.PublicKey.Key as ICspAsymmetricAlgorithm;
        if (key == null)
        {
            return IntPtr.Zero;
        }

        var providerInfo = new SignerProviderInfo
                               {
                                   cbSize = (uint)Marshal.SizeOf(typeof(SignerProviderInfo)),
                                   pwszProviderName = "Microsoft Enhanced Cryptographic Provider v1.0",
                                   dwProviderType = 0x1,
                                   // PROV_RSA_FULL
                                   dwKeySpec = 0x0,
                                   dwPvkChoice = 0x2, //PVK_TYPE_KEYCONTAINER
                                   providerUnion = new SignerProviderInfo.ProviderInfoUnion
                                                       {
                                                           pwszKeyContainer = key.CspKeyContainerInfo.KeyContainerName
                                                       },
                               };
        var pProviderInfo = Marshal.AllocHGlobal(Marshal.SizeOf(providerInfo));
        Marshal.StructureToPtr(providerInfo, pProviderInfo, false);

        return pProviderInfo;
    }

the issue is that on some machines it works perfectly ok, but on some other machine I get the following error: Error code 0x80092006 - No provider was specified for the store or object.

I tried to use SignTool.exe with the same pfx file and it works ok.

I tried to investigate what might cause this behavior, but I couldn't find any hint for wht this error is happening.

Does anyone knows why it might happen?

c#
.net
sign
signtool
asked on Stack Overflow Jan 9, 2013 by user844541 • edited May 23, 2017 by Community

1 Answer

2

provider info should be filled like this instead:

// get private key information
ICspAsymmetricAlgorithm key = (ICspAsymmetricAlgorithm)certificate.PrivateKey;
const int PVK_TYPE_KEYCONTAINER = 2;

var providerInfo = new SignerProviderInfo
{
   cbSize = (uint)Marshal.SizeOf(typeof(SignerProviderInfo)),
   pwszProviderName = key.CspKeyContainerInfo.ProviderName,
   dwProviderType = (uint)key.CspKeyContainerInfo.ProviderType,
   dwPvkChoice = PVK_TYPE_KEYCONTAINER,
   providerUnion = new SignerProviderInfo.ProviderInfoUnion
   {
       pwszKeyContainer = key.CspKeyContainerInfo.KeyContainerName
   },
};
answered on Stack Overflow Sep 17, 2013 by Simon Mourier

User contributions licensed under CC BY-SA 3.0