Skip to content
elnxPublic

About

Sign HAPs as you like

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ohosign

OpenHarmony HAP 签名工具 - 用于签名和管理 OpenHarmony HAP 包及 profile 文件。

功能

  • dump: 从 HAP 文件中提取 profile
  • adjust: 调整 profile 配置(APL、bundle name、ACLs 等)
  • sign: 签名 profile 或 HAP 文件
  • auto: 一键自动签名(提取权限 → 生成 profile → 签名 → 输出)
  • update: 更新权限定义和证书

前置条件

  • Go 1.25.5+
  • 运行期不再需要 Java/JRE;签名逻辑由纯 Go 库 hapsigntool/ 实现

构建

# 构建 ohosign(当前平台)→ bin/ohosign
make build

# 交叉编译所有平台 → dist/
make dist        # 等价于 make linux darwin windows

# 单独交叉编译
make linux       # dist/ohosign-linux-amd64
make darwin      # dist/ohosign-darwin-amd64, dist/ohosign-darwin-arm64
make windows     # dist/ohosign-windows-amd64.exe

# 构建上游兼容的 hap-sign-tool CLI(纯 Go)→ bin/hap-sign-tool
make sign-tool

# 更新权限定义(需要时手动执行)
make update-perms

# 格式化 Go 代码(gofmt)
make fmt

# 启用 git hooks(提交前对暂存的 Go 变更执行 go fix)
make hooks

# 清理构建产物
make clean

构建产物:本机构建在 bin/,交叉编译在 dist/。版本号取自 git describe --tags --always --dirty(可用 make VERSION=... 覆盖),通过 -X main.version 注入。make build / make dist 会先执行 update-perms:从 OpenHarmony 上游拉取 permission_definitions.json,与本地比较后按需更新(make dist 只拉一次);上游不可达时打印警告并沿用现有文件。

make hooks 将 core.hooksPath 指向 .githooks。之后每次 commit 前会对暂存的 Go 变更运行 go fix ./...,并把结果自动加入本次提交;若存在未暂存的 Go 修改则拒绝提交,避免把无关改动混入。克隆仓库后需手动执行一次 make hooks(该配置不会被 git 跟踪)。

使用方法

一键签名(推荐)

ohosign auto app.hap
ohosign auto app.hap -o signed.hap
ohosign auto app.hap --keystore my.p12 --store-pwd 123456 --key-pwd 123456

auto 命令会自动从 HAP 的 module.json 提取 requestPermissions,根据权限定义自动处理:

  • MDM 权限(availableType=MDM)→ 自动设置 enterprise_mdm 分发类型 + debug profile 类型
  • provisionEnable=false 权限 → 自动提升 APL 到所需级别
  • provisionEnable=true 权限 → 加入 allowed-acls 通过 ACL 授权
  • app-identifier → 优先沿用 HAP 内已有 profile 的值,其次由 bundleName 的 SHA-256 派生(十进制 19 位),可用 -i 覆盖;同一个包重复签名结果不变

提取 Profile

ohosign dump app.hap
ohosign dump app.hap -o profile.json
ohosign dump app.hap -c cert.cer -p profile.p7b
ohosign dump app.hap -V  # 验证模式

调整 Profile

ohosign adjust profile.json -a system_basic -b com.example.app
ohosign adjust profile.json --dump --hap-path app.hap -o new.json
ohosign adjust profile.json --module-json-path module.json

签名

# 签名 profile
ohosign sign profile profile.json
ohosign sign profile profile.json -a system_basic -b com.example.app

# 签名 HAP
ohosign sign hap app.hap --dump
ohosign sign hap app.hap -P profile.json -o signed.hap

更新资源

ohosign update

命令行参数

auto 命令

参数 简写 说明 默认值
--output -o 输出签名后的 HAP 路径 <name>-signed.hap
--keep-temp 保留临时 profile.json 和 .p7b 文件 false
--app-identifier -i 应用标识符(默认沿用包内 profile 的值,否则由 bundleName 派生)
--keystore 密钥库文件 keystore.p12
--store-pwd 密钥库密码 123456
--key-pwd 密钥密码 123456

dump 命令

参数 简写 说明
--output -o 输出 unsigned profile 路径
--cer -c 输出证书路径
--p7b -p 输出签名后的 profile 路径
--verify -V 启用验证模式

adjust 命令

参数 简写 说明
--apl -a 应用配置级别 (normal, system_basic, system_core)
--bundle-name -b Bundle 名称
--app-identifier -i 应用标识符
--acls -l 允许的 ACLs
--add-acls -A 添加 ACLs
--remove-acls -R 移除 ACLs
--distribution-type -d 分发类型
--hap-path -H HAP 文件路径
--module-json-path -M module.json 路径
--output -o 输出路径
--type -t Profile 类型 (debug/release)
--insert-perms -I 插入权限到 HAP
--dump -D 从 HAP 转储 profile
--zipalign zipalign 可执行文件路径

项目结构

.
├── main.go           # CLI 入口和命令定义
├── sign.go           # 签名逻辑(profile/HAP/证书)
├── auto_sign.go      # 一键签名工作流
├── profile.go        # Profile 解析、调整和 HAP 操作
├── permission.go     # 权限定义和映射
├── embed.go          # 嵌入资源管理
├── hapsigntool/      # 纯 Go 签名库(profile/HAP/codesign/verify/keystore)
├── cmd/hap-sign-tool/# 上游风格 CLI(sign-profile/sign-app/verify-*)
├── resources/        # 嵌入的签名资源
├── docs/             # 风险与测试记录
├── Makefile          # 构建脚本
└── ohosign.sh        # Shell 包装脚本

纯 Go 签名库

hapsigntool 提供:

  • SignProfile / VerifyProfileFile
  • SignApp / VerifyAppFile
  • ReadProfileFromHap
  • GenerateAppCert
  • OpenKeyStore

兼容性、设备实测与已知风险见 docs/SIGNING_RISKS.md。

签名密钥与证书

resources/ 下的密钥库包含明文可用的私钥,密码固定为 123456:

文件 内容 用途
OpenHarmony.p12 OpenHarmony 上游公开测试密钥库:Root CA、Application CA、application release、profile debug/release 等私钥 本仓库签发的证书链到上游公开测试根
keystore.p12 工具内置测试密钥库:open_app、open_profile 等私钥 auto/sign 的默认签名密钥
*.cer / *.pem 上述密钥对应的公开证书与证书链(不含私钥) profile/HAP 签名链

注意:

  • 这些私钥来自 OpenHarmony 上游公开测试材料,不是本仓库的机密;如被 secret scanning 标记,属于已知公开测试密钥,可按误报处理或加入允许列表。
  • 它们只用于开发与测试。任何人持这些私钥都能签发链到 OpenHarmony 测试根的证书,因此用其签名的产物不具备生产信任价值。
  • 生产或企业分发签名请通过 --keystore、--store-pwd、--key-pwd 指定自管密钥,并妥善保护私钥。
  • 相关风险说明见 docs/SIGNING_RISKS.md 的 R8、R9。

License

本项目采用 MIT License。

About

Sign HAPs as you like

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages