codesign

package
v0.6.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package codesign signs macOS Mach-O binaries using the system codesign tool.

It provides two entry points:

  • EnsureSigned ad-hoc signs the current executable with a set of entitlements and re-execs it once, so a binary built without signing can still acquire entitlements it needs at runtime (notably com.apple.security.virtualization).
  • SignBinary signs an arbitrary binary on disk, either ad-hoc or with a named signing identity, with optional identifier, entitlements, and force-replace flags.

EnsureSigned is idempotent and re-exec-guarded: if the executable already carries every required entitlement, or the guard environment variable is set (indicating a prior re-exec), it is a no-op. Call it once at startup:

err := codesign.EnsureSigned(codesign.Options{
	Entitlements: entitlementsPlist, // plist bytes (e.g. go:embed)
	RequireKeys:  []string{"com.apple.security.virtualization"},
	GuardEnv:     "MYAPP_CODESIGN_DONE",
})

To sign another binary on disk (for example, a guest agent before copying it into a VM image), call SignBinary:

err := codesign.SignBinary(path, codesign.SignOptions{
	Identity:   identity, // "" or "-" for ad-hoc
	Identifier: "com.example.agent",
	Force:      true,
})

For signing-identity discovery and signature verification, see github.com/tmc/macgo/codesign, which is a separate concern.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureSigned

func EnsureSigned(opts Options) error

EnsureSigned ad-hoc signs the current executable with opts.Entitlements and re-execs it once, unless the binary already carries every key in opts.RequireKeys or the guard variable opts.GuardEnv is set. On a successful sign it replaces the process image via execve, so it does not return on the signing path; it returns nil when no signing is needed.

Example
package main

import (
	"log"

	"github.com/tmc/apple/x/codesign"
)

func main() {
	const 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.virtualization</key>
	<true/>
</dict>
</plist>`

	if err := codesign.EnsureSigned(codesign.Options{
		Entitlements: []byte(plist),
		RequireKeys:  []string{"com.apple.security.virtualization"},
		GuardEnv:     "MYAPP_CODESIGN_DONE",
	}); err != nil {
		log.Fatal(err)
	}
}

func SignBinary

func SignBinary(path string, opts SignOptions) error

SignBinary signs the binary at path using the macOS codesign tool.

When opts.Identity is empty or "-", SignBinary applies an ad-hoc signature. When opts.Entitlements is non-empty, it writes the plist to a temporary file and removes it before returning.

Example
package main

import (
	"log"

	"github.com/tmc/apple/x/codesign"
)

func main() {
	if err := codesign.SignBinary("/path/to/binary", codesign.SignOptions{
		Force: true,
	}); err != nil {
		log.Fatal(err)
	}
}
Example (Identity)
package main

import (
	"log"

	"github.com/tmc/apple/x/codesign"
)

func main() {
	if err := codesign.SignBinary("/path/to/agent", codesign.SignOptions{
		Identity:   "Developer ID Application: Example Corp",
		Identifier: "com.example.agent",
		Force:      true,
	}); err != nil {
		log.Fatal(err)
	}
}

Types

type Options

type Options struct {
	// Entitlements is the entitlements plist, as bytes. Callers typically embed
	// it with go:embed or inline a literal. Required.
	Entitlements []byte

	// RequireKeys lists the entitlement keys that must all be present for the
	// binary to count as already signed. If any is missing the binary is
	// (re)signed. At least one key is required.
	//
	// Checking every key — not just one — matters when a binary needs several
	// entitlements: a binary signed with a subset would otherwise be treated as
	// done and never gain the rest.
	RequireKeys []string

	// GuardEnv is the environment variable used to detect the post-sign re-exec
	// and prevent an infinite loop. Required; it must be unique to the binary
	// (e.g. "MYAPP_CODESIGN_DONE"). No default is supplied on purpose: a shared
	// default would let one binary's already-signed re-exec be misread by a
	// differently-built sibling during a rollout.
	GuardEnv string

	// VerifyHash, when true, hashes the executable before and after signing and
	// skips the re-exec if codesign left the file unchanged (it was already
	// signed in a way the entitlement check did not detect). When false the
	// binary always re-execs after a successful codesign.
	VerifyHash bool
	// contains filtered or unexported fields
}

Options configures EnsureSigned.

type SignOptions

type SignOptions struct {
	// Identity is the signing identity passed to codesign -s.
	// An empty string or "-" performs ad-hoc signing ("-s -").
	Identity string

	// Identifier is an optional code signing identifier passed via
	// --identifier. When empty, codesign derives an identifier from the binary
	// path.
	Identifier string

	// Entitlements is an optional entitlements plist, as bytes. When non-empty,
	// it is written to a temporary file passed via --entitlements and removed
	// before SignBinary returns.
	Entitlements []byte

	// Force replaces any existing signature (-f).
	Force bool
	// contains filtered or unexported fields
}

SignOptions configures SignBinary. The zero value applies an ad-hoc signature without replacing an existing signature, without an explicit identifier, and without entitlements.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL