我正在開發 CloudFlare 背后的 API,我想完全驗證連接以擴展安全性。我現在使用的平臺是 Windows 10。
首先,我下載了在 CloudFlare 網站上找到的一些 CA(Cloudflare_CA.pem、origin_ca_rsa_root.pem、origin_ca_ecc_root.pem),然后在 CURL 中設定所需選項后嘗試聯系 API:
struct curl_blob blob;
std::string cf_pem = ...;
blob.data = cf_pem.data();
blob.len = cf_pem.size();
blob.flags = CURL_BLOB_COPY;
curl_easy_setopt(pcurl, CURLOPT_CAINFO_BLOB, &blob);
/* Set the default value: strict certificate check please */
curl_easy_setopt(pcurl, CURLOPT_SSL_VERIFYPEER, 1L);
/* Set the default value: strict name check please */
curl_easy_setopt(pcurl, CURLOPT_SSL_VERIFYHOST, 2L);
全部失敗,PEER 驗證錯誤。然后我用 openssl 測驗了以下內容:
openssl s_client -connect website:443
結果是:
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Server public key is 256 bit
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 20 (unable to get local issuer certificate)
之后我發現了一個非常好的命令,它基本上從給定的網站“提取”了 CA:
openssl s_client -showcerts -servername website -connect website:443 > cacert.pem
最后我仍然收到:
Verify return code: 20 (unable to get local issuer certificate)
但是cacert.pem是在其中包含一些內容創建的。我在那時-----BEGIN CERTIFICATE-----/-----END CERTIFICATE-----之間獲取了第一個證書并將其放入代碼中。
驗證成功,CURL 不再抱怨。但是,我無法聯系 CloudFlare 下的其他主機,這意味著此 CA 不“正常”。
所以我的問題是,該怎么辦?我如何為 CloudFlare 找到正確的 CA?
請指教,我相信其他開發人員可能會面臨同樣的問題。
uj5u.com熱心網友回復:
查看典型的 Cloudflare 托管站點,認證路徑以“DigiCert Baltimore Root”的根 CA 結束。

在 Windows 10 中,應該可以在本地機器證書 -> 受信任的根證書頒發機構 -> 證書 -> Baltimore Cyber??Trust Root 下找到此證書。
或者,從源下載(如果您使用此方法,可能還需要驗證指紋) - https://www.digicert.com/kb/digicert-root-certificates.htm
uj5u.com熱心網友回復:
正如@David 提到的,CloudFlare 使用 DigiCert Root CA(一般來說)是正確的。然而,正如他們在他們的網站上所說的那樣,這可能會改變;這意味著如果你硬編碼這樣的檢查,你的軟體將來可能會在任何時候崩潰。
沒有進一步的原因,選項如下:
選項 A
從 CloudFlare 購買“高級”證書而不是通用證書,然后選擇所需的根 CA。這確保(在一定程度上)它是相同的,并且您可以使用該 CA 驗證任何域(在我的情況下,我使用 DigiCert Root CA 進行了測驗)。
選項 B
使用mTLS功能(mTLS 是保護 API 的好方法)。這是首選的長期解決方案,但缺點是您還需要在軟體中提供/嵌入證書私鑰。但是,如果證書遭到破壞,您可以隨時撤銷該證書。
對于我的選擇,我還想驗證 CA,但是使用 DigiCert Root CA 不適用于 mTLS,這很清楚,因為在您的儀表板上您將看到:
Review Client Certificate for CN=Cloudflare, C=US
Validity Period: 15 Years
Authority: Cloudflare Managed CA for ....
我找不到/下載“Cloudflare Managed CA”的根證書。我對此不滿意,因此為確保一切正常,我使用了此處的“Baltimore Cyber??Trust Root”:https : //baltimore-cybertrust-root.chain-demos.digicert.com/info/index.html
現在,您還可以使用帶有 CA 驗證的 CURL 的 mTLS:
// https://baltimore-cybertrust-root.chain-demos.digicert.com/info/index.html
std::string ca_pem = ...
// mtls.cert
std::string cert = ...
// mtls.key
std::string key = ...
struct curl_blob blob;
blob.data = cert.data();
blob.len = cert.size();
blob.flags = CURL_BLOB_COPY;
curl_easy_setopt(pcurl, CURLOPT_SSLCERT_BLOB, &blob);
curl_easy_setopt(pcurl, CURLOPT_SSLCERTTYPE, "PEM");
blob.data = key.data();
blob.len = key.size();
curl_easy_setopt(pcurl, CURLOPT_SSLKEY_BLOB, &blob);
curl_easy_setopt(pcurl, CURLOPT_SSLKEYTYPE, "PEM");
blob.data = ca_pem.data();
blob.len = ca_pem.size();
curl_easy_setopt(pcurl, CURLOPT_CAINFO_BLOB, &blob);
關于 mTLS 的重要說明:
- 如果您將它嵌入到您的軟體中,請確保您輸入努力來加密您的證書/密鑰。
- 我不建議您將證書/密鑰作為 軟體旁邊的單獨檔案提供。
- Even if you encrypt your cert/key inside your software there is always a way to get it, either via unpacking/de-onfuscabtion/de-virtualization; either during runtime... etc. My advise is for you to immediately re-encrypt the data or delete it from memory ASAP.
- In case someone gets your cert/key, first generate a new cert/key then update your software and afterwards revoke the existing one. Then you should look into how someone got your cert/key and try to mitigate the issue.
Please feel free to comment if you disagree with anything and/or/if you have some suggestions/improvements.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/338135.html
下一篇:如何替換LPTSTR的值?
