CCSecureBoot
在 CC: Tweaked 中实现启动安全。
安装
必须已安装 CC: Tweaked 1.112.0 或更高版本。
只需将构建好的 JAR 文件安装到 mods 文件夹中。该模组仅在服务器端运行,因此多人游戏客户端无需安装。
使用方法
默认情况下,所有计算机的启动安全功能均处于禁用状态。要为计算机启用启动安全,请运行 enroll-secure-boot 程序。该程序将为计算机生成一个新密钥,并将其放置在软盘上以妥善保管。
enroll-secure-boot 首先会显示有关启动安全的一些重要信息,并询问是否继续。然后它会提示输入密钥密码——这是可选的,但建议设置以防止密钥意外泄露。如果未检测到磁盘,它将等待插入磁盘。准备就绪后,将生成并保存密钥,同时恢复启动配置也会放置在磁盘上。
当启用启动安全时,计算机只能从带有签名的文件启动。可以使用 sign 命令对文件进行签名,这将在程序旁边创建一个 .sig 文件。签名时必须将密钥磁盘放入磁盘驱动器中。
启动安全禁用了直接访问 shell 的功能——默认情况下它只会启动到 startup.lua(该文件必须已签名)。如果插入了密钥磁盘,将出现一个启动菜单,提供启动 shell 的选项。
启动安全基于 pxboot 运行,允许自定义启动屏幕,包括添加更多菜单项、更改默认条目和超时时间以及美化菜单。额外的配置可以从 /pxboot_config.lua 加载,这也必须进行签名。有关如何为 pxboot 编写配置的更多信息,请参阅仓库说明。
可以通过在任何插入了密钥卡的计算机上运行 unenroll-secure-boot 来禁用计算机的启动安全。计算机是通过密钥卡解除注册的,而不是通过计算机 ID,因此无法再启动的计算机也可以被解除注册。密钥也会被撤销,从而防止密钥泄露后的滥用。
如果丢失了计算机的密钥,则无法恢复该计算机——将无法再修改启动文件或从启动安全中解除注册。请将密钥磁盘存放在其他玩家接触不到的安全地方。
保护你的代码
启动安全仅确保进入程序的信任根——它并不能保证整个计算机是安全的。你需要自行确保代码免受黑客攻击,包括检查所有加载的代码并避免使用不安全的程序。
当启用启动安全时,由于启动管理器的设置方式,你自动受到保护,免受两种最基本的程序绕过手段:按住 Ctrl+T 终止到 shell,以及插入包含 startup 文件的磁盘。你不需要在程序中针对这些情况进行防护(尽管理想情况下你应该安全地处理 terminate 事件,而不是报错重启)。
为了帮助代码签名,CCSecureBoot 暴露了一个易于使用的库,该库基于启动管理器使用的相同代码。只需调用 dofile("/rom/modules/main/codesign.lua") 即可访问一些使检查签名更简单的函数:
codesign.verify(path):简单验证文件的签名,并返回一个布尔值表示文件是否安全。
codesign.execute(cmd, ...):shell.execute 的包装器,用于检查命令的签名。但这不允许 ROM 程序——请使用普通的 shell.execute 运行已知安全的程序。
codesign.loadfile(path, mode, env):loadfile 的包装器,用于检查文件的签名。这会自动允许 /rom/modules 中的模块,但对 ROM 的其他部分无效。
codesign.dofile(path):dofile 的包装器,用于检查文件的签名。这会自动允许 /rom/modules 中的模块,但对 ROM 的其他部分无效。
codesign.enforceModuleSigning():要求通过 require 加载的所有模块进行代码签名。在程序开始时调用一次——它将在程序的剩余生命周期内保持活动状态。这在 codesign.execute/shell.execute 调用内部不会保留——在新的程序中再次调用。
codesign.sign(data, password):为提供的数据生成签名 blob。其格式与 .sig 文件相同。必须插入密钥磁盘,如果密钥受密码保护,则必须提供密码。
codesign.load(data, sig, name, mode, env):load 的包装器,使用提供的签名 blob 检查代码的签名。
如果你的程序通过 require 使用模块,请在加载任何模块之前启用模块签名,以确保它们正确签名且未被不受信任的代码替换:
-- require 可能会加载本地模块而不是 ROM 中的模块——始终使用 dofile 加载 codesign:
local codesign = dofile "/rom/modules/main/codesign.lua"
codesign.enforceModuleSigning()
-- 现在 require 可以安全地加载外部模块:
local yourmod = require "yourmod"
-- ...
提醒一下,切勿在没有受限环境的情况下通过 load 加载不受信任的代码,尤其是当输入来自调制解调器时。如果你想加载代码,请为其提供一个高度受限的环境,仅包含所需的函数(如果有的话)。(尽管 textutils.unserialize 加载代码,但它使用空环境,因此被认为是安全的。)
内部工作原理
CCSecureBoot 使用 PKCS 或 S/MIME 协议栈来存储加密信息,并使用 Ed25519 进行签名。大部分繁重工作由 Lua 端的 libcert 和 ccryptolib 以及 Java 端的 Bouncy Castle 完成。
服务器保存一个唯一的私钥,位于存档目录中的 computercraft/root.key,以及一个关联的证书,位于 computercraft/certs/root.pem。它们作为服务器的信任根(root of trust)。随后,计算机可以请求由该根证书签名的自身证书,这些证书在验证时可追溯至根证书。请求通过一个 Java API 传递,该 API 被隐藏在 require 调用之后。
CCSecureBoot 向 /rom/autorun 插入一个程序,用于检查是否已注册。如果当前计算机已注册,则运行 pxboot。它通过查找 /rom/pxboot/certs/enrolled/<id> 来检查注册状态,其中 /rom/pxboot/certs 是一个挂载在 computercraft/certs 中的存档挂载点(允许模组在运行时编辑它)。
所使用的 pxboot 版本包含一个模块,该模块在所有加载代码的命令中插入签名检查。签名存储在必须签名的文件旁边的 PEM 编码 PKCS#7 容器中,并包含签名者的证书以及签名本身。这使得签名可以独立存在,而无需单独的有效证书列表——嵌入式证书可以与受信任的根进行验证。
注册安全启动涉及为当前计算机 ID 生成 PKCS#10 签名请求。计算机首先生成一个私钥,然后为关联的公钥创建签名请求。CSR 的主题中包含一个字段 uniqueIdentifier(OID 2.5.4.45),其中包含计算机的 ID。此 ID 必须与正在运行的计算机匹配,否则签名请求将被拒绝。
随后,CSR 以 PEM 格式传递给服务器进行签名,使用 secureboot.enroll(csr)。如果 ID 匹配,新的 X.509 证书将被返回(PEM 编码),并在 computercraft/certs/enrolled//rom/pxboot/certs/enrolled 中为计算机 ID 创建一个文件,告知引导加载器检查签名。
此后,证书和密钥分别以 X.509 和 PKCS#8 格式保存,均为 PEM 编码。为了安全起见,在磁盘上创建了用于加载 shell 的引导配置和启动程序,并使用新密钥对其进行签名。
签名是一个简单的 PKCS#7 签名操作,使用软盘上存储的证书和密钥。
取消注册涉及调用 secureboot.unenroll(cert, signature),传入计算机的证书(PEM 编码的 X.509)以及一个已签名的挑战字符串。挑战文本仅仅是目标计算机 ID 的字符串形式,签名以裸 Ed25519 签名形式传递,省略了任何 PKCS#7 包装器以简化处理(因为在 Java 中没有简便的 PKCS#7 签名检查方法)。如果证书有效且签名通过,注册文件将被删除,并且证书将存储在证书吊销列表中,存储位置为 computercraft/certs/revoked.crl。
许可证
CCSecureBoot 根据 MPLv2 许可,部分根据 MIT 许可。
CCSecureBoot
Implements boot security in CC: Tweaked.
Installation
CC: Tweaked 1.112.0 or later must already be installed.
Simply install a built JAR into your mods folder. This mod only operates on the server side, so multiplayer clients do not need it installed.
Usage
By default, secure boot is disabled for all computers. To enable secure boot for a computer, run the enroll-secure-boot program. This program will generate a new key for the computer, placing it on a floppy disk for safe keeping.
enroll-secure-boot will first display some important information about secure boot, and asks for confirmation to continue. Then it will prompt for a password for the key - this is optional, but recommended to prevent accidental key leakage. If a disk isn't detected, it will then wait for a disk to be inserted. Once ready, the key will be generated and saved, and a recovery boot config will be placed on the disk as well.
When secure boot is enabled, the computer will only be able to boot from files that have a signature alongside them. Files can be signed with the sign command, which will create a .sig file next to the program. The key disk must be in a disk drive to sign.
Secure boot disables direct access to the shell - it will only boot to startup.lua by default (which must be signed). If the key disk is inserted, a boot menu will appear with the option to start a shell.
Secure boot runs on pxboot, which allows customizing the boot screen, including adding more menu entries, changing the default entry and timeout, and stylizing the menu. Additional configs can be loaded from /pxboot_config.lua, which must be signed as well. See the repo for more information on how to write configs for pxboot.
Secure boot may be disabled for a computer by running unenroll-secure-boot on any computer with the key card inserted. The computer is unenrolled by key card, not by computer ID, so a computer which cannot be booted anymore can be unenrolled. The key will be revoked as well, which allows preventing abuse if a key is leaked.
If the key for a computer is lost, there is no way to recover the computer - it will no longer be possible to modify the boot files or unenroll the computer from secure boot. Keep the key disk in a safe place, away from other players.
Securing your code
Secure boot only ensures a root of trust going into your program - it does not guarantee that your computer as a whole is safe. It is up to you to make sure your code is secure from hacks, including checking all code that's loaded and avoiding unsafe programs.
When secure boot is enabled, due to the way the boot manager is set up, you are automatically protected from the two most basic bypasses for programs: holding Ctrl+T to terminate to the shell, and inserting a disk with a startup file. You won't have to protect against these in your program (though you should ideally handle terminate events safely instead of erroring and restarting).
To help with code signing, CCSecureBoot exposes an easy-to-use library based on the same code that the boot manager uses. Simply call dofile("/rom/modules/main/codesign.lua") to gain access to a few functions that make checking signatures easier:
codesign.verify(path): Simply verifies the signature of a file, and returns a boolean whether the file is safe.
codesign.execute(cmd, ...): A wrapper for shell.execute that checks the signature of the command. This doesn't allow ROM programs, however - use normal shell.execute to run known safe programs.
codesign.loadfile(path, mode, env): A wrapper for loadfile that checks the signature of the file. This automatically allows modules in /rom/modules, but will not work for the rest of the ROM.
codesign.dofile(path): A wrapper for dofile that checks the signature of the file. This automatically allows modules in /rom/modules, but will not work for the rest of the ROM.
codesign.enforceModuleSigning(): Requires code signing on all modules loaded through require. Call this once at the start of your program - it will stay active for the rest of the program's lifetime. This does not survive inside codesign.execute/shell.execute calls - call it again inside a new program.
codesign.sign(data, password): Generates a signature blob for the data provided. This is in the same format as .sig files. The key disk must be inserted, and if the key is password-protected, the password must be provided.
codesign.load(data, sig, name, mode, env): A wrapper for load that checks the signature of the code, using a provided signature blob.
If your program uses modules via require, enable module signing before loading any modules, to make sure they are properly signed and not replaced with untrusted code:
-- require could load a local module instead of the one in ROM - always use dofile to load codesign:
local codesign = dofile "/rom/modules/main/codesign.lua"
codesign.enforceModuleSigning()
-- Now require is safe to load external modules:
local yourmod = require "yourmod"
-- ...
As a reminder, never load untrusted code through load without a restricted environment, especially when the input is from a modem. If you want to load code, give it a highly restricted environment with only the functions required, if any. (Despite the fact that textutils.unserialize loads code, it uses an empty environment, so it's considered safe.)
Inner workings
CCSecureBoot uses the PKCS or S/MIME protocol stack for storing cryptographic info, and uses Ed25519 for signatures. Most of the heavy lifting is done by libcert and ccryptolib on the Lua side, and Bouncy Castle on the Java side.
The server keeps a unique private key, stored in computercraft/root.key in the save directory, and an associated certificate, stored in computercraft/certs/root.pem. These are used as a root of trust for the server. Computers can then request certificates for themselves signed by the root certificate, which are traced back to the root on verification. Requests are passed through a Java API which is hidden behind a require call.
CCSecureBoot inserts a program into /rom/autorun which checks for enrollment, and if the current computer is enrolled then it runs pxboot. It checks enrollment by looking for /rom/pxboot/certs/enrolled/<id>, where /rom/pxboot/certs is a save mount located in computercraft/certs (allowing the mod to edit it at runtime).
The version of pxboot used includes a module which inserts a signature check into all commands which load code. Signatures are stored in PEM-encoded PKCS#7 containers next to the files which must be signed, and contain the certificate of the signer alongside the signature. This allows the signature to stand alone without needing to have a separate list of valid certificates - the embedded cert can be validated against the trusted root.
Enrolling in secure boot involves generating a PKCS#10 signing request for the current computer ID. The computer first generates a private key, and then creates a signing request for the associated public key. The CSR contains a field in the subject, uniqueIdentifier (OID 2.5.4.45), which contains the computer's ID. This ID must match the running computer, otherwise the signing request will be rejected.
The CSR is then passed off to the server in PEM format to sign, using secureboot.enroll(csr). If the IDs match, the new X.509 certificate is passed back (PEM-encoded), and a file is created in computercraft/certs/enrolled//rom/pxboot/certs/enrolled for the computer ID, telling the bootloader to check signatures.
Afterward, the certificate and key are saved in X.509 and PKCS#8 formats, respectively, both PEM-encoded. For safety, a boot config and startup program for loading the shell are created on the disk, which are then signed with the new key.
Signing is a simple PKCS#7 signature operation, using the certificate and key stored on the floppy disk.
Unenrolling involves calling secureboot.unenroll(cert, signature) with the computer's certificate (PEM-encoded X.509), as well as a signed challenge string. The challenge text is simply the ID of the target computer as a string, and the signature is passed as a bare Ed25519 signature, foregoing any PKCS#7 wrapper for simplicity (since there's no easy PKCS#7 signature checking in Java). If the certificate is valid and the signature passes, the enrollment file is deleted and the certificate is stored in a certificate revocation list, stored at computercraft/certs/revoked.crl.
License
CCSecureBoot is licensed under MPLv2, with portions licensed under MIT.
评论 0
登录后参与评分
还没有评论,来抢沙发吧