I have been trying to create an app that digitally signs an XML document. It has to be signed with certificate because i have to send said docuemnt to tax administration servers. I used certificates as assets, but now i have a problem with signing the document. Xamarin doesn't support the System.Security.Cryptography.Xml and System.Deployment library (at least as far as i know). Even if i include them in the references as a separate file (coppied to the bin folder with apk file), i don't seem to get it to work. i get errors like "can't implicitly convert System.Xml.XmlElement to System.Xml.XmlElement".
The code for signing the file is as follows:
CryptoConfig.AddAlgorithm (typeof(RSAPKCS1SHA256SignatureDescription), "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"); System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument (); xmlDoc.Load (filename); // Create a SignedXml object. System.Security.Cryptography.Xml.SignedXml signedXml = new System.Security.Cryptography.Xml.SignedXml (xmlDoc); byte[] data = cert.GetPublicKey (); string base64 = Convert.ToBase64String (data); RSACryptoServiceProvider rsaCSP = (RSACryptoServiceProvider)cert.PrivateKey; CspParameters cspParameters = new CspParameters (); cspParameters.KeyContainerName = rsaCSP.CspKeyContainerInfo.KeyContainerName; cspParameters.KeyNumber = rsaCSP.CspKeyContainerInfo.KeyNumber == KeyNumber.Exchange ? 1 : 2; RSACryptoServiceProvider rsaAesCSP = new RSACryptoServiceProvider (cspParameters); signedXml.SigningKey = rsaAesCSP; //newKey; System.Security.Cryptography.Xml.KeyInfo keyInfo = new System.Security.Cryptography.Xml.KeyInfo (); System.Security.Cryptography.Xml.KeyInfoX509Data keyInfoData = new System.Security.Cryptography.Xml.KeyInfoX509Data (); keyInfoData.AddIssuerSerial (cert.Issuer, cert.SerialNumber); X509Extension extension = cert.Extensions [1]; AsnEncodedData asndata = new AsnEncodedData (extension.Oid, extension.RawData); keyInfoData.AddSubjectName (cert.SubjectName.Name); // Create a reference to be signed. System.Security.Cryptography.Xml.Reference reference = new System.Security.Cryptography.Xml.Reference (); reference.Uri = "#test"; reference.DigestMethod = @"http://www.w3.org/2001/04/xmlenc#sha256"; // Add an enveloped transformation to the reference. System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform env = new System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform (); reference.AddTransform (env); // Add the reference to the SignedXml object. signedXml.AddReference (reference); keyInfo.AddClause (keyInfoData); signedXml.KeyInfo = keyInfo; signedXml.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"; // Compute the signature. signedXml.ComputeSignature (); // Get the XML representation of the signature and save // it to an XmlElement object. System.Xml.XmlElement xmlDigitalSignature = (System.Xml.XmlElement)signedXml.GetXml (); // Append the element to the XML document. XmlNode element; element = xmlDoc.GetElementsByTagName ("fu:InvoiceRequest") [0]; element.AppendChild (xmlDigitalSignature);
Is there any other way you can digitally sign XML document with certificates?
No comments:
Post a Comment