Winhttp - 如果对等证书无效,则防止成功握手

时间:2018-02-06 13:40:21

标签: security delphi network-programming delphi-7 winhttp

编辑:主题: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;

必需; 为了安全合规性,连接必须在SSL握手后立即终止。 Incase对等证书被视为无效。

实际: 什么'发生的是,即使证书无效,客户端(使用winhttp)也会进行呼叫并成功确认TLS握手。但是,在握手之后和完成请求之前,它会终止连接抛出'12175'错误。哪个是无效证书的错误。哪个是对的。

问题: 为了遵守传递,WinHTTP必须不允许成功握手,因此更早地终止连接。

下面附有Wireshark屏幕:(10.0.0.221是服务器) enter image description here

1 个答案:

答案 0 :(得分:3)

我认为你在这里运气不好,因为这就是WinHTTP的设计方式。在WinHttpConnect功能文档(强调)中 nServerPort 参数的值 INTERNET_DEFAULT_HTTPS_PORT 中描述的方式会发生什么:

  

<强> INTERNET_DEFAULT_HTTPS_PORT

     

使用HTTPS服务器的默认端口(端口443)。选择这个   端口不会自动建立安全连接。你必须   仍然使用指定使用安全事务语义   WINHTP_FLAG_SECURE标志与WinHttpOpenRequest。

您需要使用指定的 WINHTTP_FLAG_SECURE 标志打开(并发送)请求,以建立安全连接。