macos-sign-binaries.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.keychain-db'
  18. KEYCHAIN_PASSWORD="$(openssl rand -base64 32)"
  19. security create-keychain -p "${KEYCHAIN_PASSWORD}" "${KEYCHAIN}"
  20. security set-keychain-settings -lut 21600 "${KEYCHAIN}"
  21. security unlock-keychain -p "${KEYCHAIN_PASSWORD}" "${KEYCHAIN}"
  22. security import "${PKSC12_FILE}" -P "${APPLE_DEVELOPER_ID_BUNDLE_PASSWORD}" -t cert -f pkcs12 -k "${KEYCHAIN}" -T /usr/bin/codesign
  23. security set-key-partition-list -S "apple-tool:,apple:,codesign:" -s -k "${KEYCHAIN_PASSWORD}" "${KEYCHAIN}"
  24. security verify-cert -k "${KEYCHAIN}" -c "${PKSC12_FILE}"
  25. security find-identity -p codesigning "${KEYCHAIN}"
  26. # add new keychain into the search path for codesign, otherwise the stuff does not work
  27. keychains=$(security list-keychains -d user)
  28. keychain_names=();
  29. for keychain in ${keychains}; do
  30. basename=$(basename "${keychain}")
  31. keychain_name=${basename::${#basename}-4}
  32. keychain_names+=("${keychain_name}")
  33. done
  34. security -v list-keychains -s "${keychain_names[@]}" "${KEYCHAIN}"
  35. # known runtime executables and binaries
  36. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  37. "${REL_DIR}"/erts-*/bin/{beam.smp,dyn_erl,epmd,erl,erl_call,erl_child_setup,erlexec,escript,heart,inet_gethost,run_erl,to_erl}
  38. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  39. "${REL_DIR}"/lib/runtime_tools-*/priv/lib/{dyntrace.so,trace_ip_drv.so,trace_file_drv.so}
  40. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  41. "${REL_DIR}"/lib/os_mon-*/priv/bin/{cpu_sup,memsup}
  42. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  43. "${REL_DIR}"/lib/jq-*/priv/{jq_nif1.so,libjq.1.dylib,libonig.4.dylib,erlang_jq_port}
  44. # other files from runtime and dependencies
  45. for f in \
  46. asn1rt_nif.so \
  47. bcrypt_nif.so \
  48. crc32cer_nif.so \
  49. crypto.so \
  50. crypto_callback.so \
  51. jiffy.so \
  52. liberocksdb.so \
  53. libquicer_nif.so \
  54. odbcserver \
  55. otp_test_engine.so \
  56. sasl_auth.so \
  57. snappyer.so \
  58. ; do
  59. find "${REL_DIR}"/lib/ -name "$f" -exec codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime {} \;
  60. done