為什麼 curl 需要根證書和中間證書才能安全地連接到 HTTP 伺服器?
我創建了一個根證書、一個中間證書和一個伺服器證書:
root (ca.cert.pem) | +---intermediate (intermediate/certs/intermediate.cert.pem) | +---www.example.com ↳ certificate: (intermediate/certs/www.example.com.cert.pem) ↳ private key: (intermediate/private/www.example.com.key.pem)
我使用私鑰和公共證書
www.example.com
來使用 Node 創建一個 HTTPS 伺服器:var tls = require('tls'); var fs = require('fs'); var options = { key: fs.readFileSync('intermediate/private/www.example.com.key.pem'), cert: fs.readFileSync('intermediate/certs/www.example.com.cert.pem') }; tls.createServer(options, function (s) { s.write("welcome!\n"); s.pipe(s); }).listen(8000);
我想驗證如果客戶端有權訪問根證書 (
ca.cert.pem
),它可以成功地向https://www.example.com:8000
.如果我嘗試:
$ curl -v --cacert certs/ca.cert.pem https://www.example.com:8000
它失敗並顯示錯誤消息:
* Rebuilt URL to: https://www.example.com:8000/ * Trying 127.0.0.1... * TCP_NODELAY set * Connected to www.example.com (127.0.0.1) port 8000 (#0) * ALPN, offering h2 * ALPN, offering http/1.1 * successfully set certificate verify locations: * CAfile: /certs/ca.cert.pem CApath: /etc/ssl/certs * TLSv1.2 (OUT), TLS handshake, Client hello (1): * TLSv1.2 (IN), TLS handshake, Server hello (2): * NPN, no overlap, use HTTP1.1 * TLSv1.2 (IN), TLS handshake, Certificate (11): * TLSv1.2 (OUT), TLS alert, Server hello (2): * SSL certificate problem: unable to get local issuer certificate * stopped the pause stream! * Closing connection 0 curl: (60) SSL certificate problem: unable to get local issuer certificate More details here: https://curl.haxx.se/docs/sslcerts.html curl failed to verify the legitimacy of the server and therefore could not establish a secure connection to it. To learn more about this situation and how to fix it, please visit the web page mentioned above.
但是,如果我創建一個證書鏈:
$ cat intermediate/certs/intermediate.cert.pem \ certs/ca.cert.pem > intermediate/certs/ca-chain.cert.pem
並將其用作 curl 的
--cacert
,一切都按預期工作:$ curl -v --cacert intermediate/certs/ca-chain.cert.pem https://www.example.com:8000 * Rebuilt URL to: https://www.example.com:8000/ * Trying 127.0.0.1... * TCP_NODELAY set * Connected to www.example.com (127.0.0.1) port 8000 (#0) * ALPN, offering h2 * ALPN, offering http/1.1 * successfully set certificate verify locations: * CAfile: /intermediate/certs/ca-chain.cert.pem CApath: /etc/ssl/certs * TLSv1.2 (OUT), TLS handshake, Client hello (1): * TLSv1.2 (IN), TLS handshake, Server hello (2): * NPN, no overlap, use HTTP1.1 * TLSv1.2 (IN), TLS handshake, Certificate (11): * TLSv1.2 (IN), TLS handshake, Server key exchange (12): * TLSv1.2 (IN), TLS handshake, Server finished (14): * TLSv1.2 (OUT), TLS handshake, Client key exchange (16): * TLSv1.2 (OUT), TLS change cipher, Client hello (1): * TLSv1.2 (OUT), TLS handshake, Unknown (67): * TLSv1.2 (OUT), TLS handshake, Finished (20): * TLSv1.2 (IN), TLS handshake, Finished (20): * SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256 * ALPN, server did not agree to a protocol * Server certificate: * subject: C=AU; ST=Victoria; L=Melbourne; O=ACME; OU=ACME Web; CN=www.example.com; emailAddress=web@example.com * start date: Nov 12 11:32:59 2018 GMT * expire date: Nov 22 11:32:59 2019 GMT * common name: www.example.com (matched) * issuer: C=AU; ST=Victoria; O=ACME; OU=ACME Certificate Authority; CN=ACME Intermediate CA; emailAddress=contact2@example.com * SSL certificate verify ok. > GET / HTTP/1.1 > Host: www.example.com:8000 > User-Agent: curl/7.58.0 > Accept: */* > welcome! GET / HTTP/1.1 Host: www.example.com:8000 User-Agent: curl/7.58.0 Accept: */*
如果我沒看錯的話,和瀏覽器類似,curl應該只需要根證書來驗證SSL證書的簽名即可
www.example.com
。那麼,為什麼curl
需要根證書和中間證書才能驗證它確實與正確的伺服器通信呢?PEM 文件
以下是此設置中使用的 PEM 文件的內容(丟棄的未加密 PEM 文件):
ca.cert.pem
-----BEGIN CERTIFICATE----- MIIGKDCCBBCgAwIBAgIJAI6dJpvVkggoMA0GCSqGSIb3DQEBCwUAMIGgMQswCQYD V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VP0 uSTuJhZPVC8VkNYPwmXvO24i59E9Sth31ti822Q+brkkh9tefiQLzWMQ+/kZPCnn 41If/WT0Ihl5rZbxUCL6SA6jDDR5EZlYF8RGrQ5KRHg3O8YC302KpcQyBruJjDwH RdvqTw+w7wmnRu4Bml+nkQKBgQCyH18rQSE0bUJq25bc72mN/BYd7LMn4aGV8ejw 9RRqlal9+SJV2MLNYX6xSAggFrMCC6WzmsV88hmePFhDTqDH/1ffIxmyrZb5ZAfo ZaCH4H/NXas/FkgKQepyTsO8lPOEpppTdTQE4+dihkqyrdfItp/SCfxc8teRKhlY k+tSiQKBgQCYMRBErv5O88aJbzePkTdv2xGyp1/p4tkvpwZ6bZDbI2W3YAv89F+K 3yWhzrP5kqNr3aHVChuKu4IFiL8D4NxT9dMqtC3M1OgZ5cD3YXlQBZBp7e5pOv43 AxFqQoilp/4VTcFmzYvKjUE262ekT60hZT7qqAfoI3r2vEF3gd4Kbw== -----END RSA PRIVATE KEY-----
(0) 這與加密無關,可能在 security.SX 上會更好,它有很多關於證書連結和 HTTPS 瀏覽器和伺服器使用的問題,但是……
(1) 雖然現在 AIA 的使用比過去更多,並且可能是一種可接受的解決方法,但官方的標準解決方案是伺服器必鬚髮送包含中間證書的鏈,但不一定包括根證書,請參閱https://www.rfc-editor.org/rfc/rfc5246#section-7.4.2 et pred(注意:TLS1.3 rfc8446 對此略有改變)或熊史詩 https://security.stackexchange.com/questions/ 20803/how-does-ssl-tls-work在標題證書和身份驗證,以及…
(2)幾個Stack上有很多關於如何配置各種伺服器按要求發送證書鏈的Q;對於 nodejs,以
https://stackoverflow.com/questions/19104215/node-js-express-js-chain-certificate-not-working
https://stackoverflow.com/questions/16224064/running-ssl-node-js開頭-server-with-godaddy-gd-bundle-crt