Pkcs11 - How to add signature to xml file?

别说谁变了你拦得住时间么 提交于 2019-12-13 03:26:17

问题


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?


回答1:


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 a SigningKey.



来源:https://stackoverflow.com/questions/49686710/pkcs11-how-to-add-signature-to-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!