Files
ep-mail-client/SIGNING.md
T

144 lines
9.9 KiB
Markdown

# Signing ep-mail builds
This guide covers privately distributed Android APKs and Windows executables built on Windows. Neither Google Play nor the Microsoft Store is required. Signing identifies the key used to produce an artifact and detects subsequent changes; it does not by itself prove the application is safe.
Android OpenPGP remains experimental and is not independently security reviewed. Signing the application does not change that status.
## Keep signing keys safe
- Keep private keys, keystores, PFX files, and passwords outside the repository and release artifacts. Never commit them or paste passwords into chat, issues, logs, or scripts.
- Restrict access to the files and store passwords separately in a password manager. Keep a secure backup of each private key and its password.
- Exchange and verify public certificate fingerprints through a separate trusted channel before trusting a certificate or installing an artifact.
- Run the PowerShell commands below from the repository root unless stated otherwise. Environment variables set here last only for the current PowerShell session, but are available to child processes during the build.
## Android: create a release signing key
Android accepts a self-signed app certificate; a paid certificate and Google Play account are unnecessary. Install the Android SDK and JDK 21 as described in the [README](./README.md).
Choose a secure directory outside the repository. Create it with appropriate access restrictions before proceeding. The following example uses Android Studio's JDK and prompts for the keystore password and certificate details:
```powershell
$env:JAVA_HOME = "C:\Program Files\Android\Android Studio\jbr"
$env:ANDROID_HOME = Join-Path $env:LOCALAPPDATA "Android\Sdk"
& "$env:JAVA_HOME\bin\keytool.exe" -genkeypair -v `
-keystore "C:\secure\ep-mail-android.p12" -storetype PKCS12 `
-alias ep-mail -keyalg RSA -keysize 3072 -validity 10000
```
Replace `C:\secure` with your secured directory. Do not overwrite or recreate an existing production keystore. With PKCS12, use the same password for the keystore and key entry. Back up the keystore and password securely; losing the key prevents ordinary in-place updates of apps signed with it. Record the alias and public certificate SHA-256 fingerprint:
```powershell
& "$env:JAVA_HOME\bin\keytool.exe" -list -v `
-keystore "C:\secure\ep-mail-android.p12" -storetype PKCS12 -alias ep-mail
```
### Build and verify
In the same PowerShell session, configure the existing release build. The password prompt avoids entering the password as a command-line argument, although the environment variables contain plaintext for the duration of the build session.
```powershell
$env:EP_MAIL_ANDROID_STORE_FILE = "C:\secure\ep-mail-android.p12"
$env:EP_MAIL_ANDROID_KEY_ALIAS = "ep-mail"
$env:EP_MAIL_ANDROID_STORE_PASSWORD = Read-Host "Keystore password" -AsSecureString |
ForEach-Object { [System.Net.NetworkCredential]::new("", $_).Password }
$env:EP_MAIL_ANDROID_KEY_PASSWORD = $env:EP_MAIL_ANDROID_STORE_PASSWORD
npm run android:release-apk
```
The output is `release\ep-mail <version>.apk`. Confirm the build succeeded before using it. Verify the signature and certificate fingerprint using the Android SDK build-tools version installed on your machine, replacing the example version if necessary:
```powershell
$version = (Get-Content package.json -Raw | ConvertFrom-Json).version
$apksigner = Join-Path $env:ANDROID_HOME "build-tools\36.0.0\apksigner.bat"
& $apksigner verify --verbose --print-certs "release\ep-mail $version.apk"
```
Compare the displayed signer SHA-256 fingerprint with the fingerprint recorded from the keystore. Afterwards, remove the password variables from the session:
```powershell
Remove-Item Env:\EP_MAIL_ANDROID_STORE_PASSWORD, Env:\EP_MAIL_ANDROID_KEY_PASSWORD
```
**Build order matters:** `npm run build` and `npm run build:all` build a debug APK and overwrite `release\ep-mail <version>.apk` with a debug-signed copy. Run `npm run android:release-apk` **after** those commands, then verify the resulting APK. The `-debug.apk` remains a debug build.
### Moving from the existing debug app
Android normally requires the installed app and its update to have the same signing certificate. A new release key will not match the currently installed debug key, so `adb install -r` will fail rather than replace it.
Before uninstalling, securely back up any private OpenPGP keys and revocation certificates, local-only drafts and other local data you need; ensure account settings can be recreated. Uninstalling removes the app's local data. After verifying your backups, uninstall the debug app using Android's app settings and install the verified release APK:
```powershell
& "$env:ANDROID_HOME\platform-tools\adb.exe" install "release\ep-mail $version.apk"
```
Future builds signed with the same release key can normally be installed with `adb install -r`. Keep the same package ID and use an appropriate version code for upgrades; do not assume that a different signing key will update the app.
## Windows: self-signed certificate for internal use
For private machines you control, a self-signed code-signing certificate is an option without buying a certificate. This does **not** make the publisher publicly trusted and does not guarantee that Microsoft Defender SmartScreen will stop warning. An unsigned build is also an option if these trust changes are not wanted.
The example below creates an exportable certificate in the current user's personal certificate store and exports a password-protected private-key PFX plus a public-only CER. Use a securely permissioned directory outside the repository and a name appropriate for your project. Do not replace an existing key without a plan for distributing and trusting the new public certificate.
```powershell
$cert = New-SelfSignedCertificate -Type CodeSigningCert `
-Subject "CN=ep-mail Internal" -CertStoreLocation "Cert:\CurrentUser\My" `
-KeyAlgorithm RSA -KeyLength 3072 -HashAlgorithm SHA256 `
-KeyExportPolicy Exportable -NotAfter (Get-Date).AddYears(5)
$pfxPassword = Read-Host "Choose a PFX password" -AsSecureString
Export-PfxCertificate -Cert $cert -FilePath "C:\secure\ep-mail-windows.pfx" -Password $pfxPassword
Export-Certificate -Cert $cert -FilePath "C:\secure\ep-mail-windows.cer"
$cert | Format-List Subject, Thumbprint, NotBefore, NotAfter
```
Back up the PFX and password securely. The CER is the public certificate: share it only with people who need to verify your builds, and verify its fingerprint independently. Renew or replace the certificate before expiry; use a trusted timestamp when signing so signature validity can be evaluated after expiry. Timestamping requires network access.
### Build and verify
Electron-builder accepts a PFX and password from the environment:
```powershell
$env:WIN_CSC_LINK = "C:\secure\ep-mail-windows.pfx"
$env:WIN_CSC_KEY_PASSWORD = Read-Host "PFX password" -AsSecureString |
ForEach-Object { [System.Net.NetworkCredential]::new("", $_).Password }
npm run dist:win
```
This builds the portable executable and installer in `release\`. `npm run build:all` also builds both Windows targets and uses these signing variables when supplied. Check the build output for signing errors; a build without credentials can otherwise produce unsigned artifacts. Verify **both** outputs:
```powershell
$version = (Get-Content package.json -Raw | ConvertFrom-Json).version
Get-AuthenticodeSignature "release\ep-mail $version.exe" |
Format-List Status, StatusMessage, SignerCertificate, TimeStamperCertificate
Get-AuthenticodeSignature "release\ep-mail Setup $version.exe" |
Format-List Status, StatusMessage, SignerCertificate, TimeStamperCertificate
Remove-Item Env:\WIN_CSC_KEY_PASSWORD
```
Compare each signer certificate fingerprint with the independently verified certificate. `Status` may indicate an untrusted chain until the public certificate is trusted on the verifying machine; a missing signer, invalid signature, or unexpected fingerprint must not be treated as success. A timestamp is distinct from the signer certificate; inspect its presence and validity as well.
### Trust the public certificate on a private machine
Only do this on machines you control, after independently verifying the CER fingerprint. Importing a root certificate changes trust for the current user, not just for ep-mail: software signed by a trusted key may also be trusted. Protect and distribute the key accordingly. Never import the PFX on machines that only need to verify builds.
```powershell
$publicCert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\secure\ep-mail-windows.cer")
$publicCert | Format-List Subject, Thumbprint, NotBefore, NotAfter
# Proceed only after checking the thumbprint through a separate trusted channel.
Import-Certificate -FilePath "C:\secure\ep-mail-windows.cer" -CertStoreLocation "Cert:\CurrentUser\Root"
Import-Certificate -FilePath "C:\secure\ep-mail-windows.cer" -CertStoreLocation "Cert:\CurrentUser\TrustedPublisher"
```
Re-run `Get-AuthenticodeSignature` on each artifact and confirm a `Valid` status, the expected signer and the timestamp. Trust is per user with these commands; other users or machines require their own trust configuration. If the certificate is compromised or no longer needed, remove its entries from those stores and stop trusting or distributing builds signed by the compromised key.
For distribution to machines where manually installing trust is impractical, use a certificate from a publicly trusted code-signing provider instead. Such certificates have costs and provider-specific requirements; a valid signature does not guarantee SmartScreen reputation.
## References
- [Android app signing](https://developer.android.com/studio/publish/app-signing)
- [Android apksigner](https://developer.android.com/tools/apksigner)
- [electron-builder Windows code signing](https://www.electron.build/docs/features/code-signing/code-signing-win/)
- [Microsoft: New-SelfSignedCertificate](https://learn.microsoft.com/en-us/powershell/module/pki/new-selfsignedcertificate)