1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: ssl-cert.eclass
8 # Max Kalika <max@gentoo.org>
9 # @BLURB: Eclass for SSL certificates
11 # This eclass implements a standard installation procedure for installing
12 # self-signed SSL certificates.
14 # "install_cert /foo/bar" installs ${ROOT}/foo/bar.{key,csr,crt,pem}
16 # @ECLASS-VARIABLE: SSL_CERT_MANDATORY
18 # Set to non zero if ssl-cert is mandatory for ebuild.
19 : ${SSL_CERT_MANDATORY:=0}
21 # @ECLASS-VARIABLE: SSL_CERT_USE
23 # Use flag to append dependency to.
24 : ${SSL_CERT_USE:=ssl}
26 if [[ "${SSL_CERT_MANDATORY}" == "0" ]]; then
27 DEPEND="${SSL_CERT_USE}? ( dev-libs/openssl )"
28 IUSE="${SSL_CERT_USE}"
30 DEPEND="dev-libs/openssl"
36 # Initializes variables and generates the needed
37 # OpenSSL configuration file and a CA serial file
41 # Location of the config file
42 SSL_CONF="${T}/${$}ssl.cnf"
43 # Location of the CA serial file
44 SSL_SERIAL="${T}/${$}ca.ser"
45 # Location of some random files OpenSSL can use: don't use
46 # /dev/u?random here -- doesn't work properly on all platforms
47 SSL_RANDOM="${T}/environment:${T}/eclass-debug.log:/etc/resolv.conf"
49 # These can be overridden in the ebuild
50 SSL_DAYS="${SSL_DAYS:-730}"
51 SSL_BITS="${SSL_BITS:-1024}"
52 SSL_COUNTRY="${SSL_COUNTRY:-US}"
53 SSL_STATE="${SSL_STATE:-California}"
54 SSL_LOCALITY="${SSL_LOCALITY:-Santa Barbara}"
55 SSL_ORGANIZATION="${SSL_ORGANIZATION:-SSL Server}"
56 SSL_UNIT="${SSL_UNIT:-For Testing Purposes Only}"
57 SSL_COMMONNAME="${SSL_COMMONNAME:-localhost}"
58 SSL_EMAIL="${SSL_EMAIL:-root@localhost}"
60 # Create the CA serial file
61 echo "01" > "${SSL_SERIAL}"
63 # Create the config file
64 ebegin "Generating OpenSSL configuration${1:+ for CA}"
65 cat <<-EOF > "${SSL_CONF}"
68 default_bits = ${SSL_BITS}
69 distinguished_name = req_dn
74 O = ${SSL_ORGANIZATION}
76 CN = ${SSL_COMMONNAME}${1:+ CA}
77 emailAddress = ${SSL_EMAIL}
86 # @RETURN: <base path>
88 # Simple function to determine whether we're creating
89 # a CA (which should only be done once) or final part
96 echo "${T}/${$}server"
101 # @USAGE: <base path>
103 # Generates an RSA key
107 local base=$(get_base "$1")
108 ebegin "Generating ${SSL_BITS} bit RSA key${1:+ for CA}"
109 openssl genrsa -rand "${SSL_RANDOM}" \
110 -out "${base}.key" "${SSL_BITS}" &> /dev/null
117 # @USAGE: <base path>
119 # Generates a certificate signing request using
120 # the key made by gen_key()
124 local base=$(get_base "$1")
125 ebegin "Generating Certificate Signing Request${1:+ for CA}"
126 openssl req -config "${SSL_CONF}" -new \
127 -key "${base}.key" -out "${base}.csr" &>/dev/null
134 # @USAGE: <base path>
136 # Generates either a self-signed CA certificate using
137 # the csr and key made by gen_csr() and gen_key() or
138 # a signed server certificate using the CA cert previously
139 # created by gen_crt()
143 local base=$(get_base "$1")
145 ebegin "Generating self-signed X.509 Certificate for CA"
146 openssl x509 -extfile "${SSL_CONF}" \
147 -days ${SSL_DAYS} -req -signkey "${base}.key" \
148 -in "${base}.csr" -out "${base}.crt" &>/dev/null
150 local ca=$(get_base 1)
151 ebegin "Generating authority-signed X.509 Certificate"
152 openssl x509 -extfile "${SSL_CONF}" \
153 -days ${SSL_DAYS} -req -CAserial "${SSL_SERIAL}" \
154 -CAkey "${ca}.key" -CA "${ca}.crt" \
155 -in "${base}.csr" -out "${base}.crt" &>/dev/null
163 # @USAGE: <base path>
165 # Generates a PEM file by concatinating the key
166 # and cert file created by gen_key() and gen_cert()
170 local base=$(get_base "$1")
171 ebegin "Generating PEM Certificate"
172 (cat "${base}.key"; echo; cat "${base}.crt") > "${base}.pem"
178 # @FUNCTION: install_cert
179 # @USAGE: <certificates>
181 # Uses all the private functions above to generate and install the
182 # requested certificates.
183 # <certificates> are full pathnames relative to ROOT, without extension.
185 # Example: "install_cert /foo/bar" installs ${ROOT}/foo/bar.{key,csr,crt,pem}
189 if [ $# -lt 1 ] ; then
190 eerror "At least one argument needed"
194 case ${EBUILD_PHASE} in
195 unpack|prepare|configure|compile|test|install)
196 die "install_cert cannot be called in ${EBUILD_PHASE}"
200 # Generate a CA environment #164601
201 gen_cnf 1 || return 1
202 gen_key 1 || return 1
203 gen_csr 1 || return 1
204 gen_crt 1 || return 1
211 for cert in "$@" ; do
212 # Check the requested certificate
213 if [ -z "${cert##*/}" ] ; then
214 ewarn "Invalid certification requested, skipping"
218 # Check for previous existence of generated files
219 for type in key csr crt pem ; do
220 if [ -e "${ROOT}${cert}.${type}" ] ; then
221 ewarn "${ROOT}${cert}.${type}: exists, skipping"
226 # Generate the requested files
233 # Install the generated files and set sane permissions
234 local base=$(get_base)
235 install -d "${ROOT}${cert%/*}"
236 install -m0400 "${base}.key" "${ROOT}${cert}.key"
237 install -m0444 "${base}.csr" "${ROOT}${cert}.csr"
238 install -m0444 "${base}.crt" "${ROOT}${cert}.crt"
239 install -m0400 "${base}.pem" "${ROOT}${cert}.pem"
244 if [ ${count} = 0 ] ; then
245 eerror "No certificates were generated"
247 elif [ ${count} != ${#} ] ; then
248 ewarn "Some requested certificates were not generated"