3 DEFAULT_CA_CN="Asterisk Private CA"
4 DEFAULT_CLIENT_CN="asterisk"
5 DEFAULT_SERVER_CN=`hostname -f`
8 # $1 "ca" if we are to generate a CA cert
9 # $2 alternate config file name (for ca)
10 # $3 alternate common name
11 # $4 alternate org name
17 basicConstraints=CA:TRUE"
20 cat > ${2:-"${CONFIG_FILE}"} << EOF
22 distinguished_name = req_distinguished_name
25 [req_distinguished_name]
26 CN=${3:-"${COMMON_NAME}"}
33 echo "Creating CA key ${CAKEY}"
34 openssl genrsa -des3 -out ${CAKEY} 4096 > /dev/null
40 echo "Creating CA certificate ${CACERT}"
41 openssl req -new -config ${CACFG} -x509 -days 365 -key ${CAKEY} -out ${CACERT} > /dev/null
50 local base=${OUTPUT_DIR}/${OUTPUT_BASE}
51 echo "Creating certificate ${base}.key"
52 openssl genrsa -out ${base}.key 1024 > /dev/null
58 echo "Creating signing request ${base}.csr"
59 openssl req -batch -new -config ${CONFIG_FILE} -key ${base}.key -out ${base}.csr > /dev/null
65 echo "Creating certificate ${base}.crt"
66 openssl x509 -req -days 365 -in ${base}.csr -CA ${CACERT} -CAkey ${CAKEY} -set_serial 01 -out ${base}.crt > /dev/null
72 echo "Combining key and crt into ${base}.pem"
73 cat ${base}.key > ${base}.pem
74 cat ${base}.crt >> ${base}.pem
79 This script is useful for quickly generating self-signed CA, server, and client
80 certificates for use with Asterisk. It is still recommended to obtain
81 certificates from a recognized Certificate Authority and to develop an
82 understanding how SSL certificates work. Real security is hard work.
86 -m Type of cert "client" or "server". Defaults to server.
87 -f Config filename (openssl config file format)
88 -c CA cert filename (creates new CA cert/key as ca.crt/ca.key if not passed)
90 -C Common name (cert field)
91 This should be the fully qualified domain name or IP address for
92 the client or server. Make sure your certs have unique common
94 -O Org name (cert field)
95 An informational string (company name)
96 -o Output filename base (defaults to asterisk)
97 -d Output directory (defaults to the current directory)
101 To create a CA and a server (pbx.mycompany.com) cert with output in /tmp:
102 ast_tls_cert -C pbx.mycompany.com -O "My Company" -d /tmp
104 This will create a CA cert and key as well as asterisk.pem and the the two
105 files that it is made from: asterisk.crt and asterisk.key. Copy asterisk.pem
106 and ca.crt somewhere (like /etc/asterisk) and set tlscertfile=/etc/asterisk.pem
107 and tlscafile=/etc/ca.crt. Since this is a self-signed key, many devices will
108 require you to import the ca.crt file as a trusted cert.
110 To create a client cert using the CA cert created by the example above:
111 ast_tls_cert -m client -c /tmp/ca.crt -k /tmp/ca.key -C phone1.mycompany.com \\
112 -O "My Company" -d /tmp -o joe_user
114 This will create client.crt/key/pem in /tmp. Use this if your device supports
115 a client certificate. Make sure that you have the ca.crt file set up as
116 a tlscafile in the necessary Asterisk configs. Make backups of all .key files
117 in case you need them later.
121 if ! type openssl >/dev/null 2>&1
123 echo "This script requires openssl to be in the path"
127 OUTPUT_BASE=asterisk # Our default cert basename
129 ORG_NAME=${DEFAULT_ORG}
131 while getopts "hf:c:k:o:d:m:C:O:" OPTION
139 CONFIG_FILE=${OPTARG}
148 OUTPUT_BASE=${OPTARG}
157 COMMON_NAME=${OPTARG}
169 if [ -z "${OUTPUT_DIR}" ]
173 mkdir -p "${OUTPUT_DIR}"
178 case "${CERT_MODE}" in
180 COMMON_NAME=${COMMON_NAME:-"${DEFAULT_SERVER_CN}"}
183 COMMON_NAME=${COMMON_NAME:-"${DEFAULT_CLIENT_CN}"}
187 echo "Unknown mode. Exiting."
192 if [ -z "${CONFIG_FILE}" ]
194 CONFIG_FILE="${OUTPUT_DIR}/tmp.cfg"
196 echo "No config file specified, creating '${CONFIG_FILE}'"
197 echo "You can use this config file to create additional certs without"
198 echo "re-entering the information for the fields in the certificate"
204 CAKEY=${OUTPUT_DIR}/ca.key
205 CACERT=${OUTPUT_DIR}/ca.crt
206 CACFG=${OUTPUT_DIR}/ca.cfg
207 create_config ca "${CACFG}" "${DEFAULT_CA_CN}" "${DEFAULT_CA_ORG}"
212 echo "-k must be specified if -c is"