macOS 开发 - Notarization 公证你的 Developer ID 应用


一、Notarization 公证 简介

相关新闻:
https://developer.apple.com/cn/news/?id=09032019a

10.15 上,新下载的应用如果没有经过公证,将无法使用。(gatekeeper 将会弹窗提示:“无法打开 xxx ,因为无法检查其是否包含恶意软件”)。此前已安装的应用还可以使用。
2020 年1月后,已安装的应用如果没有公证,也无法使用。

公证


关于Notarization 机制的由来和原理,这篇文章写得很好了,大家可以去阅读:《嘶吼RoarTalk:所有开发人员都注意了!苹果将正式采用Notarization机制》
http://app.myzaker.com/news/article.php?pk=5cdb6b0677ac645d143d5e32


关于如何 Notarizing,官方也有详细说明:

– Notarizing Your App Before Distribution
https://developer.apple.com/documentation/xcode/notarizing_your_app_before_distribution?language=objc

– Upload a macOS app to be notarized
https://help.apple.com/xcode/mac/current/#/dev88332a81e

你还可以用脚本自定义公证流程:
https://developer.apple.com/documentation/xcode/notarizing_your_app_before_distribution/customizing_the_notarization_workflow


总结大概如下:

在 2020 年 1 月后,从 10.14.5 开始,所有使用 Developer ID 签名的新的的或更新的内和扩展、软件都需要被认证才能运行。
10.15 之后,软件默认都需要认证。

如果您的 Mac 设置为允许安装来自 App Store 和被认可开发者的应用,则第一次启动来自被认可开发者的应用时,Mac 会询问您是否确定要打开这个应用。
经过 Apple 公证的应用表示已经通过了安全检查:
:


没有经过公证的应用会显示一个黄色警告图标:
在这里插入图片描述


需要认证一下几种类型的软件

  • macOS apps
  • Non-app bundles, such as kernel extensions
  • Disk images (UDIF format)
  • Flat installer packages

Notarization 不是App Review,会检查是否有恶意内容和签名问题,并迅速返回结果给你。如果没有问题,notary service 会为应用添加了一个 ticket,并发布到网上。通过ticket, Gatekeeper 知道这是个被公证 的应用。

如果 Developer ID 签名秘钥泄露了,认证服务也可以帮助你审计跟踪使用你的秘钥签名 分发的应用。如果你发现了你的产品的 不受信任的 版本,你可以通知苹果来取消这些版本的关联 tickets.

下文做一个简单的记录


方法一:使用 Xcode 公证

1、使用 Developer ID 签名你的应用

关于证书,可参考:https://blog.csdn.net/lovechris00/article/details/84848734


2、Enable hardened runtime

The hardened runtime :强化版运行时

设置需要以下条件:macOS 10.13.6 及以上, Xcode 10 及以上;
测试则必须在 10.14 及以上版本。


工程 – target – Capabilities – Hardened Runtime
勾选需要的Access 后,access 将会声称在 entitlements 中。
在这里插入图片描述


3、entitlement 添加 com.apple.security.get-task-allow

entitlement.plist 中添加 com.apple.security.get-task-allow 这个键,设置为 YES


关于 get-task-allow
http://www.pianshen.com/article/5311177849/
https://stackoverflow.com/questions/1003066/what-does-get-task-allow-do-in-xcode

get-task-allow, when signed into an application, allows other processes (like the debugger) to attach to your app.
Distribution profiles require that this value be turned off, while development profiles require this value to be turned on (otherwise Xcode would never be able to launch and attach to your app).

get-task-allow is typically added by Xcode automatically for development builds.
You can disable this default behaviour via Code Signing Inject Base Entitlements (CODE_SIGN_INJECT_BASE_ENTITLEMENTS) build setting.
Keep in mind that, like most entitlements, get-task-allow must be whitelisted by your provisioning profile, and only Development profiles have it so whitelisted.


4、Archive

Distribute App --> Developper ID --> Upload --> Review --> Loading


在这里插入图片描述


关于多 target 应用
archive 除开,可能不属于 iOS App, 也不属于 macOS App,变成了Other Items。
此时,设置主target 外其他target 的 Skip Install 为 YES 即可。


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


