From ae6586499b127f2e58efecbada588139ca5ae39b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Hartvig=20Gr=C3=B8nbech?= Date: Mon, 25 Mar 2024 16:03:14 +0100 Subject: [PATCH] Move to generated keys --- .../src/RSACryptoServiceProvider.Codeunit.al | 13 +++++++++++++ .../src/RSACryptoServiceProviderImpl.Codeunit.al | 11 +++++++++++ .../src/RSACryptoServiceProviderTests.Codeunit.al | 3 +-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/System Application/App/Cryptography Management/src/RSACryptoServiceProvider.Codeunit.al b/src/System Application/App/Cryptography Management/src/RSACryptoServiceProvider.Codeunit.al index 50bed37e55..609d6aa250 100644 --- a/src/System Application/App/Cryptography Management/src/RSACryptoServiceProvider.Codeunit.al +++ b/src/System Application/App/Cryptography Management/src/RSACryptoServiceProvider.Codeunit.al @@ -155,4 +155,17 @@ codeunit 1445 RSACryptoServiceProvider begin RSACryptoServiceProviderImpl.Decrypt(XmlString, EncryptedTextInStream, OaepPadding, DecryptedTextOutStream); end; + + /// + /// The CreateRSAKeyPair procedure is a function that generates a public and private RSA key pair. + /// + /// This is an output parameter that returns the public key in XML format. + /// This is an output parameter that returns the private key in XML format. This is a sensitive information hence marked as SecretText. + /// + /// This function does not return a value. The output is via the two parameters PublicKeyInXML and PrivateKeyInXML. + /// + procedure CreateRSAKeyPair(var PublicKeyInXML: Text; var PrivateKeyInXML: SecretText) + begin + RSACryptoServiceProviderImpl.CreateRSAKeyPair(PublicKeyInXML, PrivateKeyInXML); + end; } \ No newline at end of file diff --git a/src/System Application/App/Cryptography Management/src/RSACryptoServiceProviderImpl.Codeunit.al b/src/System Application/App/Cryptography Management/src/RSACryptoServiceProviderImpl.Codeunit.al index 9a4e120f34..dae4f8a1ba 100644 --- a/src/System Application/App/Cryptography Management/src/RSACryptoServiceProviderImpl.Codeunit.al +++ b/src/System Application/App/Cryptography Management/src/RSACryptoServiceProviderImpl.Codeunit.al @@ -32,6 +32,17 @@ codeunit 1446 "RSACryptoServiceProvider Impl." implements "Signature Algorithm v DotNetAsymmetricAlgorithm := DotNetRSACryptoServiceProvider; end; + [NonDebuggable] + procedure CreateRSAKeyPair(var PublicKeyInXML: Text; var PrivateKeyInXML: SecretText) + var + DotnetRSA: DotNet RSA; + begin + RSACryptoServiceProvider(); + DotnetRSA := DotNetRSACryptoServiceProvider.Create(); + PublicKeyInXML := DotnetRSA.ToXmlString(false); + PrivateKeyInXML := DotnetRSA.ToXmlString(true); + end; + #region SignData procedure SignData(XmlString: SecretText; DataInStream: InStream; HashAlgorithm: Enum "Hash Algorithm"; SignatureOutStream: OutStream) begin diff --git a/src/System Application/Test/Cryptography Management/src/RSACryptoServiceProviderTests.Codeunit.al b/src/System Application/Test/Cryptography Management/src/RSACryptoServiceProviderTests.Codeunit.al index fd848637a1..760c5e2f7b 100644 --- a/src/System Application/Test/Cryptography Management/src/RSACryptoServiceProviderTests.Codeunit.al +++ b/src/System Application/Test/Cryptography Management/src/RSACryptoServiceProviderTests.Codeunit.al @@ -27,8 +27,7 @@ codeunit 132613 RSACryptoServiceProviderTests begin if IsInitialized then exit; - PrivateKeyXmlStringSecret := Base64Convert.FromBase64('PFJTQUtleVZhbHVlPjxNb2R1bHVzPmNKMHBpL2ZBcXJTRy9TZTUrZGdzcksyNjNwSUkvQjhieGZyWDBoMm1aN0VZUWhURXhYa2d6Z0xwS3dFRWFmdUs0MFVqUjEzNlNLQ3I5S240aHpPek1zam5hMXJJRXJCUnMzcU5IS1NxS3JzdkhaUTljRFo3RElVekh2cVh0Q01KMFo0clFBZlZLcWl2bGZqd3RJR1ZPSXRKK0RMT1E4S2tZNjJiOTI2YzhMTEtGUG5SU1ZxQnQrL3lGbEJ3M1NlQWdNYzBtaXRKRUhvYW5LdExXc3RLVzlDTW55OWc1R1V2cnZaY0wwUjR4NkpabFlGTVMwQnBmdFJNbmFrbnI3VXpxMGdvWnY4OHhmb1IrTW1ud1ZUOWdTU0ZRRW1rOVRHaVdjd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xQnQrL3lGbEJ3M1NlQWdNYzBtaXRKRUhvYW5LdExXc3RLVzlDTW55OWc1R1V2cnZaY0wwUjR4NkpabFlGTVMwQnBmdFJNbmFrbnI3VXpxMGdvWnY4OHhmb1IrTW1ud1ZUOWdTU0ZRRW1rOVRHaVdjdUV1bnFNUmZ4b3htQW0zVGt3RTRMSDVDNHVNMWRlUGVnWDM0bmoyZmphOWhpUWtReHVqdz09PC9Nb2R1bHVzPjxFeHBvbmVudD5BUUFCPC9FeHBvbmVudD48L1JTQUtleVZhbHVlPg=='); + RSACryptoServiceProvider.CreateRSAKeyPair(PublicKeyXmlString, PrivateKeyXmlStringSecret); IsInitialized := true; end;