Last active
August 2, 2018 20:44
-
-
Save brian9206/01552cd9f48ce5ad938a30384b93afa9 to your computer and use it in GitHub Desktop.
Create certificate with custom CA
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
#!/bin/bash | |
# Config | |
COUNTRY="HK" | |
STATE="Hong Kong" | |
ORGANIZATION="Brian Choi" | |
# Create cert folder | |
mkdir -p cert | |
# Get domains | |
echo 'Enter your domains (Separated by space):' | |
read -a domains | |
if [ ${#domains[0]} -le 0 ] | |
then | |
echo 'ERROR: No domain found.' | |
exit 1 | |
fi | |
# Get IPs | |
echo 'Enter your IPs (Separated by space, optional):' | |
read -a ips | |
# Print result | |
echo | |
echo "Settings:" | |
for ((i = 0; i < ${#domains[@]}; i++)); do | |
echo "DNS."$(($i + 1))"="${domains[$i]} | |
done | |
for ((i = 0; i < ${#ips[@]}; i++)); do | |
echo "IP."$(($i + 1))"="${ips[$i]} | |
done | |
echo | |
echo "Is that ok? (Enter to continue)" | |
read | |
# Create domain.ext | |
echo 'authorityKeyIdentifier=keyid,issuer | |
basicConstraints=CA:FALSE | |
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment | |
subjectAltName = @alt_names | |
[alt_names]' > "cert/"${domains[0]}".ext" | |
for ((i = 0; i < ${#domains[@]}; i++)); do | |
echo "DNS."$(($i + 1))"="${domains[$i]} >> "cert/${domains[0]}.ext" | |
done | |
for ((i = 0; i < ${#ips[@]}; i++)); do | |
echo "IP."$(($i + 1))"="${ips[$i]} >> "cert/${domains[0]}.ext" | |
done | |
# Create private key | |
openssl genrsa -out "cert/${domains[0]}.key" 2048 | |
# Create CSR | |
openssl req -new -sha256 -key "cert/${domains[0]}.key" -subj "/C=$COUNTRY/ST=$STATE/O=$ORGANIZATION/CN=${domains[0]}" -out "cert/${domains[0]}.csr" | |
# Create certificate | |
openssl x509 -req -in "cert/${domains[0]}.csr" -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out "cert/${domains[0]}.crt" -days 3650 -sha256 -extfile "cert/${domains[0]}.ext" | |
echo | |
echo Done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create CA by using the following command before using the above script: