Debian Codegraph @

contrib/scripts/ast_tls_cert

raw symbols SCIP
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 CA_ENCRYPTION_OPT="-des3"
7 VALIDITY_DAYS=365
8
9 # arguments
10 # $1 "ca" if we are to generate a CA cert
11 # $2 alternate config file name (for ca)
12 # $3 alternate common name
13 # $4 alternate org name
14 create_config () {
15 if [ "$1" = "ca" ]
16 then
17 castring="
18 [ext]
19 basicConstraints=CA:TRUE"
20 fi
21
22 cat > ${2:-"${CONFIG_FILE}"} << EOF
23 [req]
24 distinguished_name = req_distinguished_name
25 prompt = no
26
27 [req_distinguished_name]
28 CN=${3:-"${COMMON_NAME}"}
29 O=${4:-"${ORG_NAME}"}
30 ${castring}
31 EOF
32 }
33
34 create_ca () {
35 echo "Creating CA key ${CAKEY}"
36 openssl genrsa ${CA_ENCRYPTION_OPT} -out ${CAKEY} 4096 > /dev/null
37 if [ $? -ne 0 ];
38 then
39 echo "Failed"
40 exit 1
41 fi
42 echo "Creating CA certificate ${CACERT}"
43 openssl req -new -config ${CACFG} -x509 -days ${VALIDITY_DAYS} -key ${CAKEY} -out ${CACERT} > /dev/null
44 if [ $? -ne 0 ];
45 then
46 echo "Failed"
47 exit 1
48 fi
49 }
50
51 create_cert () {
52 local base=${OUTPUT_DIR}/${OUTPUT_BASE}
53 echo "Creating certificate ${base}.key"
54 openssl genrsa -out ${base}.key ${KEYBITS:-2048} > /dev/null
55 if [ $? -ne 0 ];
56 then
57 echo "Failed"
58 exit 1
59 fi
60 echo "Creating signing request ${base}.csr"
61 openssl req -batch -new -config ${CONFIG_FILE} -key ${base}.key -out ${base}.csr > /dev/null
62 if [ $? -ne 0 ];
63 then
64 echo "Failed"
65 exit 1
66 fi
67 echo "Creating certificate ${base}.crt"
68 openssl x509 -req -days ${VALIDITY_DAYS} -in ${base}.csr -CA ${CACERT} -CAkey ${CAKEY} -set_serial 01 -out ${base}.crt > /dev/null
69 if [ $? -ne 0 ];
70 then
71 echo "Failed"
72 exit 1
73 fi
74 echo "Combining key and crt into ${base}.pem"
75 cat ${base}.key > ${base}.pem
76 cat ${base}.crt >> ${base}.pem
77 }
78
79 usage () {
80 cat << EOF
81 This script is useful for quickly generating self-signed CA, server, and client
82 certificates for use with Asterisk. It is still recommended to obtain
83 certificates from a recognized Certificate Authority and to develop an
84 understanding how SSL certificates work. Real security is hard work.
85
86 OPTIONS:
87 -h Show this message
88 -m Type of cert "client" or "server". Defaults to server.
89 -f Config filename (openssl config file format)
90 -c CA cert filename (creates new CA cert/key as ca.crt/ca.key if not passed)
91 -k CA key filename
92 -e Don't encrypt the CA private key with a passphrase (default is to use 3DES encryption)
93 -b The desired size of the private key in bits. Default is 2048.
94 -C Common name (cert field)
95 This should be the fully qualified domain name or IP address for
96 the client or server. Make sure your certs have unique common
97 names.
98 -O Org name (cert field)
99 An informational string (company name)
100 -o Output filename base (defaults to asterisk)
101 -d Output directory (defaults to the current directory)
102 -v CA/certificate validity in days (defaults to 365)
103
104 Example:
105
106 To create a CA and a server (pbx.mycompany.com) cert with output in /tmp:
107 ast_tls_cert -C pbx.mycompany.com -O "My Company" -d /tmp
108
109 This will create a CA cert and key as well as asterisk.pem and the the two
110 files that it is made from: asterisk.crt and asterisk.key. Copy asterisk.pem
111 and ca.crt somewhere (like /etc/asterisk) and set tlscertfile=/etc/asterisk.pem
112 and tlscafile=/etc/ca.crt. Since this is a self-signed key, many devices will
113 require you to import the ca.crt file as a trusted cert.
114
115 To create a client cert using the CA cert created by the example above:
116 ast_tls_cert -m client -c /tmp/ca.crt -k /tmp/ca.key -C phone1.mycompany.com \\
117 -O "My Company" -d /tmp -o joe_user
118
119 This will create client.crt/key/pem in /tmp. Use this if your device supports
120 a client certificate. Make sure that you have the ca.crt file set up as
121 a tlscafile in the necessary Asterisk configs. Make backups of all .key files
122 in case you need them later.
123 EOF
124 }
125
126 if ! type openssl >/dev/null 2>&1
127 then
128 echo "This script requires openssl to be in the path"
129 exit 1
130 fi
131
132 OUTPUT_BASE=asterisk # Our default cert basename
133 CERT_MODE=server
134 ORG_NAME=${DEFAULT_ORG}
135
136 while getopts "hf:c:ek:o:d:m:C:O:b:v:" OPTION
137 do
138 case ${OPTION} in
139 h)
140 usage
141 exit 1
142 ;;
143 f)
144 CONFIG_FILE=${OPTARG}
145 ;;
146 c)
147 CACERT=${OPTARG}
148 ;;
149 e)
150 CA_ENCRYPTION_OPT=""
151 ;;
152 k)
153 CAKEY=${OPTARG}
154 ;;
155 b)
156 KEYBITS=${OPTARG}
157 ;;
158 v)
159 VALIDITY_DAYS=${OPTARG}
160 ;;
161 o)
162 OUTPUT_BASE=${OPTARG}
163 ;;
164 d)
165 OUTPUT_DIR=${OPTARG}
166 ;;
167 m)
168 CERT_MODE=${OPTARG}
169 ;;
170 C)
171 COMMON_NAME=${OPTARG}
172 ;;
173 O)
174 ORG_NAME=${OPTARG}
175 ;;
176 ?)
177 usage
178 exit
179 ;;
180 esac
181 done
182
183 if [ -z "${OUTPUT_DIR}" ]
184 then
185 OUTPUT_DIR=.
186 else
187 mkdir -p "${OUTPUT_DIR}"
188 fi
189
190 umask 177
191
192 case "${CERT_MODE}" in
193 server)
194 COMMON_NAME=${COMMON_NAME:-"${DEFAULT_SERVER_CN}"}
195 ;;
196 client)
197 COMMON_NAME=${COMMON_NAME:-"${DEFAULT_CLIENT_CN}"}
198 ;;
199 *)
200 echo
201 echo "Unknown mode. Exiting."
202 exit 1
203 ;;
204 esac
205
206 if [ -z "${CONFIG_FILE}" ]
207 then
208 CONFIG_FILE="${OUTPUT_DIR}/tmp.cfg"
209 echo
210 echo "No config file specified, creating '${CONFIG_FILE}'"
211 echo "You can use this config file to create additional certs without"
212 echo "re-entering the information for the fields in the certificate"
213 create_config
214 fi
215
216 if [ -z ${CACERT} ]
217 then
218 CAKEY=${OUTPUT_DIR}/ca.key
219 CACERT=${OUTPUT_DIR}/ca.crt
220 CACFG=${OUTPUT_DIR}/ca.cfg
221 if [ ! -r "$CAKEY" ] && [ ! -r "$CACFG" ]; then
222 create_config ca "${CACFG}" "${DEFAULT_CA_CN}" "${DEFAULT_CA_ORG}"
223 fi
224 if [ ! -r "$CACERT" ]; then
225 create_ca
226 fi
227 else
228 if [ -z ${CAKEY} ]
229 then
230 echo "-k must be specified if -c is"
231 exit 1
232 fi
233 fi
234
235 create_cert