Nginx + LibreSSL : 尝鲜测试

开发 前端
虽然不建议在这个阶段使用 LibreSSL 来代替 OpenSSL,但我只想测试其可行性。结果证明这是可行的。 从我的测试来看,没有任何功能上或性能的问题,而且只要你找到方法,构建 nginx + libressl 就容易了……

tl;dr: 用上 LibreSSL 已经第二个夜晚了 ... 依然运行良好

7月11日,LibreSSL团队发布 LibreSSL 的可移植版本,这是第一个可在 OpenBSD, Linux, OSX, Solaris 和 FreeBSD 上运行的版本。 [1,2,5,8]

本文会介绍一些 Nginx 与 Libressl 一起使用实践经验。

本文所用软件的版本

  • nginx 1.6.0

  • libressl 2.0.0

reddit-discussion [11]中有描述一些使用最新开发版本(1.7 分支)会出现的问题。

安装

直接从源码编译LibreSSL,构建过程的输出非常简洁,源码还附带测试用例及提供并行构建支持(见附录)。

  1. # 用于构建及安装 libressl 的选项 [7]  
  2. $ ./configure --prefix=/usr LDFLAGS=-lrt && make check && sudo make install 

新安装的 LibreSSL 可替代openssl以相同的方式运行,但要注意:正如 sabotage-linux 的 spencerjohn 和 Gentoo 的 Hanno Böck 所说的那样,用libressl完全替代操作系统中的openssl会很麻烦。[3,4]

LibreSSL 会报告其版本为 LibreSSL 2.0, openssl命令的使用方法与openssl一样:

  1. $ which openssl  
  2. /usr/bin/openssl  
  3.    
  4. $ openssl version  
  5. LibreSSL 2.0 
  6.    
  7. $ openssl s_client -host www.openssl.org -port 443 
  8. CONNECTED(00000003)  
  9. depth=2 C = BE, O = GlobalSign nv-sa, OU = Root CA, CN = GlobalSign Root CA  
  10. verify error:num=19:self signed certificate in certificate chain  
  11. verify return:0 
  12. ---  
  13. Certificate chain  
  14.  0 s:/C=GB/OU=Domain Control Validated/CN=*.openssl.org  
  15.    i:/C=BE/O=GlobalSign nv-sa/CN=GlobalSign Domain Validation CA - G2  
  16.  1 s:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA  
  17.    i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA  
  18.  2 s:/C=BE/O=GlobalSign nv-sa/CN=GlobalSign Domain Validation CA - G2  
  19.    i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA  
  20. ---  
  21. Server certificate  
  22. -----BEGIN CERTIFICATE-----  
  23. ... skip  
  24. -----END CERTIFICATE-----  
  25. subject=/C=GB/OU=Domain Control Validated/CN=*.openssl.org  
  26. issuer=/C=BE/O=GlobalSign nv-sa/CN=GlobalSign Domain Validation CA - G2  
  27. ---  
  28. No client certificate CA names sent  
  29. ---  
  30. SSL handshake has read 4136 bytes and written 707 bytes  
  31. ---  
  32. New, TLSv1/SSLv3, Cipher is DHE-RSA-AES256-GCM-SHA384  
  33. Server public key is 2048 bit  
  34. Secure Renegotiation IS supported  
  35. Compression: NONE  
  36. Expansion: NONE  
  37. SSL-Session:  
  38.     Protocol  : TLSv1.2 
  39.     Cipher    : DHE-RSA-AES256-GCM-SHA384  
  40.     TLS session ticket lifetime hint: 300 (seconds)  
  41.     TLS session ticket: 

确认了 libressl 能够使用后,我便动手让 nginx 来使用 libressl 。尽管在仍然使用 openssl 0.9.x 的旧系统中,通常我都会静态构建 nginx+openssl 以使最新和最好的 tls 版本可用。第一次尝试,只使用 ./configure --with-openssl=/path/to/libressl 就大错特错了,因为 nginx 已经完全与 openssl 的构建过程融合了: 

  • 可使用名为./config的脚本来替代./configure(容易解决)

  • openssl 会收集在 .openssl/lib 下的 objects(.obj) 文件和其他文件来链接进二进制文件和库文件,而 libressl 将这些文件分开存放在 crypto/.libs 和 ssl/.libs。

尝试通过手工建立目录层次(.openssl/lib)及根据 libressl 成功构建后出现的错误提示(见下面的错误信息)来复制文件以解决这些问题;在编译 libressl 时,我看到一个类似可以通过使用 LDFLAGS=-lrt 选项来解决问题的错误提示,但在尝试编译nginx并链接到已静态编译过的libressl库时仍然无法修复这个问题(但我依然继续):

  1.   ...  
  2.    
  3.   objs/addon/nginx-upstream-fair/ngx_http_upstream_fair_module.o \  
  4.   objs/addon/src/ngx_http_headers_more_filter_module.o \  
  5.   objs/addon/src/ngx_http_headers_more_headers_out.o \  
  6.   objs/addon/src/ngx_http_headers_more_headers_in.o \  
  7.   objs/addon/src/ngx_http_headers_more_util.o \  
  8.   objs/addon/src/ngx_http_encrypted_session_module.o \  
  9.   objs/addon/src/ngx_http_encrypted_session_cipher.o \  
  10.   objs/ngx_modules.o \  
  11.   -Wl,-E -lpthread -lcrypt -L/usr/lib -lm -llua5.1 -lpcre /data/builds/froggstack/nginx_modules/openssl/.openssl/lib/libssl.a /data/builds/froggstack/nginx_modules/openssl/.openssl/lib/libcrypto.a -ldl -lz  
  12. /data/builds/froggstack/nginx_modules/openssl/.openssl/lib/libcrypto.a(libcompat_la-getentropy_linux.o): In function `getentropy_fallback':  
  13. /data/builds/froggstack/nginx_modules/openssl/crypto/compat/getentropy_linux.c:324: undefined reference to `clock_gettime'  
  14. /data/builds/froggstack/nginx_modules/openssl/crypto/compat/getentropy_linux.c:395: undefined reference to `clock_gettime'  
  15. collect2: error: ld returned 1 exit status  
  16. make[1]: *** [objs/nginx] Error 1 
  17. make[1]: Leaving directory `/data/builds/froggstack/src_nginx/nginx-1.6.0'  
  18. make: *** [build] Error 2 

下一个尝试是在安装了 libressl 的前提下通过链接到 libressl 的动态库来构建 nginx,最终成功了(完整的nginx ./configure 选项参数见附录)。 运行 nginx-libressl -t 测试成功,并将 /usr/bin/nginx 替换成新的二进制可执行文件和运行 /etc/init.d/nginx restart,更新后的 nginx + libressl 上线了。任何配置文件和 nginx 的 ssl 配置都不需要修改,非常好!

测试

感兴趣并想测试的朋友可以访问: www.mare-system.de,网站从2014-07-12开始运行在 libressl 上。如你发现任何不兼容的问题,请通过 atsecurity@mare-system.de 给我留言。

在各种 Linux 和 Android 的浏览器上测试都没有发现问题;甚至在一台已被遗忘的装有2007年10月发布并已过时的附带 OpenSSL 0.9.8g 19 的 debian 5 上使用像 w3m 这样的控制台浏览器上浏览也没有问题。

在 ssllabs.com 上测试的得分为 A+,成绩与之前的配置一样;在使用了 libressl 后,唯一给出的提示是加密算法 ChaCha20-Poly1305 还处于实验阶段。

 

做了一个小小的性能测试,结果显示没有什么大问题;LibreSSL 与平均水平相比慢了 4%。原因可能是 openssl 是静态链接到 nginx 的,而 libressl 则是动态链接到 nginx 的,所以会产生更多的资源开销。

 

纯数字的测试结果:

  1. | Parallel Requests | OpenSSL-RPS | LibreSSL-RPS   
  2. | 10                | 2341.75     | 2260.5  
  3. | 20                | 2459.75     | 2418.25  
  4. | 30                | 2472        | 2397  
  5. | 40                | 2485        | 2384.5  
  6. | 50                | 2445        | 2382.25  
  7. | 60                | 2453.25     | 2390.75  
  8. | 70                | 2426.25     | 2347.25  
  9. | 80                | 2346.5      | 2227.5  
  10. | 90                | 2325.5      | 2211  
  11. | 100               | 2297.75     | 2318.25 

性能测试方式的一些说明可能在附录中找到。

#p#

结论

此法可行。

虽然不建议在这个阶段使用 LibreSSL 来代替 OpenSSL,但我只想测试其可行性。结果证明这是可行的。 从我的测试来看,没有任何功能上或性能的问题,而且只要你找到方法,构建 nginx + libressl 就容易了。依我所见,长期使用 LibreSSL 的好处是:

  • 干净的代码

  • 更少的漏洞

  • 更多人参与

在我撰写本文的时候,我收到新的 LibreSSL 版本发布的消息,新版本解决了一些新的问题。所以,再回头使用 OpenSSL 就显得有点不理智了:

做得好,LibreSSL 团队,再次感谢

参考

  1. 第一个可移植的 LibreSSL 版本发布

  2. LibreSSL - 下载

  3. libreSSL 的兼容性如何?

  4. 在 Gentoo 上使用 LibreSSL 

  5. LibreSSL 的 github 仓库

  6. Nginx + SSL + SPDY 指南

  7. 解决 LibreSSL 在 Redhat 上使用的问题

  8. LibreSSL - 主页

  9. SSL 报告: mare-system.de

  10. 第二个可移植的 LibreSSL 版本发布

  11. reddit-discussion

  12. Nginx + BoringSSL

附录
 

SSL 配置和使用的加密算法

  1. server {  
  2.    
  3.   listen 443 ssl spdy;  
  4.   server_name www.mare-system.de;  
  5.    
  6.   ...  
  7.    
  8.   # ssl  
  9.   ssl_session_cache shared:SSL:10m;  
  10.   ssl_session_timeout 10m;  
  11.    
  12.   # older protos for browsercompatibility  
  13.   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  
  14.   ssl_prefer_server_ciphers on;  
  15.    
  16.   # suggestion from sslabs / including PFS  
  17.   ssl_ciphers EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384:EECDH+aRSA+SHA256:EECDH+aRSA+RC4:EECDH:EDH+aRSA:RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS;  
  18.    
  19.   # excluding subDomain on purpose  
  20.   add_header Strict-Transport-Security "max-age=31536000";   
  21.    
  22.   ...  
  23.    
  24.   } 

