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 # @BLURB: Eclass for SSL certificates
10 # This eclass implements a standard installation procedure for installing
11 # self-signed SSL certificates.
13 # "install_cert /foo/bar" installs ${ROOT}/foo/bar.{key,csr,crt,pem}
15 # Guard against unsupported EAPIs. We need EAPI >= 1 for slot dependencies.
18 die "${ECLASS}.eclass: EAPI=0 is not supported. Please upgrade to EAPI >= 1."
23 die "${ECLASS}.eclass: EAPI=${EAPI} is not supported yet."
27 # @ECLASS-VARIABLE: SSL_CERT_MANDATORY
29 # Set to non zero if ssl-cert is mandatory for ebuild.
30 : ${SSL_CERT_MANDATORY:=0}
32 # @ECLASS-VARIABLE: SSL_CERT_USE
34 # Use flag to append dependency to.
35 : ${SSL_CERT_USE:=ssl}
37 # @ECLASS-VARIABLE: SSL_DEPS_SKIP
39 # Set to non zero to skip adding to DEPEND and IUSE.
42 if [[ "${SSL_DEPS_SKIP}" == "0" ]]; then
43 if [[ "${SSL_CERT_MANDATORY}" == "0" ]]; then
44 DEPEND="${SSL_CERT_USE}? ( || ( dev-libs/openssl:0 dev-libs/libressl:0 ) )"
45 IUSE="${SSL_CERT_USE}"
47 DEPEND="|| ( dev-libs/openssl:0 dev-libs/libressl:0 )"
54 # Initializes variables and generates the needed
55 # OpenSSL configuration file and a CA serial file
59 # Location of the config file
60 SSL_CONF="${T}/${$}ssl.cnf"
61 # Location of the CA serial file
62 SSL_SERIAL="${T}/${$}ca.ser"
63 # Location of some random files OpenSSL can use: don't use
64 # /dev/u?random here -- doesn't work properly on all platforms
65 SSL_RANDOM="${T}/environment:${T}/eclass-debug.log:/etc/resolv.conf"
67 # These can be overridden in the ebuild
68 SSL_DAYS="${SSL_DAYS:-730}"
69 SSL_BITS="${SSL_BITS:-4096}"
70 SSL_MD="${SSL_MD:-sha256}"
71 SSL_COUNTRY="${SSL_COUNTRY:-US}"
72 SSL_STATE="${SSL_STATE:-California}"
73 SSL_LOCALITY="${SSL_LOCALITY:-Santa Barbara}"
74 SSL_ORGANIZATION="${SSL_ORGANIZATION:-SSL Server}"
75 SSL_UNIT="${SSL_UNIT:-For Testing Purposes Only}"
76 SSL_COMMONNAME="${SSL_COMMONNAME:-localhost}"
77 SSL_EMAIL="${SSL_EMAIL:-root@localhost}"
79 # Create the CA serial file
80 echo "01" > "${SSL_SERIAL}"
82 # Create the config file
83 ebegin "Generating OpenSSL configuration${1:+ for CA}"
84 cat <<-EOF > "${SSL_CONF}"
87 default_bits = ${SSL_BITS}
88 distinguished_name = req_dn
93 O = ${SSL_ORGANIZATION}
95 CN = ${SSL_COMMONNAME}${1:+ CA}
96 emailAddress = ${SSL_EMAIL}
103 # @FUNCTION: get_base
105 # @RETURN: <base path>
107 # Simple function to determine whether we're creating
108 # a CA (which should only be done once) or final part
115 echo "${T}/${$}server"
120 # @USAGE: <base path>
122 # Generates an RSA key
126 local base=$(get_base "$1")
127 ebegin "Generating ${SSL_BITS} bit RSA key${1:+ for CA}"
128 if openssl version | grep -i libressl > /dev/null; then
129 openssl genrsa -out "${base}.key" "${SSL_BITS}" &> /dev/null
131 openssl genrsa -rand "${SSL_RANDOM}" \
132 -out "${base}.key" "${SSL_BITS}" &> /dev/null
140 # @USAGE: <base path>
142 # Generates a certificate signing request using
143 # the key made by gen_key()
147 local base=$(get_base "$1")
148 ebegin "Generating Certificate Signing Request${1:+ for CA}"
149 openssl req -config "${SSL_CONF}" -new \
150 -key "${base}.key" -out "${base}.csr" &>/dev/null
157 # @USAGE: <base path>
159 # Generates either a self-signed CA certificate using
160 # the csr and key made by gen_csr() and gen_key() or
161 # a signed server certificate using the CA cert previously
162 # created by gen_crt()
166 local base=$(get_base "$1")
168 ebegin "Generating self-signed X.509 Certificate for CA"
169 openssl x509 -extfile "${SSL_CONF}" \
171 -days ${SSL_DAYS} -req -signkey "${base}.key" \
172 -in "${base}.csr" -out "${base}.crt" &>/dev/null
174 local ca=$(get_base 1)
175 ebegin "Generating authority-signed X.509 Certificate"
176 openssl x509 -extfile "${SSL_CONF}" \
177 -days ${SSL_DAYS} -req -CAserial "${SSL_SERIAL}" \
178 -CAkey "${ca}.key" -CA "${ca}.crt" -${SSL_MD} \
179 -in "${base}.csr" -out "${base}.crt" &>/dev/null
187 # @USAGE: <base path>
189 # Generates a PEM file by concatinating the key
190 # and cert file created by gen_key() and gen_cert()
194 local base=$(get_base "$1")
195 ebegin "Generating PEM Certificate"
196 (cat "${base}.key"; echo; cat "${base}.crt") > "${base}.pem"
202 # @FUNCTION: install_cert
203 # @USAGE: <certificates>
205 # Uses all the private functions above to generate and install the
206 # requested certificates.
207 # <certificates> are full pathnames relative to ROOT, without extension.
209 # Example: "install_cert /foo/bar" installs ${ROOT}/foo/bar.{key,csr,crt,pem}
213 if [ $# -lt 1 ] ; then
214 eerror "At least one argument needed"
218 case ${EBUILD_PHASE} in
219 unpack|prepare|configure|compile|test|install)
220 die "install_cert cannot be called in ${EBUILD_PHASE}"
224 # Generate a CA environment #164601
225 gen_cnf 1 || return 1
226 gen_key 1 || return 1
227 gen_csr 1 || return 1
228 gen_crt 1 || return 1
235 for cert in "$@" ; do
236 # Check the requested certificate
237 if [ -z "${cert##*/}" ] ; then
238 ewarn "Invalid certification requested, skipping"
242 # Check for previous existence of generated files
243 for type in key csr crt pem ; do
244 if [ -e "${ROOT}${cert}.${type}" ] ; then
245 ewarn "${ROOT}${cert}.${type}: exists, skipping"
250 # Generate the requested files
257 # Install the generated files and set sane permissions
258 local base=$(get_base)
259 install -d "${ROOT}${cert%/*}"
260 install -m0400 "${base}.key" "${ROOT}${cert}.key"
261 install -m0444 "${base}.csr" "${ROOT}${cert}.csr"
262 install -m0444 "${base}.crt" "${ROOT}${cert}.crt"
263 install -m0400 "${base}.pem" "${ROOT}${cert}.pem"
268 if [ ${count} = 0 ] ; then
269 eerror "No certificates were generated"
271 elif [ ${count} != ${#} ] ; then
272 ewarn "Some requested certificates were not generated"