Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > Visual Basic 2010 General Discussion
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 2nd, 2014, 06:03 AM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add a prefix to sign an XML file

I use an X509 certificate to sign an XML file but the prefix related to the signature is missing, I have to add the "ds" one.

How to add this prefix without having to break my signature?

Here is my code:

Code:
Dim f_adresse As String = System.AppDomain.CurrentDomain.BaseDirectory
Dim f_temp As String = f_adresse & "TEMP\"
Dim f_fichier As String = "20381235051-01-FF12-03.xml"
Dim f_certificat As String = f_adresse & "aG9CcVpHVndCWTd3WlVOVw==.p12"
Dim f_pwd As String = "GiQ5uTsBZ9gHxzNH"

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)
signedXml.SigningKey = MonCertificat.PrivateKey
Dim KeyInfo As KeyInfo = New KeyInfo()
Dim Reference As Reference = New Reference()
Reference.Uri = ""

Reference.AddTransform(New XmlDsigEnvelopedSignatureTransform("true"))

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()

signature.Prefix = "ds"
signedXml.ComputeSignature()
signature.ChildNodes(0).Prefix = "ds" 'this line breaks my signature
signedXml.ComputeSignature()

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

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#")

xmlDoc.SelectSingleNode("/tns:Invoice/ext:UBLExtensions/ext:UBLExtension[2]/ext:ExtensionContent", nsMgr).AppendChild(xmlDoc.ImportNode(signature, True))
xmlDoc.Save(xmlFile)

'check the signature
Dim nodeList As XmlNodeList = xmlDoc.GetElementsByTagName("ds:Signature")
If nodeList.Count <= 0 Then
     MsgBox("Verification failed: No Signature was found in the document.")
     Exit Sub
End If

If nodeList.Count >= 2 Then
     MsgBox("Verification failed: More that one signature was found for the document.")
     Exit Sub
End If
signedXml.LoadXml(CType(nodeList(0), XmlElement))
msgbox("Check Signature : " & signedXml.CheckSignature())
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add comment in a java file and xml GFreedom BOOK: Beginning Android Application Development 1 October 7th, 2011 09:12 AM
How do I add to an XML file using VB? QuadFather XML 4 April 6th, 2010 03:32 PM
Adding Prefix and Namespace to XML Lerxt XSLT 6 November 8th, 2007 03:14 PM
Create XML file - According to the Value, add elem remya1000 General .NET 0 October 3rd, 2007 12:03 AM
adding prefix to nodes and subnodes to output xml raghurns XSLT 9 November 17th, 2006 04:41 PM



All times are GMT -4. The time now is 04:42 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.