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 For a server cert, this should be the same address that clients
67 attempt to connect to. Usually this will be the Fully Qualified
68 Domain Name, but might be the IP of the server. For a CA or client
69 cert, it is merely informational. Make sure your certs have unique
71 -O Org name (cert field)
72 An informational string (company name)
73 -o Output filename base (defaults to asterisk)
74 -d Output directory (defaults to the current directory)
78 To create a CA and a server (pbx.mycompany.com) cert with output in /tmp:
79 ast_tls_cert -C pbx.mycompany.com -O "My Company" -d /tmp
81 This will create a CA cert and key as well as asterisk.pem and the the two
82 files that it is made from: asterisk.crt and asterisk.key. Copy asterisk.pem
83 and ca.crt somewhere (like /etc/asterisk) and set tlscertfile=/etc/asterisk.pem
84 and tlscafile=/etc/ca.crt. Since this is a self-signed key, many devices will
85 require you to import the ca.crt file as a trusted cert.
87 To create a client cert using the CA cert created by the example above:
88 ast_tls_cert -m client -c /tmp/ca.crt -k /tmp/ca.key -C "Joe User" -O \\
89 "My Company" -d /tmp -o joe_user
91 This will create client.crt/key/pem in /tmp. Use this if your device supports
92 a client certificate. Make sure that you have the ca.crt file set up as
93 a tlscafile in the necessary Asterisk configs. Make backups of all .key files
94 in case you need them later.
98 if ! type openssl >/dev/null 2>&1
100 echo "This script requires openssl to be in the path"
104 OUTPUT_BASE=asterisk # Our default cert basename
106 ORG_NAME=${DEFAULT_ORG}
110 while getopts "hf:c:k:o:d:m:C:O:" OPTION
118 CONFIG_FILE=${OPTARG}
127 OUTPUT_BASE=${OPTARG}
136 COMMON_NAME=${OPTARG}
148 if [ -z "${OUTPUT_DIR}" ]
152 mkdir -p "${OUTPUT_DIR}"
155 case "${CERT_MODE}" in
157 COMMON_NAME=${COMMON_NAME:-"${DEFAULT_SERVER_CN}"}
160 COMMON_NAME=${COMMON_NAME:-"${DEFAULT_CLIENT_CN}"}
164 echo "Unknown mode. Exiting."
169 if [ -z "${CONFIG_FILE}" ]
171 CONFIG_FILE="${OUTPUT_DIR}/tmp.cfg"
173 echo "No config file specified, creating '${CONFIG_FILE}'"
174 echo "You can use this config file to create additional certs without"
175 echo "re-entering the information for the fields in the certificate"
181 CAKEY=${OUTPUT_DIR}/ca.key
182 CACERT=${OUTPUT_DIR}/ca.crt
183 CACFG=${OUTPUT_DIR}/ca.cfg
184 create_config ca "${CACFG}" "${DEFAULT_CA_CN}" "${DEFAULT_CA_ORG}"