diff --git a/AUTHORIZATION.md b/AUTHORIZATION.md
index 35b6085..3cd3c9a 100644
--- a/AUTHORIZATION.md
+++ b/AUTHORIZATION.md
@@ -50,4 +50,4 @@ dotnet run --project tests/D3Startup.Smoke -- --license-only
dotnet run --project tests/Avalonia.Smoke -- --license-ui-only
```
-测试使用独立签名密钥及内存记录,不会消耗发行方授权或修改本机正式试用记录。授权界面截图固定写入工作区的 `scratch/screenshots/license-window.png`。
+测试使用独立签名密钥、内存记录以及专用存储测试文件和注册表项,不会消耗发行方授权或修改本机正式试用记录。加密存储测试文件固定使用 `scratch/test-results/license-store.dat`,结束后清理;授权界面截图固定写入工作区的 `scratch/screenshots/license-window.png`。
diff --git a/src/SopHelper.CrossPlatform.Engine/Licensing/ProtectedLicenseStore.cs b/src/SopHelper.CrossPlatform.Engine/Licensing/ProtectedLicenseStore.cs
index 4cea319..d40a35f 100644
--- a/src/SopHelper.CrossPlatform.Engine/Licensing/ProtectedLicenseStore.cs
+++ b/src/SopHelper.CrossPlatform.Engine/Licensing/ProtectedLicenseStore.cs
@@ -3,20 +3,30 @@ using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;
+using System.Runtime.CompilerServices;
+
+// 存储集成测试必须使用专用目录与注册表项,不能接触当前账号的真实试用记录。
+[assembly: InternalsVisibleTo("D3Startup.Smoke")]
namespace SopHelper.Engine.Licensing;
/// 授权记录独立于安装目录;Windows 使用当前账号 DPAPI 并镜像到注册表,避免重装或删单个文件重置试用。
public sealed class ProtectedLicenseStore : ILicenseStore
{
- private const string RegistryPath = @"Software\Bellwether\AISOP\Licensing";
+ private readonly string _registryPath;
private readonly string _path;
private readonly byte[] _entropy;
/// 固定用户数据目录,不受配方工作区和程序位置影响;Linux 使用主板派生密钥及仅本人可读写权限。
public ProtectedLicenseStore(string machine)
+ : this(machine, Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
+ "Bellwether", "AISOP", "license.dat"), @"Software\Bellwether\AISOP\Licensing") { }
+
+ /// 仅供友元测试注入隔离路径,生产构造函数始终固定用户授权目录。
+ internal ProtectedLicenseStore(string machine, string path, string registryPath)
{
- _path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Bellwether", "AISOP", "license.dat");
+ _path = path;
+ _registryPath = registryPath;
_entropy = SHA256.HashData(Encoding.UTF8.GetBytes("AISOP-state-v1|" + machine));
}
@@ -26,8 +36,11 @@ public sealed class ProtectedLicenseStore : ILicenseStore
byte[]? file = File.Exists(_path) ? File.ReadAllBytes(_path) : null;
if (OperatingSystem.IsWindows())
{
- using var key = Registry.CurrentUser.OpenSubKey(RegistryPath);
- var mirror = key?.GetValue("State") as byte[];
+ using var key = Registry.CurrentUser.OpenSubKey(_registryPath);
+ var mirrorValue = key?.GetValue("State");
+ if (mirrorValue is not null && mirrorValue is not byte[])
+ throw new InvalidDataException("授权注册表记录类型错误");
+ var mirror = mirrorValue as byte[];
if (file is not null && mirror is not null && !file.SequenceEqual(mirror))
throw new InvalidDataException("授权文件与注册表记录不一致,请恢复一致的备份并联系发行方。");
file ??= mirror;
@@ -49,7 +62,7 @@ public sealed class ProtectedLicenseStore : ILicenseStore
File.Move(temporary, _path, true);
if (OperatingSystem.IsWindows())
{
- using var key = Registry.CurrentUser.CreateSubKey(RegistryPath);
+ using var key = Registry.CurrentUser.CreateSubKey(_registryPath);
key.SetValue("State", encrypted, RegistryValueKind.Binary);
key.Flush();
}
diff --git a/tests/D3Startup.Smoke/LicenseTests.cs b/tests/D3Startup.Smoke/LicenseTests.cs
index 102f7fb..0a73e52 100644
--- a/tests/D3Startup.Smoke/LicenseTests.cs
+++ b/tests/D3Startup.Smoke/LicenseTests.cs
@@ -1,6 +1,7 @@
using System.Security.Cryptography;
using System.Text.Json;
using SopHelper.Engine.Licensing;
+using Microsoft.Win32;
/// 离线授权回归:使用临时签名密钥和内存状态,不触碰发行方私钥或本机正式试用记录。
internal static class LicenseTests
@@ -74,6 +75,7 @@ internal static class LicenseTests
using var publicKey = RSA.Create(); publicKey.ImportFromPem(LicenseService.ReadIssuerPublicKey());
Check(publicKey.KeySize >= 3072, "生产验签公钥已嵌入程序集");
if (OperatingSystem.IsWindows()) Check(MotherboardIdentity.ReadMachineCode().Length == 64, "本机 CIM 主板读取成功");
+ CheckProtectedStore(machine);
// 可选验收发行脚本真实输出,仍只写入内存,不激活当前电脑的正式记录。
var integrationPath = Environment.GetEnvironmentVariable("AISOP_TEST_LICENSE_FILE");
@@ -84,6 +86,47 @@ internal static class LicenseTests
}
}
+ /// 在固定测试文件与测试专用注册表项验证真实加密、镜像恢复及篡改拒绝,最终只清理这两个测试资源。
+ private static void CheckProtectedStore(string machine)
+ {
+ if (!OperatingSystem.IsWindows()) return;
+ var path = Path.GetFullPath("../scratch/test-results/license-store.dat");
+ const string registryPath = @"Software\Bellwether\AISOP\LicenseSmokeTest";
+ void Check(bool ok, string message) { if (!ok) throw new Exception(message); Console.WriteLine("PASS: " + message); }
+ bool Refused(ILicenseStore target)
+ {
+ try { target.Read(); return false; }
+ catch (Exception error) when (error is IOException or InvalidDataException or System.ComponentModel.Win32Exception) { return true; }
+ }
+ try
+ {
+ File.Delete(path); File.Delete(path + ".tmp");
+ Registry.CurrentUser.DeleteSubKey(registryPath, false);
+ var target = new ProtectedLicenseStore(machine, path, registryPath);
+ Check(target.Read() is null, "无记录时存储返回首次运行");
+ var clear = "license-state-roundtrip"u8.ToArray();
+ target.Write(clear);
+ Check(target.Read()!.SequenceEqual(clear) && !File.ReadAllBytes(path).SequenceEqual(clear), "Windows DPAPI 加密存储往返成功");
+ File.Delete(path);
+ Check(target.Read()!.SequenceEqual(clear), "删除授权文件后仍从注册表镜像恢复记录");
+ target.Write(clear);
+ using (var key = Registry.CurrentUser.OpenSubKey(registryPath, true)!) key.DeleteValue("State");
+ Check(target.Read()!.SequenceEqual(clear), "镜像缺失时仍保留文件中的原始授权");
+ Check(Refused(new ProtectedLicenseStore(LicenseService.MachineCode("different-board"), path, registryPath)), "不同主板无法解密复制的授权记录");
+ target.Write(clear);
+ File.WriteAllBytes(path, [1, 2, 3]);
+ Check(Refused(target), "授权文件与镜像不同则拒绝加载");
+ using (var key = Registry.CurrentUser.OpenSubKey(registryPath, true)!) key.SetValue("State", "invalid", RegistryValueKind.String);
+ File.Delete(path);
+ Check(Refused(target), "损坏的镜像类型不能被当作首次试用");
+ }
+ finally
+ {
+ File.Delete(path); File.Delete(path + ".tmp");
+ Registry.CurrentUser.DeleteSubKey(registryPath, false);
+ }
+ }
+
private sealed class MemoryStore : ILicenseStore
{
public byte[]? Data;