Skip to content

Commit

Permalink
Cherry pick branch 'genexuslabs:gamutils_eo' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrampone authored and Beta Bot committed Jan 22, 2025
1 parent a334e66 commit 632300b
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public static string RandomHexaBits(int bits)
[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); }
Expand All @@ -74,7 +77,20 @@ public static string RandomHexaBits(int bits)
[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
Expand Up @@ -2,6 +2,7 @@
using Org.BouncyCastle.Utilities.Encoders;
using log4net;
using System.Security;
using log4net.Repository.Hierarchy;

namespace GamUtils.Utils
{
Expand All @@ -24,5 +25,51 @@ internal static string B64ToB64Url(string input)
return "";
}
}

[SecuritySafeCritical]
internal static string HexaToBase64(string hexa)
{
logger.Debug("HexaToBase64");
try
{
return Base64.ToBase64String(Hex.Decode(hexa));
}
catch (Exception e)
{
logger.Error("HexaToBase64", e);
return "";
}
}

[SecuritySafeCritical]
internal static string ToBase64Url(string input)
{
logger.Debug("ToBase64Url");
try
{
return System.Text.Encoding.UTF8.GetString(UrlBase64.Encode(System.Text.Encoding.UTF8.GetBytes(input)));
}
catch (Exception e)
{
logger.Error("ToBase64Url", e);
return "";
}
}

[SecuritySafeCritical]
internal static string FromBase64Url(string base64)
{
logger.Debug("FromBase64Url");
try
{
return System.Text.Encoding.UTF8.GetString(UrlBase64.Decode(System.Text.Encoding.UTF8.GetBytes(base64)));
}
catch (Exception e)
{
logger.Error("FromBase64Url", e);
return "";
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Jose;
using log4net;

namespace GamUtils.Utils.Json
{
public enum JWTAlgorithm
{
none, HS256, HS384, HS512, RS256, RS512
}

public class JWTAlgorithmUtils
{
private static readonly ILog logger = LogManager.GetLogger(typeof(JWTAlgorithmUtils));
internal static JwsAlgorithm GetJWSAlgorithm(JWTAlgorithm alg)
{
logger.Debug("GetJWSAlgorithm");
switch (alg)
{
case JWTAlgorithm.HS256:
return JwsAlgorithm.HS256;
case JWTAlgorithm.HS512:
return JwsAlgorithm.HS512;
case JWTAlgorithm.HS384:
return JwsAlgorithm.HS384;
case JWTAlgorithm.RS256:
return JwsAlgorithm.RS256;
case JWTAlgorithm.RS512:
return JwsAlgorithm.RS512;
default:
logger.Error("GetJWSAlgorithm - not implemented algorithm");
return JwsAlgorithm.none;
}
}

internal static JWTAlgorithm GetJWTAlgoritm(string alg)
{
logger.Debug("GetJWTAlgoritm");
switch (alg.Trim().ToUpper())
{
case "HS256":
return JWTAlgorithm.HS256;
case "HS512":
return JWTAlgorithm.HS512;
case "HS384":
return JWTAlgorithm.HS384;
case "RS256":
return JWTAlgorithm.RS256;
case "RS512":
return JWTAlgorithm.RS512;
default:
logger.Error("GetJWTAlgoritm- not implemented algorithm");
return JWTAlgorithm.none;
}
}

internal static bool IsSymmetric(JWTAlgorithm alg)
{
logger.Debug("IsSymmetric");
switch (alg)
{
case JWTAlgorithm.HS256:
case JWTAlgorithm.HS384:
case JWTAlgorithm.HS512:
return true;
case JWTAlgorithm.RS256:
case JWTAlgorithm.RS512:
return false;
default:
logger.Error("IsSymmetric - not implemented algorithm");
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ internal static string GetPublic(string jwkString)
}
}

[SecuritySafeCritical]
internal static string GetAlgorithm(string jwkString)
{
if (string.IsNullOrEmpty(jwkString))
{
logger.Error("GetAlgorithm jwkString parameter is empty");
return "";
}
try
{
Jose.Jwk jwk = Jose.Jwk.FromJson(jwkString);
return jwk.Alg;
}catch(Exception e)
{
logger.Error("GetAlgorithm", e);
return "";
}

}

/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/

[SecuritySafeCritical]
Expand Down
Loading

0 comments on commit 632300b

Please sign in to comment.