Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV forum
Answered

Signature for XML file

Posted on by 3,941

Hello Team. I really stuck in a rut with next problem:

  1. I generate XML file from NAV 2018.(web request)
  2. I have *.crt and *cer files (signed certificate)
  3. I need to generate signature for XML file using *.cer or *.crt file and I have no idea how to do it using NAV.

I even agree to use C/AL and dotnet variables in NAV2018 instead of AL and extensions if there is any way to generate signature (because i suppose that it's impossible to do without dotnet).

Any help will be appreciate.

  • JJMc Profile Picture
    JJMc 280 on at
    RE: Signature for XML file

    A question.

    And how or where are you setting the certificate and password to sign the document?

    Thank you ever so much

  • Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    CSPParams DotNet System.Security.Cryptography.CspParameters.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    RSA DotNet System.Security.Cryptography.RSACryptoServiceProvider.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    XmlDocument DotNet System.Xml.XmlDocument.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    XmlTextReader DotNet System.Xml.XmlTextReader.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    SignedXml DotNet System.Security.Cryptography.Xml.SignedXml.'System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

    Reference DotNet System.Security.Cryptography.Xml.Reference.'System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

    Env DotNet System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform.'System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

    XmlDigSignature DotNet System.Xml.XmlElement.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

  • GreatScott001 Profile Picture
    GreatScott001 115 on at
    RE: Signature for XML file

    Can you list variable types?

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,152 on at
    RE: Signature for XML file

    Hi Andrey, I've checked and my addin performs criptography with signed certificate (with RSACryptoServiceProvider).

    I think this is not exactly what you was searching. Nice to see that you've solved your problem.

  • Verified answer
    Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    And the answer is:

    CSPParams := CSPParams.CspParameters(1, 'Microsoft Base Cryptographic Provider v1.0', 'DevRootCA');
    
    RSA := RSA.RSACryptoServiceProvider(CSPParams);
    
    XmlDocument := XmlDocument.XmlDocument;
    XmlDocument.PreserveWhitespace(FALSE);
    XmlDocument.Load(XmlTextReader.XmlTextReader('C:\Temp\Data.xml'));
    
    SignedXml := SignedXml.SignedXml(XmlDocument);
    SignedXml.SigningKey := RSA;
    
    Reference := Reference.Reference;
    Reference.Uri := '';
    
    Env := Env.XmlDsigEnvelopedSignatureTransform;
    Reference.AddTransform(Env);
    
    SignedXml.AddReference(Reference);
    
    SignedXml.ComputeSignature;
    XmlDigSignature := SignedXml.GetXml;
    MESSAGE(XmlDigSignature.InnerXml);


  • Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    So now i have next code:

    CSPParams := CSPParams.CspParameters;
    
    // Here is main issue ->
    //CSPParams.KeyContainerName = 'DevRootCA';
    //<-
    
    RSA := RSA.RSACryptoServiceProvider(CSPParams);
    
    XmlDocument := XmlDocument.XmlDocument;
    XmlDocument.PreserveWhitespace(FALSE);
    XmlDocument.Load(XmlTextReader.XmlTextReader('C:\Temp\Data.xml'));
    
    SignedXml := SignedXml.SignedXml(XmlDocument);
    SignedXml.SigningKey(RSA);
    
    Reference := Reference.Reference;
    Reference.Uri := '';
    
    Env := Env.XmlDsigEnvelopedSignatureTransform;
    Reference.AddTransform(Env);
    
    SignedXml.AddReference(Reference);
    
    SignedXml.ComputeSignature;
    XmlDigSignature := SignedXml.GetXml;
    MESSAGE(XmlDigSignature.InnerXml);


    I Have dotnet variable CSPParams which I took from mscorlib:

    System.Security.Cryptography.CspParameters.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    And the problem is that it doesn't have .KeyContainerName field. So I can't set it and get the key. As I understood - I just need to set SSL certificate name there? Has anyone encountered a similar problem?

    I'm moving through next instruction:

    docs.microsoft.com/.../how-to-sign-xml-documents-with-digital-signatures

  • Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    Stefano Demiliani have you had a chance to look for code? I'll be very appreciate.

  • Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    It'll be great!

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,152 on at
    RE: Signature for XML file

    Yes if you want I can share it. I've only to return at my office for doing that :)

  • Verified answer
    Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    I found System.Security.Cryptography.Xml

    Thanks for inspiring me!

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans