blob: 02906b70214a6e6432d61fdc0e66e649f2717c22 [file] [log] [blame]
Dean Troyerc83a7e12012-11-29 11:47:58 -06001# lib/tls
2# Functions to control the configuration and operation of the TLS proxy service
3
Dean Troyerc83a7e12012-11-29 11:47:58 -06004# !! source _before_ any services that use ``SERVICE_HOST``
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01005#
6# Dependencies:
7#
8# - ``functions`` file
9# - ``DEST``, ``DATA_DIR`` must be defined
10# - ``HOST_IP``, ``SERVICE_HOST``
11# - ``KEYSTONE_TOKEN_FORMAT`` must be defined
Dean Troyerc83a7e12012-11-29 11:47:58 -060012
13# Entry points:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010014#
15# - configure_CA
16# - init_CA
Dean Troyerc83a7e12012-11-29 11:47:58 -060017
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010018# - configure_proxy
19# - start_tls_proxy
Dean Troyerc83a7e12012-11-29 11:47:58 -060020
Stanislaw Pitucha2e0f0542014-06-27 16:05:53 +010021# - make_root_CA
22# - make_int_CA
23# - make_cert ca-dir cert-name "common-name" ["alt-name" ...]
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010024# - start_tls_proxy HOST_IP 5000 localhost 5000
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +100025# - ensure_certificates
26# - is_ssl_enabled_service
Dean Troyerc83a7e12012-11-29 11:47:58 -060027
Dean Troyercc6b4432013-04-08 15:38:03 -050028# Defaults
29# --------
30
Dean Troyerc83a7e12012-11-29 11:47:58 -060031if is_service_enabled tls-proxy; then
32 # TODO(dtroyer): revisit this below after the search for HOST_IP has been done
33 TLS_IP=${TLS_IP:-$SERVICE_IP}
34
35 # Set the default ``SERVICE_PROTOCOL`` for TLS
36 SERVICE_PROTOCOL=https
37fi
38
39# Make up a hostname for cert purposes
40# will be added to /etc/hosts?
41DEVSTACK_HOSTNAME=secure.devstack.org
42DEVSTACK_CERT_NAME=devstack-cert
43DEVSTACK_CERT=$DATA_DIR/$DEVSTACK_CERT_NAME.pem
44
45# CA configuration
46ROOT_CA_DIR=${ROOT_CA_DIR:-$DATA_DIR/CA/root-ca}
47INT_CA_DIR=${INT_CA_DIR:-$DATA_DIR/CA/int-ca}
48
49ORG_NAME="OpenStack"
50ORG_UNIT_NAME="DevStack"
51
52# Stud configuration
53STUD_PROTO="--tls"
54STUD_CIPHERS='TLSv1+HIGH:!DES:!aNULL:!eNULL:@STRENGTH'
55
56
57# CA Functions
58# ============
59
60# There may be more than one, get specific
61OPENSSL=${OPENSSL:-/usr/bin/openssl}
62
63# Do primary CA configuration
Ian Wienandaee18c72014-02-21 15:35:08 +110064function configure_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -060065 # build common config file
66
67 # Verify ``TLS_IP`` is good
68 if [[ -n "$HOST_IP" && "$HOST_IP" != "$TLS_IP" ]]; then
69 # auto-discover has changed the IP
70 TLS_IP=$HOST_IP
71 fi
72}
73
74# Creates a new CA directory structure
75# create_CA_base ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +110076function create_CA_base {
Dean Troyerc83a7e12012-11-29 11:47:58 -060077 local ca_dir=$1
78
79 if [[ -d $ca_dir ]]; then
80 # Bail out it exists
81 return 0
82 fi
83
84 for i in certs crl newcerts private; do
85 mkdir -p $ca_dir/$i
86 done
87 chmod 710 $ca_dir/private
88 echo "01" >$ca_dir/serial
89 cp /dev/null $ca_dir/index.txt
90}
91
92
93# Create a new CA configuration file
94# create_CA_config ca-dir common-name
Ian Wienandaee18c72014-02-21 15:35:08 +110095function create_CA_config {
Dean Troyerc83a7e12012-11-29 11:47:58 -060096 local ca_dir=$1
97 local common_name=$2
98
99 echo "
100[ ca ]
101default_ca = CA_default
102
103[ CA_default ]
104dir = $ca_dir
105policy = policy_match
106database = \$dir/index.txt
107serial = \$dir/serial
108certs = \$dir/certs
109crl_dir = \$dir/crl
110new_certs_dir = \$dir/newcerts
111certificate = \$dir/cacert.pem
112private_key = \$dir/private/cacert.key
113RANDFILE = \$dir/private/.rand
114default_md = default
115
116[ req ]
117default_bits = 1024
118default_md = sha1
119
120prompt = no
121distinguished_name = ca_distinguished_name
122
123x509_extensions = ca_extensions
124
125[ ca_distinguished_name ]
126organizationName = $ORG_NAME
127organizationalUnitName = $ORG_UNIT_NAME Certificate Authority
128commonName = $common_name
129
130[ policy_match ]
131countryName = optional
132stateOrProvinceName = optional
133organizationName = match
134organizationalUnitName = optional
135commonName = supplied
136
137[ ca_extensions ]
138basicConstraints = critical,CA:true
139subjectKeyIdentifier = hash
140authorityKeyIdentifier = keyid:always, issuer
141keyUsage = cRLSign, keyCertSign
142
143" >$ca_dir/ca.conf
144}
145
146# Create a new signing configuration file
147# create_signing_config ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100148function create_signing_config {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600149 local ca_dir=$1
150
151 echo "
152[ ca ]
153default_ca = CA_default
154
155[ CA_default ]
156dir = $ca_dir
157policy = policy_match
158database = \$dir/index.txt
159serial = \$dir/serial
160certs = \$dir/certs
161crl_dir = \$dir/crl
162new_certs_dir = \$dir/newcerts
163certificate = \$dir/cacert.pem
164private_key = \$dir/private/cacert.key
165RANDFILE = \$dir/private/.rand
166default_md = default
167
168[ req ]
169default_bits = 1024
170default_md = sha1
171
172prompt = no
173distinguished_name = req_distinguished_name
174
175x509_extensions = req_extensions
176
177[ req_distinguished_name ]
178organizationName = $ORG_NAME
179organizationalUnitName = $ORG_UNIT_NAME Server Farm
180
181[ policy_match ]
182countryName = optional
183stateOrProvinceName = optional
184organizationName = match
185organizationalUnitName = optional
186commonName = supplied
187
188[ req_extensions ]
189basicConstraints = CA:false
190subjectKeyIdentifier = hash
191authorityKeyIdentifier = keyid:always, issuer
192keyUsage = digitalSignature, keyEncipherment, keyAgreement
193extendedKeyUsage = serverAuth, clientAuth
194subjectAltName = \$ENV::SUBJECT_ALT_NAME
195
196" >$ca_dir/signing.conf
197}
198
Dean Troyerca802172013-01-09 19:08:02 -0600199# Create root and intermediate CAs
Dean Troyerc83a7e12012-11-29 11:47:58 -0600200# init_CA
201function init_CA {
202 # Ensure CAs are built
203 make_root_CA $ROOT_CA_DIR
204 make_int_CA $INT_CA_DIR $ROOT_CA_DIR
205
206 # Create the CA bundle
207 cat $ROOT_CA_DIR/cacert.pem $INT_CA_DIR/cacert.pem >>$INT_CA_DIR/ca-chain.pem
Dean Troyerca802172013-01-09 19:08:02 -0600208}
Dean Troyerc83a7e12012-11-29 11:47:58 -0600209
Dean Troyerca802172013-01-09 19:08:02 -0600210# Create an initial server cert
211# init_cert
212function init_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600213 if [[ ! -r $DEVSTACK_CERT ]]; then
214 if [[ -n "$TLS_IP" ]]; then
215 # Lie to let incomplete match routines work
216 TLS_IP="DNS:$TLS_IP"
217 fi
218 make_cert $INT_CA_DIR $DEVSTACK_CERT_NAME $DEVSTACK_HOSTNAME "$TLS_IP"
219
220 # Create a cert bundle
221 cat $INT_CA_DIR/private/$DEVSTACK_CERT_NAME.key $INT_CA_DIR/$DEVSTACK_CERT_NAME.crt $INT_CA_DIR/cacert.pem >$DEVSTACK_CERT
222 fi
223}
224
225
226# make_cert creates and signs a new certificate with the given commonName and CA
227# make_cert ca-dir cert-name "common-name" ["alt-name" ...]
Ian Wienandaee18c72014-02-21 15:35:08 +1100228function make_cert {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600229 local ca_dir=$1
230 local cert_name=$2
231 local common_name=$3
232 local alt_names=$4
233
234 # Generate a signing request
235 $OPENSSL req \
236 -sha1 \
237 -newkey rsa \
238 -nodes \
239 -keyout $ca_dir/private/$cert_name.key \
240 -out $ca_dir/$cert_name.csr \
241 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}"
242
243 if [[ -z "$alt_names" ]]; then
244 alt_names="DNS:${common_name}"
245 else
246 alt_names="DNS:${common_name},${alt_names}"
247 fi
248
249 # Sign the request valid for 1 year
250 SUBJECT_ALT_NAME="$alt_names" \
251 $OPENSSL ca -config $ca_dir/signing.conf \
252 -extensions req_extensions \
253 -days 365 \
254 -notext \
255 -in $ca_dir/$cert_name.csr \
256 -out $ca_dir/$cert_name.crt \
257 -subj "/O=${ORG_NAME}/OU=${ORG_UNIT_NAME} Servers/CN=${common_name}" \
258 -batch
259}
260
261
262# Make an intermediate CA to sign everything else
263# make_int_CA ca-dir signing-ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100264function make_int_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600265 local ca_dir=$1
266 local signing_ca_dir=$2
267
268 # Create the root CA
269 create_CA_base $ca_dir
270 create_CA_config $ca_dir 'Intermediate CA'
271 create_signing_config $ca_dir
272
273 # Create a signing certificate request
274 $OPENSSL req -config $ca_dir/ca.conf \
275 -sha1 \
276 -newkey rsa \
277 -nodes \
278 -keyout $ca_dir/private/cacert.key \
279 -out $ca_dir/cacert.csr \
280 -outform PEM
281
282 # Sign the intermediate request valid for 1 year
283 $OPENSSL ca -config $signing_ca_dir/ca.conf \
284 -extensions ca_extensions \
285 -days 365 \
286 -notext \
287 -in $ca_dir/cacert.csr \
288 -out $ca_dir/cacert.pem \
289 -batch
290}
291
292# Make a root CA to sign other CAs
293# make_root_CA ca-dir
Ian Wienandaee18c72014-02-21 15:35:08 +1100294function make_root_CA {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600295 local ca_dir=$1
296
297 # Create the root CA
298 create_CA_base $ca_dir
299 create_CA_config $ca_dir 'Root CA'
300
301 # Create a self-signed certificate valid for 5 years
302 $OPENSSL req -config $ca_dir/ca.conf \
303 -x509 \
304 -nodes \
305 -newkey rsa \
306 -days 21360 \
307 -keyout $ca_dir/private/cacert.key \
308 -out $ca_dir/cacert.pem \
309 -outform PEM
310}
311
312
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000313# Certificate Input Configuration
314# ===============================
315
316# check to see if the service(s) specified are to be SSL enabled.
317#
318# Multiple services specified as arguments are ``OR``'ed together; the test
319# is a short-circuit boolean, i.e it returns on the first match.
320#
321# Uses global ``SSL_ENABLED_SERVICES``
Ian Wienandaee18c72014-02-21 15:35:08 +1100322function is_ssl_enabled_service {
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000323 services=$@
324 for service in ${services}; do
325 [[ ,${SSL_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
326 done
327 return 1
328}
329
330
331# Ensure that the certificates for a service are in place. This function does
332# not check that a service is SSL enabled, this should already have been
333# completed.
334#
335# The function expects to find a certificate, key and CA certificate in the
336# variables {service}_SSL_CERT, {service}_SSL_KEY and {service}_SSL_CA. For
337# example for keystone this would be KEYSTONE_SSL_CERT, KEYSTONE_SSL_KEY and
338# KEYSTONE_SSL_CA. If it does not find these certificates the program will
339# quit.
Ian Wienandaee18c72014-02-21 15:35:08 +1100340function ensure_certificates {
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000341 local service=$1
342
343 local cert_var="${service}_SSL_CERT"
344 local key_var="${service}_SSL_KEY"
345 local ca_var="${service}_SSL_CA"
346
347 local cert=${!cert_var}
348 local key=${!key_var}
349 local ca=${!ca_var}
350
Solly Ross66115e52014-03-18 15:12:05 -0400351 if [[ -z "$cert" || -z "$key" || -z "$ca" ]]; then
Jamie Lennoxbd24a8d2013-09-20 16:26:42 +1000352 die $LINENO "Missing either the ${cert_var} ${key_var} or ${ca_var}" \
353 "variable to enable SSL for ${service}"
354 fi
355
356 cat $ca >> $SSL_BUNDLE_FILE
357}
358
359
Dean Troyerc83a7e12012-11-29 11:47:58 -0600360# Proxy Functions
361# ===============
362
363# Starts the TLS proxy for the given IP/ports
364# start_tls_proxy front-host front-port back-host back-port
Ian Wienandaee18c72014-02-21 15:35:08 +1100365function start_tls_proxy {
Dean Troyerc83a7e12012-11-29 11:47:58 -0600366 local f_host=$1
367 local f_port=$2
368 local b_host=$3
369 local b_port=$4
370
371 stud $STUD_PROTO -f $f_host,$f_port -b $b_host,$b_port $DEVSTACK_CERT 2>/dev/null
372}
Sean Dague584d90e2013-03-29 14:34:53 -0400373
Dean Troyercc6b4432013-04-08 15:38:03 -0500374
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100375# Tell emacs to use shell-script-mode
376## Local variables:
377## mode: shell-script
378## End: