Python requets can work with root CA, but socket must use intermedia CA

The reason requests can work with just a root CA while the ssl module's socket connection requires the intermediate CA is likely due to differences in how they handle certificate chains.

Why requests Works with Only the Root CA
The requests library is built on top of urllib3, which, in turn, relies on OpenSSL. OpenSSL includes a feature called certificate chain building, which attempts to retrieve missing intermediate certificates from the server if they are not provided in the certificate chain. This feature allows requests to work as long as the root CA is trusted because requests can fetch any missing intermediate certificates on its own.

Why ssl Requires the Intermediate CA
The ssl module in Python does not automatically retrieve missing intermediate certificates. For SSL/TLS handshakes, it requires a complete chain of trust to be locally available. This means you need to provide both the root and intermediate certificates in your custom CA bundle if the server’s response is missing them.

Solutions
Provide a Complete Certificate Chain in the CA Bundle: You can combine the root and intermediate certificates into a single file as a workaround:

cat rootCA.pem intermediateCA.pem > full_chain_ca_bundle.pem

Then, load this full_chain_ca_bundle.pem in your ssl socket configuration:


context.load_verify_locations(cafile="full_chain_ca_bundle.pem")

Request the Server to Include the Intermediate Certificates: If you have control over the server configuration, configure it to send the full certificate chain. This will allow the ssl client to validate the certificate with only the root CA, similar to how requests works.

Use requests as a Fallback: If using requests is an option, consider leveraging it to handle SSL/TLS connections directly, as it provides built-in handling for such cases.

Example with Full Chain Bundle
Here’s how to modify your function to load a complete certificate chain:

def __connect_socket(self) -> None:
    """Establish tcp connection"""
    context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
    context.minimum_version = ssl.TLSVersion.TLSv1_2
    context.verify_mode = ssl.CERT_REQUIRED
    context.load_verify_locations(cafile="full_chain_ca_bundle.pem")  # Load full chain

    # Load client cert and key if needed
    if self.ssl_client_cert:
        context.load_cert_chain(certfile=self.ssl_client_cert[0], keyfile=self.ssl_client_cert[1])

    # Create and connect the socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.__socket_client = context.wrap_socket(sock, server_hostname=self.tcp_address[0])
    self.__socket_client.connect(self.tcp_address)

This approach should enable your ssl-based socket client to connect successfully with both the root and intermediate certificates available, creating a full chain of trust.

$ openssl s_client -connect example.com:8883 -CAfile server.pem

# Show more detailed certificate verification
$ openssl s_client -connect localhost:8883 -CAfile server.pem -verify 8 -verify_return_error

# 
sudo openssl x509 -in server.pem -text -noout | grep "Issuer\|Subject"

The error message "Verify return code: 20 (unable to get local issuer certificate)" you’re encountering, unable to get local issuer certificate, indicates that the SSL/TLS client is unable to verify the server’s certificate because it cannot find the appropriate issuer certificate in its trust store. Here are some steps to troubleshoot and resolve this issue:

Check Certificate Chain:

Ensure that the server is presenting the complete certificate chain, including intermediate certificates. You can use tools like openssl s_client -connect <host>:<port> -showcerts to see the full chain.

https://poe.com/s/mO9W6JT2imvwvM6Gs4oW

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末锻梳,一起剝皮案震驚了整個濱河市咐旧,隨后出現(xiàn)的幾起案子般眉,更是在濱河造成了極大的恐慌劫映,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件齐帚,死亡現(xiàn)場離奇詭異落追,居然都是意外死亡戚篙,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進店門屡江,熙熙樓的掌柜王于貴愁眉苦臉地迎上來芭概,“玉大人,你說我怎么就攤上這事惩嘉“罩蓿” “怎么了?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵文黎,是天一觀的道長惹苗。 經(jīng)常有香客問我,道長耸峭,這世上最難降的妖魔是什么桩蓉? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮劳闹,結(jié)果婚禮上院究,老公的妹妹穿的比我還像新娘。我一直安慰自己本涕,他們只是感情好业汰,可當(dāng)我...
    茶點故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著菩颖,像睡著了一般样漆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上晦闰,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天氛濒,我揣著相機與錄音产场,去河邊找鬼。 笑死舞竿,一個胖子當(dāng)著我的面吹牛京景,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播骗奖,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼确徙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了执桌?” 一聲冷哼從身側(cè)響起鄙皇,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎仰挣,沒想到半個月后伴逸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡膘壶,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年错蝴,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颓芭。...
    茶點故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡顷锰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出亡问,到底是詐尸還是另有隱情官紫,我是刑警寧澤,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布州藕,位于F島的核電站束世,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏床玻。R本人自食惡果不足惜良狈,卻給世界環(huán)境...
    茶點故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望笨枯。 院中可真熱鬧薪丁,春花似錦、人聲如沸馅精。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽洲敢。三九已至漫玄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背睦优。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工渗常, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人汗盘。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓皱碘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親隐孽。 傳聞我的和親對象是個殘疾皇子癌椿,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內(nèi)容