Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scripts to generate certificate and add it to the macOS Keychain and iOS Keychain #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Scripts/add-certificate-to-ios-keychain.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh
set -e

CERT_FILE=root-ca.pem
if ! test -f "$CERT_FILE"; then
echo "$CERT_FILE file doesn't exists. Generate it using generate-self-signed-certificate.sh"
exit 1
fi

# Find booted iOS Simulator
while true; do
export UDID=$(xcrun simctl list devices | grep "(Booted)" | grep -E -o -i "([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})")
if [ -z "$UDID" ]
then
echo "Please launch an iOS Simulator in which you would like to install certificate and press any key"
read input
else
break
fi
done

# Add certificate to iOS Simulator
echo "Adding certificate to iOS Sumulator..."
xcrun simctl keychain booted add-root-cert root-ca.pem

# Restart booted iOS Simulator
echo "Restarning iOS Sumulator..."
xcrun simctl shutdown $UDID
xcrun simctl boot $UDID

echo "Certificate has been successfully added to the iOS Simulator Keychain"
17 changes: 17 additions & 0 deletions Scripts/add-certificate-to-system-keychain.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
set -e

CERT_FILE=root-ca.pem
if ! test -f "$CERT_FILE"; then
echo "$CERT_FILE file doesn't exists. Generate it using generate-certificate.sh."
exit 1
fi

# Add certificate to macOS Keychain
echo "You will be promted to authenticate to mark certificate as trusted"

# Get path to the local keychain and trim whitespaces and quotation marks symbol
LOGIN_KEYCHAIN="$(security login-keychain | sed 's/[[:space:]]*"//g')"
security add-trusted-cert -k $LOGIN_KEYCHAIN root-ca.pem
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to add to the system Keychain ?

sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain root-ca.pem

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems, no.


echo "Certificate has been successfully added to the macOS Keychain"
19 changes: 19 additions & 0 deletions Scripts/cert.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[ ca ]
default_ca = CA_default
[ CA_default ]
default_md = sha256
[ v3_ca ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer
basicConstraints = critical,CA:true
keyUsage=critical,keyCertSign
extendedKeyUsage = serverAuth,clientAuth
[ req ]
prompt = no
distinguished_name = req_distinguished_name
[ req_distinguished_name ]
C=RU
L=RU
O=Catbird
CN=http://localhost
OU=Catbird
5 changes: 5 additions & 0 deletions Scripts/extract-certificate-from-keychain.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
set -e

# Get an existing Catbird certificate
security find-certificate -c Catbird -p > root-ca.pem
29 changes: 29 additions & 0 deletions Scripts/generate-self-signed-certificate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh
set -e

CONFIG_FILE=cert.config
if ! test -f "$CONFIG_FILE"; then
echo "$CONFIG_FILE file doesn't exists. Add cert.config file with certificate configuration."
exit 1
fi

echo "Creating new certificate from cert.config"

echo "Enter password for new certificate."
read -s -p "Password: " password

# Generate RSA Key
openssl genrsa -aes256 -passout pass:"$password" -out key.pem 2048

# Convert RSA Key from .pem to .key format
openssl rsa -outform der -in key.pem -out cert.key -passin pass:"$password"

echo "Key created: cert.key"

# Generate the self-signed certificate and private key
openssl req -x509 -new -nodes -passin pass:"$password" -config "$CONFIG_FILE" -key key.pem -sha256 -extensions v3_ca -days 365 -out root-ca.pem

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Vaport, the key is needed in the Key format .key

openssl rsa -outform der -in key.pem -out cert.key -passin pass:"$password"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

# Cleanup
rm key.pem

echo "Certificate created: root_ca.pem"