Nginx

無法讓 Nginx 提供正確的證書鏈

  • January 30, 2015

我正在嘗試使用 StartSSL 證書設置 Nginx (1.4.6-1ubuntu3.1)。我一直在關注文件以使其正常工作,但 Nginx 僅提供伺服器證書,而不提供中間證書。

我的伺服器配置:

server {
   server_name lanzz.org www.lanzz.org;
   root /var/www/lanzz.org/public;
   index index.html;
   include listen.conf;
   ssl_certificate /etc/ssl/nginx/lanzz.org.chained.pem;
   ssl_certificate_key /etc/ssl/nginx/lanzz.org.key;
}

我的證書包(/etc/ssl/nginx/lanzz.org.chained.pem):

-----BEGIN CERTIFICATE-----
MIIGMzCCBRugAwIBAgIDE5WyMA0GCSqGSIb3DQEBCwUAMIGMMQswCQYDVQQGEwJJ
TDEWMBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMiU2VjdXJlIERpZ2l0
YWwgQ2VydGlmaWNhdGUgU2lnbmluZzE4MDYGA1UEAxMvU3RhcnRDb20gQ2xhc3Mg
MSBQcmltYXJ5IEludGVybWVkaWF0ZSBTZXJ2ZXIgQ0EwHhcNMTQxMDE0MTMzNjI1
WhcNMTUxMDE1MTUzODI3WjBKMQswCQYDVQQGEwJHQjEWMBQGA1UEAxMNd3d3Lmxh
bnp6Lm9yZzEjMCEGCSqGSIb3DQEJARYUcG9zdG1hc3RlckBsYW56ei5vcmcwggEi
MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCs8AMEaHFRZ4S3gGYhDJg05vj3
EadGIi29EJf5iW1YwKduuA62Zv9D2JGh2FCBJDBTswefxFbs2v/HHdP70gr0l669
Vz47RUJgn6xH13xEnVv5btQfPtioLQJNwLnBDR3ycw+9I/CGq+/BmStXBT2fTlDp
7FlDaemkc/mjd4TM6DBL0mfsAfqcSA4GHgQraJSwMyRGn3lon02mOWsDso6nTMEt
QYmCvYoM7wVtiBxKGP9Q6Nz3s5Ouc1U7mxKxuLNIO5ZeT+zocW7HQXk1sGal/Hxi
Y+Us/SsmcDAvqvI9f44Xe4StM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DxDX3M570uvh+v2/miI
RHLq0gfGabDBoYvvF0nXYbFFSF87ICHpW7LM9NfpMfULFWE7epTj69m8f5SuauNi
YpaoZHy4h/OZMn6SolK+u/hlz8nyMPyLwcKmltdfieFcNID1j0cHL7SRv7Gifl9L
WtBbnySGBVFaaQNlQ0lxxeBvlDRr9hvYqbBMflPrj0jfyjO1SPo2ShpTpjMM0InN
SRXNiTE8kMBy12VLUjWKRhFEuT2OKGWmPnmeXAhEKa2wNREuIU640ucQPl2Eg7PD
wuTSxv0JS3QJ3fGz0xk+gA2iCxnwOOfFwq/iI9th4p1cbiCJSS4jarJiwUW0n6+L
p/EiO/h94pDQehn7Skzj0n1fSoMD7SfWI55rjbRZotnvbIIp3XUZPD9MEI3vu3Un
0q6Dp6jOW6c=
-----END CERTIFICATE-----

輸出openssl s_client -connect lanzz.org:443

CONNECTED(00000003)
depth=0 C = GB, CN = www.lanzz.org, emailAddress = postmaster@lanzz.org
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 C = GB, CN = www.lanzz.org, emailAddress = postmaster@lanzz.org
verify error:num=27:certificate not trusted
verify return:1
depth=0 C = GB, CN = www.lanzz.org, emailAddress = postmaster@lanzz.org
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
0 s:/C=GB/CN=www.lanzz.org/emailAddress=postmaster@lanzz.org
  i:/C=IL/O=StartCom Ltd./OU=Secure Digital Certificate Signing/CN=StartCom Class 1 Primary Intermediate Server CA
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIGMzCCBRugAwIBAgIDE5WyMA0GCSqGSIb3DQEBCwUAMIGMMQswCQYDVQQGEwJJ
TDEWMBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMiU2VjdXJlIERpZ2l0
YWwgQ2VydGlmaWNhdGUgU2lnbmluZzE4MDYGA1UEAxMvU3RhcnRDb20gQ2xhc3Mg
MSBQcmltYXJ5IEludGVybWVkaWF0ZSBTZXJ2ZXIgQ0EwHhcNMTQxMDE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yBvZiB0aGUgU3RhcnRDb20gQ0EgcG9saWN5LCByZWxpYW5jZSBvbmx5IGZv
ciB0aGUgaW50ZW5kZWQgcHVycG9zZSBpbiBjb21wbGlhbmNlIG9mIHRoZSByZWx5
aW5nIHBhcnR5IG9ibGlnYXRpb25zLjA1BgNVHR8ELjAsMCqgKKAmhiRodHRwOi8v
Y3JsLnN0YXJ0c3NsLmNvbS9jcnQxLWNybC5jcmwwgY4GCCsGAQUFBwEBBIGBMH8w
OQYIKwYBBQUHMAGGLWh0dHA6Ly9vY3NwLnN0YXJ0c3NsLmNvbS9zdWIvY2xhc3Mx
L3NlcnZlci9jYTBCBggrBgEFBQcwAoY2aHR0cDovL2FpYS5zdGFydHNzbC5jb20v
Y2VydHMvc3ViLmNsYXNzMS5zZXJ2ZXIuY2EuY3J0MCMGA1UdEgQcMBqGGGh0dHA6
Ly93d3cuc3RhcnRzc2wuY29tLzANBgkqhkiG9w0BAQsFAAOCAQEAEom5lVxCbfu9
3K+BuowfCgTyA4keiQcYmTUJYXRBV9OiFUc/V5tXyhmgdyYeJB3oKMaEQ3glClZm
ueXUkALhaIlEzXjoNZgOh/bdbBPwfOq2WMBaWJbXX3x4C77s52zPBbkqhsBq5nge
1YDho1Z7tVYe8iyqBPUIFq0//LfGVAMoR7ZSwVpUgeiWs3oVKQMyR2BzrNSq392L
f8kIdIQ8fTgomtCZ2F2qp8EBIxl6G4UVpeN3Nes9UODpdNeG6lzLoSvYsVX+eWXx
3m29Aem42vlMhtQL1dtahb71nTY26qWAG337fwQoRxAXht3vSs0HhdUEHyGQGsuu
k+fcnm/wiQ==
-----END CERTIFICATE-----
subject=/C=GB/CN=www.lanzz.org/emailAddress=postmaster@lanzz.org
issuer=/C=IL/O=StartCom Ltd./OU=Secure Digital Certificate Signing/CN=StartCom Class 1 Primary Intermediate Server CA
---
No client certificate CA names sent
---
SSL handshake has read 2256 bytes and written 451 bytes
---
New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-SHA
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
   Protocol  : TLSv1
   Cipher    : ECDHE-RSA-AES256-SHA
   Session-ID: 958F818087118CA5F3344796B13FD463097B08ACF4F5497A3AD7A40D550FA155
   Session-ID-ctx: 
   Master-Key: 3AB94A204BF8E30BEDC615832E7DD5B2347458A6324A85C8C02BD441AF9F39BA09191BEB85A5F40ED99049013A51F7AB
   Key-Arg   : None
   PSK identity: None
   PSK identity hint: None
   SRP username: None
   TLS session ticket lifetime hint: 300 (seconds)
   TLS session ticket:
   0000 - 27 5f 93 a2 81 ed c3 e7-ba 41 27 0f 8f 95 be f5   '_.......A'.....
   0010 - 68 a6 7a 8b 1a 84 71 6a-00 cd ec 94 fc 3f 2c 64   h.z...qj.....?,d
   0020 - d8 b1 19 76 ba 8e ff e6-97 39 89 fb 1d 69 47 4a   ...v.....9...iGJ
   0030 - b3 40 96 a5 30 9e c6 b6-79 7f 93 36 02 31 4d 16   .@..0...y..6.1M.
   0040 - 2b d9 d6 0b 3c a9 07 10-63 af 89 b2 19 0e ac b3   +...<...c.......
   0050 - 3f c2 41 df f5 23 62 5c-11 d8 f6 be 55 1e e2 74   ?.A..#b\....U..t
   0060 - 6c 4f cd ba 72 e1 a1 f7-b4 fb d1 19 6d 33 d5 63   lO..r.......m3.c
   0070 - d6 94 a0 e6 e5 0c 33 e4-67 e3 49 fe fc b1 d5 25   ......3.g.I....%
   0080 - 9d d7 d3 72 b4 fb 5a 1f-6e 7f 5e 4b 41 21 3a d5   ...r..Z.n.^KA!:.
   0090 - 37 ef 4a b4 35 2d 14 bd-29 77 6d c5 6c c5 3c a4   7.J.5-..)wm.l.<.

   Start Time: 1422525959
   Timeout   : 300 (sec)
   Verify return code: 21 (unable to verify the first certificate)