5、公证成功

收到一封邮件提示我认证成功,点击上图中的 export 按钮即可导出我们公证过的应用了。
在这里插入图片描述***

方法二:终端命令公证 .zip、.pkg 和 .dmg

部分已打包的文件,如pkg,使用上述方法不太方便,可使用终端公证,命令如下:

1、上传公证应用

$ xcrun altool --notarize-app --primary-bundle-id "com.ms.lucky" --username "[email protected]" --password "hjsa-efsb-usne-wdsa" --file /.../demo_v2.3.pkg

上传的过程会需要3–5分钟,会返回命令:

No errors uploading '/.../demo_v2.3.pkg'.
RequestUUID = 3c8sds9-9a23-40ds2-ddasd1-628dsda2e79

这并不代表公证成功,你需要等待邮件。


关于密码

注意,password 后面接的密码,并非AppleID 账号密码。需要到 https://appleid.apple.com 重生成,如下图所示:
在这里插入图片描述


否则可能会报如下错误:

Error: Unable to validate your application. 
Please sign in with an app-specific password. 
You can create one at appleid.apple.com.

2、staple 给应用盖章

$ xcrun stapler staple /.../demo_v2.3.pkg

成功后将返回

Processing: /.../demo_v2.3.pkg
Processing: /.../demo_v2.3.pkg
The staple and validate action worked!


查看盖章信息:

$ xcrun stapler staple -v  /.../demo_v2.3.pkg

可以在此看到 signedTicket 等信息


PS : 这个方式不能公证 .app 文件,否则会提示

*** Error: Is a directory The operation couldn’t be completed. Is a directory

公证问题小结

1、终端输入公证命令后,提示 You must first sign the relevant contracts online

原因:开发者中心有新的协议没有通过。
解决办法:登录 https://appstoreconnect.apple.com/ 来同意协议

在这里插入图片描述


2、邮件提示公正失败,查错

在终端使用如下格式命令,去获取日志信息,一般日志为一个连接

$ xcrun altool --notarization-info  f3sd8c2a-0be1-4d2b-bb61-33e34fda66f1 -u "[email protected]"   --password "dcdp-uaxu-ojxo-diad"

Date: 2019-10-22 07:31:38 +0000
          Hash: 5fc0417db360c042a6d8fb72625561f2bbeae8ec137046f11b7a75b26f814f00
    LogFileURL: https://osxapps-ssl.itunes.apple.com/itunes-assets/Enigma113/v4/22/fc/ce/22fccead-b8a2-e252-f37c-3540d849ae82/developer_log.json?accessKey=xxx
   RequestUUID: ac7e65ea-f400-448f-9cf6-f052b3a5e1be
        Status: invalid
   Status Code: 2
Status Message: Package Invalid

常见问题有
"The signature of the binary is invalid." 去给这个包签名
"The executable does not have the hardened runtime enabled." 去给这个包加上强化运行时


3、10.14 后跑不起来

10.14 比 10.15 更严格,包里面的所有元素都要签名。


4、其他厂商多target应用

有些 app包不是我们开发的,难以在xcode 中设置强化运行时,可以用如下命令来签名:

$ codesign -fs "Developer ID Application: xxx (Shenzhen) Network Co., Ltd. (xxx)"  --options=runtime  a.bundle

在Xcode 中勾选强化运行时,会有多个选项来选择;代码这样签名,只是开启了强化运行时,那些选项就无从选择了。但这样可能会造成部分功能失效。
可以观察到,Xcode 勾选的选项会生成到 entitlements.plist 中,功能失效是否和 entitlements 授权相关?那么可否手动设置 entitlements?
答案必须是可以。


1)查看包中的 entitlement

$ codesign -d --entitlements - demo.app

如果没有设置,则为空;Hardened Runtime 全勾选上,就会有下面的数据效果:


2)获取 entitlements.plist 数据

