Skip to content

Commit 8806bb9

Browse files
author
eagleychen
committed
ssl 更新
1 parent 78092a2 commit 8806bb9

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

hub/utils/codec.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,31 @@ def __init__(self):
117117
def create_content(self, cadata=None):
118118
if cadata is None:
119119
cadata = self.__iot_ca_crt
120-
return ssl.create_default_context(ssl.Purpose.CLIENT_AUTH, cadata=cadata)
121120

122-
123-
124-
121+
# 创建SSL上下文
122+
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
123+
124+
# 设置TLS协议版本
125+
context.minimum_version = ssl.TLSVersion.TLSv1_2
126+
127+
# 配置证书验证
128+
context.verify_mode = ssl.CERT_REQUIRED
129+
context.check_hostname = False # 禁用主机名验证
130+
131+
# 加载CA证书
132+
try:
133+
if cadata:
134+
# 清理证书字符串,确保格式正确
135+
cleaned_cadata = "\n".join(
136+
line.strip() for line in cadata.splitlines()
137+
if line.strip() and not line.strip().startswith("--")
138+
)
139+
context.load_verify_locations(cadata=cleaned_cadata.encode())
140+
141+
# 同时加载系统默认证书
142+
context.load_default_certs()
143+
except Exception as e:
144+
# 如果证书加载失败,回退到不验证证书(仅用于测试)
145+
context.verify_mode = ssl.CERT_NONE
146+
147+
return context

0 commit comments

Comments
 (0)