---

如您所見,鏈中只有一個證書,Nginx 不提供中間證書。

我已經驗證 Nginx 看到的是正確的捆綁包,因為如果我將伺服器證書與中間證書交換,我確實會SSL_CTX_use_PrivateKey_file看到文件中提到的預期錯誤。當我以正確的順序放置它們時,我在啟動時看不到錯誤,在請求時看不到錯誤,但在 SSL 會話中看不到中間證書。

該鏈在其他方面是正確的,因為我已經驗證已經具有中間體的瀏覽器將接受伺服器證書,並且我已經驗證了中間體的指紋和我在瀏覽器中看到的伺服器證書與我得到的匹配伺服器上我的捆綁包中的證書。

我在這裡完全不知所措,我不知道還能嘗試什麼。我認為一切都是書上設置的,但我仍然沒有得到預期的結果s_client即使遇到這樣的問題,我也找不到其他人,更不用說任何現有的解決方案了。我什至嘗試在捆綁包中包含根證書,但沒有任何效果。任何提示或指示將不勝感激。

事實證明,Nginx 文件有些誤導,並且與 SNI 不一致。

執行openssl s_client -connect lanzz.org:443

CONNECTED(00000003)
depth=0 C = GB, CN = www.lanzz.org, emailAddress = postmaster@lanzz.org
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 C = GB, CN = www.lanzz.org, emailAddress = postmaster@lanzz.org
verify error:num=27:certificate not trusted
verify return:1
depth=0 C = GB, CN = www.lanzz.org, emailAddress = postmaster@lanzz.org
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
0 s:/C=GB/CN=www.lanzz.org/emailAddress=postmaster@lanzz.org
  i:/C=IL/O=StartCom Ltd./OU=Secure Digital Certificate Signing/CN=StartCom Class 1 Primary Intermediate Server CA
---

執行openssl s_client -connect lanzz.org:443 -servername lanzz.org

CONNECTED(00000003)
depth=1 C = IL, O = StartCom Ltd., OU = Secure Digital Certificate Signing, CN = StartCom Class 1 Primary Intermediate Server CA
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
0 s:/C=GB/CN=www.lanzz.org/emailAddress=postmaster@lanzz.org
  i:/C=IL/O=StartCom Ltd./OU=Secure Digital Certificate Signing/CN=StartCom Class 1 Primary Intermediate Server CA
1 s:/C=IL/O=StartCom Ltd./OU=Secure Digital Certificate Signing/CN=StartCom Class 1 Primary Intermediate Server CA
  i:/C=IL/O=StartCom Ltd./OU=Secure Digital Certificate Signing/CN=StartCom Certification Authority
---

顯然,一方面我的預設虛擬主機配置了錯誤的證書文件(一個沒有中間文件),另一方面openssl s_client沒有自動為傳遞給-connect參數的主機名提供 SNI 擴展。Nginx 文件天真地提供了一個僅測試預設虛擬主機的 SSL 設置的命令,我認為這很少是人們通常想要的。

TL;DR:-servername在測試 SSL 鏈時提供參數:

openssl s_client -connect <hostname>:443 -servername <hostname>

引用自:https://serverfault.com/questions/663332