你可以将它保存为 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>com.apple.security.automation.apple-events</key>
        <true/>
        <key>com.apple.security.cs.allow-dyld-environment-variables</key>
        <true/>
        <key>com.apple.security.cs.allow-jit</key>
        <true/>
        <key>com.apple.security.cs.allow-unsigned-executable-memory</key>
        <true/>
        <key>com.apple.security.cs.debugger</key>
        <true/>
        <key>com.apple.security.cs.disable-executable-page-protection</key>
        <true/>
        <key>com.apple.security.cs.disable-library-validation</key>
        <true/>
        <key>com.apple.security.device.audio-input</key>
        <true/>
        <key>com.apple.security.device.camera</key>
        <true/>
        <key>com.apple.security.get-task-allow</key>
        <true/>
        <key>com.apple.security.personal-information.addressbook</key>
        <true/>
        <key>com.apple.security.personal-information.calendars</key>
        <true/>
        <key>com.apple.security.personal-information.location</key>
        <true/>
        <key>com.apple.security.personal-information.photos-library</key>
        <true/>
    </dict>
</plist>


3)使用 entitlement 对 app 重签名

$ codesign -f -s "iPhone Distribution: XXX" --entitlements entitlements.plist demo.app

Mac 上的常见的安全机制也顺便在这里做个小总结

二、Gatekeeper 门禁

官方介绍:
https://support.apple.com/zh-cn/HT202491

Gatekeeper是 macOS 10.7 Mountain Lion 中引入的一项新安全技术 ,它可保证用户安装来自Mac App Store或者拥有开发者签名的应用,从而防止一些恶意软件的进入。

GateKeeper 会在打开 App Store 外部的 Mac 应用、插件和安装器软件时,macOS 会检查 Developer ID 签名和公证状态,以验证软件是不是来自获得认可的开发者并且没有遭到改动。
可能会在以下情况产生提示:

  • 安全性与营私设置为只允许 App Store,而打开非 App Store 应用时;
  • 应用没有签名:是从互联网下载的应用,您确定要打开它吗?
  • 应用没有公正:有黄色警告图标 ⚠️
  • 检测到恶意应用

要打开非 App Store 下载的应用,需要在 安全与隐私面板 设置:
在这里插入图片描述***

显示 允许任何来源

macOS 10.12 之后,安全性与隐私面板默认没有了 允许任何来源,想要打开的话,需在终端输入如下命令:

$ sudo spctl --master-disable

为了安全考虑,你也可以在安装完了你需要的软件之后,重新关闭掉,命令如下:

$ sudo spctl --master-enable

三、关于 Rootless & SIP

SIP : System Integrity Protection, 系统完整性保护

OSX 10.11 El Capitan(或更高)新添加了 SIP 这个安全机制 。这个机制下,以下目录仅供系统使用,其它用户或者程序无法直接使用

  • /System
  • /sbin
  • /usr 不包含 /usr/local/

当你安装一些需要操作这三个目录的包的时候就会出现 Operation not permitted 之类的错误,即使 sudo 授权依然无效。


1、查看是否开启了Rootless

在终端输入如下命令即可

$ csrutil status

如果显示的是System Integrity Protection status: disabled.则表示没有开启 SIP。

如果显示的是System Integrity Protection status: enabled, 则表示已开启。


2、关闭/开启 Rootless

https://blog.csdn.net/qq285744011/article/details/82219340

重启Mac,在听到经典的启动声后,按下command+R进入恢复模式,在菜单栏中 实用工具 找到 终端(Terminal) ,输入如下指令

$ csrutil disable; reboot

csrutil enable 是开启。

电脑会重新启动,进入系统后可以使用第一步的方法,可以查看是否关闭成功。


进入恢复模式的具体步骤:

  1. 关机

  2. 开机,马上按着Command+R键不放,大概10秒,出现苹果图标+加载进度条,放手,进入恢复模式(要等几分钟左右)
    (如果你看到的是“在转的地球图标”,请马上松手,并按住开机按钮不放,强制关机,再开机,按着Option不放,选择出现的“恢复-10.xx”,确定即可)

  3. 成功进入本地恢复模式,依次打开:左上角的 实用工具 --> 终端

  4. 在命令行中输入csrutil disable 确定,系统提示“successfully …”,成功关闭SIP机制,重启即可。

发布了164 篇原创文章 · 获赞 162 · 访问量 65万+

猜你喜欢

转载自blog.csdn.net/lovechris00/article/details/102309757
今日推荐