1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: ssl-cert.eclass
7 # Max Kalika <max@gentoo.org>
8 # @SUPPORTED_EAPIS: 1 2 3 4 5 6
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 # Guard against unsupported EAPIs. We need EAPI >= 1 for slot dependencies.
19 die "${ECLASS}.eclass: EAPI=0 is not supported. Please upgrade to EAPI >= 1."
24 die "${ECLASS}.eclass: EAPI=${EAPI} is not supported yet."
28 # @ECLASS-VARIABLE: SSL_CERT_MANDATORY
30 # Set to non zero if ssl-cert is mandatory for ebuild.
31 : ${SSL_CERT_MANDATORY:=0}
33 # @ECLASS-VARIABLE: SSL_CERT_USE
35 # Use flag to append dependency to.
36 : ${SSL_CERT_USE:=ssl}
38 # @ECLASS-VARIABLE: SSL_DEPS_SKIP
40 # Set to non zero to skip adding to DEPEND and IUSE.
43 if [[ "${SSL_DEPS_SKIP}" == "0" ]]; then
44 if [[ "${SSL_CERT_MANDATORY}" == "0" ]]; then
45 DEPEND="${SSL_CERT_USE}? ( || ( dev-libs/openssl:0 dev-libs/libressl:0 ) )"
46 IUSE="${SSL_CERT_USE}"
48 DEPEND="|| ( dev-libs/openssl:0 dev-libs/libressl:0 )"
55 # Initializes variables and generates the needed
56 # OpenSSL configuration file and a CA serial file
60 # Location of the config file
61 SSL_CONF="${T}/${$}ssl.cnf"
62 # Location of the CA serial file
63 SSL_SERIAL="${T}/${$}ca.ser"
64 # Location of some random files OpenSSL can use: don't use
65 # /dev/u?random here -- doesn't work properly on all platforms
66 SSL_RANDOM="${T}/environment:${T}/eclass-debug.log:/etc/resolv.conf"
68 # These can be overridden in the ebuild
69 SSL_DAYS="${SSL_DAYS:-730}"
70 SSL_BITS="${SSL_BITS:-4096}"
71 SSL_MD="${SSL_MD:-sha256}"
72 SSL_COUNTRY="${SSL_COUNTRY:-US}"
73 SSL_STATE="${SSL_STATE:-California}"
74 SSL_LOCALITY="${SSL_LOCALITY:-Santa Barbara}"
75 SSL_ORGANIZATION="${SSL_ORGANIZATION:-SSL Server}"
76 SSL_UNIT="${SSL_UNIT:-For Testing Purposes Only}"
77 SSL_COMMONNAME="${SSL_COMMONNAME:-localhost}"
78 SSL_EMAIL="${SSL_EMAIL:-root@localhost}"
80 # Create the CA serial file
81 echo "01" > "${SSL_SERIAL}"
83 # Create the config file
84 ebegin "Generating OpenSSL configuration${1:+ for CA}"
85 cat <<-EOF > "${SSL_CONF}"
88 default_bits = ${SSL_BITS}
89 distinguished_name = req_dn
94 O = ${SSL_ORGANIZATION}
96 CN = ${SSL_COMMONNAME}${1:+ CA}
97 emailAddress = ${SSL_EMAIL}
104 # @FUNCTION: get_base
106 # @RETURN: <base path>
108 # Simple function to determine whether we're creating
109 # a CA (which should only be done once) or final part
116 echo "${T}/${$}server"
121 # @USAGE: <base path>
123 # Generates an RSA key
127 local base=$(get_base "$1")
128 ebegin "Generating ${SSL_BITS} bit RSA key${1:+ for CA}"
129 if openssl version | grep -i libressl > /dev/null; then
130 openssl genrsa -out "${base}.key" "${SSL_BITS}" &> /dev/null
132 openssl genrsa -rand "${SSL_RANDOM}" \
133 -out "${base}.key" "${SSL_BITS}" &> /dev/null
141 # @USAGE: <base path>
143 # Generates a certificate signing request using
144 # the key made by gen_key()
148 local base=$(get_base "$1")
149 ebegin "Generating Certificate Signing Request${1:+ for CA}"
150 openssl req -config "${SSL_CONF}" -new \
151 -key "${base}.key" -out "${base}.csr" &>/dev/null
158 # @USAGE: <base path>
160 # Generates either a self-signed CA certificate using
161 # the csr and key made by gen_csr() and gen_key() or
162 # a signed server certificate using the CA cert previously
163 # created by gen_crt()
167 local base=$(get_base "$1")
169 ebegin "Generating self-signed X.509 Certificate for CA"
170 openssl x509 -extfile "${SSL_CONF}" \
172 -days ${SSL_DAYS} -req -signkey "${base}.key" \
173 -in "${base}.csr" -out "${base}.crt" &>/dev/null
175 local ca=$(get_base 1)
176 ebegin "Generating authority-signed X.509 Certificate"
177 openssl x509 -extfile "${SSL_CONF}" \
178 -days ${SSL_DAYS} -req -CAserial "${SSL_SERIAL}" \
179 -CAkey "${ca}.key" -CA "${ca}.crt" -${SSL_MD} \
180 -in "${base}.csr" -out "${base}.crt" &>/dev/null
188 # @USAGE: <base path>
190 # Generates a PEM file by concatinating the key
191 # and cert file created by gen_key() and gen_cert()
195 local base=$(get_base "$1")
196 ebegin "Generating PEM Certificate"
197 (cat "${base}.key"; echo; cat "${base}.crt") > "${base}.pem"
203 # @FUNCTION: install_cert
204 # @USAGE: <certificates>
206 # Uses all the private functions above to generate and install the
207 # requested certificates.
208 # <certificates> are full pathnames relative to ROOT, without extension.
210 # Example: "install_cert /foo/bar" installs ${ROOT}/foo/bar.{key,csr,crt,pem}
214 if [ $# -lt 1 ] ; then
215 eerror "At least one argument needed"
219 case ${EBUILD_PHASE} in
220 unpack|prepare|configure|compile|test|install)
221 die "install_cert cannot be called in ${EBUILD_PHASE}"
225 # Generate a CA environment #164601
226 gen_cnf 1 || return 1
227 gen_key 1 || return 1
228 gen_csr 1 || return 1
229 gen_crt 1 || return 1
236 for cert in "$@" ; do
237 # Check the requested certificate
238 if [ -z "${cert##*/}" ] ; then
239 ewarn "Invalid certification requested, skipping"
243 # Check for previous existence of generated files
244 for type in key csr crt pem ; do
245 if [ -e "${ROOT}${cert}.${type}" ] ; then
246 ewarn "${ROOT}${cert}.${type}: exists, skipping"
251 # Generate the requested files
258 # Install the generated files and set sane permissions
259 local base=$(get_base)
260 install -d "${ROOT}${cert%/*}"
261 install -m0400 "${base}.key" "${ROOT}${cert}.key"
262 install -m0444 "${base}.csr" "${ROOT}${cert}.csr"
263 install -m0444 "${base}.crt" "${ROOT}${cert}.crt"
264 install -m0400 "${base}.pem" "${ROOT}${cert}.pem"
269 if [ ${count} = 0 ] ; then
270 eerror "No certificates were generated"
272 elif [ ${count} != ${#} ] ; then
273 ewarn "Some requested certificates were not generated"