仓库里硬编码的公钥 gc-2026-11 没有对应的私钥(.signing/ 在 .gitignore 里, 私钥从来没入过库,也无法找回),导致 build.sh 每次签名都失败,version.json 始终是「未签名」。而客户端默认 RequireSignedManifest=true,未签名清单一律 拒绝更新 —— 自动更新实际上完全不可用。 改动: - keygen 生成新密钥 gc-2026-12(指纹 ee6083be47a87907),公钥写进 SignatureKeyring;旧 gc-2026-11 保留一个版本周期,标注为「私钥遗失、已停用」 - GC-SAVEDOG 的 DefaultKeyId 与 build_funcs.sh 的 GC_SIGNING_KEY_ID 默认值 同步改为 gc-2026-12,避免 keyId 与私钥不匹配又签出 Invalid - GC.Tests 构造可信清单时用的 keyId 改为 gc-2026-12(原来硬编码旧 keyId, 换钥匙后会判成 Invalid) - README 补上密钥轮换的实际步骤与「换钥匙老客户端就更新不了」的代价 - Directory.Build.props 版本 1.0.7 → 1.0.9(此前只靠 -version 传参, 仓库里一直停在 1.0.7,与实际发布脱节) - version.json / model/version.json 换成 1.0.9 且带 gc-2026-12 签名, 两份字节一致 验证: - GC.Tests --update-test 全通过,日志里是「清单验签通过(gc-2026-12)」, 篡改清单被判 Invalid 并拒绝更新 - GC-SAVEDOG verify --manifest version.json → Verified - 三平台重新出包:win-amd64 109 MB、win-arm64 105 MB、mac-arm64 57 MB 注意:1.0.8 及更早的客户端只认识旧公钥,这份清单在它们眼里是「签名无效」, 与之前的「未签名」等价 —— 都不会自动更新。要让自动更新真正生效,需要把 带新公钥的 1.0.9 发出去。
89 lines
3.5 KiB
C#
89 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace GC.Services;
|
|
|
|
/// <summary>
|
|
/// 受信任的签名公钥。
|
|
///
|
|
/// 公钥是公开的,写在源码里没有问题;**私钥永远不进仓库**
|
|
/// (放在仓库根 <c>.signing/</c>,已在 .gitignore 中)。
|
|
///
|
|
/// 轮换密钥:用 <c>GC-SAVEDOG keygen</c> 生成新的一对,把新公钥追加到这里,
|
|
/// 旧公钥保留一个版本周期,避免老客户端直接失效。
|
|
/// 也可用环境变量临时注入(测试 / 私有构建):
|
|
/// <c>GC_TRUSTED_KEYS="id1:base64;id2:base64"</c>
|
|
/// </summary>
|
|
public static class SignatureKeyring
|
|
{
|
|
public sealed record KeyEntry(string KeyId, string PublicKeyBase64, string? Note = null);
|
|
|
|
private static readonly List<KeyEntry> KeysInternal = new()
|
|
{
|
|
// 生产密钥(2026-12 换发)。指纹 ee6083be47a87907
|
|
// 上一代 gc-2026-11 的私钥没有留存,无法再签出可通过校验的清单,
|
|
// 因此新增这一把;旧公钥保留一个版本周期,方便过渡期排查。
|
|
new KeyEntry(
|
|
"gc-2026-12",
|
|
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE4R+R2Pvinsu/xdaUu5mATPyYoEpL6" +
|
|
"cyugSVKAVMz6yY15SjkgEvdosfWuQJBbrPOOgGulE6Jht6v7FZx1dQguQ==",
|
|
"GC 主发布密钥"),
|
|
|
|
// 已停用(私钥遗失):保留仅为兼容历史诊断输出,不能用于验签新清单。
|
|
new KeyEntry(
|
|
"gc-2026-11",
|
|
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEny5YKVBZUzOyRcSiuutlGmBb4+xm" +
|
|
"PrNDGshilBlpmFyWu5Atq+FGP2tn+8Or8V7OH+/7lhCmXu8jZuC5/5CWSw==",
|
|
"已停用:私钥遗失")
|
|
};
|
|
|
|
static SignatureKeyring() => LoadFromEnvironment();
|
|
|
|
public static IReadOnlyList<KeyEntry> Keys => KeysInternal;
|
|
|
|
/// <summary>是否配置了受信任密钥(配置了就必须验签)。</summary>
|
|
public static bool Enforced => KeysInternal.Count > 0;
|
|
|
|
public static KeyEntry? Find(string? keyId)
|
|
{
|
|
if (KeysInternal.Count == 0) return null;
|
|
foreach (var key in KeysInternal)
|
|
if (string.Equals(key.KeyId, keyId, StringComparison.Ordinal))
|
|
return key;
|
|
// 未写 keyId 且只有一个密钥时容忍(早期构建产物)
|
|
if (string.IsNullOrWhiteSpace(keyId) && KeysInternal.Count == 1) return KeysInternal[0];
|
|
return null;
|
|
}
|
|
|
|
/// <summary>测试 / 私有分发用:追加一个受信任公钥。</summary>
|
|
public static void Add(KeyEntry key)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key.KeyId) || string.IsNullOrWhiteSpace(key.PublicKeyBase64)) return;
|
|
if (Find(key.KeyId) != null) return;
|
|
KeysInternal.Add(key);
|
|
}
|
|
|
|
/// <summary>测试用:替换整个密钥环。</summary>
|
|
public static void ReplaceAll(IEnumerable<KeyEntry> keys)
|
|
{
|
|
KeysInternal.Clear();
|
|
foreach (var key in keys) KeysInternal.Add(key);
|
|
}
|
|
|
|
private static void LoadFromEnvironment()
|
|
{
|
|
var raw = Environment.GetEnvironmentVariable("GC_TRUSTED_KEYS");
|
|
if (string.IsNullOrWhiteSpace(raw)) return;
|
|
foreach (var part in raw!.Split(';', StringSplitOptions.RemoveEmptyEntries))
|
|
{
|
|
var idx = part.IndexOf(':');
|
|
if (idx <= 0 || idx >= part.Length - 1) continue;
|
|
var keyId = part[..idx].Trim();
|
|
var value = part[(idx + 1)..].Trim();
|
|
if (keyId.Length == 0 || value.Length == 0) continue;
|
|
if (Find(keyId) != null) continue;
|
|
KeysInternal.Add(new KeyEntry(keyId, value, "来自 GC_TRUSTED_KEYS"));
|
|
}
|
|
}
|
|
}
|