I've a problem that occurs when getting certificate and putting to xml file. Should I use private key for signing? I see only public key in certificate. There is no private key in my certificate.
byte[] ckaIdd = objectAttributess[0].GetValueAsByteArray();
string ckaLabel = objectAttributess[1].GetValueAsString();
byte[] ckaValue = objectAttributess[2].GetValueAsByteArray();
var _rawData = ckaValue ?? throw new ArgumentNullException(nameof(ckaValue));
var _parsedCertificate = new X509Certificate2(_rawData);
ECertificate cert = new ECertificate(_parsedCertificate.GetRawCertData());
string signatureListString = "";
XmlDocument document = new XmlDocument();
document.Load(@"C:\Users\MyUser\Desktop\myfile.xml");
Esya e = new Esya();
Context context = e.CreateContext();
context.Document = document;
XMLSignature signature = new XMLSignature(context, false);
signature.addKeyInfo(new ECertificate(cert.getEncoded()));
//signature.sign(v); << ! My problem is with this line
var inv = (XmlElement)signature.Document.GetElementsByTagName("Invoice")[0];
signatureListString += inv.OuterXml + "\n";
var elementCount = (XmlElement)document.GetElementsByTagName("ElementCount")[0];
if (elementCount != null)
{
elementCount.InnerText = "1";
}
var element = (XmlElement)document.GetElementsByTagName("ElementList")[0];
if (element != null)
{
element.InnerXml = signatureListString;
}
var xmlPageSettings = document.GetElementsByTagName("Invoice");
foreach (XmlElement xmlElement in xmlPageSettings)
{
xmlElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
}
session.Logout();
return cert;
}
}
What should I do with signature.sign(v);
? How can I add signature to xml file?
You have to implement class inherited from
System.Security.Cryptography.RSA
class, use Pkcs11Interop in its implementation and then use instance of your custom class as aSigningKey
.