macos-sign-binaries.sh 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env bash
  2. # intended to run on MacOS only
  3. # signs all executable files in a given folder (as $1) with developer certificate
  4. # required variables:
  5. # APPLE_DEVELOPER_IDENTITY: "Developer ID Application: <company name> (<hex id>)"
  6. # APPLE_DEVELOPER_ID_BUNDLE: base64-encoded content of apple developer id certificate bundle in pksc12 format
  7. # APPLE_DEVELOPER_ID_BUNDLE_PASSWORD: password used when exporting the bundle
  8. # note: 'bundle' in apple terminology is 'identity'
  9. set -euo pipefail
  10. if [[ "${APPLE_DEVELOPER_ID_BUNDLE:-0}" == 0 || "${APPLE_DEVELOPER_ID_BUNDLE_PASSWORD:-0}" == 0 ]]; then
  11. echo "Apple developer certificate is not configured, skip signing"
  12. exit 0
  13. fi
  14. REL_DIR="${1}"
  15. PKSC12_FILE="$HOME/developer-id-application.p12"
  16. base64 --decode > "${PKSC12_FILE}" <<<"${APPLE_DEVELOPER_ID_BUNDLE}"
  17. KEYCHAIN="emqx-$(date +%s).keychain-db"
  18. KEYCHAIN_PASSWORD="$(openssl rand -base64 32)"
  19. trap cleanup EXIT
  20. function cleanup {
  21. set +e
  22. security delete-keychain "${KEYCHAIN}" 2>/dev/null
  23. }
  24. security create-keychain -p "${KEYCHAIN_PASSWORD}" "${KEYCHAIN}"
  25. security set-keychain-settings -lut 21600 "${KEYCHAIN}"
  26. security unlock-keychain -p "${KEYCHAIN_PASSWORD}" "${KEYCHAIN}"
  27. security import "${PKSC12_FILE}" -P "${APPLE_DEVELOPER_ID_BUNDLE_PASSWORD}" -t cert -f pkcs12 -k "${KEYCHAIN}" -T /usr/bin/codesign
  28. security set-key-partition-list -S "apple-tool:,apple:,codesign:" -s -k "${KEYCHAIN_PASSWORD}" "${KEYCHAIN}"
  29. security verify-cert -k "${KEYCHAIN}" -c "${PKSC12_FILE}"
  30. security find-identity -p codesigning "${KEYCHAIN}"
  31. # add new keychain into the search path for codesign, otherwise the stuff does not work
  32. keychains=$(security list-keychains -d user)
  33. keychain_names=();
  34. for keychain in ${keychains}; do
  35. basename=$(basename "${keychain}")
  36. keychain_name=${basename::${#basename}-4}
  37. keychain_names+=("${keychain_name}")
  38. done
  39. security -v list-keychains -s "${keychain_names[@]}" "${KEYCHAIN}"
  40. # known runtime executables and binaries
  41. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  42. "${REL_DIR}"/erts-*/bin/{beam.smp,dyn_erl,epmd,erl,erl_call,erl_child_setup,erlexec,escript,heart,inet_gethost,run_erl,to_erl}
  43. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  44. "${REL_DIR}"/lib/runtime_tools-*/priv/lib/{dyntrace.so,trace_ip_drv.so,trace_file_drv.so}
  45. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  46. "${REL_DIR}"/lib/os_mon-*/priv/bin/{cpu_sup,memsup}
  47. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  48. "${REL_DIR}"/lib/jq-*/priv/{jq_nif1.so,libjq.1.dylib,libonig.4.dylib,erlang_jq_port}
  49. # other files from runtime and dependencies
  50. for f in \
  51. asn1rt_nif.so \
  52. bcrypt_nif.so \
  53. crc32cer_nif.so \
  54. crypto.so \
  55. crypto_callback.so \
  56. jiffy.so \
  57. liberocksdb.so \
  58. libquicer_nif.so \
  59. odbcserver \
  60. otp_test_engine.so \
  61. sasl_auth.so \
  62. snappyer.so \
  63. ; do
  64. find "${REL_DIR}"/lib/ -name "$f" -exec codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime {} \;
  65. done
  66. cleanup