Skip to content

[Bug]: Can not generate a .p12 Certificate in AL #2516

@StefanMaron

Description

@StefanMaron

Describe the issue

Some APIs require Client Certificate Authentication which means that I need to provide a certificate that contains the private key and is protected with a password.

The HTTPClient.AddCertificate() does support that already today.
https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/httpclient/httpclient-addcertificate-secrettext-secrettext-method

If I let the user upload the .p12 certificate and the password, I can call the API with the client certificate auth without issues.

However, it would be much better if I create a Certificate Signing Request in AL (which is possible today), get the certificate created for me, and then create the .p12 certificate inside Business central.

That way, the primary key does not have to leave BC.

Expected behavior

Here is mock for how that could look like:

procedure CreateP12(CertBase64: Text; PrivateKey: SecretText; Password: SecretText)
var
    RSA: Codeunit RSA;
    X509Certificate2: Codeunit X509Certificate2;
    X509ContentType: Enum "X509 Content Type";
    P12CertBase64: Text;
begin
    RSA.FromSecretXmlString(PrivateKey);
    X509Certificate2.CreateFromPem(CertBase64, RSA);
    P12CertBase64 := X509Certificate2.Export(X509ContentType::Pkcs12, Password);
end;

The fix would include exposing RSA.FromSecretXmlString, X509Certificate2.CreateFromPem and X509Certificate2.Export with just small adjustments to use the X509ContentType Enum and return a base64 for easier handling

https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsa.fromxmlstring?view=net-9.0
https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.createfrompem?view=net-9.0
https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate.export?view=net-9.0#system-security-cryptography-x509certificates-x509certificate-export(system-security-cryptography-x509certificates-x509contenttype-system-security-securestring)

Steps to reproduce

See description above

Additional context

No response

I will provide a fix for a bug

  • I will provide a fix for a bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApprovedThe issue is approvedIntegrationGitHub request for Integration area

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions