Skip to content
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

GAM Cryptography EO migration from SVN to GamUtils EO on Github #1055

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c90bd11
GAM Cryptography EO migration from SVN to GamUtils EO on Github
sgrampone Aug 2, 2024
a392103
Ignore pfx test
sgrampone Aug 2, 2024
270bb10
Fix AssemblyInfo VisibleTo property
sgrampone Aug 2, 2024
c8a7a4a
New features + tests and refactoring
sgrampone Aug 7, 2024
7ff5f04
Fix missing test file
sgrampone Aug 7, 2024
7903f7b
Complete hexabits test
sgrampone Aug 7, 2024
697b55d
GamUtils NetCore implementation + tests
sgrampone Aug 8, 2024
0558e6f
GamUtilsEO simplification, module refacoring + tests
sgrampone Aug 15, 2024
de01a25
Fix GamUtils projects PackageId
sgrampone Aug 15, 2024
d7893b9
Add DynamicCall EO to GamUtils
sgrampone Aug 19, 2024
6846b23
Add sha256 and b64url encoding for PKCE implementation
sgrampone Aug 30, 2024
3862706
Update BouncyCastle to version 2.4.0 for testing projects in the beta…
Sep 10, 2024
126fc2c
Update BouncyCastle to version 2.4.0 to align with the changes introd…
Sep 10, 2024
03e204c
Strong naming GamUtils dll
sgrampone Oct 14, 2024
597a8af
Remove unused target framework net6.0 from GamUtilsNet.csproj
claudiamurialdo Nov 12, 2024
2ea6bee
Remove unused target framework net6.0 from GamTestNet.csproj
claudiamurialdo Nov 12, 2024
8e93fc1
Adding missing functions
sgrampone Jan 15, 2025
7fb3901
Adding Base64 encoding features and fixing symmetric and asymmetric J…
sgrampone Jan 21, 2025
fb28264
Replace System.Security.Cryptography.Cng version 4.7.0 with 5.0.0 for…
claudiamurialdo Feb 4, 2025
9ad4adc
Merge branch 'master' into gamutils_eo
claudiamurialdo Feb 4, 2025
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
50 changes: 50 additions & 0 deletions dotnet/DotNetStandardClasses.sln
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,34 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LogTest", "test\benchmarks\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccessTokenController_Test", "test\NativeAccessControllerTest\AccessTokenController_Test.csproj", "{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gam", "gam", "{BAD7F078-C67E-484A-AF74-EC29F163F1F7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7465132B-47BD-44D1-8193-483465FD94F4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{035C3DAF-553E-4E64-BA9E-113F5C80FD97}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DotNet", "DotNet", "{56429994-8A51-48AB-8105-737B77840D18}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DotNetFramework", "DotNetFramework", "{B3B988A2-AFF1-41E6-82DC-578D332A5CB8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GamUtils", "src\extensions\gam\src\DotNetFramework\GamUtils\GamUtils.csproj", "{34FE57F2-E45F-4F9B-AFEE-60D9CA8373A7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DotNet", "DotNet", "{1942B0FE-49BD-4EC4-9788-F19FDD921E69}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DotNetFramework", "DotNetFramework", "{B33F3709-5EA0-4FE8-9E3A-50D3046817D6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GamTest", "src\extensions\gam\test\DotNetFramework\GamTest\GamTest.csproj", "{52B67EA3-B17D-4002-8EC3-0D5DDB62988B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GamUtilsNet", "src\extensions\gam\src\DotNet\GamUtilsNet\GamUtilsNet.csproj", "{AA93273B-2E0B-4CD3-A921-A462B214F424}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GamTestNet", "src\extensions\gam\test\DotNet\GamTestNet\GamTestNet.csproj", "{D46D0666-AF65-4875-954C-82AB2240B17D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ws", "ws", "{B5C28D81-BCD9-4B29-9B68-EDD81D1018D5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F8959289-4ED7-430C-97B7-FAAA29829708}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GxSoapHandler", "src\extensions\ws\src\GxSoapHandler\GxSoapHandler.csproj", "{58C84EC7-A0B3-4C1B-BD78-989AEE87EA32}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Functions", "Functions", "{E59B3248-4C26-4DB0-96CB-67437319E22B}"
EndProject
Global
Expand Down Expand Up @@ -648,6 +671,22 @@ Global
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1}.Release|Any CPU.Build.0 = Release|Any CPU
{34FE57F2-E45F-4F9B-AFEE-60D9CA8373A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{34FE57F2-E45F-4F9B-AFEE-60D9CA8373A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34FE57F2-E45F-4F9B-AFEE-60D9CA8373A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34FE57F2-E45F-4F9B-AFEE-60D9CA8373A7}.Release|Any CPU.Build.0 = Release|Any CPU
{52B67EA3-B17D-4002-8EC3-0D5DDB62988B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{52B67EA3-B17D-4002-8EC3-0D5DDB62988B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52B67EA3-B17D-4002-8EC3-0D5DDB62988B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52B67EA3-B17D-4002-8EC3-0D5DDB62988B}.Release|Any CPU.Build.0 = Release|Any CPU
{AA93273B-2E0B-4CD3-A921-A462B214F424}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA93273B-2E0B-4CD3-A921-A462B214F424}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA93273B-2E0B-4CD3-A921-A462B214F424}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA93273B-2E0B-4CD3-A921-A462B214F424}.Release|Any CPU.Build.0 = Release|Any CPU
{D46D0666-AF65-4875-954C-82AB2240B17D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D46D0666-AF65-4875-954C-82AB2240B17D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D46D0666-AF65-4875-954C-82AB2240B17D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D46D0666-AF65-4875-954C-82AB2240B17D}.Release|Any CPU.Build.0 = Release|Any CPU
{58C84EC7-A0B3-4C1B-BD78-989AEE87EA32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58C84EC7-A0B3-4C1B-BD78-989AEE87EA32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58C84EC7-A0B3-4C1B-BD78-989AEE87EA32}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -778,6 +817,17 @@ Global
{46DAAFD1-FAF5-4904-8EC5-406BE04E5538} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
{A1DBDCE0-4F09-445F-A202-9B260CDD46CF} = {46DAAFD1-FAF5-4904-8EC5-406BE04E5538}
{A5589382-DB6F-4450-AE2B-6C6AA1643EF1} = {1D6F1776-FF4B-46C2-9B3D-BC46CCF049DC}
{BAD7F078-C67E-484A-AF74-EC29F163F1F7} = {C6AFB6A3-FF0B-4970-B1F1-10BCD3D932B2}
{7465132B-47BD-44D1-8193-483465FD94F4} = {BAD7F078-C67E-484A-AF74-EC29F163F1F7}
{035C3DAF-553E-4E64-BA9E-113F5C80FD97} = {BAD7F078-C67E-484A-AF74-EC29F163F1F7}
{56429994-8A51-48AB-8105-737B77840D18} = {7465132B-47BD-44D1-8193-483465FD94F4}
{B3B988A2-AFF1-41E6-82DC-578D332A5CB8} = {7465132B-47BD-44D1-8193-483465FD94F4}
{34FE57F2-E45F-4F9B-AFEE-60D9CA8373A7} = {B3B988A2-AFF1-41E6-82DC-578D332A5CB8}
{1942B0FE-49BD-4EC4-9788-F19FDD921E69} = {035C3DAF-553E-4E64-BA9E-113F5C80FD97}
{B33F3709-5EA0-4FE8-9E3A-50D3046817D6} = {035C3DAF-553E-4E64-BA9E-113F5C80FD97}
{52B67EA3-B17D-4002-8EC3-0D5DDB62988B} = {B33F3709-5EA0-4FE8-9E3A-50D3046817D6}
{AA93273B-2E0B-4CD3-A921-A462B214F424} = {56429994-8A51-48AB-8105-737B77840D18}
{D46D0666-AF65-4875-954C-82AB2240B17D} = {1942B0FE-49BD-4EC4-9788-F19FDD921E69}
{B5C28D81-BCD9-4B29-9B68-EDD81D1018D5} = {C6AFB6A3-FF0B-4970-B1F1-10BCD3D932B2}
{F8959289-4ED7-430C-97B7-FAAA29829708} = {B5C28D81-BCD9-4B29-9B68-EDD81D1018D5}
{58C84EC7-A0B3-4C1B-BD78-989AEE87EA32} = {F8959289-4ED7-430C-97B7-FAAA29829708}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
<Compile Include="..\..\dotnetframework\SecurityAPICommons\Utils\SecurityUtils.cs" Link="Utils\SecurityUtils.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="jose-jwt" Version="5.0.0" />
<PackageReference Include="System.Security.Cryptography.Cng" Version="4.7.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="jose-jwt" Version="5.0.0" />
<PackageReference Include="System.Security.Cryptography.Cng" Version="5.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Config\" />
Expand Down
Binary file added dotnet/src/extensions/gam/gamkey.snk
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<AssemblyName>GamUtilsNetImpl</AssemblyName>
<Version>17.4.0</Version>
<NoWarn>CA1031, CA1801, SYSLIB0027</NoWarn>
<PackageId>Gam.Utils.Net</PackageId>
</PropertyGroup>

<PropertyGroup>
<DefineConstants>NETCORE</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\DotNetFramework\GamUtils\GamUtilsEO.cs" Link="GamUtilsEO.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Cryprography\Encryption.cs" Link="Utils\Cryptography\Encryption.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Cryprography\Hash.cs" Link="Utils\Cryptography\Hash.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\DynamicCall.cs" Link="Utils\DynamicCall.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Json\Jwk.cs" Link="Utils\Json\Jwk.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Json\Jwt.cs" Link="Utils\Json\Jwt.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Json\JWTAlgorithm.cs" Link="Utils\Json\JWTAlgorithm.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Json\UnixTimestamp.cs" Link="Utils\Json\UnixTimestamp.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Keys\PublicKeyExt.cs" Link="Utils\Keys\PublicKeyExt.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Keys\PrivateKeyExt.cs" Link="Utils\Keys\PrivateKeyExt.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Random.cs" Link="Utils\Random.cs" />
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Encoding.cs" Link="Utils\Encoding.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="jose-jwt" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Security.Cryptography.Cng" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\..\dotnetcore\GxClasses\GxClasses.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Utils\Cryptography\" />
<Folder Include="Utils\Keys\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net47</TargetFramework>
<AssemblyName>GamUtilsImpl</AssemblyName>
<NoWarn>CA1031, CA1801</NoWarn>
<PackageId>Gam.Utils</PackageId>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\..\gamkey.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>True</Optimize>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="jose-jwt" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="StrongNamer" Version="0.2.5" />
<PackageReference Include="System.Security.Cryptography.Cng" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\..\dotnetframework\GxClasses\GxClasses.csproj" />
</ItemGroup>


</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Security;
using GamUtils.Utils;
using GamUtils.Utils.Cryprography;
using GamUtils.Utils.Json;

namespace GamUtils
{
[SecuritySafeCritical]
public class GamUtilsEO
{
/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/

//**HASH**//
[SecuritySafeCritical]
public static string Sha512(string plainText)
{
return HashUtil.Hashing(plainText, Hash.SHA512);
}

[SecuritySafeCritical]
public static string Sha256(string plainText)
{
return HashUtil.Hashing(plainText, Hash.SHA256);
}

//**ENCRYPTION**//

[SecuritySafeCritical]
public static string AesGcm(string input, string key, string nonce, int macSize, bool toEncrypt)
{
return Encryption.AesGcm(input, key, nonce, macSize, toEncrypt);
}

//**RANDOM**//
[SecuritySafeCritical]
public static string RandomAlphanumeric(int length)
{
return Utils.Random.Alphanumeric(length);
}

[SecuritySafeCritical]
public static string RandomNumeric(int length)
{
return Utils.Random.Numeric(length);
}

[SecuritySafeCritical]
public static string RandomHexaBits(int bits)
{
return Utils.Random.HexaBits(bits);
}

//**JWK**//
[SecuritySafeCritical]
public static string GenerateKeyPair() { return Jwk.GenerateKeyPair(); }

[SecuritySafeCritical]
public static string GetPublicJwk(string jwkString) { return Jwk.GetPublic(jwkString); }

[SecuritySafeCritical]
public static string GetJwkAlgorithm(string jwkString) { return Jwk.GetAlgorithm(jwkString); }

//**JWT**//
[SecuritySafeCritical]
public static bool VerifyJwt(string path, string alias, string password, string token) { return Jwt.Verify(path, alias, password, token); }

[SecuritySafeCritical]
public static string CreateJwt(string path, string alias, string password, string payload, string header) { return Jwt.Create(path, alias, password, payload, header); }

[SecuritySafeCritical]
public static long CreateUnixTimestamp(DateTime date) { return UnixTimestamp.Create(date); }

[SecuritySafeCritical]
public static string GetJwtHeader(string token) { return Jwt.GetHeader(token); }

[SecuritySafeCritical]
public static string GetJwtPayload(string token) { return Jwt.GetPayload(token); }

[SecuritySafeCritical]
public static bool VerifyAlgorithm(string expectedAlgorithm, string token) { return Jwt.VerifyAlgorithm(expectedAlgorithm, token); }

//**ENCODING**//
[SecuritySafeCritical]
public static string Base64ToBase64Url(string base64) { return Encoding.B64ToB64Url(base64); }

[SecuritySafeCritical]
public static string HexaToBase64(string hexa) { return Encoding.HexaToBase64(hexa); }

[SecuritySafeCritical]
public static string ToBase64Url(string input) { return Encoding.ToBase64Url(input); }

[SecuritySafeCritical]
public static string FromBase64Url(string base64) { return Encoding.FromBase64Url(base64); }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System.Security;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AllowPartiallyTrustedCallers]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Security;
using System.Text;
using log4net;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities.Encoders;

namespace GamUtils.Utils.Cryprography
{
[SecuritySafeCritical]
internal class Encryption
{

private static readonly ILog logger = LogManager.GetLogger(typeof(Encryption));

[SecuritySafeCritical]
public static string AesGcm(string input, string key, string nonce, int macSize, bool toEncrypt)
{
return toEncrypt ? Base64.ToBase64String(Internal_AesGcm(System.Text.Encoding.UTF8.GetBytes(input), key, nonce, macSize, toEncrypt)) : System.Text.Encoding.UTF8.GetString(Internal_AesGcm(Base64.Decode(input), key, nonce, macSize, toEncrypt));
}

[SecuritySafeCritical]
private static byte[] Internal_AesGcm(byte[] inputBytes, string key, string nonce, int macSize, bool toEncrypt)
{
logger.Debug("Internal_AesGcm");

IAeadBlockCipher cipher = new GcmBlockCipher(new AesEngine());
AeadParameters AEADparams = new AeadParameters(new KeyParameter(Hex.Decode(key)), macSize, Hex.Decode(nonce));
try
{
cipher.Init(toEncrypt, AEADparams);
byte[] outputBytes = new byte[cipher.GetOutputSize(inputBytes.Length)];
int length = cipher.ProcessBytes(inputBytes, 0, inputBytes.Length, outputBytes, 0);
cipher.DoFinal(outputBytes, length);
return outputBytes;
}
catch (Exception e)
{
logger.Error("Internal_AesGcm", e);
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Text;
using log4net;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Utilities.Encoders;
using System.Security;

namespace GamUtils.Utils
{
[SecuritySafeCritical]
internal enum Hash
{
NONE, SHA512, SHA256
}

[SecuritySafeCritical]
public class HashUtil
{

private static readonly ILog logger = LogManager.GetLogger(typeof(HashUtil));

[SecuritySafeCritical]
internal static string Hashing(string plainText, Hash hash)
{
switch (hash)
{
case Hash.SHA256:
return InternalHash(new Sha256Digest(), plainText);
case Hash.SHA512:
return InternalHash(new Sha512Digest(), plainText);
default:
logger.Error("unrecognized hash");
return "";
}
}

private static string InternalHash(IDigest digest, string plainText)
{
logger.Debug("InternalHash");
if (String.IsNullOrEmpty(plainText))
{
logger.Error("hash plainText is empty");
return "";
}
byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
byte[] retValue = new byte[digest.GetDigestSize()];
digest.BlockUpdate(inputBytes, 0, inputBytes.Length);
digest.DoFinal(retValue, 0);
return Base64.ToBase64String(retValue);
}
}
}

Loading
Loading