Skip to content

Instantly share code, notes, and snippets.

@command-tab
Created April 12, 2012 20:17
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save command-tab/2370710 to your computer and use it in GitHub Desktop.
Save command-tab/2370710 to your computer and use it in GitHub Desktop.
Extract Certificate Subject from a Provisioning Profile
# Parse a provisioning profile
# Extract the first DeveloperCertificates <data> entry
# Remove any leading whitespace
# Remove any blank lines
# Base64 decode the blob
# Parse the .cer with OpenSSL
# Extract the first line, which is the certificate subject (the rest is the cert blob)
# End up with a string like: subject= /UID=AABBCCDDEE/CN=iPhone Developer: First Last (FFGGHHIIJJ)/C=US
# Note: Uses xmlstarlet to parse the plist, but you could probably use PlistBuddy or grep, too
security cms -D -i "/path/to/some.mobileprovision" | \
xml sel -t -v "/plist/dict/key[. = 'DeveloperCertificates']/following-sibling::array[1]/data[1]" | \
awk '{print $1}' | sed '/^$/d' | base64 -D | openssl x509 -subject -inform der | head -n 1
@ikrabbe
Copy link

ikrabbe commented May 22, 2015

# A simpler version with less dependencies and more informative output
fn="${1:-none}"
security cms -D -i "$fn" |sed -ne 's/^.*<data>\(.*\)<\/data>.*$/\1/p' |base64 -D | openssl x509 -subject -   inform der | head -n 1
security cms -D -i "$fn" |sed -ne 's/^.*<data>\(.*\)<\/data>.*$/\1/p' |base64 -D | shasum -

# The first command reads the subject lines, the second caclulates the SHA1 sum, as it's useable for codesign and PackageApplication --sign arguments together with the --embed Profile
# actually security cms -D -i ... can be put into the sed step with

sed -ne '2,/^<\/plist>/s/^.*<data>\(.*\)<\/data>.*$/\1/p' "$fn"

@below
Copy link

below commented Dec 7, 2015

Hrm, neither script works for me.

First says:
unable to load certificate
18494:error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long:/SourceCache/OpenSSL098/OpenSSL098-52.40.1/src/crypto/asn1/asn1_lib.c:153:

Second says:
unknown option -
usage: x509 args

@punksta
Copy link

punksta commented Jul 1, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment