有一陣子熱衷於尋找解法之際,曾看過 Everyday Work 的這篇解法,直接在 Certificate 中塞進多個 CN。最近則看到了國外提到用 mod_gnutls 這個 Apache Module 來實行,而且竟然又是個老早就有的專案。
mod_gnutls 是實做出 RFC3546 中的 Server Name Indication(SNI)解決方案,透過 TLS extension 達成 name-based 的 SSL 虛擬主機設定。mod_gnutls 的開發目的類似於 mod_ssl,但它並不依賴 OpenSSL,一方面是因為 OpenSSL 到目前 0.98 為止仍未支援 SNI,不過據說 0.99 會開始支援。
在下載並編譯後,將 libmod_gnutls.so 複製到 Apahce modules 目錄 (依平台而不同,如 /usr/lib/httpd/modules 或 /usr/local/libexec/apache2 等),並更名為 mod_gnutls.so,再將剛剛來源目錄中的 dhfile、rsafile 複製到 Apache 設定檔 httpd.conf 所在目錄。
接下來就是用你原本的方法產生每個網站的 Certificate,並參考官方說明文件編輯 Apache 設定檔:
"67.875149105368">### Main server configuration
"67.875149105368"># Load the module into Apache. LoadModule gnutls_module modules/mod_gnutls.so
"67.875149105368"># Using 4 memcache servers to distribute the SSL Session Cache. GnuTLSCache memcache "mc1.example.com mc2.example.com mc3.example.com mc4.example.com"
"67.875149105368"># With normal SSL Websites, you need one IP Address per-site. Listen 1.2.3.1:443
其中 GnuTLSCache 是設定 SSL Session Cache,為了效能考量,不建議關閉(None),如果沒有採用分散架構,可以用 dbm 取代上面的 memcache。先自行建立一個用以存放 cache 資料的目錄,並將 Apache 執行帳號設
為擁有者,回到剛剛編輯的 Apache 設定檔:
"67.875149105368">GnuTLSCache dbm "剛剛建立之絕對路徑"
"67.875149105368"># Set a timeout for the SSL Session Cache entries.
"67.875149105368"># Usually, this is set to 300 seconds:
GnuTLSCacheTimeout 300
最後就是虛擬主機設定部份:
### Virtual Hosts
"67.875149105368">NameVirtualHost 1.2.3.1:443
"67.875149105368"><VirtualHost 1.2.3.1:443>
GnuTLSEnable on
"67.875149105368">GnuTLSPriorities NORMAL
"67.875149105368">DocumentRoot /www/site1.example.com/html
"67.875149105368">ServerName site1.example.com:443
"67.875149105368">GnuTLSCertificateFile conf/ssl/site1.crt
"67.875149105368">GnuTLSKeyFile conf/ss/site1.key
</VirtualHost>
"67.875149105368"><VirtualHost 1.2.3.1:443>
GnuTLSEnable on
"67.875149105368">GnuTLSPriorities NORMAL
"67.875149105368">DocumentRoot /www/site2.example.com/html
"67.875149105368">ServerName site2.example.com:443
"67.875149105368">GnuTLSCertificateFile conf/ssl/site2.crt
"67.875149105368">GnuTLSKeyFile conf/ss/site2.key
</VirtualHost>
其實這資訊 lag 了一年多了吧 (現在都 2.2.17 了)。
前幾週被人問到 SSL 時丟相關資訊給他,
才發現 wikipedia 的 SNI 那頁更新過了:Server Name Indication
這功能是從 Apache 2.3 merge 回來的:#34607 - Support for Server Name Indication
設定範例可以參考 Apache 的 wiki:SSL with Virtual Hosts Using SNI
其實設定上沒什麼新東西要注意,
一切都恢復到過去設 SSL vhost 的方式即可。
最多就是記得要擺 NameVirtualHost *:443 和 SSLStrictSNIVHostCheck off 這兩行吧。
說起來好像外國人流行用 MoinMoinWiki?
像是 GCC Wiki 和 FreeBSD Wiki 都是用這套。
不清楚是因為有什麼便利性或功能性?
還是說單純就是因為偏好 Python 寫的東西?
找時間來玩玩看好了...
最近要忙著做 LLVM 的 trace 和 porting,
因為有簽萬惡的 NDA所以相關資訊不便公開。
總之因為 Apache 的 mod_ssl 已經原生支援 SNI 了,
所以這兩篇可以拿去丟了:
Multiple SSL NameVirtualHosts in Apache2
mod_gnutls 0.5.6 是地雷 (地雷?要炸就讓它繼續炸吧。I don't care.)
不過我沒有砍文章的習慣,所以就留個提示訊息在上面吧。
Apache2.2.xでVirtualHost + SSL まずはデフォルトでインストールされているOpenSSLで証明書を作成しておきます。 [ 証明書の作成 ]
mkdir ~/keys
cd ~/keys
openssl genrsa -des3 1024 > server.key
openssl rsa -in server.key -out server.key
openssl req -new -key server.key -out server.cert
openssl x509 -in server.cert -out server.cert -req -signkey server.key -days 3650
openssl-0.9.8i.tar.gzが必要になるので、そちらをインストールします。
[ OpenSSL インストール ]
cd /usr/local/src
wget http://www.openssl.org/source/openssl-0.9.8i.tar.gz
tar xzf openssl-0.9.8i.tar.gz
cd openssl-0.9.8i
./config --prefix=/usr/local/openssl enable-tlsext; make depend; make; make install; make clean;
パッチを充てて、Apacheをインストール。
[ Apache2.2 インストール ]
wget http://www.meisei-u.ac.jp/mirror/apache/httpd/httpd-2.2.10.tar.gz
wget https://sni.velox.ch/misc/httpd-2.2.x-sni.patch
tar xzf httpd-2.2.10.tar.gz
cd httpd-2.2.10
patch -p1 < ../httpd-2.2.x-sni.patch
./configure --prefix=/usr/local/apache --enable-so --enable-ssl --with-ssl=/usr/local/openssl
make; make install; make clean
cp ~/keys/* /usr/local/apache/conf/
/usr/local/apache/conf/httpd.conf に下記1行を追記。
[ /usr/local/apache/conf/httpd.conf ]
# My configration
Include conf/extra/httpd-my.conf
httpd-my.confで設定を行う
vi ./conf/extra/httpd-my.conf
-------------------------------
#
# When we also provide SSL we have to listen to the
# standard HTTP port (see above) and to the HTTPS port
#
# Note: Configurations that use IPv6 but not IPv4-mapped addresses need two
# Listen directives: "Listen [::]:443" and "Listen 0.0.0.0:443"
#
Listen 443
##
## SSL Global Context
##
## All SSL configuration in this context applies both to
## the main server and all SSL-enabled virtual hosts.
##
#
# Some MIME-types for downloading Certificates and CRLs
#
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
# Pass Phrase Dialog:
# Configure the pass phrase gathering process.
# The filtering dialog program (`builtin' is a internal
# terminal dialog) has to provide the pass phrase on stdout.
#SSLPassPhraseDialog builtin
# Inter-Process Session Cache:
# Configure the SSL Session Cache: First the mechanism
# to use and second the expiring timeout (in seconds).
#SSLSessionCache "dbm:/usr/local/apache/logs/ssl_scache"
#SSLSessionCache "shmcb:/usr/local/apache/logs/ssl_scache(512000)"
#SSLSessionCacheTimeout 300
# Semaphore:
# Configure the path to the mutual exclusion semaphore the
# SSL engine uses internally for inter-process synchronization.
#SSLMutex "file:/usr/local/apache/logs/ssl_mutex"
#
# Virtual Hosts
NameVirtualHost *:80
NameVirtualHost *:443 <VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/home/www/v1"
ServerName v1.hoge.com
<Directory "/home/www/v1">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/home/www/v2"
ServerName v2.hoge.com
<Directory "/home/www/v2">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
# Secure Shell ver.
<VirtualHost *:443>
ServerAdmin [email protected]
DocumentRoot "/home/www/v1"
ServerName v1.hoge.com:443
#SSL Engine Switch:
#Enable/Disable SSL for this virtual host.
SSLEngine on
# Server Private Key:
# If the key is not combined with the certificate, use this
# directive to point at the key file. Keep in mind that if
# you've both a RSA and a DSA private key you can configure
# both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateFile "/usr/local/apache/conf/server.cert"
SSLCertificateKeyFile "/usr/local/apache/conf/server.key"
<Directory "/home/www/v1">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerAdmin [email protected]
DocumentRoot "/home/www/v2"
ServerName v2.hoge.com:443
#SSL Engine Switch:
#Enable/Disable SSL for this virtual host.
SSLEngine on
# Server Private Key:
# If the key is not combined with the certificate, use this
# directive to point at the key file. Keep in mind that if
# you've both a RSA and a DSA private key you can configure
# both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateFile "/usr/local/apache/conf/server.cert"
SSLCertificateKeyFile "/usr/local/apache/conf/server.key"
<Directory "/home/www/v2">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
-------------------------------
VirtualHostとSSLを併用する場合は、<VirtualHost *:443><VirtualHost *:80>を必要台数分記述します。
■追記
Apache 2.2.11 の場合は https://sni.velox.ch/misc/ から対応しているパッチを落として充てて下さい。
■追記その2 -2009/10/12-
Apache 2.2.12以降はパッチ適用の必要がなくなりました。
./configure --prefix=/usr/local/apache --enable-so --enable-ssl --with-ssl=/usr/local/openssl;
make; make install; make clean
だけで大丈夫です。opensslはenable-tlsext を指定して下さい。