sign-notarize.bash 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env bash
  2. set -e
  3. readonly usage='usage: sign-notarize.bash -i <id> -d <dev-acct> -k <key-item> [-p <provider>] [--] <package>.dmg
  4. Sign and notarize the "CMake.app" bundle inside the given "<package>.dmg" disk image.
  5. Also produce a "<package>.tar.gz" tarball containing the same "CMake.app".
  6. Options:
  7. -i <id> Signing Identity
  8. -d <dev-acct> Developer account name
  9. -k <key-item> Keychain item containing account credentials
  10. -p <provider> Provider short name
  11. '
  12. cleanup() {
  13. if test -d "$tmpdir"; then
  14. rm -rf "$tmpdir"
  15. fi
  16. if test -d "$vol_path"; then
  17. hdiutil detach "$vol_path"
  18. fi
  19. }
  20. trap "cleanup" EXIT
  21. die() {
  22. echo "$@" 1>&2; exit 1
  23. }
  24. id=''
  25. dev_acct=''
  26. key_item=''
  27. provider=''
  28. while test "$#" != 0; do
  29. case "$1" in
  30. -i) shift; id="$1" ;;
  31. -d) shift; dev_acct="$1" ;;
  32. -k) shift; key_item="$1" ;;
  33. -p) shift; provider="$1" ;;
  34. --) shift ; break ;;
  35. -*) die "$usage" ;;
  36. *) break ;;
  37. esac
  38. shift
  39. done
  40. case "$1" in
  41. *.dmg) readonly dmg="$1"; shift ;;
  42. *) die "$usage" ;;
  43. esac
  44. test "$#" = 0 || die "$usage"
  45. # Verify arguments.
  46. if test -z "$id" -o -z "$dev_acct" -o -z "$key_item"; then
  47. die "$usage"
  48. fi
  49. if test -n "$provider"; then
  50. provider="--provider $provider"
  51. fi
  52. # Verify environment.
  53. if ! xcnotary="$(type -p xcnotary)"; then
  54. die "'xcnotary' not found in PATH"
  55. fi
  56. readonly xcnotary
  57. readonly tmpdir="$(mktemp -d)"
  58. # Prepare entitlements.
  59. readonly entitlements_xml="$tmpdir/entitlements.xml"
  60. echo '<?xml version="1.0" encoding="UTF-8"?>
  61. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  62. <plist version="1.0">
  63. <dict>
  64. <key>com.apple.security.cs.allow-dyld-environment-variables</key>
  65. <true/>
  66. </dict>
  67. </plist>' > "$entitlements_xml"
  68. # Extract SLA
  69. readonly sla_xml="$tmpdir/sla.xml"
  70. hdiutil udifderez -xml "$dmg" > "$sla_xml"
  71. plutil -remove 'blkx' "$sla_xml"
  72. plutil -remove 'plst' "$sla_xml"
  73. # Convert from read-only original image to read-write.
  74. readonly udrw_dmg="$tmpdir/udrw.dmg"
  75. hdiutil convert "$dmg" -format UDRW -o "${udrw_dmg}"
  76. # Mount the temporary udrw image.
  77. readonly vol_name="$(basename "${dmg%.dmg}")"
  78. readonly vol_path="/Volumes/$vol_name"
  79. hdiutil attach "${udrw_dmg}"
  80. codesign --verify --timestamp --options=runtime --verbose --deep \
  81. -s "$id" \
  82. --entitlements "$entitlements_xml" \
  83. "$vol_path/CMake.app/Contents/bin/cmake" \
  84. "$vol_path/CMake.app/Contents/bin/ccmake" \
  85. "$vol_path/CMake.app/Contents/bin/ctest" \
  86. "$vol_path/CMake.app/Contents/bin/cpack" \
  87. "$vol_path/CMake.app"
  88. xcnotary notarize "$vol_path/CMake.app" -d "$dev_acct" -k "$key_item" $provider
  89. # Create a tarball of the volume next to the original disk image.
  90. readonly tar_gz="${dmg/%.dmg/.tar.gz}"
  91. tar cvzf "$tar_gz" -C /Volumes "$vol_name/CMake.app"
  92. # Unmount the modified udrw image.
  93. hdiutil detach "$vol_path"
  94. # Convert back to read-only, compressed image.
  95. hdiutil convert "${udrw_dmg}" -format UDZO -imagekey zlib-level=9 -ov -o "$dmg"
  96. # Re-insert SLA.
  97. hdiutil udifrez -xml "${sla_xml}" 'FIXME_WHY_IS_THIS_ARGUMENT_NEEDED' "$dmg"