Files
ssw-mpc/deploy/tee/kbs/serve.py
T
admin 5dec915185 feat(tee): KBS 客户端 + 证明结构校验,OCI 打包与真机编排资产入仓
- mpc-tee 新增 kbs.rs(最小 HTTP/1.1 KBS 客户端 + fail-closed 校验 + KbsKeySource 替代内置 PSK)、verify.rs(SNP 报告结构解析 + 可插拔外部芯片验签器 + verify_attestation 显式区分「芯片签名未验证」)。
- mpc-cli 新增 mpc tee verify / mpc tee kbs-get 子命令。
- catalog.rs 补 verify/kbs 模块到 TEE 直接调度段并加回归断言(清单铁律)。
- deploy/tee 新增 oci/(分层镜像构建)、kbs/(serve stub)、guest/(init)、real-machine/(跨机双 guest 编排);docs/TEE_OCI_PATH.md 记录 Kata 实测复核(仅 AMD 有、海光无且不支持 CSV,不引入)与 e2b 远程调度接口参考。
- 影响:密钥经 KBS 注入(不再内置明文 PSK),证明结论如实区分结构与芯片验签;远程调度走 OCI digest + 框架选节点,不引入 Kata/e2b 依赖。
- 测试:./tools/ci.sh 四道全绿;mpc-tee 单测 73→86。
2026-11-06 19:33:53 +08:00

113 lines
4.6 KiB
Python
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.
#!/usr/bin/env python3
"""可复现的 KBS 资源下发 stub(仅用于本地 / 真机演练)。
**这不是 Trustee / KBS,也不做芯片证明校验**。它只提供最小的
`GET /kbs/v0/resource/<repo>/<tag>/<kind>` 资源接口,让 guest 内的
`mpc tee kbs-get` 有东西可取,从而把「PSK 内置 initrd」换成
「按会话从 KBS 取」。
生产必须换成带远程证明校验的 KBS:由它验证 SNP/CSV 报告、证书链与
measurement 后才下发密钥。本 stub 只检查请求头里的 session 与 token
是否匹配启动参数,token 是占位字符串时(非 --strict)会放行并告警。
资源目录布局:
<resources>/mpc/<session_id_64hex>/psk
<resources>/mpc/<session_id_64hex>/seed
<resources>/mpc/<session_id_64hex>/root-key
Python 3.6 兼容(海光 Anolis 8.10)。
"""
import argparse
import os
import sys
from http.server import BaseHTTPRequestHandler, HTTPServer
PREFIX = "/kbs/v0/resource/"
DEMO_TOKEN = "offline-demo"
def make_handler(resources, session, strict):
class Handler(BaseHTTPRequestHandler):
server_version = "mpc-tee-kbs-stub/1.0"
def log_message(self, fmt, *args):
sys.stderr.write("[kbs] %s\n" % (fmt % args))
def _deny(self, code, reason):
body = ("denied: %s" % reason).encode("utf-8")
self.send_response(code)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_GET(self):
path = self.path.split("?", 1)[0]
if not path.startswith(PREFIX):
return self._deny(404, "unknown path")
parts = path[len(PREFIX):].split("/")
if len(parts) != 3 or not all(parts):
return self._deny(400, "expected /<repo>/<tag>/<kind>")
repo, tag, kind = parts
allowed = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz0123456789._-")
if any(not set(p) <= allowed for p in parts):
return self._deny(400, "illegal resource component")
got_session = self.headers.get("MPC-Tee-Session", "")
token = self.headers.get("MPC-Tee-Attestation", "")
if got_session != session or tag != session:
return self._deny(403, "session mismatch")
if not token:
return self._deny(403, "missing attestation token")
if strict and token == DEMO_TOKEN:
return self._deny(403, "demo token rejected in strict mode")
if token == DEMO_TOKEN:
sys.stderr.write("[kbs] WARNING: 放行占位证明 token(演练模式)\n")
full = os.path.abspath(os.path.join(resources, repo, tag, kind))
base = os.path.abspath(os.path.join(resources, repo, tag))
if not (full == base or full.startswith(base + os.sep)):
return self._deny(400, "path traversal")
if not os.path.isfile(full):
return self._deny(404, "resource not found")
with open(full, "rb") as f:
body = f.read()
self.send_response(200)
self.send_header("Content-Type", "application/octet-stream")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_POST(self):
self._deny(405, "read-only stub")
return Handler
def main():
ap = argparse.ArgumentParser(description="KBS 资源下发 stub(演练用)")
ap.add_argument("--resources", required=True, help="资源根目录")
ap.add_argument("--session", required=True, help="本轮会话 id(64 位十六进制)")
ap.add_argument("--host", default="127.0.0.1")
ap.add_argument("--port", type=int, default=18080)
ap.add_argument("--strict", action="store_true",
help="拒绝 offline-demo 占位 token(生产应开启)")
args = ap.parse_args()
if not os.path.isdir(args.resources):
sys.exit("资源目录不存在: %s" % args.resources)
srv = HTTPServer((args.host, args.port),
make_handler(args.resources, args.session, args.strict))
sys.stderr.write("[kbs] stub listening %s:%d strict=%s\n" %
(args.host, args.port, args.strict))
sys.stderr.write("[kbs] 提醒:这是演练 stub,生产请用带证明校验的 KBS\n")
try:
srv.serve_forever()
except KeyboardInterrupt:
pass
return 0
if __name__ == "__main__":
sys.exit(main())