Sub topic 12
Verifying signatures with codesign, spctl and pkgutil
This chapter assumes you can open Terminal and run a command, and that you know what code signing, notarization and Gatekeeper are. You will use three built-in tools to check a download yourself instead of relying only on the first-launch prompt. The examples use Example.app and Example.pkg as placeholders. Replace them with your own paths.
What each tool answers
Each tool answers a different question, so you usually run more than one.
| Tool | Question it answers |
|---|---|
| `codesign` | Is the signature intact, and who signed it? |
| `spctl` | Would Gatekeeper allow this app to run? |
| `xcrun stapler` | Is a notarization ticket attached? |
| `pkgutil` | Is this installer package signed, and by whom? |
Check that the signature is intact
Run codesign with the verify option on an app you copied to Applications.
codesign --verify --deep --strict --verbose=2 /Applications/Example.appIf the signature is valid, the command prints valid on disk and satisfies its Designated Requirement. If any file inside the app changed after signing, it prints an error and exits with a non-zero status. A valid result proves the contents are unmodified since signing. It does not prove the signer is trustworthy.
Read who signed it
To see the signer, display the signature details and filter for the useful lines.
codesign -dv --verbose=4 /Applications/Example.app 2>&1 | grep -E 'Authority|TeamIdentifier'The codesign -d output goes to standard error, which is why the command redirects it with 2>&1 before filtering. Look at two things:
- The
Authoritylines list the certificate chain. A Developer ID app showsDeveloper ID Application: Name (TEAMID), thenDeveloper ID Certification Authority, thenApple Root CA. TeamIdentifieris the developer's Team ID. Compare it with the developer's published details.
Ask Gatekeeper's policy
spctl shows what Gatekeeper would decide.
spctl --assess --type execute --verbose=4 /Applications/Example.appAn accepted app prints accepted and a source= line. Typical sources are Mac App Store and Notarized Developer ID. A source of Developer ID without "Notarized" means the app is signed but not notarized. A rejected result means Gatekeeper would block it.
To check for a notarization ticket attached to the app, use the stapler tool.
xcrun stapler validate /Applications/Example.appA stapled ticket lets Gatekeeper accept the app without an internet connection. A missing ticket is not always a problem, because macOS can look up notarization online.
Inspect an installer package
Installer packages can run scripts with administrator rights, so check them before you open them.
pkgutil --check-signature ~/Downloads/Example.pkgThe output reports the status, such as signed by a developer certificate issued by Apple, and the certificate chain. If it says no signature, treat the package with extra caution.
Common errors
- Checking the wrong thing. A valid signature from an unknown developer is still a risk.
- Running
spctlon a file in a disk image you have not mounted. Mount the image first, then point the tool at the app inside. - Reading
rejectedas proof of malware. Old or unsigned legitimate software is also rejected.
Exercise
An app passes codesign --verify, but spctl --assess reports rejected. What does each result tell you?
Show answer
The codesign result means the app's files are unchanged since signing. The spctl result means Gatekeeper's policy would not allow it, for example because it lacks Developer ID signing or notarization. The signature is intact, but the app does not meet Gatekeeper's requirements, so do not run it until you confirm the source.