Skip to content

Instantly share code, notes, and snippets.

@ivan-pinatti
Created January 29, 2018 17:18
Show Gist options
  • Save ivan-pinatti/bf4398bdb74b81620aaf31610fcd8461 to your computer and use it in GitHub Desktop.
Save ivan-pinatti/bf4398bdb74b81620aaf31610fcd8461 to your computer and use it in GitHub Desktop.
Create self-signed certificate - #linux #openssl #certificate
#!/usr/bin/env bash
: ' Script to create self-signed certificate
'
# check if debug flag is set
if [ "${DEBUG}" = true ]; then
set -x # enable print commands and their arguments as they are executed.
export # show all declared variables (includes system variables)
whoami # print current user
else
# unset if flag is not set
unset DEBUG
fi
# bash default parameters
set -o errexit # make your script exit when a command fails
set -o pipefail # exit status of the last command that threw a non-zero exit code is returned
set -o nounset # exit when your script tries to use undeclared variables
# binaries
__MKTEMP=$(which mktemp)
__OPENSSL=$(which openssl)
# parameters
__url="${1:-"www.mycompany.com"}"
__company_name="${2:-"My Company Inc"}"
__country_code="${3:-"US"}"
# create temp folder
__temp_folder=$(${__MKTEMP} --directory)
# create the key
${__OPENSSL} genrsa -out ${__temp_folder}/key.pem
# create the certifcate request
${__OPENSSL} req -new \
-subj "/CN=${__url}/O=${__company_name}/C=${__country_code}" \
-key ${__temp_folder}/key.pem \
-out ${__temp_folder}/csr.pem
# create the certificate
${__OPENSSL} x509 -req \
-days 365 \
-in ${__temp_folder}/csr.pem \
-signkey ${__temp_folder}/key.pem \
-out ${__temp_folder}/cert.pem
echo -e "\nCertificate and key created sucessfully, please check folder ${__temp_folder}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment