Skip to content

Commit

Permalink
Added auth tag size validation to AesGcm decrypt to always ensure 16 …
Browse files Browse the repository at this point in the history
…bytes tags
  • Loading branch information
dvsekhvalnov committed Nov 6, 2024
1 parent 9d09c4f commit 18f15e8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
19 changes: 19 additions & 0 deletions UnitTestsNet46/SecurityVulnerabilitiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,24 @@ public void DeflateBomb()
Console.Out.WriteLine(e.ToString());
}
}

[Fact]
public void TruncatedGcmAuthTag()
{
// given
string token = "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..PEXf1goWOF0SZRe_.Zp3CHYq4ZqM3_opMIy25O50gmQzw_p-nCOiW2ROuQSv80-aD-78n8m103kgPRPCsOt7qrckDRGSDACOBZGr2WovzSC-dxIcW3EsPqtibueyh0p3FY43h-bcbhPzXBdjQPaNTCY0o26wcEV_4FzPYdE9_ngRFIUe_7Kby-E2CWYLFc5D9RO9TLGN5dpHL6l4SOGbNz8M0o4aQuyJv3BV1wj_KswqyVcKBHjm0eh6RmFhoERxWjvt5yeo83bzxTfReVWAxXw.AVLr7JE1r1uiUSLj";

try
{
// when decrypt token with trunated AES GCM tag, it should fail
Jose.JWT.Decode(token, aes128Key);
Assert.True(false, "Should fail with IntegrityException");

}
catch (ArgumentException e)
{
Console.Out.WriteLine(e.ToString());
}
}
}
}
7 changes: 5 additions & 2 deletions jose-jwt/crypto/AesGcm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Jose
{
public static class AesGcm
{
{
/// <summary>
/// Performs AES encryption in GCM chaining mode over plain text
/// </summary>
Expand Down Expand Up @@ -71,11 +71,14 @@ public static byte[] Decrypt(byte[] key, byte[] iv, byte[] aad, byte[] cipherTex
IntPtr hKey, keyDataBuffer = ImportKey(hAlg, key, out hKey);

byte[] plainText;
int expectedTagSize = MaxAuthTagSize(hAlg);

Ensure.ByteSize(authTag, expectedTagSize, "Expected auth tag of length: {0} bytes, but got: {1} bytes", expectedTagSize, authTag.Length);

var authInfo = new BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO(iv, aad, authTag);
using (authInfo)
{
byte[] ivData = new byte[MaxAuthTagSize(hAlg)];
byte[] ivData = new byte[expectedTagSize];

int plainTextSize = 0;

Expand Down
6 changes: 6 additions & 0 deletions jose-jwt/util/Ensure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public static void BitSize(byte[] array, long expectedSize, string msg, params o
if(expectedSize != array.Length * 8L)
throw new ArgumentException(string.Format(msg,args));
}

public static void ByteSize(byte[] array, long expectedSize, string msg, params object[] args)
{
if(expectedSize != array.Length)
throw new ArgumentException(string.Format(msg,args));
}

public static void BitSize(int actualSize, int expectedSize, string msg)
{
Expand Down

0 comments on commit 18f15e8

Please sign in to comment.