-
Notifications
You must be signed in to change notification settings - Fork 290
[Cryptography Management] Fix instability test #5169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| DecryptingTempBlob.CreateInStream(DecryptedInStream); | ||
| DecryptedText := Base64Convert.FromBase64(Base64Convert.ToBase64(DecryptedInStream)); | ||
| LibraryAssert.AreNotEqual(PlainText, DecryptedText, 'Decryption with wrong padding should fail or return garbage data.'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
There was a problem hiding this comment.
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...