Files
MoShiro/backend/src/MoShiro.Backend.Application/Updates/ReleaseSignature.cs
T

26 lines
1.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Security.Cryptography;
using System.Text;
namespace MoShiro.Backend.Application;
/// <summary>
/// 发布清单的签名校验:用 Backend 内嵌的 CI 发布公钥(<c>packaging/update/release-key.pub.pem</c>)验 ECDSA P-256 / SHA-256(DER)签名,签名对象是清单的原始字节。
/// 登记清单因此不需要任何令牌:能被登记的只有 CI 签过的清单,节点的更新器之后还会用本机的公钥再验一遍。
/// </summary>
public static class ReleaseSignature
{
public static bool Verify(string publicKeyPem, string manifest, string signatureBase64)
{
try
{
using var key = ECDsa.Create();
key.ImportFromPem(publicKeyPem);
return key.VerifyData(Encoding.UTF8.GetBytes(manifest), Convert.FromBase64String(signatureBase64), HashAlgorithmName.SHA256, DSASignatureFormat.Rfc3279DerSequence);
}
catch (Exception ex) when (ex is FormatException or CryptographicException or ArgumentException)
{
return false;
}
}
}