Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@AleksanderGladkov
Copy link
Contributor

@AleksanderGladkov AleksanderGladkov commented May 12, 2025

I need to sign some data with the private key of the certificate that is stored in Azure Key Vault.

  • The private key is obtained in base64 format by running AzureKeyVault.GetAzureKeyVaultCertificate('Key', Certificate).
  • Then dotnet object X509Certificate2 is initialized from that base64 string by running SignatureKey.FromBase64String(Certificate.Unwrap(), EmptySecretText, true).
  • And finally the function CryptographyManagement.SignData() is used, but it accepts private key only in XML format.

To convert X509Certificate2 to XML format, SignatureKey.ToXmlString() function can be used, but it is now internal, so I want to make it external to use for that purpose.

Full code:

    [NonDebuggable]
    procedure SignDataWithAKVCertificate(DataToSign: Text)
    var
        AzureKeyVault: Codeunit "Azure Key Vault";
        CryptographyManagement: Codeunit "Cryptography Management";
        SignatureKey: Codeunit "Signature Key";
        Certificate: SecretText;
        EmptySecretText: SecretText;
        SignatureOutStream: OutStream;
    begin
        AzureKeyVault.GetAzureKeyVaultCertificate('Key', Certificate);
        SignatureKey.FromBase64String(Certificate.Unwrap(), EmptySecretText, true);
        CryptographyManagement.SignData(DataToSign, SignatureKey.ToXmlString(), Enum::"Hash Algorithm"::SHA256, SignatureOutStream);
        // Do something with SignatureOutStream
    end;

Fixes AB#578074

@github-actions
Copy link

Issue #578074 is not valid. Please make sure you link an issue that exists, is open and is approved.

@github-actions github-actions bot modified the milestone: Version 27.0 May 12, 2025
@AleksanderGladkov AleksanderGladkov merged commit 39a600a into main May 19, 2025
28 of 29 checks passed
@AleksanderGladkov AleksanderGladkov deleted the bugs/578074-main-Signature-Key-ToXmlString-External branch May 19, 2025 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants