mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-11-07 17:36:07 +00:00
Basic support for minecraft 1.19 (#2084)
* merge commit from milutinke * chat signature & encrypted login * Bug fix :EncryptionResponse format error below 1.18.2 * Implemented chat command signature * Chat message parsing and verification for 1.19 * Add signature settings * Update Simplified Chinese Translation * Clear up comments * Fix wrong variable naming * Bug fix: SignatureV2 Processing
This commit is contained in:
parent
d9f1a77ac2
commit
a8bbb1ac76
55 changed files with 5218 additions and 1174 deletions
137
MinecraftClient/Protocol/ProfileKey/KeyUtils.cs
Normal file
137
MinecraftClient/Protocol/ProfileKey/KeyUtils.cs
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MinecraftClient.Protocol.Keys
|
||||
{
|
||||
static class KeyUtils
|
||||
{
|
||||
private static string certificates = "https://api.minecraftservices.com/player/certificates";
|
||||
|
||||
public static PlayerKeyPair? GetKeys(string accessToken)
|
||||
{
|
||||
ProxiedWebRequest.Response? response = null;
|
||||
try
|
||||
{
|
||||
var request = new ProxiedWebRequest(certificates)
|
||||
{
|
||||
Accept = "application/json"
|
||||
};
|
||||
request.Headers.Add("Authorization", string.Format("Bearer {0}", accessToken));
|
||||
|
||||
response = request.Post("application/json", "");
|
||||
|
||||
if (Settings.DebugMessages)
|
||||
{
|
||||
ConsoleIO.WriteLine(response.Body.ToString());
|
||||
}
|
||||
|
||||
string jsonString = response.Body;
|
||||
Json.JSONData json = Json.ParseJson(jsonString);
|
||||
|
||||
PublicKey publicKey = new(pemKey: json.Properties["keyPair"].Properties["publicKey"].StringValue,
|
||||
sig: json.Properties["publicKeySignature"].StringValue,
|
||||
sigV2: json.Properties["publicKeySignatureV2"].StringValue);
|
||||
|
||||
PrivateKey privateKey = new(pemKey: json.Properties["keyPair"].Properties["privateKey"].StringValue);
|
||||
|
||||
return new PlayerKeyPair(publicKey, privateKey,
|
||||
expiresAt: json.Properties["expiresAt"].StringValue,
|
||||
refreshedAfter: json.Properties["refreshedAfter"].StringValue);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
int code = (response == null) ? 0 : response.StatusCode;
|
||||
ConsoleIO.WriteLineFormatted("§cFetch profile key failed: HttpCode = " + code + ", Error = " + e.Message);
|
||||
if (Settings.DebugMessages)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§c" + e.StackTrace);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] DecodePemKey(String key, String prefix, String suffix)
|
||||
{
|
||||
int i = key.IndexOf(prefix);
|
||||
if (i != -1)
|
||||
{
|
||||
i += prefix.Length;
|
||||
int j = key.IndexOf(suffix, i);
|
||||
key = key[i..j];
|
||||
}
|
||||
key = key.Replace("\r", String.Empty);
|
||||
key = key.Replace("\n", String.Empty);
|
||||
return Convert.FromBase64String(key);
|
||||
}
|
||||
|
||||
public static byte[] GetSignatureData(string message, string uuid, DateTimeOffset timestamp, ref byte[] salt)
|
||||
{
|
||||
List<byte> data = new();
|
||||
|
||||
data.AddRange(salt);
|
||||
|
||||
byte[] UUIDLeastSignificantBits = BitConverter.GetBytes(Convert.ToInt64(uuid[..16], 16));
|
||||
Array.Reverse(UUIDLeastSignificantBits);
|
||||
data.AddRange(UUIDLeastSignificantBits);
|
||||
|
||||
byte[] UUIDMostSignificantBits = BitConverter.GetBytes(Convert.ToInt64(uuid.Substring(16, 16), 16));
|
||||
Array.Reverse(UUIDMostSignificantBits);
|
||||
data.AddRange(UUIDMostSignificantBits);
|
||||
|
||||
byte[] timestampByte = BitConverter.GetBytes(timestamp.ToUnixTimeSeconds());
|
||||
Array.Reverse(timestampByte);
|
||||
data.AddRange(timestampByte);
|
||||
|
||||
data.AddRange(Encoding.UTF8.GetBytes(message));
|
||||
|
||||
return data.ToArray();
|
||||
}
|
||||
|
||||
// https://github.com/mono/mono/blob/master/mcs/class/System.Json/System.Json/JsonValue.cs
|
||||
public static string EscapeString(string src)
|
||||
{
|
||||
StringBuilder sb = new();
|
||||
|
||||
int start = 0;
|
||||
for (int i = 0; i < src.Length; i++)
|
||||
{
|
||||
char c = src[i];
|
||||
bool needEscape = c < 32 || c == '"' || c == '\\';
|
||||
// Broken lead surrogate
|
||||
needEscape = needEscape || (c >= '\uD800' && c <= '\uDBFF' &&
|
||||
(i == src.Length - 1 || src[i + 1] < '\uDC00' || src[i + 1] > '\uDFFF'));
|
||||
// Broken tail surrogate
|
||||
needEscape = needEscape || (c >= '\uDC00' && c <= '\uDFFF' &&
|
||||
(i == 0 || src[i - 1] < '\uD800' || src[i - 1] > '\uDBFF'));
|
||||
// To produce valid JavaScript
|
||||
needEscape = needEscape || c == '\u2028' || c == '\u2029';
|
||||
|
||||
if (needEscape)
|
||||
{
|
||||
sb.Append(src, start, i - start);
|
||||
switch (src[i])
|
||||
{
|
||||
case '\b': sb.Append("\\b"); break;
|
||||
case '\f': sb.Append("\\f"); break;
|
||||
case '\n': sb.Append("\\n"); break;
|
||||
case '\r': sb.Append("\\r"); break;
|
||||
case '\t': sb.Append("\\t"); break;
|
||||
case '\"': sb.Append("\\\""); break;
|
||||
case '\\': sb.Append("\\\\"); break;
|
||||
default:
|
||||
sb.Append("\\u");
|
||||
sb.Append(((int)src[i]).ToString("x04"));
|
||||
break;
|
||||
}
|
||||
start = i + 1;
|
||||
}
|
||||
|
||||
}
|
||||
sb.Append(src, start, src.Length - start);
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
196
MinecraftClient/Protocol/ProfileKey/KeysCache.cs
Normal file
196
MinecraftClient/Protocol/ProfileKey/KeysCache.cs
Normal file
|
|
@ -0,0 +1,196 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Timers;
|
||||
using MinecraftClient.Protocol.Session;
|
||||
|
||||
namespace MinecraftClient.Protocol.Keys
|
||||
{
|
||||
/// <summary>
|
||||
/// Handle keys caching and storage.
|
||||
/// </summary>
|
||||
public static class KeysCache
|
||||
{
|
||||
private const string KeysCacheFilePlaintext = "ProfileKeyCache.ini";
|
||||
|
||||
private static FileMonitor cachemonitor;
|
||||
private static Dictionary<string, PlayerKeyPair> keys = new Dictionary<string, PlayerKeyPair>();
|
||||
private static Timer updatetimer = new Timer(100);
|
||||
private static List<KeyValuePair<string, PlayerKeyPair>> pendingadds = new List<KeyValuePair<string, PlayerKeyPair>>();
|
||||
private static BinaryFormatter formatter = new BinaryFormatter();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve whether KeysCache contains a keys for the given login.
|
||||
/// </summary>
|
||||
/// <param name="login">User login used with Minecraft.net</param>
|
||||
/// <returns>TRUE if keys are available</returns>
|
||||
public static bool Contains(string login)
|
||||
{
|
||||
return keys.ContainsKey(login);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Store keys and save it to disk if required.
|
||||
/// </summary>
|
||||
/// <param name="login">User login used with Minecraft.net</param>
|
||||
/// <param name="playerKeyPair">User keys</param>
|
||||
public static void Store(string login, PlayerKeyPair playerKeyPair)
|
||||
{
|
||||
if (Contains(login))
|
||||
{
|
||||
keys[login] = playerKeyPair;
|
||||
}
|
||||
else
|
||||
{
|
||||
keys.Add(login, playerKeyPair);
|
||||
}
|
||||
|
||||
if (Settings.ProfileKeyCaching == CacheType.Disk && updatetimer.Enabled == true)
|
||||
{
|
||||
pendingadds.Add(new KeyValuePair<string, PlayerKeyPair>(login, playerKeyPair));
|
||||
}
|
||||
else if (Settings.ProfileKeyCaching == CacheType.Disk)
|
||||
{
|
||||
SaveToDisk();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve keys for the given login.
|
||||
/// </summary>
|
||||
/// <param name="login">User login used with Minecraft.net</param>
|
||||
/// <returns>PlayerKeyPair for given login</returns>
|
||||
public static PlayerKeyPair Get(string login)
|
||||
{
|
||||
return keys[login];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize cache monitoring to keep cache updated with external changes.
|
||||
/// </summary>
|
||||
/// <returns>TRUE if keys are seeded from file</returns>
|
||||
public static bool InitializeDiskCache()
|
||||
{
|
||||
cachemonitor = new FileMonitor(AppDomain.CurrentDomain.BaseDirectory, KeysCacheFilePlaintext, new FileSystemEventHandler(OnChanged));
|
||||
updatetimer.Elapsed += HandlePending;
|
||||
return LoadFromDisk();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reloads cache on external cache file change.
|
||||
/// </summary>
|
||||
/// <param name="sender">Sender</param>
|
||||
/// <param name="e">Event data</param>
|
||||
private static void OnChanged(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
updatetimer.Stop();
|
||||
updatetimer.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called after timer elapsed. Reads disk cache and adds new/modified keys back.
|
||||
/// </summary>
|
||||
/// <param name="sender">Sender</param>
|
||||
/// <param name="e">Event data</param>
|
||||
private static void HandlePending(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
updatetimer.Stop();
|
||||
LoadFromDisk();
|
||||
|
||||
foreach (KeyValuePair<string, PlayerKeyPair> pending in pendingadds.ToArray())
|
||||
{
|
||||
Store(pending.Key, pending.Value);
|
||||
pendingadds.Remove(pending);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads cache file and loads KeysInfos into KeysCache.
|
||||
/// </summary>
|
||||
/// <returns>True if data is successfully loaded</returns>
|
||||
private static bool LoadFromDisk()
|
||||
{
|
||||
//User-editable keys cache file in text format
|
||||
if (File.Exists(KeysCacheFilePlaintext))
|
||||
{
|
||||
if (Settings.DebugMessages)
|
||||
ConsoleIO.WriteLineFormatted(Translations.Get("cache.loading_keys", KeysCacheFilePlaintext));
|
||||
|
||||
try
|
||||
{
|
||||
foreach (string line in FileMonitor.ReadAllLinesWithRetries(KeysCacheFilePlaintext))
|
||||
{
|
||||
if (!line.Trim().StartsWith("#"))
|
||||
{
|
||||
|
||||
int separatorIdx = line.IndexOf('=');
|
||||
if (separatorIdx >= 1 && line.Length > separatorIdx + 1)
|
||||
{
|
||||
string login = line.Substring(0, separatorIdx);
|
||||
string value = line.Substring(separatorIdx + 1);
|
||||
try
|
||||
{
|
||||
PlayerKeyPair playerKeyPair = PlayerKeyPair.FromString(value);
|
||||
keys[login] = playerKeyPair;
|
||||
if (Settings.DebugMessages)
|
||||
ConsoleIO.WriteLineFormatted(Translations.Get("cache.loaded_keys", playerKeyPair.ExpiresAt.ToString()));
|
||||
}
|
||||
catch (InvalidDataException e)
|
||||
{
|
||||
if (Settings.DebugMessages)
|
||||
ConsoleIO.WriteLineFormatted(Translations.Get("cache.ignore_string_keys", value, e.Message));
|
||||
}
|
||||
catch (FormatException e)
|
||||
{
|
||||
if (Settings.DebugMessages)
|
||||
ConsoleIO.WriteLineFormatted(Translations.Get("cache.ignore_string_keys", value, e.Message));
|
||||
}
|
||||
catch (ArgumentNullException e)
|
||||
{
|
||||
if (Settings.DebugMessages)
|
||||
ConsoleIO.WriteLineFormatted(Translations.Get("cache.ignore_string_keys", value, e.Message));
|
||||
|
||||
}
|
||||
}
|
||||
else if (Settings.DebugMessages)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted(Translations.Get("cache.ignore_line_keys", line));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted(Translations.Get("cache.read_fail_plain_keys", e.Message));
|
||||
}
|
||||
}
|
||||
|
||||
return keys.Count > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves player's keypair from KeysCache into cache file.
|
||||
/// </summary>
|
||||
private static void SaveToDisk()
|
||||
{
|
||||
if (Settings.DebugMessages)
|
||||
Translations.WriteLineFormatted("cache.saving_keys");
|
||||
|
||||
List<string> KeysCacheLines = new List<string>();
|
||||
KeysCacheLines.Add("# Generated by MCC v" + Program.Version + " - Keep it secret & Edit at own risk!");
|
||||
KeysCacheLines.Add("# ProfileKey=PublicKey(base64),PublicKeySignature(base64),PublicKeySignatureV2(base64),PrivateKey(base64),ExpiresAt,RefreshAfter");
|
||||
foreach (KeyValuePair<string, PlayerKeyPair> entry in keys)
|
||||
KeysCacheLines.Add(entry.Key + '=' + entry.Value.ToString());
|
||||
|
||||
try
|
||||
{
|
||||
FileMonitor.WriteAllLinesWithRetries(KeysCacheFilePlaintext, KeysCacheLines);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted(Translations.Get("cache.save_fail_keys", e.Message));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
79
MinecraftClient/Protocol/ProfileKey/PlayerKeyPair.cs
Normal file
79
MinecraftClient/Protocol/ProfileKey/PlayerKeyPair.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace MinecraftClient.Protocol.Keys
|
||||
{
|
||||
public class PlayerKeyPair
|
||||
{
|
||||
public PublicKey PublicKey;
|
||||
|
||||
public PrivateKey PrivateKey;
|
||||
|
||||
public DateTime ExpiresAt;
|
||||
|
||||
public DateTime RefreshedAfter; // Todo: add a timer
|
||||
|
||||
private const string DataTimeFormat = "O";
|
||||
|
||||
public PlayerKeyPair(PublicKey keyPublic, PrivateKey keyPrivate, string expiresAt, string refreshedAfter)
|
||||
{
|
||||
PublicKey = keyPublic;
|
||||
PrivateKey = keyPrivate;
|
||||
ExpiresAt = DateTime.Parse(expiresAt).ToUniversalTime();
|
||||
RefreshedAfter = DateTime.Parse(refreshedAfter).ToUniversalTime();
|
||||
}
|
||||
|
||||
public bool NeedRefresh()
|
||||
{
|
||||
return DateTime.Now.ToUniversalTime() > this.RefreshedAfter;
|
||||
}
|
||||
|
||||
public bool IsExpired()
|
||||
{
|
||||
return DateTime.Now.ToUniversalTime() > this.ExpiresAt;
|
||||
}
|
||||
|
||||
public long GetExpirationMilliseconds()
|
||||
{
|
||||
DateTimeOffset timeOffset = new(ExpiresAt);
|
||||
return timeOffset.ToUnixTimeMilliseconds();
|
||||
}
|
||||
|
||||
public long GetExpirationSeconds()
|
||||
{
|
||||
DateTimeOffset timeOffset = new(ExpiresAt);
|
||||
return timeOffset.ToUnixTimeSeconds();
|
||||
}
|
||||
|
||||
public static PlayerKeyPair FromString(string tokenString)
|
||||
{
|
||||
string[] fields = tokenString.Split(',');
|
||||
|
||||
if (fields.Length < 6)
|
||||
throw new InvalidDataException("Invalid string format");
|
||||
|
||||
PublicKey publicKey = new PublicKey(pemKey: fields[0].Trim(),
|
||||
sig: fields[1].Trim(), sigV2: fields[2].Trim());
|
||||
|
||||
PrivateKey privateKey = new PrivateKey(pemKey: fields[3].Trim());
|
||||
|
||||
return new PlayerKeyPair(publicKey, privateKey, fields[4].Trim(), fields[5].Trim());
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
List<string> datas = new List<string>();
|
||||
datas.Add(Convert.ToBase64String(PublicKey.Key));
|
||||
datas.Add(Convert.ToBase64String(PublicKey.Signature));
|
||||
if (PublicKey.SignatureV2 == null)
|
||||
datas.Add(String.Empty);
|
||||
else
|
||||
datas.Add(Convert.ToBase64String(PublicKey.SignatureV2));
|
||||
datas.Add(Convert.ToBase64String(PrivateKey.Key));
|
||||
datas.Add(ExpiresAt.ToString(DataTimeFormat));
|
||||
datas.Add(RefreshedAfter.ToString(DataTimeFormat));
|
||||
return String.Join(",", datas.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
39
MinecraftClient/Protocol/ProfileKey/PrivateKey.cs
Normal file
39
MinecraftClient/Protocol/ProfileKey/PrivateKey.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MinecraftClient.Protocol.Keys
|
||||
{
|
||||
public class PrivateKey
|
||||
{
|
||||
public byte[] Key { get; set; }
|
||||
|
||||
private readonly RSA rsa;
|
||||
|
||||
public PrivateKey(string pemKey)
|
||||
{
|
||||
this.Key = KeyUtils.DecodePemKey(pemKey, "-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----");
|
||||
|
||||
this.rsa = RSA.Create();
|
||||
rsa.ImportPkcs8PrivateKey(this.Key, out _);
|
||||
}
|
||||
|
||||
public byte[] SignData(byte[] data)
|
||||
{
|
||||
return rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
}
|
||||
|
||||
public byte[] SignMessage(string message, string uuid, DateTimeOffset timestamp, ref byte[] salt)
|
||||
{
|
||||
string messageJson = "{\"text\":\"" + KeyUtils.EscapeString(message) + "\"}";
|
||||
|
||||
byte[] data = KeyUtils.GetSignatureData(messageJson, uuid, timestamp, ref salt);
|
||||
|
||||
return SignData(data);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
54
MinecraftClient/Protocol/ProfileKey/PublicKey.cs
Normal file
54
MinecraftClient/Protocol/ProfileKey/PublicKey.cs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MinecraftClient.Protocol.Keys
|
||||
{
|
||||
public class PublicKey
|
||||
{
|
||||
public byte[] Key { get; set; }
|
||||
public byte[] Signature { get; set; }
|
||||
public byte[]? SignatureV2 { get; set; }
|
||||
|
||||
private readonly RSA rsa;
|
||||
|
||||
public PublicKey(string pemKey, string sig, string? sigV2 = null)
|
||||
{
|
||||
this.Key = KeyUtils.DecodePemKey(pemKey, "-----BEGIN RSA PUBLIC KEY-----", "-----END RSA PUBLIC KEY-----");
|
||||
|
||||
this.rsa = RSA.Create();
|
||||
rsa.ImportSubjectPublicKeyInfo(this.Key, out _);
|
||||
|
||||
this.Signature = Convert.FromBase64String(sig);
|
||||
|
||||
if (!string.IsNullOrEmpty(sigV2))
|
||||
this.SignatureV2 = Convert.FromBase64String(sigV2!);
|
||||
}
|
||||
|
||||
public PublicKey(byte[] key, byte[] signature)
|
||||
{
|
||||
this.Key = key;
|
||||
|
||||
this.rsa = RSA.Create();
|
||||
rsa.ImportSubjectPublicKeyInfo(this.Key, out _);
|
||||
|
||||
this.Signature = signature;
|
||||
}
|
||||
|
||||
public bool VerifyData(byte[] data, byte[] signature)
|
||||
{
|
||||
return rsa.VerifyData(data, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
}
|
||||
|
||||
public bool VerifyMessage(string message, string uuid, DateTimeOffset timestamp, ref byte[] salt, ref byte[] signature)
|
||||
{
|
||||
byte[] data = KeyUtils.GetSignatureData(message, uuid, timestamp, ref salt);
|
||||
|
||||
return VerifyData(data, signature);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue