這篇關於codesign的文章不錯 https://www.objccn.io/issue-17-2/ 英文原文:https://www.objc.io/issues/17-security/inside-code-signing/
第二篇 http://blog.cnbang.net/tech/3386/
首先ipa重簽名並不是一個程序發布的標准途徑,多用於特殊情況。作用就是讓因簽名問題無法運行在真機上的ipa能在真機上順利運行。
因為我要在ios10上測試,而網上的教程大多比較早,曾經一度認為這種重簽名方法在ios10上失效了,結果是自己沒有用對命令。
重簽名的核心就是運行codesign命令,不但需要簽名程序,還需要對ipa中的embeded framework進行簽名(沒使用就不用了),否則會閃退。
詳細的步奏我就不寫了,請參考:http://stackoverflow.com/questions/6896029/re-sign-ipa-iphone
這里列出2個關鍵命令:
//重簽名應用
/usr/bin/codesign -f -s "iPhone Distribution: xxx" --entitlements entitlements.plist Payload/12121212.app //重簽名應用中的embeded framework(如果存在的話就需要運行) /usr/bin/codesign -f -s "iPhone Distribution: xxx" --entitlements entitlements.plist Payload/12121212.app/Frameworks/*
對framrworks中的文件簽名,再利用codesign 命令查看,可以有如下截圖
MacBook-Air:test Rufus$ codesign -vv -d /Users/Rufus/Desktop/test/Payload/12121212.app/Frameworks/libswiftDarwin.dylib Executable=/Users/Rufus/Desktop/test/Payload/12121212.app/Frameworks/libswiftDarwin.dylib Identifier=com.apple.dt.runtime.swiftDarwin Format=Mach-O universal (armv7 armv7s arm64) CodeDirectory v=20200 size=1024 flags=0x0(none) hashes=24+5 location=embedded Signature size=4714 Authority=iPhone Distribution: xxx(隱藏了) Authority=Apple Worldwide Developer Relations Certification Authority Authority=Apple Root CA Signed Time=24 Apr 2017, 1:34:52 PM Info.plist entries=5 TeamIdentifier= xxx(隱藏了) Sealed Resources=none Internal requirements count=1 size=200
可以看到,這個dylib被順利簽名了。
另外,對12121212.app的簽名,其實就是對其中的2進制文件12121212進行了簽名,還順便計算了一下app中的其他文件的hash值,保存在了_CodeSignature下的CodeResources文件中。
codesign命令針對的就是 可執行2進制文件 和 動態鏈接庫文件,其他的比如png,nib等資源文件,一概不會codesign。
這里 entitlements.plist 比較關鍵,先貼出一個基本的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>application-identifier</key> <string>xxx.com.xxx.xxx</string> <key>get-task-allow</key> <false/> </dict> </plist>
如果ipa中使用了keychain,還需要向entitlements加入下面這對鍵值,並且,要修改info.plist文件里的bundle id。
<key>keychain-access-groups</key> <array> <string>xxx.com.xxx.xxx</string> </array>
說到這里,重簽名能夠把別人ipa重新簽名,當成自己的ipa向app store 發布嗎?我們測試一下
測試過程如下,我使用公司的賬戶簽名一個企業級應用,之后使用自己的帳號吧這個ipa重簽名,並上傳到app sotre。
由於xcode默認只支持使用xarchiver 上傳,而我們這里是ipa文件,所以我們使用Application Loader上傳。
首先,我使用了最基本的重簽名方式,即:沒有改變ipa的bundle id。上傳出現了錯誤:
看來必須把bundle id改了才行。修改info.plist中的bundle id后,再次上傳,進行到了下面的步奏,卡住不動了:
經過了漫長app上傳和校驗過程,返回了錯誤:
ERROR ITMS-90035: "Invalid Signature. A sealed resource is missing or invalid. Make sure you have signed your application with a distribution certificate, not an ad hoc certificate or a development certificate. Verify that the code signing settings in Xcode are correct at the target level (which override any values at the project level). Additionally, make sure the bundle you are uploading was built using a Release target in Xcode, not a Simulator target. If you are certain your code signing settings are correct, choose "Clean All" in Xcode, delete the "build" directory in the Finder, and rebuild your release target. For more information, please consult https://developer.apple.com/library/ios/documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html"
沒什么有意思的信息,就是提示簽名是有問題的,無法上傳app store。
要明白為什么蘋果認為這個ipa是非法的,就需要仔細研究一下ipa簽名的過程了。