前言
前段时间由于业务需要,在服务器上新增一个服务专门接收各个门店的业务结算数据,接口文档指明需要使用https协议。这本不是什么问题,因为之前服务器已经有配置过https。但等到服务部署之后才发现这些客户端死活连接不上。一直提示“Error connecting with SSL”的错误。服务器明明已经配置好了,而且其它服务使用https协议通信都正常,为什么偏偏这些客户端连接不上呢?
故障排解
windows软件使用https协议访问服务器时无法成功连接,通过wireshark发现客户端软件所发送的client hello请求所支持的cipher suites的版本比较老旧,而服务器返回handshake failure的提示。
客户端软件所发送的握手请求中cipher suites的支持如下:
Cipher Specs (29 specs)
Cipher Spec: TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA (0x000016)
Cipher Spec: TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA (0x000013)
Cipher Spec: TLS_RSA_WITH_3DES_EDE_CBC_SHA (0x00000a)
Cipher Spec: TLS_DHE_DSS_WITH_RC4_128_SHA (0x000066)
Cipher Spec: TLS_RSA_WITH_IDEA_CBC_SHA (0x000007)
Cipher Spec: TLS_RSA_WITH_RC4_128_SHA (0x000005)
Cipher Spec: TLS_RSA_WITH_RC4_128_MD5 (0x000004)
Cipher Spec: TLS_DHE_RSA_WITH_DES_CBC_SHA (0x000015)
Cipher Spec: TLS_DHE_DSS_WITH_DES_CBC_SHA (0x000012)
Cipher Spec: TLS_RSA_WITH_DES_CBC_SHA (0x000009)
Cipher Spec: SSL2_DES_192_EDE3_CBC_WITH_MD5 (0x0700c0)
Cipher Spec: SSL2_IDEA_128_CBC_WITH_MD5 (0x050080)
Cipher Spec: SSL2_RC2_128_CBC_WITH_MD5 (0x030080)
Cipher Spec: SSL2_RC4_128_WITH_MD5 (0x010080)
Cipher Spec: SSL2_RC4_64_WITH_MD5 (0x080080)
Cipher Spec: SSL2_DES_64_CBC_WITH_MD5 (0x060040)
Cipher Spec: TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA (0x000065)
Cipher Spec: TLS_RSA_EXPORT1024_WITH_RC4_56_SHA (0x000064)
Cipher Spec: TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA (0x000063)
Cipher Spec: TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA (0x000062)
Cipher Spec: TLS_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5 (0x000061)
Cipher Spec: TLS_RSA_EXPORT1024_WITH_RC4_56_MD5 (0x000060)
Cipher Spec: TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA (0x000014)
Cipher Spec: TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA (0x000011)
Cipher Spec: TLS_RSA_EXPORT_WITH_DES40_CBC_SHA (0x000008)
Cipher Spec: TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 (0x000006)
Cipher Spec: TLS_RSA_EXPORT_WITH_RC4_40_MD5 (0x000003)
Cipher Spec: SSL2_RC2_128_CBC_EXPORT40_WITH_MD5 (0x040080)
Cipher Spec: SSL2_RC4_128_EXPORT40_WITH_MD5 (0x020080)
google资料发现这些加密套件都是很老旧的,有人(https://www.v2ex.com/amp/t/355178)指出,“XP 版本的 IE 8 只支持 SSLv3 和 TLS 1.0 的 Cipher Suite ,另外 nginx 自 1.9.1 开始默认不会开启 SSLv3”。因此,为了让服务器支持像IE8/XP这类客户端,就不得不重新编译Nginx,在其中加入支持3DES的参数。
编译安装Nginx
依赖工具/库
[email protected]:~$ sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g-dev unzip git
下载Nginx
[email protected]:~$ wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
[email protected]:~$ tar zxf nginx-1.12.2.tar.gz
查看openSSL版本
本机的openSSL是否支持3DES算法并不影响Nginx是否支持3DES,Nginx是否支持只与它编译时所使用的openSSL版本有关,从上面的讨论中可以知道当前版本的Nginx默认是不支持3DES算法的。而openSSL 1.1.0 以上版本默认不支持也3DES加密。
[email protected]:~$ openssl version
查看openssl是否支持 3DES加密
[email protected]:~$ openssl ciphers -v "3DES"
下载openssl源
此处的openSSL源码只是为了用来编译Nginx,跟已经安装在本机上的openSSL没有任何关系。
[email protected]:~$ wget -O openssl.zip -c https://github.com/openssl/openssl/archive/OpenSSL_1_0_1f.zip
[email protected]:~$ unzip openssl.zip
[email protected]:~$ mv openssl-OpenSSL_1_0_1f/ openssl
编译安装Nginx
其中--with-openssl指向openssl源文件 --with-http_ssl_module指明支持ssl 参数--with-openssl-opt指明openssl的选项,其中若要兼容IE8则要添加‘enable-weak-ssl-ciphers‘。其它的参数可按需配置。
(--with-cc-opt=‘-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC‘ --with-ld-opt=‘-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie‘ 这两个参数添加之后有时候会报错)
[email protected]:~$ cd nginx-1.12.2
[email protected]:~/nginx-1.12.2$ ./configure --with-openssl=../openssl --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-openssl-opt=‘enable-weak-ssl-ciphers‘ --with-http_ssl_module
[email protected]:~/nginx-1.12.2$ make
[email protected]:~/nginx-1.12.2$ sudo make install
配置Nginx
下面是Nginx关于SSL/TLS的相关配置。
server {
listen 443 ssl;
proxy_set_header X-Real-IP $remote_addr;
server_name a.example.com;
ssl_certificate /etc/nginx/cert/cert.pem;
ssl_certificate_key /etc/nginx/cert/cert.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20-draft:EECDH+CHACHA20:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
root /home/www/html;
index index.html index.htm;
location /api {
proxy_pass http://localhost:8000;
}
}
参考
https://imququ.com/post/troubleshooting-https.html
https://www.centos.bz/2017/08/nginx-enable-tls-1-3/
https://dyn.im/2016/09/14/21/
https://www.feistyduck.com/library/openssl%2dcookbook/online/
https://www.ssllabs.com/ssltest/analyze.html