ast_tls_cert: don't recreate generated files
[asterisk/asterisk.git] / contrib / scripts / ast_tls_cert
1 #!/bin/sh -e
2 DEFAULT_ORG="Asterisk"
3 DEFAULT_CA_CN="Asterisk Private CA"
4 DEFAULT_CLIENT_CN="asterisk"
5 DEFAULT_SERVER_CN=`hostname -f`
6
7 # arguments
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
12 create_config () {
13         if [ "$1" = "ca" ]
14         then
15 castring="
16 [ext]
17 basicConstraints=CA:TRUE"
18         fi
19
20 cat > ${2:-"${CONFIG_FILE}"} << EOF
21 [req]
22 distinguished_name = req_distinguished_name
23 prompt = no
24
25 [req_distinguished_name]
26 CN=${3:-"${COMMON_NAME}"}
27 O=${4:-"${ORG_NAME}"}
28 ${castring}
29 EOF
30 }
31
32 create_ca () {
33         echo "Creating CA key ${CAKEY}"
34         openssl genrsa -des3 -out ${CAKEY} 4096 > /dev/null
35         if [ $? -ne 0 ];
36         then
37                 echo "Failed"
38                 exit 1
39         fi
40         echo "Creating CA certificate ${CACERT}"
41         openssl req -new -config ${CACFG} -x509 -days 365 -key ${CAKEY} -out ${CACERT} > /dev/null
42         if [ $? -ne 0 ];
43         then
44                 echo "Failed"
45                 exit 1
46         fi
47 }
48
49 create_cert () {
50         local base=${OUTPUT_DIR}/${OUTPUT_BASE}
51         echo "Creating certificate ${base}.key"
52         openssl genrsa -out ${base}.key 1024 > /dev/null
53         if [ $? -ne 0 ];
54         then
55                 echo "Failed"
56                 exit 1
57         fi
58         echo "Creating signing request ${base}.csr"
59         openssl req -batch -new -config ${CONFIG_FILE} -key ${base}.key -out ${base}.csr > /dev/null
60         if [ $? -ne 0 ];
61         then
62                 echo "Failed"
63                 exit 1
64         fi
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
67         if [ $? -ne 0 ];
68         then
69                 echo "Failed"
70                 exit 1
71         fi
72         echo "Combining key and crt into ${base}.pem"
73         cat ${base}.key > ${base}.pem
74         cat ${base}.crt >> ${base}.pem
75 }
76
77 usage () {
78 cat << EOF
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.
83
84 OPTIONS:
85   -h  Show this message
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)
89   -k  CA key filename
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
93         names.
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)
98
99 Example:
100
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
103
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.
109
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
113
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.
118 EOF
119 }
120
121 if ! type openssl >/dev/null 2>&1
122 then
123         echo "This script requires openssl to be in the path"
124         exit 1
125 fi
126
127 OUTPUT_BASE=asterisk # Our default cert basename
128 CERT_MODE=server
129 ORG_NAME=${DEFAULT_ORG}
130
131 while getopts "hf:c:k:o:d:m:C:O:" OPTION
132 do
133         case ${OPTION} in
134                 h)
135                         usage
136                         exit 1
137                         ;;
138                 f)
139                         CONFIG_FILE=${OPTARG}
140                         ;;
141                 c)
142                         CACERT=${OPTARG}
143                         ;;
144                 k)
145                         CAKEY=${OPTARG}
146                         ;;
147                 o)
148                         OUTPUT_BASE=${OPTARG}
149                         ;;
150                 d)
151                         OUTPUT_DIR=${OPTARG}
152                         ;;
153                 m)
154                         CERT_MODE=${OPTARG}
155                         ;;
156                 C)
157                         COMMON_NAME=${OPTARG}
158                         ;;
159                 O)
160                         ORG_NAME=${OPTARG}
161                         ;;
162                 ?)
163                         usage
164                         exit
165                         ;;
166         esac
167 done
168
169 if [ -z "${OUTPUT_DIR}" ]
170 then
171         OUTPUT_DIR=.
172 else
173         mkdir -p "${OUTPUT_DIR}"
174 fi
175
176 umask 177
177
178 case "${CERT_MODE}" in
179         server)
180                 COMMON_NAME=${COMMON_NAME:-"${DEFAULT_SERVER_CN}"}
181                 ;;
182         client)
183                 COMMON_NAME=${COMMON_NAME:-"${DEFAULT_CLIENT_CN}"}
184                 ;;
185         *)
186                 echo
187                 echo "Unknown mode. Exiting."
188                 exit 1
189                 ;;
190 esac
191
192 if [ -z "${CONFIG_FILE}" ]
193 then
194         CONFIG_FILE="${OUTPUT_DIR}/tmp.cfg"
195         echo
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"
199         create_config
200 fi
201
202 if [ -z ${CACERT} ]
203 then
204         CAKEY=${OUTPUT_DIR}/ca.key
205         CACERT=${OUTPUT_DIR}/ca.crt
206         CACFG=${OUTPUT_DIR}/ca.cfg
207         if [ ! -r "$CAKEY" ] &&  [ ! -r "$CACFG" ]; then
208                 create_config ca "${CACFG}" "${DEFAULT_CA_CN}" "${DEFAULT_CA_ORG}"
209         fi
210         if  [ ! -r "$CACERT" ]; then
211                 create_ca
212         fi
213 else
214         if [ -z ${CAKEY} ]
215         then
216                 echo "-k must be specified if -c is"
217                 exit 1
218         fi
219 fi
220
221 create_cert