Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,34 @@ codeunit 132617 "RSA Test"
EncryptingOutStream: OutStream;
EncryptedInStream: InStream;
EncryptedOutStream: OutStream;
DecryptedInStream: InStream;
DecryptedOutStream: OutStream;
PlainText: Text;
DecryptedText: Text;
DecryptionFailed: Boolean;
begin
// [SCENARIO] Decrypt text encrypted with use of PKCS#1 padding, using OAEP padding.
// [SCENARIO] Due to random padding, decryption may occasionally not throw but returns garbage data.
Initialize();

// [GIVEN] With RSA pair of keys, plain text and encryption stream
EncryptingTempBlob.CreateOutStream(EncryptingOutStream);
SaveRandomTextToOutStream(EncryptingOutStream);
PlainText := SaveRandomTextToOutStream(EncryptingOutStream);
EncryptingTempBlob.CreateInStream(EncryptingInStream);
EncryptedTempBlob.CreateOutStream(EncryptedOutStream);
RSA.Encrypt(PrivateKeyXmlStringSecret, EncryptingInStream, false, EncryptedOutStream);
EncryptedTempBlob.CreateInStream(EncryptedInStream);

// [WHEN] Decrypt encrypted text stream using OAEP Padding
DecryptingTempBlob.CreateOutStream(DecryptedOutStream);
asserterror RSA.Decrypt(PrivateKeyXmlStringSecret, EncryptedInStream, true, DecryptedOutStream);
DecryptionFailed := not TryDecrypt(RSA, PrivateKeyXmlStringSecret, EncryptedInStream, true, DecryptedOutStream);

// [THEN] Either decryption fails with an exception, or the decrypted text is garbage (not equal to plaintext)
if not DecryptionFailed then begin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no verification if encryption fails, no check whether the error is the expected one...

DecryptingTempBlob.CreateInStream(DecryptedInStream);
DecryptedText := Base64Convert.FromBase64(Base64Convert.ToBase64(DecryptedInStream));
LibraryAssert.AreNotEqual(PlainText, DecryptedText, 'Decryption with wrong padding should fail or return garbage data.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your message here should only be about garbage data no? if encryption fails you will never get to this assert

end;
end;

[Test]
Expand All @@ -251,22 +263,40 @@ codeunit 132617 "RSA Test"
EncryptingOutStream: OutStream;
EncryptedInStream: InStream;
EncryptedOutStream: OutStream;
DecryptedInStream: InStream;
DecryptedOutStream: OutStream;
PlainText: Text;
DecryptedText: Text;
DecryptionFailed: Boolean;
begin
// [SCENARIO] Decrypt text encrypted with use of OAEP padding, using PKCS#1 padding.
// [SCENARIO] Due to random padding, decryption may occasionally not throw but returns garbage data.
Initialize();

// [GIVEN] With RSA pair of keys, plain text, padding and encryption stream
EncryptingTempBlob.CreateOutStream(EncryptingOutStream);
SaveRandomTextToOutStream(EncryptingOutStream);
PlainText := SaveRandomTextToOutStream(EncryptingOutStream);
EncryptingTempBlob.CreateInStream(EncryptingInStream);
EncryptedTempBlob.CreateOutStream(EncryptedOutStream);
RSA.Encrypt(PrivateKeyXmlStringSecret, EncryptingInStream, true, EncryptedOutStream);
EncryptedTempBlob.CreateInStream(EncryptedInStream);

// [WHEN] Decrypt encrypted text stream using PKCS#1 padding.
DecryptingTempBlob.CreateOutStream(DecryptedOutStream);
asserterror RSA.Decrypt(PrivateKeyXmlStringSecret, EncryptedInStream, false, DecryptedOutStream);
DecryptionFailed := not TryDecrypt(RSA, PrivateKeyXmlStringSecret, EncryptedInStream, false, DecryptedOutStream);

// [THEN] Either decryption fails with an exception, or the decrypted text is garbage (not equal to plaintext)
if not DecryptionFailed then begin
DecryptingTempBlob.CreateInStream(DecryptedInStream);
DecryptedText := Base64Convert.FromBase64(Base64Convert.ToBase64(DecryptedInStream));
LibraryAssert.AreNotEqual(PlainText, DecryptedText, 'Decryption with wrong padding should fail or return garbage data.');
end;
end;

[TryFunction]
local procedure TryDecrypt(RSA: Codeunit RSA; XmlString: SecretText; EncryptedInStream: InStream; OaepPadding: Boolean; DecryptedOutStream: OutStream)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is better to have 2 named helpers than have a boolean parameter

begin
RSA.Decrypt(XmlString, EncryptedInStream, OaepPadding, DecryptedOutStream);
end;

local procedure SaveRandomTextToOutStream(OutStream: OutStream) PlainText: Text
Expand Down
Loading