Debian Codegraph @

contrib/scripts/ast_tls_cert

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/bin/sh`. -e
2 DEFAULT_ORG="Asterisk"
3 DEFAULT_CA_CN="Asterisk Private CA"
4 DEFAULT_CLIENT_CN="asterisk"
5 DEFAULT_SERVER_CN=`hostnamescip-shell system . . 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_configscip-shell shell shell-project 0.0.0 create_config(). () {
15 if [scip-shell system . . `[`. "$1" = "ca" ]
16 then
17 castring="
18 [ext]
19 basicConstraints=CA:TRUE"
20 fi
21
22 catscip-shell system . . 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_cascip-shell shell shell-project 0.0.0 create_ca(). () {
35 echoscip-shell system . . echo. "Creating CA key ${CAKEY}"
36 opensslscip-shell system . . openssl. genrsa ${CA_ENCRYPTION_OPT} -out ${CAKEY} 4096 > /dev/nullscip-shell filesystem . . `/dev/null`.
37 if [scip-shell system . . `[`. $? -ne 0 ];
38 then
39 echoscip-shell system . . echo. "Failed"
40 exit 1
41 fi
42 echoscip-shell system . . echo. "Creating CA certificate ${CACERT}"
43 opensslscip-shell system . . openssl. req -new -config ${CACFG} -x509 -days ${VALIDITY_DAYS} -key ${CAKEY} -out ${CACERT} > /dev/nullscip-shell filesystem . . `/dev/null`.
44 if [scip-shell system . . `[`. $? -ne 0 ];
45 then
46 echoscip-shell system . . echo. "Failed"
47 exit 1
48 fi
49 }
50
51 create_certscip-shell shell shell-project 0.0.0 create_cert(). () {
52 local base=${OUTPUT_DIR}/${OUTPUT_BASE}
53 echoscip-shell system . . echo. "Creating certificate ${base}.key"
54 opensslscip-shell system . . openssl. genrsa -out ${base}.key ${KEYBITS:-2048} > /dev/nullscip-shell filesystem . . `/dev/null`.
55 if [scip-shell system . . `[`. $? -ne 0 ];
56 then
57 echoscip-shell system . . echo. "Failed"
58 exit 1
59 fi
60 echoscip-shell system . . echo. "Creating signing request ${base}.csr"
61 opensslscip-shell system . . openssl. req -batch -new -config ${CONFIG_FILE} -key ${base}.key -out ${base}.csr > /dev/nullscip-shell filesystem . . `/dev/null`.
62 if [scip-shell system . . `[`. $? -ne 0 ];
63 then
64 echoscip-shell system . . echo. "Failed"
65 exit 1
66 fi
67 echoscip-shell system . . echo. "Creating certificate ${base}.crt"
68 opensslscip-shell system . . openssl. x509 -req -days ${VALIDITY_DAYS} -in ${base}.csr -CA ${CACERT} -CAkey ${CAKEY} -set_serial 01 -out ${base}.crt > /dev/nullscip-shell filesystem . . `/dev/null`.
69 if [scip-shell system . . `[`. $? -ne 0 ];
70 then
71 echoscip-shell system . . echo. "Failed"
72 exit 1
73 fi
74 echoscip-shell system . . echo. "Combining key and crt into ${base}.pem"
75 catscip-shell system . . cat. ${base}.key > ${base}.pem
76 catscip-shell system . . cat. ${base}.crt >> ${base}.pem
77 }
78
79 usagescip-shell shell shell-project 0.0.0 usage(). () {
80 catscip-shell system . . 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/nullscip-shell filesystem . . `/dev/null`. 2>&1
127 then
128 echoscip-shell system . . 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 usagescip-shell shell shell-project 0.0.0 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 usagescip-shell shell shell-project 0.0.0 usage().
178 exit
179 ;;
180 esac
181 done
182
183 if [scip-shell system . . `[`. -z "${OUTPUT_DIR}" ]
184 then
185 OUTPUT_DIR=.
186 else
187 mkdirscip-shell system . . 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 echoscip-shell system . . echo.
201 echoscip-shell system . . echo. "Unknown mode. Exiting."
202 exit 1
203 ;;
204 esac
205
206 if [scip-shell system . . `[`. -z "${CONFIG_FILE}" ]
207 then
208 CONFIG_FILE="${OUTPUT_DIR}/tmp.cfg"
209 echoscip-shell system . . echo.
210 echoscip-shell system . . echo. "No config file specified, creating '${CONFIG_FILE}'"
211 echoscip-shell system . . echo. "You can use this config file to create additional certs without"
212 echoscip-shell system . . echo. "re-entering the information for the fields in the certificate"
213 create_configscip-shell shell shell-project 0.0.0 create_config().
214 fi
215
216 if [scip-shell system . . `[`. -z ${CACERT} ]
217 then
218 CAKEY=${OUTPUT_DIR}/ca.key
219 CACERT=${OUTPUT_DIR}/ca.crt
220 CACFG=${OUTPUT_DIR}/ca.cfg
221 if [scip-shell system . . `[`. ! -r "$CAKEY" ] && [scip-shell system . . `[`. ! -r "$CACFG" ]; then
222 create_configscip-shell shell shell-project 0.0.0 create_config(). ca "${CACFG}" "${DEFAULT_CA_CN}" "${DEFAULT_CA_ORG}"
223 fi
224 if [scip-shell system . . `[`. ! -r "$CACERT" ]; then
225 create_cascip-shell shell shell-project 0.0.0 create_ca().
226 fi
227 else
228 if [scip-shell system . . `[`. -z ${CAKEY} ]
229 then
230 echoscip-shell system . . echo. "-k must be specified if -c is"
231 exit 1
232 fi
233 fi
234
235 create_certscip-shell shell shell-project 0.0.0 create_cert().