Nginx 用于动态链接到 LibreSSL 的 Configure 选项

  1. nginx_libressl_opts = """    --conf-path=/etc/nginx/nginx.conf   
  2.   --sbin-path=/usr/sbin/nginx  
  3.   --prefix=  
  4.   --error-log-path=/var/log/nginx/error.log  
  5.   --http-log-path=/var/log/nginx/access.log  
  6.   --http-client-body-temp-path=/var/run/nginx/client_temp  
  7.   --http-proxy-temp-path=/var/run/nginx/proxy_temp  
  8.   --http-fastcgi-temp-path=/var/run/nginx/fastcgi_temp  
  9.   --with-file-aio  
  10.   --with-http_gzip_static_module  
  11.   --with-http_ssl_module  
  12.   --with-http_spdy_module  
  13.   --with-http_stub_status_module  
  14.   --with-debug  
  15.   --without-mail_pop3_module  
  16.   --without-mail_smtp_module  
  17.   --without-mail_imap_module  
  18.   --without-http_uwsgi_module  
  19.   --without-http_scgi_module  
  20.   --without-http_ssi_module      
  21.   --add-module=$nmd/nginx-openssl-version  
  22.   --add-module=$nmd/naxsi  
  23.   --add-module=$nmd/lua-nginx-module  
  24.   --add-module=$nmd/ngx_devel_kit  
  25.   --add-module=$nmd/echo-nginx-module  
  26.   --add-module=$nmd/nginx-accesskey  
  27.   --add-module=$nmd/ngx_http_log_request_speed  
  28.   --add-module=$nmd/set-misc-nginx-module  
  29.   --add-module=$nmd/nginx-sticky-module-ng  
  30.   --add-module=$nmd/ngx_cache_purge  
  31.   --add-module=$nmd/memc-nginx-module  
  32.   --add-module=$nmd/nginx-upstream-fair  
  33.   --add-module=$nmd/headers-more-nginx-module  
  34.   --add-module=$nmd/encrypted-session-nginx-module 

LibreSSL - 构建时间和输出

  1. # libressl  
  2. $ make check   
  3.    
  4. real  1m58.610s  
  5. user  1m24.517s  
  6. sys 0m11.477s  
  7.    
  8.    
  9. $ make check -j4   
  10.    
  11. real  0m34.231s  
  12. user  1m28.626s  
  13. sys 0m11.357s  
  14.    
  15.    
  16. # make check output   
  17.    
  18. $ make check   
  19.    
  20. ============================================================================  
  21. Testsuite summary for libressl 2.0.0 
  22. ============================================================================  
  23. # TOTAL: 41 
  24. # PASS:  41 
  25. # SKIP:  0 
  26. # XFAIL: 0 
  27. # FAIL:  0 
  28. # XPASS: 0 
  29. # ERROR: 0 
  30. ============================================================================ 

性能测试方法

  • 小服务器,双核,2GB内存,没有特别的调整

  • 从另一台服务器发起性能测试,使用 automake ab(abmeter)脚本

  • 打开 keepalive

  • url: / (由 nginx 缓存)

  • 测试了  10,20,30...100 个并发连接, 每次100.000 个请求

  • 每个配置测试 4 轮

  • 累积结果 / 4 -> 每次运行的值

  • OpenSSL Valhalla Rampage

  • https://www.ssllabs.com/ssltest/analyze.html?d=mare-system.de

英文原文:Nginx + LibreSSL - a first test

译文链接:http://www.oschina.net/translate/nginx-libressl-first-test

责任编辑:林师授 来源: 开源中国社区 编译
相关推荐

2012-04-19 10:10:50

红帽

2022-01-18 09:35:36

GNOME编辑器Linux

2009-03-30 17:43:29

四核服务器AMD

2015-06-10 16:05:26

NginxFastcgi

2021-07-23 11:38:42

Android AutBeta测试项目谷歌

2011-03-23 11:19:37

2021-07-01 17:21:13

iOS 15公测版苹果

2018-08-02 17:11:33

iOS 12苹果bug

2012-02-16 10:18:04

Nginx压力测试

2020-12-29 10:50:33

KubernetesKubernetes 运维

2021-06-28 11:00:48

QEMUWindows 11Windows

2014-07-23 10:07:34

2018-03-26 20:49:08

图像分类

2012-07-20 14:48:54

Nginx测试

2019-03-14 13:28:53

Android Q B谷歌开发者

2019-10-12 08:40:43

HTTP 3UDB协议服务器

2010-09-30 09:09:04

2018-05-17 13:53:54

IBMLinuxONE

2022-05-13 15:32:11

GNOME文本编辑器

2015-06-26 16:30:24

Docker云容器技术
点赞
收藏

51CTO技术栈公众号