macos-sign-binaries.sh 3.1 KB

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