這兩天做 In-House 簽名時,提示 SSL 憑證過期。索性把過去折騰 In-House 發布的過程完整記錄一次,之後有更新也回來補上。
2012 年我們寫了一個 shell 腳本來重新簽名:
IPA="xxx.ipa"
APPNAME="xxx.app"
PROVISION="embedded.mobileprovision"
CERTIFICATE="xxx" # must be in keychain, such as "iPhone Distribution: Tencent Co.,Ltd"
rm -rf Payload
rm -rf resigned.ipa
unzip -q "$IPA"
# remove the signature
rm -rf Payload/*.app/_CodeSignature
Payload/*.app/CodeResources
# replace the provision
rm -rf Payload/*.app/embedded.mobileprovision
cp "$PROVISION" Payload/$APPNAME/embedded.mobileprovision
# sign with the new certificate
/usr/bin/codesign -f -s "$CERTIFICATE" \
--resource-rules Payload/*.app/ResourceRules.plist \
--entitlements game.plist \
Payload/*.app
# zip it back up
zip -qr resigned.ipa Payload
rm -rf Payload
中間隨著 iOS 與 Xcode 版本升級,陸續小修細節。每次 Xcode 升級後,記得重新下載安裝對應的 Command Line Tools。
iOS 7.1 更新後,蘋果收緊了檢查,用 Safari 安裝時會出現「無法安裝應用程序 因為證書無效」。解法是把 plist 放到 https 服務器上。最簡單是丟到 Dropbox 這類雲存儲,但國內訪問可能受限,因此我在公司內網的 Apache 上自建了 SSL。
1. 載入 SSL 動態庫
打開 conf/httpd.conf,把 LoadModule ssl_module modules/mod_ssl.so 前面的註解拿掉。
2. 配置 SSL,編輯 httpd-ssl.conf:
SSLSessionCache "shmcb:logs/ssl.scache(512000)"
Listen 442 # 為了避免端口衝突,我改成 442
<VirtualHost _default_:442>
# General setup for the virtual host
DocumentRoot "D:/programs/xampp-win32-1.8.1-VC9/htdocs"
ServerName 192.168.1.221 # 必須是 ip 或域名
ServerAdmin webmaster@localhost # 可隨意填
ErrorLog "logs/error.log"
<IfModule log_config_module>
CustomLog "logs/access.log" combined
</IfModule>
SSLEngine on
SSLCertificateFile "conf/ssl.crt/server.crt"
SSLCertificateKeyFile "conf/ssl.key/server.key"
</VirtualHost>
3. 建立伺服器私鑰(在 apache/bin 目錄下執行):
openssl genrsa -out server.key 1024
4. 建立簽署申請,Common Name 必須是伺服器 IP 或域名,其餘可留空:
openssl req -new -out server.csr -key server.key -config ..\conf\httpd-ssl.cnf
5. 建立 CA 私鑰:
openssl genrsa -out ca.key 1024
6. 建立 CA 憑證,Common Name 同樣必須是伺服器 IP 或域名;為免過期,有效期我設為 10 年:
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -config ..\conf\httpd-ssl.cnf
7. 建立 demoCA 目錄結構:在當前目錄建立 demoCA,裡面準備一個空目錄 newcerts、一個空檔案 index.txt,以及一個名為 serial 的檔案,內容寫入 01。
8. 用 CA 為伺服器簽署憑證:
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key -config ..\conf\openssl.cnf
9. 擺放檔案:把 server.crt 複製到 conf/ssl.crt/,server.key 複製到 conf/ssl.key/。
10. 重啟 Apache,用 https://localhost:442 測試。
11. 修改 html 中的 plist 連結:
itms-services://?action=download-manifest&url=https://192.168.1.221:442/down/dev/xxx.plist
這樣在手機瀏覽器上就能直接線上安裝了。
中間有一陣子沒做 In-House 發布,此期間 Mac 升到了 10.10、Xcode 升到 6.1.1。前天簽名時出現警告:
--resource-rules has been deprecated in max os x >= 10.10
簽完放到伺服器上,線上安裝到最後一刻跳出「無法下載應用程序 此時無法安裝 xxx」。

單純把 --resource-rules 那行註解掉並不能解決。後來在 Stack Overflow 找到可行方案,實測有效:
進入 專案 → Targets → 選取你的 target → Build Settings → Code Signing Resource Rules Path,填入:
$(SDKROOT)/ResourceRules.plist
補充一點:上面流程中的自建憑證屬自簽名,安裝時使用者會看到「未受信任的開發者」提示,需要在「設定 → 通用 → 描述檔」中手動信任。若發布規模較大,直接租用正式憑證會省下不少溝通成本。