Files
iOSBuildServer/backend/automation/obfuscation/generate_encrypted_private_key.py
T
shenlei 83a1db21e5 feat: 接入 iOSGuard LLVM 混淆工具链与 IPA 加固门禁,按分支策略兼容旧版本打包
- 新增 toolchain_manager:构建前自愈校验 LLVM opt 工具链,按 Xcode 版本与渠道隔离 obj_cache
- readoor31/readoor31OtherPay Archive 透传 IG_DIST_CHANNEL/IG_OBF_MODE/IG_ALL_GUARD_OFF/IG_RELEASE_SEED
- Archive 前核对分发渠道编译条件:上架包缺失 IG_APPSTORE_DISTRIBUTION 即阻断,非上架包带该条件仅告警,未接入 anti_tamper 的分支跳过
- 导出后执行 IPA 加固门禁审计,失败阻断上传;持久化 dSYM、混淆映射、诊断事件码与审计报告
- 混淆是否执行改由分支 AutoPacking/build_policy.json 声明:force_obfuscation=true 强制,否则按任务勾选,兼容旧分支/旧 Tag;恢复打包页混淆勾选框
- 分支未自带混淆脚本时回退服务端脚本并告警;同步更新服务端 L1 混淆脚本
- .gitignore 忽略 iosguard_cache 与 artifacts 运行时目录;README 补充分支打包策略说明
2026-11-24 12:03:54 +09:00

97 lines
4.1 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
"""
SD 鉴权 X-CT=3.2 私钥 CI 加密脚本
在 Xcode Run Script Build Phase 中调用(仿照同目录 generate_image_whitelist.py 的模式),
构建期读取仓库里的原始 PEM 私钥 AutoPacking/sd_priv_ios_v1.pem,用 AES-256-GCM 加密后
直接写入当前 target 的 app bundle(${TARGET_BUILD_DIR}/${WRAPPER_NAME}/encrypted_private_key.bin),
不经过 Xcode 的 Copy Bundle Resources,早于 CodeSign 执行即可随包一起签名(同 whitelist 脚本的做法)。
产出格式为 nonce(12字节) + ciphertext + tag(16字节) 拼接,与 Swift 侧 CryptoKit
AES.GCM.SealedBox(combined:) 的输入格式完全一致,App 运行时解密见
readoor/3.0/Network/RDSDPrivateKeyManager.swift。
PBKDF2 密钥派生参数必须和 Swift 侧完全一致,见该文件里的
pbkdf2Password / pbkdf2Salt / pbkdf2Iterations 常量。
依赖:cryptography(第三方 Python 包,标准库不自带 AES-GCM)。这是方案文档 §8 记录的一个已知
风险,已在本次实现中实测确认:macOS 系统自带的 /usr/bin/openssl 是 LibreSSL 3.3.6,其 enc
子命令对 -aes-256-gcm 直接报 "bad decrypt",openssl 命令行方案不可行;改用 cryptography 包
经端到端验证(Xcode Run Script Build Phase 实际跑通、产出的密文能正确解密回原始 PEM)确认可行,
但需要在每台构建机(含 iOSBuildServer)上为 Xcode 实际调用的 python3(通常是
/usr/bin/python3,Xcode 自带的 3.9)显式安装该依赖:
/usr/bin/python3 -m pip install --user cryptography
"""
import hashlib
import os
import sys
try:
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
except ImportError:
sys.stderr.write(
"[sd_v32] 缺少 cryptography 包,无法加密私钥。\n"
"[sd_v32] 请在构建机上执行: /usr/bin/python3 -m pip install --user cryptography\n"
"[sd_v32] (macOS 系统自带 openssl 是 LibreSSL,不支持 aes-256-gcm,已确认不可用作替代方案)\n"
)
sys.exit(1)
# 必须和 RDSDPrivateKeyManager.swift 里的同名常量保持一致
PBKDF2_PASSWORD = b"readoor_sd_v32_ci_key_2026"
PBKDF2_SALT = b"readoor_sd_v32_salt_fixed"
PBKDF2_ITERATIONS = 100_000
AES_KEY_LENGTH = 32 # AES-256
NONCE_LENGTH = 12 # 与 CryptoKit AES.GCM 默认 nonce 长度一致
def derive_key() -> bytes:
return hashlib.pbkdf2_hmac(
"sha256", PBKDF2_PASSWORD, PBKDF2_SALT, PBKDF2_ITERATIONS, dklen=AES_KEY_LENGTH
)
def main() -> int:
srcroot = os.environ.get("SRCROOT")
if not srcroot:
# 允许手动执行时退回脚本所在目录向上两级推断项目根目录
srcroot = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
private_key_path = os.path.join(srcroot, "AutoPacking", "sd_priv_ios_v1.pem")
if not os.path.isfile(private_key_path):
sys.stderr.write(f"[sd_v32] 未找到私钥文件: {private_key_path}\n")
return 1
with open(private_key_path, "rb") as f:
plaintext = f.read()
key = derive_key()
nonce = os.urandom(NONCE_LENGTH)
ciphertext_with_tag = AESGCM(key).encrypt(nonce, plaintext, None)
combined = nonce + ciphertext_with_tag
target_build_dir = os.environ.get("TARGET_BUILD_DIR")
wrapper_name = os.environ.get("WRAPPER_NAME")
if target_build_dir and wrapper_name:
# Xcode Build Phase 场景:直接写进当前 target 的 app bundle,早于 CodeSign 执行
output_dir = os.path.join(target_build_dir, wrapper_name)
else:
# 手动执行场景:写到项目根目录,方便本地验证加解密是否正确
output_dir = srcroot
output_path = os.path.join(output_dir, "rd_glyph_lut_v2.dat")
for stale_name in ("rd_net_cfg_v1.dat", "encrypted_private_key.bin"):
stale_path = os.path.join(output_dir, stale_name)
if os.path.isfile(stale_path):
os.remove(stale_path)
os.makedirs(output_dir, exist_ok=True)
with open(output_path, "wb") as f:
f.write(combined)
print(f"[sd_v32] asset written: {output_path}")
return 0
if __name__ == "__main__":
sys.exit(main())