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 ${CAKEY}"
34 openssl genrsa -des3 -out ${CAKEY} 4096 > /dev/null
35 echo "Creating ${CACERT}"
36 openssl req -new -config ${CACFG} -x509 -days 365 -key ${CAKEY} -out ${CACERT} > /dev/null
40 local base=${OUTPUT_DIR}/${OUTPUT_BASE}
41 echo "Creating ${base}.key"
42 openssl genrsa -out ${base}.key 1024 > /dev/null
43 echo "Creating signing request"
44 openssl req -batch -new -config ${CONFIG_FILE} -key ${base}.key -out ${base}.csr > /dev/null
45 echo "Creating ${base}.crt"
46 openssl x509 -req -days 365 -in ${base}.csr -CA ${CACERT} -CAkey ${CAKEY} -set_serial 01 -out ${base}.crt > /dev/null
47 echo "Combining key and crt into ${base}.pem"
48 cat ${base}.key > ${base}.pem
49 cat ${base}.crt >> ${base}.pem
54 This script is useful for quickly generating self-signed CA, server, and client
55 certificates for use with Asterisk. It is still recommended to obtain
56 certificates from a recognized Certificate Authority and to develop an
57 understanding how SSL certificates work. Real security is hard work.
61 -m Type of cert "client" or "server". Defaults to server.
62 -f Config filename (openssl config file format)
63 -c CA cert filename (creates new CA cert/key as ca.crt/ca.key if not passed)
65 -C Common name (cert field)
66 This should be the fully qualified domain name or IP address for
67 the client or server. Make sure your certs have unique common
69 -O Org name (cert field)
70 An informational string (company name)
71 -o Output filename base (defaults to asterisk)
72 -d Output directory (defaults to the current directory)
76 To create a CA and a server (pbx.mycompany.com) cert with output in /tmp:
77 ast_tls_cert -C pbx.mycompany.com -O "My Company" -d /tmp
79 This will create a CA cert and key as well as asterisk.pem and the the two
80 files that it is made from: asterisk.crt and asterisk.key. Copy asterisk.pem
81 and ca.crt somewhere (like /etc/asterisk) and set tlscertfile=/etc/asterisk.pem
82 and tlscafile=/etc/ca.crt. Since this is a self-signed key, many devices will
83 require you to import the ca.crt file as a trusted cert.
85 To create a client cert using the CA cert created by the example above:
86 ast_tls_cert -m client -c /tmp/ca.crt -k /tmp/ca.key -C phone1.mycompany.com \\
87 -O "My Company" -d /tmp -o joe_user
89 This will create client.crt/key/pem in /tmp. Use this if your device supports
90 a client certificate. Make sure that you have the ca.crt file set up as
91 a tlscafile in the necessary Asterisk configs. Make backups of all .key files
92 in case you need them later.
96 if ! type openssl >/dev/null 2>&1
98 echo "This script requires openssl to be in the path"
102 OUTPUT_BASE=asterisk # Our default cert basename
104 ORG_NAME=${DEFAULT_ORG}
106 while getopts "hf:c:k:o:d:m:C:O:" OPTION
114 CONFIG_FILE=${OPTARG}
123 OUTPUT_BASE=${OPTARG}
132 COMMON_NAME=${OPTARG}
144 if [ -z "${OUTPUT_DIR}" ]
148 mkdir -p "${OUTPUT_DIR}"
153 case "${CERT_MODE}" in
155 COMMON_NAME=${COMMON_NAME:-"${DEFAULT_SERVER_CN}"}
158 COMMON_NAME=${COMMON_NAME:-"${DEFAULT_CLIENT_CN}"}
162 echo "Unknown mode. Exiting."
167 if [ -z "${CONFIG_FILE}" ]
169 CONFIG_FILE="${OUTPUT_DIR}/tmp.cfg"
171 echo "No config file specified, creating '${CONFIG_FILE}'"
172 echo "You can use this config file to create additional certs without"
173 echo "re-entering the information for the fields in the certificate"
179 CAKEY=${OUTPUT_DIR}/ca.key
180 CACERT=${OUTPUT_DIR}/ca.crt
181 CACFG=${OUTPUT_DIR}/ca.cfg
182 create_config ca "${CACFG}" "${DEFAULT_CA_CN}" "${DEFAULT_CA_ORG}"