ast_tls_cert 5.0 KB

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