Skip to content

Instantly share code, notes, and snippets.

@omarmciver
Created August 6, 2024 16:03
Show Gist options
  • Save omarmciver/decdba5af2a9e2a43171de6b5cb3120d to your computer and use it in GitHub Desktop.
Save omarmciver/decdba5af2a9e2a43171de6b5cb3120d to your computer and use it in GitHub Desktop.
[create_self_signed_cert] #Shell
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <hostname> <IP address>"
exit 1
fi
HOSTNAME=$1
IP=$2
# Generate a Private Key
openssl genpkey -algorithm RSA -out ${HOSTNAME}.key -pkeyopt rsa_keygen_bits:2048
# Create a Certificate Signing Request (CSR)
openssl req -new -key ${HOSTNAME}.key -out ${HOSTNAME}.csr -subj "/CN=${HOSTNAME}"
# Create a Configuration File for Extensions
cat > ${HOSTNAME}.ext <<EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = ${HOSTNAME}
IP.1 = ${IP}
EOF
# Generate the Self-Signed Certificate
openssl x509 -req -in ${HOSTNAME}.csr -signkey ${HOSTNAME}.key -out ${HOSTNAME}.crt -days 365 -extfile ${HOSTNAME}.ext
# Clean up intermediate files
rm ${HOSTNAME}.csr
rm ${HOSTNAME}.ext
echo "Certificate and key have been generated:"
echo "Certificate: ${HOSTNAME}.crt"
echo "Private Key: ${HOSTNAME}.key"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment