Reputation: 23
I have an app that used to allow users to sign XML files via USB token with their certificate within. Some days ago, the company of the tokens updated their driver software (SafeNet Authentication Client) from version 9 to version 10. Now my program stops working in the .ComputeSignature() function.
The tip I got from the token organization was "to use a new DLL, instead of the native mscorlib.dll" (they gave me an specified dll file), but I cant figure out how to do it, or where to start. The code is the following.
public void InsertSignature(...)
{
//Opens the local user certificates store
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates;
XmlDocument documento = new XmlDocument();
documento.PreserveWhitespace = true;
try
{
documento.Load(pathDocument);
SignedXml firmado = new SignedXml(documento);
X509Certificate2 certificado = null;
foreach (X509Certificate2 cert in certs)
{
if (cert.Thumbprint == huella)
{
certificado = new X509Certificate2(cert.GetRawCertData(), "");
certificado.PrivateKey = cert.PrivateKey;
break;
}
}
if (certificado == null)
{
MessageBox.Show("Check the token/certificate", "Certificate not found", MessageBoxButtons.OK);
return;
}
store.Close();
firmado.SigningKey = certificado.PrivateKey;
firmado.SignedInfo.SignatureMethod = SignedXml.XmlDsigRSASHA1Url;
#region looking for the reference (the structure of the file to sign may vary)
#endregion
//Add the reference to the SignedXml object.
firmado.AddReference(referencia);
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue((RSA)certificado.PrivateKey));
keyInfo.AddClause(new KeyInfoX509Data(certificado));
firmado.KeyInfo = keyInfo;
//HERE HAPPENS THE ERROR/EXCEPTION
firmado.ComputeSignature();
XmlElement xmlDigitalSignature = null;
xmlDigitalSignature = firmado.GetXml();
#region looking the place to set the sign
#endregion
//Insert the signature
XmlNode parent = elemento.ParentNode;
parent.InsertAfter(xmlDigitalSignature, elemento);
#region location routes
#endregion
//File is saved
XmlWriter writer = XmlWriter.Create(ruta_completa, _xmlWriterSettings);
documento.Save(writer);
writer.Close();
}
catch (Exception ex)
{
MessageBox.Show("Excepción producida en: " + ex.Source.ToString() + ". " + (ex.ToString()).Substring(0,107) , "Error en Try/Catch");
}
}
Output: Exception: 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll The system cannot find the file specified.
I tried importing the new DLL, but there was a message saying "the feature of local function attributes is not available in C#7.3"
I want to change the target dll, instead of mscorlid, it should target to the new dll "eTpkcs11.dll"
Upvotes: 2
Views: 108