前言:
在搭建支持HTTPS的前端代理服务器时候,通常会遇到让人头痛的证书问题。根据HTTPS的工作原理,浏览器在访问一个HTTPS站点时,先与服务器建立SSL连接,建立连接的第一步就是请求服务器的证书。而服务器在发送证书的时候,是不知道浏览器访问的是哪个域名的,所以不能根据不同域名发送不同的证书。用过GoAgent的人都知道需要给浏览器导入证书才能使用HTTPS正常登录Twitter等网站。
SNI(Server Name Indication)是为了解决一个服务器使用多个域名和证书的SSL/TLS扩展。一句话简述它的工作原理就是,在连接到服务器建立SSL链接之前先发送要访问站点的域名(Hostname),这样服务器根据这个域名返回一个合适的证书。目前,大多数操作系统和浏览器都已经很好地支持SNI扩展,OpenSSL 0.9.8已经内置这一功能,据说新版的nginx也支持SNI。
早期的SSLv2根据经典的公钥基础设施PKI(Public Key Infrastructure)设计,它默认认为:一台服务器(或者说一个IP)只会提供一个服务,所以在SSL握手时,服务器端可以确信客户端申请的是哪张证书。
但是让人万万没有想到的是,虚拟主机大力发展起来了,这就造成了一个IP会对应多个域名的情况。解决办法有一些,例如申请泛域名证书,对所有*.yourdomain.com的域名都可以认证,但如果你还有一个yourdomain.net的域名,那就不行了。
在HTTP协议中,请求的域名作为主机头(Host)放在HTTP Header中,所以服务器端知道应该把请求引向哪个域名,但是早期的SSL做不到这一点,因为在SSL握手的过程中,根本不会有Host的信息,所以服务器端通常返回的是配置中的第一个可用证书。因而一些较老的环境,可能会产生多域名分别配好了证书,但返回的始终是同一个。
既然问题的原因是在SSL握手时缺少主机头信息,那么补上就是了。
SNI(Server Name Indication)定义在RFC 4366,是一项用于改善SSL/TLS的技术,在SSLv3/TLSv1中被启用。它允许客户端在发起SSL握手请求时(具体说来,是客户端发出SSL请求中的ClientHello阶段),就提交请求的Host信息,使得服务器能够切换到正确的域并返回相应的证书。
[warning]要使用SNI,需要客户端和服务器端同时满足条件,幸好对于现代浏览器来说,大部分都支持SSLv3/TLSv1,所以都可以享受SNI带来的便利。[/warning]
二. nginx / apache 服务器端实现
nginx和apache服务端支持SNI参见:
《nginx 同一个IP上配置多个HTTPS主机》
《apache mod_gnutls实现多HTTPS虚拟主机》
三. 支持SNI的浏览器、服务器、库
Internet Explorer 7 or later, on Windows Vista or higher. Does not work on Windows XP, even Internet Explorer 8.
Mozilla Firefox 2.0 or later
Opera 8.0 or later (the TLS 1.1 protocol must be enabled)
Opera Mobile at least version 10.1 beta on Android[citation needed]
Google Chrome (Vista or higher. XP on Chrome 6 or newer. OS X 10.5.7 or higher on Chrome 5.0.342.1 or newer)
Safari 2.1 or later (Mac OS X 10.5.6 or higher and Windows Vista or higher)
Konqueror/KDE 4.7 or later
MobileSafari in Apple iOS 4.0 or later
Android default browser on Honeycomb or newer
Windows Phone 7[citation needed]
MicroB on Maemo
四. 支持SNI的服务器
Apache 2.2.12 or later using mod_ssl(or alternatively with experimental mod_gnutls)
Cherokee if compiled with TLS support
Versions of lighttpd 1.4.x and 1.5.x with patch, or 1.4.24+ without patch
Nginx with an accompanying OpenSSL built with SNI support
LiteSpeed 4.1 or later
Pound 2.6 or later
Apache Tomcat on Java 7 or later
Microsoft Internet Information Server IIS 8
五. 支持SNI的库
Mozilla NSS 3.11.1 client-side only
OpenSSL
0.9.8f (released 11 Oct 2007) – not compiled in by default, can be compiled in with config option ‘–enable-tlsext’
0.9.8j (released 07 Jan 2009) through 1.0.0 (released 29 March 2010) – compiled in by default
GNU TLS
libcurl / cURL since 7.18.1 (released 30 Mar 2008) when compiled against an SSL/TLS toolkit with SNI support
Python 3.2 (ssl, urllib and httplib modules)
Qt 4.8
Oracle Java 7 JSSE
-------------------------------F5-----------------------
K13452: Configuring a virtual server to serve multiple HTTPS sites using the TLS Server Name Indication feature
Non-Diagnostic
Original Publication Date:
Updated Date:
Topic
Purpose
You should consider using this procedure under the following condition:
You want to configure a single virtual server to serve multiple HTTPS sites using the Transport Layer Security (TLS) Server Name Indication (SNI) feature.
Prerequisites
You must meet the following prerequisite to use this procedure:
The certificate and key pairs for each of the HTTPS sites must be hosted on the virtual server.
Description
Prior to the introduction of TLS SNI as part of the TLS extensions, a single virtual server could not host multiple secure websites. This was the case because the destination server name can be decoded from the HTTP request header only after the Secure Sockets Layer (SSL) connection has been established.
With the introduction of TLS SNI, the client that supports TLS SNI can indicate the name of the server to which the client is attempting to connect, in the ClientHello packet, during the SSL handshake process. The server that supports TLS SNI can use this information to select the appropriate SSL certificate to return to the client in the ServerHello packet during the SSL handshake. As a result, the client can establish secure connections to the desired secure website from the list of multiple secure websites that are hosted on a single virtual server.
To support the TLS SNI feature, a virtual server must be assigned a default SSL profile for fallback and one SSL profile per HTTPS site. The fallback SSL profile is used when the server name does not match or when the client does not support the TLS SNI extensions. The following list is an example of the sequence of events that may occur when two clients, clientA (which supports the TLS SNI extension) and clientB (which does not support the TLS SNI extension), attempt to establish secure connections with the HTTPS site my.site1.com that is hosted on the TLS SNI virtual server:
clientA establishes a TCP connection to the TLS SNI virtual server.
clientA indicates the server name my.site1.com in its ClientHello packet and forwards the ClientHello packet to the TLS SNI virtual server.
The TLS SNI virtual server observes that the server name my.site1.com is indicated in the received ClientHello packet.
The TLS SNI virtual server checks its list of assigned SSL profiles and selects the SSL profile mysite1profile that has the server name my.site1.comconfigured.
The TLS SNI virtual server returns mysite1profile's SSL certificate in its ServerHello packet to clientA.
clientA establishes a secure connection to the TLS SNI virtual server after it successfully negotiates the remaining SSL options during the SSL handshake.
clientB establishes a TCP connection to the TLS SNI virtual server.
clientB does not support TLS SNI extension, hence there is no server name indicated in its ClientHello packet to the TLS SNI virtual server.
The TLS SNI virtual server observes no SNI extension in the received ClientHello packet and selects the fallback SSL profile mydefaultprofile.
The TLS SNI virtual server returns mydefaultprofile's SSL certificate (with CN my.default.com) in its ServerHello packet to clientB.
clientB warns of a possible certificate mismatch when it receives the SSL certificate (with CN my.default.com) from the ServerHello packet.
For security purposes, F5 recommends that you configure the following settings with the same values for all of the SSL/TLS SNI profiles associated with the same virtual server:
Ciphers
Client Authentication
Client Certificate
Frequency
Certificate Chain Traversal Depth
Advertised Certificate Authorities
Certificate Revocation List (CRL)
In BIG-IP 11.2.0 and later, the BIG-IP system will display an error message that appears similar to the following example if any of the settings are non-matching:
0107157c:3: Selected client SSL profiles do not match security policies for Virtual Server /Common/<virtual server>
Additionally, the BIG-IP system displays this error message if you attempt to reconfigure the previously mentioned settings in any of the SSL/TLS SNI profiles associated with the same virtual server. To avoid this error message in subsequent attempts to reconfigure the previously mentioned settings, F5 recommends that you configure a base SSL/TLS SNI profile and use this base profile as the parent profile for the SSL/TLS SNI profiles associated to the same virtual server.
Procedures
To configure multiple HTTPS sites using TLS SNI, you must perform the following procedures:
Importing the SSL certificate and key pairs for each server name
Before you begin configuring the BIG-IP objects for TLS SNI, you must import, to the BIG-IP system, all of the SSL certificate and key pairs that belong to the multiple HTTPS sites. To do so, perform the following procedure:
Impact of procedure: Performing the following procedure should not have a negative impact on your system.
Log in to the Configuration utility.
Click System.
Click File Management.
Click SSL Certificate List.
Click Import.
From the Import Type menu, select Key.
In the Key Name box, type a name for the certificate and key pair.
To locate the key file, click Browse.
To upload the key file to the BIG-IP system, click Import.
Click the name of the certificate and key pair from the SSL Certificate List.
Click Import.
To locate the certificate file, click Browse.
To upload the certificate file to the BIG-IP system, click Import.
To import each SSL certificate and key pair, repeat steps 5 through 13.
Configuring the base client SSL profile
To facilitate subsequent reconfiguration of the Cipher and/or Client Authentication settings described in the earlier part of this article, you should create a base client SSL profile to serve as the parent profile of the SSL/TLS SNI profiles associated with the same virtual server. To do so, perform the following procedure:
Impact of procedure: Performing the following procedure should not have a negative impact on your system.
If you have not done so, log in to the Configuration utility.
Click Local Traffic.
Click Profiles.
Click SSL.
Click Client.
Click Create.
In the Name box, type a name for the base client SSL profile.
Optional: Configure the remaining client SSL profile options, as desired.
Click Finished.
Configuring the fallback (default) client SSL profile
The system uses the fallback client SSL profile as the default SSL profile when there is no match to the server name, or when the client provides no SNI extension support. You can assign only one fallback SSL profile to each TLS SNI virtual server. To configure the fallback client SSL profile, perform the following procedure:
Note: For clients that do not support TLS SNI, if the requested server name does not match the certificate and key pair for the fallback profile, clients receive certificate warnings.
Impact of procedure: Performing the following procedure should not have a negative impact on your system.
If you have not done so, log in to the Configuration utility.
Click Local Traffic.
Click Profiles.
Click SSL.
Click Client.
Click Create.
In the Name box, type a name for the fallback client SSL profile.
For the Parent Profile setting, select the base client SSL profile you have created in the Configuring the base client SSL profile procedure.
From the Configuration menu, select Advanced.
Select the Certificate Key Chain check box, select the desired certificate and key for fallback, and click Add.
Note: For BIG-IP versions earlier than 11.5.0, select the individual Certificate and Key checkboxes, and then select the desired certificates and keys for fallback.
Check both the boxes for Default SSL Profile for SNI.
Optional: Configure the remaining client SSL profile options, as desired.
Click Finished.
Configuring the client SSL profiles for TLS SNI
To support TLS SNI, you must configure one client SSL profile per HTTPS site. To do so, perform the following procedure:
Note: Beginning in BIG-IP 11.6.0, if you leave the Server Name field blank, the BIG-IP system reads the Subject Alternative Name (SAN) from the certificate. You can skip steps 10 and 11 of this section if Hostname is used for SAN.
Impact of procedure: Performing the following procedure should not have a negative impact on your system.
Log in to the Configuration utility.
Click Local Traffic.
Click Profiles.
Click SSL.
Click Client.
Click Create.
In the Name box, type a name for a HTTPS site’s client SSL profile.
For the Parent Profile setting, select the base client SSL profile you have created in the Configuring the base client SSL profile procedure.
From the Configuration menu, select Advanced.
Select the Certificate Key Chain check box, select the desired certificate and key for the HTTPS site, and click Add.
Note: For BIG-IP versions earlier than 11.5.0, select the individual Certificate and Key check boxes, and then select the desired certificate and key for the HTTPS site.
Select the Server Name check box.
Type the name of the HTTPS site in the Server Name box.
Note: Beginning in BIG-IP 11.6.0, if you leave the Server Name box blank, the BIG-IP system reads the Subject Alternative Name (SAN) from the certificate. For versions prior to BIG-IP 11.6.0, if you leave the Server Name box blank, the BIG-IP system reads the Common Name (CN) from the certificate.
Optional: Configure the remaining client SSL profile options, as desired.
Click Finished.
Repeat steps 7 through 13 for each HTTPS site.
Configuring the virtual server for TLS SNI
To configure a virtual server for TLS SNI, you must assign the related client SSL profiles to the virtual server. To do so, perform the following procedure:
Impact of procedure: Performing the following procedure should not have a negative impact on your system.
Log in to the Configuration utility.
Click Local Traffic.
Click Virtual Servers.
Click Virtual Server List.
Click Create.
In the Name box, type the name of the virtual server.
In the Address box, type the IP address of the virtual server.
In the Service Port box, type the listening port number of the virtual server.
From the HTTP Profile menu, select the appropriate HTTP profile.
Select the backup client SSL profile created in the previous procedure from the Available box for SSL Profile (Client), and click the << button.
Select the HTTPS site’s client SSL profile created in the previous procedure from the Available box for SSL Profile (Client), and click the << button.
Repeat the previous step for each HTTPS site.
Optional: Configure the remaining virtual server options, as desired.
Click Finished.