mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-11 02:24:36 +01:00
Change encArg impl
This commit is contained in:
parent
349ba01572
commit
bda6b9a822
3 changed files with 98 additions and 99 deletions
|
|
@ -94,11 +94,15 @@ namespace Dalamud.Bootstrap
|
|||
throw new BootstrapException($"Process id {process.Id} does not have any arguments to parse.");
|
||||
}
|
||||
|
||||
if (EncryptedArgument.Extract(arguments[0], out var encryptedPayload, out var _))
|
||||
var argument = arguments[0];
|
||||
|
||||
if (EncryptedArgument.TryParse(argument, out var encryptedArgument))
|
||||
{
|
||||
var key = RecoverKey(process);
|
||||
EncryptedArgument.Decry(encryptedPayload, key)
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -1,36 +1,40 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Buffers.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Dalamud.Bootstrap.Crypto;
|
||||
|
||||
namespace Dalamud.Bootstrap.SqexArg
|
||||
{
|
||||
internal sealed class EncryptedArgument
|
||||
{
|
||||
private static char[] ChecksumTable = new char[]
|
||||
{
|
||||
'f', 'X', '1', 'p', 'G', 't', 'd', 'S',
|
||||
'5', 'C', 'A', 'P', '4', '_', 'V', 'L'
|
||||
};
|
||||
/// <summary>
|
||||
/// A data that is encrypted and encoded in url-safe variant of base64.
|
||||
/// </summary>
|
||||
public string Data { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Denotes that no checksum is encoded.
|
||||
/// A checksum that is used to validate the encryption key.
|
||||
/// </summary>
|
||||
private const char NoChecksumMarker = '!';
|
||||
public char Checksum { get; }
|
||||
|
||||
private IMemoryOwner<byte> m_encryptedData;
|
||||
public EncryptedArgument(string data, char checksum)
|
||||
{
|
||||
Data = data;
|
||||
Checksum = checksum;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts the argument with given key.
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="argument"></param>
|
||||
/// <param name="key"></param>
|
||||
public EncryptedArgument(string argument, uint key)
|
||||
/// <param name="argument">An argument that is encrypted and usually starts with //**sqex0003 and ends with **//</param>
|
||||
/// <returns>Returns true if successful, false otherwise.</returns>
|
||||
public static bool TryParse(string argument, out EncryptedArgument output)
|
||||
{
|
||||
|
||||
if (!Extract(argument, out var data, out var checksum))
|
||||
{
|
||||
output = null!;
|
||||
return false;
|
||||
}
|
||||
|
||||
output = new EncryptedArgument(data, checksum);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -40,11 +44,11 @@ namespace Dalamud.Bootstrap.SqexArg
|
|||
/// <param name="payload">An encrypted payload extracted. The value is undefined if the function fails.</param>
|
||||
/// <param name="checksum">A checksum of the key extracted. The value is undefined if the function fails.</param>
|
||||
/// <returns>Returns true on success, false otherwise.</returns>
|
||||
public static bool Extract(string argument, out byte[] payload, out char checksum)
|
||||
private static bool Extract(string argument, out string payload, out char checksum)
|
||||
{
|
||||
// must start with //**sqex0003, some characters, one checksum character and end with **//
|
||||
var regex = new Regex(@"^\/\/\*\*sqex0003(?<payload>.+)(?<checksum>.)\*\*\/\/$");
|
||||
|
||||
|
||||
var match = regex.Match(argument);
|
||||
if (!match.Success)
|
||||
{
|
||||
|
|
@ -53,86 +57,13 @@ namespace Dalamud.Bootstrap.SqexArg
|
|||
return false;
|
||||
}
|
||||
|
||||
// Extract checksum
|
||||
// Extract
|
||||
checksum = match.Groups["checksum"].Value[0];
|
||||
|
||||
// Extract payload
|
||||
var payloadStr = match.Groups["payload"].Value;
|
||||
payload = DecodeUrlSafeBase64(payloadStr);
|
||||
payload = match.Groups["payload"].Value;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var checksum = GetChecksumFromKey();
|
||||
}
|
||||
|
||||
private static char GetChecksumFromKey(uint key)
|
||||
{
|
||||
var index = (key & 0x000F_0000) >> 16;
|
||||
|
||||
return ChecksumTable[index];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="minimumSize">Indicates that returned buffer must be larger than `minimumSize` bytes.</param>
|
||||
/// <returns>
|
||||
/// A buffer aligned to next multiple of block size.
|
||||
/// Dispose() must be called when it's not used anymore.
|
||||
/// </returns>
|
||||
private static IMemoryOwner<byte> CreateAlignedBuffer(int minimumSize)
|
||||
{
|
||||
// align to next multiple of block size.
|
||||
var alignedSize = (minimumSize + Blowfish.BlockSize - 1) & (~(-Blowfish.BlockSize));
|
||||
|
||||
return MemoryPool<byte>.Shared.Rent(alignedSize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats a key.
|
||||
/// </summary>
|
||||
/// <param name="key">A secret key.</param>
|
||||
/// <param name="destination">A buffer where formatted key will be stored. This must be larger than 8 bytes.</param>
|
||||
private static void CreateKey(uint key, Span<byte> destination)
|
||||
{
|
||||
if (!Utf8Formatter.TryFormat(key, destination, out var _, new StandardFormat('x', 8)))
|
||||
{
|
||||
throw new InvalidOperationException("BUG: Could not create a key");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the url-safe variant of base64 string to bytes.
|
||||
/// </summary>
|
||||
/// <param name="payload">A url-safe variant of base64 string.</param>
|
||||
private static byte[] DecodeUrlSafeBase64(string payload)
|
||||
{
|
||||
var base64Str = payload
|
||||
.Replace('-', '+')
|
||||
.Replace('_', '/');
|
||||
|
||||
try
|
||||
{
|
||||
return Convert.FromBase64String(base64Str);
|
||||
}
|
||||
catch (FormatException ex)
|
||||
{
|
||||
// This is expected to happen if the argument is ill-formed
|
||||
throw new SqexArgException($"A payload {payload} does not look like a valid encrypted argument.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static string EncodeUrlSafeBase64(byte[] payload)
|
||||
{
|
||||
var payloadStr = Convert.ToBase64String(payload);
|
||||
|
||||
return payloadStr
|
||||
.Replace('+', '-')
|
||||
.Replace('/', '_');
|
||||
|
||||
}
|
||||
public override string ToString() => $"//**sqex0003{Data}{Checksum}**//";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
64
Dalamud.Bootstrap/SqexArg/EncryptedArgumentExtensions.cs
Normal file
64
Dalamud.Bootstrap/SqexArg/EncryptedArgumentExtensions.cs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Buffers.Text;
|
||||
using Dalamud.Bootstrap.Crypto;
|
||||
|
||||
namespace Dalamud.Bootstrap.SqexArg
|
||||
{
|
||||
internal static class EncryptedArgumentExtensions
|
||||
{
|
||||
public static void Decrypt(this EncryptedArgument argument, uint key)
|
||||
{
|
||||
Span<byte> keyBytes = stackalloc byte[8];
|
||||
CreateKey(key, keyBytes);
|
||||
|
||||
var blowfish = new Blowfish(keyBytes);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats a key.
|
||||
/// </summary>
|
||||
/// <param name="key">A secret key.</param>
|
||||
/// <param name="destination">A buffer where formatted key will be stored. This must be larger than 8 bytes.</param>
|
||||
private static void CreateKey(uint key, Span<byte> destination)
|
||||
{
|
||||
if (!Utf8Formatter.TryFormat(key, destination, out var _, new StandardFormat('x', 8)))
|
||||
{
|
||||
throw new InvalidOperationException("BUG: Could not create a key");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the url-safe variant of base64 string to bytes.
|
||||
/// </summary>
|
||||
/// <param name="payload">A url-safe variant of base64 string.</param>
|
||||
private static byte[] DecodeUrlSafeBase64(string payload)
|
||||
{
|
||||
var base64Str = payload
|
||||
.Replace('-', '+')
|
||||
.Replace('_', '/');
|
||||
|
||||
try
|
||||
{
|
||||
return Convert.FromBase64String(base64Str);
|
||||
}
|
||||
catch (FormatException ex)
|
||||
{
|
||||
// This is expected to happen if the argument is ill-formed
|
||||
throw new SqexArgException($"A payload {payload} does not look like a valid encrypted argument.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static string EncodeUrlSafeBase64(byte[] payload)
|
||||
{
|
||||
var payloadStr = Convert.ToBase64String(payload);
|
||||
|
||||
return payloadStr
|
||||
.Replace('+', '-')
|
||||
.Replace('/', '_');
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue