Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save opragel/5b85354bcf3ce10b33f8 to your computer and use it in GitHub Desktop.
Save opragel/5b85354bcf3ce10b33f8 to your computer and use it in GitHub Desktop.
add_certificate_to_keychain_and_trust.sh
#!/bin/bash
KEYCHAIN_PATH="/Library/Keychains/System.keychain"
CERTIFICATE_CER_PATHS=( \
"/private/tmp/my-certificate-1.cer" \
"/private/tmp/my-certificate-2.cer" )
for certificateCerPath in "${CERTIFICATE_CER_PATHS[@]}"; do
certificatePemPath="$certificateCerPath.pem"
openssl x509 -inform der -in "$certificateCerPath" -out "$certificatePemPath"
opensslVerifyResponse=$(openssl verify -CAfile "$certificatePemPath" "$certificatePemPath" | grep -o 'error.*at .*depth')
if [ "$opensslVerifyResponse" ]; then
security add-trusted-cert -d -r trustAsRoot -k "$KEYCHAIN_PATH" "$certificateCerPath"
else
security add-trusted-cert -d -r trustRoot -k "$KEYCHAIN_PATH" "$certificateCerPath"
fi
rm "$certificatePemPath"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment