| 1 | # Copyright 1999-2004 Gentoo Foundation |
1 | # Copyright 1999-2011 Gentoo Foundation |
| 2 | # Distributed under the terms of the GNU General Public License v2 |
2 | # Distributed under the terms of the GNU General Public License v2 |
| 3 | # $Header: /var/cvsroot/gentoo-x86/eclass/ssl-cert.eclass,v 1.1.1.1 2005/11/30 09:59:20 chriswhite Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/ssl-cert.eclass,v 1.20 2013/01/03 19:19:55 alonbl Exp $ |
| 4 | # |
4 | |
|
|
5 | # @ECLASS: ssl-cert.eclass |
|
|
6 | # @MAINTAINER: |
|
|
7 | # @AUTHOR: |
| 5 | # Author: Max Kalika <max@gentoo.org> |
8 | # Max Kalika <max@gentoo.org> |
| 6 | # |
9 | # @BLURB: Eclass for SSL certificates |
|
|
10 | # @DESCRIPTION: |
| 7 | # This eclass implements standard installation procedure for installing |
11 | # This eclass implements a standard installation procedure for installing |
| 8 | # self-signed SSL certificates. |
12 | # self-signed SSL certificates. |
|
|
13 | # @EXAMPLE: |
|
|
14 | # "install_cert /foo/bar" installs ${ROOT}/foo/bar.{key,csr,crt,pem} |
| 9 | |
15 | |
| 10 | # Conditionally depend on OpenSSL: allows inheretence |
16 | # @ECLASS-VARIABLE: SSL_CERT_MANDATORY |
| 11 | # without pulling extra packages if not needed |
17 | # @DESCRIPTION: |
|
|
18 | # Set to non zero if ssl-cert is mandatory for ebuild. |
|
|
19 | # |
|
|
20 | SSL_CERT_MANDATORY="${SSL_CERT_MANDATORY:-0}" |
|
|
21 | |
|
|
22 | # @ECLASS-VARIABLE: SSL_CERT_USE |
|
|
23 | # @DESCRIPTION: |
|
|
24 | # Use flag to append dependency to. |
|
|
25 | # |
|
|
26 | SSL_CERT_USE="${SSL_CERT_USE:-ssl}" |
|
|
27 | |
|
|
28 | if [[ "${SSL_CERT_MANDATORY}" = 0 ]]; then |
|
|
29 | DEPEND="${SSL_CERT_USE}? ( dev-libs/openssl )" |
|
|
30 | IUSE="${SSL_CERT_USE}" |
|
|
31 | else |
| 12 | DEPEND="ssl? ( dev-libs/openssl )" |
32 | DEPEND="dev-libs/openssl" |
| 13 | IUSE="ssl" |
33 | fi |
| 14 | |
34 | |
|
|
35 | # @FUNCTION: gen_cnf |
|
|
36 | # @USAGE: |
|
|
37 | # @DESCRIPTION: |
| 15 | # Initializes variables and generates the needed |
38 | # Initializes variables and generates the needed |
| 16 | # OpenSSL configuration file and a CA serial file |
39 | # OpenSSL configuration file and a CA serial file |
| 17 | # |
40 | # |
| 18 | # Access: private |
41 | # Access: private |
| 19 | gen_cnf() { |
42 | gen_cnf() { |
| … | |
… | |
| 24 | # Location of some random files OpenSSL can use: don't use |
47 | # Location of some random files OpenSSL can use: don't use |
| 25 | # /dev/u?random here -- doesn't work properly on all platforms |
48 | # /dev/u?random here -- doesn't work properly on all platforms |
| 26 | SSL_RANDOM="${T}/environment:${T}/eclass-debug.log:/etc/resolv.conf" |
49 | SSL_RANDOM="${T}/environment:${T}/eclass-debug.log:/etc/resolv.conf" |
| 27 | |
50 | |
| 28 | # These can be overridden in the ebuild |
51 | # These can be overridden in the ebuild |
| 29 | SSL_DAYS="${SSL_BITS:-730}" |
52 | SSL_DAYS="${SSL_DAYS:-730}" |
| 30 | SSL_BITS="${SSL_BITS:-1024}" |
53 | SSL_BITS="${SSL_BITS:-1024}" |
| 31 | SSL_COUNTRY="${SSL_COUNTRY:-US}" |
54 | SSL_COUNTRY="${SSL_COUNTRY:-US}" |
| 32 | SSL_STATE="${SSL_STATE:-California}" |
55 | SSL_STATE="${SSL_STATE:-California}" |
| 33 | SSL_LOCALITY="${SSL_LOCALITY:-Santa Barbara}" |
56 | SSL_LOCALITY="${SSL_LOCALITY:-Santa Barbara}" |
| 34 | SSL_ORGANIZATION="${SSL_ORGANIZATION:-SSL Server}" |
57 | SSL_ORGANIZATION="${SSL_ORGANIZATION:-SSL Server}" |
| … | |
… | |
| 38 | |
61 | |
| 39 | # Create the CA serial file |
62 | # Create the CA serial file |
| 40 | echo "01" > "${SSL_SERIAL}" |
63 | echo "01" > "${SSL_SERIAL}" |
| 41 | |
64 | |
| 42 | # Create the config file |
65 | # Create the config file |
| 43 | ebegin "Generating OpenSSL configuration" |
66 | ebegin "Generating OpenSSL configuration${1:+ for CA}" |
| 44 | cat <<-EOF > "${SSL_CONF}" |
67 | cat <<-EOF > "${SSL_CONF}" |
| 45 | [ req ] |
68 | [ req ] |
| 46 | prompt = no |
69 | prompt = no |
| 47 | default_bits = ${SSL_BITS} |
70 | default_bits = ${SSL_BITS} |
| 48 | distinguished_name = req_dn |
71 | distinguished_name = req_dn |
| … | |
… | |
| 50 | C = ${SSL_COUNTRY} |
73 | C = ${SSL_COUNTRY} |
| 51 | ST = ${SSL_STATE} |
74 | ST = ${SSL_STATE} |
| 52 | L = ${SSL_LOCALITY} |
75 | L = ${SSL_LOCALITY} |
| 53 | O = ${SSL_ORGANIZATION} |
76 | O = ${SSL_ORGANIZATION} |
| 54 | OU = ${SSL_UNIT} |
77 | OU = ${SSL_UNIT} |
| 55 | CN = ${SSL_COMMONNAME} |
78 | CN = ${SSL_COMMONNAME}${1:+ CA} |
| 56 | emailAddress = ${SSL_EMAIL} |
79 | emailAddress = ${SSL_EMAIL} |
| 57 | EOF |
80 | EOF |
| 58 | eend $? |
81 | eend $? |
| 59 | |
82 | |
| 60 | return $? |
83 | return $? |
| 61 | } |
84 | } |
| 62 | |
85 | |
|
|
86 | # @FUNCTION: get_base |
|
|
87 | # @USAGE: [if_ca] |
|
|
88 | # @RETURN: <base path> |
|
|
89 | # @DESCRIPTION: |
| 63 | # Simple function to determine whether we're creating |
90 | # Simple function to determine whether we're creating |
| 64 | # a CA (which should only be done once) or final part |
91 | # a CA (which should only be done once) or final part |
| 65 | # |
92 | # |
| 66 | # Access: private |
93 | # Access: private |
| 67 | get_base() { |
94 | get_base() { |
| … | |
… | |
| 70 | else |
97 | else |
| 71 | echo "${T}/${$}server" |
98 | echo "${T}/${$}server" |
| 72 | fi |
99 | fi |
| 73 | } |
100 | } |
| 74 | |
101 | |
|
|
102 | # @FUNCTION: gen_key |
|
|
103 | # @USAGE: <base path> |
|
|
104 | # @DESCRIPTION: |
| 75 | # Generates an RSA key |
105 | # Generates an RSA key |
| 76 | # |
106 | # |
| 77 | # Access: private |
107 | # Access: private |
| 78 | gen_key() { |
108 | gen_key() { |
| 79 | local base=`get_base $1` |
109 | local base=`get_base $1` |
| … | |
… | |
| 83 | eend $? |
113 | eend $? |
| 84 | |
114 | |
| 85 | return $? |
115 | return $? |
| 86 | } |
116 | } |
| 87 | |
117 | |
|
|
118 | # @FUNCTION: gen_csr |
|
|
119 | # @USAGE: <base path> |
|
|
120 | # @DESCRIPTION: |
| 88 | # Generates a certificate signing request using |
121 | # Generates a certificate signing request using |
| 89 | # the key made by gen_key() |
122 | # the key made by gen_key() |
| 90 | # |
123 | # |
| 91 | # Access: private |
124 | # Access: private |
| 92 | gen_csr() { |
125 | gen_csr() { |
| … | |
… | |
| 97 | eend $? |
130 | eend $? |
| 98 | |
131 | |
| 99 | return $? |
132 | return $? |
| 100 | } |
133 | } |
| 101 | |
134 | |
|
|
135 | # @FUNCTION: gen_crt |
|
|
136 | # @USAGE: <base path> |
|
|
137 | # @DESCRIPTION: |
| 102 | # Generates either a self-signed CA certificate using |
138 | # Generates either a self-signed CA certificate using |
| 103 | # the csr and key made by gen_csr() and gen_key() or |
139 | # the csr and key made by gen_csr() and gen_key() or |
| 104 | # a signed server certificate using the CA cert previously |
140 | # a signed server certificate using the CA cert previously |
| 105 | # created by gen_crt() |
141 | # created by gen_crt() |
| 106 | # |
142 | # |
| … | |
… | |
| 123 | eend $? |
159 | eend $? |
| 124 | |
160 | |
| 125 | return $? |
161 | return $? |
| 126 | } |
162 | } |
| 127 | |
163 | |
|
|
164 | # @FUNCTION: gen_pem |
|
|
165 | # @USAGE: <base path> |
|
|
166 | # @DESCRIPTION: |
| 128 | # Generates a PEM file by concatinating the key |
167 | # Generates a PEM file by concatinating the key |
| 129 | # and cert file created by gen_key() and gen_cert() |
168 | # and cert file created by gen_key() and gen_cert() |
| 130 | # |
169 | # |
| 131 | # Access: private |
170 | # Access: private |
| 132 | gen_pem() { |
171 | gen_pem() { |
| … | |
… | |
| 136 | eend $? |
175 | eend $? |
| 137 | |
176 | |
| 138 | return $? |
177 | return $? |
| 139 | } |
178 | } |
| 140 | |
179 | |
|
|
180 | # Removed due to bug 174759 |
|
|
181 | docert() { |
|
|
182 | eerror "Function \"docert\" has been removed for security reasons." |
|
|
183 | eerror "\"install_cert\" should be used instead. See bug 174759." |
|
|
184 | die |
|
|
185 | } |
|
|
186 | |
|
|
187 | # @FUNCTION: install_cert |
|
|
188 | # @USAGE: <certificates> |
|
|
189 | # @DESCRIPTION: |
| 141 | # Uses all the private functions above to generate |
190 | # Uses all the private functions above to generate and install the |
| 142 | # and install the requested certificates |
191 | # requested certificates. |
|
|
192 | # <certificates> are full pathnames relative to ROOT, without extension. |
|
|
193 | # |
|
|
194 | # Example: "install_cert /foo/bar" installs ${ROOT}/foo/bar.{key,csr,crt,pem} |
| 143 | # |
195 | # |
| 144 | # Access: public |
196 | # Access: public |
| 145 | docert() { |
197 | install_cert() { |
| 146 | if [ $# -lt 1 ] ; then |
198 | if [ $# -lt 1 ] ; then |
| 147 | eerror "At least one argument needed" |
199 | eerror "At least one argument needed" |
| 148 | return 1; |
200 | return 1; |
| 149 | fi |
201 | fi |
| 150 | |
202 | |
| 151 | # Initialize configuration |
203 | case ${EBUILD_PHASE} in |
|
|
204 | unpack|compile|test|install) |
|
|
205 | eerror "install_cert cannot be called in ${EBUILD_PHASE}" |
|
|
206 | return 1 ;; |
|
|
207 | esac |
|
|
208 | |
|
|
209 | # Generate a CA environment #164601 |
| 152 | gen_cnf || return 1 |
210 | gen_cnf 1 || return 1 |
| 153 | echo |
|
|
| 154 | |
|
|
| 155 | # Generate a CA environment |
|
|
| 156 | gen_key 1 || return 1 |
211 | gen_key 1 || return 1 |
| 157 | gen_csr 1 || return 1 |
212 | gen_csr 1 || return 1 |
| 158 | gen_crt 1 || return 1 |
213 | gen_crt 1 || return 1 |
| 159 | echo |
214 | echo |
| 160 | |
215 | |
|
|
216 | gen_cnf || return 1 |
|
|
217 | echo |
|
|
218 | |
| 161 | local count=0 |
219 | local count=0 |
| 162 | for cert in "$@" ; do |
220 | for cert in "$@" ; do |
| 163 | # Sanitize and check the requested certificate |
221 | # Check the requested certificate |
| 164 | cert="`/usr/bin/basename "${cert}"`" |
|
|
| 165 | if [ -z "${cert}" ] ; then |
222 | if [ -z "${cert##*/}" ] ; then |
| 166 | ewarn "Invalid certification requested, skipping" |
223 | ewarn "Invalid certification requested, skipping" |
| 167 | continue |
224 | continue |
| 168 | fi |
225 | fi |
| 169 | |
226 | |
| 170 | # Check for previous existence of generated files |
227 | # Check for previous existence of generated files |
| 171 | for type in key crt pem ; do |
228 | for type in key csr crt pem ; do |
| 172 | if [ -e "${D}${INSDESTTREE}/${cert}.${type}" ] ; then |
229 | if [ -e "${ROOT}${cert}.${type}" ] ; then |
| 173 | ewarn "${D}${INSDESTTREE}/${cert}.${type}: exists, skipping" |
230 | ewarn "${ROOT}${cert}.${type}: exists, skipping" |
| 174 | continue 2 |
231 | continue 2 |
| 175 | fi |
232 | fi |
| 176 | done |
233 | done |
| 177 | |
234 | |
| 178 | # Generate the requested files |
235 | # Generate the requested files |
| … | |
… | |
| 181 | gen_crt || continue |
238 | gen_crt || continue |
| 182 | gen_pem || continue |
239 | gen_pem || continue |
| 183 | echo |
240 | echo |
| 184 | |
241 | |
| 185 | # Install the generated files and set sane permissions |
242 | # Install the generated files and set sane permissions |
| 186 | local base=`get_base` |
243 | local base=$(get_base) |
|
|
244 | install -d "${ROOT}${cert%/*}" |
| 187 | newins "${base}.key" "${cert}.key" |
245 | install -m0400 "${base}.key" "${ROOT}${cert}.key" |
| 188 | fperms 0400 "${INSDESTTREE}/${cert}.key" |
|
|
| 189 | newins "${base}.csr" "${cert}.csr" |
246 | install -m0444 "${base}.csr" "${ROOT}${cert}.csr" |
| 190 | fperms 0444 "${INSDESTTREE}/${cert}.csr" |
|
|
| 191 | newins "${base}.crt" "${cert}.crt" |
247 | install -m0444 "${base}.crt" "${ROOT}${cert}.crt" |
| 192 | fperms 0444 "${INSDESTTREE}/${cert}.crt" |
|
|
| 193 | newins "${base}.pem" "${cert}.pem" |
248 | install -m0400 "${base}.pem" "${ROOT}${cert}.pem" |
| 194 | fperms 0400 "${INSDESTTREE}/${cert}.pem" |
|
|
| 195 | count=$((${count}+1)) |
249 | count=$((${count}+1)) |
| 196 | done |
250 | done |
| 197 | |
251 | |
| 198 | # Resulting status |
252 | # Resulting status |
| 199 | if [ ! ${count} ] ; then |
253 | if [ ${count} = 0 ] ; then |
| 200 | eerror "No certificates were generated" |
254 | eerror "No certificates were generated" |
| 201 | return 1 |
255 | return 1 |
| 202 | elif [ ${count} != ${#} ] ; then |
256 | elif [ ${count} != ${#} ] ; then |
| 203 | ewarn "Some requested certificates were not generated" |
257 | ewarn "Some requested certificates were not generated" |
| 204 | fi |
258 | fi |