88# 用法:
99# packages/app/src-tauri/native/macos/sign.sh --setup # 首次创建并安装长期自签名证书(无需 Apple 账号)
1010# packages/app/src-tauri/native/macos/sign.sh --check # 检查当前钥匙串是否有可用签名身份
11+ # packages/app/src-tauri/native/macos/sign.sh --dev # 构建并启动固定签名开发包
12+ # packages/app/src-tauri/native/macos/sign.sh --import-ci # CI 导入签名证书
1113# packages/app/src-tauri/native/macos/sign.sh # 自动签名存在的 debug/release 二进制与 .app
1214# packages/app/src-tauri/native/macos/sign.sh <路径> # 签名指定的 .app 或可执行文件
1315#
@@ -53,7 +55,6 @@ setup_identity() {
5355
5456 local key_path=" $SIGNING_DIR /$IDENTITY .key"
5557 local cert_path=" $SIGNING_DIR /$IDENTITY .crt"
56- local config_path=" $ROOT /packages/app/src-tauri/native/macos/signing-openssl.cnf"
5758
5859 if [ -z " ${SBOX_CERTIFICATE_PASSWORD:- } " ]; then
5960 read -r -s -p " 设置 $IDENTITY .p12 的导出密码(用于 GitHub Secret): " SBOX_CERTIFICATE_PASSWORD
@@ -66,9 +67,25 @@ setup_identity() {
6667 export SBOX_CERTIFICATE_PASSWORD
6768
6869 openssl req -x509 -newkey rsa:3072 -sha256 -days 3650 -nodes \
69- -config " $config_path " \
70+ -config /dev/stdin \
7071 -keyout " $key_path " \
71- -out " $cert_path "
72+ -out " $cert_path " << 'OPENSSL_CONFIG '
73+ [req]
74+ prompt = no
75+ distinguished_name = subject
76+ x509_extensions = codesign
77+
78+ [subject]
79+ CN = sbox-dev
80+ O = sbox local development
81+
82+ [codesign]
83+ basicConstraints = critical, CA:true
84+ keyUsage = critical, digitalSignature, keyCertSign
85+ extendedKeyUsage = codeSigning
86+ subjectKeyIdentifier = hash
87+ authorityKeyIdentifier = keyid:always
88+ OPENSSL_CONFIG
7289 openssl pkcs12 -export \
7390 -name " $IDENTITY " \
7491 -inkey " $key_path " \
@@ -102,18 +119,78 @@ sign() {
102119 echo " 已签名: $1 "
103120}
104121
105- if [ " ${1:- } " = " --setup" ]; then
106- setup_identity
107- elif [ " ${1:- } " = " --check" ]; then
108- check_identity
109- elif [ $# -ge 1 ]; then
110- sign " $1 "
111- else
112- # tauri dev 实际运行的裸二进制
113- sign " $TARGET_DIR /debug/sbox"
114- sign " $TARGET_DIR /release/sbox"
115- # 打包产物(包括 --target 生成的子目录)
116- while IFS= read -r -d ' ' app; do
117- sign " $app "
118- done < <( find " $TARGET_DIR " -type d -path ' */bundle/macos/sbox.app' -print0 2> /dev/null)
119- fi
122+ signed_dev () {
123+ check_identity > /dev/null
124+ (
125+ cd " $ROOT /packages/app"
126+ APPLE_SIGNING_IDENTITY=" $IDENTITY " pnpm exec tauri build --debug --bundles app
127+ open " $TARGET_DIR /debug/bundle/macos/sbox.app"
128+ )
129+ }
130+
131+ import_ci () {
132+ : " ${APPLE_CERTIFICATE:? 缺少 GitHub Secret APPLE_CERTIFICATE} "
133+ : " ${APPLE_CERTIFICATE_PASSWORD:? 缺少 GitHub Secret APPLE_CERTIFICATE_PASSWORD} "
134+
135+ local ci_keychain_password=" sbox-ci-keychain"
136+ local ci_keychain_path=" $RUNNER_TEMP /sbox-signing.keychain-db"
137+ local ci_p12_path=" $RUNNER_TEMP /sbox-signing.p12"
138+ local ci_cert_path=" $RUNNER_TEMP /sbox-signing.crt"
139+
140+ printf ' %s' " $APPLE_CERTIFICATE " | base64 -D > " $ci_p12_path "
141+
142+ echo " 正在创建临时签名钥匙串..."
143+ security create-keychain -p " $ci_keychain_password " " $ci_keychain_path "
144+ security set-keychain-settings -lut 21600 " $ci_keychain_path "
145+ security unlock-keychain -p " $ci_keychain_password " " $ci_keychain_path "
146+
147+ echo " 正在导入签名身份..."
148+ security import " $ci_p12_path " -k " $ci_keychain_path " \
149+ -P " $APPLE_CERTIFICATE_PASSWORD " -T /usr/bin/codesign
150+ # 从已导入的钥匙串读取证书,避免 OpenSSL 3 拒绝旧 P12 中的 RC2 加密。
151+ security find-certificate -c " $IDENTITY " -p " $ci_keychain_path " > " $ci_cert_path "
152+
153+ echo " 正在信任自签名证书..."
154+ sudo -n security add-trusted-cert -d -r trustRoot -p codeSign \
155+ -k /Library/Keychains/System.keychain " $ci_cert_path "
156+
157+ echo " 正在授权 codesign 访问私钥..."
158+ security set-key-partition-list -S apple-tool:,apple:,codesign: \
159+ -s -k " $ci_keychain_password " " $ci_keychain_path "
160+
161+ local existing_keychains
162+ existing_keychains=" $( security list-keychains -d user | tr -d ' "' ) "
163+ # shellcheck disable=SC2086
164+ security list-keychains -d user -s " $ci_keychain_path " $existing_keychains
165+
166+ if ! security find-identity -v -p codesigning " $ci_keychain_path " | grep -Fq " \" $IDENTITY \" " ; then
167+ echo " 导入后仍未找到有效签名身份: $IDENTITY " >&2
168+ security find-identity -v -p codesigning " $ci_keychain_path " >&2
169+ return 1
170+ fi
171+
172+ echo " APPLE_SIGNING_IDENTITY=$IDENTITY " >> " $GITHUB_ENV "
173+ echo " 已导入固定 macOS 签名身份: $IDENTITY "
174+ }
175+
176+ case " ${1:- } " in
177+ --setup) setup_identity ;;
178+ --check) check_identity ;;
179+ --dev) signed_dev ;;
180+ --import-ci) import_ci ;;
181+ --help|-h)
182+ echo " Usage: bash sign.sh [--setup|--check|--dev|--import-ci|<path>]"
183+ ;;
184+ --* ) echo " 未知参数: $1 " >&2 ; exit 1 ;;
185+ * )
186+ if [ $# -ge 1 ]; then
187+ sign " $1 "
188+ else
189+ sign " $TARGET_DIR /debug/sbox"
190+ sign " $TARGET_DIR /release/sbox"
191+ while IFS= read -r -d ' ' app; do
192+ sign " $app "
193+ done < <( find " $TARGET_DIR " -type d -path ' */bundle/macos/sbox.app' -print0 2> /dev/null)
194+ fi
195+ ;;
196+ esac
0 commit comments