Skip to content

Instantly share code, notes, and snippets.

@scolton99
Created June 16, 2022 14:08
Show Gist options
  • Save scolton99/dd2e03b06643c519e4f3ecc834e50a9d to your computer and use it in GitHub Desktop.
Save scolton99/dd2e03b06643c519e4f3ecc834e50a9d to your computer and use it in GitHub Desktop.
Use OpenSSL to generate a self-signed certificate with SANs.
if [ -f ".req.conf" ]; then
rm .req.conf
fi
STRBASE="[req]\ndistinguished_name = dn\nx509_extensions = v3_req\nprompt = no\n\n[dn]\nC = US\nST = Illinois\nL = Chicago\nO = Northwestern University\nOU = Information Technology\nCN = $1\n\n[v3_req]\nkeyUsage = keyEncipherment, nonRepudiation, digitalSignature, dataEncipherment, keyAgreement, keyCertSign\nextendedKeyUsage = serverAuth\nbasicConstraints = CA:true\nsubjectKeyIdentifier = hash\nauthorityKeyIdentifier = keyid:always,issuer:always\nsubjectAltName = @alt_names\n\n[alt_names]"
C=1
for var in "$@"
do
STRBASE="${STRBASE}\nDNS.${C} = ${var}"
C=$(( $C + 1 ))
done
echo -e $STRBASE >> .req.conf
openssl req -new -x509 -newkey rsa:2048 -sha256 -nodes -keyout $1.key -days 730 -out $1.crt -config .req.conf
openssl pkcs12 -export -out $1.pfx -inkey $1.key -in $1.crt
rm .req.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment