Skip to content

Instantly share code, notes, and snippets.

@Shkarlatov
Last active March 22, 2024 09:34
Show Gist options
  • Select an option

  • Save Shkarlatov/a17332583d1e65558eb2e288ff50240c to your computer and use it in GitHub Desktop.

Select an option

Save Shkarlatov/a17332583d1e65558eb2e288ff50240c to your computer and use it in GitHub Desktop.
Openssl: Create Self-Sign cert with altName IP
#!/usr/bin/env bash
if [ -z "$1" ]
then
echo "Usage: $0 HOSTNAME IP (optional)"
exit 0
fi
HOST=$1
IP=$2
RSA_SIZE=4096
EXT="subjectAltName=DNS:$HOST,DNS:*.$HOST"
[ -d "certs" ] || mkdir certs
if [ -n "$2" ]
then
EXT="${EXT},IP:$IP"
fi
if [ ! -f "rootca.key" ]
then
openssl genrsa -out rootca.key $RSA_SIZE
fi
if [ ! -f "rootca.crt" ]
then
openssl req -x509 -sha256 -key rootca.key -out rootca.crt \
-subj "/C=RU/ST=CA/L=Moscow/O=Test/OU=RND/CN=My Root CA"
fi
openssl genrsa -out certs/$HOST.key $RSA_SIZE
openssl req -new -sha256 -key certs/$HOST.key \
-subj "/C=RU/ST=CA/L=Moscow/O=Test/OU=RND/CN=$HOST" \
-out certs/$HOST.csr \
-addext $EXT
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:$RSA_SIZE \
-CA rootca.crt -CAkey rootca.key \
-keyout certs/$HOST.key -out certs/$HOST.crt \
-subj "/CN=$HOST" \
-addext "extendedKeyUsage=serverAuth, clientAuth" \
-addext "keyUsage = nonRepudiation, digitalSignature, keyEncipherment, keyAgreement" \
-reqexts v3_req \
-addext $EXT
openssl x509 -in certs/$HOST.crt -text -noout
openssl pkcs12 -export \
-out certs/$HOST.pfx \
-inkey certs/$HOST.key \
-in certs/$HOST.crt \
-certfile certs/$HOST.crt \
-passout pass:
openssl pkcs12 -in certs/$HOST.pfx -info -nodes -passin pass:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment