CMSSigner.ComputeSignature throws WindowsCryptographicException

Vicente Pulso Informática 1 Reputation point
2020-03-25T17:20:32.423+00:00

When trying to make a signature with CmsSigner class invoking the ComputeSignature method, it throws the following exception:
{"Invalid type specified."} System.Exception {Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException}

The certificate is obtained from the certificate store and stored in an X509Certificate2 object. The private key exists. The problem is that when ComputeSignature is performed, the provider's Dialog PIN does not jump (in this case Siemens CARD API CSP) and throws the exception. This problem happens to us in .NET Core and with certificates whose private key is located on a Smart Card and is not exportable. With the .NET Framework it works correctly and if we use a certificate whose private key is in the store, it also works correctly in both .NET Core and .NET FrameWork.
In .NET Core it works indicating SubjectIdentifierType.NoSignature, but in this way the signer is not identified and this solution is not worth it.

public static byte [] sign (X509Certificate2 cert, byte [] data, bool compress, int SignOption)
{
try
{
ContentInfo info;
if (compress)
info = new ContentInfo (ZipXML (data));
else
info = new ContentInfo (data);
SignedCms signedMes = new SignedCms (info, true);
CmsSigner signature = new CmsSigner (cert);
signedMes.ComputeSignature (signature, false); -> Throws Exception !!!!
return signedMes.Encode ();
} catch (Exception e)
{
throw new Exception ("Signature error." + e.Message + "\ n" + e.Source + ". \ n" + e.StackTrace);
}
}

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,975 questions
{count} votes