# How to sign and notarize a CLI binary for macOS

> Sign a Go or Rust CLI with a Developer ID certificate, the hardened runtime, and a secure timestamp, notarize it, and check the signature before Apple does.

Source: https://voxgig.com/howto/sign-and-notarize-a-cli-binary-for-macos

- Audience: platform-team
- Level: advanced
- Languages: go, rust
- Verified: 2026-11-30
- Published: 2026-11-30

## Short answer

On a Mac, sign the binary with `codesign -s "Developer ID Application" -f --timestamp -o runtime`, zip it with `ditto`, and submit the zip with `xcrun notarytool submit --wait` and an App Store Connect API key. From Linux, rcodesign or goreleaser with quill signs and submits instead. A bare binary cannot be stapled, so a Mac that is offline at first launch cannot find its ticket.

---
## You will need

An Apple Developer Program membership and three things from it. A Developer ID Application
certificate signs Mach-O files and command-line tools, as
[Apple's notarization troubleshooting page](https://developer.apple.com/documentation/security/resolving-common-notarization-issues)
says. An App Store Connect API key has an issuer, a key ID, and a private key that
[can be downloaded once](https://developer.apple.com/documentation/appstoreconnectapi/creating-api-keys-for-app-store-connect-api).
Make it a team key: the same page says individual keys cannot use `notaryTool`. The preflight
sample needs Go 1.24 or later. The binaries come from
[cross-compile CLI release archives with goreleaser](/howto/cross-compile-release-archives-with-goreleaser).

## Approaches compared

| Approach | When it fits | What it costs you | When to pick something else |
| --- | --- | --- | --- |
| [codesign and notarytool on a macOS runner](https://developer.apple.com/documentation/security/customizing-the-notarization-workflow) | You want Apple's reference tools, and the release job can run on a Mac | A macOS runner in every release, with the Developer ID identity installed on it | Your release pipeline is Linux only and must stay that way |
| [goreleaser notarize with quill](https://goreleaser.com/customization/sign/notarize/) | goreleaser already builds the release and you ship bare binaries | Works on bare and universal binaries only, since a bundle with only its binary signed is deemed damaged | You ship an app bundle, an installer package, or a disk image |
| [rcodesign (apple-codesign) from Linux](https://gregoryszorc.com/docs/apple-codesign/0.29.0/index.html) | Signing, notarizing, and stapling from any platform, outside goreleaser | A second toolchain to build, pin, and keep in step with Apple's rules | Notarization has never passed with Apple's tools for this binary |

Apple's tools are the reference, so a failure they report is a failure of the binary rather than of
a reimplementation. The price is a Mac in the release path. rcodesign and quill sign from Linux and
drop that runner, but each reimplements Apple's format, and its authors document the gaps.
rcodesign's own guide asks you to
[notarize with Apple tooling first](https://gregoryszorc.com/docs/apple-codesign/0.29.0/apple_codesign_rcodesign_notarizing.html#notarize-with-apple-tooling-first)
for software that has never been notarized.

## Read what the Go linker leaves in a Mac binary

Cross-compile both Mac architectures, then read their signatures with the sample's preflight, a Go
program that parses the Mach-O load commands with the standard library's `debug/macho`:

```bash
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -trimpath -o bin/meters-arm64 ./cmd/meters
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -trimpath -o bin/meters-amd64 ./cmd/meters
go build -o bin/preflight ./cmd/preflight
bin/preflight bin/meters-arm64 bin/meters-amd64; echo "exit status $?"
```

```text output
bin/meters-arm64 (arm64)
  signed:           yes
  identifier:       a.out
  signer:           ad hoc, linker-signed, no certificate
  team:             none
  hardened runtime: no
  secure timestamp: no
  notary preflight: 3 problem(s)
    - not signed with a Developer ID Application certificate
    - hardened runtime not enabled
    - no secure timestamp
bin/meters-amd64 (x86_64)
  signed:           no
  notary preflight: 1 problem(s)
    - no code signature
exit status 1
```

The two builds differ because the linker signs only one target. Its
[`NeedCodeSign`](https://github.com/golang/go/blob/go1.25.14/src/cmd/link/internal/ld/lib.go#L300)
returns true for `darwin/arm64` alone. The signature it writes is
[ad hoc and linker-signed](https://github.com/golang/go/blob/go1.25.14/src/cmd/internal/codesign/codesign.go#L226),
with the identifier `a.out` whatever `-o` says.[^1] Apple silicon refuses to run unsigned code,
and an ad hoc signature satisfies that rule, but the
[macOS 11 release notes](https://developer.apple.com/documentation/macos-release-notes/macos-big-sur-11_0_1-universal-apps-release-notes)
add that binaries signed this way cannot pass through Gatekeeper.

A Rust CLI meets the same wall by another road. Its macOS targets need
[Xcode or the macOS SDK to link](https://doc.rust-lang.org/rustc/platform-support/apple-darwin.html),
and the same release notes say Apple's toolchain signs an executable at link time when `clang` or
`ld` links it. Everything below applies to the Mach-O file either toolchain produces.

## Sign with the Developer ID identity

On a Mac, Apple's
[distribution signing guide](https://developer.apple.com/documentation/xcode/creating-distribution-signed-code-for-the-mac)
gives the command for a binary outside a bundle. `--timestamp` asks for a secure timestamp,
`-o runtime` turns on the hardened runtime, and `-i` sets the identifier. `-f` signs over the
signature the linker wrote, as the re-signing command in the macOS 11 release notes also does:

```bash
codesign -s "Developer ID Application" -f --timestamp -o runtime -i com.example.meters meters
```

From Linux, rcodesign takes the certificate as a `.p12` file and the runtime flag as
`--code-signature-flags runtime`, as its
[signing page](https://gregoryszorc.com/docs/apple-codesign/0.29.0/apple_codesign_rcodesign_signing.html)
shows. It asks
`timestamp.apple.com` for a timestamp unless told otherwise.[^2]

```bash
rcodesign sign --p12-file developer-id.p12 --p12-password-file P12_PASSWORD_FILE --code-signature-flags runtime meters
```

goreleaser does both from its configuration through quill, a path its documentation says has no
external dependencies and works on any operating system. Its `notarize.macos` section takes the
`.p12` and the API key from the environment:

```yaml title=".goreleaser.yaml"
notarize:
  macos:
    - enabled: '{{ isEnvSet "MACOS_SIGN_P12" }}'
      ids:
        - meters
      sign:
        certificate: "{{.Env.MACOS_SIGN_P12}}"
        password: "{{.Env.MACOS_SIGN_PASSWORD}}"
      notarize:
        issuer_id: "{{.Env.MACOS_NOTARY_ISSUER_ID}}"
        key_id: "{{.Env.MACOS_NOTARY_KEY_ID}}"
        key: "{{.Env.MACOS_NOTARY_KEY}}"
        wait: true
        timeout: 20m
```

goreleaser's documentation caps `timeout` at `20m`, because Apple rejects an authentication token that lives
too long. The `macos_native` section, which drives `codesign` and `xcrun`, is marked as a
GoReleaser Pro feature.

## Preflight the signature before you upload

Each rejection from Apple's notary service costs another submission and another wait. Most of its
[requirements](https://developer.apple.com/documentation/security/notarizing-macos-software-before-distribution)
are readable in the file itself, so the sample checks them locally first:

```go title="preflight/preflight.go"
func (r Report) Problems() []string {
	if !r.Signed {
		return []string{"no code signature"}
	}
	var p []string
	switch {
	case r.AdHoc || !r.DeveloperIDLeaf:
		p = append(p, "not signed with a Developer ID Application certificate")
	case !r.IssuedByDevIDCA:
		p = append(p, "certificate not issued by a Developer ID intermediate in the signature")
	case r.TeamID == "" || r.TeamID != r.CertTeamID:
		p = append(p, fmt.Sprintf("team %q in the CodeDirectory is not the certificate's team %q", r.TeamID, r.CertTeamID))
	}
	if !r.HardenedRuntime {
		p = append(p, "hardened runtime not enabled")
	}
	if !r.Timestamp {
		p = append(p, "no secure timestamp")
	}
	if r.GetTaskAllow {
		p = append(p, "requests the com.apple.security.get-task-allow entitlement")
	}
	return p
}
```

The hardened runtime is `CS_RUNTIME`, bit `0x10000` of the CodeDirectory flags in Apple's kernel header
[`cs_blobs.h`](https://github.com/apple-oss-distributions/xnu/blob/main/osfmk/kern/cs_blobs.h).
The preflight refuses a signature whose alternate CodeDirectories disagree with the first, since it
cannot tell which one macOS reads. A timestamp is an
[unsigned attribute](https://www.rfc-editor.org/rfc/rfc3161.html) on the CMS signer. The
Developer ID test reads the certificate that the signer's identifier names, and looks for the two
fields that Gatekeeper's Developer ID rules name.[^3] The team check compares the CodeDirectory's
team with that certificate's, because Apple's distribution signing guide names a Developer ID
identity after its team.

Presence and agreement are all it checks. It never validates the chain to Apple's root, the CMS signature, or the
timestamp token, so a pass is necessary and not sufficient.

## Zip it and submit it

The notary service accepts
[disk images, signed flat installer packages, and zip archives](https://developer.apple.com/documentation/security/customizing-the-notarization-workflow),
so a bare binary goes up inside a zip. Build it with `ditto`, as Apple's
[packaging guide](https://developer.apple.com/documentation/xcode/packaging-mac-software-for-distribution)
shows, then submit it with the API key flags that
[TN3147](https://developer.apple.com/documentation/technotes/tn3147-migrating-to-the-latest-notarization-tool)
gives:

```bash
mkdir meters-1.4.0 && ditto meters meters-1.4.0/meters && ditto -c -k --keepParent meters-1.4.0 meters-1.4.0.zip
xcrun notarytool submit meters-1.4.0.zip --issuer ISSUER_UUID --key-id API_KEY --key PATH_TO_KEY --wait
```

`--wait` returns once the service has finished, and `xcrun notarytool log` fetches the JSON log
for the submission id. rcodesign does the same with `rcodesign notary-submit --api-key-file key.json --wait`,
after `rcodesign encode-app-store-connect-api-key` has folded the three parts of the key into one
JSON file. goreleaser submits during the release, and its `wait` setting decides whether the
release waits for Apple's answer.

## Check it worked

Run the preflight's tests. They cross-compile both architectures and read what the linker left.
Then they build synthetic signatures in each state, three of them grafted over the arm64 build.
Their certificates come from a throwaway CA made per run, so they prove what the checker reads,
never that Apple would accept the signature:

```bash
go test -count=1 -v ./preflight | grep -E '^(=== RUN   Test[A-Za-z0-9]+$|PASS$|FAIL$)'
```

```text output
=== RUN   TestADeveloperIDSignatureWithRuntimeAndTimestampPasses
=== RUN   TestEachMissingPropertyIsReported
=== RUN   TestBrokenSignaturesAreRefused
=== RUN   TestGoLinkerSignsArm64AdHocOnly
=== RUN   TestASignedBinaryPassesAndEachGapFailsIt
=== RUN   TestAUniversalFileReportsEachArchitecture
=== RUN   TestFilesItCannotReadAreRefused
PASS
```

The last line reads `PASS`. On the Mac that signed the binary, Apple's troubleshooting page gives
three checks: `codesign -vvv --deep --strict meters` for the signature, `codesign -dvv meters` for
a `Timestamp` line, and `spctl -vvv --assess --type exec meters` for the policy. Then download the
release archive with a browser on another Mac, one that has never run the binary, and run it there.

## When it goes wrong

The binary runs on a connected Mac, but Gatekeeper might block it on one without a network, as
Apple's packaging guide warns. Apple's tools
[cannot staple a ticket to a standalone binary](https://developer.apple.com/documentation/security/customizing-the-notarization-workflow),
so Gatekeeper looks the ticket up online, and rcodesign's stapler
[refuses a Mach-O file](https://github.com/indygreg/apple-platform-rs/blob/apple-codesign/0.29.0/apple-codesign/src/stapling.rs#L306)
for the same reason. Ship a signed installer package or disk image, notarize it, and staple that.

The notary log reports `The executable does not have the hardened runtime enabled.` or
`The signature does not include a secure timestamp.` The signing step left out `-o runtime` or
`--timestamp`. Run the preflight on the signed binary before submitting.

The preflight refuses a file instead of judging it. A truncated download and a file that is not
Mach-O both stop it with exit status 2, never a pass:

```bash
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -trimpath -o bin/meters-arm64 ./cmd/meters
go build -o bin/preflight ./cmd/preflight
head -c 4096 bin/meters-arm64 > bin/truncated
bin/preflight bin/truncated 2>&1; echo "exit status $?"
bin/preflight go.mod 2>&1; echo "exit status $?"
```

```text output
preflight: bin/truncated: not a Mach-O file: EOF
exit status 2
preflight: go.mod: not a Mach-O file: invalid magic number in record at byte 0x0
exit status 2
```

## When not to do this

Do not sign on Linux before the same binary has passed notarization once with Apple's tools. A
rejection from rcodesign or quill can be the tool or the binary, and only Apple's path separates
the two. Keep a Mac job for that first release and for any change to entitlements.

Do not ship a bare binary to users who may be offline at first launch, such as build farms behind
a strict egress policy. A bare binary cannot carry its ticket, so wrap it in an installer package and staple that.

Do not read a preflight pass as notarization. It proves that the properties it reads are present,
while the notary service also scans the contents for malicious code and checks the chain to Apple.

Do not use goreleaser's quill path for an app bundle. Its documentation says a bundle in which only
the binary is signed is deemed damaged, and its native path needs GoReleaser Pro and a Mac.

## Related how-tos

- [Snapshot-test CLI output across operating systems](/howto/snapshot-test-cli-output-across-operating-systems)

- [End-to-end test a CLI against a mock API](/howto/e2e-test-a-cli-against-a-mock-api)

- [Generate an SBOM for a generated SDK](/howto/generate-an-sbom-for-a-generated-sdk)

- [Generate shell completions for bash, zsh, and fish](/howto/shell-completions-for-bash-zsh-and-fish)

## Last verified

Verified 2026-11-30 against Go 1.24.7 and Go 1.25.14 on Linux. Every output block is what the
command preceding it printed. No Mac and no Apple account were used. `codesign`, `notarytool`,
`stapler`, `ditto`, `spctl`, rcodesign 0.29.0, quill, and goreleaser were not run: their commands
and flags are quoted from their documentation and source, read on 2026-11-30. Nothing in Rust
was built: the Rust paragraph is read from the Rust platform-support page and Apple's release notes.

[^1]: The linker passes the string `a.out` as the identifier in
[both places it signs](https://github.com/golang/go/blob/go1.25.14/src/cmd/link/internal/ld/macho.go#L839),
which is the name of another executable format. FreeBSD's
[manual page for it](https://man.freebsd.org/cgi/man.cgi?query=a.out&sektion=5) describes "the
format of executable binary files" and records that its header appeared in Version 7 AT&T UNIX.
Every arm64 Mac binary that Go's own linker signs is therefore named after a format it is not written in,
until `codesign -i` gives it a name of its own.

[^2]: The default is `http://timestamp.apple.com/ts01`, set as
[a constant in rcodesign's command line](https://github.com/indygreg/apple-platform-rs/blob/apple-codesign/0.29.0/apple-codesign/src/cli/mod.rs#L69).
Apple's troubleshooting page says macOS accepts only one secure timestamp server, namely
`timestamp.apple.com`, and lists its address ranges. The choice of server is therefore not a
setting so much as a formality.

[^3]: The rules, as `spctl --list` prints them in
[the primer](https://gregoryszorc.com/docs/apple-codesign/0.29.0/apple_codesign_gatekeeper.html),
ask for one field on the intermediate certificate and one on the leaf, under an Apple anchor. The
rule that denies execution of Developer ID code without notarization applies only to a leaf whose timestamp
field is on or after `20190408000000Z`. A leaf with an earlier field, or none, meets the plain
Developer ID rule instead, which allows execution without asking about notarization.