2 #define NONAMELESSUNION
6 * Wininet - networking layer
8 * Copyright 2002 TransGaming Technologies Inc.
9 * Copyright 2013 Jacek Caban for CodeWeavers
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #define NONAMELESSUNION
44 #include "wine/debug.h"
46 #endif /* defined(__REACTOS__) */
48 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
50 static DWORD
netconn_verify_cert(netconn_t
*conn
, PCCERT_CONTEXT cert
, HCERTSTORE store
)
53 CERT_CHAIN_PARA chainPara
= { sizeof(chainPara
), { 0 } };
54 PCCERT_CHAIN_CONTEXT chain
;
55 char oid_server_auth
[] = szOID_PKIX_KP_SERVER_AUTH
;
56 char *server_auth
[] = { oid_server_auth
};
57 DWORD err
= ERROR_SUCCESS
, errors
;
59 static const DWORD supportedErrors
=
60 CERT_TRUST_IS_NOT_TIME_VALID
|
61 CERT_TRUST_IS_UNTRUSTED_ROOT
|
62 CERT_TRUST_IS_PARTIAL_CHAIN
|
63 CERT_TRUST_IS_NOT_SIGNATURE_VALID
|
64 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
66 TRACE("verifying %s\n", debugstr_w(conn
->server
->name
));
68 chainPara
.RequestedUsage
.Usage
.cUsageIdentifier
= 1;
69 chainPara
.RequestedUsage
.Usage
.rgpszUsageIdentifier
= server_auth
;
70 if (!(ret
= CertGetCertificateChain(NULL
, cert
, NULL
, store
, &chainPara
, 0, NULL
, &chain
))) {
72 return GetLastError();
75 errors
= chain
->TrustStatus
.dwErrorStatus
;
78 /* This seems strange, but that's what tests show */
79 if(errors
& CERT_TRUST_IS_PARTIAL_CHAIN
) {
80 WARN("ERROR_INTERNET_SEC_CERT_REV_FAILED\n");
81 err
= ERROR_INTERNET_SEC_CERT_REV_FAILED
;
83 conn
->security_flags
|= _SECURITY_FLAG_CERT_REV_FAILED
;
84 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_REVOCATION
))
88 if (chain
->TrustStatus
.dwErrorStatus
& ~supportedErrors
) {
89 WARN("error status %x\n", chain
->TrustStatus
.dwErrorStatus
& ~supportedErrors
);
90 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_INVALID_CERT
;
91 errors
&= supportedErrors
;
92 if(!conn
->mask_errors
)
94 WARN("unknown error flags\n");
97 if(errors
& CERT_TRUST_IS_NOT_TIME_VALID
) {
98 WARN("CERT_TRUST_IS_NOT_TIME_VALID\n");
99 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
)) {
100 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_CERT_DATE_INVALID
;
101 if(!conn
->mask_errors
)
103 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_DATE
;
105 errors
&= ~CERT_TRUST_IS_NOT_TIME_VALID
;
108 if(errors
& CERT_TRUST_IS_UNTRUSTED_ROOT
) {
109 WARN("CERT_TRUST_IS_UNTRUSTED_ROOT\n");
110 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
)) {
111 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_INVALID_CA
;
112 if(!conn
->mask_errors
)
114 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_CA
;
116 errors
&= ~CERT_TRUST_IS_UNTRUSTED_ROOT
;
119 if(errors
& CERT_TRUST_IS_PARTIAL_CHAIN
) {
120 WARN("CERT_TRUST_IS_PARTIAL_CHAIN\n");
121 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
)) {
122 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_INVALID_CA
;
123 if(!conn
->mask_errors
)
125 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_CA
;
127 errors
&= ~CERT_TRUST_IS_PARTIAL_CHAIN
;
130 if(errors
& CERT_TRUST_IS_NOT_SIGNATURE_VALID
) {
131 WARN("CERT_TRUST_IS_NOT_SIGNATURE_VALID\n");
132 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
)) {
133 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_INVALID_CA
;
134 if(!conn
->mask_errors
)
136 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_CA
;
138 errors
&= ~CERT_TRUST_IS_NOT_SIGNATURE_VALID
;
141 if(errors
& CERT_TRUST_IS_NOT_VALID_FOR_USAGE
) {
142 WARN("CERT_TRUST_IS_NOT_VALID_FOR_USAGE\n");
143 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_WRONG_USAGE
)) {
144 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_INVALID_CERT
;
145 if(!conn
->mask_errors
)
147 WARN("CERT_TRUST_IS_NOT_VALID_FOR_USAGE, unknown error flags\n");
149 errors
&= ~CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
152 if(err
== ERROR_INTERNET_SEC_CERT_REV_FAILED
) {
153 assert(conn
->security_flags
& SECURITY_FLAG_IGNORE_REVOCATION
);
158 if(!err
|| conn
->mask_errors
) {
159 CERT_CHAIN_POLICY_PARA policyPara
;
160 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara
;
161 CERT_CHAIN_POLICY_STATUS policyStatus
;
162 CERT_CHAIN_CONTEXT chainCopy
;
164 /* Clear chain->TrustStatus.dwErrorStatus so
165 * CertVerifyCertificateChainPolicy will verify additional checks
166 * rather than stopping with an existing, ignored error.
168 memcpy(&chainCopy
, chain
, sizeof(chainCopy
));
169 chainCopy
.TrustStatus
.dwErrorStatus
= 0;
170 sslExtraPolicyPara
.u
.cbSize
= sizeof(sslExtraPolicyPara
);
171 sslExtraPolicyPara
.dwAuthType
= AUTHTYPE_SERVER
;
172 sslExtraPolicyPara
.pwszServerName
= conn
->server
->name
;
173 sslExtraPolicyPara
.fdwChecks
= conn
->security_flags
;
174 policyPara
.cbSize
= sizeof(policyPara
);
175 policyPara
.dwFlags
= 0;
176 policyPara
.pvExtraPolicyPara
= &sslExtraPolicyPara
;
177 ret
= CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL
,
178 &chainCopy
, &policyPara
, &policyStatus
);
179 /* Any error in the policy status indicates that the
180 * policy couldn't be verified.
183 if(policyStatus
.dwError
== CERT_E_CN_NO_MATCH
) {
184 WARN("CERT_E_CN_NO_MATCH\n");
185 if(conn
->mask_errors
)
186 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_CN
;
187 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_CERT_CN_INVALID
;
188 }else if(policyStatus
.dwError
) {
189 WARN("policyStatus.dwError %x\n", policyStatus
.dwError
);
190 if(conn
->mask_errors
)
191 WARN("unknown error flags for policy status %x\n", policyStatus
.dwError
);
192 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_INVALID_CERT
;
195 err
= GetLastError();
200 WARN("failed %u\n", err
);
201 CertFreeCertificateChain(chain
);
202 if(conn
->server
->cert_chain
) {
203 CertFreeCertificateChain(conn
->server
->cert_chain
);
204 conn
->server
->cert_chain
= NULL
;
206 if(conn
->mask_errors
)
207 conn
->server
->security_flags
|= conn
->security_flags
& _SECURITY_ERROR_FLAGS_MASK
;
211 /* FIXME: Reuse cached chain */
212 if(conn
->server
->cert_chain
)
213 CertFreeCertificateChain(chain
);
215 conn
->server
->cert_chain
= chain
;
216 return ERROR_SUCCESS
;
219 static SecHandle cred_handle
, compat_cred_handle
;
220 static BOOL cred_handle_initialized
, have_compat_cred_handle
;
222 static CRITICAL_SECTION init_sechandle_cs
;
223 static CRITICAL_SECTION_DEBUG init_sechandle_cs_debug
= {
224 0, 0, &init_sechandle_cs
,
225 { &init_sechandle_cs_debug
.ProcessLocksList
,
226 &init_sechandle_cs_debug
.ProcessLocksList
},
227 0, 0, { (DWORD_PTR
)(__FILE__
": init_sechandle_cs") }
229 static CRITICAL_SECTION init_sechandle_cs
= { &init_sechandle_cs_debug
, -1, 0, 0, 0, 0 };
231 static BOOL
ensure_cred_handle(void)
233 SECURITY_STATUS res
= SEC_E_OK
;
235 EnterCriticalSection(&init_sechandle_cs
);
237 if(!cred_handle_initialized
) {
238 SCHANNEL_CRED cred
= {SCHANNEL_CRED_VERSION
};
239 SecPkgCred_SupportedProtocols prots
;
241 res
= AcquireCredentialsHandleW(NULL
, (WCHAR
*)UNISP_NAME_W
, SECPKG_CRED_OUTBOUND
, NULL
, &cred
,
242 NULL
, NULL
, &cred_handle
, NULL
);
243 if(res
== SEC_E_OK
) {
244 res
= QueryCredentialsAttributesA(&cred_handle
, SECPKG_ATTR_SUPPORTED_PROTOCOLS
, &prots
);
245 if(res
!= SEC_E_OK
|| (prots
.grbitProtocol
& SP_PROT_TLS1_1PLUS_CLIENT
)) {
246 cred
.grbitEnabledProtocols
= prots
.grbitProtocol
& ~SP_PROT_TLS1_1PLUS_CLIENT
;
247 res
= AcquireCredentialsHandleW(NULL
, (WCHAR
*)UNISP_NAME_W
, SECPKG_CRED_OUTBOUND
, NULL
, &cred
,
248 NULL
, NULL
, &compat_cred_handle
, NULL
);
249 have_compat_cred_handle
= res
== SEC_E_OK
;
253 cred_handle_initialized
= res
== SEC_E_OK
;
256 LeaveCriticalSection(&init_sechandle_cs
);
258 if(res
!= SEC_E_OK
) {
259 WARN("Failed: %08x\n", res
);
266 static BOOL winsock_loaded
= FALSE
;
268 static BOOL WINAPI
winsock_startup(INIT_ONCE
*once
, void *param
, void **context
)
273 res
= WSAStartup(MAKEWORD(2,2), &wsa_data
);
274 if(res
== ERROR_SUCCESS
)
275 winsock_loaded
= TRUE
;
277 ERR("WSAStartup failed: %u\n", res
);
281 void init_winsock(void)
283 static INIT_ONCE init_once
= INIT_ONCE_STATIC_INIT
;
284 InitOnceExecuteOnce(&init_once
, winsock_startup
, NULL
, NULL
);
287 static void set_socket_blocking(netconn_t
*conn
, BOOL is_blocking
)
289 if(conn
->is_blocking
!= is_blocking
) {
290 ULONG arg
= !is_blocking
;
291 ioctlsocket(conn
->socket
, FIONBIO
, &arg
);
293 conn
->is_blocking
= is_blocking
;
296 static DWORD
create_netconn_socket(server_t
*server
, netconn_t
*netconn
, DWORD timeout
)
304 assert(server
->addr_len
);
305 result
= netconn
->socket
= socket(server
->addr
.ss_family
, SOCK_STREAM
, 0);
307 set_socket_blocking(netconn
, FALSE
);
308 result
= connect(netconn
->socket
, (struct sockaddr
*)&server
->addr
, server
->addr_len
);
311 res
= WSAGetLastError();
312 if (res
== WSAEINPROGRESS
|| res
== WSAEWOULDBLOCK
) {
315 socklen_t len
= sizeof(res
);
316 TIMEVAL timeout_timeval
= {0, timeout
*1000};
319 FD_SET(netconn
->socket
, &set
);
320 res
= select(netconn
->socket
+1, NULL
, &set
, NULL
, &timeout_timeval
);
321 if(!res
|| res
== SOCKET_ERROR
) {
322 closesocket(netconn
->socket
);
323 netconn
->socket
= -1;
324 return ERROR_INTERNET_CANNOT_CONNECT
;
326 if (!getsockopt(netconn
->socket
, SOL_SOCKET
, SO_ERROR
, (void *)&res
, &len
) && !res
)
332 closesocket(netconn
->socket
);
333 netconn
->socket
= -1;
337 return ERROR_INTERNET_CANNOT_CONNECT
;
340 result
= setsockopt(netconn
->socket
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&flag
, sizeof(flag
));
342 WARN("setsockopt(TCP_NODELAY) failed\n");
344 return ERROR_SUCCESS
;
347 DWORD
create_netconn(server_t
*server
, DWORD security_flags
, BOOL mask_errors
, DWORD timeout
, netconn_t
**ret
)
352 netconn
= heap_alloc_zero(sizeof(*netconn
));
354 return ERROR_OUTOFMEMORY
;
356 netconn
->socket
= -1;
357 netconn
->security_flags
= security_flags
| server
->security_flags
;
358 netconn
->mask_errors
= mask_errors
;
359 list_init(&netconn
->pool_entry
);
360 SecInvalidateHandle(&netconn
->ssl_ctx
);
362 result
= create_netconn_socket(server
, netconn
, timeout
);
363 if (result
!= ERROR_SUCCESS
) {
368 server_addref(server
);
369 netconn
->server
= server
;
374 BOOL
is_valid_netconn(netconn_t
*netconn
)
376 return netconn
&& netconn
->socket
!= -1;
379 void close_netconn(netconn_t
*netconn
)
381 closesocket(netconn
->socket
);
382 netconn
->socket
= -1;
385 void free_netconn(netconn_t
*netconn
)
387 server_release(netconn
->server
);
389 if (netconn
->secure
) {
390 heap_free(netconn
->peek_msg_mem
);
391 netconn
->peek_msg_mem
= NULL
;
392 netconn
->peek_msg
= NULL
;
393 netconn
->peek_len
= 0;
394 heap_free(netconn
->ssl_buf
);
395 netconn
->ssl_buf
= NULL
;
396 heap_free(netconn
->extra_buf
);
397 netconn
->extra_buf
= NULL
;
398 netconn
->extra_len
= 0;
400 if (SecIsValidHandle(&netconn
->ssl_ctx
))
401 DeleteSecurityContext(&netconn
->ssl_ctx
);
403 close_netconn(netconn
);
407 void NETCON_unload(void)
409 if(cred_handle_initialized
)
410 FreeCredentialsHandle(&cred_handle
);
411 if(have_compat_cred_handle
)
412 FreeCredentialsHandle(&compat_cred_handle
);
413 DeleteCriticalSection(&init_sechandle_cs
);
418 int sock_send(int fd
, const void *msg
, size_t len
, int flags
)
423 ret
= send(fd
, msg
, len
, flags
);
425 while(ret
== -1 && WSAGetLastError() == WSAEINTR
);
429 int sock_recv(int fd
, void *msg
, size_t len
, int flags
)
434 ret
= recv(fd
, msg
, len
, flags
);
436 while(ret
== -1 && WSAGetLastError() == WSAEINTR
);
440 static DWORD
netcon_secure_connect_setup(netconn_t
*connection
, BOOL compat_mode
)
442 SecBuffer out_buf
= {0, SECBUFFER_TOKEN
, NULL
}, in_bufs
[2] = {{0, SECBUFFER_TOKEN
}, {0, SECBUFFER_EMPTY
}};
443 SecBufferDesc out_desc
= {SECBUFFER_VERSION
, 1, &out_buf
}, in_desc
= {SECBUFFER_VERSION
, 2, in_bufs
};
444 SecHandle
*cred
= &cred_handle
;
446 SIZE_T read_buf_size
= 2048;
451 const CERT_CONTEXT
*cert
;
452 SECURITY_STATUS status
;
453 DWORD res
= ERROR_SUCCESS
;
455 const DWORD isc_req_flags
= ISC_REQ_ALLOCATE_MEMORY
|ISC_REQ_USE_SESSION_KEY
|ISC_REQ_CONFIDENTIALITY
456 |ISC_REQ_SEQUENCE_DETECT
|ISC_REQ_REPLAY_DETECT
|ISC_REQ_MANUAL_CRED_VALIDATION
;
458 if(!ensure_cred_handle())
459 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
462 if(!have_compat_cred_handle
)
463 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
464 cred
= &compat_cred_handle
;
467 read_buf
= heap_alloc(read_buf_size
);
469 return ERROR_OUTOFMEMORY
;
471 status
= InitializeSecurityContextW(cred
, NULL
, connection
->server
->name
, isc_req_flags
, 0, 0, NULL
, 0,
472 &ctx
, &out_desc
, &attrs
, NULL
);
474 assert(status
!= SEC_E_OK
);
476 set_socket_blocking(connection
, TRUE
);
478 while(status
== SEC_I_CONTINUE_NEEDED
|| status
== SEC_E_INCOMPLETE_MESSAGE
) {
479 if(out_buf
.cbBuffer
) {
480 assert(status
== SEC_I_CONTINUE_NEEDED
);
482 TRACE("sending %u bytes\n", out_buf
.cbBuffer
);
484 size
= sock_send(connection
->socket
, out_buf
.pvBuffer
, out_buf
.cbBuffer
, 0);
485 if(size
!= out_buf
.cbBuffer
) {
486 ERR("send failed\n");
487 status
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
491 FreeContextBuffer(out_buf
.pvBuffer
);
492 out_buf
.pvBuffer
= NULL
;
493 out_buf
.cbBuffer
= 0;
496 if(status
== SEC_I_CONTINUE_NEEDED
) {
497 assert(in_bufs
[1].cbBuffer
< read_buf_size
);
499 memmove(read_buf
, (BYTE
*)in_bufs
[0].pvBuffer
+in_bufs
[0].cbBuffer
-in_bufs
[1].cbBuffer
, in_bufs
[1].cbBuffer
);
500 in_bufs
[0].cbBuffer
= in_bufs
[1].cbBuffer
;
502 in_bufs
[1].BufferType
= SECBUFFER_EMPTY
;
503 in_bufs
[1].cbBuffer
= 0;
504 in_bufs
[1].pvBuffer
= NULL
;
507 assert(in_bufs
[0].BufferType
== SECBUFFER_TOKEN
);
508 assert(in_bufs
[1].BufferType
== SECBUFFER_EMPTY
);
510 if(in_bufs
[0].cbBuffer
+ 1024 > read_buf_size
) {
513 new_read_buf
= heap_realloc(read_buf
, read_buf_size
+ 1024);
515 status
= E_OUTOFMEMORY
;
519 in_bufs
[0].pvBuffer
= read_buf
= new_read_buf
;
520 read_buf_size
+= 1024;
523 size
= sock_recv(connection
->socket
, read_buf
+in_bufs
[0].cbBuffer
, read_buf_size
-in_bufs
[0].cbBuffer
, 0);
525 WARN("recv error\n");
526 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
530 TRACE("recv %lu bytes\n", size
);
532 in_bufs
[0].cbBuffer
+= size
;
533 in_bufs
[0].pvBuffer
= read_buf
;
534 status
= InitializeSecurityContextW(cred
, &ctx
, connection
->server
->name
, isc_req_flags
, 0, 0, &in_desc
,
535 0, NULL
, &out_desc
, &attrs
, NULL
);
536 TRACE("InitializeSecurityContext ret %08x\n", status
);
538 if(status
== SEC_E_OK
) {
539 if(SecIsValidHandle(&connection
->ssl_ctx
))
540 DeleteSecurityContext(&connection
->ssl_ctx
);
541 connection
->ssl_ctx
= ctx
;
543 if(in_bufs
[1].BufferType
== SECBUFFER_EXTRA
)
544 FIXME("SECBUFFER_EXTRA not supported\n");
546 status
= QueryContextAttributesW(&ctx
, SECPKG_ATTR_STREAM_SIZES
, &connection
->ssl_sizes
);
547 if(status
!= SEC_E_OK
) {
548 WARN("Could not get sizes\n");
552 status
= QueryContextAttributesW(&ctx
, SECPKG_ATTR_REMOTE_CERT_CONTEXT
, (void*)&cert
);
553 if(status
== SEC_E_OK
) {
554 res
= netconn_verify_cert(connection
, cert
, cert
->hCertStore
);
555 CertFreeCertificateContext(cert
);
556 if(res
!= ERROR_SUCCESS
) {
557 WARN("cert verify failed: %u\n", res
);
561 WARN("Could not get cert\n");
565 connection
->ssl_buf
= heap_alloc(connection
->ssl_sizes
.cbHeader
+ connection
->ssl_sizes
.cbMaximumMessage
566 + connection
->ssl_sizes
.cbTrailer
);
567 if(!connection
->ssl_buf
) {
568 res
= GetLastError();
576 if(status
!= SEC_E_OK
|| res
!= ERROR_SUCCESS
) {
577 WARN("Failed to establish SSL connection: %08x (%u)\n", status
, res
);
578 heap_free(connection
->ssl_buf
);
579 connection
->ssl_buf
= NULL
;
580 return res
? res
: ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
583 TRACE("established SSL connection\n");
584 connection
->secure
= TRUE
;
585 connection
->security_flags
|= SECURITY_FLAG_SECURE
;
587 bits
= NETCON_GetCipherStrength(connection
);
589 connection
->security_flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
591 connection
->security_flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
593 connection
->security_flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
595 if(connection
->mask_errors
)
596 connection
->server
->security_flags
= connection
->security_flags
;
597 return ERROR_SUCCESS
;
600 /******************************************************************************
601 * NETCON_secure_connect
602 * Initiates a secure connection over an existing plaintext connection.
604 DWORD
NETCON_secure_connect(netconn_t
*connection
, server_t
*server
)
608 /* can't connect if we are already connected */
609 if(connection
->secure
) {
610 ERR("already connected\n");
611 return ERROR_INTERNET_CANNOT_CONNECT
;
614 if(server
!= connection
->server
) {
615 server_release(connection
->server
);
616 server_addref(server
);
617 connection
->server
= server
;
620 /* connect with given TLS options */
621 res
= netcon_secure_connect_setup(connection
, FALSE
);
622 if (res
== ERROR_SUCCESS
)
625 /* FIXME: when got version alert and FIN from server */
626 /* fallback to connect without TLSv1.1/TLSv1.2 */
627 if (res
== ERROR_INTERNET_SECURITY_CHANNEL_ERROR
&& have_compat_cred_handle
)
629 closesocket(connection
->socket
);
630 res
= create_netconn_socket(connection
->server
, connection
, 500);
631 if (res
!= ERROR_SUCCESS
)
633 res
= netcon_secure_connect_setup(connection
, TRUE
);
638 static BOOL
send_ssl_chunk(netconn_t
*conn
, const void *msg
, size_t size
)
640 SecBuffer bufs
[4] = {
641 {conn
->ssl_sizes
.cbHeader
, SECBUFFER_STREAM_HEADER
, conn
->ssl_buf
},
642 {size
, SECBUFFER_DATA
, conn
->ssl_buf
+conn
->ssl_sizes
.cbHeader
},
643 {conn
->ssl_sizes
.cbTrailer
, SECBUFFER_STREAM_TRAILER
, conn
->ssl_buf
+conn
->ssl_sizes
.cbHeader
+size
},
644 {0, SECBUFFER_EMPTY
, NULL
}
646 SecBufferDesc buf_desc
= {SECBUFFER_VERSION
, ARRAY_SIZE(bufs
), bufs
};
649 memcpy(bufs
[1].pvBuffer
, msg
, size
);
650 res
= EncryptMessage(&conn
->ssl_ctx
, 0, &buf_desc
, 0);
651 if(res
!= SEC_E_OK
) {
652 WARN("EncryptMessage failed\n");
656 if(sock_send(conn
->socket
, conn
->ssl_buf
, bufs
[0].cbBuffer
+bufs
[1].cbBuffer
+bufs
[2].cbBuffer
, 0) < 1) {
657 WARN("send failed\n");
664 /******************************************************************************
666 * Basically calls 'send()' unless we should use SSL
667 * number of chars send is put in *sent
669 DWORD
NETCON_send(netconn_t
*connection
, const void *msg
, size_t len
, int flags
,
672 /* send is always blocking. */
673 set_socket_blocking(connection
, TRUE
);
675 if(!connection
->secure
)
677 *sent
= sock_send(connection
->socket
, msg
, len
, flags
);
678 return *sent
== -1 ? WSAGetLastError() : ERROR_SUCCESS
;
682 const BYTE
*ptr
= msg
;
688 chunk_size
= min(len
, connection
->ssl_sizes
.cbMaximumMessage
);
689 if(!send_ssl_chunk(connection
, ptr
, chunk_size
))
690 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
697 return ERROR_SUCCESS
;
701 static BOOL
read_ssl_chunk(netconn_t
*conn
, void *buf
, SIZE_T buf_size
, BOOL blocking
, SIZE_T
*ret_size
, BOOL
*eof
)
703 const SIZE_T ssl_buf_size
= conn
->ssl_sizes
.cbHeader
+conn
->ssl_sizes
.cbMaximumMessage
+conn
->ssl_sizes
.cbTrailer
;
705 SecBufferDesc buf_desc
= {SECBUFFER_VERSION
, ARRAY_SIZE(bufs
), bufs
};
706 SSIZE_T size
, buf_len
= 0;
710 assert(conn
->extra_len
< ssl_buf_size
);
712 if(conn
->extra_len
) {
713 memcpy(conn
->ssl_buf
, conn
->extra_buf
, conn
->extra_len
);
714 buf_len
= conn
->extra_len
;
716 heap_free(conn
->extra_buf
);
717 conn
->extra_buf
= NULL
;
720 set_socket_blocking(conn
, blocking
&& !buf_len
);
721 size
= sock_recv(conn
->socket
, conn
->ssl_buf
+buf_len
, ssl_buf_size
-buf_len
, 0);
724 if(WSAGetLastError() == WSAEWOULDBLOCK
) {
725 TRACE("would block\n");
726 return WSAEWOULDBLOCK
;
728 WARN("recv failed\n");
729 return ERROR_INTERNET_CONNECTION_ABORTED
;
739 return ERROR_SUCCESS
;
745 memset(bufs
, 0, sizeof(bufs
));
746 bufs
[0].BufferType
= SECBUFFER_DATA
;
747 bufs
[0].cbBuffer
= buf_len
;
748 bufs
[0].pvBuffer
= conn
->ssl_buf
;
750 res
= DecryptMessage(&conn
->ssl_ctx
, &buf_desc
, 0, NULL
);
754 case SEC_I_CONTEXT_EXPIRED
:
755 TRACE("context expired\n");
757 return ERROR_SUCCESS
;
758 case SEC_E_INCOMPLETE_MESSAGE
:
759 assert(buf_len
< ssl_buf_size
);
761 set_socket_blocking(conn
, blocking
);
762 size
= sock_recv(conn
->socket
, conn
->ssl_buf
+buf_len
, ssl_buf_size
-buf_len
, 0);
764 if(size
< 0 && WSAGetLastError() == WSAEWOULDBLOCK
) {
765 TRACE("would block\n");
767 /* FIXME: Optimize extra_buf usage. */
768 conn
->extra_buf
= heap_alloc(buf_len
);
770 return ERROR_NOT_ENOUGH_MEMORY
;
772 conn
->extra_len
= buf_len
;
773 memcpy(conn
->extra_buf
, conn
->ssl_buf
, conn
->extra_len
);
774 return WSAEWOULDBLOCK
;
777 return ERROR_INTERNET_CONNECTION_ABORTED
;
783 WARN("failed: %08x\n", res
);
784 return ERROR_INTERNET_CONNECTION_ABORTED
;
786 } while(res
!= SEC_E_OK
);
788 for(i
= 0; i
< ARRAY_SIZE(bufs
); i
++) {
789 if(bufs
[i
].BufferType
== SECBUFFER_DATA
) {
790 size
= min(buf_size
, bufs
[i
].cbBuffer
);
791 memcpy(buf
, bufs
[i
].pvBuffer
, size
);
792 if(size
< bufs
[i
].cbBuffer
) {
793 assert(!conn
->peek_len
);
794 conn
->peek_msg_mem
= conn
->peek_msg
= heap_alloc(bufs
[i
].cbBuffer
- size
);
796 return ERROR_NOT_ENOUGH_MEMORY
;
797 conn
->peek_len
= bufs
[i
].cbBuffer
-size
;
798 memcpy(conn
->peek_msg
, (char*)bufs
[i
].pvBuffer
+size
, conn
->peek_len
);
805 for(i
= 0; i
< ARRAY_SIZE(bufs
); i
++) {
806 if(bufs
[i
].BufferType
== SECBUFFER_EXTRA
) {
807 conn
->extra_buf
= heap_alloc(bufs
[i
].cbBuffer
);
809 return ERROR_NOT_ENOUGH_MEMORY
;
811 conn
->extra_len
= bufs
[i
].cbBuffer
;
812 memcpy(conn
->extra_buf
, bufs
[i
].pvBuffer
, conn
->extra_len
);
816 return ERROR_SUCCESS
;
819 /******************************************************************************
821 * Basically calls 'recv()' unless we should use SSL
822 * number of chars received is put in *recvd
824 DWORD
NETCON_recv(netconn_t
*connection
, void *buf
, size_t len
, BOOL blocking
, int *recvd
)
828 return ERROR_SUCCESS
;
830 if (!connection
->secure
)
832 set_socket_blocking(connection
, blocking
);
833 *recvd
= sock_recv(connection
->socket
, buf
, len
, 0);
834 return *recvd
== -1 ? WSAGetLastError() : ERROR_SUCCESS
;
842 if(connection
->peek_msg
) {
843 size
= min(len
, connection
->peek_len
);
844 memcpy(buf
, connection
->peek_msg
, size
);
845 connection
->peek_len
-= size
;
846 connection
->peek_msg
+= size
;
848 if(!connection
->peek_len
) {
849 heap_free(connection
->peek_msg_mem
);
850 connection
->peek_msg_mem
= connection
->peek_msg
= NULL
;
854 return ERROR_SUCCESS
;
858 res
= read_ssl_chunk(connection
, (BYTE
*)buf
, len
, blocking
, &size
, &eof
);
859 if(res
!= ERROR_SUCCESS
) {
860 if(res
== WSAEWOULDBLOCK
) {
864 WARN("read_ssl_chunk failed\n");
868 }while(!size
&& !eof
);
870 TRACE("received %ld bytes\n", size
);
876 BOOL
NETCON_is_alive(netconn_t
*netconn
)
881 set_socket_blocking(netconn
, FALSE
);
882 len
= sock_recv(netconn
->socket
, &b
, 1, MSG_PEEK
);
884 return len
== 1 || (len
== -1 && WSAGetLastError() == WSAEWOULDBLOCK
);
887 LPCVOID
NETCON_GetCert(netconn_t
*connection
)
889 const CERT_CONTEXT
*ret
;
892 res
= QueryContextAttributesW(&connection
->ssl_ctx
, SECPKG_ATTR_REMOTE_CERT_CONTEXT
, (void*)&ret
);
893 return res
== SEC_E_OK
? ret
: NULL
;
896 int NETCON_GetCipherStrength(netconn_t
*connection
)
898 SecPkgContext_ConnectionInfo conn_info
;
901 if (!connection
->secure
)
904 res
= QueryContextAttributesW(&connection
->ssl_ctx
, SECPKG_ATTR_CONNECTION_INFO
, (void*)&conn_info
);
906 WARN("QueryContextAttributesW failed: %08x\n", res
);
907 return res
== SEC_E_OK
? conn_info
.dwCipherStrength
: 0;
910 DWORD
NETCON_set_timeout(netconn_t
*connection
, BOOL send
, DWORD value
)
914 result
= setsockopt(connection
->socket
, SOL_SOCKET
,
915 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, (void*)&value
,
919 WARN("setsockopt failed\n");
920 return WSAGetLastError();
922 return ERROR_SUCCESS
;