### P2-5c 圆角盒残余 seam 96→0
- 根因:平面单环耳切剔除共线中间点,与共享采样 3 段边界不一致;新增 restore_ring_boundary_points 把共享边界点逐点补回;
- 26 面圆角盒 open 96→0(Plane/Cylinder/Sphere 全 0),tris 6396,box 类保持 0;平面面与曲面相邻时 10 tris/面(纯平面 box 仍 2),
测试断言按新语义更新;
### P3-4 外部服务 OS 级沙箱
- vde-service.json limits(max_memory_mb/max_cpu_seconds/max_processes/kill_on_close);Windows Job Object(挂起创建→Assign→Resume、
KILL_ON_JOB_CLOSE、内存/CPU/进程数限制事件)+ POSIX setsid/rlimit;结果 limit_exceeded/limit_kind;
- test_plugin_external_service 18/18(内存/CPU/进程数超限、kill_on_close 回收、旧行为、非法清单);docs 更新;
### P3-5 插件 DLL 工件哈希 + 密钥轮换/吊销
- manifest artifacts[{path,sha256}]/key_id;SHA-256 自实现 + 路径白名单;Policy require_artifact_hashes/trusted_keys/revoked_key_ids;
新错误码;evaluate 扩展 + manager hook 已接线;tool verify --hash/--key-id;test_plugin_security 42/42;docs plugin-security §4.5/§4.6;
### P3-6 C++ 跨 DLL ABI 边界定稿(opt-in 实验)
- VDE_BUILD_CPP_SHARED(默认 OFF)12 内核模块 SHARED + consumer smoke(CPP_SHARED_CONSUMER_PASS faces=6 volume=6000);
- 决策:保持 C++ 静态 + 纯 C ABI + 插件默认;跨 DLL 单例/STL/RTTI/导出规模风险,实验仅 opt-in;architecture §7.8/design §13;
### P3-7 MSVC/Linux 跨平台验证收口
- .github/workflows/ci.yml 新增 windows-msvc job(YAML 校验通过);vde_platform UTF-8 宽 API/error/自守卫修复;
- 可移植性审计 12 类;docs/guides/cross-platform-validation.md;本机无 MSVC/WSL/docker,实机由 CI 反馈;
### VDE-1249 / 工程
- 反馈单更新:圆角盒 open 960→96→0,1.0.68 预算 Normal≈6396,宿主复测指引改 1.0.68;
- 版本 1.0.68;全量构建 0 error(340 targets);全量 ctest 7472 注册、7377/7377 PASS(0 failed,25 skipped,560.68s)。
349 lines
20 KiB
C++
349 lines
20 KiB
C++
#pragma once
|
||
/**
|
||
* @file plugin_security.h
|
||
* @brief 插件签名校验 + 许可策略 + 权限模型 + 工件哈希/密钥轮换(P3-1/P3-5)
|
||
*
|
||
* 设计要点(见 docs/design/plugin-security.md):
|
||
* - **分层**:本模块编译进内核库 \c vde_plugin,只依赖 foundation;
|
||
* 不直接依赖 \c vde_license(业务模块),也不内置任何加密实现。
|
||
* 真正的 Ed25519 验签由宿主注入 \ref SignatureVerifier 回调完成
|
||
* (宿主可用 vde_license::crypto::ed25519_verify、平台 KMS、HSM 等实现)。
|
||
* - **签名载荷**:\c canonical_manifest_payload() 定义稳定的规范化规则——
|
||
* 清单 JSON 中除 \c signature 外字段的确定性序列化(对象键字典序、无空白、
|
||
* UTF-8 原样、数组顺序保留、数字最短往返表示)。签名族固定为
|
||
* \c ed25519:<base64(64B)>。
|
||
* - **权限**:清单可选 \c permissions(数组,fs/net/shell 等字符串);
|
||
* 缺失 = 空(向后兼容)。默认拒绝(policy.permissions_default_deny)。
|
||
* - **兼容**:默认 policy 允许未签名插件(allow_unsigned = true);
|
||
* 严格模式由宿主显式开启(strict_plugin_security_policy())。
|
||
*
|
||
* 所有接口都是纯函数/值类型,线程安全;失败返回结构化错误码 + 原因文本。
|
||
*/
|
||
#include <cstddef>
|
||
#include <cstdint>
|
||
#include <map>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
namespace vde::plugin {
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 错误码
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// 安全校验结果码(Ok=0;失败均为明确原因,便于宿主策略与日志)
|
||
enum class PluginSecurityErrorCode {
|
||
Ok = 0,
|
||
ManifestInvalid, ///< 清单 JSON 非法/字段类型错误
|
||
SignatureMissing, ///< 要求签名但清单无 signature
|
||
SignatureMalformed, ///< 签名格式不合法(缺前缀/Base64 错误/长度错误)
|
||
SignatureAlgorithmUnsupported, ///< 签名算法非 ed25519
|
||
SignatureInvalid, ///< 验签失败(被篡改/公钥不匹配)
|
||
PublicKeyMissing, ///< 需要验签但未提供公钥
|
||
VerifierUnavailable, ///< 宿主未注入 verifier 或不支持该算法
|
||
PublisherNotAllowed, ///< 发布者不在白名单
|
||
PermissionNotDeclared, ///< 宿主要求显式声明权限但清单未声明
|
||
PermissionDenied, ///< 请求的权限未被宿主授予(默认拒绝)
|
||
|
||
// ── P3-5:DLL/工件哈希 + 密钥轮换/吊销(追加值,既有错误码数值不变)──
|
||
ArtifactMissing, ///< 清单声明 artifacts 但文件不存在/不可读
|
||
ArtifactHashMismatch, ///< artifact SHA-256 与清单声明不一致
|
||
ArtifactPathInvalid, ///< artifact 路径逃逸插件目录/绝对路径/非法
|
||
UnknownKeyId, ///< 签名 key_id 不在策略受信 key 表
|
||
RevokedKey ///< 签名 key_id 已被策略吊销
|
||
};
|
||
|
||
/// 错误码稳定名称(用于日志/CLI/测试断言,永不返回 nullptr)
|
||
[[nodiscard]] const char* plugin_security_error_name(PluginSecurityErrorCode code);
|
||
|
||
/// 是否为签名类错误(SignatureMissing..VerifierUnavailable)
|
||
[[nodiscard]] bool plugin_security_error_is_signature(PluginSecurityErrorCode code);
|
||
|
||
/// 是否为工件哈希类错误(ArtifactMissing/HashMismatch/PathInvalid)
|
||
[[nodiscard]] bool plugin_security_error_is_artifact(PluginSecurityErrorCode code);
|
||
|
||
/// 是否为密钥选择/信任类错误(UnknownKeyId/RevokedKey)
|
||
[[nodiscard]] bool plugin_security_error_is_key(PluginSecurityErrorCode code);
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 策略
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 插件安全策略(宿主配置;默认值保持 P0/P1 兼容行为)
|
||
*
|
||
* 默认:不强制签名、允许未签名(allow_unsigned=true)、不限制发布者、
|
||
* 权限默认拒绝且宿主未授予任何权限(无声明 = 空请求 → 通过)。
|
||
*/
|
||
struct PluginSecurityPolicy {
|
||
/// 是否强制要求签名(true 时无签名/空 signature → SignatureMissing)
|
||
bool require_signature = false;
|
||
/// 是否允许未签名插件(仅当 require_signature=false 时生效;兼容默认 ON)
|
||
bool allow_unsigned = true;
|
||
/// 发布者白名单(空 = 不限制;匹配清单 publisher 字段,精确匹配)
|
||
std::vector<std::string> allowed_publishers;
|
||
/// 权限默认拒绝(true = 请求权限必须在 granted_permissions 中)
|
||
bool permissions_default_deny = true;
|
||
/// 是否要求清单显式声明 permissions 字段(严格宿主可开;默认关,兼容旧清单)
|
||
bool require_permission_declaration = false;
|
||
/// 宿主授予的权限("*" 表示授予全部请求权限)
|
||
std::vector<std::string> granted_permissions;
|
||
|
||
// ── P3-5:DLL/工件哈希 + 密钥轮换/吊销 ──
|
||
|
||
/// 是否要求清单必须声明 artifacts 且全部通过哈希校验(普通默认 false 兼容;
|
||
/// strict_plugin_security_policy() 置 true)
|
||
bool require_artifact_hashes = false;
|
||
/// 多受信公钥:key_id → 32B Ed25519 公钥。非空时按清单 key_id 选择;
|
||
/// 仅 1 条且清单无 key_id 时可自动选用;空则回退到 evaluate 的 public_key 参数。
|
||
std::map<std::string, std::vector<uint8_t>> trusted_keys;
|
||
/// 已吊销 key_id(即使仍在 trusted_keys 中也拒绝)
|
||
std::vector<std::string> revoked_key_ids;
|
||
};
|
||
|
||
/// 严格策略:强制签名 + 不允许未签名 + 要求 artifacts 哈希(P3-5)
|
||
/// (发布者/受信 key/权限由调用方继续填充)
|
||
[[nodiscard]] PluginSecurityPolicy strict_plugin_security_policy();
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Base64 / 公钥工具(签名规范的一部分,公开便于工具/测试复用)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// 标准 Base64(RFC 4648,带 = 填充)
|
||
[[nodiscard]] std::string base64_encode(const uint8_t* data, size_t size);
|
||
[[nodiscard]] std::string base64_encode(const std::vector<uint8_t>& data);
|
||
/// 便捷重载:按字节编码字符串内容(常用于签名/公钥文本处理)
|
||
[[nodiscard]] std::string base64_encode(const std::string& text);
|
||
|
||
/// 解码标准 Base64(接受无填充输入;非法字符/长度返回 false 并写 error)
|
||
[[nodiscard]] bool base64_decode(const std::string& text, std::vector<uint8_t>* out,
|
||
std::string* error = nullptr);
|
||
|
||
/**
|
||
* @brief 解析公钥文本:支持 "ed25519:<base64>"、裸 base64、64 hex 字符
|
||
* @param text 公钥文本(自动 trim)
|
||
* @param out 输出 32B Ed25519 公钥
|
||
* @param error 失败原因(可空)
|
||
*/
|
||
[[nodiscard]] bool parse_public_key_text(const std::string& text,
|
||
std::vector<uint8_t>* out,
|
||
std::string* error = nullptr);
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 签名模型
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// 解析后的清单签名(算法族 + 原始签名字节)
|
||
struct PluginSignature {
|
||
bool present = false; ///< 清单是否带 signature 字段
|
||
std::string algorithm; ///< 小写算法族(当前仅 "ed25519")
|
||
std::vector<uint8_t> bytes; ///< 解码后的签名字节(ed25519 为 64B)
|
||
std::string raw; ///< 原始字符串("ed25519:<base64>")
|
||
};
|
||
|
||
/**
|
||
* @brief 解析 "ed25519:<base64>" 签名串
|
||
* @return 格式合法返回 true(含空串 → present=false, true);否则 false + error
|
||
*/
|
||
[[nodiscard]] bool parse_plugin_signature(const std::string& raw,
|
||
PluginSignature* out,
|
||
std::string* error = nullptr);
|
||
|
||
/**
|
||
* @brief 计算清单的规范化签名载荷(canonical payload)
|
||
*
|
||
* 规则(稳定、可跨语言复现):
|
||
* 1. 解析清单 JSON 对象;删除顶层 "signature" 字段(仅顶层);
|
||
* 2. 递归序列化:对象键按字典序(UTF-8 字节序)排列,数组顺序保留;
|
||
* 3. 无任何空白字符;字符串用 JSON 标准转义、UTF-8 非 ASCII 原样输出;
|
||
* 4. 数字使用最短往返十进制表示(与 nlohmann::json dump 一致);
|
||
* 5. 输出 UTF-8 字节序列(不含结尾换行)。
|
||
* 验签时对**该字节序列**做 Ed25519 校验。
|
||
*/
|
||
[[nodiscard]] bool canonical_manifest_payload(const std::string& manifest_json,
|
||
std::string* out_payload,
|
||
std::string* error = nullptr);
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 验签器注入(内核不依赖加密库 / vde_license)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// 宿主注入的签名验证器接口(实现方负责真实 Ed25519 校验)
|
||
class SignatureVerifier {
|
||
public:
|
||
virtual ~SignatureVerifier() = default;
|
||
|
||
/// 是否支持该算法族(默认仅 ed25519)
|
||
[[nodiscard]] virtual bool supports(const std::string& algorithm) const;
|
||
|
||
/// 校验 signature 是否为 public_key 对 payload 的有效签名
|
||
[[nodiscard]] virtual bool verify(const uint8_t* payload, size_t payload_size,
|
||
const uint8_t* signature, size_t signature_size,
|
||
const uint8_t* public_key, size_t public_key_size) = 0;
|
||
};
|
||
|
||
/// C 形式回调(便于插件/宿主用纯 C 接口桥接)
|
||
using SignatureVerifyCallback = bool (*)(void* user,
|
||
const uint8_t* payload, size_t payload_size,
|
||
const uint8_t* signature, size_t signature_size,
|
||
const uint8_t* public_key, size_t public_key_size);
|
||
|
||
/// 基于 C 回调的 SignatureVerifier 适配器
|
||
class CallbackSignatureVerifier final : public SignatureVerifier {
|
||
public:
|
||
CallbackSignatureVerifier(SignatureVerifyCallback callback, void* user = nullptr)
|
||
: callback_(callback), user_(user) {}
|
||
[[nodiscard]] bool verify(const uint8_t* payload, size_t payload_size,
|
||
const uint8_t* signature, size_t signature_size,
|
||
const uint8_t* public_key, size_t public_key_size) override;
|
||
|
||
private:
|
||
SignatureVerifyCallback callback_ = nullptr;
|
||
void* user_ = nullptr;
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 校验报告
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// 清单安全校验的结构化结果(Ok 时也填充签名/发布者/权限信息,供审计)
|
||
struct PluginSecurityReport {
|
||
PluginSecurityErrorCode code = PluginSecurityErrorCode::Ok;
|
||
std::string message; ///< 人类可读原因/说明
|
||
|
||
bool signature_present = false; ///< 清单带 signature
|
||
bool signature_valid = false; ///< 验签通过
|
||
std::string signature_algorithm; ///< 签名算法族(如 "ed25519")
|
||
size_t signature_size = 0; ///< 签名字节数
|
||
|
||
std::string publisher; ///< 清单 publisher 字段(可空)
|
||
bool publisher_allowed = false; ///< 白名单判定(空白名单=true)
|
||
|
||
bool permissions_declared = false; ///< 清单是否显式声明 permissions
|
||
std::vector<std::string> requested_permissions; ///< 清单声明
|
||
std::vector<std::string> granted_permissions; ///< 宿主授予
|
||
std::vector<std::string> denied_permissions; ///< 未授予(默认拒绝)
|
||
|
||
// ── P3-5:签名密钥选择 + 工件哈希明细 ──
|
||
std::string key_id; ///< 清单 key_id(可空)
|
||
bool key_trusted = false; ///< key_id 命中 policy.trusted_keys
|
||
bool key_revoked = false; ///< key_id 在 policy.revoked_key_ids
|
||
bool artifacts_declared = false; ///< 清单是否声明 artifacts
|
||
bool artifacts_required = false; ///< 策略是否要求 artifacts
|
||
std::vector<struct PluginArtifactReport> artifact_reports; ///< 工件校验明细
|
||
|
||
[[nodiscard]] bool ok() const { return code == PluginSecurityErrorCode::Ok; }
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 工件(DLL/数据文件)哈希:artifacts[{path, sha256}](P3-5)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// 清单声明的一个工件(路径为插件目录内相对路径;sha256 为小写/大写 hex)
|
||
struct PluginArtifact {
|
||
std::string path;
|
||
std::string sha256_hex;
|
||
};
|
||
|
||
/// 单个工件的校验结果(供 report/工具展示;失败时 code≠Ok)
|
||
struct PluginArtifactReport {
|
||
std::string path;
|
||
std::string expected_sha256;
|
||
std::string actual_sha256;
|
||
PluginSecurityErrorCode code = PluginSecurityErrorCode::Ok;
|
||
std::string message;
|
||
};
|
||
|
||
/// SHA-256(FIPS 180-4)hex 小写;用于 artifact 期望/实际哈希比较
|
||
[[nodiscard]] std::string sha256_hex(const uint8_t* data, size_t size);
|
||
[[nodiscard]] std::string sha256_hex(const std::string& text);
|
||
[[nodiscard]] bool sha256_file(const std::string& path, std::string* out_hex,
|
||
std::string* error = nullptr);
|
||
|
||
/// 路径安全:必须是非空相对路径、无盘符/前导分隔符、无 ".." 组件(禁止逃逸)
|
||
[[nodiscard]] bool is_artifact_path_safe(const std::string& relative_path,
|
||
std::string* error = nullptr);
|
||
|
||
/// 解析清单 artifacts 字段(缺失 → 空且 declared=false;schema 错误 → false)
|
||
[[nodiscard]] bool parse_plugin_artifacts(const std::string& manifest_json,
|
||
std::vector<PluginArtifact>* out,
|
||
std::string* error = nullptr,
|
||
bool* declared = nullptr);
|
||
|
||
/**
|
||
* @brief 校验清单声明的全部 artifacts(路径安全 + 存在性 + SHA-256)
|
||
*
|
||
* - artifacts 未声明:policy.require_artifact_hashes → ArtifactMissing;否则 Ok;
|
||
* - 已声明:逐项校验(路径逃逸 → ArtifactPathInvalid;不存在/不可读 → ArtifactMissing;
|
||
* 哈希不一致 → ArtifactHashMismatch),reports 填写明细;
|
||
* - plugin_dir 为空且存在声明 artifacts → ManifestInvalid(无法解析相对路径)。
|
||
*/
|
||
[[nodiscard]] bool verify_plugin_artifacts(const std::string& manifest_json,
|
||
const std::string& plugin_dir,
|
||
const PluginSecurityPolicy& policy,
|
||
std::vector<PluginArtifactReport>* reports,
|
||
PluginSecurityErrorCode* first_error = nullptr,
|
||
std::string* message = nullptr);
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 核心:清单安全评估 + 权限工具
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 评估清单安全状态(签名/发布者/权限),并填写结构化报告
|
||
*
|
||
* 校验顺序:清单合法性 → 签名(缺失/格式/算法/公钥/验签)→ 发布者白名单 → 权限。
|
||
* - 签名校验失败返回对应签名错误码;
|
||
* - 发布者不在白名单 → PublisherNotAllowed(仅签名通过后判定);
|
||
* - 权限:请求权限必须在 policy.granted_permissions 中("*" 授予全部),
|
||
* 否则 PermissionDenied;policy.require_permission_declaration 且清单未声明 → PermissionNotDeclared。
|
||
*
|
||
* @param manifest_json 清单 JSON 文本
|
||
* @param policy 宿主策略
|
||
* @param verifier 宿主注入的验签器(无签名路径可为空;有签名时为空 → VerifierUnavailable)
|
||
* @param public_key 验签公钥(32B Ed25519;空 → PublicKeyMissing)
|
||
* @param report 输出报告(Ok 时也包含签名/权限明细)
|
||
* @return report->ok()
|
||
*/
|
||
[[nodiscard]] bool evaluate_plugin_security(const std::string& manifest_json,
|
||
const PluginSecurityPolicy& policy,
|
||
SignatureVerifier* verifier,
|
||
const std::vector<uint8_t>& public_key,
|
||
PluginSecurityReport* report);
|
||
|
||
/**
|
||
* @brief 扩展版:额外传入插件目录以校验 artifacts(DLL/数据文件)SHA-256
|
||
*
|
||
* 与上一重载相比:支持 policy.trusted_keys(key_id→公钥)/revoked_key_ids 密钥轮换,
|
||
* 并在签名/发布者通过后按 policy.require_artifact_hashes 校验 artifacts。
|
||
* 旧重载等价于 plugin_dir 为空(不做文件哈希;若策略强制 artifacts 则返回
|
||
* ArtifactMissing/ManifestInvalid)。管理器/工具应在 dlopen 前使用本重载。
|
||
*/
|
||
[[nodiscard]] bool evaluate_plugin_security(const std::string& manifest_json,
|
||
const std::string& plugin_dir,
|
||
const PluginSecurityPolicy& policy,
|
||
SignatureVerifier* verifier,
|
||
const std::vector<uint8_t>& public_key,
|
||
PluginSecurityReport* report);
|
||
|
||
/// 解析清单 permissions 字段(缺失 → 空数组;字段存在但类型非 string 数组 → false)
|
||
[[nodiscard]] bool parse_plugin_permissions(const std::string& manifest_json,
|
||
std::vector<std::string>* out,
|
||
std::string* error = nullptr,
|
||
bool* declared = nullptr);
|
||
|
||
/// 权限是否被策略授予("*" 授予全部;空白名单=拒绝)
|
||
[[nodiscard]] bool is_permission_allowed(const PluginSecurityPolicy& policy,
|
||
const std::string& permission);
|
||
|
||
/// 计算授予列表(请求 ∩ 策略授予;"*" → 全部请求)
|
||
[[nodiscard]] std::vector<std::string> resolve_permissions(
|
||
const PluginSecurityPolicy& policy,
|
||
const std::vector<std::string>& requested);
|
||
|
||
/// 发布者是否被策略允许(空白名单=允许)
|
||
[[nodiscard]] bool is_publisher_allowed(const PluginSecurityPolicy& policy,
|
||
const std::string& publisher);
|
||
|
||
} // namespace vde::plugin
|