事件由來
如果最近發現iOS APP打開h5非常慢,達到5秒以上,而安卓又正常,如果剛好你又是使用了Let's Encrypt 的免費SSL服務,恭喜你,你可能是一位運營商干擾的受害者。
原因:
近期由於衆所周知的原因,國內無法直接訪問 Let's Encrypt 的 OSCP 域名,導致出現了不能簽發證書、OCSP Stapling 失敗、網頁打開慢等問題。
經檢查目前是 ocsp.int-x3.letsencrypt.org 的 cname 域名 a771.dscq.akamai.net 受到了干擾,可以採用本地修改hosts的方案進行臨時處理。
在/etc/hosts中添加
23.32.3.72 ocsp.int-x3.letsencrypt.org
生成 OCSP Stapling 文件
經過以下步驟生成所需的用於 OCSP Stapling 驗證的文件
首先,需要準備三份證書:
站點證書(website.pem)+ 根證書(root.pem)+ 中間證書(intermediate.pem)
中間證書和根證書,需要根據你的證書的 CA,去下載對應的證書
以下列出了 Let's Encrypt 的中間證書和根證書的下載地址:
根證書:
DST Root CA X3 https://ssl-tools.net/certificates/dac9024f54d8f6df94935fb1732638ca6ad77c13.pem
ISRG Root X1 https://letsencrypt.org/certs/isrgrootx1.pem
中間證書:
Let's Encrypt Authority X1 https://letsencrypt.org/certs/lets-encrypt-x1-cross-signed.pem
Let's Encrypt Authority X2 https://letsencrypt.org/certs/lets-encrypt-x2-cross-signed.pem
Let's Encrypt Authority X3 https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem
Let's Encrypt Authority X4 https://letsencrypt.org/certs/lets-encrypt-x4-cross-signed.pem
這裏以 DST Root CA X3 根證書 + Let's Encrypt Authority X3 中間證書 爲例(現在 Let's Encrypt 簽發的證書基本都是這樣的組合):
# 下載根證書和中間證書 wget -O root.pem https://ssl-tools.net/certificates/dac9024f54d8f6df94935fb1732638ca6ad77c13.pem wget -O intermediate.pem https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem # 生成 OCSP Stapling 驗證文件 # 注意,中間證書在上、根證書在下 cat cat intermediate.pem > chained.pem cat root.pem >> chained.pem
這樣,生成的 chained.pem 就是所需的 OCSP Stapling 驗證文件。
如果沒有 站點證書(website.pem),可以輸入如下命令:
openssl s_client -connect mydomain.com:443 -servername mydomain.com-showcerts < /dev/null 2>&1
[root@hadoop-nginx .acme.sh]# openssl s_client -connect mydomain.com:443 -servername mydomain.com-showcerts < /dev/null 2>&1
CONNECTED(00000003)
depth=2 O = Digital Signature Trust Co., CN = DST Root CA X3
verify return:1
depth=1 C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
verify return:1
depth=0 CN = app6.kedaqianbao.com
verify return:1
---
Certificate chain
0 s:/CN=app6.kedaqianbao.com
i:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
-----BEGIN CERTIFICATE-----
MIIFXzCCB......................................................
...............................................................
...........................................
..........
.
-----END CERTIFICATE-----
1 s:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
i:/O=Digital Signature Trust Co./CN=DST Root CA X3
-----BEGIN CERTIFICATE-----
MIIEkjCCA3qgA..................................................
...........................................
...........................
..................
-----END CERTIFICATE-----
---
Server certificate
subject=/CN=app6.kedaqianbao.com
issuer=/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
---
No client certificate CA names sent
Peer signing digest: SHA512
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 3246 bytes and written 444 bytes
---
New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
Protocol : TLSv1.2
Cipher : ECDHE-RSA-AES256-GCM-SHA384
Session-ID: A3E868ED8A9FFC0687B8379A6AD26AA7B368FB8B270DDD6C61441485E56839A4
Session-ID-ctx:
Master-Key: C0D9365E17DA7C48053CC179D6E15C0E12FB2572C00E58725044D1A8E7B88823D7757E31C847F01543D3FD0CFB01517E
Key-Arg : None
Krb5 Principal: None
PSK identity: None
PSK identity hint: None
TLS session ticket lifetime hint: 600 (seconds)
TLS session ticket:
0000 - 09 34 28 07 47 20 76 19-96 1c ca 19 17 3c bf 32 .4(.G v......<.2
0010 - fc 6e d7 7d 0e 44 0e 45-18 b5 a4 00 a5 8d d9 98 .n.}.D.E........
0020 - f4 18 38 9e 1b b1 58 bb-26 b5 cb 60 b2 38 9a b9 ..8...X.&..`.8..
0030 - a0 e5 8c e2 81 9a 63 95-e2 9e 71 84 29 23 65 d8 ......c...q.)#e.
0040 - 1c 4a 76 78 02 ac c4 a0-e9 5d b2 23 87 db 47 8c .Jvx.....].#..G.
0050 - e6 52 95 45 80 a4 f5 d8-c2 4f 19 1b 2f 1e 92 b7 .R.E.....O../...
0060 - 43 53 e9 a4 af 00 4f 4c-d0 11 af 7b bf 5f 90 a9 CS....OL...{._..
0070 - c6 fe 7e a8 ca ac a4 08-a8 d2 83 fb cd 4d 27 e4 ..~..........M'.
0080 - e2 0d 17 46 00 53 39 2b-c8 5d c9 15 3f 60 2a 0c ...F.S9+.]..?`*.
0090 - d9 7e f7 11 eb 75 1a 0d-a9 06 e7 f5 38 bb 2b 39 .~...u......8.+9
00a0 - f7 9d 2a 07 f4 b5 a1 29-d3 39 c2 71 3b 84 d3 f5 ..*....).9.q;...
00b0 - fa 47 4a ab 3d ae de c6-b9 e7 4a 99 2f 10 7c 0c .GJ.=.....J./.|.
Start Time: 1592291163
Timeout : 300 (sec)
Verify return code: 0 (ok)
---
DONE
[root@hadoop-nginx .acme.sh]#
可以看到 0 和 1 兩個,-----BEGIN CERTIFICATE----- 開始的 證書文本。
0、表示 站點證書
1、 表示中間證書
複製 站點證書內容(需要包含-----BEGIN CERTIFICATE----- 和 -----END CERTIFICATE-----),爲 website.pem 文件。
OCSP Stapling Response
openssl x509 -in website.pem -noout -ocsp_uri
使用這個命令後,返回你的證書對應的 OCSP 服務地址
例如,Let's Encrypt 現在的 OCSP 服務地址是 http://ocsp.int-x3.letsencrypt.org/
以 Let's Encrypt 爲例,獲取站點證書的 OCSP Response
[root@hadoop-nginx kedawenhua]# openssl ocsp -no_nonce \
> -respout /home/docker/software/nginx/conf/ssl/mydomain.ocsp_res.der \
> -issuer intermediate.pem \
> -CAfile chained.pem \
> -VAfile chained.pem \
> -cert website.pem \
> -url http://ocsp.int-x3.letsencrypt.org \
> -header "HOST" "ocsp.int-x3.letsencrypt.org"
Response verify OK
website.pem: good
This Update: Jun 16 05:00:00 2020 GMT
Next Update: Jun 23 05:00:00 2020 GMT
-respout參數 是指定OCSP 響應數據的 保存位置,將這個位置填入在 nginx 配置文件的 ssl_stapling_file 中,如下開啓ssl_stapling。
這裏如果出現如下錯誤的話,說明你的openssl使用了1.1.0版本,這個時候已經不需要指定HOST,把上面命令中的“-header "HOST" "ocsp.int-x3.letsencrypt.org"”刪掉就好了。
Missing = in header key=value
ocsp: Use -help for summary.
ssl_stapling on;
ssl_stapling_verify on;
ssl_stapling_file /home/docker/software/nginx/conf/ssl/mydomain.ocsp_res.der;
重載nginx服務之後,檢查是否成功開啓。
openssl s_client -connect mydomain.com:443 -status -tlsextdebug < /dev/null 2>&1 | grep -i "OCSP response"
如下:
最後將有關操作製成腳本,添加到 crontab 中自動更新。
重新加載H5, 會發現訪問速度,基本恢復正常