2 * Copyright 2008 Hans Leidekker for CodeWeavers
3 * Copyright 2013 Jacek Caban for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
30 #include "wine/debug.h"
31 #include "winhttp_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
35 static int sock_send(int fd
, const void *msg
, size_t len
, int flags
)
40 if ((ret
= send(fd
, msg
, len
, flags
)) == -1) WARN("send error %u\n", WSAGetLastError());
42 while(ret
== -1 && WSAGetLastError() == WSAEINTR
);
46 static int sock_recv(int fd
, void *msg
, size_t len
, int flags
)
51 if ((ret
= recv(fd
, msg
, len
, flags
)) == -1) WARN("recv error %u\n", WSAGetLastError());
53 while(ret
== -1 && WSAGetLastError() == WSAEINTR
);
57 static DWORD
netconn_verify_cert( PCCERT_CONTEXT cert
, WCHAR
*server
, DWORD security_flags
, BOOL check_revocation
)
59 HCERTSTORE store
= cert
->hCertStore
;
61 CERT_CHAIN_PARA chainPara
= { sizeof(chainPara
), { 0 } };
62 PCCERT_CHAIN_CONTEXT chain
;
63 char oid_server_auth
[] = szOID_PKIX_KP_SERVER_AUTH
;
64 char *server_auth
[] = { oid_server_auth
};
65 DWORD err
= ERROR_SUCCESS
;
67 TRACE("verifying %s\n", debugstr_w( server
));
68 chainPara
.RequestedUsage
.Usage
.cUsageIdentifier
= 1;
69 chainPara
.RequestedUsage
.Usage
.rgpszUsageIdentifier
= server_auth
;
70 ret
= CertGetCertificateChain( NULL
, cert
, NULL
, store
, &chainPara
,
71 check_revocation
? CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
: 0,
75 if (chain
->TrustStatus
.dwErrorStatus
)
77 static const DWORD supportedErrors
=
78 CERT_TRUST_IS_NOT_TIME_VALID
|
79 CERT_TRUST_IS_UNTRUSTED_ROOT
|
80 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
82 if (chain
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_NOT_TIME_VALID
)
84 if (!(security_flags
& SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
))
85 err
= ERROR_WINHTTP_SECURE_CERT_DATE_INVALID
;
87 else if ((chain
->TrustStatus
.dwErrorStatus
&
88 CERT_TRUST_IS_UNTRUSTED_ROOT
) ||
89 (chain
->TrustStatus
.dwErrorStatus
&
90 CERT_TRUST_IS_PARTIAL_CHAIN
))
92 if (!(security_flags
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
))
93 err
= ERROR_WINHTTP_SECURE_INVALID_CA
;
95 else if ((chain
->TrustStatus
.dwErrorStatus
&
96 CERT_TRUST_IS_OFFLINE_REVOCATION
) ||
97 (chain
->TrustStatus
.dwErrorStatus
&
98 CERT_TRUST_REVOCATION_STATUS_UNKNOWN
))
99 err
= ERROR_WINHTTP_SECURE_CERT_REV_FAILED
;
100 else if (chain
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_REVOKED
)
101 err
= ERROR_WINHTTP_SECURE_CERT_REVOKED
;
102 else if (chain
->TrustStatus
.dwErrorStatus
&
103 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
)
105 if (!(security_flags
& SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
))
106 err
= ERROR_WINHTTP_SECURE_CERT_WRONG_USAGE
;
108 else if (chain
->TrustStatus
.dwErrorStatus
& ~supportedErrors
)
109 err
= ERROR_WINHTTP_SECURE_INVALID_CERT
;
113 CERT_CHAIN_POLICY_PARA policyPara
;
114 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara
;
115 CERT_CHAIN_POLICY_STATUS policyStatus
;
116 CERT_CHAIN_CONTEXT chainCopy
;
118 /* Clear chain->TrustStatus.dwErrorStatus so
119 * CertVerifyCertificateChainPolicy will verify additional checks
120 * rather than stopping with an existing, ignored error.
122 memcpy(&chainCopy
, chain
, sizeof(chainCopy
));
123 chainCopy
.TrustStatus
.dwErrorStatus
= 0;
124 sslExtraPolicyPara
.u
.cbSize
= sizeof(sslExtraPolicyPara
);
125 sslExtraPolicyPara
.dwAuthType
= AUTHTYPE_SERVER
;
126 sslExtraPolicyPara
.pwszServerName
= server
;
127 sslExtraPolicyPara
.fdwChecks
= security_flags
;
128 policyPara
.cbSize
= sizeof(policyPara
);
129 policyPara
.dwFlags
= 0;
130 policyPara
.pvExtraPolicyPara
= &sslExtraPolicyPara
;
131 ret
= CertVerifyCertificateChainPolicy( CERT_CHAIN_POLICY_SSL
,
132 &chainCopy
, &policyPara
,
134 /* Any error in the policy status indicates that the
135 * policy couldn't be verified.
137 if (ret
&& policyStatus
.dwError
)
139 if (policyStatus
.dwError
== CERT_E_CN_NO_MATCH
)
140 err
= ERROR_WINHTTP_SECURE_CERT_CN_INVALID
;
142 err
= ERROR_WINHTTP_SECURE_INVALID_CERT
;
145 CertFreeCertificateChain( chain
);
148 err
= ERROR_WINHTTP_SECURE_CHANNEL_ERROR
;
149 TRACE("returning %08x\n", err
);
153 static BOOL winsock_loaded
;
155 void netconn_unload( void )
157 if (winsock_loaded
) WSACleanup();
160 static BOOL WINAPI
winsock_startup( INIT_ONCE
*once
, void *param
, void **ctx
)
164 if (!(ret
= WSAStartup( MAKEWORD(1,1), &data
))) winsock_loaded
= TRUE
;
165 else ERR( "WSAStartup failed: %d\n", ret
);
169 static void winsock_init(void)
171 static INIT_ONCE once
= INIT_ONCE_STATIC_INIT
;
172 InitOnceExecuteOnce( &once
, winsock_startup
, NULL
, NULL
);
175 static void set_blocking( struct netconn
*conn
, BOOL blocking
)
177 ULONG state
= !blocking
;
178 ioctlsocket( conn
->socket
, FIONBIO
, &state
);
181 DWORD
netconn_create( struct hostdata
*host
, const struct sockaddr_storage
*sockaddr
, int timeout
,
182 struct netconn
**ret_conn
)
184 struct netconn
*conn
;
185 unsigned int addr_len
;
190 if (!(conn
= heap_alloc_zero( sizeof(*conn
) ))) return ERROR_OUTOFMEMORY
;
192 conn
->sockaddr
= *sockaddr
;
193 if ((conn
->socket
= socket( sockaddr
->ss_family
, SOCK_STREAM
, 0 )) == -1)
195 ret
= WSAGetLastError();
196 WARN("unable to create socket (%u)\n", ret
);
201 switch (conn
->sockaddr
.ss_family
)
204 addr_len
= sizeof(struct sockaddr_in
);
207 addr_len
= sizeof(struct sockaddr_in6
);
210 ERR( "unhandled family %u\n", conn
->sockaddr
.ss_family
);
212 return ERROR_INVALID_PARAMETER
;
215 if (timeout
> 0) set_blocking( conn
, FALSE
);
217 if (!connect( conn
->socket
, (const struct sockaddr
*)&conn
->sockaddr
, addr_len
)) ret
= ERROR_SUCCESS
;
220 ret
= WSAGetLastError();
221 if (ret
== WSAEWOULDBLOCK
|| ret
== WSAEINPROGRESS
)
224 TIMEVAL timeval
= { 0, timeout
* 1000 };
228 FD_SET( conn
->socket
, &set
);
229 if ((res
= select( conn
->socket
+ 1, NULL
, &set
, NULL
, &timeval
)) > 0) ret
= ERROR_SUCCESS
;
230 else if (!res
) ret
= ERROR_WINHTTP_TIMEOUT
;
234 if (timeout
> 0) set_blocking( conn
, TRUE
);
238 WARN("unable to connect to host (%u)\n", ret
);
239 closesocket( conn
->socket
);
245 return ERROR_SUCCESS
;
248 void netconn_close( struct netconn
*conn
)
252 heap_free( conn
->peek_msg_mem
);
253 heap_free(conn
->ssl_buf
);
254 heap_free(conn
->extra_buf
);
255 DeleteSecurityContext(&conn
->ssl_ctx
);
257 closesocket( conn
->socket
);
258 release_host( conn
->host
);
262 DWORD
netconn_secure_connect( struct netconn
*conn
, WCHAR
*hostname
, DWORD security_flags
, CredHandle
*cred_handle
,
263 BOOL check_revocation
)
265 SecBuffer out_buf
= {0, SECBUFFER_TOKEN
, NULL
}, in_bufs
[2] = {{0, SECBUFFER_TOKEN
}, {0, SECBUFFER_EMPTY
}};
266 SecBufferDesc out_desc
= {SECBUFFER_VERSION
, 1, &out_buf
}, in_desc
= {SECBUFFER_VERSION
, 2, in_bufs
};
268 SIZE_T read_buf_size
= 2048;
272 const CERT_CONTEXT
*cert
;
273 SECURITY_STATUS status
;
274 DWORD res
= ERROR_SUCCESS
;
276 const DWORD isc_req_flags
= ISC_REQ_ALLOCATE_MEMORY
|ISC_REQ_USE_SESSION_KEY
|ISC_REQ_CONFIDENTIALITY
277 |ISC_REQ_SEQUENCE_DETECT
|ISC_REQ_REPLAY_DETECT
|ISC_REQ_MANUAL_CRED_VALIDATION
;
279 if (!(read_buf
= heap_alloc( read_buf_size
))) return ERROR_OUTOFMEMORY
;
281 status
= InitializeSecurityContextW(cred_handle
, NULL
, hostname
, isc_req_flags
, 0, 0, NULL
, 0,
282 &ctx
, &out_desc
, &attrs
, NULL
);
284 assert(status
!= SEC_E_OK
);
286 while(status
== SEC_I_CONTINUE_NEEDED
|| status
== SEC_E_INCOMPLETE_MESSAGE
) {
287 if(out_buf
.cbBuffer
) {
288 assert(status
== SEC_I_CONTINUE_NEEDED
);
290 TRACE("sending %u bytes\n", out_buf
.cbBuffer
);
292 size
= sock_send(conn
->socket
, out_buf
.pvBuffer
, out_buf
.cbBuffer
, 0);
293 if(size
!= out_buf
.cbBuffer
) {
294 ERR("send failed\n");
295 res
= ERROR_WINHTTP_SECURE_CHANNEL_ERROR
;
299 FreeContextBuffer(out_buf
.pvBuffer
);
300 out_buf
.pvBuffer
= NULL
;
301 out_buf
.cbBuffer
= 0;
304 if(status
== SEC_I_CONTINUE_NEEDED
) {
305 assert(in_bufs
[1].cbBuffer
< read_buf_size
);
307 memmove(read_buf
, (BYTE
*)in_bufs
[0].pvBuffer
+in_bufs
[0].cbBuffer
-in_bufs
[1].cbBuffer
, in_bufs
[1].cbBuffer
);
308 in_bufs
[0].cbBuffer
= in_bufs
[1].cbBuffer
;
310 in_bufs
[1].BufferType
= SECBUFFER_EMPTY
;
311 in_bufs
[1].cbBuffer
= 0;
312 in_bufs
[1].pvBuffer
= NULL
;
315 assert(in_bufs
[0].BufferType
== SECBUFFER_TOKEN
);
316 assert(in_bufs
[1].BufferType
== SECBUFFER_EMPTY
);
318 if(in_bufs
[0].cbBuffer
+ 1024 > read_buf_size
) {
321 new_read_buf
= heap_realloc(read_buf
, read_buf_size
+ 1024);
323 status
= E_OUTOFMEMORY
;
327 in_bufs
[0].pvBuffer
= read_buf
= new_read_buf
;
328 read_buf_size
+= 1024;
331 size
= sock_recv(conn
->socket
, read_buf
+in_bufs
[0].cbBuffer
, read_buf_size
-in_bufs
[0].cbBuffer
, 0);
333 status
= ERROR_WINHTTP_SECURE_CHANNEL_ERROR
;
337 TRACE("recv %lu bytes\n", size
);
339 in_bufs
[0].cbBuffer
+= size
;
340 in_bufs
[0].pvBuffer
= read_buf
;
341 status
= InitializeSecurityContextW(cred_handle
, &ctx
, hostname
, isc_req_flags
, 0, 0, &in_desc
,
342 0, NULL
, &out_desc
, &attrs
, NULL
);
343 TRACE("InitializeSecurityContext ret %08x\n", status
);
345 if(status
== SEC_E_OK
) {
346 if(in_bufs
[1].BufferType
== SECBUFFER_EXTRA
)
347 FIXME("SECBUFFER_EXTRA not supported\n");
349 status
= QueryContextAttributesW(&ctx
, SECPKG_ATTR_STREAM_SIZES
, &conn
->ssl_sizes
);
350 if(status
!= SEC_E_OK
) {
351 WARN("Could not get sizes\n");
355 status
= QueryContextAttributesW(&ctx
, SECPKG_ATTR_REMOTE_CERT_CONTEXT
, (void*)&cert
);
356 if(status
== SEC_E_OK
) {
357 res
= netconn_verify_cert(cert
, hostname
, security_flags
, check_revocation
);
358 CertFreeCertificateContext(cert
);
359 if(res
!= ERROR_SUCCESS
) {
360 WARN("cert verify failed: %u\n", res
);
364 WARN("Could not get cert\n");
368 conn
->ssl_buf
= heap_alloc(conn
->ssl_sizes
.cbHeader
+ conn
->ssl_sizes
.cbMaximumMessage
+ conn
->ssl_sizes
.cbTrailer
);
370 res
= ERROR_OUTOFMEMORY
;
378 if(status
!= SEC_E_OK
|| res
!= ERROR_SUCCESS
) {
379 WARN("Failed to initialize security context: %08x\n", status
);
380 heap_free(conn
->ssl_buf
);
381 conn
->ssl_buf
= NULL
;
382 DeleteSecurityContext(&ctx
);
383 return ERROR_WINHTTP_SECURE_CHANNEL_ERROR
;
387 TRACE("established SSL connection\n");
390 return ERROR_SUCCESS
;
393 static DWORD
send_ssl_chunk( struct netconn
*conn
, const void *msg
, size_t size
)
395 SecBuffer bufs
[4] = {
396 {conn
->ssl_sizes
.cbHeader
, SECBUFFER_STREAM_HEADER
, conn
->ssl_buf
},
397 {size
, SECBUFFER_DATA
, conn
->ssl_buf
+conn
->ssl_sizes
.cbHeader
},
398 {conn
->ssl_sizes
.cbTrailer
, SECBUFFER_STREAM_TRAILER
, conn
->ssl_buf
+conn
->ssl_sizes
.cbHeader
+size
},
399 {0, SECBUFFER_EMPTY
, NULL
}
401 SecBufferDesc buf_desc
= {SECBUFFER_VERSION
, ARRAY_SIZE(bufs
), bufs
};
404 memcpy( bufs
[1].pvBuffer
, msg
, size
);
405 if ((res
= EncryptMessage(&conn
->ssl_ctx
, 0, &buf_desc
, 0)) != SEC_E_OK
)
407 WARN("EncryptMessage failed: %08x\n", res
);
411 if (sock_send( conn
->socket
, conn
->ssl_buf
, bufs
[0].cbBuffer
+ bufs
[1].cbBuffer
+ bufs
[2].cbBuffer
, 0 ) < 1)
413 WARN("send failed\n");
414 return WSAGetLastError();
417 return ERROR_SUCCESS
;
420 DWORD
netconn_send( struct netconn
*conn
, const void *msg
, size_t len
, int *sent
)
424 const BYTE
*ptr
= msg
;
431 chunk_size
= min( len
, conn
->ssl_sizes
.cbMaximumMessage
);
432 if ((res
= send_ssl_chunk( conn
, ptr
, chunk_size
)))
440 return ERROR_SUCCESS
;
443 if ((*sent
= sock_send( conn
->socket
, msg
, len
, 0 )) < 0) return WSAGetLastError();
444 return ERROR_SUCCESS
;
447 static DWORD
read_ssl_chunk( struct netconn
*conn
, void *buf
, SIZE_T buf_size
, SIZE_T
*ret_size
, BOOL
*eof
)
449 const SIZE_T ssl_buf_size
= conn
->ssl_sizes
.cbHeader
+conn
->ssl_sizes
.cbMaximumMessage
+conn
->ssl_sizes
.cbTrailer
;
451 SecBufferDesc buf_desc
= {SECBUFFER_VERSION
, ARRAY_SIZE(bufs
), bufs
};
452 SSIZE_T size
, buf_len
;
456 assert(conn
->extra_len
< ssl_buf_size
);
458 if(conn
->extra_len
) {
459 memcpy(conn
->ssl_buf
, conn
->extra_buf
, conn
->extra_len
);
460 buf_len
= conn
->extra_len
;
462 heap_free(conn
->extra_buf
);
463 conn
->extra_buf
= NULL
;
465 if ((buf_len
= sock_recv( conn
->socket
, conn
->ssl_buf
+ conn
->extra_len
, ssl_buf_size
- conn
->extra_len
, 0)) < 0)
466 return WSAGetLastError();
471 return ERROR_SUCCESS
;
479 memset(bufs
, 0, sizeof(bufs
));
480 bufs
[0].BufferType
= SECBUFFER_DATA
;
481 bufs
[0].cbBuffer
= buf_len
;
482 bufs
[0].pvBuffer
= conn
->ssl_buf
;
484 switch ((res
= DecryptMessage( &conn
->ssl_ctx
, &buf_desc
, 0, NULL
)))
489 case SEC_I_RENEGOTIATE
:
490 TRACE("renegotiate\n");
491 return ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED
;
493 case SEC_I_CONTEXT_EXPIRED
:
494 TRACE("context expired\n");
496 return ERROR_SUCCESS
;
498 case SEC_E_INCOMPLETE_MESSAGE
:
499 assert(buf_len
< ssl_buf_size
);
501 if ((size
= sock_recv( conn
->socket
, conn
->ssl_buf
+ buf_len
, ssl_buf_size
- buf_len
, 0 )) < 1)
502 return SEC_E_INCOMPLETE_MESSAGE
;
508 WARN("failed: %08x\n", res
);
511 } while (res
!= SEC_E_OK
);
513 for(i
= 0; i
< ARRAY_SIZE(bufs
); i
++) {
514 if(bufs
[i
].BufferType
== SECBUFFER_DATA
) {
515 size
= min(buf_size
, bufs
[i
].cbBuffer
);
516 memcpy(buf
, bufs
[i
].pvBuffer
, size
);
517 if(size
< bufs
[i
].cbBuffer
) {
518 assert(!conn
->peek_len
);
519 conn
->peek_msg_mem
= conn
->peek_msg
= heap_alloc(bufs
[i
].cbBuffer
- size
);
521 return ERROR_OUTOFMEMORY
;
522 conn
->peek_len
= bufs
[i
].cbBuffer
-size
;
523 memcpy(conn
->peek_msg
, (char*)bufs
[i
].pvBuffer
+size
, conn
->peek_len
);
530 for(i
= 0; i
< ARRAY_SIZE(bufs
); i
++) {
531 if(bufs
[i
].BufferType
== SECBUFFER_EXTRA
) {
532 conn
->extra_buf
= heap_alloc(bufs
[i
].cbBuffer
);
534 return ERROR_OUTOFMEMORY
;
536 conn
->extra_len
= bufs
[i
].cbBuffer
;
537 memcpy(conn
->extra_buf
, bufs
[i
].pvBuffer
, conn
->extra_len
);
541 return ERROR_SUCCESS
;
544 DWORD
netconn_recv( struct netconn
*conn
, void *buf
, size_t len
, int flags
, int *recvd
)
547 if (!len
) return ERROR_SUCCESS
;
557 *recvd
= min( len
, conn
->peek_len
);
558 memcpy( buf
, conn
->peek_msg
, *recvd
);
559 conn
->peek_len
-= *recvd
;
560 conn
->peek_msg
+= *recvd
;
562 if (conn
->peek_len
== 0)
564 heap_free( conn
->peek_msg_mem
);
565 conn
->peek_msg_mem
= NULL
;
566 conn
->peek_msg
= NULL
;
568 /* check if we have enough data from the peek buffer */
569 if (!(flags
& MSG_WAITALL
) || *recvd
== len
) return ERROR_SUCCESS
;
576 if ((res
= read_ssl_chunk( conn
, (BYTE
*)buf
+ size
, len
- size
, &cread
, &eof
)))
578 WARN("read_ssl_chunk failed: %u\n", res
);
579 if (!size
) return res
;
589 } while (!size
|| ((flags
& MSG_WAITALL
) && size
< len
));
591 TRACE("received %ld bytes\n", size
);
593 return ERROR_SUCCESS
;
596 if ((*recvd
= sock_recv( conn
->socket
, buf
, len
, flags
)) < 0) return WSAGetLastError();
597 return ERROR_SUCCESS
;
600 ULONG
netconn_query_data_available( struct netconn
*conn
)
602 return conn
->secure
? conn
->peek_len
: 0;
605 DWORD
netconn_set_timeout( struct netconn
*netconn
, BOOL send
, int value
)
607 int opt
= send
? SO_SNDTIMEO
: SO_RCVTIMEO
;
608 if (setsockopt( netconn
->socket
, SOL_SOCKET
, opt
, (void *)&value
, sizeof(value
) ) == -1)
610 DWORD err
= WSAGetLastError();
611 WARN("setsockopt failed (%u)\n", err
);
614 return ERROR_SUCCESS
;
617 BOOL
netconn_is_alive( struct netconn
*netconn
)
623 set_blocking( netconn
, FALSE
);
624 len
= sock_recv( netconn
->socket
, &b
, 1, MSG_PEEK
);
625 err
= WSAGetLastError();
626 set_blocking( netconn
, TRUE
);
628 return len
== 1 || (len
== -1 && err
== WSAEWOULDBLOCK
);
631 static DWORD
resolve_hostname( const WCHAR
*name
, INTERNET_PORT port
, struct sockaddr_storage
*sa
)
633 ADDRINFOW
*res
, hints
;
636 memset( &hints
, 0, sizeof(hints
) );
637 /* Prefer IPv4 to IPv6 addresses, since some web servers do not listen on
638 * their IPv6 addresses even though they have IPv6 addresses in the DNS.
640 hints
.ai_family
= AF_INET
;
642 ret
= GetAddrInfoW( name
, NULL
, &hints
, &res
);
645 TRACE("failed to get IPv4 address of %s, retrying with IPv6\n", debugstr_w(name
));
646 hints
.ai_family
= AF_INET6
;
647 ret
= GetAddrInfoW( name
, NULL
, &hints
, &res
);
650 TRACE("failed to get address of %s\n", debugstr_w(name
));
651 return ERROR_WINHTTP_NAME_NOT_RESOLVED
;
654 memcpy( sa
, res
->ai_addr
, res
->ai_addrlen
);
655 switch (res
->ai_family
)
658 ((struct sockaddr_in
*)sa
)->sin_port
= htons( port
);
661 ((struct sockaddr_in6
*)sa
)->sin6_port
= htons( port
);
665 FreeAddrInfoW( res
);
666 return ERROR_SUCCESS
;
671 const WCHAR
*hostname
;
673 struct sockaddr_storage
*addr
;
678 static void CALLBACK
resolve_proc( TP_CALLBACK_INSTANCE
*instance
, void *ctx
)
680 struct async_resolve
*async
= ctx
;
681 async
->result
= resolve_hostname( async
->hostname
, async
->port
, async
->addr
);
682 SetEvent( async
->done
);
685 DWORD
netconn_resolve( WCHAR
*hostname
, INTERNET_PORT port
, struct sockaddr_storage
*addr
, int timeout
)
689 if (!timeout
) ret
= resolve_hostname( hostname
, port
, addr
);
692 struct async_resolve async
;
694 async
.hostname
= hostname
;
697 if (!(async
.done
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
))) return GetLastError();
698 if (!TrySubmitThreadpoolCallback( resolve_proc
, &async
, NULL
))
700 CloseHandle( async
.done
);
701 return GetLastError();
703 if (WaitForSingleObject( async
.done
, timeout
) != WAIT_OBJECT_0
) ret
= ERROR_WINHTTP_TIMEOUT
;
704 else ret
= async
.result
;
705 CloseHandle( async
.done
);
711 const void *netconn_get_certificate( struct netconn
*conn
)
713 const CERT_CONTEXT
*ret
;
716 if (!conn
->secure
) return NULL
;
717 res
= QueryContextAttributesW(&conn
->ssl_ctx
, SECPKG_ATTR_REMOTE_CERT_CONTEXT
, (void*)&ret
);
718 return res
== SEC_E_OK
? ret
: NULL
;
721 int netconn_get_cipher_strength( struct netconn
*conn
)
723 SecPkgContext_ConnectionInfo conn_info
;
726 if (!conn
->secure
) return 0;
727 res
= QueryContextAttributesW(&conn
->ssl_ctx
, SECPKG_ATTR_CONNECTION_INFO
, (void*)&conn_info
);
729 WARN("QueryContextAttributesW failed: %08x\n", res
);
730 return res
== SEC_E_OK
? conn_info
.dwCipherStrength
: 0;