2 * Wininet - networking layer. Uses unix sockets.
4 * Copyright 2002 TransGaming Technologies Inc.
5 * Copyright 2013 Jacek Caban for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <sys/types.h>
30 #ifdef HAVE_SYS_FILIO_H
31 # include <sys/filio.h>
33 #ifdef HAVE_NETINET_TCP_H
34 # include <netinet/tcp.h>
39 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
42 #define WINE_MSG_DONTWAIT MSG_DONTWAIT
44 #define WINE_MSG_DONTWAIT 0
48 * This should use winsock - To use winsock the functions will have to change a bit
49 * as they are designed for unix sockets.
52 static DWORD
netconn_verify_cert(netconn_t
*conn
, PCCERT_CONTEXT cert
, HCERTSTORE store
)
55 CERT_CHAIN_PARA chainPara
= { sizeof(chainPara
), { 0 } };
56 PCCERT_CHAIN_CONTEXT chain
;
57 char oid_server_auth
[] = szOID_PKIX_KP_SERVER_AUTH
;
58 char *server_auth
[] = { oid_server_auth
};
59 DWORD err
= ERROR_SUCCESS
, errors
;
61 static const DWORD supportedErrors
=
62 CERT_TRUST_IS_NOT_TIME_VALID
|
63 CERT_TRUST_IS_UNTRUSTED_ROOT
|
64 CERT_TRUST_IS_PARTIAL_CHAIN
|
65 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
67 TRACE("verifying %s\n", debugstr_w(conn
->server
->name
));
69 chainPara
.RequestedUsage
.Usage
.cUsageIdentifier
= 1;
70 chainPara
.RequestedUsage
.Usage
.rgpszUsageIdentifier
= server_auth
;
71 if (!(ret
= CertGetCertificateChain(NULL
, cert
, NULL
, store
, &chainPara
, 0, NULL
, &chain
))) {
73 return GetLastError();
76 errors
= chain
->TrustStatus
.dwErrorStatus
;
79 /* This seems strange, but that's what tests show */
80 if(errors
& CERT_TRUST_IS_PARTIAL_CHAIN
) {
81 WARN("ERROR_INTERNET_SEC_CERT_REV_FAILED\n");
82 err
= ERROR_INTERNET_SEC_CERT_REV_FAILED
;
84 conn
->security_flags
|= _SECURITY_FLAG_CERT_REV_FAILED
;
85 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_REVOCATION
))
89 if (chain
->TrustStatus
.dwErrorStatus
& ~supportedErrors
) {
90 WARN("error status %x\n", chain
->TrustStatus
.dwErrorStatus
& ~supportedErrors
);
91 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_INVALID_CERT
;
92 errors
&= supportedErrors
;
93 if(!conn
->mask_errors
)
95 WARN("unknown error flags\n");
98 if(errors
& CERT_TRUST_IS_NOT_TIME_VALID
) {
99 WARN("CERT_TRUST_IS_NOT_TIME_VALID\n");
100 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
)) {
101 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_CERT_DATE_INVALID
;
102 if(!conn
->mask_errors
)
104 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_DATE
;
106 errors
&= ~CERT_TRUST_IS_NOT_TIME_VALID
;
109 if(errors
& CERT_TRUST_IS_UNTRUSTED_ROOT
) {
110 WARN("CERT_TRUST_IS_UNTRUSTED_ROOT\n");
111 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
)) {
112 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_INVALID_CA
;
113 if(!conn
->mask_errors
)
115 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_CA
;
117 errors
&= ~CERT_TRUST_IS_UNTRUSTED_ROOT
;
120 if(errors
& CERT_TRUST_IS_PARTIAL_CHAIN
) {
121 WARN("CERT_TRUST_IS_PARTIAL_CHAIN\n");
122 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
)) {
123 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_INVALID_CA
;
124 if(!conn
->mask_errors
)
126 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_CA
;
128 errors
&= ~CERT_TRUST_IS_PARTIAL_CHAIN
;
131 if(errors
& CERT_TRUST_IS_NOT_VALID_FOR_USAGE
) {
132 WARN("CERT_TRUST_IS_NOT_VALID_FOR_USAGE\n");
133 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_WRONG_USAGE
)) {
134 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_INVALID_CERT
;
135 if(!conn
->mask_errors
)
137 WARN("CERT_TRUST_IS_NOT_VALID_FOR_USAGE, unknown error flags\n");
139 errors
&= ~CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
142 if(err
== ERROR_INTERNET_SEC_CERT_REV_FAILED
) {
143 assert(conn
->security_flags
& SECURITY_FLAG_IGNORE_REVOCATION
);
148 if(!err
|| conn
->mask_errors
) {
149 CERT_CHAIN_POLICY_PARA policyPara
;
150 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara
;
151 CERT_CHAIN_POLICY_STATUS policyStatus
;
152 CERT_CHAIN_CONTEXT chainCopy
;
154 /* Clear chain->TrustStatus.dwErrorStatus so
155 * CertVerifyCertificateChainPolicy will verify additional checks
156 * rather than stopping with an existing, ignored error.
158 memcpy(&chainCopy
, chain
, sizeof(chainCopy
));
159 chainCopy
.TrustStatus
.dwErrorStatus
= 0;
160 sslExtraPolicyPara
.u
.cbSize
= sizeof(sslExtraPolicyPara
);
161 sslExtraPolicyPara
.dwAuthType
= AUTHTYPE_SERVER
;
162 sslExtraPolicyPara
.pwszServerName
= conn
->server
->name
;
163 sslExtraPolicyPara
.fdwChecks
= conn
->security_flags
;
164 policyPara
.cbSize
= sizeof(policyPara
);
165 policyPara
.dwFlags
= 0;
166 policyPara
.pvExtraPolicyPara
= &sslExtraPolicyPara
;
167 ret
= CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL
,
168 &chainCopy
, &policyPara
, &policyStatus
);
169 /* Any error in the policy status indicates that the
170 * policy couldn't be verified.
173 if(policyStatus
.dwError
== CERT_E_CN_NO_MATCH
) {
174 WARN("CERT_E_CN_NO_MATCH\n");
175 if(conn
->mask_errors
)
176 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_CN
;
177 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_CERT_CN_INVALID
;
178 }else if(policyStatus
.dwError
) {
179 WARN("policyStatus.dwError %x\n", policyStatus
.dwError
);
180 if(conn
->mask_errors
)
181 WARN("unknown error flags for policy status %x\n", policyStatus
.dwError
);
182 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_INVALID_CERT
;
185 err
= GetLastError();
190 WARN("failed %u\n", err
);
191 CertFreeCertificateChain(chain
);
192 if(conn
->server
->cert_chain
) {
193 CertFreeCertificateChain(conn
->server
->cert_chain
);
194 conn
->server
->cert_chain
= NULL
;
196 if(conn
->mask_errors
)
197 conn
->server
->security_flags
|= conn
->security_flags
& _SECURITY_ERROR_FLAGS_MASK
;
201 /* FIXME: Reuse cached chain */
202 if(conn
->server
->cert_chain
)
203 CertFreeCertificateChain(chain
);
205 conn
->server
->cert_chain
= chain
;
206 return ERROR_SUCCESS
;
209 static SecHandle cred_handle
, compat_cred_handle
;
210 static BOOL cred_handle_initialized
, have_compat_cred_handle
;
212 static CRITICAL_SECTION init_sechandle_cs
;
213 static CRITICAL_SECTION_DEBUG init_sechandle_cs_debug
= {
214 0, 0, &init_sechandle_cs
,
215 { &init_sechandle_cs_debug
.ProcessLocksList
,
216 &init_sechandle_cs_debug
.ProcessLocksList
},
217 0, 0, { (DWORD_PTR
)(__FILE__
": init_sechandle_cs") }
219 static CRITICAL_SECTION init_sechandle_cs
= { &init_sechandle_cs_debug
, -1, 0, 0, 0, 0 };
221 static BOOL
ensure_cred_handle(void)
223 SECURITY_STATUS res
= SEC_E_OK
;
225 EnterCriticalSection(&init_sechandle_cs
);
227 if(!cred_handle_initialized
) {
228 SCHANNEL_CRED cred
= {SCHANNEL_CRED_VERSION
};
229 SecPkgCred_SupportedProtocols prots
;
231 res
= AcquireCredentialsHandleW(NULL
, (WCHAR
*)UNISP_NAME_W
, SECPKG_CRED_OUTBOUND
, NULL
, &cred
,
232 NULL
, NULL
, &cred_handle
, NULL
);
233 if(res
== SEC_E_OK
) {
234 res
= QueryCredentialsAttributesA(&cred_handle
, SECPKG_ATTR_SUPPORTED_PROTOCOLS
, &prots
);
235 if(res
!= SEC_E_OK
|| (prots
.grbitProtocol
& SP_PROT_TLS1_1PLUS_CLIENT
)) {
236 cred
.grbitEnabledProtocols
= prots
.grbitProtocol
& ~SP_PROT_TLS1_1PLUS_CLIENT
;
237 res
= AcquireCredentialsHandleW(NULL
, (WCHAR
*)UNISP_NAME_W
, SECPKG_CRED_OUTBOUND
, NULL
, &cred
,
238 NULL
, NULL
, &compat_cred_handle
, NULL
);
239 have_compat_cred_handle
= res
== SEC_E_OK
;
243 cred_handle_initialized
= res
== SEC_E_OK
;
246 LeaveCriticalSection(&init_sechandle_cs
);
248 if(res
!= SEC_E_OK
) {
249 WARN("Failed: %08x\n", res
);
256 static DWORD
create_netconn_socket(server_t
*server
, netconn_t
*netconn
, DWORD timeout
)
261 assert(server
->addr_len
);
262 result
= netconn
->socket
= socket(server
->addr
.ss_family
, SOCK_STREAM
, 0);
265 ioctlsocket(netconn
->socket
, FIONBIO
, &flag
);
266 result
= connect(netconn
->socket
, (struct sockaddr
*)&server
->addr
, server
->addr_len
);
269 if (sock_get_error(errno
) == WSAEINPROGRESS
) {
270 // ReactOS: use select instead of poll
276 FD_SET(netconn
->socket
, &outfd
);
277 tv
.tv_sec
= timeout
/ 1000;
278 tv
.tv_usec
= (timeout
% 1000) * 1000;
279 res
= select(0, NULL
, &outfd
, NULL
, &tv
);
282 closesocket(netconn
->socket
);
283 return ERROR_INTERNET_CANNOT_CONNECT
;
288 socklen_t len
= sizeof(err
);
289 if (!getsockopt(netconn
->socket
, SOL_SOCKET
, SO_ERROR
, (void *)&err
, &len
) && !err
)
296 closesocket(netconn
->socket
);
297 netconn
->socket
= -1;
301 ioctlsocket(netconn
->socket
, FIONBIO
, &flag
);
305 return ERROR_INTERNET_CANNOT_CONNECT
;
309 result
= setsockopt(netconn
->socket
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&flag
, sizeof(flag
));
311 WARN("setsockopt(TCP_NODELAY) failed\n");
314 return ERROR_SUCCESS
;
317 DWORD
create_netconn(BOOL useSSL
, server_t
*server
, DWORD security_flags
, BOOL mask_errors
, DWORD timeout
, netconn_t
**ret
)
322 netconn
= heap_alloc_zero(sizeof(*netconn
));
324 return ERROR_OUTOFMEMORY
;
326 netconn
->socket
= -1;
327 netconn
->security_flags
= security_flags
| server
->security_flags
;
328 netconn
->mask_errors
= mask_errors
;
329 list_init(&netconn
->pool_entry
);
330 SecInvalidateHandle(&netconn
->ssl_ctx
);
332 result
= create_netconn_socket(server
, netconn
, timeout
);
333 if (result
!= ERROR_SUCCESS
) {
338 server_addref(server
);
339 netconn
->server
= server
;
344 BOOL
is_valid_netconn(netconn_t
*netconn
)
346 return netconn
&& netconn
->socket
!= -1;
349 void close_netconn(netconn_t
*netconn
)
351 closesocket(netconn
->socket
);
352 netconn
->socket
= -1;
355 void free_netconn(netconn_t
*netconn
)
357 server_release(netconn
->server
);
359 if (netconn
->secure
) {
360 heap_free(netconn
->peek_msg_mem
);
361 netconn
->peek_msg_mem
= NULL
;
362 netconn
->peek_msg
= NULL
;
363 netconn
->peek_len
= 0;
364 heap_free(netconn
->ssl_buf
);
365 netconn
->ssl_buf
= NULL
;
366 heap_free(netconn
->extra_buf
);
367 netconn
->extra_buf
= NULL
;
368 netconn
->extra_len
= 0;
369 if (SecIsValidHandle(&netconn
->ssl_ctx
))
370 DeleteSecurityContext(&netconn
->ssl_ctx
);
376 void NETCON_unload(void)
378 if(cred_handle_initialized
)
379 FreeCredentialsHandle(&cred_handle
);
380 if(have_compat_cred_handle
)
381 FreeCredentialsHandle(&compat_cred_handle
);
382 DeleteCriticalSection(&init_sechandle_cs
);
386 /* translate a unix error code into a winsock one */
387 int sock_get_error( int err
)
389 #if !defined(__MINGW32__) && !defined (_MSC_VER)
392 case EINTR
: return WSAEINTR
;
393 case EBADF
: return WSAEBADF
;
395 case EACCES
: return WSAEACCES
;
396 case EFAULT
: return WSAEFAULT
;
397 case EINVAL
: return WSAEINVAL
;
398 case EMFILE
: return WSAEMFILE
;
399 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
400 case EINPROGRESS
: return WSAEINPROGRESS
;
401 case EALREADY
: return WSAEALREADY
;
402 case ENOTSOCK
: return WSAENOTSOCK
;
403 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
404 case EMSGSIZE
: return WSAEMSGSIZE
;
405 case EPROTOTYPE
: return WSAEPROTOTYPE
;
406 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
407 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
408 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
409 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
410 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
411 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
412 case EADDRINUSE
: return WSAEADDRINUSE
;
413 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
414 case ENETDOWN
: return WSAENETDOWN
;
415 case ENETUNREACH
: return WSAENETUNREACH
;
416 case ENETRESET
: return WSAENETRESET
;
417 case ECONNABORTED
: return WSAECONNABORTED
;
419 case ECONNRESET
: return WSAECONNRESET
;
420 case ENOBUFS
: return WSAENOBUFS
;
421 case EISCONN
: return WSAEISCONN
;
422 case ENOTCONN
: return WSAENOTCONN
;
423 case ESHUTDOWN
: return WSAESHUTDOWN
;
424 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
425 case ETIMEDOUT
: return WSAETIMEDOUT
;
426 case ECONNREFUSED
: return WSAECONNREFUSED
;
427 case ELOOP
: return WSAELOOP
;
428 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
429 case EHOSTDOWN
: return WSAEHOSTDOWN
;
430 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
431 case ENOTEMPTY
: return WSAENOTEMPTY
;
433 case EPROCLIM
: return WSAEPROCLIM
;
436 case EUSERS
: return WSAEUSERS
;
439 case EDQUOT
: return WSAEDQUOT
;
442 case ESTALE
: return WSAESTALE
;
445 case EREMOTE
: return WSAEREMOTE
;
447 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
454 static void set_socket_blocking(int socket
, blocking_mode_t mode
)
456 #if defined(__MINGW32__) || defined (_MSC_VER)
457 ULONG arg
= mode
== BLOCKING_DISALLOW
;
458 ioctlsocket(socket
, FIONBIO
, &arg
);
462 static DWORD
netcon_secure_connect_setup(netconn_t
*connection
, BOOL compat_mode
)
464 SecBuffer out_buf
= {0, SECBUFFER_TOKEN
, NULL
}, in_bufs
[2] = {{0, SECBUFFER_TOKEN
}, {0, SECBUFFER_EMPTY
}};
465 SecBufferDesc out_desc
= {SECBUFFER_VERSION
, 1, &out_buf
}, in_desc
= {SECBUFFER_VERSION
, 2, in_bufs
};
466 SecHandle
*cred
= &cred_handle
;
468 SIZE_T read_buf_size
= 2048;
473 const CERT_CONTEXT
*cert
;
474 SECURITY_STATUS status
;
475 DWORD res
= ERROR_SUCCESS
;
477 const DWORD isc_req_flags
= ISC_REQ_ALLOCATE_MEMORY
|ISC_REQ_USE_SESSION_KEY
|ISC_REQ_CONFIDENTIALITY
478 |ISC_REQ_SEQUENCE_DETECT
|ISC_REQ_REPLAY_DETECT
|ISC_REQ_MANUAL_CRED_VALIDATION
;
480 if(!ensure_cred_handle())
481 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
484 if(!have_compat_cred_handle
)
485 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
486 cred
= &compat_cred_handle
;
489 read_buf
= heap_alloc(read_buf_size
);
491 return ERROR_OUTOFMEMORY
;
493 status
= InitializeSecurityContextW(cred
, NULL
, connection
->server
->name
, isc_req_flags
, 0, 0, NULL
, 0,
494 &ctx
, &out_desc
, &attrs
, NULL
);
496 assert(status
!= SEC_E_OK
);
498 while(status
== SEC_I_CONTINUE_NEEDED
|| status
== SEC_E_INCOMPLETE_MESSAGE
) {
499 if(out_buf
.cbBuffer
) {
500 assert(status
== SEC_I_CONTINUE_NEEDED
);
502 TRACE("sending %u bytes\n", out_buf
.cbBuffer
);
504 size
= send(connection
->socket
, out_buf
.pvBuffer
, out_buf
.cbBuffer
, 0);
505 if(size
!= out_buf
.cbBuffer
) {
506 ERR("send failed\n");
507 status
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
511 FreeContextBuffer(out_buf
.pvBuffer
);
512 out_buf
.pvBuffer
= NULL
;
513 out_buf
.cbBuffer
= 0;
516 if(status
== SEC_I_CONTINUE_NEEDED
) {
517 assert(in_bufs
[1].cbBuffer
< read_buf_size
);
519 memmove(read_buf
, (BYTE
*)in_bufs
[0].pvBuffer
+in_bufs
[0].cbBuffer
-in_bufs
[1].cbBuffer
, in_bufs
[1].cbBuffer
);
520 in_bufs
[0].cbBuffer
= in_bufs
[1].cbBuffer
;
522 in_bufs
[1].BufferType
= SECBUFFER_EMPTY
;
523 in_bufs
[1].cbBuffer
= 0;
524 in_bufs
[1].pvBuffer
= NULL
;
527 assert(in_bufs
[0].BufferType
== SECBUFFER_TOKEN
);
528 assert(in_bufs
[1].BufferType
== SECBUFFER_EMPTY
);
530 if(in_bufs
[0].cbBuffer
+ 1024 > read_buf_size
) {
533 new_read_buf
= heap_realloc(read_buf
, read_buf_size
+ 1024);
535 status
= E_OUTOFMEMORY
;
539 in_bufs
[0].pvBuffer
= read_buf
= new_read_buf
;
540 read_buf_size
+= 1024;
543 size
= recv(connection
->socket
, read_buf
+in_bufs
[0].cbBuffer
, read_buf_size
-in_bufs
[0].cbBuffer
, 0);
545 WARN("recv error\n");
546 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
550 TRACE("recv %lu bytes\n", size
);
552 in_bufs
[0].cbBuffer
+= size
;
553 in_bufs
[0].pvBuffer
= read_buf
;
554 status
= InitializeSecurityContextW(cred
, &ctx
, connection
->server
->name
, isc_req_flags
, 0, 0, &in_desc
,
555 0, NULL
, &out_desc
, &attrs
, NULL
);
556 TRACE("InitializeSecurityContext ret %08x\n", status
);
558 if(status
== SEC_E_OK
) {
559 if(SecIsValidHandle(&connection
->ssl_ctx
))
560 DeleteSecurityContext(&connection
->ssl_ctx
);
561 connection
->ssl_ctx
= ctx
;
563 if(in_bufs
[1].BufferType
== SECBUFFER_EXTRA
)
564 FIXME("SECBUFFER_EXTRA not supported\n");
566 status
= QueryContextAttributesW(&ctx
, SECPKG_ATTR_STREAM_SIZES
, &connection
->ssl_sizes
);
567 if(status
!= SEC_E_OK
) {
568 WARN("Could not get sizes\n");
572 status
= QueryContextAttributesW(&ctx
, SECPKG_ATTR_REMOTE_CERT_CONTEXT
, (void*)&cert
);
573 if(status
== SEC_E_OK
) {
574 res
= netconn_verify_cert(connection
, cert
, cert
->hCertStore
);
575 CertFreeCertificateContext(cert
);
576 if(res
!= ERROR_SUCCESS
) {
577 WARN("cert verify failed: %u\n", res
);
581 WARN("Could not get cert\n");
585 connection
->ssl_buf
= heap_alloc(connection
->ssl_sizes
.cbHeader
+ connection
->ssl_sizes
.cbMaximumMessage
586 + connection
->ssl_sizes
.cbTrailer
);
587 if(!connection
->ssl_buf
) {
588 res
= GetLastError();
594 if(status
!= SEC_E_OK
|| res
!= ERROR_SUCCESS
) {
595 WARN("Failed to establish SSL connection: %08x (%u)\n", status
, res
);
596 heap_free(connection
->ssl_buf
);
597 connection
->ssl_buf
= NULL
;
598 return res
? res
: ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
601 TRACE("established SSL connection\n");
602 connection
->secure
= TRUE
;
603 connection
->security_flags
|= SECURITY_FLAG_SECURE
;
605 bits
= NETCON_GetCipherStrength(connection
);
607 connection
->security_flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
609 connection
->security_flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
611 connection
->security_flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
613 if(connection
->mask_errors
)
614 connection
->server
->security_flags
= connection
->security_flags
;
615 return ERROR_SUCCESS
;
618 /******************************************************************************
619 * NETCON_secure_connect
620 * Initiates a secure connection over an existing plaintext connection.
622 DWORD
NETCON_secure_connect(netconn_t
*connection
, server_t
*server
)
626 /* can't connect if we are already connected */
627 if(connection
->secure
) {
628 ERR("already connected\n");
629 return ERROR_INTERNET_CANNOT_CONNECT
;
632 if(server
!= connection
->server
) {
633 server_release(connection
->server
);
634 server_addref(server
);
635 connection
->server
= server
;
638 /* connect with given TLS options */
639 res
= netcon_secure_connect_setup(connection
, FALSE
);
640 if (res
== ERROR_SUCCESS
)
643 /* FIXME: when got version alert and FIN from server */
644 /* fallback to connect without TLSv1.1/TLSv1.2 */
645 if (res
== ERROR_INTERNET_SECURITY_CHANNEL_ERROR
&& have_compat_cred_handle
)
647 closesocket(connection
->socket
);
648 res
= create_netconn_socket(connection
->server
, connection
, 500);
649 if (res
!= ERROR_SUCCESS
)
651 res
= netcon_secure_connect_setup(connection
, TRUE
);
656 static BOOL
send_ssl_chunk(netconn_t
*conn
, const void *msg
, size_t size
)
658 SecBuffer bufs
[4] = {
659 {conn
->ssl_sizes
.cbHeader
, SECBUFFER_STREAM_HEADER
, conn
->ssl_buf
},
660 {size
, SECBUFFER_DATA
, conn
->ssl_buf
+conn
->ssl_sizes
.cbHeader
},
661 {conn
->ssl_sizes
.cbTrailer
, SECBUFFER_STREAM_TRAILER
, conn
->ssl_buf
+conn
->ssl_sizes
.cbHeader
+size
},
662 {0, SECBUFFER_EMPTY
, NULL
}
664 SecBufferDesc buf_desc
= {SECBUFFER_VERSION
, sizeof(bufs
)/sizeof(*bufs
), bufs
};
667 memcpy(bufs
[1].pvBuffer
, msg
, size
);
668 res
= EncryptMessage(&conn
->ssl_ctx
, 0, &buf_desc
, 0);
669 if(res
!= SEC_E_OK
) {
670 WARN("EncryptMessage failed\n");
674 if(send(conn
->socket
, conn
->ssl_buf
, bufs
[0].cbBuffer
+bufs
[1].cbBuffer
+bufs
[2].cbBuffer
, 0) < 1) {
675 WARN("send failed\n");
682 /******************************************************************************
684 * Basically calls 'send()' unless we should use SSL
685 * number of chars send is put in *sent
687 DWORD
NETCON_send(netconn_t
*connection
, const void *msg
, size_t len
, int flags
,
690 if(!connection
->secure
)
692 *sent
= send(connection
->socket
, msg
, len
, flags
);
694 return sock_get_error(errno
);
695 return ERROR_SUCCESS
;
699 const BYTE
*ptr
= msg
;
705 chunk_size
= min(len
, connection
->ssl_sizes
.cbMaximumMessage
);
706 if(!send_ssl_chunk(connection
, ptr
, chunk_size
))
707 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
714 return ERROR_SUCCESS
;
718 static BOOL
read_ssl_chunk(netconn_t
*conn
, void *buf
, SIZE_T buf_size
, blocking_mode_t mode
, SIZE_T
*ret_size
, BOOL
*eof
)
720 const SIZE_T ssl_buf_size
= conn
->ssl_sizes
.cbHeader
+conn
->ssl_sizes
.cbMaximumMessage
+conn
->ssl_sizes
.cbTrailer
;
722 SecBufferDesc buf_desc
= {SECBUFFER_VERSION
, sizeof(bufs
)/sizeof(*bufs
), bufs
};
723 SSIZE_T size
, buf_len
= 0;
724 blocking_mode_t tmp_mode
;
728 assert(conn
->extra_len
< ssl_buf_size
);
730 /* BLOCKING_WAITALL is handled by caller */
731 if(mode
== BLOCKING_WAITALL
)
732 mode
= BLOCKING_ALLOW
;
734 if(conn
->extra_len
) {
735 memcpy(conn
->ssl_buf
, conn
->extra_buf
, conn
->extra_len
);
736 buf_len
= conn
->extra_len
;
738 heap_free(conn
->extra_buf
);
739 conn
->extra_buf
= NULL
;
742 tmp_mode
= buf_len
? BLOCKING_DISALLOW
: mode
;
743 set_socket_blocking(conn
->socket
, tmp_mode
);
744 size
= recv(conn
->socket
, conn
->ssl_buf
+buf_len
, ssl_buf_size
-buf_len
, tmp_mode
== BLOCKING_ALLOW
? 0 : WINE_MSG_DONTWAIT
);
747 if(errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
748 TRACE("would block\n");
749 return WSAEWOULDBLOCK
;
751 WARN("recv failed\n");
752 return ERROR_INTERNET_CONNECTION_ABORTED
;
762 return ERROR_SUCCESS
;
768 memset(bufs
, 0, sizeof(bufs
));
769 bufs
[0].BufferType
= SECBUFFER_DATA
;
770 bufs
[0].cbBuffer
= buf_len
;
771 bufs
[0].pvBuffer
= conn
->ssl_buf
;
773 res
= DecryptMessage(&conn
->ssl_ctx
, &buf_desc
, 0, NULL
);
777 case SEC_I_CONTEXT_EXPIRED
:
778 TRACE("context expired\n");
780 return ERROR_SUCCESS
;
781 case SEC_E_INCOMPLETE_MESSAGE
:
782 assert(buf_len
< ssl_buf_size
);
784 set_socket_blocking(conn
->socket
, mode
);
785 size
= recv(conn
->socket
, conn
->ssl_buf
+buf_len
, ssl_buf_size
-buf_len
, mode
== BLOCKING_ALLOW
? 0 : WINE_MSG_DONTWAIT
);
787 if(size
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) {
788 TRACE("would block\n");
790 /* FIXME: Optimize extra_buf usage. */
791 conn
->extra_buf
= heap_alloc(buf_len
);
793 return ERROR_NOT_ENOUGH_MEMORY
;
795 conn
->extra_len
= buf_len
;
796 memcpy(conn
->extra_buf
, conn
->ssl_buf
, conn
->extra_len
);
797 return WSAEWOULDBLOCK
;
800 return ERROR_INTERNET_CONNECTION_ABORTED
;
806 WARN("failed: %08x\n", res
);
807 return ERROR_INTERNET_CONNECTION_ABORTED
;
809 } while(res
!= SEC_E_OK
);
811 for(i
=0; i
< sizeof(bufs
)/sizeof(*bufs
); i
++) {
812 if(bufs
[i
].BufferType
== SECBUFFER_DATA
) {
813 size
= min(buf_size
, bufs
[i
].cbBuffer
);
814 memcpy(buf
, bufs
[i
].pvBuffer
, size
);
815 if(size
< bufs
[i
].cbBuffer
) {
816 assert(!conn
->peek_len
);
817 conn
->peek_msg_mem
= conn
->peek_msg
= heap_alloc(bufs
[i
].cbBuffer
- size
);
819 return ERROR_NOT_ENOUGH_MEMORY
;
820 conn
->peek_len
= bufs
[i
].cbBuffer
-size
;
821 memcpy(conn
->peek_msg
, (char*)bufs
[i
].pvBuffer
+size
, conn
->peek_len
);
828 for(i
=0; i
< sizeof(bufs
)/sizeof(*bufs
); i
++) {
829 if(bufs
[i
].BufferType
== SECBUFFER_EXTRA
) {
830 conn
->extra_buf
= heap_alloc(bufs
[i
].cbBuffer
);
832 return ERROR_NOT_ENOUGH_MEMORY
;
834 conn
->extra_len
= bufs
[i
].cbBuffer
;
835 memcpy(conn
->extra_buf
, bufs
[i
].pvBuffer
, conn
->extra_len
);
839 return ERROR_SUCCESS
;
842 /******************************************************************************
844 * Basically calls 'recv()' unless we should use SSL
845 * number of chars received is put in *recvd
847 DWORD
NETCON_recv(netconn_t
*connection
, void *buf
, size_t len
, blocking_mode_t mode
, int *recvd
)
851 return ERROR_SUCCESS
;
853 if (!connection
->secure
)
860 case BLOCKING_DISALLOW
:
861 flags
= WINE_MSG_DONTWAIT
;
863 case BLOCKING_WAITALL
:
868 set_socket_blocking(connection
->socket
, mode
);
869 *recvd
= recv(connection
->socket
, buf
, len
, flags
);
870 return *recvd
== -1 ? sock_get_error(errno
) : ERROR_SUCCESS
;
874 SIZE_T size
= 0, cread
;
878 if(connection
->peek_msg
) {
879 size
= min(len
, connection
->peek_len
);
880 memcpy(buf
, connection
->peek_msg
, size
);
881 connection
->peek_len
-= size
;
882 connection
->peek_msg
+= size
;
884 if(!connection
->peek_len
) {
885 heap_free(connection
->peek_msg_mem
);
886 connection
->peek_msg_mem
= connection
->peek_msg
= NULL
;
888 /* check if we have enough data from the peek buffer */
889 if(mode
!= BLOCKING_WAITALL
|| size
== len
) {
891 return ERROR_SUCCESS
;
894 mode
= BLOCKING_DISALLOW
;
898 res
= read_ssl_chunk(connection
, (BYTE
*)buf
+size
, len
-size
, mode
, &cread
, &eof
);
899 if(res
!= ERROR_SUCCESS
) {
900 if(res
== WSAEWOULDBLOCK
) {
904 WARN("read_ssl_chunk failed\n");
915 }while(!size
|| (mode
== BLOCKING_WAITALL
&& size
< len
));
917 TRACE("received %ld bytes\n", size
);
923 /******************************************************************************
924 * NETCON_query_data_available
925 * Returns the number of bytes of peeked data plus the number of bytes of
926 * queued, but unread data.
928 BOOL
NETCON_query_data_available(netconn_t
*connection
, DWORD
*available
)
932 if(!connection
->secure
)
936 int retval
= ioctlsocket(connection
->socket
, FIONREAD
, &unread
);
939 TRACE("%d bytes of queued, but unread data\n", unread
);
940 *available
+= unread
;
946 *available
= connection
->peek_len
;
951 BOOL
NETCON_is_alive(netconn_t
*netconn
)
957 len
= recv(netconn
->socket
, &b
, 1, MSG_PEEK
|MSG_DONTWAIT
);
958 return len
== 1 || (len
== -1 && errno
== EWOULDBLOCK
);
959 #elif defined(__MINGW32__) || defined(_MSC_VER)
965 if(!ioctlsocket(netconn
->socket
, FIONBIO
, &mode
))
968 len
= recv(netconn
->socket
, &b
, 1, MSG_PEEK
);
971 if(!ioctlsocket(netconn
->socket
, FIONBIO
, &mode
))
974 return len
== 1 || (len
== -1 && errno
== WSAEWOULDBLOCK
);
976 FIXME("not supported on this platform\n");
981 LPCVOID
NETCON_GetCert(netconn_t
*connection
)
983 const CERT_CONTEXT
*ret
;
986 res
= QueryContextAttributesW(&connection
->ssl_ctx
, SECPKG_ATTR_REMOTE_CERT_CONTEXT
, (void*)&ret
);
987 return res
== SEC_E_OK
? ret
: NULL
;
990 int NETCON_GetCipherStrength(netconn_t
*connection
)
992 SecPkgContext_ConnectionInfo conn_info
;
995 if (!connection
->secure
)
998 res
= QueryContextAttributesW(&connection
->ssl_ctx
, SECPKG_ATTR_CONNECTION_INFO
, (void*)&conn_info
);
1000 WARN("QueryContextAttributesW failed: %08x\n", res
);
1001 return res
== SEC_E_OK
? conn_info
.dwCipherStrength
: 0;
1004 DWORD
NETCON_set_timeout(netconn_t
*connection
, BOOL send
, DWORD value
)
1009 /* value is in milliseconds, convert to struct timeval */
1010 if (value
== INFINITE
)
1017 tv
.tv_sec
= value
/ 1000;
1018 tv
.tv_usec
= (value
% 1000) * 1000;
1020 result
= setsockopt(connection
->socket
, SOL_SOCKET
,
1021 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, (void*)&tv
,
1025 WARN("setsockopt failed (%s)\n", strerror(errno
));
1026 return sock_get_error(errno
);
1028 return ERROR_SUCCESS
;