| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 1 | # lib/tls | 
|  | 2 | # Functions to control the configuration and operation of the TLS proxy service | 
|  | 3 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 4 | # !! source _before_ any services that use ``SERVICE_HOST`` | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 5 | # | 
|  | 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 Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 12 |  | 
|  | 13 | # Entry points: | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 14 | # | 
|  | 15 | # - configure_CA | 
|  | 16 | # - init_CA | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 17 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 18 | # - configure_proxy | 
|  | 19 | # - start_tls_proxy | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 20 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 21 | # - make_root_ca | 
|  | 22 | # - make_int_ca | 
|  | 23 | # - new_cert $INT_CA_DIR int-server "abc" | 
|  | 24 | # - start_tls_proxy HOST_IP 5000 localhost 5000 | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 25 | # - ensure_certificates | 
|  | 26 | # - is_ssl_enabled_service | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 27 |  | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 28 | # Defaults | 
|  | 29 | # -------- | 
|  | 30 |  | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 31 | if 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 | 
|  | 37 | fi | 
|  | 38 |  | 
|  | 39 | # Make up a hostname for cert purposes | 
|  | 40 | # will be added to /etc/hosts? | 
|  | 41 | DEVSTACK_HOSTNAME=secure.devstack.org | 
|  | 42 | DEVSTACK_CERT_NAME=devstack-cert | 
|  | 43 | DEVSTACK_CERT=$DATA_DIR/$DEVSTACK_CERT_NAME.pem | 
|  | 44 |  | 
|  | 45 | # CA configuration | 
|  | 46 | ROOT_CA_DIR=${ROOT_CA_DIR:-$DATA_DIR/CA/root-ca} | 
|  | 47 | INT_CA_DIR=${INT_CA_DIR:-$DATA_DIR/CA/int-ca} | 
|  | 48 |  | 
|  | 49 | ORG_NAME="OpenStack" | 
|  | 50 | ORG_UNIT_NAME="DevStack" | 
|  | 51 |  | 
|  | 52 | # Stud configuration | 
|  | 53 | STUD_PROTO="--tls" | 
|  | 54 | STUD_CIPHERS='TLSv1+HIGH:!DES:!aNULL:!eNULL:@STRENGTH' | 
|  | 55 |  | 
|  | 56 |  | 
|  | 57 | # CA Functions | 
|  | 58 | # ============ | 
|  | 59 |  | 
|  | 60 | # There may be more than one, get specific | 
|  | 61 | OPENSSL=${OPENSSL:-/usr/bin/openssl} | 
|  | 62 |  | 
|  | 63 | # Do primary CA configuration | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 64 | function configure_CA { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 65 | # 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 76 | function create_CA_base { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 77 | 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 95 | function create_CA_config { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 96 | local ca_dir=$1 | 
|  | 97 | local common_name=$2 | 
|  | 98 |  | 
|  | 99 | echo " | 
|  | 100 | [ ca ] | 
|  | 101 | default_ca = CA_default | 
|  | 102 |  | 
|  | 103 | [ CA_default ] | 
|  | 104 | dir                     = $ca_dir | 
|  | 105 | policy                  = policy_match | 
|  | 106 | database                = \$dir/index.txt | 
|  | 107 | serial                  = \$dir/serial | 
|  | 108 | certs                   = \$dir/certs | 
|  | 109 | crl_dir                 = \$dir/crl | 
|  | 110 | new_certs_dir           = \$dir/newcerts | 
|  | 111 | certificate             = \$dir/cacert.pem | 
|  | 112 | private_key             = \$dir/private/cacert.key | 
|  | 113 | RANDFILE                = \$dir/private/.rand | 
|  | 114 | default_md              = default | 
|  | 115 |  | 
|  | 116 | [ req ] | 
|  | 117 | default_bits            = 1024 | 
|  | 118 | default_md              = sha1 | 
|  | 119 |  | 
|  | 120 | prompt                  = no | 
|  | 121 | distinguished_name      = ca_distinguished_name | 
|  | 122 |  | 
|  | 123 | x509_extensions         = ca_extensions | 
|  | 124 |  | 
|  | 125 | [ ca_distinguished_name ] | 
|  | 126 | organizationName        = $ORG_NAME | 
|  | 127 | organizationalUnitName  = $ORG_UNIT_NAME Certificate Authority | 
|  | 128 | commonName              = $common_name | 
|  | 129 |  | 
|  | 130 | [ policy_match ] | 
|  | 131 | countryName             = optional | 
|  | 132 | stateOrProvinceName     = optional | 
|  | 133 | organizationName        = match | 
|  | 134 | organizationalUnitName  = optional | 
|  | 135 | commonName              = supplied | 
|  | 136 |  | 
|  | 137 | [ ca_extensions ] | 
|  | 138 | basicConstraints        = critical,CA:true | 
|  | 139 | subjectKeyIdentifier    = hash | 
|  | 140 | authorityKeyIdentifier  = keyid:always, issuer | 
|  | 141 | keyUsage                = cRLSign, keyCertSign | 
|  | 142 |  | 
|  | 143 | " >$ca_dir/ca.conf | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | # Create a new signing configuration file | 
|  | 147 | # create_signing_config ca-dir | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 148 | function create_signing_config { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 149 | local ca_dir=$1 | 
|  | 150 |  | 
|  | 151 | echo " | 
|  | 152 | [ ca ] | 
|  | 153 | default_ca = CA_default | 
|  | 154 |  | 
|  | 155 | [ CA_default ] | 
|  | 156 | dir                     = $ca_dir | 
|  | 157 | policy                  = policy_match | 
|  | 158 | database                = \$dir/index.txt | 
|  | 159 | serial                  = \$dir/serial | 
|  | 160 | certs                   = \$dir/certs | 
|  | 161 | crl_dir                 = \$dir/crl | 
|  | 162 | new_certs_dir           = \$dir/newcerts | 
|  | 163 | certificate             = \$dir/cacert.pem | 
|  | 164 | private_key             = \$dir/private/cacert.key | 
|  | 165 | RANDFILE                = \$dir/private/.rand | 
|  | 166 | default_md              = default | 
|  | 167 |  | 
|  | 168 | [ req ] | 
|  | 169 | default_bits            = 1024 | 
|  | 170 | default_md              = sha1 | 
|  | 171 |  | 
|  | 172 | prompt                  = no | 
|  | 173 | distinguished_name      = req_distinguished_name | 
|  | 174 |  | 
|  | 175 | x509_extensions         = req_extensions | 
|  | 176 |  | 
|  | 177 | [ req_distinguished_name ] | 
|  | 178 | organizationName        = $ORG_NAME | 
|  | 179 | organizationalUnitName  = $ORG_UNIT_NAME Server Farm | 
|  | 180 |  | 
|  | 181 | [ policy_match ] | 
|  | 182 | countryName             = optional | 
|  | 183 | stateOrProvinceName     = optional | 
|  | 184 | organizationName        = match | 
|  | 185 | organizationalUnitName  = optional | 
|  | 186 | commonName              = supplied | 
|  | 187 |  | 
|  | 188 | [ req_extensions ] | 
|  | 189 | basicConstraints        = CA:false | 
|  | 190 | subjectKeyIdentifier    = hash | 
|  | 191 | authorityKeyIdentifier  = keyid:always, issuer | 
|  | 192 | keyUsage                = digitalSignature, keyEncipherment, keyAgreement | 
|  | 193 | extendedKeyUsage        = serverAuth, clientAuth | 
|  | 194 | subjectAltName          = \$ENV::SUBJECT_ALT_NAME | 
|  | 195 |  | 
|  | 196 | " >$ca_dir/signing.conf | 
|  | 197 | } | 
|  | 198 |  | 
| Dean Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 199 | # Create root and intermediate CAs | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 200 | # init_CA | 
|  | 201 | function 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 Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 208 | } | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 209 |  | 
| Dean Troyer | ca80217 | 2013-01-09 19:08:02 -0600 | [diff] [blame] | 210 | # Create an initial server cert | 
|  | 211 | # init_cert | 
|  | 212 | function init_cert { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 213 | 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 228 | function make_cert { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 229 | 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 264 | function make_int_CA { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 265 | 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 294 | function make_root_CA { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 295 | 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 Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 313 | # 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 322 | function is_ssl_enabled_service { | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 323 | 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 340 | function ensure_certificates { | 
| Jamie Lennox | bd24a8d | 2013-09-20 16:26:42 +1000 | [diff] [blame] | 341 | 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 |  | 
|  | 351 | if [[ !($cert && $key && $ca) ]]; then | 
|  | 352 | 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 Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 360 | # 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 Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 365 | function start_tls_proxy { | 
| Dean Troyer | c83a7e1 | 2012-11-29 11:47:58 -0600 | [diff] [blame] | 366 | 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 Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 373 |  | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 374 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 375 | # Tell emacs to use shell-script-mode | 
|  | 376 | ## Local variables: | 
|  | 377 | ## mode: shell-script | 
|  | 378 | ## End: |