Sign in Join
Wiki copytemporary

Code Signing for macOS: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Code Signing for macOS

OldNewLine
1+> **Note:** For Apple Notarization requirements for kernel extensions and applications from Mojave 10.14.5 onwards (for kernel extensions from 7 April 2019 and for developers whose first use of their code signing certificate occurred from 7 April 2019) and for all software from Catalina 10.15 onwards that is not distributed via the App Store, see [[Notarization for macOS 10.14.5+]]
2+
3+## Introduction
4+
5+**[[Code signing]]** ensures both authenticity and integrity of executables that have been downloaded from wide area networks like the Internet. The discussion below applies equally to App Store distribution and distribution outside the App Store.
6+
7+Code signing is required in [[Portal:iOS|iOS]]. On [[Portal:Mac|macOS]] 10.7 and later, it enables programs downloaded from the Internet to be opened without any warnings (if they are signed with an Apple-issued certificate) and it is required when using certain functionality (eg APIs used by debuggers; note that in this case a self-signed certificate that is marked as "trusted" suffices). This functionality is performed by Apple's Gatekeeper software.
8+
9+## The evolution of Gatekeeper
10+
11+Gatekeeper, first introduced in Mountain Lion (10.8, 2012), is a Mac security feature that was designed to protect Apple computers from malicious software. Gatekeeper checks applications against the list of apps that Apple has approved for its App Store or have been code signed by developers who have Apple-issued certificates where the application is not offered through the app store. It does not perform any safety checks by itself, other than that the application wasn't changed since the developer signed it, nor does it offer any guarantees about the developer other than that they are paying Apple \$US 99 per year (aka an "Identified Developer").
12+
13+The original Gatekeeper options introduced in Mountain Lion, accessed from Preferences \> Security and Privacy \> General, were:
14+
15+- App Store
16+- App Store and Identified Developers
17+- Anywhere
18+
19+By choosing the **Anywhere** option, the user was once able to entirely disable Gatekeeper. The default setting only allowed the launching of applications from the App Store or from a developer who had signed their application with an Apple-issued certificate.
20+
21+When macOS Sierra was released in 2016, Apple made some important changes to Gatekeeper and limited the the Gatekeeper options to:
22+
23+- App Store
24+- App Store and Identified Developers
25+
26+However, you can restore the missing Gatekeeper *Anywhere*' option in Preferences by opening a terminal and executing the command:
27+
28+ $ sudo spctl --master-disable
29+
30+which still works up to and including macOS Catalina. The better, as in more secure, alternative was to instead bypass Gatekeeper by opening the application from the right-click context menu or by control clicking on the application. This still triggered the alert dialog but it now contained an **Open** button to successfully launch the application. This method of bypassing Gatekeeper still works in macOS Catalina, except that on Catalina you need to launch the application a second time with the control-click or right-click to successfully bypass Gatekeeper - the first time you launch the application Gatekeeper cannot be bypassed whether you control-click or right-click or not.
31+
32+### Big Sur and later on Apple M1 ARM64 processors
33+
34+And then Apple changed the game.
35+
36+When Big Sur is running on an Apple M1 (ARM64) processor, **all native ARM64 code must be signed or the operating system prevents its execution**; and this new Gatekeeper requirement cannot be bypassed by users. Developers can bypass it by running the following command in an Applications \> Utilities \> Terminal window:
37+
38+`spctl developer-mode enable-terminal`
39+
40+and then going to System Preferences \> Security & Privacy \> Privacy, scrolling down until you see an entry for "Developer Tools", authenticate and then tick the checkbox next to Terminal.
41+
42+Intel executables being run under Big Sur on an Apple M1 processor via Rosetta 2 can still be run even though they are unsigned using the control-click or right-click method to bypass Gatekeeper.
43+
44+For more details, see: [[macOS Big Sur changes for developers|Big Sur changes for developers: Code signing - Intel vs M1]].
45+
46+## Gatekeeper Dialogs
47+
48+<File:load> security warning dmg leopard en.png\|Simple warning after opening a downloaded application in Leopard. <File:load> security warning dmg mavericks en.png\|Default settings are more strict in Mavericks, where they prevent downloaded unsigned apps from being started via double-clicking. <File:load> security warning mavericks context en.png\|If the application is launched via context menu in Mavericks a simple warning is displayed similar to that of earlier versions. <File:simple> load security warning mavericks en.png\|Gatekeeper shows a simplified warning, if a code-signed app has been downloaded and launched for the first time. In subsequent launches the program is starts immediately. <File:codesigning.png%7COverview> of code-signing a program written with Lazarus and Free Pascal
49+
50+## Overview
51+
52+The basic steps to sign an application that has been written with Lazarus and/or Free Pascal are:
53+
54+1. Obtain a Developer ID from Apple and install it in your system's key chain.
55+2. Note the alphanumeric key of your Developer ID (aka TeamIdentifier).
56+3. Sign your application with the *codesign* command.
57+4. Sign your installer pkg with the *productsign* command.
58+
59+It is **not possible** to use certificates from third-party providers like Comodo because they will not pass Gatekeeper which requires an Apple developer issued certificate. Also note that you cannot sign Windows applications with the Apple developer certificate (this time you do need a third-party Comodo etc certificate).
60+
61+A macOS application distributed outside the Mac App Store will generally have 3–4 separate layers of signing:
62+
63+- The application itself will be code signed using an application certificate.
64+- If the application has an installer, the package file will be signed using an installer certificate.
65+- The disk image containing the application or installer will be signed using an application certificate.
66+- The disk image will [[Notarization for macOS 10.14.5+|notarized]], and then the ticket generated by the notary service will be [[Notarization for macOS 10.14.5+|stapled]] to it.
67+
68+## Gatekeeper requirements
69+
70+- Your application should be standalone with no unacceptable external dependencies. The only acceptable external dependencies are system libraries. All other dependencies should be copied to your MyApp.app bundle folder. Gatekeeper rejects any application that has non-system external dependencies.
71+
72+- All binary files inside MyApp.app should be code signed.
73+
74+- All binary files should be located in standard locations inside the MyApp.app bundle folder. Refer to the table below.
75+
76+| Location | Description |
77+|----|----|
78+| Contents | Top content directory of the bundle |
79+| Contents/MacOS | Main executable; helper apps and tools |
80+| Contents/Frameworks | Frameworks, dylibs |
81+| Contents/PlugIns | Plug-ins, both loadable and extensions |
82+| Contents/XPCServices | XPC services |
83+| Contents/Helpers | Helper apps and tools |
84+| Contents/Library/Automator | Automator actions |
85+| Contents/Library/Spotlight | Spotlight importers |
86+| Contents/Library/LoginItems | Installable login items |
87+| Contents/Library/LaunchServices | Privileged helper tools installed by the ServiceManagement framework |
88+
89+Standard locations for code inside a bundle
90+
91+Note: No non-binary files should ever be located in the folders specified in the table above.
92+
93+## Using codesign to sign your application
94+
95+**1.** Sign your application with:
96+
97+``` bash
98+codesign -f -o runtime --timestamp -s "Developer ID Application: YOUR NAME (TEAM_ID)" /path/to/bundle.app
99+```
100+
101+**2.** Display basic information about the result of the signing process:
102+
103+``` bash
104+codesign -dv -r- /path/to/bundle.app
105+```
106+
107+If your application was successfully signed this command will return information similar to this:
108+
109+ Executable=/Users/apple/Desktop/your.app/Contents/MacOS/yourappname
110+ Identifier=org.yourdomain.yourappname
111+ Format=app bundle with Mach-O thin (x86_64)
112+ CodeDirectory v=20500 size=101470 flags=0x10000(runtime) hashes=3164+3 location=embedded
113+ Signature size=9063
114+ Timestamp=7 Dec 2019 at 18:36:08
115+ Info.plist entries=16
116+ TeamIdentifier=<10 alpha numeric digits>
117+ Runtime Version=10.14.0
118+ Sealed Resources version=2 rules=13 files=45
119+ designated => identifier "org.yourdomain.yourappname" and anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] /* exists */ and certificate leaf[field.1.2.840.113635.100.6.1.13] /* exists */ and certificate leaf[subject.OU] = <10 alpha numeric digits>
120+
121+**3.** Verify your signature:
122+
123+``` bash
124+codesign -vv /path/to/bundle.app
125+```
126+
127+If your app was successfully signed this command will return the strings "valid on disk" and "satisfies its Designated Requirement", respectively, after the path to your application.
128+
129+## Using productsign to sign your pkg installer
130+
131+**1.** Sign your installer pkg file with:
132+
133+``` bash
134+productsign --timestamp --sign "Developer ID Installer: YOUR NAME (TEAM_ID)" /path/to/unsigned.pkg /path/to/signed.pkg
135+```
136+
137+**2.** Verify your signature with:
138+
139+``` bash
140+spctl -vv --assess --type install /path/to/your.pkg
141+```
142+
143+which should yield information similar to this if it was successful:
144+
145+ your.pkg: accepted
146+ source=Developer ID
147+ origin=Developer ID Installer: YOUR NAME (<10 alpha numeric digits>)
148+
149+## Using codesign to sign your disk image
150+
151+Beginning in macOS 10.11.5, you can apply a code signature to read-only, compressed disk images that you use to distribute content. In this case you do not need to separately sign your application.
152+
153+**1.** Sign your disk image with:
154+
155+``` bash
156+codesign -f -o runtime --timestamp -s "Developer ID Application: YOUR NAME (TEAM_ID)" /path/to/YourImage.dmg
157+```
158+
159+**2.** Verify your signature with:
160+
161+``` bash
162+spctl -a -t open --context context:primary-signature -vv /path/to/YourImage.dmg
163+```
164+
165+which should yield information similar to this if it was successful:
166+
167+ YourImage.dmg: accepted
168+ source=Developer ID
169+ origin=Developer ID Application: YOUR NAME (<10 alpha numeric digits>)
170+
171+**After 3 February 2020 Apple is fully enforcing notarization** for macOS 10.14.5+ (Mojave) with the consequence that the above verification of your disk image will now return information similar to this:
172+
173+ YourImage.dmg: rejected
174+ source=Unnotarized Developer ID
175+ origin=Developer ID Application: YOUR NAME (<10 alpha numeric digits>)
176+
177+See [[Notarization for macOS 10.14.5+]] for more details.
178+
179+## Updating a signed file
180+
181+Whenever you update a signed file, create a new file. Do not copy a new signed file over an old signed file.
182+
183+Specifically, the code signing information (code directory hash) is hung off the vnode within the kernel, and modifying the file behind that cache will cause problems. You need a new vnode, which means a new file, that is, a new inode. Documented in WWDC 2019 Session 703 All About Notarization - see [slide 65 (PDF)](https://devstreaming-cdn.apple.com/videos/wwdc/2019/703yqvyjv9mi5ur6/703/703_all_about_notarization.pdf).
184+
185+## Ad hoc signing
186+
187+This is especially important on ARM64 M1 Apple processors which require all native code to be validly signed (if only ad hoc) or the operating system will not execute it, instead killing it on launch. To ad hoc sign an application:
188+
189+` codesign --force --deep -s - lazarus.app`
190+
191+or to ad hoc sign an executable file:
192+
193+` codesign --force -s - lazarus`
194+
195+A single dash/hyphen on its own for the identity makes it an ad hoc signature with no certificate.
196+
197+You can check the validity of an ad hoc signed executable with:
198+
199+` codesign -dv -r- lazarus`
200+
201+which should produce output similar to the below:
202+
203+`Executable=/Users/trev/lazarus-src/lazarus`\
204+`Identifier=lazarus`\
205+`Format=Mach-O thin (arm64)`\
206+`CodeDirectory v=20400 size=336137 flags=0x2(adhoc) hashes=10498+2 location=embedded`\
207+`Signature=adhoc`\
208+`Info.plist=not bound`\
209+`TeamIdentifier=not set`\
210+`Sealed Resources=none`\
211+`# designated => cdhash H"2e6f474452344d6fa42feee3db14a05c14d595a3"`
212+
213+In cases where you have compiled the executable yourself, the macOS linker will ad hoc sign the executable for you in which case you need do nothing. You can check the validity with the command above in which case the CodeDirectory output line will indicate that the linker signed the executable with an ad hoc signature as follows::
214+
215+` CodeDirectory v=20400 size=336032 flags=0x20002(adhoc,linker-signed) hashes=10498+0 location=embedded`
216+
217+## Code signing an x86_64 binary on an aarch64 machine
218+
219+## How to install certificates on a second computer
220+
221+There are two ways to do this:
222+
223+1. using the Keychain Access application; and
224+2. using Xcode.
225+
226+### Using the Keychain Access application
227+
228+- On the first computer which has the certificates, open the Keychain Access application and create a new keychain. Now select your existing login keychain, and then My Certificates. Your certificates should be shown on the right. There should be a disclosure arrow on the left side, and selecting that should reveal your private key. Select each certificate, and copy and paste (do not drag or you'll remove it from your login keychain) it into your new keychain.
229+
230+- Copy the newly created keychain from your first computer to your second computer, open it with the Keychain Access application and copy/paste/drag the certificate to your second computer's login keychain.
231+
232+### Using Xcode
233+
234+- On the first computer which has the certificates, open Xcode and go to Preferences \> Accounts, select your account, select the gear symbol in the bottom left, choose Export Apple ID and Code Signing Assets, and then save the file.
235+
236+- Copy the developer profile file you saved on your first computer to the second computer, open Xcode, go to Preferences \> Accounts, select your account, select the gear symbol in the bottom left, and then choose Import Apple ID and Code Signing Assets.
237+
238+## How to check what certificates are installed
239+
240+Once you have created or imported the certificates on your computer, you can check for their presence by opening an Applications \> Utilities \> Terminal and executing the following command:
241+
242+`$ security find-identity -p basic -v`
243+
244+This command will list all available certificates on this computer. Make sure that you can see the presence of the *Developer ID Application* and *Developer ID Installer* certificates. If you are a member of more than one team, you may see multiple certificates for each team.
245+
246+You can identify the certificates by the hexadecimal number or by the descriptive name, eg "Developer ID Application: Your Name (AB123456DE)"
247+
248+The ten character alpha-numeric code at the end of your name is your Developer Team ID. If you are a member of multiple developer teams, you will have multiple certificates and the team ID will help you distinguish them.
249+
250+## See also
251+
252+- [[macOS Big Sur changes for developers|Code signing: Intel vs Apple Silicon]]
253+- [[Apple Developer Certificates]]
254+- [[Notarization for macOS 10.14.5+]]
255+- [[Hardened runtime for macOS]]
256+- [[Sandboxing for macOS]]
257+- [[Signing and Notarization FAQ]]
258+- [[macOS Gatekeeper Debugging Tips]]
259+
260+## External links
261+
262+- [Apple Developer portal](https://developer.apple.com/)
263+- [Apple: Code Signing Overview](https://developer.apple.com/support/technical/code-signing/)
264+- [Apple: Code Signing Guide](https://developer.apple.com/library/mac/documentation/Security/Conceptual/CodeSigningGuide/)
265+- [Apple: TN2206 - macOS Code Signing In Depth](https://developer.apple.com/library/archive/technotes/tn2206/_index.html)
266+- [Apple: Code signing x86_64 binaries on aarch64](https://developer.apple.com/forums/thread/673323).
267+
268+---
269+*From the Free Pascal wiki, page "Code Signing for macOS", as it stood on 2022-02-13 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history