3 # Licensed to the Apache Software Foundation (ASF) under one or more
4 # contributor license agreements. See the NOTICE file distributed with
5 # this work for additional information regarding copyright ownership.
6 # The ASF licenses this file to You under the Apache License, Version 2.0
7 # (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # This script will populate a directory 'sni' with 3 sites, httpd.conf
19 # and certificates as to facilitate testing of TLS server name
20 # indication support (RFC 4366) or SNI.
23 OPENSSL=${OPENSSL:-openssl}
24 DOMAIN=${DOMAIN:-my-sni-test.org}
27 # List of hostnames automatically created by default.
28 NAMES=${NAMES:-ape nut pear apple banana}
30 # IP address these hostnames are bound to.
33 # A certificate password for the .p12 files of the client
34 # authentication test. Normally not set. However some browsers
35 # require a password of at least 4 characters.
39 args=`getopt a:fd:D:p: $*`
41 echo "Syntax: $0 [-f] [-a IPaddress] [-d outdir] [-D domain ] [two or more vhost names ]"
42 echo " -f Force overwriting of outdir (default is $DIR)"
43 echo " -d dir Directory to create the SNI test server in (default is $DIR)"
44 echo " -D domain Domain name to use for this test (default is $DOMAIN)"
45 echo " -a IP IP address to use for this virtual host (default is $IP)"
46 echo " -p str Password for the client certificate test (some browsers require a set password)"
47 echo " [names] List of optional vhost names (default is $NAMES)"
50 echo " $0 -D SecureBlogsAreUs.com peter fred mary jane ardy"
52 echo "Which will create peter.SecureBlogsAreUs.com, fred.SecureBlogsAreUs.com and"
53 echo "so on. Note that the _first_ FQDN is also the default for non SNI hosts. It"
54 echo "may make sense to give this host a generic name - and allow each of the real"
55 echo "SNI site as sub directories/URI's of this generic name; thus allowing the "
56 echo "few non-SNI browsers access."
85 echo "Aborted - just specifying one vhost makes no sense for SNI testing. Go wild !"
93 if ! openssl version | grep -q OpenSSL; then
94 echo Aborted - your openssl is very old or misconfigured.
99 if test "0$2" \< "00.9"; then
100 echo Aborted - version of openssl too old, 0.9 or up required.
104 if test -d ${DIR} -a "x$FORCE" != "x1"; then
105 echo Aborted - already an ${DIR} directory. Use the -f flag to overwrite.
109 mkdir -p ${DIR} || exit 1
110 mkdir -p ${DIR}/ssl ${DIR}/htdocs ${DIR}/logs || exit 1
112 # Create a 'CA' - keep using different serial numbers
113 # as the browsers get upset if they see an identical
114 # serial with a different pub-key.
116 # Note that we're not relying on the 'v3_ca' section as
117 # in the default openssl.conf file - so the certificate
118 # will be without the basicConstraints = CA:true and
119 # keyUsage = cRLSign, keyCertSign values. This is fine
124 openssl req -new -nodes -batch \
126 -days 10 -subj '/CN=Da Root/O=SNI testing/' -set_serial $serial \
127 -keyout ${DIR}/root.key -out ${DIR}/root.pem \
130 CDIR=${DIR}/client-xs-control
132 # Create some certificate authorities for testing client controls
134 openssl req -new -nodes -batch \
136 -days 10 -subj '/CN=Da Second Root/O=SNI user access I/' -set_serial 2$serial$$\
137 -keyout ${CDIR}/xs-root-1.key -out ${CDIR}/xs-root-1.pem \
140 openssl req -new -nodes -batch \
142 -days 10 -subj '/CN=Da Second Root/O=SNI user access II/' -set_serial 3$serial$$ \
143 -keyout ${CDIR}/xs-root-2.key -out ${CDIR}/xs-root-2.pem \
146 # Create a chain of just the two access authorites:
147 cat ${CDIR}/xs-root-2.pem ${CDIR}/xs-root-1.pem > ${CDIR}/xs-root-chain.pem
149 # And likewise a directory with the same information (using the
150 # required 'hash' naming format
152 mkdir -p ${CDIR}/xs-root-dir || exit 1
154 ln ${CDIR}/xs-root-1.pem ${CDIR}/xs-root-dir/`openssl x509 -noout -hash -in ${CDIR}/xs-root-1.pem`.0
155 ln ${CDIR}/xs-root-2.pem ${CDIR}/xs-root-dir/`openssl x509 -noout -hash -in ${CDIR}/xs-root-2.pem`.0
157 # Use the above two client certificate authorities to make a few users
160 # Create a certificate request for a test user.
162 openssl req -new -nodes -batch \
163 -days 9 -subj "/CN=User $i/O=SNI Test Crash Dummy Dept/" \
164 -keyout ${CDIR}/client-$i.key -out ${CDIR}/client-$i.req -batch \
167 # And get it signed by either our client cert issuing root authority.
169 openssl x509 -text -req \
170 -CA ${CDIR}/xs-root-$i.pem -CAkey ${CDIR}/xs-root-$i.key \
171 -set_serial 3$serial$$ -in ${CDIR}/client-$i.req -out ${CDIR}/client-$i.pem \
174 # And create a pkcs#12 version for easy browser import.
176 openssl pkcs12 -export \
177 -inkey ${CDIR}/client-$i.key -in ${CDIR}/client-$i.pem -name "Client $i" \
178 -caname "Issuing client root $i" -certfile ${CDIR}/xs-root-$i.pem \
179 -out ${CDIR}/client.p12 -passout pass:"$PASSWD" || exit 5
181 rm ${CDIR}/client-$i.req
184 # Create the header for the example '/etc/hosts' file.
186 echo '# To append to your hosts file' > ${DIR}/hosts
188 # Create a header for the httpd.conf snipped.
190 cat > ${DIR}/httpd-sni.conf << EOM
191 # To append to your httpd.conf file'
193 NameVirtualHost ${IP}:443
195 LoadModule ssl_module modules/mod_ssl.so
197 SSLRandomSeed startup builtin
198 SSLRandomSeed connect builtin
201 TransferLog ${DIR}/logs/access_log
202 ErrorLog ${DIR}/logs/error_log
204 # You'll get a warning about this.
208 # Note that this SSL configuration is far
209 # from complete - you propably will want
210 # to configure SSLSession Caches at the
219 <Directory "${DIR}/htdocs">
224 # This first entry is also the default for non SNI
225 # supporting clients.
229 # Create the header of a sample BIND zone file.
232 echo "; Configuration sample to be added to the $DOMAIN zone file of BIND."
233 echo "\$ORIGIN $DOMAIN."
237 INFO="and also the site you see when the browser does not support SNI."
245 serial=`expr $serial + 1`
247 # Create a certificate request for this host.
249 openssl req -new -nodes -batch \
250 -days 9 -subj "/CN=$FQDN/O=SNI Testing/" \
251 -keyout ${DIR}/$n.key -out ${DIR}/$n.req -batch \
254 # And get it signed by our root authority.
256 openssl x509 -text -req \
257 -CA ${DIR}/root.pem -CAkey ${DIR}/root.key \
258 -set_serial $serial -in ${DIR}/$n.req -out ${DIR}/$n.pem \
261 # Combine the key and certificate in one file.
263 cat ${DIR}/$n.pem ${DIR}/$n.key > ${DIR}/ssl/$n.crt
264 rm ${DIR}/$n.req ${DIR}/$n.key ${DIR}/$n.pem
267 https://$FQDN/index.html"
269 # Create a /etc/host and bind-zone file example
271 echo "${IP} $FQDN $n" >> ${DIR}/hosts
272 echo "$n $ZADD" >> ${DIR}/zone-file
273 ZADD="IN CNAME $DEFAULT"
275 # Create and populate a docroot for this host.
277 mkdir -p ${DIR}/htdocs/$n || exit 1
278 echo We are $FQDN $INFO > ${DIR}/htdocs/$n/index.html || exit 1
280 # And change the info text - so that only the default/fallback site
281 # gets marked as such.
283 INFO="and you'd normally only see this site when there is proper SNI support."
285 # And create a configuration snipped.
287 cat >> ${DIR}/httpd-sni.conf << EOM
288 <VirtualHost ${IP}:443>
291 DocumentRoot ${DIR}/htdocs/$n
292 SSLCertificateChainFile ${DIR}/root.pem
293 SSLCertificateFile ${DIR}/ssl/$n.crt
295 # Uncomment the following lines if you
296 # want to only allow access to clients with
297 # a certificate issued/signed by some
298 # selection of the issuing authorites
300 # SSLCACertificate ${CDIR}/xs-root-1.pem # just root 1
301 # SSLCACertificate ${CDIR}/xs-root-2.pem # just root 2
302 # SSLCACertificate ${CDIR}/xs-root-chain.pem # 1 & 2
303 # SSLCACertificateDir ${CDIR}/xs-root-dir # 1 & 2 - but as a directory.
305 # SSLVerifyClient require
308 TransferLog ${DIR}/logs/access_$n
319 The directory ${DIR}/sni has been populated with the following
321 - root.key|pem Certificate authority root and key. (You could
322 import the root.pem key into your browser to
323 quell warnings about an unknown authority).
325 - hosts /etc/hosts file with fake entries for the hosts
327 - htdocs directory with one docroot for each domain,
328 each with a small sample file.
330 - ssl directory with an ssl cert (signed by root)
331 for each of the domains).
333 - logs logfiles, one for each domain and an
334 access_log for any misses.
336 The directory ${CDIR} contains optional test files to allow client
337 authentication testing:
339 - client*pem/p12 Files for client authentication testing. These
340 need to be imported into the browser.
342 - xs-root-1/2 Certificate authority which has issued above
343 client authentication certificates.
345 - xs-root-dir A directory specific for the SSLCACertificateDir
348 - xs-root-chain A chain of the two client xs authorities for the
349 SSLCACertificate directive.
354 A directory ${DIR}/sni has been created. Run an apache
355 server against it with
357 .../httpd -f ${DIR}/httpd-sni.conf
359 and keep an eye on ${DIR}/logs/error_log. When everything
360 is fine you will see entries like:
362 Feb 11 16:12:26 2008] [debug] Init:
363 SSL server IP/port overlap: ape.*:443 (httpd-sni.conf:24) vs. jane.*:443 (httpd-sni.conf:42)
365 for each vhost configured and a concluding warning:
367 [Mon Feb 11 16:12:26 2008] [warn] Init:
368 Name-based SSL virtual hosts only work for clients with TLS server name indication support (RFC 4366)
370 HOWEVER - If you see an entry like:
372 [Mon Feb 11 15:41:41 2008] [warn] Init:
373 You should not use name-based virtual hosts in conjunction with SSL!!
375 then you are either using an OpenSSL which is too old and/or you need to ensure that the
376 TLS Extensions are compiled into openssl with the 'enable-tlsext' flag. Once you have
377 recompiled or reinstalled OpenSSL with TLS Extensions you will have to recompile mod_ssl
378 to allow it to recognize SNI support.
380 Meanwhile add 'hosts' to your c:\windows\system32\drivers\etc\hosts
381 or /etc/hosts file as to point the various URL's to your server:
384 and verify that each returns its own name (and an entry in its
385 own ${DIR}/logs) file).
390 Note that in the generated example the 'first' domain is special - and is the
391 catch all for non-SNI browsers. Depending on your circumstances it may make
392 sense to use a generic name - and have each of the SNI domains as subdirectories
393 (and hence URI's under this generic name). Thus allowing non SNI browsers also
394 access to those sites.