| 1 |
#!/usr/bin/env zsh |
| 2 |
|
| 3 |
function dxx_codesign { |
| 4 |
# codesign parameters: |
| 5 |
# --timestamp: Request a signing timestamp from Apple's servers (required for notarization). |
| 6 |
# --options=runtime: Enable a hardened runtime (required for notarization). |
| 7 |
# --verbose: More verbose output for tracking status. |
| 8 |
# --force: Force overwriting existing ad hoc or identity-signed signatures. Existing resources are likely to have at least one signature (likely ad hoc) to replace. |
| 9 |
# --sign: The signing identity to use to sign the resource. |
| 10 |
codesign --timestamp --options=runtime --verbose --force --sign "${signing_identity[2]}" "$@" |
| 11 |
} |
| 12 |
|
| 13 |
# Echo a message to stdout, and include the name of the script so that users |
| 14 |
# can recognize the origin when this script is run as part of a larger build |
| 15 |
# job. |
| 16 |
function emsg { |
| 17 |
echo "$0: $@" |
| 18 |
} |
| 19 |
|
| 20 |
# Echo a message to stderr, and include the string `error:` for users to easily |
| 21 |
# find it via text search in a log. |
| 22 |
function eerror { |
| 23 |
echo "$0: error: $@" >&2 |
| 24 |
} |
| 25 |
|
| 26 |
zmodload zsh/zutil |
| 27 |
autoload is-at-least |
| 28 |
|
| 29 |
if ! is-at-least 5.8 ${ZSH_VERSION}; then |
| 30 |
eerror "zsh 5.8 is required for the notarization script, but zsh version ${ZSH_VERSION} was used. Please update to macOS 12 or higher, or install zsh 5.8 or higher and place it before the system zsh in your \$PATH." |
| 31 |
exit 1 |
| 32 |
fi |
| 33 |
|
| 34 |
zparseopts -D -E -F - s:=signing_identity -signing-identity:=signing_identity a:=app_bundle_path -app-bundle-path:=app_bundle_path b:=binary_name -binary-name:=binary_name z:=zip_path -zip-path:=zip_path k:=notarization_keychain_profile -notarization-keychain-profile:=notarization_keychain_profile i:=apple_id -apple-id:=apple_id t:=team_id -team-id:=team_id || exit 1 |
| 35 |
|
| 36 |
end_opts=$@[(i)(--|-)] |
| 37 |
set -- "${@[0,end_opts-1]}" "${@[end_opts+1,-1]}" |
| 38 |
|
| 39 |
DXX_SIGN_HAS_ERROR=0 |
| 40 |
|
| 41 |
if [[ -z "${signing_identity}" ]]; then |
| 42 |
eerror "--signing-identity is required." |
| 43 |
DXX_SIGN_HAS_ERROR=1 |
| 44 |
fi |
| 45 |
|
| 46 |
if [[ -z "${app_bundle_path}" ]]; then |
| 47 |
eerror "--app-bundle-path is required." |
| 48 |
DXX_SIGN_HAS_ERROR=1 |
| 49 |
elif [[ ! -d "${app_bundle_path[2]}" ]]; then |
| 50 |
eerror "App bundle specified in --app-bundle-path (${app_bundle_path[2]}) does not exist." |
| 51 |
DXX_SIGN_HAS_ERROR=1 |
| 52 |
fi |
| 53 |
|
| 54 |
if [[ -z "${binary_name}" ]]; then |
| 55 |
eerror "--binary-name is required." |
| 56 |
DXX_SIGN_HAS_ERROR=1 |
| 57 |
elif [[ ! -f "${app_bundle_path[2]}/Contents/MacOS/${binary_name[2]}" ]]; then |
| 58 |
eerror "Binary specified in --binary-name (${binary_name[2]}) does not exist." |
| 59 |
DXX_SIGN_HAS_ERROR=1 |
| 60 |
fi |
| 61 |
|
| 62 |
if [[ -z "${zip_path}" ]]; then |
| 63 |
eerror "--zip-path is required." |
| 64 |
DXX_SIGN_HAS_ERROR=1 |
| 65 |
fi |
| 66 |
|
| 67 |
if [[ -z "${notarization_keychain_profile}" ]]; then |
| 68 |
if [[ -z "${apple_id}" || -z "${team_id}" ]]; then |
| 69 |
eerror "If --notarization-keychain-profile is not provided, then both --apple-id and --team-id must be provided." |
| 70 |
DXX_SIGN_HAS_ERROR=1 |
| 71 |
fi |
| 72 |
fi |
| 73 |
|
| 74 |
if [[ DXX_SIGN_HAS_ERROR -ne 0 ]]; then |
| 75 |
# No message is needed here. A message was printed in the path(s) that set DXX_SIGN_HAS_ERROR. |
| 76 |
exit 1 |
| 77 |
fi |
| 78 |
|
| 79 |
if [[ -d "${app_bundle_path[2]}/Contents/libs" ]]; then |
| 80 |
DXX_DYLIB_PATH="${app_bundle_path[2]}/Contents/libs" |
| 81 |
else |
| 82 |
# Unset this to prevent surprising results if the parent process set this in |
| 83 |
# the environment. |
| 84 |
unset DXX_DYLIB_PATH |
| 85 |
fi |
| 86 |
|
| 87 |
DXX_BINARY_PATH="${app_bundle_path[2]}/Contents/MacOS/${binary_name[2]}" |
| 88 |
|
| 89 |
if [[ ! -z "${DXX_DYLIB_PATH}" ]]; then |
| 90 |
emsg "Signing dylibs at ${DXX_DYLIB_PATH} with identity ${signing_identity[2]} ..." |
| 91 |
|
| 92 |
dxx_codesign "${DXX_DYLIB_PATH}"/*.dylib |
| 93 |
if [[ $? -ne 0 ]]; then |
| 94 |
eerror "Failed to sign dylibs in ${DXX_DYLIB_PATH}" |
| 95 |
exit 1 |
| 96 |
fi |
| 97 |
fi |
| 98 |
|
| 99 |
emsg "Signing application binary at ${DXX_BINARY_PATH} with identity ${signing_identity[2]} ..." |
| 100 |
|
| 101 |
dxx_codesign "${DXX_BINARY_PATH}" |
| 102 |
if [[ $? -ne 0 ]]; then |
| 103 |
eerror "Failed to sign application binary ${DXX_BINARY_PATH}" |
| 104 |
exit 1 |
| 105 |
fi |
| 106 |
|
| 107 |
emsg "Signing app bundle at ${app_bundle_path[2]} with identity ${signing_identity[2]} ..." |
| 108 |
|
| 109 |
dxx_codesign "${app_bundle_path[2]}" |
| 110 |
if [[ $? -ne 0 ]]; then |
| 111 |
eerror "Failed to sign app bundle ${app_bundle_path[2]}" |
| 112 |
exit 1 |
| 113 |
fi |
| 114 |
|
| 115 |
DXX_ZIP_NAME=${zip_path[2]##*/} |
| 116 |
|
| 117 |
DXX_TMP_ZIP_PATH="${TMPDIR}${DXX_ZIP_NAME}" |
| 118 |
|
| 119 |
emsg "Compressing ${app_bundle_path[2]} to temporary ZIP file at ${DXX_TMP_ZIP_PATH} ..." |
| 120 |
|
| 121 |
if [[ -f "${DXX_TMP_ZIP_PATH}" ]]; then |
| 122 |
rm -f "${DXX_TMP_ZIP_PATH}" |
| 123 |
fi |
| 124 |
|
| 125 |
if [[ -f "${DXX_TMP_ZIP_PATH}" ]]; then |
| 126 |
eerror "Unable to remove existing temporary ZIP file at ${DXX_TMP_ZIP_PATH}" |
| 127 |
exit 1 |
| 128 |
fi |
| 129 |
|
| 130 |
/usr/bin/ditto -c -k --keepParent "${app_bundle_path[2]}" "${DXX_TMP_ZIP_PATH}" |
| 131 |
|
| 132 |
if [[ ! -f "${DXX_TMP_ZIP_PATH}" ]]; then |
| 133 |
eerror "Error compressing app bundle to ZIP file." |
| 134 |
exit 1 |
| 135 |
fi |
| 136 |
|
| 137 |
# Note that the notarization process does NOT change the app bundle or the ZIP file. |
| 138 |
# Instead, Apple reads the signature associated with the submitted resources and, if |
| 139 |
# they don't detect any malicious code, create a ticket on their side that can be |
| 140 |
# requested by a macOS client for the specific resource that was signed in order to |
| 141 |
# validate that it went through the notarization process. This ticket can also be |
| 142 |
# stapled to the app bundle for offline validation by macOS clients which are not |
| 143 |
# connected to the Internet at the time. This stapling process happens further in |
| 144 |
# the script. |
| 145 |
|
| 146 |
emsg "Beginning notarization process. This may take a few minutes." |
| 147 |
|
| 148 |
if [[ -z "${notarization_keychain_profile}" ]]; then |
| 149 |
emsg "Using Apple ID '${apple_id[2]}' and Apple team ID '${team_id[2]}' for notarization credentials." |
| 150 |
if [[ ! -z "${DXX_NOTARIZATION_PASSWORD}" ]]; then |
| 151 |
emsg "Using password passed in via DXX_NOTARIZATION_PASSWORD environment variable." |
| 152 |
xcrun notarytool submit "${DXX_TMP_ZIP_PATH}" --apple-id "${apple_id[2]}" --team-id "${team_id[2]}" --password "${DXX_NOTARIZATION_PASSWORD}" --wait |
| 153 |
if [[ $? -ne 0 ]]; then |
| 154 |
eerror "Error notarizing application. Check history for details." |
| 155 |
exit 1 |
| 156 |
fi |
| 157 |
else |
| 158 |
emsg "Enter your application-specific password when prompted to submit for notarization." |
| 159 |
xcrun notarytool submit "${DXX_TMP_ZIP_PATH}" --apple-id "${apple_id[2]}" --team-id "${team_id[2]}" --wait |
| 160 |
if [[ $? -ne 0 ]]; then |
| 161 |
eerror "Error notarizing application. Check history for details." |
| 162 |
exit 1 |
| 163 |
fi |
| 164 |
fi |
| 165 |
else |
| 166 |
emsg "Using Keychain item specified in '${notarization_keychain_profile[2]}' for notarization credentials." |
| 167 |
xcrun notarytool submit "${DXX_TMP_ZIP_PATH}" --keychain-profile "${notarization_keychain_profile[2]}" --wait |
| 168 |
if [[ $? -ne 0 ]]; then |
| 169 |
eerror "Error notarizing application. Check history for details." |
| 170 |
exit 1 |
| 171 |
fi |
| 172 |
fi |
| 173 |
|
| 174 |
emsg "Stapling ticket to app bundle ${app_bundle_path[2]} ..." |
| 175 |
|
| 176 |
xcrun stapler staple "${app_bundle_path[2]}" |
| 177 |
if [[ $? -ne 0 ]]; then |
| 178 |
eerror "Failed to staple ticket to app bundle ${app_bundle_path[2]}" |
| 179 |
exit 1 |
| 180 |
fi |
| 181 |
|
| 182 |
rm -f "${DXX_TMP_ZIP_PATH}" |
| 183 |
|
| 184 |
if [[ -f "${zip_path[2]}" ]]; then |
| 185 |
rm -f "${zip_path[2]}" |
| 186 |
fi |
| 187 |
|
| 188 |
if [[ -f "${zip_path[2]}" ]]; then |
| 189 |
eerror "Unable to remove existing target ZIP file at ${zip_path[2]}" |
| 190 |
exit 1 |
| 191 |
fi |
| 192 |
|
| 193 |
emsg "Creating ${zip_path[2]} ..." |
| 194 |
|
| 195 |
/usr/bin/ditto -c -k --keepParent "${app_bundle_path[2]}" "${zip_path[2]}" |