Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / wininet / netconnection.c
blob9633f54319fa326f017314583dee25f156e0fd5e
1 /*
2 * Wininet - networking layer. Uses unix sockets or OpenSSL.
4 * Copyright 2002 TransGaming Technologies Inc.
6 * David Hammerton
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
28 #endif
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 # include <sys/socket.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <errno.h>
42 #include "wine/library.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "wininet.h"
46 #include "winerror.h"
48 /* To avoid conflicts with the Unix socket headers. we only need it for
49 * the error codes anyway. */
50 #define USE_WS_PREFIX
51 #include "winsock2.h"
53 #include "wine/debug.h"
54 #include "internet.h"
55 #include "wincrypt.h"
57 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
60 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
62 /* FIXME!!!!!!
63 * This should use winsock - To use winsock the functions will have to change a bit
64 * as they are designed for unix sockets.
65 * SSL stuff should use crypt32.dll
68 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
70 #include <openssl/err.h>
72 #ifndef SONAME_LIBSSL
73 #define SONAME_LIBSSL "libssl.so"
74 #endif
75 #ifndef SONAME_LIBCRYPTO
76 #define SONAME_LIBCRYPTO "libcrypto.so"
77 #endif
79 static void *OpenSSL_ssl_handle;
80 static void *OpenSSL_crypto_handle;
82 static SSL_METHOD *meth;
83 static SSL_CTX *ctx;
85 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
87 /* OpenSSL functions that we use */
88 MAKE_FUNCPTR(SSL_library_init);
89 MAKE_FUNCPTR(SSL_load_error_strings);
90 MAKE_FUNCPTR(SSLv23_method);
91 MAKE_FUNCPTR(SSL_CTX_new);
92 MAKE_FUNCPTR(SSL_new);
93 MAKE_FUNCPTR(SSL_free);
94 MAKE_FUNCPTR(SSL_set_fd);
95 MAKE_FUNCPTR(SSL_connect);
96 MAKE_FUNCPTR(SSL_shutdown);
97 MAKE_FUNCPTR(SSL_write);
98 MAKE_FUNCPTR(SSL_read);
99 MAKE_FUNCPTR(SSL_get_verify_result);
100 MAKE_FUNCPTR(SSL_get_peer_certificate);
101 MAKE_FUNCPTR(SSL_CTX_get_timeout);
102 MAKE_FUNCPTR(SSL_CTX_set_timeout);
103 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
104 MAKE_FUNCPTR(i2d_X509);
106 /* OpenSSL's libcrypto functions that we use */
107 MAKE_FUNCPTR(BIO_new_fp);
108 MAKE_FUNCPTR(ERR_get_error);
109 MAKE_FUNCPTR(ERR_error_string);
110 #undef MAKE_FUNCPTR
112 #endif
114 BOOL NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
116 connection->useSSL = FALSE;
117 connection->socketFD = -1;
118 if (useSSL)
120 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
121 const char *libcrypto_name_candidates[] = {SONAME_LIBCRYPTO,
122 "libcrypto.so.0.9.7",
123 "libcrypto.so.0.9.7a",
124 "libcrypto.so.0.9.7f",
125 "libcrypto.so.0.9.8",
126 "libcrypto.so.0.9.8a",
127 "libcrypto.so.6",
128 "libcrypto.so.5",
129 "libcrypto.so.4",
130 "libcrypto.so.0",
131 NULL};
132 int i;
133 const char *libssl_name_candidates[] = {SONAME_LIBSSL,
134 "libssl.so.0.9.7",
135 "libssl.so.0.9.7a",
136 "libssl.so.0.9.7f",
137 "libssl.so.0.9.8",
138 "libssl.so.0.9.8a",
139 "libssl.so.6",
140 "libssl.so.5",
141 "libssl.so.4",
142 "libssl.so.0",
143 NULL};
145 TRACE("using SSL connection\n");
146 if (OpenSSL_ssl_handle) /* already initialized everything */
147 return TRUE;
149 for (i = 0; libssl_name_candidates[i] && !OpenSSL_ssl_handle; i++ )
150 OpenSSL_ssl_handle = wine_dlopen(libssl_name_candidates[i], RTLD_NOW, NULL, 0);
152 if (!OpenSSL_ssl_handle)
154 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
155 SONAME_LIBSSL);
156 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
157 return FALSE;
160 for (i = 0; libcrypto_name_candidates[i] && !OpenSSL_crypto_handle; i++ )
161 OpenSSL_crypto_handle = wine_dlopen(libcrypto_name_candidates[i], RTLD_NOW, NULL, 0);
163 if (!OpenSSL_crypto_handle)
165 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
166 SONAME_LIBCRYPTO);
167 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
168 return FALSE;
171 /* mmm nice ugly macroness */
172 #define DYNSSL(x) \
173 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
174 if (!p##x) \
176 ERR("failed to load symbol %s\n", #x); \
177 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
178 return FALSE; \
181 DYNSSL(SSL_library_init);
182 DYNSSL(SSL_load_error_strings);
183 DYNSSL(SSLv23_method);
184 DYNSSL(SSL_CTX_new);
185 DYNSSL(SSL_new);
186 DYNSSL(SSL_free);
187 DYNSSL(SSL_set_fd);
188 DYNSSL(SSL_connect);
189 DYNSSL(SSL_shutdown);
190 DYNSSL(SSL_write);
191 DYNSSL(SSL_read);
192 DYNSSL(SSL_get_verify_result);
193 DYNSSL(SSL_get_peer_certificate);
194 DYNSSL(SSL_CTX_get_timeout);
195 DYNSSL(SSL_CTX_set_timeout);
196 DYNSSL(SSL_CTX_set_default_verify_paths);
197 DYNSSL(i2d_X509);
198 #undef DYNSSL
200 #define DYNCRYPTO(x) \
201 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
202 if (!p##x) \
204 ERR("failed to load symbol %s\n", #x); \
205 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
206 return FALSE; \
208 DYNCRYPTO(BIO_new_fp);
209 DYNCRYPTO(ERR_get_error);
210 DYNCRYPTO(ERR_error_string);
211 #undef DYNCRYPTO
213 pSSL_library_init();
214 pSSL_load_error_strings();
215 pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
217 meth = pSSLv23_method();
218 connection->peek_msg = NULL;
219 connection->peek_msg_mem = NULL;
220 #else
221 FIXME("can't use SSL, not compiled in.\n");
222 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
223 return FALSE;
224 #endif
226 return TRUE;
229 BOOL NETCON_connected(WININET_NETCONNECTION *connection)
231 if (connection->socketFD == -1)
232 return FALSE;
233 else
234 return TRUE;
237 /* translate a unix error code into a winsock one */
238 static int sock_get_error( int err )
240 switch (err)
242 case EINTR: return WSAEINTR;
243 case EBADF: return WSAEBADF;
244 case EPERM:
245 case EACCES: return WSAEACCES;
246 case EFAULT: return WSAEFAULT;
247 case EINVAL: return WSAEINVAL;
248 case EMFILE: return WSAEMFILE;
249 case EWOULDBLOCK: return WSAEWOULDBLOCK;
250 case EINPROGRESS: return WSAEINPROGRESS;
251 case EALREADY: return WSAEALREADY;
252 case ENOTSOCK: return WSAENOTSOCK;
253 case EDESTADDRREQ: return WSAEDESTADDRREQ;
254 case EMSGSIZE: return WSAEMSGSIZE;
255 case EPROTOTYPE: return WSAEPROTOTYPE;
256 case ENOPROTOOPT: return WSAENOPROTOOPT;
257 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
258 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
259 case EOPNOTSUPP: return WSAEOPNOTSUPP;
260 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
261 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
262 case EADDRINUSE: return WSAEADDRINUSE;
263 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
264 case ENETDOWN: return WSAENETDOWN;
265 case ENETUNREACH: return WSAENETUNREACH;
266 case ENETRESET: return WSAENETRESET;
267 case ECONNABORTED: return WSAECONNABORTED;
268 case EPIPE:
269 case ECONNRESET: return WSAECONNRESET;
270 case ENOBUFS: return WSAENOBUFS;
271 case EISCONN: return WSAEISCONN;
272 case ENOTCONN: return WSAENOTCONN;
273 case ESHUTDOWN: return WSAESHUTDOWN;
274 case ETOOMANYREFS: return WSAETOOMANYREFS;
275 case ETIMEDOUT: return WSAETIMEDOUT;
276 case ECONNREFUSED: return WSAECONNREFUSED;
277 case ELOOP: return WSAELOOP;
278 case ENAMETOOLONG: return WSAENAMETOOLONG;
279 case EHOSTDOWN: return WSAEHOSTDOWN;
280 case EHOSTUNREACH: return WSAEHOSTUNREACH;
281 case ENOTEMPTY: return WSAENOTEMPTY;
282 #ifdef EPROCLIM
283 case EPROCLIM: return WSAEPROCLIM;
284 #endif
285 #ifdef EUSERS
286 case EUSERS: return WSAEUSERS;
287 #endif
288 #ifdef EDQUOT
289 case EDQUOT: return WSAEDQUOT;
290 #endif
291 #ifdef ESTALE
292 case ESTALE: return WSAESTALE;
293 #endif
294 #ifdef EREMOTE
295 case EREMOTE: return WSAEREMOTE;
296 #endif
297 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
301 /******************************************************************************
302 * NETCON_create
303 * Basically calls 'socket()'
305 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
306 int type, int protocol)
308 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
309 if (connection->useSSL)
310 return FALSE;
311 #endif
313 connection->socketFD = socket(domain, type, protocol);
314 if (connection->socketFD == -1)
316 INTERNET_SetLastError(sock_get_error(errno));
317 return FALSE;
319 return TRUE;
322 /******************************************************************************
323 * NETCON_close
324 * Basically calls 'close()' unless we should use SSL
326 BOOL NETCON_close(WININET_NETCONNECTION *connection)
328 int result;
330 if (!NETCON_connected(connection)) return FALSE;
332 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
333 if (connection->useSSL)
335 HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
336 connection->peek_msg = NULL;
337 connection->peek_msg_mem = NULL;
338 connection->peek_len = 0;
340 pSSL_shutdown(connection->ssl_s);
341 pSSL_free(connection->ssl_s);
342 connection->ssl_s = NULL;
344 connection->useSSL = FALSE;
346 #endif
348 result = closesocket(connection->socketFD);
349 connection->socketFD = -1;
351 if (result == -1)
353 INTERNET_SetLastError(sock_get_error(errno));
354 return FALSE;
356 return TRUE;
358 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
359 static BOOL check_hostname(X509 *cert, char *hostname)
361 /* FIXME: implement */
362 return TRUE;
364 #endif
365 /******************************************************************************
366 * NETCON_secure_connect
367 * Initiates a secure connection over an existing plaintext connection.
369 BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
371 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
372 long verify_res;
373 X509 *cert;
374 int len;
375 char *hostname_unix;
377 /* can't connect if we are already connected */
378 if (connection->useSSL)
380 ERR("already connected\n");
381 return FALSE;
384 ctx = pSSL_CTX_new(meth);
385 if (!pSSL_CTX_set_default_verify_paths(ctx))
387 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
388 pERR_error_string(pERR_get_error(), 0));
389 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
390 return FALSE;
392 connection->ssl_s = pSSL_new(ctx);
393 if (!connection->ssl_s)
395 ERR("SSL_new failed: %s\n",
396 pERR_error_string(pERR_get_error(), 0));
397 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
398 goto fail;
401 if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
403 ERR("SSL_set_fd failed: %s\n",
404 pERR_error_string(pERR_get_error(), 0));
405 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
406 goto fail;
409 if (pSSL_connect(connection->ssl_s) <= 0)
411 ERR("SSL_connect failed: %s\n",
412 pERR_error_string(pERR_get_error(), 0));
413 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
414 goto fail;
416 cert = pSSL_get_peer_certificate(connection->ssl_s);
417 if (!cert)
419 ERR("no certificate for server %s\n", debugstr_w(hostname));
420 /* FIXME: is this the best error? */
421 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
422 goto fail;
424 verify_res = pSSL_get_verify_result(connection->ssl_s);
425 if (verify_res != X509_V_OK)
427 ERR("couldn't verify the security of the connection, %ld\n", verify_res);
428 /* FIXME: we should set an error and return, but we only warn at
429 * the moment */
432 len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
433 hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
434 if (!hostname_unix)
436 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
437 goto fail;
439 WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
441 if (!check_hostname(cert, hostname_unix))
443 HeapFree(GetProcessHeap(), 0, hostname_unix);
444 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
445 goto fail;
448 HeapFree(GetProcessHeap(), 0, hostname_unix);
449 connection->useSSL = TRUE;
450 return TRUE;
452 fail:
453 if (connection->ssl_s)
455 pSSL_shutdown(connection->ssl_s);
456 pSSL_free(connection->ssl_s);
457 connection->ssl_s = NULL;
459 #endif
460 return FALSE;
463 /******************************************************************************
464 * NETCON_connect
465 * Connects to the specified address.
467 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
468 unsigned int addrlen)
470 int result;
472 if (!NETCON_connected(connection)) return FALSE;
474 result = connect(connection->socketFD, serv_addr, addrlen);
475 if (result == -1)
477 WARN("Unable to connect to host (%s)\n", strerror(errno));
478 INTERNET_SetLastError(sock_get_error(errno));
480 closesocket(connection->socketFD);
481 connection->socketFD = -1;
482 return FALSE;
485 return TRUE;
488 /******************************************************************************
489 * NETCON_send
490 * Basically calls 'send()' unless we should use SSL
491 * number of chars send is put in *sent
493 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
494 int *sent /* out */)
496 if (!NETCON_connected(connection)) return FALSE;
497 if (!connection->useSSL)
499 *sent = send(connection->socketFD, msg, len, flags);
500 if (*sent == -1)
502 INTERNET_SetLastError(sock_get_error(errno));
503 return FALSE;
505 return TRUE;
507 else
509 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
510 if (flags)
511 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
512 *sent = pSSL_write(connection->ssl_s, msg, len);
513 if (*sent < 1 && len)
514 return FALSE;
515 return TRUE;
516 #else
517 return FALSE;
518 #endif
522 /******************************************************************************
523 * NETCON_recv
524 * Basically calls 'recv()' unless we should use SSL
525 * number of chars received is put in *recvd
527 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
528 int *recvd /* out */)
530 *recvd = 0;
531 if (!NETCON_connected(connection)) return FALSE;
532 if (!len)
533 return TRUE;
534 if (!connection->useSSL)
536 *recvd = recv(connection->socketFD, buf, len, flags);
537 if (*recvd == -1)
539 INTERNET_SetLastError(sock_get_error(errno));
540 return FALSE;
542 return TRUE;
544 else
546 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
547 if (flags & ~(MSG_PEEK|MSG_WAITALL))
548 FIXME("SSL_read does not support the following flag: %08x\n", flags);
550 /* this ugly hack is all for MSG_PEEK. eww gross */
551 if (flags & MSG_PEEK && !connection->peek_msg)
553 connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
555 else if (flags & MSG_PEEK && connection->peek_msg)
557 if (len < connection->peek_len)
558 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
559 *recvd = min(len, connection->peek_len);
560 memcpy(buf, connection->peek_msg, *recvd);
561 return TRUE;
563 else if (connection->peek_msg)
565 *recvd = min(len, connection->peek_len);
566 memcpy(buf, connection->peek_msg, *recvd);
567 connection->peek_len -= *recvd;
568 connection->peek_msg += *recvd;
569 if (connection->peek_len == 0)
571 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
572 connection->peek_msg_mem = NULL;
573 connection->peek_msg = NULL;
575 /* check if we got enough data from the peek buffer */
576 if (!(flags & MSG_WAITALL) || (*recvd == len))
577 return TRUE;
578 /* otherwise, fall through */
580 *recvd += pSSL_read(connection->ssl_s, (char*)buf + *recvd, len - *recvd);
581 if (flags & MSG_PEEK) /* must copy stuff into buffer */
583 connection->peek_len = *recvd;
584 if (!*recvd)
586 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
587 connection->peek_msg_mem = NULL;
588 connection->peek_msg = NULL;
590 else
591 memcpy(connection->peek_msg, buf, *recvd);
593 if (*recvd < 1 && len)
594 return FALSE;
595 return TRUE;
596 #else
597 return FALSE;
598 #endif
602 /******************************************************************************
603 * NETCON_getNextLine
605 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
608 TRACE("\n");
610 if (!NETCON_connected(connection)) return FALSE;
612 if (!connection->useSSL)
614 struct timeval tv;
615 fd_set infd;
616 BOOL bSuccess = FALSE;
617 DWORD nRecv = 0;
619 FD_ZERO(&infd);
620 FD_SET(connection->socketFD, &infd);
621 tv.tv_sec=RESPONSE_TIMEOUT;
622 tv.tv_usec=0;
624 while (nRecv < *dwBuffer)
626 if (select(connection->socketFD+1,&infd,NULL,NULL,&tv) > 0)
628 if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
630 INTERNET_SetLastError(sock_get_error(errno));
631 goto lend;
634 if (lpszBuffer[nRecv] == '\n')
636 bSuccess = TRUE;
637 break;
639 if (lpszBuffer[nRecv] != '\r')
640 nRecv++;
642 else
644 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
645 goto lend;
649 lend: /* FIXME: don't use labels */
650 if (bSuccess)
652 lpszBuffer[nRecv++] = '\0';
653 *dwBuffer = nRecv;
654 TRACE(":%u %s\n", nRecv, lpszBuffer);
655 return TRUE;
657 else
659 return FALSE;
662 else
664 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
665 long prev_timeout;
666 DWORD nRecv = 0;
667 BOOL success = TRUE;
669 prev_timeout = pSSL_CTX_get_timeout(ctx);
670 pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
672 while (nRecv < *dwBuffer)
674 int recv = 1;
675 if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
677 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
678 success = FALSE;
681 if (lpszBuffer[nRecv] == '\n')
683 success = TRUE;
684 break;
686 if (lpszBuffer[nRecv] != '\r')
687 nRecv++;
690 pSSL_CTX_set_timeout(ctx, prev_timeout);
691 if (success)
693 lpszBuffer[nRecv++] = '\0';
694 *dwBuffer = nRecv;
695 TRACE("_SSL:%u %s\n", nRecv, lpszBuffer);
696 return TRUE;
698 return FALSE;
699 #else
700 return FALSE;
701 #endif
706 LPCVOID NETCON_GetCert(WININET_NETCONNECTION *connection)
709 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
710 X509* cert;
711 unsigned char* buffer,*p;
712 INT len;
713 BOOL malloced = FALSE;
714 LPCVOID r = NULL;
716 if (!connection->useSSL)
717 return NULL;
719 cert = pSSL_get_peer_certificate(connection->ssl_s);
720 p = NULL;
721 len = pi2d_X509(cert,&p);
723 * SSL 0.9.7 and above malloc the buffer if it is null.
724 * however earlier version do not and so we would need to alloc the buffer.
726 * see the i2d_X509 man page for more details.
728 if (!p)
730 buffer = HeapAlloc(GetProcessHeap(),0,len);
731 p = buffer;
732 len = pi2d_X509(cert,&p);
734 else
736 buffer = p;
737 malloced = TRUE;
740 r = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
742 if (malloced)
743 free(buffer);
744 else
745 HeapFree(GetProcessHeap(),0,buffer);
747 return r;
748 #else
749 return NULL;
750 #endif
753 BOOL NETCON_set_timeout(WININET_NETCONNECTION *connection, BOOL send, int value)
755 int result;
756 struct timeval tv;
758 /* FIXME: we should probably store the timeout in the connection to set
759 * when we do connect */
760 if (!NETCON_connected(connection))
761 return TRUE;
763 /* value is in milliseconds, convert to struct timeval */
764 tv.tv_sec = value / 1000;
765 tv.tv_usec = (value % 1000) * 1000;
767 result = setsockopt(connection->socketFD, SOL_SOCKET,
768 send ? SO_SNDTIMEO : SO_RCVTIMEO, &tv,
769 sizeof(tv));
771 if (result == -1)
773 WARN("setsockopt failed (%s)\n", strerror(errno));
774 INTERNET_SetLastError(sock_get_error(errno));
775 return FALSE;
778 return TRUE;