2022-08-15 23:55:44 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Security.Cryptography;
|
2022-08-27 02:10:44 +08:00
|
|
|
|
using MinecraftClient.Protocol.Message;
|
2023-01-13 16:12:10 +08:00
|
|
|
|
using static MinecraftClient.Protocol.Message.LastSeenMessageList;
|
2022-08-15 23:55:44 +08:00
|
|
|
|
|
2022-12-06 15:50:17 +08:00
|
|
|
|
namespace MinecraftClient.Protocol.ProfileKey
|
2022-08-15 23:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
public class PrivateKey
|
|
|
|
|
|
{
|
|
|
|
|
|
public byte[] Key { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
private readonly RSA rsa;
|
|
|
|
|
|
|
2022-08-27 02:10:44 +08:00
|
|
|
|
private byte[]? precedingSignature = null;
|
|
|
|
|
|
|
2022-08-15 23:55:44 +08:00
|
|
|
|
public PrivateKey(string pemKey)
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
Key = KeyUtils.DecodePemKey(pemKey, "-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----");
|
2022-08-15 23:55:44 +08:00
|
|
|
|
|
2022-10-02 18:31:08 +08:00
|
|
|
|
rsa = RSA.Create();
|
|
|
|
|
|
rsa.ImportPkcs8PrivateKey(Key, out _);
|
2022-08-15 23:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] SignData(byte[] data)
|
|
|
|
|
|
{
|
|
|
|
|
|
return rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-27 02:10:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sign message - 1.19
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="message">Message content</param>
|
|
|
|
|
|
/// <param name="uuid">Sender uuid</param>
|
|
|
|
|
|
/// <param name="timestamp">Timestamp</param>
|
|
|
|
|
|
/// <param name="salt">Salt</param>
|
|
|
|
|
|
/// <returns>Signature data</returns>
|
|
|
|
|
|
public byte[] SignMessage(string message, Guid uuid, DateTimeOffset timestamp, ref byte[] salt)
|
2022-08-15 23:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
string messageJson = "{\"text\":\"" + KeyUtils.EscapeString(message) + "\"}";
|
|
|
|
|
|
|
|
|
|
|
|
byte[] data = KeyUtils.GetSignatureData(messageJson, uuid, timestamp, ref salt);
|
|
|
|
|
|
|
|
|
|
|
|
return SignData(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-27 02:10:44 +08:00
|
|
|
|
/// <summary>
|
2023-01-13 16:12:10 +08:00
|
|
|
|
/// Sign message - 1.19.1 and 1.19.2
|
2022-08-27 02:10:44 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="message">Message content</param>
|
|
|
|
|
|
/// <param name="uuid">Sender uuid</param>
|
|
|
|
|
|
/// <param name="timestamp">Timestamp</param>
|
|
|
|
|
|
/// <param name="salt">Salt</param>
|
|
|
|
|
|
/// <param name="lastSeenMessages">LastSeenMessageList</param>
|
|
|
|
|
|
/// <returns>Signature data</returns>
|
|
|
|
|
|
public byte[] SignMessage(string message, Guid uuid, DateTimeOffset timestamp, ref byte[] salt, LastSeenMessageList lastSeenMessages)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] bodySignData = KeyUtils.GetSignatureData(message, timestamp, ref salt, lastSeenMessages);
|
|
|
|
|
|
byte[] bodyDigest = KeyUtils.ComputeHash(bodySignData);
|
|
|
|
|
|
|
|
|
|
|
|
byte[] msgSignData = KeyUtils.GetSignatureData(precedingSignature, uuid, bodyDigest);
|
|
|
|
|
|
byte[] msgSign = SignData(msgSignData);
|
2022-10-02 18:31:08 +08:00
|
|
|
|
|
|
|
|
|
|
precedingSignature = msgSign;
|
2022-08-27 02:10:44 +08:00
|
|
|
|
|
|
|
|
|
|
return msgSign;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-13 16:12:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sign message - 1.19.3 and above
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="message">Message content</param>
|
|
|
|
|
|
/// <param name="uuid">Sender uuid</param>
|
|
|
|
|
|
/// <param name="timestamp">Timestamp</param>
|
|
|
|
|
|
/// <param name="salt">Salt</param>
|
|
|
|
|
|
/// <param name="lastSeenMessages">LastSeenMessageList</param>
|
|
|
|
|
|
/// <returns>Signature data</returns>
|
|
|
|
|
|
public byte[] SignMessage(string message, Guid playerUuid, Guid chatUuid, int messageIndex, DateTimeOffset timestamp, ref byte[] salt, AcknowledgedMessage[] lastSeenMessages)
|
2023-01-13 15:53:33 +08:00
|
|
|
|
{
|
2023-01-13 16:12:10 +08:00
|
|
|
|
byte[] bodySignData = KeyUtils.GetSignatureData_1_19_3(message, playerUuid, chatUuid, messageIndex, timestamp, ref salt, lastSeenMessages);
|
2023-01-14 21:07:09 +08:00
|
|
|
|
|
2023-01-13 16:12:10 +08:00
|
|
|
|
return SignData(bodySignData);
|
2023-01-13 15:53:33 +08:00
|
|
|
|
}
|
2023-01-13 16:12:10 +08:00
|
|
|
|
|
2022-08-15 23:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|