使用证书签署xml文档

时间:2014-06-13 20:53:46

标签: xml vb.net x509certificate sign

我需要使用x509 v3证书对此xml文件(https://www.dropbox.com/s/qfkfvkc83kt72mb/20381235051-01-FF11-04.xml)进行签名,但签名必须附加在此节点上:

Invoice / ext:UBLExtensions / ext:UBLExtension / ext:ExtensionContent(第19-20行)

我找到了一个签署xml文件的代码,但是它贴在文档的末尾,我该如何处理?

这是我的代码:

Dim f_adresse As String = System.AppDomain.CurrentDomain.BaseDirectory
Dim f_temp As String = f_adresse & "TEMP"
Dim f_fichier As String = "20381235051-01-FF11-04.xml"
Dim f_certificat As String = f_adresse & "aG9CcVpHVndCWTd3WlVOVw==.p12"
Dim f_pwd As String = "xxxxxxxxxxxxxxxx"

Dim xmlFile As String = f_temp & f_fichier

Dim MonCertificat As X509Certificate2 = New X509Certificate2(f_certificat, f_pwd)

Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.PreserveWhitespace = True
xmlDoc.Load(xmlFile)

Dim signedXml As SignedXml = New SignedXml(xmlDoc)

Dim KeyInfo As KeyInfo = New KeyInfo()
Dim Reference As Reference = New Reference()
Reference.Uri = ""

Reference.AddTransform(New XmlDsigEnvelopedSignatureTransform())

signedXml.AddReference(Reference)

Dim X509Chain As X509Chain = New X509Chain()
X509Chain.Build(MonCertificat)

Dim local_element As X509ChainElement = X509Chain.ChainElements(0)
Dim x509Data As KeyInfoX509Data = New KeyInfoX509Data(local_element.Certificate)
Dim subjectName As String = local_element.Certificate.Subject

x509Data.AddSubjectName(subjectName)
KeyInfo.AddClause(x509Data)

signedXml.KeyInfo = KeyInfo
signedXml.ComputeSignature()

Dim signature As XmlElement = signedXml.GetXml()

For Each node As XmlNode In signature.SelectNodes("descendant-or-self::*[namespace-uri()='http://www.w3.org/2000/09/xmldsig#']")
    node.Prefix = "ds"
    If node.LocalName = "Signature" Then
        Dim newAttribute As XmlAttribute = xmlDoc.CreateAttribute("Id")
        newAttribute.Value = "SignatureSP"
        node.Attributes.Append(newAttribute)
     End If
Next node


xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(signature, True))
xmlDoc.Save(xmlFile)

由于

编辑: 在我的代码结束但不起作用:

For Each node As XmlNode In signature.SelectNodes("descendant-or-self::*[namespace-uri()='http://www.w3.org/2000/09/xmldsig#']")
    node.Prefix = "ds"
    If node.LocalName = "Signature" Then
        Dim newAttribute As XmlAttribute = xmlDoc.CreateAttribute("Id")
        newAttribute.Value = "SignatureSP"
        node.Attributes.Append(newAttribute)
     End If
Next node

Dim signNode As XmlNode = xmlDoc.SelectSingleNode("/tns:Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent")
If Not signNode Is Nothing Then
    signNode.AppendChild(xmlDoc.ImportNode(signature, True))
End If

xmlDoc.Save(xmlFile)

1 个答案:

答案 0 :(得分:0)

获取该节点然后追加。如果必须加载命名空间:

Dim nsMgr As XmlNamespaceManager
nsMgr = New XmlNamespaceManager(xmlDoc.NameTable)
nsMgr.AddNamespace("sac", "urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1")
nsMgr.AddNamespace("ccts", "urn:un:unece:uncefact:documentation:2")
nsMgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
nsMgr.AddNamespace("tns", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2")
nsMgr.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
nsMgr.AddNamespace("udt", "urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2")
nsMgr.AddNamespace("ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2")
nsMgr.AddNamespace("qdt", "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2")
nsMgr.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
nsMgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#")

Dim signNodes = mlDoc.SelectNodes
    ("/tns:Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent", nsMgr)

If signNodes.Count > 0 Then
 signNodes(0).AppendChild(xmlDoc.ImportNode(signature, True))
End If