-
Notifications
You must be signed in to change notification settings - Fork 2
/
mac_app_sign_and_package.sh
executable file
·479 lines (439 loc) · 14.4 KB
/
mac_app_sign_and_package.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/sh --
#
# This script signs and packages a Mac OS X application bundle.
# The final package will be created as ../dist/|APP_NAME|-|VERSION|-setup-MacOS.pkg.
#
# Requirements for this script are:
#
# User must specify the application bundle name without extension: (example -a "MyApplication")
# IMPORTANT: Application bundle must exist in folder noted below
# User must specify the package version: (example: "-v 1.0.56")
#
# All other parameters are optional: (FTDI driver installer, restart requirement, developer identities)
#
# These files and folders must exist in the paths show below (in relation to this script's folder):
# ./drivers/FTDIUSBSerialDriver.kext
# ./mac-resources/neededToRun.entitlements
# ../dist/|application_bundle_name|.app
#
# To update the driver,
# - download and install driver (from http://www.ftdichip.com/Drivers/VCP.htm)
# - select the currently supported Mac OS X VCP driver from that page (i.e. x64 (64-bit))
# - or use: http://www.ftdichip.com/Drivers/VCP/MacOSX/FTDIUSBSerialDriver_v2_4.2.dmg
# - install FTDI's driver package onto the development Mac OS X system
# - copy the FTDIUSBSerialDriver.kext from /Library/Extensions/ to the ../drivers/ folder
#
# The ../dist/|application_bundle_name|.app will be modified by this script to digitally sign it with the default or optional
# application developer identity certificate as well as with the given runtime entitlements (neededToRun.entitlements)
#
usage()
{
cat << EOF
Usage: $0 OPTIONS
This script signs an application bundle and builds a signed installation package.
OPTIONS:
-h show usage
-a (REQUIRED) application bundle name
- example: -a "MyApplication"
-v (REQUIRED) version
- example: -v 0.5.1
-r require restart after installation (applies only if FTDIUSBSerialDriver is included)
-f include FTDIUSBSerialDriver in the package
-s application developer identity certificate key
- example: -s "Developer Identity" (default is "Developer ID Application")
-t installer developer identity certificate key
- example: -t "Developer Identity" (default is "Developer ID Installer")
-d use deployment identifier (default is: com.test.ParallaxInc, deploy is: com.ParallaxInc.|APP_NAME|)
examples: $0 -a "MyApplication" -v 1.0.56 -d
$0 —a "MyApplication" -v 1.0.56 -r -f -s "Developer ID Application" -t "Developer ID Installer" -d
EOF
}
#
# Resource paths
#
# Notes:
# * All are paths relative to this script.
# * NWJS_FW_LIBRARIES (NW.js Framework Libraries) is the path inside of APP_BUNDLE (defined later)
#
RESOURCES="./mac-resources/"
DISTRIBUTION="../dist/"
NWJS_FW="/Contents/Frameworks/nwjs Framework.framework/"
NWJS_FW_LIBRARIES="${NWJS_FW}Versions/Current/Libraries/"
ENTITLEMENTS="${RESOURCES}neededToRun.entitlements"
#
# Default installation locations
#
# Note: the FTDI kext used to be in "/System/Library/Extensions/" per Apple's previous suggestion (before Mavericks?)
#
FTDIDRIVER_DEST_DIR="/Library/Extensions/"
DEFAULT_APP_DIR="/Applications/"
#
# Default component names
#
FTDIDRIVER=FTDIUSBSerialDriver
FTDIDRIVER_KEXT=${FTDIDRIVER}.kext
#
# Modified temporary distro xml
#
# Note: will contain copied or sed-modified version of template DistributionXXXX.xml
#
DIST_DST=DistributionMOD.xml
#
# initialize input options with default values
#
APP_NAME=
VERSION=
APP_IDENTITY="Developer ID Application"
INST_IDENTITY="Developer ID Installer"
REQUIRE_RESTART_TEXT="requireRestart"
RESTART=false
DEPLOY=false
FTDI=false
#
# get parms as flags or as requiring arguments
#
while getopts "ha:v:rfs:t:d" OPTION
do
case $OPTION in
h)
usage; exit 1 ;;
a)
APP_NAME=$OPTARG
;;
v)
VERSION=$OPTARG
;;
r)
if [[ $OPTARG =~ ^[0-9]+$ ]]
then
RESTART=$OPTARG
elif [[ $OPTARG =~ ^-, ]]
then
RESTART=true
let OPTIND=$OPTIND-1
else
RESTART=true
fi
;;
f)
if [[ $OPTARG =~ ^[0-9]+$ ]]
then
FTDI=$OPTARG
elif [[ $OPTARG =~ ^-, ]]
then
FTDI=true
let OPTIND=$OPTIND-1
else
FTDI=true
fi
;;
s)
APP_IDENTITY=$OPTARG
;;
t)
INST_IDENTITY=$OPTARG
;;
d)
if [[ $OPTARG =~ ^[0-9]+$ ]]
then
DEPLOY=$OPTARG
elif [[ $OPTARG =~ ^-, ]]
then
DEPLOY=true
let OPTIND=$OPTIND-1
else
DEPLOY=true
fi
;;
?)
echo "[HALT] Misconfigured options - see usage notes"
usage; exit ;;
esac
done
#
# Error if no application bundle name (-a bundle) was declared
#
if [[ -z $APP_NAME ]]
then
echo "[HALT] No application bundle was declared - see usage notes for -a."
echo
usage
exit 1
fi
#
# Set bundle name
#
APP_BUNDLE=${APP_NAME}.app
#
# Error if no version (-v) option declared
#
if [[ -z $VERSION ]]
then
echo "[HALT] No version option was declared - see usage notes for -v."
echo
usage
exit 1
fi
#
# Error if no version string declared
#
if [ ${VERSION}X == X ]
then
echo "[HALT] No version string was declared - see usage notes for -v."
echo
usage
exit 1
fi
#
# Show Info
#
echo
echo "----------------RECIPE----------------"
echo "* Processing target: \"${DISTRIBUTION}${APP_NAME}.app\""
echo "* As build version: \"${VERSION}\""
echo "* Using application identity: \"${APP_IDENTITY}\""
echo "* Using installer identity: \"${INST_IDENTITY}\""
if [[ $RESTART == true ]]
then
echo "* Restart required after installation"
else
echo "* Restart NOT required after installation"
fi
if [[ ${FTDI} == true ]]
then
echo "* FTDI kext WILL BE installed by this package"
else
echo "* FTDI kext WILL NOT BE installed by this package"
fi
echo
#
# Use security utility to determine if the developer installation identity is valid
#
echo "Validating developer identity certificates..."
APP_SECUREID=`security find-certificate -c "$APP_IDENTITY" | grep labl`
INST_SECUREID=`security find-certificate -c "$INST_IDENTITY" | grep labl`
if [[ -n ${APP_SECUREID} ]]
then
echo " Found Application Identity: \"${APP_IDENTITY}\""
else
echo " [Error] Application Identity: \"${APP_IDENTITY}\" does not exist!"
echo " Use Keychain Access app to verify that you are using an authorized developer installation certificate..."
echo " i.e. search within Login Keychain 'My Certificates' Category for certificate, for example: 'Developer ID Application'"
echo
exit 1
fi
if [[ -n ${INST_SECUREID} ]]
then
echo " Found Installer Identity: \"${INST_IDENTITY}\""
else
echo " [Error] Installer Identity: \"${INST_IDENTITY}\" does not exist!"
echo " Use Keychain Access app to verify that you are using an authorized developer installation certificate..."
echo " i.e. search within Login Keychain 'My Certificates' Category for certificate, for example: 'Developer ID Installer'."
echo
exit 1
fi
echo
#
# Set bundle version number (in Info.plist)
#
if [[ -e "${DISTRIBUTION}${APP_BUNDLE}" ]]
then
if [[ -e "${DISTRIBUTION}${APP_BUNDLE}/Contents/Info.plist" ]]
then
if sed -i.bak s_\<string\>0.0.0\<\/string\>_\<string\>${VERSION}\<\/string\>_ "${DISTRIBUTION}${APP_BUNDLE}/Contents/Info.plist"
then
echo "Set bundle's Info.plist to version: \"${VERSION}\""
rm "${DISTRIBUTION}${APP_BUNDLE}/Contents/Info.plist.bak"
else
echo "[Error] Could not set bundle's version in Info.plist."
exit 1
fi
else
echo "[Error] Application bundle does not include an Info.plist file."
exit 1
fi
else
echo "[Error] Application bundle not found: ${DISTRIBUTION}${APP_BUNDLE}"
exit 1
fi
echo
#
# Attempt to deeply codesign the known-to-be-unsigned nwjs libraries and nwjs framework
#
echo "Code signing nwjs libraries and framework within the application bundle: ${DISTRIBUTION}${APP_BUNDLE} with identity: \"${APP_IDENTITY}\""
#
# signing libEGL.dylib
#
codesign -s "$APP_IDENTITY" --deep -f -v --options runtime --timestamp --entitlements "${ENTITLEMENTS}" "${DISTRIBUTION}${APP_BUNDLE}${NWJS_FW_LIBRARIES}libEGL.dylib"
if [ "$?" != "0" ]; then
echo "[Error] Code signing nwjs library failed!" 1>&2
exit 1
fi
#
# libGLESv2.dylib
#
codesign -s "$APP_IDENTITY" --deep -f -v --options runtime --timestamp --entitlements "${ENTITLEMENTS}" "${DISTRIBUTION}${APP_BUNDLE}${NWJS_FW_LIBRARIES}libGLESv2.dylib"
if [ "$?" != "0" ]; then
echo "[Error] Code signing nwjs library failed!" 1>&2
exit 1
fi
#
# libvk_swiftshader.dylib
#
codesign -s "$APP_IDENTITY" --deep -f -v --options runtime --timestamp --entitlements "${ENTITLEMENTS}" "${DISTRIBUTION}${APP_BUNDLE}${NWJS_FW_LIBRARIES}libvk_swiftshader.dylib"
if [ "$?" != "0" ]; then
echo "[Error] Code signing nwjs library failed!" 1>&2
exit 1
fi
#
# nwjs Framework
#
codesign -s "$APP_IDENTITY" --deep -f -v --options runtime --timestamp --entitlements "${ENTITLEMENTS}" "${DISTRIBUTION}${APP_BUNDLE}${NWJS_FW}nwjs Framework"
if [ "$?" != "0" ]; then
echo "[Error] Code signing nwjs framework failed!" 1>&2
exit 1
fi
echo
#
# Attempt to deeply codesign the app bundle
#
echo "Code signing the application bundle (hardened runtime with entitlements): ${DISTRIBUTION}${APP_BUNDLE} with identity: \"${APP_IDENTITY}\""
codesign -s "$APP_IDENTITY" --deep -f -v --options runtime --timestamp --entitlements "${ENTITLEMENTS}" "${DISTRIBUTION}${APP_BUNDLE}"
if [ "$?" != "0" ]; then
echo "[Error] Code signing the application bundle failed!" 1>&2
exit 1
fi
echo
#
# Verify that app is code-signed
# A properly signed app will contain a _CodeSignature directory and CodeResource file
#
echo "Validating application signature..."
if [[ -e "${DISTRIBUTION}${APP_BUNDLE}" ]]
then
#
# Found bundle
#
if [[ -e "${DISTRIBUTION}${APP_BUNDLE}/Contents/_CodeSignature/CodeResources" ]]
then
#
# Found code signature, now we'll check validity
# A single "-v" == "verify app signing", gives no result on valid app signing
#
codesign -v "${DISTRIBUTION}${APP_BUNDLE}"
if [ "$?" != "0" ]; then
echo " [Error] Application signature is invalid!" 1>&2
exit 1
else
echo " Verified ${DISTRIBUTION}${APP_BUNDLE} signature"
fi
else
echo " [Error] Application bundle is not signed."
exit 1
fi
fi
echo
#
# Developer PARALLAX_IDENTIFIER & FTDI_IDENTIFIER (package can be for testing or deployment)
#
if [[ $DEPLOY == true ]]
then
PARALLAX_IDENTIFIER=com.ParallaxInc
# Will get modified to: "com.ParallaxInc.|APP_NAME|" below
FTDI_IDENTIFIER=com.FTDI.driver
# Will get modified to: "com.FTDI.driver.FTDIUSBSerialDriver" below
echo "Package’s CFBundleIdentifiers will be set for deployment"
else
PARALLAX_IDENTIFIER=com.test.ParallaxInc
# Will get modified to: "com.test.ParallaxInc.|APP_NAME|" below
FTDI_IDENTIFIER=com.test.FTDI.driver
# Will get modified to: "com.test.FTDI.driver.FTDIUSBSerialDriver" below
echo "Package’s CFBundleIdentifiers will be set for testing"
fi
#
# touch the entire bundle directory to set most-recent mod dates
#
touch ${DISTRIBUTION}*
#
# If necessary, build the FTDIUSBSerialDriver.kext component package
#
if [[ ${FTDI} == true ]]
then
# is the FTDI Driver kext available?
if [[ -e ./drivers/${FTDIDRIVER_KEXT} ]]
then
echo; echo "Found FTDI USB Serial Driver kext"
DIST_SRC=DistributionFTDI.xml
#
# build the FTDI Driver component package
echo; echo "Building FTDI USB Driver package..."
pkgbuild --root ./drivers/${FTDIDRIVER_KEXT} \
--identifier ${FTDI_IDENTIFIER}.${FTDIDRIVER} \
--timestamp \
--install-location ${FTDIDRIVER_DEST_DIR}${FTDIDRIVER_KEXT} \
--sign "$INST_IDENTITY" \
--version ${VERSION} \
${DISTRIBUTION}FTDIUSBSerialDriver.pkg
else
echo "[Error] FTDI USB Serial Driver kext is missing. Please read $0 comments."
exit 1
fi
else
DIST_SRC=Distribution.xml
fi
echo
#
# Build the application component package
#
echo "Building application package..."
pkgbuild --root "${DISTRIBUTION}${APP_BUNDLE}" \
--identifier "${PARALLAX_IDENTIFIER}.${APP_NAME}" \
--timestamp \
--install-location "${DEFAULT_APP_DIR}${APP_BUNDLE}" \
--sign "$INST_IDENTITY" \
--version ${VERSION} \
"${DISTRIBUTION}${APP_NAME}.pkg"
#
# Write a synthesized distribution xml directly (NO LONGER USED, BUT CAN PROVIDE A DISTRIBUTION XML FILE AS A TEMPLATE)
#
#productbuild --synthesize --sign "$INST_IDENTITY" --timestamp=none --package "${DISTRIBUTION}${APP_NAME}.pkg" --package ${DISTRIBUTION}FTDIUSBSerialDriver.pkg ${RESOURCES}${DIST_SRC}
#
#
# Modify the existing DistributionXXXX.xml only if requiredRestart is requested
#
if [[ ${FTDI} == true ]]
then
if [[ ${RESTART} == true ]]
then
echo; echo "Modifying distribution xml to require restart..."
sed "s/\"none\"\>FTDI/\"${REQUIRE_RESTART_TEXT}\"\>FTDI/g" ${RESOURCES}${DIST_SRC} > ${RESOURCES}${DIST_DST}
else
cat ${RESOURCES}${DIST_SRC} > ${RESOURCES}${DIST_DST}
fi
else
cat ${RESOURCES}${DIST_SRC} > ${RESOURCES}${DIST_DST}
fi
echo
#
# Build the Product Installation Package
#
# note: $DIST_DST holds a copied or modified version of one of the 2 DistributionXXXX.xml files
# The $DIST_DST contains installation options & links to resources for the product package
echo "Building installation package..."
productbuild --distribution ${RESOURCES}${DIST_DST} \
--resources ${RESOURCES} \
--timestamp \
--version $VERSION \
--package-path ${DISTRIBUTION} \
--sign "$INST_IDENTITY" \
"${DISTRIBUTION}${APP_NAME}-${VERSION}-setup-MacOS.pkg"
echo
if [[ -e ${RESOURCES}${DIST_DST} ]]
then
echo "Cleaning up temporary files..."
rm ${RESOURCES}${DIST_DST}
rm ${DISTRIBUTION}${APP_NAME}.pkg
fi
echo
echo "Done!"
exit 0