2020-11-11 18:31:26 -05:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2020-10-24 16:02:38 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2020-10-24 16:02:38 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Misc
|
|
|
|
|
{
|
|
|
|
|
public class RemoteAssemblyHandler : IRemoteAssemblyHandler
|
|
|
|
|
{
|
2023-05-27 13:15:22 -04:00
|
|
|
|
private const int KeyLength = 32;
|
|
|
|
|
private const int TagLength = 16;
|
|
|
|
|
private const int NonceLength = 12;
|
|
|
|
|
private const int IterationCount = 10000;
|
2020-10-24 16:02:38 -04:00
|
|
|
|
|
|
|
|
|
private readonly ApplicationConfiguration _appconfig;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
public RemoteAssemblyHandler(ILogger<RemoteAssemblyHandler> logger, ApplicationConfiguration appconfig)
|
2020-10-24 16:02:38 -04:00
|
|
|
|
{
|
|
|
|
|
_appconfig = appconfig;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Assembly> DecryptAssemblies(string[] encryptedAssemblies)
|
|
|
|
|
{
|
|
|
|
|
return DecryptContent(encryptedAssemblies)
|
2023-05-27 13:15:22 -04:00
|
|
|
|
.Select(Assembly.Load);
|
2020-10-24 16:02:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> DecryptScripts(string[] encryptedScripts)
|
|
|
|
|
{
|
|
|
|
|
return DecryptContent(encryptedScripts).Select(decryptedScript => Encoding.UTF8.GetString(decryptedScript));
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-27 13:15:22 -04:00
|
|
|
|
private IEnumerable<byte[]> DecryptContent(string[] content)
|
2020-10-24 16:02:38 -04:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(_appconfig.Id) || string.IsNullOrWhiteSpace(_appconfig.SubscriptionId))
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogWarning($"{nameof(_appconfig.Id)} and {nameof(_appconfig.SubscriptionId)} must be provided to attempt loading remote assemblies/scripts");
|
2023-05-27 13:15:22 -04:00
|
|
|
|
return Array.Empty<byte[]>();
|
2020-10-24 16:02:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var assemblies = content.Select(piece =>
|
|
|
|
|
{
|
2023-05-27 13:15:22 -04:00
|
|
|
|
var byteContent = Convert.FromBase64String(piece);
|
|
|
|
|
var encryptedContent = byteContent.Take(byteContent.Length - (TagLength + NonceLength)).ToArray();
|
|
|
|
|
var tag = byteContent.Skip(byteContent.Length - (TagLength + NonceLength)).Take(TagLength).ToArray();
|
|
|
|
|
var nonce = byteContent.Skip(byteContent.Length - NonceLength).Take(NonceLength).ToArray();
|
|
|
|
|
var decryptedContent = new byte[encryptedContent.Length];
|
2020-10-24 16:02:38 -04:00
|
|
|
|
|
2023-05-27 13:15:22 -04:00
|
|
|
|
var keyGen = new Rfc2898DeriveBytes(Encoding.UTF8.GetBytes(_appconfig.SubscriptionId), Encoding.UTF8.GetBytes(_appconfig.Id), IterationCount, HashAlgorithmName.SHA512);
|
|
|
|
|
var encryption = new AesGcm(keyGen.GetBytes(KeyLength));
|
2020-10-24 16:02:38 -04:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
encryption.Decrypt(nonce, encryptedContent, tag, decryptedContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (CryptographicException ex)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogError(ex, "Could not decrypt remote plugin assemblies");
|
2020-10-24 16:02:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return decryptedContent;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return assemblies.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|