Muh*_*han 3 delphi security network-programming winhttp delphi-7
编辑:线程:https://security.stackexchange.com/questions/179352/winhttp-prevent-successful-handshake-if-peer-certificate-is-invalid讨论并回答了这个问题。该功能暂时可以关闭
在delphi项目上使用windows平台提供的WinHTTP来调用服务器。
脚本:
userAgent := 'TestClient.exe';
hsession := WinHttpOpen(pwidechar(userAgent), WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, nil, nil, 0);
if(hsession = nil) then ShowMessage('Failed WinhttpOpen');
p := 'https';
port := 443;
requestflags := WINHTTP_FLAG_SECURE;
server := '10.0.0.221';
hconnection := WinHttpConnect(hsession, PWideChar(server), port, 0);
if(hconnection = nil) then
begin
le := GetLastError;
ShowMessage('Failed to connect: ' + IntToStr(le));
end;
Action := 'GET';
hInetRequest := WinHttpOpenRequest(hconnection, pwidechar(Action), nil, nil, nil, nil, WINHTTP_FLAG_SECURE);
if(hInetRequest = nil) then
begin
le := GetLastError;
ShowMessage('Failed to connect: ' + IntToStr(le));
end;
WinResult:=WinHttpSendRequest(hInetRequest, nil,0, 0, 0,0,0);
if(not WinResult) then
begin
le := GetLastError;
WinHttpCloseHandle(hInetRequest);
ShowMessage('No result obtained : ' + IntToStr(le));
end;
Run Code Online (Sandbox Code Playgroud)
必需的; 为了符合安全性,连接必须在 SSL 握手后立即终止。如果同行证书被视为无效。
实际情况: 发生的情况是,客户端(使用 winhttp)进行调用并成功确认 TLS 握手,即使证书无效。但是,在握手之后和完成请求之前,它会终止连接并抛出“12175”错误。这是无效证书的错误。哪个是对的。
问题: 为了通过合规性,WinHTTP 不得允许成功握手,从而提前终止连接。
我认为你只是运气不好,因为 WinHTTP 就是这样设计的。所发生的情况按照WinHttpConnect函数文档中nServerPort参数值INTERNET_DEFAULT_HTTPS_PORT中描述的方式进行(强调):
INTERNET_DEFAULT_HTTPS_PORT
使用 HTTPS 服务器的默认端口(端口 443)。选择此端口不会自动建立安全连接。您仍然必须通过将 WINHTTP_FLAG_SECURE 标志与 WinHttpOpenRequest 一起使用来指定安全事务语义的使用。
这意味着您需要打开(并发送)指定WINHTTP_FLAG_SECURE标志的请求来建立安全连接。