Last active
October 17, 2024 12:49
-
-
Save luckman212/ec52e9291f27bc39c2eecee07e7a9aa7 to your computer and use it in GitHub Desktop.
grab Apple DEVELOPMENT_TEAM ID from Keychain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
CODESIGN_CN_STRING='Developer ID Application' | |
#CODESIGN_CN_STRING='Apple Development' | |
#requires openssl@3 from Homebrew | |
_openssl=$(brew --prefix openssl 2>/dev/null)/bin/openssl | |
[[ -x $_openssl ]] || { echo "missing openssl, try \`brew install openssl\`"; exit 1; } | |
#find development cert | |
csids=$(security find-identity -v -p codesigning | grep -E '[A-F0-9]{40}') | |
[[ -n $csids ]] || { echo 1>&2 "could not find codesigning identity"; exit 1; } | |
read -r sha1 cn _ < <(sed -En "s/^.*([A-F0-9]{40}).*$CODESIGN_CN_STRING.*\((.*)\).*$/\1 \2/p" <<<"$csids") | |
[[ -n $cn && -n $sha1 ]] || { echo 1>&2 "could not find valid development cert"; exit 1; } | |
#make temp dir | |
outdir=$(mktemp -d /private/tmp/teamid.XXXXXX) | |
[[ -n $outdir ]] || { echo "error creating temp dir"; exit 1; } | |
#export cert | |
if ! security find-certificate -a -c "$cn" -Z -p >"${outdir}/${cn}.pem"; then | |
echo "error exporting cert from Keychain" | |
exit 1 | |
fi | |
#check for hash match | |
certhash=$(awk -v h="$sha1" '$0 ~ "^SHA-1 hash: " h {print $NF; exit}' "${outdir}/${cn}.pem") | |
[[ $certhash == "$sha1" ]] || { echo "hash mismatch! ($certhash vs $sha1)"; exit 1; } | |
#output DEVELOPMENT_TEAM | |
$_openssl x509 -in "${outdir}/${cn}.pem" -subject -noout | | |
sed -En 's/.*OU ?= ?([^,]+),.*$/\1/p' | |
#cleanup | |
rm -r "${outdir:?}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
src: https://stackoverflow.com/a/70464809/617864