sign_windows.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env bash
  2. # Authenticode-sign the Windows PE produced by GoReleaser (osslsigncode + PKCS#11 token).
  3. #
  4. # Usage: sign_windows.sh <artifact.exe>
  5. #
  6. # Remote signing (e.g. GoReleaser in Docker → host USB token):
  7. # Set SIGN_HTTP_URL to the host signing server, e.g. http://host.docker.internal:8765
  8. # The server must see the same repo mount (path under .../open-oscar-server/...).
  9. #
  10. # When SKIP_CODE_SIGN=1, exits 0 without signing (unless SIGN_HTTP_URL is set).
  11. #
  12. # For local signing, set:
  13. # SIGN_PKCS11_ENGINE - path to libpkcs11 engine (e.g. Homebrew libp11)
  14. # SIGN_PKCS11_MODULE - path to vendor PKCS#11 module (.dylib / .so)
  15. # SIGN_CERT_PEM - path to signer certificate chain PEM
  16. # SIGN_KEY_ID - PKCS#11 key identifier (hex)
  17. # SIGN_PASSWORD - token/PIN (optional if not required)
  18. # Optional:
  19. # SIGN_TIMESTAMP_URL - default http://time.certum.pl/
  20. # SIGN_SERVER_TOKEN - Bearer token if sign_server uses SIGN_SERVER_TOKEN
  21. #
  22. # Example (match your osslsigncode flags; do not commit secrets):
  23. # export SIGN_PKCS11_ENGINE=/opt/homebrew/.../libpkcs11.dylib
  24. # export SIGN_PKCS11_MODULE=/usr/local/lib/crypto3PKCS/...dylib
  25. # export SIGN_CERT_PEM=/path/to/chain.pem
  26. # export SIGN_KEY_ID=<pkcs11-key-id-hex>
  27. # export SIGN_PASSWORD='...'
  28. # make release-sign
  29. set -euo pipefail
  30. artifact="${1:?artifact path required}"
  31. if [[ -n "${SIGN_HTTP_URL:-}" ]]; then
  32. rel="${artifact#*/open-oscar-server/}"
  33. if [[ "$rel" == "$artifact" ]]; then
  34. echo "cannot derive repo-relative path from ${artifact} (expected .../open-oscar-server/...)" >&2
  35. exit 1
  36. fi
  37. url="${SIGN_HTTP_URL%/}/sign"
  38. payload=$(printf '{"path":"%s"}' "${rel}")
  39. hdr=()
  40. if [[ -n "${SIGN_SERVER_TOKEN:-}" ]]; then
  41. hdr=( -H "Authorization: Bearer ${SIGN_SERVER_TOKEN}" )
  42. fi
  43. curl -fsS "${hdr[@]}" -X POST -H "Content-Type: application/json" -d "${payload}" "${url}"
  44. exit 0
  45. fi
  46. if [[ "${SKIP_CODE_SIGN:-}" == "1" ]]; then
  47. echo "Skipping Windows Authenticode (SKIP_CODE_SIGN=1)"
  48. exit 0
  49. fi
  50. : "${SIGN_PKCS11_ENGINE:?set SIGN_PKCS11_ENGINE}"
  51. : "${SIGN_PKCS11_MODULE:?set SIGN_PKCS11_MODULE}"
  52. : "${SIGN_CERT_PEM:?set SIGN_CERT_PEM}"
  53. : "${SIGN_KEY_ID:?set SIGN_KEY_ID}"
  54. timestamp_url="${SIGN_TIMESTAMP_URL:-http://time.certum.pl/}"
  55. tmp="${artifact}.~signing~"
  56. rm -f "${tmp}"
  57. pass_args=()
  58. if [[ -n "${SIGN_PASSWORD:-}" ]]; then
  59. pass_args+=( -pass "${SIGN_PASSWORD}" )
  60. fi
  61. osslsigncode sign \
  62. -verbose \
  63. -pkcs11engine "${SIGN_PKCS11_ENGINE}" \
  64. -pkcs11module "${SIGN_PKCS11_MODULE}" \
  65. -certs "${SIGN_CERT_PEM}" \
  66. -key "${SIGN_KEY_ID}" \
  67. "${pass_args[@]}" \
  68. -h sha256 \
  69. -t "${timestamp_url}" \
  70. -in "${artifact}" \
  71. -out "${tmp}"
  72. mv "${tmp}" "${artifact}"