# Code signing

> Sign your Windows binaries and installer or your macOS app as part of kurogane bundle.

Signing is off by default. `--sign` turns it on:

```bash
kurogane bundle --format nsis --sign
```

Each platform that signs reads its own table in `kurogane.toml`. `[signing.windows]` covers a Windows bundle's binaries and installer. `[signing.macos]` covers a macOS app. One file serves a project that ships both. The tables hold references to certificates and never secrets or passwords.

Linux bundles aren't signed. `--sign` on Linux checks the tables and stops.

## Sign on Windows

Name a certificate file or a certificate in the Windows certificate store:

```toml
[signing.windows]
certificate = "certs/codesign.pfx"
certificate-password-env = "CODESIGN_PASSWORD"
timestamp-url = "http://timestamp.digicert.com"
digest-algorithm = "sha256"
```

| Key | What it is |
|---|---|
| `certificate` | a certificate file; `.pfx` or `.p12`, or a PEM or DER chain with `osslsigncode` |
| `certificate-thumbprint` | a certificate in the Windows certificate store, for `signtool`; never beside `certificate` |
| `certificate-password-env` | the environment variable a certificate file's password is read from |
| `timestamp-url` | the RFC 3161 timestamp server |
| `digest-algorithm` | the digest; `sha256` by default |

Keep the password out of `kurogane.toml`. Set the variable in your shell or your CI's secrets:

```powershell
$env:CODESIGN_PASSWORD = "..."
kurogane bundle --format nsis --sign
```

Kurogane signs every `.exe` and `.dll` in the bundle. With `--format nsis` it signs them before the installer is built and then signs the installer.

### Tools

Kurogane uses `signtool.exe` when it finds one and `osslsigncode` otherwise:

| Tool | Found at |
|---|---|
| `signtool.exe` | `KUROGANE_SIGNTOOL_PATH`, then the Windows SDK |
| `osslsigncode` | `KUROGANE_OSSLSIGNCODE_PATH`, then `PATH` |

`osslsigncode` has no certificate store. A thumbprint with it is an error.

### A signing program of your own

`custom-command` runs a program of yours for each file instead of a certificate. Every `%1` in an argument becomes the file's path:

```toml
[signing.windows]
custom-command = ["azuresigntool", "sign", "-kvu", "https://my-vault.vault.azure.net", "%1"]
```

* Each item is one argument. A path with spaces needs no quoting.
* A command without `%1` is an error.
* It can't stand beside `certificate`, `certificate-thumbprint`, `certificate-password-env`, `timestamp-url` or `digest-algorithm`. Those would be ignored.

What the command signed is still verified.

### Sandboxed apps

A sandboxed Windows bundle holds CEF's bootstrap and your app as a library. Sign both or neither. The bootstrap refuses to load a library signed otherwise than itself. `--sign` signs both.

## Sign on macOS

Name the signing identity from your keychain:

```toml
[signing.macos]
identity = "Developer ID Application: Example (ABC1234DE5)"
```

```bash
kurogane bundle --format app --sign
```

| Identity | Signs |
|---|---|
| `Developer ID Application: …` | with a timestamp and the hardened runtime, as distribution requires |
| `-` | ad hoc, for local testing only; no timestamp and no hardened runtime |

The keychain holds the certificate. The table takes no other key. `codesign` comes from the Xcode Command Line Tools, or from `KUROGANE_CODESIGN_PATH`.

### What gets signed

Signing runs from the inside out:

1. The five helper bundles
2. The CEF framework
3. The `.app` with its entitlements
4. The `.dmg`, built from the signed app

A failure at any step fails the bundle. No unsigned `.app` is written. Nothing changes signed code afterwards.

### Entitlements

The app and its helpers get the exceptions Chromium needs and the devices your app declares:

```xml
<dict>
    <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.disable-library-validation</key><true/>
    <key>com.apple.security.device.camera</key><true/>
    <key>com.apple.security.device.audio-input</key><true/>
</dict>
```

They're a signing input and never ship inside the app.

### Notarization

Kurogane doesn't notarize. `kurogane bundle` stops at a signed `.app` and its signed disk image. Submit the `.dmg` to Apple's notary service yourself.

### Keychain prompts

An unsigned build raises a Keychain prompt on every run. Signing ends the prompts. See [Credential storage](https://kurogane-rs.org/docs/guides/credential-storage.md).

## Checks

* `--sign` checks every signing table wherever it runs. A mistake in `[signing.macos]` fails a Windows build too.
* A table refuses a key it doesn't take, the older flat `[signing]` keys included. The error names the key and its line.
* `--sign` fails with an actionable error when this platform's table configures no signing.
* The platform's tool verifies every signed file afterwards: `signtool verify`, `osslsigncode verify` or `codesign --verify --deep --strict`.
