- Sync wininet to Wine HEAD
[reactos.git] / reactos / dll / win32 / wininet / netconnection.c
1 /*
2 * Wininet - networking layer. Uses unix sockets or OpenSSL.
3 *
4 * Copyright 2002 TransGaming Technologies Inc.
5 *
6 * David Hammerton
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <sys/types.h>
27 #ifdef HAVE_POLL_H
28 #include <poll.h>
29 #endif
30 #ifdef HAVE_SYS_POLL_H
31 # include <sys/poll.h>
32 #endif
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
35 #endif
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
39 #endif
40 #ifdef HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
45 #endif
46 #include <time.h>
47 #ifdef HAVE_NETDB_H
48 # include <netdb.h>
49 #endif
50 #ifdef HAVE_NETINET_IN_H
51 # include <netinet/in.h>
52 #endif
53 #ifdef HAVE_OPENSSL_SSL_H
54 # include <openssl/ssl.h>
55 #undef FAR
56 #undef DSA
57 #endif
58 #ifdef HAVE_SYS_SOCKET_H
59 # include <sys/socket.h>
60 #endif
61
62 #include <stdarg.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <stdio.h>
66 #include <errno.h>
67
68 #include "wine/library.h"
69 #include "windef.h"
70 #include "winbase.h"
71 #include "wininet.h"
72 #include "winerror.h"
73 #include "wincrypt.h"
74
75 #include "wine/debug.h"
76 #include "internet.h"
77
78 /* To avoid conflicts with the Unix socket headers. we only need it for
79 * the error codes anyway. */
80 #define USE_WS_PREFIX
81 #include "winsock2.h"
82
83 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
84 #define sock_get_error(x) WSAGetLastError()
85
86 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
87
88 /* FIXME!!!!!!
89 * This should use winsock - To use winsock the functions will have to change a bit
90 * as they are designed for unix sockets.
91 * SSL stuff should use crypt32.dll
92 */
93
94 #ifdef SONAME_LIBSSL
95
96 #include <openssl/err.h>
97
98 static void *OpenSSL_ssl_handle;
99 static void *OpenSSL_crypto_handle;
100
101 static SSL_METHOD *meth;
102 static SSL_CTX *ctx;
103
104 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
105
106 /* OpenSSL functions that we use */
107 MAKE_FUNCPTR(SSL_library_init);
108 MAKE_FUNCPTR(SSL_load_error_strings);
109 MAKE_FUNCPTR(SSLv23_method);
110 MAKE_FUNCPTR(SSL_CTX_new);
111 MAKE_FUNCPTR(SSL_new);
112 MAKE_FUNCPTR(SSL_free);
113 MAKE_FUNCPTR(SSL_set_fd);
114 MAKE_FUNCPTR(SSL_connect);
115 MAKE_FUNCPTR(SSL_shutdown);
116 MAKE_FUNCPTR(SSL_write);
117 MAKE_FUNCPTR(SSL_read);
118 MAKE_FUNCPTR(SSL_get_verify_result);
119 MAKE_FUNCPTR(SSL_get_peer_certificate);
120 MAKE_FUNCPTR(SSL_CTX_get_timeout);
121 MAKE_FUNCPTR(SSL_CTX_set_timeout);
122 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
123 MAKE_FUNCPTR(i2d_X509);
124
125 /* OpenSSL's libcrypto functions that we use */
126 MAKE_FUNCPTR(BIO_new_fp);
127 MAKE_FUNCPTR(ERR_get_error);
128 MAKE_FUNCPTR(ERR_error_string);
129 #undef MAKE_FUNCPTR
130
131 #endif
132
133 BOOL NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
134 {
135 connection->useSSL = FALSE;
136 connection->socketFD = -1;
137 if (useSSL)
138 {
139 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
140 TRACE("using SSL connection\n");
141 if (OpenSSL_ssl_handle) /* already initialized everything */
142 return TRUE;
143 OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
144 if (!OpenSSL_ssl_handle)
145 {
146 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
147 SONAME_LIBSSL);
148 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
149 return FALSE;
150 }
151 OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
152 if (!OpenSSL_crypto_handle)
153 {
154 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
155 SONAME_LIBCRYPTO);
156 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
157 return FALSE;
158 }
159
160 /* mmm nice ugly macroness */
161 #define DYNSSL(x) \
162 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
163 if (!p##x) \
164 { \
165 ERR("failed to load symbol %s\n", #x); \
166 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
167 return FALSE; \
168 }
169
170 DYNSSL(SSL_library_init);
171 DYNSSL(SSL_load_error_strings);
172 DYNSSL(SSLv23_method);
173 DYNSSL(SSL_CTX_new);
174 DYNSSL(SSL_new);
175 DYNSSL(SSL_free);
176 DYNSSL(SSL_set_fd);
177 DYNSSL(SSL_connect);
178 DYNSSL(SSL_shutdown);
179 DYNSSL(SSL_write);
180 DYNSSL(SSL_read);
181 DYNSSL(SSL_get_verify_result);
182 DYNSSL(SSL_get_peer_certificate);
183 DYNSSL(SSL_CTX_get_timeout);
184 DYNSSL(SSL_CTX_set_timeout);
185 DYNSSL(SSL_CTX_set_default_verify_paths);
186 DYNSSL(i2d_X509);
187 #undef DYNSSL
188
189 #define DYNCRYPTO(x) \
190 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
191 if (!p##x) \
192 { \
193 ERR("failed to load symbol %s\n", #x); \
194 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
195 return FALSE; \
196 }
197 DYNCRYPTO(BIO_new_fp);
198 DYNCRYPTO(ERR_get_error);
199 DYNCRYPTO(ERR_error_string);
200 #undef DYNCRYPTO
201
202 pSSL_library_init();
203 pSSL_load_error_strings();
204 pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
205
206 meth = pSSLv23_method();
207 connection->peek_msg = NULL;
208 connection->peek_msg_mem = NULL;
209 #else
210 FIXME("can't use SSL, not compiled in.\n");
211 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
212 return FALSE;
213 #endif
214 }
215 return TRUE;
216 }
217
218 BOOL NETCON_connected(WININET_NETCONNECTION *connection)
219 {
220 if (connection->socketFD == -1)
221 return FALSE;
222 else
223 return TRUE;
224 }
225
226 #if 0
227 /* translate a unix error code into a winsock one */
228 static int sock_get_error( int err )
229 {
230 #if !defined(__MINGW32__) && !defined (_MSC_VER)
231 switch (err)
232 {
233 case EINTR: return WSAEINTR;
234 case EBADF: return WSAEBADF;
235 case EPERM:
236 case EACCES: return WSAEACCES;
237 case EFAULT: return WSAEFAULT;
238 case EINVAL: return WSAEINVAL;
239 case EMFILE: return WSAEMFILE;
240 case EWOULDBLOCK: return WSAEWOULDBLOCK;
241 case EINPROGRESS: return WSAEINPROGRESS;
242 case EALREADY: return WSAEALREADY;
243 case ENOTSOCK: return WSAENOTSOCK;
244 case EDESTADDRREQ: return WSAEDESTADDRREQ;
245 case EMSGSIZE: return WSAEMSGSIZE;
246 case EPROTOTYPE: return WSAEPROTOTYPE;
247 case ENOPROTOOPT: return WSAENOPROTOOPT;
248 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
249 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
250 case EOPNOTSUPP: return WSAEOPNOTSUPP;
251 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
252 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
253 case EADDRINUSE: return WSAEADDRINUSE;
254 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
255 case ENETDOWN: return WSAENETDOWN;
256 case ENETUNREACH: return WSAENETUNREACH;
257 case ENETRESET: return WSAENETRESET;
258 case ECONNABORTED: return WSAECONNABORTED;
259 case EPIPE:
260 case ECONNRESET: return WSAECONNRESET;
261 case ENOBUFS: return WSAENOBUFS;
262 case EISCONN: return WSAEISCONN;
263 case ENOTCONN: return WSAENOTCONN;
264 case ESHUTDOWN: return WSAESHUTDOWN;
265 case ETOOMANYREFS: return WSAETOOMANYREFS;
266 case ETIMEDOUT: return WSAETIMEDOUT;
267 case ECONNREFUSED: return WSAECONNREFUSED;
268 case ELOOP: return WSAELOOP;
269 case ENAMETOOLONG: return WSAENAMETOOLONG;
270 case EHOSTDOWN: return WSAEHOSTDOWN;
271 case EHOSTUNREACH: return WSAEHOSTUNREACH;
272 case ENOTEMPTY: return WSAENOTEMPTY;
273 #ifdef EPROCLIM
274 case EPROCLIM: return WSAEPROCLIM;
275 #endif
276 #ifdef EUSERS
277 case EUSERS: return WSAEUSERS;
278 #endif
279 #ifdef EDQUOT
280 case EDQUOT: return WSAEDQUOT;
281 #endif
282 #ifdef ESTALE
283 case ESTALE: return WSAESTALE;
284 #endif
285 #ifdef EREMOTE
286 case EREMOTE: return WSAEREMOTE;
287 #endif
288 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
289 }
290 #endif
291 return err;
292 }
293 #endif
294
295 /******************************************************************************
296 * NETCON_create
297 * Basically calls 'socket()'
298 */
299 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
300 int type, int protocol)
301 {
302 #ifdef SONAME_LIBSSL
303 if (connection->useSSL)
304 return FALSE;
305 #endif
306
307 connection->socketFD = socket(domain, type, protocol);
308 if (connection->socketFD == -1)
309 {
310 INTERNET_SetLastError(sock_get_error(errno));
311 return FALSE;
312 }
313 return TRUE;
314 }
315
316 /******************************************************************************
317 * NETCON_close
318 * Basically calls 'close()' unless we should use SSL
319 */
320 BOOL NETCON_close(WININET_NETCONNECTION *connection)
321 {
322 int result;
323
324 if (!NETCON_connected(connection)) return FALSE;
325
326 #ifdef SONAME_LIBSSL
327 if (connection->useSSL)
328 {
329 HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
330 connection->peek_msg = NULL;
331 connection->peek_msg_mem = NULL;
332 connection->peek_len = 0;
333
334 pSSL_shutdown(connection->ssl_s);
335 pSSL_free(connection->ssl_s);
336 connection->ssl_s = NULL;
337
338 connection->useSSL = FALSE;
339 }
340 #endif
341
342 result = closesocket(connection->socketFD);
343 connection->socketFD = -1;
344
345 if (result == -1)
346 {
347 INTERNET_SetLastError(sock_get_error(errno));
348 return FALSE;
349 }
350 return TRUE;
351 }
352 #ifdef SONAME_LIBSSL
353 static BOOL check_hostname(X509 *cert, char *hostname)
354 {
355 /* FIXME: implement */
356 return TRUE;
357 }
358 #endif
359 /******************************************************************************
360 * NETCON_secure_connect
361 * Initiates a secure connection over an existing plaintext connection.
362 */
363 BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
364 {
365 #ifdef SONAME_LIBSSL
366 long verify_res;
367 X509 *cert;
368 int len;
369 char *hostname_unix;
370
371 /* can't connect if we are already connected */
372 if (connection->useSSL)
373 {
374 ERR("already connected\n");
375 return FALSE;
376 }
377
378 ctx = pSSL_CTX_new(meth);
379 if (!pSSL_CTX_set_default_verify_paths(ctx))
380 {
381 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
382 pERR_error_string(pERR_get_error(), 0));
383 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
384 return FALSE;
385 }
386 connection->ssl_s = pSSL_new(ctx);
387 if (!connection->ssl_s)
388 {
389 ERR("SSL_new failed: %s\n",
390 pERR_error_string(pERR_get_error(), 0));
391 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
392 goto fail;
393 }
394
395 if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
396 {
397 ERR("SSL_set_fd failed: %s\n",
398 pERR_error_string(pERR_get_error(), 0));
399 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
400 goto fail;
401 }
402
403 if (pSSL_connect(connection->ssl_s) <= 0)
404 {
405 ERR("SSL_connect failed: %s\n",
406 pERR_error_string(pERR_get_error(), 0));
407 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
408 goto fail;
409 }
410 cert = pSSL_get_peer_certificate(connection->ssl_s);
411 if (!cert)
412 {
413 ERR("no certificate for server %s\n", debugstr_w(hostname));
414 /* FIXME: is this the best error? */
415 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
416 goto fail;
417 }
418 verify_res = pSSL_get_verify_result(connection->ssl_s);
419 if (verify_res != X509_V_OK)
420 {
421 ERR("couldn't verify the security of the connection, %ld\n", verify_res);
422 /* FIXME: we should set an error and return, but we only warn at
423 * the moment */
424 }
425
426 len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
427 hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
428 if (!hostname_unix)
429 {
430 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
431 goto fail;
432 }
433 WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
434
435 if (!check_hostname(cert, hostname_unix))
436 {
437 HeapFree(GetProcessHeap(), 0, hostname_unix);
438 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
439 goto fail;
440 }
441
442 HeapFree(GetProcessHeap(), 0, hostname_unix);
443 connection->useSSL = TRUE;
444 return TRUE;
445
446 fail:
447 if (connection->ssl_s)
448 {
449 pSSL_shutdown(connection->ssl_s);
450 pSSL_free(connection->ssl_s);
451 connection->ssl_s = NULL;
452 }
453 #endif
454 return FALSE;
455 }
456
457 /******************************************************************************
458 * NETCON_connect
459 * Connects to the specified address.
460 */
461 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
462 unsigned int addrlen)
463 {
464 int result;
465
466 if (!NETCON_connected(connection)) return FALSE;
467
468 result = connect(connection->socketFD, serv_addr, addrlen);
469 if (result == -1)
470 {
471 WARN("Unable to connect to host (%s)\n", strerror(errno));
472 INTERNET_SetLastError(sock_get_error(errno));
473
474 closesocket(connection->socketFD);
475 connection->socketFD = -1;
476 return FALSE;
477 }
478
479 return TRUE;
480 }
481
482 /******************************************************************************
483 * NETCON_send
484 * Basically calls 'send()' unless we should use SSL
485 * number of chars send is put in *sent
486 */
487 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
488 int *sent /* out */)
489 {
490 if (!NETCON_connected(connection)) return FALSE;
491 if (!connection->useSSL)
492 {
493 *sent = send(connection->socketFD, msg, len, flags);
494 if (*sent == -1)
495 {
496 INTERNET_SetLastError(sock_get_error(errno));
497 return FALSE;
498 }
499 return TRUE;
500 }
501 else
502 {
503 #ifdef SONAME_LIBSSL
504 if (flags)
505 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
506 *sent = pSSL_write(connection->ssl_s, msg, len);
507 if (*sent < 1 && len)
508 return FALSE;
509 return TRUE;
510 #else
511 return FALSE;
512 #endif
513 }
514 }
515
516 /******************************************************************************
517 * NETCON_recv
518 * Basically calls 'recv()' unless we should use SSL
519 * number of chars received is put in *recvd
520 */
521 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
522 int *recvd /* out */)
523 {
524 *recvd = 0;
525 if (!NETCON_connected(connection)) return FALSE;
526 if (!len)
527 return TRUE;
528 if (!connection->useSSL)
529 {
530 *recvd = recv(connection->socketFD, buf, len, flags);
531 if (*recvd == -1)
532 {
533 INTERNET_SetLastError(sock_get_error(errno));
534 return FALSE;
535 }
536 return TRUE;
537 }
538 else
539 {
540 #ifdef SONAME_LIBSSL
541 if (flags & ~(MSG_PEEK|MSG_WAITALL))
542 FIXME("SSL_read does not support the following flag: %08x\n", flags);
543
544 /* this ugly hack is all for MSG_PEEK. eww gross */
545 if (flags & MSG_PEEK && !connection->peek_msg)
546 {
547 connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
548 }
549 else if (flags & MSG_PEEK && connection->peek_msg)
550 {
551 if (len < connection->peek_len)
552 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
553 *recvd = min(len, connection->peek_len);
554 memcpy(buf, connection->peek_msg, *recvd);
555 return TRUE;
556 }
557 else if (connection->peek_msg)
558 {
559 *recvd = min(len, connection->peek_len);
560 memcpy(buf, connection->peek_msg, *recvd);
561 connection->peek_len -= *recvd;
562 connection->peek_msg += *recvd;
563 if (connection->peek_len == 0)
564 {
565 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
566 connection->peek_msg_mem = NULL;
567 connection->peek_msg = NULL;
568 }
569 /* check if we got enough data from the peek buffer */
570 if (!(flags & MSG_WAITALL) || (*recvd == len))
571 return TRUE;
572 /* otherwise, fall through */
573 }
574 *recvd += pSSL_read(connection->ssl_s, (char*)buf + *recvd, len - *recvd);
575 if (flags & MSG_PEEK) /* must copy stuff into buffer */
576 {
577 connection->peek_len = *recvd;
578 if (!*recvd)
579 {
580 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
581 connection->peek_msg_mem = NULL;
582 connection->peek_msg = NULL;
583 }
584 else
585 memcpy(connection->peek_msg, buf, *recvd);
586 }
587 if (*recvd < 1 && len)
588 return FALSE;
589 return TRUE;
590 #else
591 return FALSE;
592 #endif
593 }
594 }
595
596 /******************************************************************************
597 * NETCON_query_data_available
598 * Returns the number of bytes of peeked data plus the number of bytes of
599 * queued, but unread data.
600 */
601 BOOL NETCON_query_data_available(WININET_NETCONNECTION *connection, DWORD *available)
602 {
603 *available = 0;
604 if (!NETCON_connected(connection))
605 return FALSE;
606
607 #ifdef SONAME_LIBSSL
608 if (connection->peek_msg) *available = connection->peek_len;
609 #endif
610
611 #ifdef FIONREAD
612 if (!connection->useSSL)
613 {
614 int unread;
615 int retval = ioctlsocket(connection->socketFD, FIONREAD, &unread);
616 if (!retval)
617 {
618 TRACE("%d bytes of queued, but unread data\n", unread);
619 *available += unread;
620 }
621 }
622 #endif
623 return TRUE;
624 }
625
626 /******************************************************************************
627 * NETCON_getNextLine
628 */
629 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
630 {
631
632 TRACE("\n");
633
634 if (!NETCON_connected(connection)) return FALSE;
635
636 if (!connection->useSSL)
637 {
638 struct timeval tv;
639 fd_set infd;
640 BOOL bSuccess = FALSE;
641 DWORD nRecv = 0;
642
643 FD_ZERO(&infd);
644 FD_SET(connection->socketFD, &infd);
645 tv.tv_sec=RESPONSE_TIMEOUT;
646 tv.tv_usec=0;
647
648 while (nRecv < *dwBuffer)
649 {
650 if (select(connection->socketFD+1,&infd,NULL,NULL,&tv) > 0)
651 {
652 if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
653 {
654 INTERNET_SetLastError(sock_get_error(errno));
655 goto lend;
656 }
657
658 if (lpszBuffer[nRecv] == '\n')
659 {
660 bSuccess = TRUE;
661 break;
662 }
663 if (lpszBuffer[nRecv] != '\r')
664 nRecv++;
665 }
666 else
667 {
668 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
669 goto lend;
670 }
671 }
672
673 lend: /* FIXME: don't use labels */
674 if (bSuccess)
675 {
676 lpszBuffer[nRecv++] = '\0';
677 *dwBuffer = nRecv;
678 TRACE(":%u %s\n", nRecv, lpszBuffer);
679 return TRUE;
680 }
681 else
682 {
683 return FALSE;
684 }
685 }
686 else
687 {
688 #ifdef SONAME_LIBSSL
689 long prev_timeout;
690 DWORD nRecv = 0;
691 BOOL success = TRUE;
692
693 prev_timeout = pSSL_CTX_get_timeout(ctx);
694 pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
695
696 while (nRecv < *dwBuffer)
697 {
698 int recv = 1;
699 if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
700 {
701 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
702 success = FALSE;
703 }
704
705 if (lpszBuffer[nRecv] == '\n')
706 {
707 success = TRUE;
708 break;
709 }
710 if (lpszBuffer[nRecv] != '\r')
711 nRecv++;
712 }
713
714 pSSL_CTX_set_timeout(ctx, prev_timeout);
715 if (success)
716 {
717 lpszBuffer[nRecv++] = '\0';
718 *dwBuffer = nRecv;
719 TRACE("_SSL:%u %s\n", nRecv, lpszBuffer);
720 return TRUE;
721 }
722 return FALSE;
723 #else
724 return FALSE;
725 #endif
726 }
727 }
728
729
730 LPCVOID NETCON_GetCert(WININET_NETCONNECTION *connection)
731 {
732 #ifdef SONAME_LIBSSL
733 X509* cert;
734 unsigned char* buffer,*p;
735 INT len;
736 BOOL malloced = FALSE;
737 LPCVOID r = NULL;
738
739 if (!connection->useSSL)
740 return NULL;
741
742 cert = pSSL_get_peer_certificate(connection->ssl_s);
743 p = NULL;
744 len = pi2d_X509(cert,&p);
745 /*
746 * SSL 0.9.7 and above malloc the buffer if it is null.
747 * however earlier version do not and so we would need to alloc the buffer.
748 *
749 * see the i2d_X509 man page for more details.
750 */
751 if (!p)
752 {
753 buffer = HeapAlloc(GetProcessHeap(),0,len);
754 p = buffer;
755 len = pi2d_X509(cert,&p);
756 }
757 else
758 {
759 buffer = p;
760 malloced = TRUE;
761 }
762
763 r = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
764
765 if (malloced)
766 free(buffer);
767 else
768 HeapFree(GetProcessHeap(),0,buffer);
769
770 return r;
771 #else
772 return NULL;
773 #endif
774 }
775
776 DWORD NETCON_set_timeout(WININET_NETCONNECTION *connection, BOOL send, int value)
777 {
778 int result;
779 struct timeval tv;
780
781 /* FIXME: we should probably store the timeout in the connection to set
782 * when we do connect */
783 if (!NETCON_connected(connection))
784 return ERROR_SUCCESS;
785
786 /* value is in milliseconds, convert to struct timeval */
787 tv.tv_sec = value / 1000;
788 tv.tv_usec = (value % 1000) * 1000;
789
790 result = setsockopt(connection->socketFD, SOL_SOCKET,
791 send ? SO_SNDTIMEO : SO_RCVTIMEO, &tv,
792 sizeof(tv));
793
794 if (result == -1)
795 {
796 WARN("setsockopt failed (%s)\n", strerror(errno));
797 return sock_get_error(errno);
798 }
799
800 return ERROR_SUCCESS;
801 }