|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +self_dir=$(/usr/bin/dirname "$0") |
| 4 | +app_to_sign="$1" |
| 5 | +identity="$2" |
| 6 | + |
| 7 | +if test -z "$app_to_sign"; then |
| 8 | + echo "Usage: $0 <path/to/app> [identity]" |
| 9 | + echo "" |
| 10 | + echo "Arguments:" |
| 11 | + echo " path/to/app Path to the .app bundle to codesign" |
| 12 | + echo " identity (optional) Signing identity. Use '-' for ad-hoc signing" |
| 13 | + echo "" |
| 14 | + echo "Examples:" |
| 15 | + echo " $0 MyApp.app" |
| 16 | + echo " $0 MyApp.app 'TEAMID123'" |
| 17 | + echo "" |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +# full path |
| 22 | +app_to_sign=$(/bin/realpath "$app_to_sign") |
| 23 | +app_id=$(/usr/bin/defaults read "$app_to_sign/Contents/Info.plist" CFBundleIdentifier) |
| 24 | +if test "$?" != "0"; then |
| 25 | + echo "error: could not obtain bundle identifier for app at: $app_to_sign" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +log "Removing quarantine xattr" |
| 30 | +/usr/bin/xattr -dr 'com.apple.quarantine' "$app_to_sign" 2>/dev/null |
| 31 | + |
| 32 | +entitlements_path="$self_dir/OMCApplet/OMCApplet.entitlements" |
| 33 | +entitlements_path_root="$self_dir/OMCApplet.entitlements" |
| 34 | + |
| 35 | +entitlements="" |
| 36 | + |
| 37 | +if test -z "$identity"; then |
| 38 | + identity="-" |
| 39 | + timestamp="--timestamp=none" |
| 40 | + sign_options="" |
| 41 | +else |
| 42 | + if [ -f "${entitlements_path}" ]; then |
| 43 | + entitlements="--entitlements $entitlements_path" |
| 44 | + elif [ -f "${entitlements_path_root}" ]; then |
| 45 | + entitlements="--entitlements $entitlements_path_root" |
| 46 | + fi |
| 47 | + timestamp="--timestamp" |
| 48 | + sign_options="--options runtime" |
| 49 | +fi |
| 50 | + |
| 51 | +refresh_app() { |
| 52 | + local app_path=$1 |
| 53 | + echo "Refreshing bundle modification date" |
| 54 | + /usr/bin/touch -c "${app_path}" |
| 55 | + |
| 56 | + echo "Registering applet with Launch Services" |
| 57 | + /System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister \ |
| 58 | + -f -R -trusted "${app_path}" 2>/dev/null |
| 59 | +} |
| 60 | + |
| 61 | +# Function to sign all executables in a directory recursively |
| 62 | +sign_executables_in_dir() { |
| 63 | + local dir="$1" |
| 64 | + |
| 65 | + if test ! -d "$dir"; then |
| 66 | + return |
| 67 | + fi |
| 68 | + |
| 69 | + echo "" |
| 70 | + echo "Searching for executables in: $dir" |
| 71 | + echo "-----------------------------------" |
| 72 | + |
| 73 | + # Find all executable files (excluding symlinks to avoid double-signing) |
| 74 | + local exec_files=$(/usr/bin/find "$dir" -type f -perm +111 -print | /usr/bin/sort) |
| 75 | + |
| 76 | + if test -z "$exec_files"; then |
| 77 | + echo "No executables found" |
| 78 | + echo "-----------------------------------" |
| 79 | + return |
| 80 | + fi |
| 81 | + |
| 82 | + # Iterate over found files |
| 83 | + echo "$exec_files" | while IFS= read -r exec_file; do |
| 84 | + # Skip if it's not a Mach-O binary or executable script |
| 85 | + file_type=$(/usr/bin/file -b "$exec_file") |
| 86 | + echo "$file_type" | /usr/bin/grep -qE "(Mach-O|executable|script)" |
| 87 | + |
| 88 | + if test "$?" = "0"; then |
| 89 | + echo "Signing: $exec_file" |
| 90 | + /usr/bin/codesign --verbose --force $sign_options $timestamp --sign "$identity" "$exec_file" |
| 91 | + |
| 92 | + if test "$?" != "0"; then |
| 93 | + echo "warning: failed to sign $exec_file" |
| 94 | + fi |
| 95 | + fi |
| 96 | + done |
| 97 | + |
| 98 | + echo "-----------------------------------" |
| 99 | +} |
| 100 | + |
| 101 | +# Sign executables in Contents/Helpers if it exists |
| 102 | +if test -d "$app_to_sign/Contents/Helpers"; then |
| 103 | + sign_executables_in_dir "$app_to_sign/Contents/Helpers" |
| 104 | +fi |
| 105 | + |
| 106 | +# Sign executables in Contents/Library if it exists |
| 107 | +if test -d "$app_to_sign/Contents/Library"; then |
| 108 | + sign_executables_in_dir "$app_to_sign/Contents/Library" |
| 109 | +fi |
| 110 | + |
| 111 | +# Sign executables in Contents/Support if it exists |
| 112 | +if test -d "$app_to_sign/Contents/Support"; then |
| 113 | + sign_executables_in_dir "$app_to_sign/Contents/Support" |
| 114 | +fi |
| 115 | + |
| 116 | +echo "" |
| 117 | + |
| 118 | +# Finally sign the app bundle itself |
| 119 | +echo "Signing app bundle: $app_to_sign" |
| 120 | +echo "/usr/bin/codesign --deep --verbose --force $sign_options $entitlements $timestamp --identifier $app_id --sign $identity $app_to_sign" |
| 121 | +/usr/bin/codesign --deep --verbose --force $sign_options $entitlements $timestamp --identifier "$app_id" --sign "$identity" "$app_to_sign" |
| 122 | + |
| 123 | +if test "$?" != "0"; then |
| 124 | + echo "" |
| 125 | + echo "error: failed to sign app bundle" |
| 126 | + exit 1 |
| 127 | +fi |
| 128 | + |
| 129 | +refresh_app "$app_to_sign" |
| 130 | + |
| 131 | +echo "" |
| 132 | +echo "Verifying and validating codesigned app:" |
| 133 | +echo "-----------------------------------------" |
| 134 | +/usr/bin/codesign --verify --display --verbose=4 "$app_to_sign" |
| 135 | + |
| 136 | +if test "$?" = "0"; then |
| 137 | + echo "-----------------------------------------" |
| 138 | + echo "✓ App signature is valid" |
| 139 | +else |
| 140 | + echo "-----------------------------------------" |
| 141 | + echo "✗ App signature validation failed" |
| 142 | + exit 1 |
| 143 | +fi |
0 commit comments