iOS signature principle

1. Code signing

So let's analyze what it needs:

  1. The installation package does not need to be uploaded to App Store, and can be installed directly on the phone.

  2. In order to ensure the security of the system, Apple must have absolute control over the installed APP

  3. Can only be installed with Apple's permission

  4. It cannot be abused to cause non-development apps to be installed

  5. In order to achieve these requirements, the complexity of iOS signatures began to increase, and Apple's solution here is a two-layer signature.

Signature flow chart: image.pngHere is a brief overview of the two-tier code signing process for iOS. This is not the final iOS signature principle. The final signature of iOS needs to add a little something on this basis.

First of all, there are two roles here. One is the iOS system and the other is our Mac system. Because the iOS APP development environment is under the Mac system, this dependency becomes the basis of Apple's double-layer signature.

  1. Generate a pair of public key\private key of asymmetric encryption algorithm in Mac system (your Xcode will help you do it). Here it is called public key M private key M . M = Mac

  2. Apple itself has a fixed pair of public and private keys. It is the same as the previous App Store principle. The private key is in the background of Apple, and the public key is in each iOS system. Here it is called public key A and private key A. A=Apple

  3. Send the public key M and some information about your developer to the Apple backend (this is the CSR file), and use the private key A in the Apple backend to sign the public key M. Get a piece of data including the public key M and its signature, and call this piece of data a certificate.

  4. During development, after compiling an APP, sign the APP with the local private key M (the P12 you will export in the future), and at the same time pack the certificate obtained in the third step into the APP and install it on the mobile phone.

  5. During installation, the iOS system obtains the certificate, and verifies whether the digital signature of the certificate is correct through the built-in public key A of the system.

  6. After verifying the certificate, make sure that the key M is certified by Apple, and then use the public key M to verify the signature of the APP, which indirectly verifies whether the installation of the APP is officially approved by Apple. (Here, only the installation behavior is verified, and whether the APP has been changed is not verified, because the content of the APP is always changing during the development stage, and Apple does not need to take care of it.)

With the above process, the authentication of the developer and the security of the program can already be guaranteed. However, you need to know that the main channel for iOS programs is to distribute to user devices through the APP Store. If only the above-mentioned process is the only way, wouldn’t it be possible to install it on all iOS devices after applying for a certificate?

2. Generation of description files

In order to solve the problem of app abuse, Apple added two more restrictions.

第一限制在苹果后台注册过的设备才可以安装.

第二限制签名只能针对某一个具体的APP.

并且苹果还想控制App里面的iCloud/PUSH/后台运行/调试器附加这些权限,所以苹果把这些权限开关统一称为Entitlements(授权文件).并将这个文件放在了一个叫做Provisioning Profile(描述文件)文件中.
    
描述文件是在AppleDevelop网站创建的(在Xcode中填上AppleID它会代办创建),Xcode运行时会打包进入APP内. 所以我们使用CSR申请证书时,我们还要申请一个东西!! 就是描述文件!
    
在开发时,编译完一个 APP 后,用本地的私钥M对这个APP进行签名,同时把从苹果服务器得到的 Provisioning Profile 文件打包进APP里,文件名为embedded.mobileprovision,把 APP 安装到手机上.最后系统进行验证。
    
复制代码

3. Re-signature

1. Coding re-signature

You can use the pp assistant to download the app that has been shelled. Or obtain ipa by yourself through jailbreak tools, smashing tools, etc.

Xocde provides a signature tool, codesign, we can complete the re-signature with a few commands

coding command (used to re-sign ipa):

$security find-identity -v -p codesigning 列出钥匙串里可签名的证书

$Codesign –fs “证书串” 文件名   强制替换签名

$Chmod +x 可执行文件   给文件添加权限

$security cms -D -i ../embedded.mobileprovision 查看描述文件

$codesign -fs “证书串” --no-strict --entitlements=权限文件.plist APP包

$Zip –ry 输出文件 输入文件  将输入文件压缩为输出文件 


复制代码

2. Xcode re-signature

  1. Remove plugins and .app bundles with plugins (like Watch)

  2. Re-sign the library in Frameworks

  3. Give the executable +x (executable) permission

  4. Add a description file (create a new project and compile it on the real machine)

  5. Replace BundleID

  6. Re-sign the .app package through the authorization file (Entilements)

shell

shell是一种特殊的交互式工具, which provides a way for users to start programs, manage files in the file system, and processes running on the system. Shell generally refers to a command-line tool. It allows you to enter text commands, which are then interpreted and executed in the kernel. Shell script, that is, a script file that is pre-put into a text file with various commands for one-time execution.

$source FileName	
//意思:在当前shell环境中读取并执行FileName中的命令
//特点:
//命令可以强行让一个脚本去立即影响当前的环境(一般用于加载配置文件)。
//命令会强制执行脚本中的全部命令,而忽略文件的权限。

$bash FileName  、  $zsh FileName   
//意思:重新建立一个子shell,在子shell中执行脚本里面的句子。

$./FileName
//意思:读取并执行文件中的命令。但有一个前提,脚本文件需要有可执行权限。

复制代码

image.png

image.png

Change permissions: chmodimage.png

Four. Summary

  1. Code signing: In order to achieve these requirements, the complexity of iOS signatures begins to increase. Apple’s solution here is a two-layer signature
  2. Re-signature: Xocde provides a signature tool, codesign, we can complete the re-signature with a few commands, , Shell脚本that is, a script file that is pre-put into a text file with various commands for one-time execution.

Guess you like

Origin juejin.im/post/7228504953814532133