WAMR SGX 远程证明证据的非 SGX 平台验证:基于 C 与 librats 的完整实践指南
WAMR SGX 远程证明证据的非 SGX 平台验证:基于 C# 与 librats 的完整实践指南
WAMR(WebAssembly Micro Runtime)支持在 Intel SGX 平台上借助 librats 生成远程证明(Remote Attestation)证据(evidence),但证据的验证并不强制要求验证方也持有 SGX 硬件。本文基于仓库中的 samples/sgx-ra/non-sgx-verify 示例,完整讲解如何在无 SGX 能力、甚至非 Intel 处理器的可信平台上,使用 C# 托管程序与 librats 原生库校验 WAMR 生成的 SGX ECDSA 证据,并给出环境搭建、代码逐段拆解与 claims 校验要点,帮助你在云端可信机或自建可信服务中落地跨架构的证明验证链路。
场景与原理:为什么要在非 SGX 平台验证证据
WAMR 的 SGX 远程证明示例(samples/sgx-ra)演示了如何在 SGX 支持的处理器上使用 librats 收集证据,并让 iwasm 在 enclave 内执行 Wasm 应用完成证明流程。该示例只能构建于支持 SGX 的处理器之上。
但一个典型的生产场景是:在 SGX 平台(如提供机密计算的云环境)生成证据,在另一台不具备 SGX 能力、甚至 CPU 架构不同的可信机器上完成验证。例如,部署在可信机房中的 Web 服务作为验证方(verifier),接收来自 SGX 平台应用上报的证据并判定其可信性。这正是 samples/sgx-ra/non-sgx-verify 示例要解决的问题:WAMR 生成的 Intel SGX 证据可以在没有 Intel SGX、也没有 Intel 处理器的可信平台上被校验。
从源码结构看,验证能力的核心来自 librats:WAMR 通过 lib_rats_wrapper.c 将 librats_collect、librats_verify、librats_parse_evidence、librats_dispose_evidence_json 四个函数以 native 函数形式暴露给 Wasm 应用(见 native_symbols_lib_rats 注册表)。这意味着验证逻辑并不绑定 SGX 硬件,非 SGX 平台可以直接调用 librats 的原生 API 完成同样的校验,从而摆脱对 WebAssembly 执行环境(WAMR)的依赖。
环境准备:非 SGX 平台需要安装的组件
依据 csharp 示例的前置条件说明,验证方机器需要:
- dotnet-sdk(8.0 及以上):用于编译运行 C# 验证程序;
- librats:提供
librats_verify_evidence_from_json等原生验证接口; - Intel 证据验证基础设施:即 SGX DCAP 相关库与 Intel PCCS(Provisioning Certification Caching Service),用于下载和缓存引证材料(collateral)。
该示例已在 Linux Ubuntu 20.04+ 上测试通过,其他 Linux 发行版通常也适用;只要 librats 能在目标操作系统上编译,示例同样可以移植到其他 OS。
安装 SGX DCAP 验证库
验证方虽然不需要 SGX 硬件,但仍需安装一小部分 SGX 库以支持 quote 校验(步骤源自 samples/sgx-ra/README.md 的 "Validate quotes on non-SGX platforms" 章节):
$ OS_CODE_NAME=`lsb_release -sc`
# 安装 SGX DCAP Library
$ echo "deb [arch=amd64] https://download.01.org/intel-sgx/sgx_repo/ubuntu $OS_CODE_NAME main" | sudo tee /etc/apt/sources.list.d/intel-sgx.list
$ wget -O - https://download.01.org/intel-sgx/sgx_repo/ubuntu/intel-sgx-deb.key | sudo apt-key add
$ sudo apt-get update
$ sudo apt-get install -y libsgx-quote-ex libsgx-dcap-ql libsgx-dcap-quote-verify libsgx-dcap-default-qpl
关键包的作用:
libsgx-quote-ex:quote 生成扩展库,验证场景下用于解析 quote 结构;libsgx-dcap-ql:DCAP quote 库;libsgx-dcap-quote-verify:quote 验证库,核心校验组件;libsgx-dcap-default-qpl:默认的引证材料提供者(Quote Provider Library),负责从 PCCS 拉取 collateral。
搭建 Intel PCCS 引证材料缓存服务
验证 SGX 证据需要 enclave 的引证材料(attestation collateral,包括 PCK 证书链、QE 身份等)。为避免验证过程中反复直连 Intel PCS,需部署 Intel PCCS 作为缓存。设置步骤与 SGX 平台侧一致(见 父文档 PCCS 章节):
# 安装 Node.js
$ sudo apt install -y curl cracklib-runtime
$ curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - && sudo apt-get install -y nodejs
# 安装 PCCS 软件
$ sudo apt-get install -y sgx-dcap-pccs
安装过程的配置问答要点:
- 提示
Do you want to configure PCCS now? (Y/N)时回答 Y; - HTTPS 监听端口接受默认值 8081;
- 提示
Set the PCCS service to accept local connections only? [Y] (Y/N)时回答 N,因为验证服务需要接受其他系统的连接; - 输入从 Intel PCS 订阅获得的 primary/secondary API key;
- 缓存填充方式(
Choose caching fill method)选择 REQ(按需模式),即收到验证请求时才拉取对应 collateral; - 设置 PCCS 管理员密码与用户密码;
- 提示生成不安全 HTTPS 证书时回答 Y(测试环境自签名证书)。
配置运行时并灌入全部 collateral
验证机还需修改运行时配置文件 /etc/sgx_default_qcnl.conf:将 PCCS_URL 指向本机 PCCS 服务地址,并将 USE_SECURE_CERT 设为 FALSE(自签名证书场景)。
与 SGX 平台只需注册本机 collateral 不同,非 SGX 验证方需要为所有可能上报证据的 SGX CPU 预先拉取并缓存 collateral。使用 Intel PCCS 管理工具完成(父文档操作步骤):
# 设置 Intel PCCS 管理工具
$ git clone https://github.com/intel/SGXDataCenterAttestationPrimitives.git
$ cd SGXDataCenterAttestationPrimitives/tools/PccsAdminTool
$ sudo apt-get install -y python3 python3-pip
$ pip3 install -r requirements.txt
# 1. 从 PCCS 服务获取注册数据
./pccsadmin.py get
# 2. 依据注册数据从 Intel PCS 拉取平台 collateral
./pccsadmin.py fetch
# 3. 将 collateral 或策略文件写入 PCCS 缓存数据库
./pccsadmin.py put
# 4. 请求 PCCS 刷新证书或缓存
./pccsadmin.py refresh
第一步:在 SGX 平台上生成 JSON 格式证据
验证方需要一份由 SGX 平台生成的合法 JSON 证据。WAMR 侧的生成逻辑可参考 wasm-app/main.c:Wasm 应用调用 librats_collect(&evidence_json, buffer) 收集证据,随后用 librats_parse_evidence 解析出 quote、user data、MRENCLAVE、MRSIGNER、Product ID、Security Version 等字段并打印,最终调用 librats_verify 在 enclave 内完成自校验。
值得注意的细节是 user_data 的构造约定。从 lib_rats_wrapper.c 的 librats_collect_wrapper 可以看到,证据中的用户数据并非任意字节,而是:
user_data = SHA256( SHA256(wasm_module) || buffer )
即先对 Wasm 模块二进制求 SHA256,再与该次会话提供的 nonce 字符串拼接后整体求 SHA256。验证方必须按同样规则重算 user_data,才能确认证据确实由该 Wasm 模块在当前会话中生成。这也解释了为什么 C# 示例需要读取 wasm 文件路径并复现该哈希流程。
第二步:在非 SGX 平台用 C# 验证证据
示例主体位于 samples/sgx-ra/non-sgx-verify/csharp,是一个 .NET 8.0 控制台项目(VerifyEvidence.csproj 中 TargetFramework 为 net8.0)。使用流程如下:
- 在 Program.cs 中填入你认为可信的参考值(MRENCLAVE、MRSIGNER、Security Version、Product ID);
- 在 SGX 平台用 WAMR 生成一份合法的 JSON 证据;
- 将 JSON 证据填入
Program.cs的evidenceAsString; - 在示例目录执行
dotnet run。
参考值与证据 JSON 的填充
byte[] mrEnclaveReference = { 0xDA, 0xE0, 0xDA, 0x2F, /* ... 32 字节 ... */ };
byte[] mrSignerReference = { 0x83, 0xD7, 0x19, 0xE7, /* ... 32 字节 ... */ };
const ushort securityVersionReference = 0;
const ushort productIdReference = 0;
string nonce = "This is a sample.\0"; // 注意结尾的 \0,C 字符串以此终止,必填
string evidenceAsString = """{"type":"sgx_ecdsa","report_base64":"[..]","report_len":[..]}""";
string wasmFilePath = "../build/wasm-app/test.wasm";
其中 evidenceAsString 的 JSON 结构由 librats 定义(见 Program.cs 中的 EvidenceJson 类):type 为证据类型(SGX 为 sgx_ecdsa)、report_base64 为 quote 的 Base64 编码、report_len 为 quote 长度。解析时使用 JsonNamingPolicy.SnakeCaseLower 以匹配 snake_case 字段命名。
复算 user_data 并调用 librats 原生验证
byte[] wasmFileContent = await File.ReadAllBytesAsync(wasmFilePath);
byte[] nonceAsBytes = Encoding.UTF8.GetBytes(nonce);
byte[] computedUserData = await ComputeUserData(wasmFileContent, nonceAsBytes);
byte[] evidenceAsBytes = Convert.FromBase64String(evidenceAsJson.ReportBase64);
Evidence evidence = new(evidenceAsBytes);
int libRatsReturnValue = LibRats.VerifyEvidenceFromJson(evidenceAsString, computedUserData);
ComputeUserData 完整复刻 WAMR 侧的 user_data 构造约定(Program.cs 静态方法):
static async ValueTask<byte[]> ComputeUserData(byte[] wasmFileContent, byte[] nonce)
{
using var sha256 = SHA256.Create();
var wasmFileContentHash = sha256.ComputeHash(wasmFileContent);
using MemoryStream stream = new();
await stream.WriteAsync(wasmFileContentHash);
await stream.WriteAsync(nonce);
stream.Position = 0;
byte[] computedUserData = await sha256.ComputeHashAsync(stream);
return computedUserData;
}
librats 原生接口通过 P/Invoke 引入(Program.cs 中的 LibRats 类),对应原生签名 int librats_verify_evidence_from_json(const char *json_string, const uint8_t *hash):
[DllImport("/usr/local/lib/librats/librats_lib.so", EntryPoint = "librats_verify_evidence_from_json")]
public static extern int VerifyEvidenceFromJson(string json, byte[] hash);
直接从 quote 解析 claims
为了做 claims 比对,示例在托管侧直接解析 evidence 字节流。由于 _report_body_t 结构起始于偏移 0x30(对应 SGX REPORT 结构中 REPORTDATA 之后的位置),各字段可通过固定偏移取出(Program.cs 中的 Evidence 类):
| 字段 | 偏移(hex) | 长度 |
|---|---|---|
| MRENCLAVE | 0x70..0x90 |
32 字节 |
| MRSIGNER | 0xB0..0xD0 |
32 字节 |
| Product ID | 0x130 |
2 字节(ushort) |
| Security Version | 0x132 |
2 字节(ushort) |
| UserData(ReportData) | 0x170..0x190 |
32 字节 |
程序随后逐一比对并打印结果(Program.cs 输出段):
Console.WriteLine($"User data, evidence: {BitConverter.ToString(evidence.UserData)}");
Console.WriteLine($"User Data, computed: {BitConverter.ToString(computedUserData)}");
Console.WriteLine($"Do the two user data match? {evidence.UserData.SequenceEqual(computedUserData)}");
Console.WriteLine($"MrEnclave: {BitConverter.ToString(evidence.MrEnclave)}");
Console.WriteLine($"Do the MrEnclave match? {mrEnclaveReference.SequenceEqual(evidence.MrEnclave)}");
Console.WriteLine($"MrSigner: {BitConverter.ToString(evidence.MrSigner)}");
Console.WriteLine($"Do the MrSigner match? {mrSignerReference.SequenceEqual(evidence.MrSigner)}");
Console.WriteLine($"Security Version: {evidence.SecurityVersion}, expected: {securityVersionReference}");
Console.WriteLine($"Product ID: {evidence.ProductId}, expected: {productIdReference}");
Console.WriteLine($"VerifyJsonUsingLibrats returned: {libRatsReturnValue:X}");
Claims 验证:签名校验之外的必查项
librats_verify_evidence_from_json 完成了 quote 的签名与证书链校验,但真正的信任判定还需要验证方核对 quote 内嵌的 claims 是否与预期一致。父文档的 "Claims validation" 章节 给出了必须检查的清单:
- Enclave Identity Checking:核对 MRENCLAVE(enclave 身份哈希)或 MRSIGNER 与 Product ID(软件提供方身份),确认运行的是预期 enclave 或预期厂商签名的 enclave;
- Verify Attributes:生产环境的 enclave 不应将
Debug标志置为 1。对应的标志位定义可参考 lib_rats_common.h 中的 SGX_FLAGS_DEBUG; - Verify SSA Frame extended feature set:检查 SSA 帧扩展特性集;
- Verify the ISV_SVN level:enclave 发生安全更新后 ISV_SVN 会递增,验证方应要求 SVN 不低于最低安全水位;
- Verify that the ReportData contains the expected value:ReportData 可携带 enclave 特有数据,或一段更大数据块的哈希;由于 quote 签名已保证 REPORT body(含 ReportData)的完整性,验证 ReportData 等于预期值即可证明该数据确实出自该 enclave。
C# 示例中 mrEnclaveReference、mrSignerReference、securityVersionReference、productIdReference 与 computedUserData 的比对,正是上述清单中 Enclave Identity、SVN、Product ID 与 ReportData 四项的具体落地实现。
运行验证与结果解读
在 csharp 示例目录下直接运行:
$ dotnet run
程序会输出 user data、MRENCLAVE、MRSIGNER 的十六进制表示及各自匹配结果,Security Version 与 Product ID 的实际值与期望值,以及 librats_verify_evidence_from_json 的返回码(以十六进制显示)。当所有比对一致且返回码为 0 时,证据即被判定为可信。
作为对照,samples/sgx-ra/README.md 中 SGX 平台侧的 ./iwasm wasm-app/test.wasm 运行后会打印 JSON 证据并输出 Evidence is trusted.;若验证失败会返回 0xeXXX 形式的错误码。非 SGX 侧的 C# 示例正是把这段验证逻辑搬到了不依赖 SGX 硬件的托管环境中。另外注意,WAMR 侧生成的 evidence 需通过 librats_dispose_evidence_json 释放模块内存(见 lib_rats_wrapper.c 的 dispose 封装),托管侧示例则直接复用原始 JSON 字符串传入原生验证接口,无需处理模块内存生命周期。
适用边界与两种验证路径的选择
仓库提供了两种非 SGX 平台验证路径,可按运行环境取舍:
- Wasm 应用内验证:修改 SGX 平台侧生成的 Wasm 应用,调用暴露给 Wasm 的
librats_verifynative 函数对预计算好的 quote 做验证(父文档说明); - 脱离 WAMR 直接调用 librats:若验证平台根本不执行 WebAssembly 代码(没有 WAMR),可直接使用底层库 librats 的原生 API 验证,这正是 non-sgx-verify 目录下 C# 示例所展示的方式。
无论采用哪种路径,环境前提是一致的:验证机需安装 SGX DCAP 验证库、配置 PCCS 并预灌所有相关 SGX CPU 的 collateral。需要说明的是,示例中的参考值、nonce 与证据均为演示用途的样例数据,生产环境应替换为真实的 enclave 度量值并采用一次性 nonce 与会话绑定机制。