cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
tcp.c
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * cryptlib TCP/IP Interface Routines *
4 * Copyright Peter Gutmann 1998-2007 *
5 * *
6 ****************************************************************************/
7 
8 #include <ctype.h>
9 #if defined( INC_ALL )
10  #include "crypt.h"
11  #include "stream_int.h"
12  #include "tcp.h"
13 #else
14  #include "crypt.h"
15  #include "io/stream_int.h"
16  #include "io/tcp.h"
17 #endif /* Compiler-specific includes */
18 
19 #ifdef USE_TCP
20 
21 /****************************************************************************
22 * *
23 * Init/Shutdown Routines *
24 * *
25 ****************************************************************************/
26 
27 /* Forward declarations for socket pool functions */
28 
29 CHECK_RETVAL \
30 static int initSocketPool( void );
31 static void endSocketPool( void );
32 
33 #ifdef __WINDOWS__
34 
35 #ifndef TEXT
36  #define TEXT /* Win32 windows.h defines this, but not the Win16 one */
37 #endif /* TEXT */
38 
39 #ifdef _MSC_VER
40  #pragma warning( disable: 4127 ) /* False-positive in winsock.h */
41 #endif /* VC++ */
42 
43 /* Global function pointers. These are necessary because the functions need
44  to be dynamically linked since not all systems contain the necessary
45  libraries */
46 
47 static INSTANCE_HANDLE hTCP, hIPv6;
48 
49 typedef CHECK_RETVAL STDC_NONNULL_ARG( ( 2, 3 ) ) \
50  SOCKET ( SOCKET_API *ACCEPT )( IN SOCKET s, OUT struct sockaddr *addr,
51  INOUT int *addrlen );
52 typedef CHECK_RETVAL STDC_NONNULL_ARG( ( 2 ) ) \
53  int ( SOCKET_API *BIND )( IN SOCKET s,
54  const struct sockaddr FAR *addr,
55  IN_LENGTH_DNS int namelen );
56 typedef CHECK_RETVAL STDC_NONNULL_ARG( ( 2 ) ) \
57  int ( SOCKET_API *CONNECT )( IN SOCKET s, IN_BUFFER( namelen ) \
58  const struct sockaddr *name,
59  IN_LENGTH_DNS int namelen );
60 typedef STDC_NONNULL_ARG( ( 4, 5 ) ) \
61  int ( SOCKET_API *GETSOCKOPT )( IN SOCKET s, IN int level,
62  IN int optname,
63  OUT_BUFFER_FIXED( *optlen ) char *optval,
64  INOUT int FAR *optlen );
65 typedef CHECK_RETVAL \
66  int ( SOCKET_API *LISTEN )( IN SOCKET s, IN int backlog );
67 typedef CHECK_RETVAL RETVAL STDC_NONNULL_ARG( ( 2 ) ) \
68  int ( SOCKET_API *RECV )( IN SOCKET s,
69  OUT_BUFFER( len, return ) char *buf,
70  IN_LENGTH int len, IN int flags );
71 typedef CHECK_RETVAL STDC_NONNULL_ARG( ( 4, 5 ) ) \
72  int ( SOCKET_API *SELECT )( IN int nfds, INOUT_OPT fd_set *readfds,
73  INOUT_OPT fd_set *writefds,
74  INOUT fd_set *exceptfds,
75  const struct timeval *timeout );
76 typedef CHECK_RETVAL STDC_NONNULL_ARG( ( 2 ) ) \
77  int ( SOCKET_API *SEND )( IN SOCKET s,
78  IN_BUFFER( len ) const char *buf,
79  IN_LENGTH int len, IN int flags );
80 typedef STDC_NONNULL_ARG( ( 4 ) ) \
81  int ( SOCKET_API *SETSOCKOPT )( IN SOCKET s, IN int level, \
82  IN int optname,
83  IN_BUFFER( optlen ) char *optval,
84  IN int optlen );
85 typedef int ( SOCKET_API *SHUTDOWN )( IN SOCKET s, IN int how );
86 typedef CHECK_RETVAL \
87  SOCKET ( SOCKET_API *SOCKETFN )( IN int af, IN int type,
88  IN int protocol );
89 #ifdef __WINDOWS__
90 typedef int ( SOCKET_API *CLOSESOCKET )( IN SOCKET s );
91 typedef CHECK_RETVAL STDC_NONNULL_ARG( ( 2 ) ) \
92  int ( SOCKET_API *FDISSETFN )( IN SOCKET s, fd_set *fds );
93 typedef CHECK_RETVAL STDC_NONNULL_ARG( ( 3 ) ) \
94  int ( SOCKET_API *IOCTLSOCKET )( IN SOCKET s, IN long cmd,
95  INOUT u_long FAR *argp );
96 typedef int ( SOCKET_API *WSACLEANUP )( void );
97 typedef CHECK_RETVAL \
98  int ( SOCKET_API *WSAGETLASTERROR )( void );
99 typedef CHECK_RETVAL \
100  int ( SOCKET_API *WSASTARTUP )( IN WORD wVersionRequested,
101  OUT LPWSADATA lpWSAData );
102 #endif /* __WINDOWS__ */
103 static ACCEPT paccept = NULL;
104 static BIND pbind = NULL;
105 static CONNECT pconnect = NULL;
106 static GETSOCKOPT pgetsockopt = NULL;
107 static LISTEN plisten = NULL;
108 static RECV precv = NULL;
109 static SELECT pselect = NULL;
110 static SEND psend = NULL;
111 static SETSOCKOPT psetsockopt = NULL;
112 static SHUTDOWN pshutdown = NULL;
113 static SOCKETFN psocket = NULL;
114 #ifdef __WINDOWS__
115 static CLOSESOCKET pclosesocket = NULL;
116 static FDISSETFN pFDISSETfn = NULL;
117 static IOCTLSOCKET pioctlsocket = NULL;
118 static WSACLEANUP pWSACleanup = NULL;
119 static WSAGETLASTERROR pWSAGetLastError = NULL;
120 static WSASTARTUP pWSAStartup = NULL;
121 #endif /* __WINDOWS__ */
122 #if ( defined( sun ) && OSVERSION > 4 )
123  static int *h_errnoPtr;
124 
125  #undef getHostErrorCode
126  #define getHostErrorCode() *h_errnoPtr
127 #endif /* Slowaris */
128 
129 #define accept paccept
130 #define bind pbind
131 #define connect pconnect
132 #define getsockopt pgetsockopt
133 #define listen plisten
134 #define recv precv
135 #define select pselect
136 #define send psend
137 #define setsockopt psetsockopt
138 #define shutdown pshutdown
139 #define socket psocket
140 #ifdef __WINDOWS__
141 #define closesocket pclosesocket
142 #define __WSAFDIsSet pFDISSETfn
143 #define ioctlsocket pioctlsocket
144 #define WSACleanup pWSACleanup
145 #ifndef WSAGetLastError
146  /* In some environments WSAGetLastError() is a macro that maps to
147  GetLastError() */
148  #define WSAGetLastError pWSAGetLastError
149  #define DYNLOAD_WSAGETLASTERROR
150 #endif /* WSAGetLastError */
151 #define WSAStartup pWSAStartup
152 #endif /* __WINDOWS__ */
153 
154 /* Dynamically load and unload any necessary TCP/IP libraries. Under Windows
155  the dynamic loading is complicated by the existence of Winsock 1 vs.
156  Winsock 2, all recent systems use Winsock 2 but we allow for Winsock 1 as
157  well just in case */
158 
159 #ifdef __WINDOWS__
160  #ifdef __WIN16__
161  #define TCP_LIBNAME "winsock.dll"
162  #elif defined( __WIN32__ )
163  #define TCP_LIBNAME TEXT( "ws2_32.dll" )
164  #define WINSOCK_OLD_LIBNAME TEXT( "wsock32.dll" )
165  #elif defined( __WINCE__ )
166  #define TCP_LIBNAME TEXT( "ws2.dll" )
167  #else
168  #error Unknown Windows variant encountered
169  #endif /* Win16/Win32/WinCE */
170 #else
171  #define TCP_LIBNAME "libsocket.so"
172  #define TEXT( x ) x
173 #endif /* OS-specific TCP/IP library naming */
174 
175 RETVAL \
176 int netInitTCP( void )
177  {
178 #ifdef __WINDOWS__
179  WSADATA wsaData;
180  #ifdef __WIN16__
181  UINT errorMode;
182  #endif /* __WIN16__ */
183  BOOLEAN ip6inWinsock = FALSE;
184  int status;
185 #endif /* __WINDOWS__ */
186 
187  /* Obtain a handle to the modules containing the TCP/IP functions */
188 #ifdef __WINDOWS__
189  hTCP = hIPv6 = NULL_INSTANCE;
190  #if defined( __WIN16__ )
191  errorMode = SetErrorMode( SEM_NOOPENFILEERRORBOX );
192  hTCP = DynamicLoad( TCP_LIBNAME );
193  SetErrorMode( errorMode );
194  if( hTCP < HINSTANCE_ERROR )
195  {
196  hTCP = NULL_INSTANCE;
197  return( CRYPT_ERROR );
198  }
199  #elif defined( __WIN32__ )
200  if( ( hTCP = DynamicLoad( TCP_LIBNAME ) ) == NULL_INSTANCE && \
201  ( hTCP = DynamicLoad( WINSOCK_OLD_LIBNAME ) ) == NULL_INSTANCE )
202  return( CRYPT_ERROR );
203  if( DynamicBind( hTCP, "getaddrinfo" ) != NULL )
204  ip6inWinsock = TRUE;
205  else
206  {
207  /* Newer releases of Windows put the IPv6 functions in the Winsock 2
208  library, older (non-IPv6-enabled) releases had it available as an
209  experimental add-on using the IPv6 Technology Preview library */
210  hIPv6 = DynamicLoad( "wship6.dll" );
211  }
212  #elif defined( __WINCE__ )
213  if( ( hTCP = DynamicLoad( TCP_LIBNAME ) ) == NULL_INSTANCE )
214  return( CRYPT_ERROR );
215  if( DynamicBind( hTCP, TEXT( "getaddrinfo" ) ) != NULL )
216  ip6inWinsock = TRUE;
217  #endif /* Win16/Win32/WinCE */
218 #else
219  if( ( hTCP = DynamicLoad( TCP_LIBNAME ) ) == NULL_INSTANCE )
220  return( CRYPT_ERROR );
221 #endif /* OS-specific dynamic load */
222 
223  /* Now get pointers to the functions */
224  accept = ( ACCEPT ) DynamicBind( hTCP, TEXT( "accept" ) );
225  bind = ( BIND ) DynamicBind( hTCP, TEXT( "bind" ) );
226  connect = ( CONNECT ) DynamicBind( hTCP, TEXT( "connect" ) );
227  getsockopt = ( GETSOCKOPT ) DynamicBind( hTCP, TEXT( "getsockopt" ) );
228  listen = ( LISTEN ) DynamicBind( hTCP, TEXT( "listen" ) );
229  recv = ( RECV ) DynamicBind( hTCP, TEXT( "recv" ) );
230  select = ( SELECT ) DynamicBind( hTCP, TEXT( "select" ) );
231  send = ( SEND ) DynamicBind( hTCP, TEXT( "send" ) );
232  setsockopt = ( SETSOCKOPT ) DynamicBind( hTCP, TEXT( "setsockopt" ) );
233  shutdown = ( SHUTDOWN ) DynamicBind( hTCP, TEXT( "shutdown" ) );
234  socket = ( SOCKETFN ) DynamicBind( hTCP, TEXT( "socket" ) );
235 #ifdef __WINDOWS__
236  closesocket = ( CLOSESOCKET ) DynamicBind( hTCP, TEXT( "closesocket" ) );
237  __WSAFDIsSet = ( FDISSETFN ) DynamicBind( hTCP, TEXT( "__WSAFDIsSet" ) );
238  ioctlsocket = ( IOCTLSOCKET ) DynamicBind( hTCP, TEXT( "ioctlsocket" ) );
239  WSACleanup = ( WSACLEANUP ) DynamicBind( hTCP, TEXT( "WSACleanup" ) );
240  #ifdef DYNLOAD_WSAGETLASTERROR
241  WSAGetLastError = ( WSAGETLASTERROR ) DynamicBind( hTCP, TEXT( "WSAGetLastError" ) );
242  #endif /* DYNLOAD_WSAGETLASTERROR */
243  WSAStartup = ( WSASTARTUP ) DynamicBind( hTCP, TEXT( "WSAStartup" ) );
244  if( ip6inWinsock || hIPv6 != NULL_INSTANCE )
245  status = initDNS( hTCP, ip6inWinsock ? hTCP : hIPv6 );
246  else
247  status = initDNS( hTCP, NULL_INSTANCE );
248  if( cryptStatusError( status ) )
249  {
250  if( hIPv6 != NULL_INSTANCE )
251  {
252  DynamicUnload( hIPv6 );
253  hIPv6 = NULL_INSTANCE;
254  }
255  DynamicUnload( hTCP );
256  hTCP = NULL_INSTANCE;
257  return( CRYPT_ERROR );
258  }
259 #endif /* __WINDOWS__ */
260 #if ( defined( sun ) && OSVERSION > 4 )
261  h_errnoPtr = ( int * ) DynamicBind( hTCP, "h_errno" );
262  if( h_errnoPtr == NULL )
263  {
264  DynamicUnload( hTCP );
265  hTCP = NULL_INSTANCE;
266  return( CRYPT_ERROR );
267  }
268 #endif /* Slowaris */
269 
270  /* Make sure that we got valid pointers for every TCP/IP function */
271  if( accept == NULL || bind == NULL || connect == NULL || \
272  getsockopt == NULL || listen == NULL || recv == NULL || \
273  select == NULL || send == NULL || setsockopt == NULL || \
274  shutdown == NULL || socket == NULL )
275  {
276  endDNS( hTCP );
277  DynamicUnload( hTCP );
278  hTCP = NULL_INSTANCE;
279  if( hIPv6 != NULL_INSTANCE )
280  {
281  DynamicUnload( hIPv6 );
282  hIPv6 = NULL_INSTANCE;
283  }
284  return( CRYPT_ERROR );
285  }
286 
287 #ifdef __WINDOWS__
288  if( closesocket == NULL || __WSAFDIsSet == NULL || \
289  ioctlsocket == NULL || WSACleanup == NULL ||
290  #ifdef DYNLOAD_WSAGETLASTERROR
291  WSAGetLastError == NULL ||
292  #endif /* DYNLOAD_WSAGETLASTERROR */
293  WSAStartup == NULL || \
294  ( WSAStartup( 2, &wsaData ) && WSAStartup( 1, &wsaData ) ) )
295  {
296  endDNS( hTCP );
297  DynamicUnload( hTCP );
298  hTCP = NULL_INSTANCE;
299  if( hIPv6 != NULL_INSTANCE )
300  {
301  DynamicUnload( hIPv6 );
302  hIPv6 = NULL_INSTANCE;
303  }
304  return( CRYPT_ERROR );
305  }
306 #endif /* __WINDOWS__ */
307 
308  /* Set up the socket pool state information */
309  return( initSocketPool() );
310  }
311 
312 void netEndTCP( void )
313  {
314  /* Clean up the socket pool state information */
315  endSocketPool();
316 
317  endDNS( hTCP );
318  if( hIPv6 != NULL_INSTANCE )
319  DynamicUnload( hIPv6 );
320  if( hTCP != NULL_INSTANCE )
321  {
322 #ifdef __WINDOWS__
323  /* Wipe the Sheets Afterwards and Cleanup */
324  WSACleanup();
325 #endif /* __WINDOWS__ */
326  DynamicUnload( hTCP );
327  }
328  hTCP = hIPv6 = NULL_INSTANCE;
329  }
330 
331 /* Return the status of the network interface */
332 
333 CHECK_RETVAL_BOOL \
334 static BOOLEAN transportOKFunction( void )
335  {
336  return( hTCP != NULL_INSTANCE ? TRUE : FALSE );
337  }
338 #else
339 
340 RETVAL \
341 int netInitTCP( void )
342  {
343 #ifdef __SCO_VERSION__
344  struct sigaction act, oact;
345 
346  /* Work around the broken SCO/UnixWare signal-handling, which sometimes
347  sends a nonblocking socket a SIGIO (thus killing the process) when
348  waiting in a select() (this may have been fixed by the switch to
349  blocking sockets necessitated by Winsock bugs with non-blocking
350  sockets, and will be fixed long-term when SCO's long death march
351  eventually ends). Since SIGIO is an alias for SIGPOLL, SCO doesn't
352  help by reporting this as a "polling alarm". To fix this we need to
353  catch and swallow SIGIOs */
354  memset( &act, 0, sizeof( act ) );
355  act.sa_handler = SIG_IGN;
356  sigemptyset( &act.sa_mask );
357  if( sigaction( SIGIO, &act, &oact ) < 0 )
358  {
359  /* This assumes that stderr is open, i.e. that we're not a daemon.
360  This should be the case, at least during the development/debugging
361  stage */
362  fprintf( stderr, "cryptlib: sigaction failed, errno = %d, "
363  "file = %s, line = %d.\n", errno, __FILE__, __LINE__ );
364  abort();
365  }
366 
367  /* Check for handler override. */
368  if( oact.sa_handler != SIG_DFL && oact.sa_handler != SIG_IGN )
369  {
370  /* We overwrote the caller's handler, reinstate the old handler and
371  warn them about this */
372  fprintf( stderr, "Warning: Conflicting SIGIO handling detected in "
373  "UnixWare socket bug\n workaround, file " __FILE__
374  ", line %d. This may cause\n false SIGIO/SIGPOLL "
375  "errors.\n", __LINE__ );
376  sigaction( SIGIO, &oact, &act );
377  }
378 #endif /* UnixWare/SCO */
379 
380  /* Set up the socket pool state information */
381  return( initSocketPool() );
382  }
383 
384 void netEndTCP( void )
385  {
386  /* Clean up the socket pool state information */
387  endSocketPool();
388 
389 #ifdef __SCO_VERSION__
390  signal( SIGIO, SIG_DFL );
391 #endif /* UnixWare/SCO */
392  }
393 
394 CHECK_RETVAL_BOOL \
395 static BOOLEAN transportOKFunction( void )
396  {
397 #if defined( __TANDEM_NSK__ ) || defined( __TANDEM_OSS__ )
398  static BOOLEAN transportOK = FALSE;
399 
400  if( !transportOK )
401  {
402  SOCKET netSocket;
403 
404  /* If the networking subsystem isn't enabled, attempting any network
405  operations will return ENOENT (which isn't a normal return code,
406  but is the least inappropriate thing to return). In order to
407  check this before we get deep into the networking code, we create
408  a test socket here to make sure that everything is OK. If the
409  network transport is unavailable, we re-try each time we're
410  called in case it's been enabled in the meantime */
411  netSocket = socket( PF_INET, SOCK_STREAM, 0 );
412  if( !isBadSocket( netSocket ) )
413  {
414  closesocket( netSocket );
415  transportOK = TRUE;
416  }
417  }
418  return( transportOK );
419 #else
420  return( TRUE );
421 #endif /* OS-specific socket availability check */
422  }
423 #endif /* __WINDOWS__ */
424 
425 /****************************************************************************
426 * *
427 * Utility Routines *
428 * *
429 ****************************************************************************/
430 
431 /* Map of common error codes to strings. The error code supplied by the
432  caller is usually used as the return status code, however if a more
433  specific error code than the default is available it's specified via the
434  cryptSpecificCode member */
435 
436 typedef struct {
437  const int errorCode; /* Native error code */
438  const int cryptSpecificCode;/* Specific cryptlib error code */
439  const BOOLEAN isFatal; /* Seriousness level */
440  BUFFER_FIXED( errorStringLength ) \
441  const char FAR_BSS *errorString;
442  const int errorStringLength;/* Error message */
443  } SOCKETERROR_INFO;
444 
445 #ifdef __WINDOWS__
446 
447 static const SOCKETERROR_INFO FAR_BSS socketErrorInfo[] = {
448  { WSAECONNREFUSED, CRYPT_ERROR_PERMISSION, TRUE,
449  "WSAECONNREFUSED: The attempt to connect was rejected", 52 },
450  { WSAEADDRNOTAVAIL, CRYPT_ERROR_NOTFOUND, TRUE,
451  "WSAEADDRNOTAVAIL: The remote address is not a valid address", 59 },
452  { WSAECONNABORTED, CRYPT_OK, TRUE,
453  "WSAECONNABORTED: Connection was terminated due to a time-out or "
454  "other failure", 77 },
455  { WSAECONNRESET, CRYPT_OK, TRUE,
456  "WSAECONNRESET: Connection was reset by the remote host executing "
457  "a close", 72 },
458  { WSAEHOSTUNREACH, CRYPT_OK, TRUE,
459  "WSAEHOSTUNREACH: Remote host cannot be reached from this host at "
460  "this time", 74 },
461  { WSAEMSGSIZE, CRYPT_ERROR_OVERFLOW, FALSE,
462  "WSAEMSGSIZE: Message is larger than the maximum supported by the "
463  "underlying transport", 85 },
464  { WSAENETDOWN, CRYPT_OK, FALSE,
465  "WSAENETDOWN: The network subsystem has failed", 45 },
466  { WSAENETRESET, CRYPT_OK, FALSE,
467  "WSAENETRESET: Connection was broken due to keep-alive detecting a "
468  "failure while operation was in progress", 105 },
469  { WSAENETUNREACH, CRYPT_ERROR_NOTAVAIL, FALSE,
470  "WSAENETUNREACH: Network cannot be reached from this host at this "
471  "time", 69 },
472  { WSAENOBUFS, CRYPT_ERROR_MEMORY, FALSE,
473  "WSAENOBUFS: No buffer space available", 37 },
474  { WSAENOTCONN, CRYPT_OK, TRUE,
475  "WSAENOTCONN: Socket is not connected", 36 },
476  { WSAETIMEDOUT, CRYPT_ERROR_TIMEOUT, FALSE,
477  "WSAETIMEDOUT: Function timed out before completion", 50 },
478  { WSAHOST_NOT_FOUND, CRYPT_ERROR_NOTFOUND, FALSE,
479  "WSAHOST_NOT_FOUND: Host not found", 34 },
480  { WSATRY_AGAIN, CRYPT_OK, FALSE,
481  "WSATRY_AGAIN: Host not found (non-authoritative)", 48 },
482  { WSANO_ADDRESS, CRYPT_OK, FALSE,
483  "WSANO_ADDRESS: No address record available for this name", 56 },
484  { WSANO_DATA, CRYPT_OK, FALSE,
485  "WSANO_DATA: Valid name, no data record of requested type", 56 },
486  { CRYPT_ERROR }, { CRYPT_ERROR }
487  };
488 #define hostErrorInfo socketErrorInfo /* Winsock uses unified error codes */
489 
490 #define TIMEOUT_ERROR WSAETIMEDOUT /* Code for timeout error */
491 
492 #else
493 
494 static const SOCKETERROR_INFO FAR_BSS socketErrorInfo[] = {
495  { EACCES, CRYPT_ERROR_PERMISSION, TRUE,
496  "EACCES: Permission denied", 25 },
497  { EADDRINUSE, CRYPT_OK, TRUE,
498  "EADDRINUSE: Address in use", 26 },
499  { EADDRNOTAVAIL, CRYPT_ERROR_NOTFOUND, TRUE,
500  "EADDRNOTAVAIL: Specified address is not available from the local "
501  "machine", 72 },
502  { EAFNOSUPPORT, CRYPT_ERROR_NOTAVAIL, TRUE,
503  "EAFNOSUPPORT: Address family not supported", 42 },
504  { EALREADY, CRYPT_OK, FALSE,
505  "EALREADY: Connection already in progress", 41 },
506  { EBADF, CRYPT_OK, FALSE,
507  "EBADF: Bad file descriptor", 26 },
508 #if !( defined( __PALMOS__ ) || defined( __SYMBIAN32__ ) )
509  { ECONNABORTED, CRYPT_OK, TRUE,
510  "ECONNABORTED: Software caused connection abort", 46 },
511  { ECONNRESET, CRYPT_OK, TRUE,
512  "ECONNRESET: Connection was forcibly closed by remote host", 57 },
513 #endif /* PalmOS || Symbian OS */
514  { ECONNREFUSED, CRYPT_ERROR_PERMISSION, TRUE,
515  "ECONNREFUSED: Attempt to connect was rejected", 45 },
516  { EINPROGRESS, CRYPT_OK, FALSE,
517  "EINPROGRESS: Operation in progress", 34 },
518  { EINTR, CRYPT_OK, FALSE,
519  "EINTR: Function was interrupted by a signal", 43 },
520  { EIO, CRYPT_OK, TRUE,
521  "EIO: Input/output error", 24 },
522  { EISCONN, CRYPT_OK, FALSE,
523  "EISCONN: Socket is connected", 28 },
524  { EMFILE, CRYPT_OK, FALSE,
525  "EMFILE: Per-process descriptor table is full", 44 },
526 #ifndef __SYMBIAN32__
527  { EMSGSIZE, CRYPT_ERROR_OVERFLOW, FALSE,
528  "EMSGSIZE: Message is too large to be sent all at once", 53 },
529  { ENETUNREACH, CRYPT_OK, FALSE,
530  "ENETUNREACH: No route to the network or host is present", 55 },
531  { ENOBUFS, CRYPT_ERROR_MEMORY, FALSE,
532  "ENOBUFS: Insufficient system resources available to complete the "
533  "call", 69 },
534  { ENODEV, CRYPT_OK, TRUE,
535  "ENODEV: No such device", 22 },
536  { ENOPROTOOPT, CRYPT_OK, TRUE,
537  "ENOPROTOOPT: Protocol not available", 35 },
538  { ENOTCONN, CRYPT_OK, TRUE,
539  "ENOTCONN: Socket is not connected", 33 },
540  { ENOTSOCK, CRYPT_OK, TRUE,
541  "ENOTSOCK: Not a socket", 22 },
542 #endif /* Symbian OS */
543  { EPERM, CRYPT_ERROR_PERMISSION, TRUE,
544  "EPERM: Operation not permitted", 30 },
545  { EPROTOTYPE, CRYPT_ERROR_NOTAVAIL, TRUE,
546  "EPROTOTYPE: Protocol wrong type for socket", 42 },
547  { ETIMEDOUT, CRYPT_ERROR_TIMEOUT, FALSE,
548  "ETIMEDOUT: Function timed out before completion", 47 },
549  { HOST_NOT_FOUND, CRYPT_ERROR_NOTFOUND, TRUE,
550  "HOST_NOT_FOUND: Not an official hostname or alias", 49 },
551 #ifndef __ECOS__
552  { NO_ADDRESS, CRYPT_ERROR_NOTFOUND, TRUE,
553  "NO_ADDRESS: Name is valid but does not have an IP address at the "
554  "name server", 76 },
555 #endif /* __ECOS__ */
556  { TRY_AGAIN, CRYPT_OK, FALSE,
557  "TRY_AGAIN: Local server did not receive a response from an "
558  "authoritative server", 79 },
559  { CRYPT_ERROR }, { CRYPT_ERROR }
560  };
561 
562 #define TIMEOUT_ERROR ETIMEDOUT /* Code for timeout error */
563 
564 static const SOCKETERROR_INFO FAR_BSS hostErrorInfo[] = {
565  { HOST_NOT_FOUND, CRYPT_ERROR_NOTFOUND, TRUE,
566  "HOST_NOT_FOUND: Host not found", 30 },
567 #ifndef __ECOS__
568  { NO_ADDRESS, CRYPT_ERROR_NOTFOUND, TRUE,
569  "NO_ADDRESS: No address record available for this name", 53 },
570 #endif /* __ECOS__ */
571  { NO_DATA, CRYPT_ERROR_NOTFOUND, TRUE,
572  "NO_DATA: Valid name, no data record of requested type", 53 },
573  { TRY_AGAIN, CRYPT_OK, FALSE,
574  "TRY_AGAIN: Local server did not receive a response from an "
575  "authoritative server", 79 },
576  { CRYPT_ERROR }, { CRYPT_ERROR }
577  };
578 #endif /* System-specific socket error codes */
579 
580 /* Get and set the low-level error information from a socket- and host-
581  lookup-based error. In theory under Windows we could also use the
582  Network List Manager to try and get additional diagnostic information
583  but this is only available under Vista and requires playing with COM
584  objects, the significant extra complexity caused by this isn't worth the
585  tiny additional level of granularity that we might gain in reporting
586  errors. In any case the NLM functionality seems primarily intended for
587  interactively obtaining information before any networking actions are
588  initiated ("Do we currently have an Internet connection?") rather than
589  diagnosing problems afterwards ("What went wrong with the attempt to
590  initiate an Internet connection?") */
591 
593 static int mapError( NET_STREAM_INFO *netStream,
594  const int netStreamErrorCode,
595  const BOOLEAN useHostErrorInfo,
596  IN_ERROR int status )
597  {
598  const SOCKETERROR_INFO *errorInfo = \
599  useHostErrorInfo ? hostErrorInfo : socketErrorInfo;
600  const int errorInfoSize = useHostErrorInfo ? \
601  FAILSAFE_ARRAYSIZE( hostErrorInfo, SOCKETERROR_INFO ) : \
602  FAILSAFE_ARRAYSIZE( socketErrorInfo, SOCKETERROR_INFO );
603  int i;
604 
605  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
606  assert( cryptStatusError( status ) );
607 
608  clearErrorString( &netStream->errorInfo );
609  if( netStreamErrorCode == 0 )
610  {
611  /* There's no further error information available, we can't report
612  any more detail */
613  return( status );
614  }
615  for( i = 0; i < errorInfoSize && \
616  errorInfo[ i ].errorCode != CRYPT_ERROR; i++ )
617  {
618  if( errorInfo[ i ].errorCode == netStreamErrorCode )
619  {
620  REQUIRES( errorInfo[ i ].errorStringLength > 16 && \
621  errorInfo[ i ].errorStringLength < 150 );
622  setErrorString( NETSTREAM_ERRINFO, errorInfo[ i ].errorString,
623  errorInfo[ i ].errorStringLength );
624  if( errorInfo[ i ].cryptSpecificCode != CRYPT_OK )
625  {
626  /* There's a more specific error code than the generic one
627  that we've been given available, use that instead */
628  status = errorInfo[ i ].cryptSpecificCode;
629  }
630  if( errorInfo[ i ].isFatal )
631  {
632  /* It's a fatal error, make it persistent for the stream */
633  netStream->persistentStatus = status;
634  }
635  break;
636  }
637  }
638  ENSURES( i < errorInfoSize );
639 
640  return( status );
641  }
642 
644 int getSocketError( NET_STREAM_INFO *netStream,
645  IN_ERROR const int status,
646  OUT_INT_Z int *socketErrorCode )
647  {
648  const int errorCode = getErrorCode();
649 
650  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
651  assert( isWritePtr( socketErrorCode, sizeof( int ) ) );
652 
653  REQUIRES( cryptStatusError( status ) );
654 
655  /* Get the low-level error code and map it to an error string if
656  possible */
657  *socketErrorCode = errorCode;
658 
659  return( mapError( netStream, errorCode, FALSE, status ) );
660  }
661 
663 int getHostError( NET_STREAM_INFO *netStream,
664  IN_ERROR const int status )
665  {
666  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
667 
668  REQUIRES( cryptStatusError( status ) );
669 
670  /* Get the low-level error code and map it to an error string if
671  possible */
672  return( mapError( netStream, getHostErrorCode(), TRUE, status ) );
673  }
674 
675 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
676 int setSocketError( INOUT NET_STREAM_INFO *netStream,
677  IN_BUFFER( errorMessageLength ) const char *errorMessage,
678  IN_LENGTH_ERRORMESSAGE const int errorMessageLength,
679  IN_ERROR const int status, const BOOLEAN isFatal )
680  {
681  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
682  assert( isReadPtr( errorMessage, 16 ) );
683 
684  REQUIRES( errorMessageLength > 16 && \
685  errorMessageLength < MAX_INTLENGTH );
686  REQUIRES( cryptStatusError( status ) );
687 
688  /* Set a cryptlib-supplied socket error message */
689  setErrorString( NETSTREAM_ERRINFO, errorMessage, errorMessageLength );
690  if( isFatal )
691  {
692  /* It's a fatal error, make it persistent for the stream */
693  netStream->persistentStatus = status;
694  }
695  return( status );
696  }
697 
698 /* Some buggy firewall software will block any data transfer attempts made
699  after the initial connection setup, if we're in a situation where this
700  can happen then we check for the presence of a software firewall and
701  report a problem due to the firewall rather than a general networking
702  problem if we find one */
703 
704 #ifdef __WIN32__
705 
706 #define MAX_DRIVERS 1024
707 
709 int checkFirewallError( INOUT NET_STREAM_INFO *netStream )
710  {
711  INSTANCE_HANDLE hPSAPI = NULL_INSTANCE;
712  typedef BOOL ( WINAPI *ENUMDEVICEDRIVERS )( LPVOID *lpImageBase, DWORD cb,
713  LPDWORD lpcbNeeded );
714  typedef DWORD ( WINAPI *GETDEVICEDRIVERBASENAME )( LPVOID ImageBase,
715  LPTSTR lpBaseName,
716  DWORD nSize );
717  ENUMDEVICEDRIVERS pEnumDeviceDrivers;
718  GETDEVICEDRIVERBASENAME pGetDeviceDriverBaseName;
719  LPVOID drivers[ MAX_DRIVERS + 8 ];
720  DWORD cbNeeded;
721  int i;
722 
723  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
724 
725  /* Use the PSAPI library to check for the presence of firewall filter
726  drivers. Since this operation is rarely performed and is only called
727  as part of an error handler in which performance isn't a major factor
728  we load the library on demand each time (which we'd have to do in any
729  case because it's not supported on older systems) rather than using
730  an on-init load as we do for the networking functions */
731  if( ( hPSAPI = DynamicLoad( "psapi.dll" ) ) == NULL_INSTANCE )
732  return( CRYPT_ERROR_TIMEOUT );
733  pEnumDeviceDrivers = ( ENUMDEVICEDRIVERS ) \
734  GetProcAddress( hPSAPI, "EnumDeviceDrivers" );
735  pGetDeviceDriverBaseName = ( GETDEVICEDRIVERBASENAME ) \
736  GetProcAddress( hPSAPI, "GetDeviceDriverBaseNameA" );
737  if( pEnumDeviceDrivers == NULL || \
738  pGetDeviceDriverBaseName == NULL || \
739  !pEnumDeviceDrivers( drivers, MAX_DRIVERS * sizeof( DWORD ),
740  &cbNeeded ) )
741  {
742  DynamicUnload( hPSAPI );
743  return( CRYPT_ERROR_TIMEOUT );
744  }
745 
746  /* Check whether a suspect filter driver is present */
747  for( i = 0; i < cbNeeded / sizeof( DWORD ); i++ )
748  {
749  typedef struct {
750  const char *name;
751  const int nameLen;
752  const BOOLEAN isMcafee;
753  } DRIVER_INFO;
754  static const DRIVER_INFO driverInfoTbl[] = {
755  { "firelm01.sys", 8, TRUE }, /* McAfee Host IPS */
756  { "firehk4x.sys", 8, TRUE }, /* McAfee Host IPS */
757  { "firehk5x.sys", 8, TRUE }, /* McAfee Host IPS */
758  { "fw220.sys", 5, TRUE }, /* McAfee FW */
759  { "mpfirewall.sys", 10, TRUE }, /* McAfee personal FW */
760  { "symtdi.sys", 6, FALSE }, /* Symantec TDI */
761  { "spbbcdrv.sys", 8, FALSE }, /* Norton Personal FW */
762  { NULL, 0, FALSE }, { NULL, 0, FALSE }
763  };
764  char driverName[ 256 + 8 ];
765  int driverNameLen, driverIndex;
766 
767  driverNameLen = pGetDeviceDriverBaseName( drivers[ i ],
768  driverName, 256 );
769  if( driverNameLen <= 0 )
770  continue;
771  for( driverIndex = 0;
772  driverInfoTbl[ driverIndex ].name != NULL && \
773  driverIndex < FAILSAFE_ARRAYSIZE( driverInfoTbl, \
774  DRIVER_INFO );
775  driverIndex++ )
776  {
777  if( driverNameLen >= driverInfoTbl[ driverIndex ].nameLen && \
778  !strnicmp( driverName, driverInfoTbl[ driverIndex ].name,
779  driverInfoTbl[ driverIndex ].nameLen ) )
780  {
781  DynamicUnload( hPSAPI );
784  "Network data transfer blocked, probably due to "
785  "%s firewall software installed on the PC",
786  driverInfoTbl[ driverIndex ].isMcafee ? \
787  "McAfee" : "Symantec/Norton" ) );
788  }
789  }
790  }
791  DynamicUnload( hPSAPI );
792 
793  return( CRYPT_ERROR_TIMEOUT );
794  }
795 #else
796  #define checkFirewallError( netStream ) CRYPT_ERROR_TIMEOUT
797 #endif /* Win32 */
798 
799 #if defined( __BEOS__ ) && !defined( BONE_VERSION )
800 
801 /* BeOS doesn't support checking for anything except readability in select()
802  and only supports one or two socket options so we define our own versions
803  of these functions that no-op out unsupported options */
804 
805 #undef select /* Restore normal select() around the wrapper */
806 
807 static int my_select( int socket_range, struct fd_set *read_bits,
808  struct fd_set *write_bits,
809  struct fd_set *exception_bits,
810  struct timeval *timeout )
811  {
812  /* BeOS doesn't support nonblocking connects, it always waits about a
813  minute for the connect and then times out, so it we get a wait on a
814  connecting socket we report it as being successful by exiting with
815  the fds as set by the caller and a successful return status */
816  if( read_bits != NULL && write_bits != NULL )
817  return( 1 );
818 
819  /* If we're checking for writeability the best that we can do is to
820  always report the socket as writeable. Since the socket is a
821  blocking socket the data will (eventually) get written */
822  if( read_bits == NULL && write_bits != NULL )
823  {
824  if( exception_bits != NULL )
825  FD_ZERO( exception_bits );
826  return( 1 );
827  }
828 
829  /* Since BeOS doesn't support checking for writeability or errors, we
830  have to clear these values before we call select() so that the
831  caller won't find anything still set when we return */
832  if( write_bits != NULL )
833  FD_ZERO( write_bits );
834  if( exception_bits != NULL )
835  FD_ZERO( exception_bits );
836 
837  return( select( socket_range, read_bits, NULL, NULL, timeout ) );
838  }
839 
840 #define select( sockets, readFD, writeFD, exceptFD, timeout ) \
841  my_select( sockets, readFD, writeFD, exceptFD, timeout )
842 
843 static int my_setsockopt( int socket, int level, int option,
844  const void *data, uint size )
845  {
846  if( option != SO_NONBLOCK && option != SO_REUSEADDR )
847  return( 0 );
848  return( setsockopt( socket, level, option, data, size ) );
849  }
850 
851 static int my_getsockopt( int socket, int level, int option,
852  void *data, uint *size )
853  {
854  BYTE buffer[ 8 + 8 ];
855 
856  if( option != SO_ERROR )
857  return( 0 );
858  *( ( int * ) data ) = 0; /* Clear return status */
859 
860  /* It's unclear whether the following setsockopt actually does anything
861  under BeOS or not. If it fails, the alternative below may work */
862 #if 1
863  return( setsockopt( socket, level, option, data, *size ) );
864 #else
865  int count;
866 
867  count = recv( socket, buffer, 0, 0 );
868  printf( "recv( 0 ) = %d, errno = %d.\n", count, errno );
869  if( count < 0 )
870  *( ( int * ) data ) = errno;
871 #endif /* 1 */
872  }
873 #endif /* BeOS without BONE */
874 
875 /****************************************************************************
876 * *
877 * Network Socket Manager *
878 * *
879 ****************************************************************************/
880 
881 /* cryptlib's separation kernel causes some problems with objects that use
882  sockets because it doesn't allow sharing of sockets, which is a problem
883  because the Unix server programming model assumes that a single process
884  will listen on a socket and fork off children to handle incoming
885  connections (in fact the accept() function more or less forces you to do
886  this whether you want to or not). A second problem occurs because when a
887  thread is blocked in an object waiting on a socket there's no way to
888  unblock it apart from killing the thread (actually we could create some
889  sort of lookback socket and wait for it alongside the listen socket in
890  the pre-accept select wait, signalling a shutdown by closing the loopback
891  socket, but this starts to get ugly). In order to work around this we
892  maintain a socket pool that serves two functions:
893 
894  - Maintains a list of sockets that an object is listening on to allow a
895  listening socket to be reused rather than having to listen on a
896  socket and close it as soon as an incoming connection is made in
897  order to switch to the connected socket.
898 
899  - Allows sockets to be closed from another thread, which results in any
900  objects waiting on them being woken up and exiting.
901 
902  For now we limit the socket pool to a maximum of 256 sockets (16 in
903  resource-constrained environments) both as a safety feature to protect
904  against runaway use of sockets in the calling application and because
905  cryptlib was never designed to function as a high-volume server
906  application. If necessary this can be changed to dynamically expand the
907  socket pool in the same way that the kernel dynamically expands its
908  object table. However it's not a good idea to simply remove the
909  restriction entirely (or set it to too high a value) because this can
910  cause problems with excess consumption of kernel resources. For example
911  under Windows opening several tens of thousands of connections will
912  eventually return WSAENOBUFS when the nonpaged pool is exhausted. At
913  this point things start to get problematic because many drivers don't
914  handle the inability to allocate memory very well, and can start to fail
915  and render the whole system unstable. This is a general resource-
916  consumption problem that affects all users of the shared nonpaged pool,
917  but we can at least make sure that we're not the cause of any crashes by
918  limiting our own consumption */
919 
920 #ifdef CONFIG_CONSERVE_MEMORY
921  #define SOCKETPOOL_SIZE 16
922 #else
923  #define SOCKETPOOL_SIZE 256
924 #endif /* CONFIG_CONSERVE_MEMORY */
925 
926 typedef struct {
927  SOCKET netSocket; /* Socket handle */
928  int refCount; /* Reference count for the socket */
929  int iChecksum; /* Family, interface, and port */
930  BYTE iData[ 32 + 8 ]; /* info for server socket */
931  size_t iDataLen;
932  } SOCKET_INFO;
933 
934 static SOCKET_INFO *socketInfo;
935 static const SOCKET_INFO SOCKET_INFO_TEMPLATE = \
936  { INVALID_SOCKET, 0, 0, { 0 }, 0 };
937 
938 /* Initialise and shut down the socket pool */
939 
940 CHECK_RETVAL \
941 static int initSocketPool( void )
942  {
943  int i;
944 
945  /* Allocate and clear the socket pool */
946  if( ( socketInfo = \
947  clAlloc( "initSocketPool", SOCKETPOOL_SIZE * \
948  sizeof( SOCKET_INFO ) ) ) == NULL )
949  return( CRYPT_ERROR_MEMORY );
950  for( i = 0; i < SOCKETPOOL_SIZE; i++ )
951  socketInfo[ i ] = SOCKET_INFO_TEMPLATE;
952 
953  return( CRYPT_OK );
954  }
955 
956 static void endSocketPool( void )
957  {
958  clFree( "endSocketPool", socketInfo );
959  }
960 
961 /* Create/add and remove a socket to/from the pool. The difference between
962  creating and adding a socket is that newSocket() creates and adds a
963  completely new socket while addSocket() adds an externally-created (via
964  accept()) socket */
965 
966 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
967 static int newSocket( OUT SOCKET *newSocketPtr,
968  const struct addrinfo *addrInfoPtr,
969  const BOOLEAN isServer )
970  {
971  SOCKET netSocket;
972  int iCheck = DUMMY_INIT, i, status;
973 
974  assert( isWritePtr( newSocketPtr, sizeof( SOCKET ) ) );
975  assert( isReadPtr( addrInfoPtr, sizeof( struct addrinfo ) ) );
976 
977  /* Clear return value */
978  *newSocketPtr = INVALID_SOCKET;
979 
980  /* Perform any required pre-calculations before we acquire the mutex */
981  if( isServer )
982  {
983  iCheck = checksumData( addrInfoPtr->ai_addr,
984  addrInfoPtr->ai_addrlen );
985  }
986 
987  status = krnlEnterMutex( MUTEX_SOCKETPOOL );
988  if( cryptStatusError( status ) )
989  return( status );
990 
991  /* If this is a server socket (i.e. one bound to a specific interface and
992  port), check to see whether there's already a socket bound here and if
993  there is, return the existing socket rather than creating a new one.
994  This check isn't currently totally foolproof since it compares some
995  nonessential fields that may differ for otherwise identical sockets
996  (it's difficult to do this in a clean manner because the comparison
997  becomes very protocol- and implementation-specific). A workaround
998  would be to check whether the sin_family is AF_INET or AF_INET6 and
999  perform an appropriate situation-specific comparison, but this will
1000  break the nice portability that was added by the RFC 2553
1001  reorganisation of socket functions for IPv6 */
1002  if( isServer )
1003  {
1004  for( i = 0; i < SOCKETPOOL_SIZE; i++ )
1005  {
1006  if( socketInfo[ i ].refCount > 0 && \
1007  socketInfo[ i ].iChecksum == iCheck && \
1008  socketInfo[ i ].iDataLen == addrInfoPtr->ai_addrlen && \
1009  !memcmp( socketInfo[ i ].iData, addrInfoPtr->ai_addr,
1010  addrInfoPtr->ai_addrlen ) )
1011  {
1012  if( socketInfo[ i ].refCount >= 10000 )
1013  {
1015  DEBUG_DIAG(( "Socket in pool has reference count > 10,000" ));
1016  assert( DEBUG_WARN );
1017  return( CRYPT_ERROR_OVERFLOW );
1018  }
1019  ENSURES( socketInfo[ i ].refCount > 0 && \
1020  socketInfo[ i ].refCount < 10000 );
1021  ENSURES( !isBadSocket( socketInfo[ i ].netSocket ) );
1022  socketInfo[ i ].refCount++;
1023  *newSocketPtr = socketInfo[ i ].netSocket;
1025 
1026  /* The socket already exists, don't perform any further
1027  initialisation with it */
1028  return( CRYPT_OK );
1029  }
1030  }
1031  }
1032 
1033  /* Create a new socket entry */
1034  for( i = 0; i < SOCKETPOOL_SIZE; i++ )
1035  {
1036  /* Check whether this is a zombie socket that we couldn't close
1037  earlier, usually due to written data being left in the TCP/IP
1038  stack. As a result it's probably trapped in the TIME_WAIT
1039  state, so we periodically try and close it to free up the
1040  resource */
1041  if( socketInfo[ i ].refCount <= 0 && \
1042  !isBadSocket( socketInfo[ i ].netSocket ) )
1043  {
1044  status = closesocket( socketInfo[ i ].netSocket );
1045  if( !isSocketError( status ) )
1046  socketInfo[ i ] = SOCKET_INFO_TEMPLATE;
1047  }
1048 
1049  if( isBadSocket( socketInfo[ i ].netSocket ) )
1050  break;
1051  }
1052  if( i >= SOCKETPOOL_SIZE )
1053  {
1055  DEBUG_DIAG(( "Tried to add more than %d sockets to socket pool",
1056  SOCKETPOOL_SIZE ));
1057  assert( DEBUG_WARN );
1058  return( CRYPT_ERROR_OVERFLOW ); /* Should never happen */
1059  }
1060  netSocket = socket( addrInfoPtr->ai_family,
1061  addrInfoPtr->ai_socktype, 0 );
1062  if( isBadSocket( netSocket ) )
1063  {
1065  return( CRYPT_ERROR_OPEN );
1066  }
1067  socketInfo[ i ].netSocket = netSocket;
1068  if( isServer )
1069  {
1070  const int addrInfoSize = min( addrInfoPtr->ai_addrlen, 32 );
1071 
1072  /* Remember the details for this socket so that we can detect another
1073  attempt to bind to it */
1074  socketInfo[ i ].iChecksum = checksumData( addrInfoPtr->ai_addr,
1075  addrInfoPtr->ai_addrlen );
1076  memcpy( socketInfo[ i ].iData, addrInfoPtr->ai_addr,
1077  addrInfoSize );
1078  socketInfo[ i ].iDataLen = addrInfoSize;
1079  }
1080  socketInfo[ i ].refCount = 1;
1081  *newSocketPtr = netSocket;
1082 
1083  /* If we're creating a new server socket we can't unlock the socket info
1084  yet because we need to bind it to a port before we do anything else
1085  with it. If we were to unlock the socket info another thread could
1086  perform an accept() on the incompletely set up socket, so we return
1087  with the socket info still locked. When the caller has finished
1088  setting it up they call newSocketDone() to signal that the socket is
1089  now really ready for use */
1090  if( isServer )
1091  return( OK_SPECIAL );
1092 
1094 
1095  return( CRYPT_OK );
1096  }
1097 
1098 static void newSocketDone( void )
1099  {
1100  /* The caller has finished setting up a new server socket, unlock the
1101  socket info to allow others to access it */
1103  }
1104 
1105 CHECK_RETVAL \
1106 static int addSocket( const SOCKET netSocket )
1107  {
1108  int i, status;
1109 
1110  REQUIRES( !isBadSocket( netSocket ) );
1111 
1112  status = krnlEnterMutex( MUTEX_SOCKETPOOL );
1113  if( cryptStatusError( status ) )
1114  return( status );
1115 
1116  /* Add an existing socket entry */
1117  for( i = 0; i < SOCKETPOOL_SIZE; i++ )
1118  {
1119  if( isBadSocket( socketInfo[ i ].netSocket ) )
1120  break;
1121  }
1122  if( i >= SOCKETPOOL_SIZE )
1123  {
1125  DEBUG_DIAG(( "Tried to add more than %d sockets to socket pool",
1126  SOCKETPOOL_SIZE ));
1127  assert( DEBUG_WARN );
1128  return( CRYPT_ERROR_OVERFLOW );
1129  }
1130  socketInfo[ i ] = SOCKET_INFO_TEMPLATE;
1131  socketInfo[ i ].netSocket = netSocket;
1132  socketInfo[ i ].refCount = 1;
1133 
1135 
1136  return( CRYPT_OK );
1137  }
1138 
1139 static void deleteSocket( const SOCKET netSocket )
1140  {
1141  int i, status;
1142 
1143  REQUIRES_V( !isBadSocket( netSocket ) );
1144 
1145  status = krnlEnterMutex( MUTEX_SOCKETPOOL );
1146  if( cryptStatusError( status ) )
1147  return;
1148 
1149  /* Find the entry for this socket in the pool. There may not be one
1150  present if the pool has received a shutdown signal and closed all
1151  network sockets, so if we don't find it we just exit normally */
1152  for( i = 0; i < SOCKETPOOL_SIZE; i++ )
1153  {
1154  if( socketInfo[ i ].netSocket == netSocket )
1155  break;
1156  }
1157  if( i >= SOCKETPOOL_SIZE )
1158  {
1160  return;
1161  }
1162  REQUIRES_V( socketInfo[ i ].refCount > 0 );
1163 
1164  /* Decrement the socket's reference count */
1165  socketInfo[ i ].refCount--;
1166  if( socketInfo[ i ].refCount <= 0 )
1167  {
1168  /* If the reference count has reached zero, close the socket
1169  and delete the pool entry */
1170  status = closesocket( socketInfo[ i ].netSocket );
1171  if( isSocketError( status ) )
1172  {
1173  /* There was a problem closing the socket, mark it as not-
1174  present for matching purposes but keep its entry active so
1175  that we'll periodically try and close it when we search the
1176  socket pool for these slots, and again when we close down */
1177  socketInfo[ i ].iChecksum = 0;
1178  memset( socketInfo[ i ].iData, 0,
1179  sizeof( socketInfo[ i ].iData ) );
1180  socketInfo[ i ].iDataLen = 0;
1181 
1182  DEBUG_DIAG(( "Couldn't close socket pool socket" ));
1183  assert( DEBUG_WARN );
1184  }
1185  else
1186  socketInfo[ i ] = SOCKET_INFO_TEMPLATE;
1187  }
1188 
1190  }
1191 
1192 /* Force all objects waiting on sockets to exit by closing their sockets.
1193  This is the only portable and reliable way to cause them to terminate
1194  since an object waiting on a socket is marked as busy by the cryptlib
1195  kernel, and in fact will be blocked inside the OS out of reach of even
1196  the cryptlib kernel. Alternatively, the user can provide their own
1197  socket externally and close it from the outside, which will unblock the
1198  thread waiting on it.
1199 
1200  A somewhat less drastic alternative to closing the socket is to use
1201  shutdown(), but the behaviour of this is somewhat implementation-specific.
1202  For example under Slowaris 5.x trying to shutdown a listening socket (to
1203  unlock a thread blocking in accept()) returns ENOTCONN, so the shutdown
1204  requires setting up a dummy connection to the socket to be shut down
1205  before it can actually be shut down. Trying to shut down a thread blocked
1206  in connect() is more or less impossible under Slowaris 5.x. Other systems
1207  are more flexible, but there's not enough consistency to rely on this */
1208 
1209 void netSignalShutdown( void )
1210  {
1211  int i, status;
1212 
1213  /* Exactly what to do if we can't acquire the mutex is a bit complicated
1214  because at this point our primary goal is to force all objects to exit
1215  rather than worrying about socket-pool consistency. On the other
1216  hand if another object is currently in the middle of cleaning up and
1217  is holding the socket pool mutex we don't want to stomp on it while
1218  it's doing its cleanup. Since failing to acquire the mutex is a
1219  special-case exception condition, it's not even possible to plan for
1220  this since it's uncertain under which conditions (if ever) this
1221  situation would occur. For now we play it by the book and don't do
1222  anything if we can't acquire the mutex, which is at least
1223  consistent */
1224  status = krnlEnterMutex( MUTEX_SOCKETPOOL );
1225  if( cryptStatusError( status ) )
1226  retIntError_Void();
1227 
1228  /* For each open socket, close it and set its reference count to zero */
1229  for( i = 0; i < SOCKETPOOL_SIZE; i++ )
1230  {
1231  if( !isBadSocket( socketInfo[ i ].netSocket ) )
1232  {
1233  closesocket( socketInfo[ i ].netSocket );
1234  socketInfo[ i ] = SOCKET_INFO_TEMPLATE;
1235  }
1236  }
1237 
1239  }
1240 
1241 /****************************************************************************
1242 * *
1243 * Network Socket Interface *
1244 * *
1245 ****************************************************************************/
1246 
1247 /* Wait for I/O to become possible on a socket. The particular use of
1248  select that we employ here is reasonably optimal under load because we're
1249  only asking select() to monitor a single descriptor. There are a variety
1250  of inefficiencies related to select that fall into either the category of
1251  user <-> kernel copying or of descriptor list scanning. For the first
1252  category, when calling select() the system has to copy an entire list of
1253  descriptors into kernel space and then back out again. Large selects can
1254  potentially contain hundreds or thousands of descriptors, which can in
1255  turn involve allocating memory in the kernel and freeing it on return.
1256  We're only using one so the amount of data to copy is minimal.
1257 
1258  The second category involves scanning the descriptor list, an O(n)
1259  activity. First the kernel has to scan the list to see whether there's
1260  pending activity on a descriptor. If there aren't any descriptors with
1261  activity pending it has to update the descriptor's selinfo entry in the
1262  event that the calling process calls tsleep() (used to handle event-based
1263  process blocking in the kernel) while waiting for activity on the
1264  descriptor. After the select() returns or the process is woken up from a
1265  tsleep() the user process in turn has to scan the list to see which
1266  descriptors the kernel indicated as needing attention. As a result, the
1267  list has to be scanned three times.
1268 
1269  These problems arise because select() (and it's cousin poll()) are
1270  stateless by design so everything has to be recalculated on each call.
1271  After various false starts the kqueue interface is now seen as the best
1272  solution to this problem. However cryptlib's use of only a single
1273  descriptor per select() avoids the need to use system-specific and rather
1274  non-portable interfaces like kqueue (and earlier alternatives like Sun's
1275  /dev/poll, FreeBSD's get_next_event(), and SGI's /dev/imon) */
1276 
1277 typedef enum {
1278  IOWAIT_NONE, /* No I/O wait type */
1279  IOWAIT_READ, /* Wait for read availability */
1280  IOWAIT_WRITE, /* Wait for write availability */
1281  IOWAIT_CONNECT, /* Wait for connect to complete */
1282  IOWAIT_ACCEPT, /* Wait for accept to complete */
1283  IOWAIT_LAST /* Last possible wait type */
1284  } IOWAIT_TYPE;
1285 
1286 CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
1287 static int ioWait( INOUT NET_STREAM_INFO *netStream,
1288  IN_INT_Z const int timeout,
1289  const BOOLEAN previousDataRead,
1290  IN_ENUM( IOWAIT ) const IOWAIT_TYPE type )
1291  {
1292  static const struct {
1293  const int status;
1294  const char *errorString;
1295  } errorInfo[] = {
1296  { CRYPT_ERROR_OPEN, "unknown" },
1297  { CRYPT_ERROR_READ, "read" }, /* IOWAIT_READ */
1298  { CRYPT_ERROR_WRITE, "write" }, /* IOWAIT_WRITE */
1299  { CRYPT_ERROR_OPEN, "connect" }, /* IOWAIT_CONNECT */
1300  { CRYPT_ERROR_OPEN, "accept" }, /* IOWAIT_ACCEPT */
1301  { CRYPT_ERROR_OPEN, "unknown" }, { CRYPT_ERROR_OPEN, "unknown" }
1302  };
1303  MONOTIMER_INFO timerInfo;
1304  struct timeval tv;
1305  fd_set readfds, writefds, exceptfds;
1306  fd_set *readFDPtr = ( type == IOWAIT_READ || \
1307  type == IOWAIT_CONNECT || \
1308  type == IOWAIT_ACCEPT ) ? &readfds : NULL;
1309  fd_set *writeFDPtr = ( type == IOWAIT_WRITE || \
1310  type == IOWAIT_CONNECT ) ? &writefds : NULL;
1311  int selectIterations = 0, status;
1312 
1313  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
1314 
1315  REQUIRES( timeout >= 0 && timeout < MAX_INTLENGTH );
1316  REQUIRES( type > IOWAIT_NONE && type < IOWAIT_LAST );
1317 
1318  /* Set up the information needed to handle timeouts and wait on the
1319  socket. If there's no timeout, we wait at least 5ms on the theory
1320  that it isn't noticeable to the caller but ensures that we at least
1321  get a chance to get anything that may be pending.
1322 
1323  The exact wait time depends on the system, but usually it's quantised
1324  to the system timer quantum. This means that on Unix systems with a
1325  1ms timer resolution the wait time is quantised on a 1ms boundary.
1326  Under Windows NT/2000/XP/Vista it's quantised on a 10ms boundary
1327  (some early NT systems had a granularity ranging from 7.5 - 15ms but
1328  all newer systems use 10ms) and for Win95/98/ME it's quantised on a
1329  55ms boundary. In other words when performing a select() on a Win95
1330  box it'll either return immediately or wait some multiple of 55ms
1331  even with the time set to 1ms.
1332 
1333  In theory we shouldn't have to reset either the fds or the timeval
1334  each time through the loop since we're only waiting on one descriptor
1335  so it's always set and the timeval is a const, however some versions
1336  of Linux can update it if the select fails due to an EINTR (which is
1337  the exact reason why we'd be going through the loop a second time in
1338  the first place) and/or if a file descriptor changes status (e.g. due
1339  to data becoming available) so we have to reset it each time to be on
1340  the safe side. It would actually be nice if the tv value were
1341  updated reliably to reflect how long the select() had to wait since
1342  it'd provide a nice source of entropy for the randomness pool (we
1343  could simulate this by readig a high-res timer before and after the
1344  select() but that would adds a pile of highly system-dependent code
1345  and defeat the intent of making use of using the "free" entropy
1346  that's provided as a side-effect of the select().
1347 
1348  The wait on connect is a slightly special case, the socket will
1349  become writeable if the connect succeeds normally, but both readable
1350  and writeable if there's an error on the socket or if there's data
1351  already waiting on the connection (i.e. it arrives as part of the
1352  connect). It's up to the caller to check for these conditions */
1353  status = setMonoTimer( &timerInfo, timeout );
1354  if( cryptStatusError( status ) )
1355  return( status );
1356  do
1357  {
1358  if( readFDPtr != NULL )
1359  {
1360  FD_ZERO( readFDPtr );
1361  FD_SET( netStream->netSocket, readFDPtr );
1362  }
1363  if( writeFDPtr != NULL )
1364  {
1365  FD_ZERO( writeFDPtr );
1366  FD_SET( netStream->netSocket, writeFDPtr );
1367  }
1368  FD_ZERO( &exceptfds );
1369  FD_SET( netStream->netSocket, &exceptfds );
1370  tv.tv_sec = timeout;
1371  tv.tv_usec = ( timeout <= 0 ) ? 5000 : 0;
1372 
1373  /* See if we can perform the I/O */
1374  status = select( netStream->netSocket + 1, readFDPtr, writeFDPtr,
1375  &exceptfds, &tv );
1376 
1377  /* If there's a problem and it's not something transient like an
1378  interrupted system call, exit. For a transient problem, we just
1379  retry the select until the overall timeout expires */
1380  if( isSocketError( status ) && !isRestartableError() )
1381  {
1382  int dummy;
1383 
1384  return( getSocketError( netStream, errorInfo[ type ].status,
1385  &dummy ) );
1386  }
1387  }
1388  while( isSocketError( status ) && \
1389  !checkMonoTimerExpired( &timerInfo ) && \
1390  selectIterations++ < FAILSAFE_ITERATIONS_MED );
1391  if( selectIterations > FAILSAFE_ITERATIONS_MED )
1392  {
1393  char errorMessage[ 128 + 8 ];
1394  int errorMessageLength;
1395 
1396  /* We've gone through the select loop a suspiciously large number
1397  of times, there's something wrong. In theory we could report
1398  this as a more serious error than a simple timeout since it means
1399  that there's either a bug in our code or a bug in the select()
1400  implementation, but without knowing in advance what caused this
1401  can't-occur condition it's difficult to anticipate the correct
1402  action to take, so all that we do is warn in the debug build */
1403  DEBUG_DIAG(( "select() went through %d iterations without "
1404  "returning data", FAILSAFE_ITERATIONS_MED ));
1405  assert( DEBUG_WARN );
1406  errorMessageLength = sprintf_s( errorMessage, 128,
1407  "select() on %s went through %d "
1408  "iterations without returning a "
1409  "result",
1410  errorInfo[ type ].errorString,
1411  selectIterations );
1412  return( setSocketError( netStream, errorMessage, errorMessageLength,
1414  }
1415 
1416  /* If the wait timed out, either explicitly in the select (status == 0)
1417  or implicitly in the wait loop (isSocketError()), report it as a
1418  select() timeout error */
1419  if( status == 0 || isSocketError( status ) )
1420  {
1421  char errorMessage[ 128 + 8 ];
1422  int errorMessageLength;
1423 
1424  /* If we've already received data from a previous I/O, tell the
1425  caller to use that as the transferred byte count even though we
1426  timed out this time round */
1427  if( previousDataRead )
1428  return( OK_SPECIAL );
1429 
1430  /* If it's a nonblocking wait (usually used as a poll to determine
1431  whether I/O is possible) then a timeout isn't an error (this can
1432  be distinguished from the previous OK_SPECIAL return by whether
1433  previousDataRead is set or not) */
1434  if( timeout <= 0 )
1435  return( OK_SPECIAL );
1436 
1437  /* The select() timed out, exit */
1438  errorMessageLength = sprintf_s( errorMessage, 128,
1439  "Timeout on %s (select()) after %d "
1440  "seconds",
1441  errorInfo[ type ].errorString,
1442  timeout );
1443  return( setSocketError( netStream, errorMessage, errorMessageLength,
1445  }
1446 
1447  /* If there's an exception condition on a socket, exit. This is
1448  implementation-specific, traditionally under Unix this only indicates
1449  the arrival of out-of-band data rather than any real error condition,
1450  but in some cases it can be used to signal errors. In these cases we
1451  have to explicitly check for an exception condition because some
1452  types of errors will result in select() timing out waiting for
1453  readability rather than indicating an error and returning. In
1454  addition for OOB data we could just ignore the notification (which
1455  happens automatically with the default setting of SO_OOBINLINE =
1456  false and a socket owner to receive SIGURG's not set, the OOB data
1457  byte just languishes in a side-buffer), however we shouldn't be
1458  receiving OOB data so we treat that as an error too */
1459  if( FD_ISSET( netStream->netSocket, &exceptfds ) )
1460  {
1461  int socketErrorCode;
1462 
1463  status = getSocketError( netStream, errorInfo[ type ].status,
1464  &socketErrorCode );
1465  if( socketErrorCode == 0 )
1466  {
1467  /* If there's a (supposed) exception condition present but no
1468  error information available then this may be a mis-handled
1469  select() timeout. This can happen with Winsock under
1470  certain circumstances and seems to be related to another
1471  socket-using application performing network I/O at the same
1472  time as we do the select() wait. Non-Winsock cases can occur
1473  because some implementations don't treat a soft timeout as an
1474  error, and at least one (Tandem) returns EINPROGRESS rather
1475  than ETIMEDOUT, so we insert a timeout error code ourselves.
1476  Since we're merely updating the extended internal error
1477  information (we already know what the actual error status
1478  is) we don't need to do anything with the mapError() return
1479  value.
1480 
1481  There is one special-case exception for this and that's when
1482  we're waiting on a nonblocking connect, in which case a
1483  failure to connect due to e.g. an ECONNREFUSED will be
1484  reported as a select() error (this can happen under Winsock
1485  in some cases). Since we can't be sure what the actual
1486  problem is without adding our own timer handling (a fast
1487  reject would be due to an explicit notification like
1488  ECONNREFUSED while a slow reject might be an ENETUNREACH
1489  or something similar) we can't report much more than a
1490  generic open error. A genuine timeout error should have
1491  been caught by the "wait timed out" code above */
1492  if( type == IOWAIT_CONNECT )
1493  {
1494  ( void ) mapError( netStream, 0, FALSE,
1495  CRYPT_ERROR_OPEN );
1496  }
1497  else
1498  {
1499  ( void ) mapError( netStream, TIMEOUT_ERROR, FALSE,
1501  }
1502  }
1503  return( status );
1504  }
1505 
1506  /* The socket is read for reading or writing */
1507  ENSURES( status > 0 );
1508  ENSURES( ( type == IOWAIT_READ && \
1509  FD_ISSET( netStream->netSocket, &readfds ) ) || \
1510  ( type == IOWAIT_WRITE && \
1511  FD_ISSET( netStream->netSocket, &writefds ) ) || \
1512  ( type == IOWAIT_CONNECT && \
1513  ( FD_ISSET( netStream->netSocket, &readfds ) || \
1514  FD_ISSET( netStream->netSocket, &writefds ) ) ) || \
1515  ( type == IOWAIT_ACCEPT ) );
1516  return( CRYPT_OK );
1517  }
1518 
1519 /* Open a connection to a remote server or wait for a connection from a
1520  remote client. The connection-open function performs that most amazing
1521  of all things, the nonblocking connect. This is currently done in order
1522  to allow a shorter timeout than the default fortnight or so but it also
1523  allows for two-phase connects in which we start the connect operation,
1524  perform further processing (e.g. signing and encrypting data prior to
1525  sending it over the connected socket) and then complete the connect
1526  before the first read or write. Currently we just use a wrapper that
1527  performs the two back-to-back as a single operation, so for now it only
1528  functions as a timeout-management mechanism - the high-level API for
1529  this would be a bit difficult to handle since there's no readily-
1530  available facility for handling an interruptible sNetConnect(), the best
1531  option would be to handle it via a complete-connect IOCTL. However since
1532  we've got at least a little time to play with in most cases we could at
1533  least perform a quick entropy poll in the idle interval, if nothing
1534  else */
1535 
1536 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
1537 static int preOpenSocket( INOUT NET_STREAM_INFO *netStream,
1538  IN_BUFFER( hostNameLen ) const char *host,
1539  IN_LENGTH_DNS const int hostNameLen,
1540  IN_PORT const int port )
1541  {
1542  SOCKET netSocket = DUMMY_INIT;
1543  struct addrinfo *addrInfoPtr, *addrInfoCursor;
1544  BOOLEAN nonBlockWarning = FALSE;
1545  int addressCount, status;
1546 
1547  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
1548  assert( isReadPtr( host, hostNameLen ) );
1549 
1550  REQUIRES( hostNameLen > 0 && hostNameLen <= MAX_DNS_SIZE );
1551  REQUIRES( port >= 22 && port < 65536L );
1552 
1553  /* Clear return value */
1554  netStream->netSocket = CRYPT_ERROR;
1555 
1556  /* Set up addressing information */
1557  status = getAddressInfo( netStream, &addrInfoPtr, host, hostNameLen, port,
1558  FALSE );
1559  if( cryptStatusError( status ) )
1560  return( status );
1561  ANALYSER_HINT( addrInfoPtr != NULL );
1562 
1563  /* Create a socket, make it nonblocking, and start the connect to the
1564  remote server, falling back through alternative addresses if the
1565  connect fails. Since this is a nonblocking connect it could still
1566  fail during the second phase where we can no longer try to recover
1567  by falling back to an alternative address, but it's better than just
1568  giving up after the first address that we try (this is actually what
1569  happens in some cases under Vista/Win7 which, like most other IPv6-
1570  enabled systems preferentially tries to provide an IPv6 address for
1571  "localhost" (see the long comment in openServerSocket()) and allows a
1572  connect() to the IPv6 address, but then returns a WSAETIMEDOUT if the
1573  target application is only listening on an IPv4 address) */
1574  for( addrInfoCursor = addrInfoPtr, addressCount = 0;
1575  addrInfoCursor != NULL && addressCount < IP_ADDR_COUNT;
1576  addrInfoCursor = addrInfoCursor->ai_next, addressCount++ )
1577  {
1578  status = newSocket( &netSocket, addrInfoCursor, FALSE );
1579  if( cryptStatusError( status ) )
1580  continue;
1581  setSocketNonblocking( netSocket );
1582  status = connect( netSocket, addrInfoCursor->ai_addr,
1583  addrInfoCursor->ai_addrlen );
1584  nonBlockWarning = isNonblockWarning();
1585  if( status >= 0 || nonBlockWarning )
1586  {
1587  /* We've got a successfully-started connect, exit */
1588  break;
1589  }
1590  deleteSocket( netSocket );
1591  }
1592  if( addressCount >= IP_ADDR_COUNT )
1593  {
1594  /* We went through a suspiciously large number of remote server
1595  addresses without being able to even initiate a connect attempt
1596  to any of them, there's something wrong */
1597  DEBUG_DIAG(( "Iterated through %d server addresses without being "
1598  "able to connect", IP_ADDR_COUNT ));
1599  assert( DEBUG_WARN );
1600  return( mapError( netStream, 0, FALSE, CRYPT_ERROR_OPEN ) );
1601  }
1602  freeAddressInfo( addrInfoPtr );
1603  if( status < 0 && !nonBlockWarning )
1604  {
1605  /* There was an error condition other than a notification that the
1606  operation hasn't completed yet */
1607  return( mapError( netStream, getErrorCode(), FALSE,
1608  CRYPT_ERROR_OPEN ) );
1609  }
1610  if( status == 0 )
1611  {
1612  /* If we're connecting to a local host the connect can complete
1613  immediately rather than returning an in-progress status, in
1614  which case we don't need to do anything else */
1615  netStream->netSocket = netSocket;
1616  return( CRYPT_OK );
1617  }
1618 
1619  /* The connect is in progress, mark the stream as not-quite-ready for
1620  use */
1621 /* netStream->xxx = yyy; */
1622  netStream->netSocket = netSocket;
1623 
1624  return( CRYPT_OK );
1625  }
1626 
1627 CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
1628 static int completeOpen( INOUT NET_STREAM_INFO *netStream )
1629  {
1630  static const int trueValue = 1;
1631  SIZE_TYPE intLength = sizeof( int );
1632  int value, status;
1633 
1634  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
1635  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
1636 
1637  /* Wait around until the connect completes. Some select()s limit the
1638  size of the second count so we set it to a maximum of about a week,
1639  although why anyone would wait around that long (and whether any
1640  network stack would even maintain a SYN_SENT for that amount of time)
1641  is unclear.
1642 
1643  BeOS doesn't allow setting a timeout (that is, it doesn't allow
1644  asynchronous connects), but it hardcodes in a timeout of about a
1645  minute so we get a vaguely similar effect */
1646  status = ioWait( netStream, min( netStream->timeout, 500000L ), FALSE,
1647  IOWAIT_CONNECT );
1648  if( cryptStatusError( status ) )
1649  {
1650  netStream->transportDisconnectFunction( netStream, TRUE );
1651  return( status );
1652  }
1653 
1654  /* The socket is readable or writeable, however this may be because of
1655  an error (it's readable and writeable) or because everything's OK
1656  (it's writeable) or because everything's OK and there's data waiting
1657  (it's readable and writeable), so we have to see what the error
1658  condition is for the socket to determine what's really happening.
1659 
1660  How to best determine all of these conditions is a bit tricky. Other
1661  possibilities include calling recv() with a length of zero bytes
1662  (returns an error if the connect failed), calling connect() again
1663  (fails with EISCONN if the connect succeeded), and calling
1664  getmsg( netSocket, NULL, NULL, &( flags = 0 ) ) (fails with
1665  errno == EAGAIN or EWOULDBLOCK if the only error is that there's
1666  nothing available yet), but these are somewhat implementation-
1667  specific and not consistent across different platforms */
1668  status = getsockopt( netStream->netSocket, SOL_SOCKET, SO_ERROR,
1669  ( void * ) &value, &intLength );
1670  if( status == 0 )
1671  {
1672  /* Berkeley-derived implementation, error is in value variable */
1673  if( value != 0 )
1674  {
1675  status = mapError( netStream, value, FALSE, CRYPT_ERROR_OPEN );
1676  netStream->transportDisconnectFunction( netStream, TRUE );
1677  return( status );
1678  }
1679  }
1680  else
1681  {
1682  /* Slowaris, error is in errno */
1683  if( isSocketError( status ) )
1684  {
1685  int dummy;
1686 
1687  status = getSocketError( netStream, CRYPT_ERROR_OPEN, &dummy );
1688  netStream->transportDisconnectFunction( netStream, TRUE );
1689  return( status );
1690  }
1691  }
1692 
1693  /* Turn off Nagle (since we do our own optimised TCP handling) and make
1694  the socket blocking again. This is necessary because with a
1695  nonblocking socket Winsock will occasionally return 0 bytes from
1696  recv() (a sign that the other side has closed the connection, see the
1697  comment in readSocketFunction()) even though the connection is still
1698  fully open, and in any case there's no real need for a nonblocking
1699  socket since we have select() handling timeouts/blocking for us */
1700  setsockopt( netStream->netSocket, IPPROTO_TCP, TCP_NODELAY,
1701  ( void * ) &trueValue, sizeof( int ) );
1702  setSocketBlocking( netStream->netSocket );
1703 
1704  /* We've completed the connection, mark the stream as ready for use */
1705 /* netStream->xxx = zzz; */
1706  return( CRYPT_OK );
1707  }
1708 
1709 CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
1710 static int openServerSocket( INOUT NET_STREAM_INFO *netStream,
1711  IN_BUFFER_OPT( hostNameLen ) const char *host,
1712  IN_LENGTH_DNS const int hostNameLen,
1713  IN_PORT const int port )
1714  {
1715  SOCKET listenSocket = DUMMY_INIT, netSocket;
1716  SOCKADDR_STORAGE clientAddr;
1717  struct addrinfo *addrInfoPtr, *addrInfoCursor;
1718  static const int trueValue = 1;
1719  static const int falseValue = 0;
1720  SIZE_TYPE clientAddrLen = sizeof( SOCKADDR_STORAGE );
1721  char hostNameBuffer[ MAX_DNS_SIZE + 1 + 8 ];
1722  int addressCount, errorCode = 0, status;
1723 
1724  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
1725  assert( ( host == NULL && hostNameLen == 0 ) || \
1726  isReadPtr( host, hostNameLen ) );
1727 
1728  REQUIRES( ( host == NULL && hostNameLen == 0 ) || \
1729  ( host != NULL && \
1730  hostNameLen > 0 && hostNameLen <= MAX_DNS_SIZE ) );
1731  REQUIRES( port >= 22 && port < 65536L );
1732 
1733  /* Clear return value */
1734  netStream->netSocket = CRYPT_ERROR;
1735 
1736  /* Convert the host name into the null-terminated string required by the
1737  sockets API if necessary */
1738  if( host != NULL )
1739  {
1740  REQUIRES( hostNameLen > 0 && hostNameLen < MAX_DNS_SIZE );
1741 
1742  memcpy( hostNameBuffer, host, hostNameLen );
1743  hostNameBuffer[ hostNameLen ] = '\0';
1744  host = hostNameBuffer;
1745  }
1746 
1747  /* Set up addressing information. If we're not binding to a specified
1748  interface we allow connections on any interface. Note that in
1749  combination with SO_REUSEADDR and old unpatched Unix kernels this
1750  allows port hijacking by another process running on the same machine
1751  that binds to the port with a more specific binding than "any". It
1752  also allows port hijacking under Windows, where the situation is a
1753  bit more complex. Actually it's representative of the problem of the
1754  port-binding situation in general so it's informative to walk through
1755  the issue.
1756 
1757  Windows provides a socket option SO_EXCLUSIVEADDRUSE that can be used
1758  to exclude re-binding to a socket. Unfortunately what this means is
1759  that if this option is set for a socket then the port can't be re-
1760  used right after the socket is closed but only after the connection
1761  is no longer active, where "active" means that it's not only not in
1762  the ESTABLISHED state but also not in the FIN, FIN_WAIT, FIN_WAIT_2,
1763  or LAST_ACK state. However the ability to re-bind to the port at
1764  this point is exactly what SO_REUSEADDR is supposed to allow. In
1765  other words use of SO_EXCLUSIVEADDRUSE is a bit like not setting
1766  SO_REUSEADDR (with a few technical differences based on how
1767  SO_EXCLUSIVEADDRUSE works that aren't important here). So we have
1768  to not only close the socket but wait for the system to send all
1769  buffered data, hang around for data acks, send a disconnect to the
1770  remote system, and wait to get a disconnect back. If the remote
1771  system (or a MITM) advertises a zero-length window or something
1772  similar then the connection can remain "active" (in the sense of
1773  preventing a re-bind, although not necessarily doing anything) more
1774  or less indefinitely.
1775 
1776  This is a nasty situation because while SO_EXCLUSIVEADDRUSE can
1777  prevent local socket-hijacking attacks it opens us up to remote
1778  network-based DoS attacks. In theory if we have complete control
1779  of the application we can use a background thread to wait in a recv()
1780  loop until all data is read after performing a shutdown( SD_SEND )
1781  and if necessary alert the user that something funny is going on, but
1782  since we're a library (and sometimes running as a UI-less service) we
1783  can't really do this.
1784 
1785  Given the choice between allowing a local session-hijack or a remote
1786  DoS, we opt for the session hijack. Since we're using secured
1787  protocols over the socket this isn't nearly as serious as (say) a
1788  socket being used for straight HTTP */
1789  status = getAddressInfo( netStream, &addrInfoPtr, host, hostNameLen,
1790  port, TRUE );
1791  if( cryptStatusError( status ) )
1792  return( status );
1793  ANALYSER_HINT( addrInfoPtr != NULL );
1794 
1795  /* Create a new server socket, falling back through alternative
1796  interfaces if the initial socket creation fails. This may seem less
1797  necessary than for the client-side connect but is required because
1798  getaddrinfo() usually preferentially provides an IPv6 interface even
1799  if there's no IPv6 configured for the system (see the long comment in
1800  getAddressInfo() for more on this), so we have to step through until
1801  we get to an IPv4 interface, or at least one that we can listen on.
1802  Qui habet aures audiendi audiat (the speaker appears to be speaking
1803  metaphorically with 'ears' referring to 'network sockets', latin
1804  having no native term for the latter) */
1805  for( addrInfoCursor = addrInfoPtr, addressCount = 0;
1806  addrInfoCursor != NULL && addressCount < IP_ADDR_COUNT;
1807  addrInfoCursor = addrInfoCursor->ai_next, addressCount++ )
1808  {
1809  SIZE_TYPE valueLen = sizeof( int );
1810  int value;
1811 
1812  status = newSocket( &listenSocket, addrInfoCursor, TRUE );
1813  if( status == CRYPT_OK )
1814  {
1815  /* It's a second thread listening on an existing socket,
1816  we're done */
1817  break;
1818  }
1819  if( status != OK_SPECIAL )
1820  {
1821  /* There was a problem creating the socket, try again with
1822  another interface */
1823  continue;
1824  }
1825 
1826  /* At this point we still have the socket pool locked while we
1827  complete initialisation so we need to call newSocketDone()
1828  before we break out of the loop at any point */
1829 
1830  /* Now we run into some problems with IPv4/IPv6 dual stacks, see
1831  the long comment about this in io/dns.c, in brief what happens
1832  is that if there's a choice between using IPv4 or IPv6, most
1833  systems will use IPv6 first. This is typically encountered
1834  through the first entry in the addrInfo list being an IPv6
1835  interface and the second one being an IPv4 interface, which means
1836  that the default first match will be to an IPv6 interface and not
1837  an IPv4 one. There's an option to listen on both IPv6 and IPv4
1838  interfaces, but whether this is enabled is system-dependent, most
1839  Unix systems enable it but Windows disables it.
1840 
1841  In order for things to work as expected we check for the use of
1842  IPv6 and, if that's being used, check whether the dual-stack
1843  option is enabled (indicated, somewhat counterintuitively, by
1844  having the IPV6_V6ONLY socket option set to FALSE). If it's not
1845  enabled then we explicitly enable it for the socket */
1846  if( addrInfoCursor->ai_family == PF_INET6 && \
1847  getsockopt( listenSocket, IPPROTO_IPV6, IPV6_V6ONLY,
1848  ( char * ) &value, &valueLen ) == 0 && value == 1 )
1849  {
1850  setsockopt( listenSocket, IPPROTO_IPV6, IPV6_V6ONLY,
1851  ( char * ) &falseValue, sizeof( int ) );
1852  }
1853 
1854  /* This is a new socket, set SO_REUSEADDR to avoid TIME_WAIT
1855  problems and prepare to accept connections (nemo surdior est
1856  quam is qui non audiet). Note that BeOS can only bind to one
1857  interface at a time, so if we're binding to INADDR_ANY under
1858  BeOS we actually bind to the first interface that we find */
1859  if( setsockopt( listenSocket, SOL_SOCKET, SO_REUSEADDR,
1860  ( char * ) &trueValue, sizeof( int ) ) || \
1861  bind( listenSocket, addrInfoCursor->ai_addr,
1862  addrInfoCursor->ai_addrlen ) || \
1863  listen( listenSocket, 5 ) )
1864  {
1865  /* Remember the error code now in case there's a later error
1866  in the cleanup functions that overwrites it */
1867  errorCode = getErrorCode();
1868 
1869  /* Clean up so that we can try again, making sure that we have
1870  an appropriate error status set when we continue in case this
1871  was our last iteration through the loop */
1872  deleteSocket( listenSocket );
1873  newSocketDone();
1874  status = CRYPT_ERROR_OPEN;
1875  continue;
1876  }
1877 
1878  /* We've finished initialising the socket, tell the socket pool
1879  manager that it's safe to let others access the pool */
1880  newSocketDone();
1881  status = CRYPT_OK;
1882  break;
1883  }
1884  freeAddressInfo( addrInfoPtr );
1885  if( addressCount >= IP_ADDR_COUNT )
1886  {
1887  /* We went through a suspiciously large number of server addresses
1888  without being able to even initiate a listen attempt on any of
1889  them, there's something wrong */
1890  DEBUG_DIAG(( "Iterated through %d server addresses without being "
1891  "able to listen", IP_ADDR_COUNT ));
1892  assert( DEBUG_WARN );
1893  return( mapError( netStream, 0, FALSE, CRYPT_ERROR_OPEN ) );
1894  }
1895  if( cryptStatusError( status ) )
1896  {
1897  /* There was an error setting up the socket, don't try anything
1898  further */
1899  return( mapError( netStream,
1900  ( errorCode == 0 ) ? getErrorCode() : errorCode,
1901  FALSE, CRYPT_ERROR_OPEN ) );
1902  }
1903 
1904  /* Wait for a connection. At the moment this always waits forever
1905  (actually some select()s limit the size of the second count so we
1906  set it to a maximum of 1 year's worth), but in the future we could
1907  have a separate timeout value for accepting incoming connections to
1908  mirror the connection-wait timeout for outgoing connections.
1909 
1910  Because of the way that accept works, the socket that we eventually
1911  and up with isn't the one that we listen on, but we have to
1912  temporarily make it the one associated with the stream in order for
1913  ioWait() to work */
1914  netStream->netSocket = listenSocket;
1915  status = ioWait( netStream, min( netStream->timeout, 30000000L ), FALSE,
1916  IOWAIT_ACCEPT );
1917  netStream->netSocket = CRYPT_ERROR;
1918  if( cryptStatusError( status ) )
1919  return( status );
1920 
1921  /* We have an incoming connection ready to go, accept it. There's a
1922  potential complication here in that if a client connects and then
1923  immediately sends a RST after the TCP handshake has completed,
1924  ioWait() will return with an indication that there's an incoming
1925  connection ready to go but the following accept(), if it's called
1926  after the RST has arrived, will block waiting for the next incoming
1927  connection. This is rather unlikely in practice, but could occur
1928  as part of a DoS by setting the SO_LINGER time to 0 and disconnecting
1929  immediately. This has the effect of turning the accept() with
1930  timeout into an indefinite-wait accept().
1931 
1932  To get around this we make the socket temporarily non-blocking, so
1933  that accept() returns an error if the client has closed the
1934  connection. The exact error varies, BSD implementations handle the
1935  error internally and return to the accept() while SVR4
1936  implementations return either EPROTO (older, pre-Posix behaviour) or
1937  ECONNABORTED (newer Posix-compliant behaviour, since EPROTO is also
1938  used for other protocol-related errors).
1939 
1940  Since BSD implementations hide the problem they wouldn't normally
1941  return an error, however by temporarily making the socket non-
1942  blocking we force it to return an EWOULDBLOCK if this situation
1943  occurs. Since this could lead to a misleading returned error, we
1944  intercept it and substitute a custom error string. Note that when
1945  we make the listen socket blocking again, we also have to make the
1946  newly-created ephemeral socket blocking, since it inherits its
1947  attributes from the listen socket */
1948  setSocketNonblocking( listenSocket );
1949  netSocket = accept( listenSocket, ( struct sockaddr * ) &clientAddr,
1950  &clientAddrLen );
1951  if( isBadSocket( netSocket ) )
1952  {
1953  if( isNonblockWarning() )
1954  {
1955  status = setSocketError( netStream,
1956  "Remote system closed the connection "
1957  "after completing the TCP handshake",
1958  70, CRYPT_ERROR_OPEN, TRUE );
1959  }
1960  else
1961  {
1962  int dummy;
1963 
1964  status = getSocketError( netStream, CRYPT_ERROR_OPEN, &dummy );
1965  }
1966  setSocketBlocking( listenSocket );
1967  deleteSocket( listenSocket );
1968  return( status );
1969  }
1970  setSocketBlocking( listenSocket );
1971  setSocketBlocking( netSocket );
1972 
1973  /* Get the IP address of the connected client. We could get its full
1974  name, but this can slow down connections because of the time that it
1975  takes to do the lookup and is less authoritative because of potential
1976  spoofing. In any case the caller can still look up the name if they
1977  need it. Since we don't want to abort an entire network connect just
1978  because we can't return the peer's IP address, do don't do anything
1979  with the return value of this function */
1980  ( void ) getNameInfo( ( const struct sockaddr * ) &clientAddr,
1981  clientAddrLen, netStream->clientAddress,
1982  CRYPT_MAX_TEXTSIZE / 2,
1983  &netStream->clientAddressLen,
1984  &netStream->clientPort );
1985 
1986  /* We've got a new connection, add the socket to the pool. Since this
1987  was created externally to the pool we don't use newSocket() to create
1988  a new socket but only add the existing socket */
1989  status = addSocket( netSocket );
1990  if( cryptStatusError( status ) )
1991  {
1992  /* There was a problem adding the new socket, close it and exit.
1993  We don't call deleteSocket() since it wasn't added to the pool,
1994  instead we call closesocket() directly */
1995  closesocket( netSocket );
1996  return( setSocketError( netStream,
1997  "Couldn't add socket to socket pool", 34,
1998  status, FALSE ) );
1999  }
2000  netStream->netSocket = netSocket;
2001  netStream->listenSocket = listenSocket;
2002 
2003  /* Turn off Nagle, since we do our own optimised TCP handling */
2004  setsockopt( netStream->netSocket, IPPROTO_TCP, TCP_NODELAY,
2005  ( void * ) &trueValue, sizeof( int ) );
2006 
2007  return( CRYPT_OK );
2008  }
2009 
2010 CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
2011 static int openSocketFunction( INOUT NET_STREAM_INFO *netStream,
2012  IN_BUFFER_OPT( hostNameLen) const char *hostName,
2013  IN_LENGTH_DNS_Z const int hostNameLen,
2014  IN_PORT const int port )
2015  {
2016  int status;
2017 
2018  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
2019  assert( ( hostName == NULL && hostNameLen == 0 ) || \
2020  isReadPtr( hostName, hostNameLen ) );
2021 
2022  REQUIRES( ( hostName == NULL && hostNameLen == 0 ) || \
2023  ( hostName != NULL && \
2024  hostNameLen > 0 && hostNameLen <= MAX_DNS_SIZE ) );
2025  REQUIRES( port >= 22 && port < 65536L );
2026  REQUIRES( ( netStream->nFlags & STREAM_NFLAG_ISSERVER ) || \
2027  hostName != NULL );
2028 
2029  /* If it's a server stream, open a listening socket */
2030  if( netStream->nFlags & STREAM_NFLAG_ISSERVER )
2031  {
2032  const int savedTimeout = netStream->timeout;
2033 
2034  /* Timeouts for server sockets are actually three-level rather than
2035  the usual two-level model, there's an initial (pre-connect)
2036  timeout while we wait for an incoming connection to arrive, and
2037  then we go to the usual session connect vs. session read/write
2038  timeout mechanism. To handle the pre-connect phase we set an
2039  (effectively infinite) timeout at this point to ensure that the
2040  server always waits forever for an incoming connection to
2041  appear */
2042  netStream->timeout = MAX_INTLENGTH;
2043  status = openServerSocket( netStream, hostName, hostNameLen, port );
2044  netStream->timeout = savedTimeout;
2045  return( status );
2046  }
2047 
2048  ENSURES( hostName != NULL && \
2049  ( hostNameLen > 0 && hostNameLen < MAX_INTLENGTH ) );
2050 
2051  /* It's a client stream, perform a two-part nonblocking open. Currently
2052  the two portions are performed back-to-back, in the future we can
2053  interleave the two and perform general crypto processing (e.g. hash/
2054  MAC context setup for SSL) while the open is completing */
2055  status = preOpenSocket( netStream, hostName, hostNameLen, port );
2056  if( cryptStatusOK( status ) )
2057  status = completeOpen( netStream );
2058  ENSURES( ( cryptStatusError( status ) && \
2059  netStream->netSocket == CRYPT_ERROR ) || \
2060  ( cryptStatusOK( status ) && \
2061  netStream->netSocket != CRYPT_ERROR ) );
2062  return( status );
2063  }
2064 
2065 /* Close a connection. Safely handling closes is extremely difficult due to
2066  a combination of the way TCP/IP (and TCP stacks) work and various bugs
2067  and quirks in implementations. After a close (and particularly if short-
2068  timeout non-blocking writes are used) there can still be data left in
2069  TCP send buffers, and also as unacknowledged segments on the network. At
2070  this point there's no easy way for the TCP stack to know how long it
2071  should hang around trying to get the data out and waiting for acks to
2072  come back. If it doesn't wait long enough, it'll end up discarding
2073  unsent data. If it waits too long, it could potentially wait forever in
2074  the presence of network outages or crashed peers. What's worse, since
2075  the socket is now closed, there's no way to report any problems that may
2076  occur at this point back to the caller.
2077 
2078  We try and handle this with a combination of shutdown() and close(), but
2079  due to implementation bugs/quirks and the TCP stack issues mentioned
2080  above this doesn't work all of the time. The details get very
2081  implementation-specific, for example with glibc the manpage says that
2082  setting SO_LINGER causes shutdown() not to return until queued messages
2083  are sent (which is wrong, and non non-glibc implementations like PHUX and
2084  Solaris specifically point out that only close() is affected), but that
2085  shutdown() discards unsent data. glibc in turn is dependent on the
2086  kernel it's running on top of, under Linux shutdown() returns immediately
2087  but data is still sent regardless of the SO_LINGER setting.
2088 
2089  BSD Net/2 and later (which many stacks are derived from, including non-
2090  Unix systems like OS/2) returned immediately from a close() but still
2091  sent queued data on a best-effort basis. With SO_LINGER set and a zero
2092  timeout the close was abortive (which Linux also implemented starting
2093  with the 2.4 kernel), and with a non-zero timeout it would wait until all
2094  the data was sent, which meant that it could block almost indefinitely
2095  (minutes or even hours, this is the worst-case behaviour mentioned
2096  above). This was finally fixed in 4.4BSD (although a lot of 4.3BSD-
2097  derived stacks ended up with the indefinite-wait behaviour), but even
2098  then there was some confusion as to whether the wait time was in machine-
2099  specific ticks or seconds (Posix finally declared it to be seconds).
2100  Under Winsock, close() simply discards queued data while shutdown() has
2101  the same effect as under Linux, sending enqueued data asynchronously
2102  regardless of the SO_LINGER setting.
2103 
2104  This is a real mess to sort out safely, the best that we can do is to
2105  perform a shutdown() followed later by a close(). Messing with SO_LINGER
2106  is too risky and something like performing an ioWait() doesn't work
2107  either because it just results in whoever initiated the shutdown being
2108  blocked for the I/O wait time, and waiting for a recv() of 0 bytes isn't
2109  safe because the higher-level code may need to read back a shutdown ack
2110  from the other side which a recv() performed at this point would
2111  interfere with. Under Windows we could handle it by waiting for an
2112  FD_CLOSE to be posted but this requires the use of a window handle which
2113  we don't have access to, and which may not even exist for some classes of
2114  applications */
2115 
2116 STDC_NONNULL_ARG( ( 1 ) ) \
2117 static void closeSocketFunction( INOUT NET_STREAM_INFO *netStream,
2118  const BOOLEAN fullDisconnect )
2119  {
2120  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
2121 
2122  /* If it's a partial disconnect, close only the send side of the channel.
2123  The send-side close can help with ensuring that all data queued for
2124  transmission is sent */
2125  if( !fullDisconnect )
2126  {
2127  if( netStream->netSocket != CRYPT_ERROR )
2128  shutdown( netStream->netSocket, SHUT_WR );
2129  return;
2130  }
2131 
2132  /* If it's an open-on-demand HTTP stream then the socket isn't
2133  necessarily open even if the stream was successfully connected so we
2134  only close it if necessary. It's easier handling it at this level
2135  than expecting the caller to distinguish between an opened-stream-but-
2136  not-opened-socket and a conventional open stream */
2137  if( netStream->netSocket != CRYPT_ERROR )
2138  deleteSocket( netStream->netSocket );
2139  if( netStream->listenSocket != CRYPT_ERROR )
2140  deleteSocket( netStream->listenSocket );
2141  netStream->netSocket = netStream->listenSocket = CRYPT_ERROR;
2142  }
2143 
2144 /* Check an externally-supplied socket to make sure that it's set up as
2145  required by cryptlib. See the long comment in tcp.h about the numerous
2146  problems that this theoretically simple operation actually causes */
2147 
2148 CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
2149 static int checkSocketFunction( INOUT NET_STREAM_INFO *netStream )
2150  {
2151  int value;
2152 
2153  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
2154 
2155  /* Check that we've been passed a valid network socket, and that it's
2156  blocking socket */
2157  getSocketNonblockingStatus( netStream->netSocket, value );
2158  if( isSocketError( value ) )
2159  {
2160  int dummy;
2161 
2162  return( getSocketError( netStream, CRYPT_ARGERROR_NUM1, &dummy ) );
2163  }
2164  if( value )
2165  {
2166  return( setSocketError( netStream, "Socket is non-blocking", 22,
2168  }
2169  return( CRYPT_OK );
2170  }
2171 
2172 /* Read and write data from and to a socket. Because data can appear in
2173  bits and pieces when reading we have to implement timeout handling at two
2174  levels, once via ioWait() and a second time as an overall timeout. If we
2175  only used ioWait() this could potentially stretch the overall timeout to
2176  (length * timeout) so we also perform a time check that leads to a worst-
2177  case timeout of (timeout-1 + timeout). This is the same as the
2178  implementation of SO_SND/RCVTIMEO in Berkeley-derived implementations,
2179  where the timeout value is actually an interval timer rather than a
2180  absolute timer.
2181 
2182  In addition to the standard stream-based timeout behaviour we can also be
2183  called with flags specifying explicit blocking behaviour (for a read
2184  where we know that we're expecting a certain amount of data) or explicit
2185  nonblocking behaviour (for speculative reads to fill a buffer). These
2186  flags are used by the buffered-read routines, which try and speculatively
2187  read as much data as possible to avoid the many small reads required by
2188  some protocols. We don't do the blocking read using MSG_WAITALL since
2189  this can (potentially) block forever if not all of the data arrives.
2190 
2191  Finally, if we're performing an explicit blocking read (which is usually
2192  done when we're expecting a predetermined number of bytes) we dynamically
2193  adjust the timeout so that if data is streaming in at a steady rate we
2194  don't abort the read just because there's more data to transfer than we
2195  can manage in the originally specified timeout interval. This is
2196  especially useful when transferring large data amounts, for which a one-
2197  size-fits-all fixed timeout doesn't accurately reflect the amount of time
2198  required to transfer the full data amount.
2199 
2200  Handling of return values is as follows:
2201 
2202  timeout byteCount return
2203  ------- --------- ------
2204  0 0 0
2205  0 > 0 byteCount
2206  > 0 0 CRYPT_ERROR_TIMEOUT
2207  > 0 > 0 byteCount
2208 
2209  At the sread()/swrite() level if the partial-read/write flags aren't set
2210  for the stream, a byteCount < length is also converted to a
2211  CRYPTO_ERROR_TIMEOUT */
2212 
2213 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
2214 static int readSocketFunction( INOUT STREAM *stream,
2215  OUT_BUFFER( maxLength, *length ) BYTE *buffer,
2216  IN_LENGTH const int maxLength,
2217  OUT_LENGTH int *length,
2218  IN_FLAGS_Z( TRANSPORT ) const int flags )
2219  {
2220  NET_STREAM_INFO *netStream = ( NET_STREAM_INFO * ) stream->netStreamInfo;
2221  MONOTIMER_INFO timerInfo;
2222  BYTE *bufPtr = buffer;
2223  const int timeout = ( flags & TRANSPORT_FLAG_NONBLOCKING ) ? 0 : \
2224  ( flags & TRANSPORT_FLAG_BLOCKING ) ? \
2225  max( 30, netStream->timeout ) : netStream->timeout;
2226  int bytesToRead, byteCount = 0, iterationCount, status;
2227 
2228  assert( isWritePtr( stream, sizeof( STREAM ) ) );
2229  assert( isWritePtr( buffer, maxLength ) );
2230  assert( isWritePtr( length, sizeof( int ) ) );
2231 
2232  REQUIRES_S( stream->type == STREAM_TYPE_NETWORK );
2233  REQUIRES_S( maxLength > 0 && maxLength < MAX_INTLENGTH );
2234  REQUIRES_S( ( ( flags & TRANSPORT_FLAG_NONBLOCKING ) && \
2235  timeout == 0 ) || \
2236  ( !( flags & TRANSPORT_FLAG_NONBLOCKING ) && \
2237  timeout >= 0 && timeout < MAX_INTLENGTH ) );
2238  REQUIRES_S( flags == TRANSPORT_FLAG_NONE || \
2239  flags == TRANSPORT_FLAG_NONBLOCKING || \
2240  flags == TRANSPORT_FLAG_BLOCKING );
2241 
2242  /* Clear return value */
2243  *length = 0;
2244 
2245  status = setMonoTimer( &timerInfo, timeout );
2246  if( cryptStatusError( status ) )
2247  return( status );
2248  for( bytesToRead = maxLength, iterationCount = 0;
2249  bytesToRead > 0 && \
2250  ( timeout <= 0 || !checkMonoTimerExpired( &timerInfo ) ) && \
2251  iterationCount < FAILSAFE_ITERATIONS_MAX;
2252  iterationCount++ )
2253  {
2254  int bytesRead;
2255 
2256  /* Wait for data to become available */
2257  status = ioWait( netStream, timeout,
2258  ( byteCount > 0 ) ? TRUE : FALSE, IOWAIT_READ );
2259  if( status == OK_SPECIAL )
2260  {
2261  /* We got a timeout but either there's already data present from
2262  a previous read or it's a nonblocking wait, so this isn't an
2263  error */
2264  if( byteCount > 0 )
2265  *length = byteCount;
2266  return( CRYPT_OK );
2267  }
2268  if( cryptStatusError( status ) )
2269  {
2270  /* Some buggy firewall software will block any data transfer
2271  attempts made after the initial connection setup
2272  (specifically they'll allow the initial SYN/SYN/ACK to
2273  establish connection state but then block any further
2274  information from being transferred), to handle this we
2275  check whether we get a timeout on the first read and if we
2276  do then we check for the presence of the software firewall,
2277  reporting a problem due to the firewall rather than a
2278  general networking problem if we find one */
2279  if( status == CRYPT_ERROR_TIMEOUT && \
2280  ( netStream->nFlags & STREAM_NFLAG_FIRSTREADOK ) )
2281  {
2282  return( checkFirewallError( netStream ) );
2283  }
2284  return( status );
2285  }
2286 
2287  /* We've got data waiting, read it */
2288  bytesRead = recv( netStream->netSocket, bufPtr, bytesToRead, 0 );
2289  if( isSocketError( bytesRead ) )
2290  {
2291  int dummy;
2292 
2293  /* If it's a restartable read due to something like an
2294  interrupted system call, retry the read */
2295  if( isRestartableError() )
2296  {
2297  assert( !"Restartable read, recv() indicated error" );
2298  continue;
2299  }
2300 
2301  /* There was a problem with the read */
2302  return( getSocketError( netStream, CRYPT_ERROR_READ, &dummy ) );
2303  }
2304  if( bytesRead <= 0 )
2305  {
2306  /* Under some odd circumstances (Winsock bugs when using non-
2307  blocking sockets, or calling select() with a timeout of 0),
2308  recv() can return zero bytes without an EOF condition being
2309  present, even though it should return an error status if this
2310  happens (this could also happen under very old SysV
2311  implementations using O_NDELAY for nonblocking I/O). To try
2312  and catch this we check for a restartable read due to
2313  something like an interrupted system call and retry the read
2314  if it is. Unfortunately this doesn't catch the Winsock zero-
2315  delay bug but it may catch problems in other implementations.
2316 
2317  Unfortunately this doesn't work under all circumstances
2318  either. If the connection is genuinely closed select() will
2319  return a data-available status and recv() will return zero,
2320  both without changing errno. If the last status set in errno
2321  matches the isRestartableError() check, the code will loop
2322  forever. Because of this we can't use the following check,
2323  although since it doesn't catch the Winsock zero-delay bug
2324  anyway it's probably no big deal.
2325 
2326  The real culprit here is the design flaw in recv(), which
2327  uses a valid bytes-received value to indicate an out-of-band
2328  condition that should be reported via an error code ("There's
2329  nowt wrong with owt what mitherin clutterbucks don't barley
2330  grummit") */
2331 #if 0 /* See above comment */
2332  if( isRestartableError() )
2333  {
2334  assert( !"Restartable read, recv() indicated no error" );
2335  continue;
2336  }
2337 #endif /* 0 */
2338 
2339  /* Once this Winsock bug hits, we've fallen and can't get up any
2340  more. WSAGetLastError() reports no error, select() reports
2341  data available for reading, and recv() reports zero bytes
2342  read. If the following is used, the code will loop endlessly
2343  (or at least until the loop iteration watchdog triggers)
2344  waiting for data that can never be read */
2345 #if 0 /* See above comment */
2346  getSocketError( stream, CRYPT_ERROR_READ, &dummy );
2347  status = ioWait( stream, 0, 0, IOWAIT_READ );
2348  if( cryptStatusOK( status ) )
2349  continue;
2350 #endif /* 0 */
2351 
2352  /* "It said its piece, and then it sodded off" - Baldrick,
2353  Blackadder's Christmas Carol */
2354  bytesToRead = 0; /* Force exit from loop */
2355  continue;
2356  }
2357  bufPtr += bytesRead;
2358  bytesToRead -= bytesRead;
2359  byteCount += bytesRead;
2360  ENSURES_S( bytesToRead >= 0 && bytesToRead < MAX_INTLENGTH );
2361  ENSURES_S( byteCount > 0 && byteCount < MAX_INTLENGTH );
2362 
2363  /* Remember that we've got some data, used for error diagnosis (see
2364  the long comment above) */
2365  netStream->nFlags |= STREAM_NFLAG_FIRSTREADOK;
2366 
2367  /* If this is a blocking read and we've been moving data at a
2368  reasonable rate (~1K/s) and we're about to time out, adjust the
2369  timeout to give us a bit more time. This is an adaptive process
2370  that grants us more time for the read if data is flowing at
2371  a reasonable rate, but ensures that we don't hang around forever
2372  if data is trickling in at a few bytes a second */
2373  if( flags & TRANSPORT_FLAG_BLOCKING )
2374  {
2375  ENSURES( timeout > 0 );
2376 
2377  /* If the timer expiry is imminent but data is still flowing in,
2378  extend the timing duration to allow for further data to
2379  arrive. Because of the minimum flow-rate limit that's
2380  imposed above this is unlikely to be subject to much of a DoS
2381  problem (at worst an attacker can limit us to reading data
2382  at 1K/s, which means 16s for SSL/TLS packets and 32s for SSH
2383  packets), but to make things a bit less predictable we dither
2384  the timeout a bit */
2385  if( ( byteCount / timeout ) >= 1000 && \
2386  checkMonoTimerExpiryImminent( &timerInfo, 5 ) )
2387  {
2388  extendMonoTimer( &timerInfo,
2389  ( getRandomInteger() % 5 ) + 2 );
2390  }
2391  }
2392  }
2393  ENSURES_S( iterationCount < FAILSAFE_ITERATIONS_MAX );
2394  if( maxLength > 0 && byteCount <= 0 )
2395  {
2396  /* We didn't get anything because the other side closed the
2397  connection. We report this is a read-complete status rather than
2398  a read error since it isn't necessarily a real error */
2399  return( setSocketError( netStream,
2400  "No data was read because the remote system "
2401  "closed the connection (recv() == 0)", 78,
2403  }
2404  *length = byteCount;
2405 
2406  return( CRYPT_OK );
2407  }
2408 
2409 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
2410 static int writeSocketFunction( INOUT STREAM *stream,
2411  IN_BUFFER( length ) const BYTE *buffer,
2412  IN_LENGTH const int maxLength,
2413  OUT_LENGTH_Z int *length,
2414  IN_FLAGS_Z( TRANSPORT ) const int flags )
2415  {
2416  NET_STREAM_INFO *netStream = ( NET_STREAM_INFO * ) stream->netStreamInfo;
2417  MONOTIMER_INFO timerInfo;
2418  const BYTE *bufPtr = buffer;
2419  const int timeout = ( flags & TRANSPORT_FLAG_NONBLOCKING ) ? 0 : \
2420  ( flags & TRANSPORT_FLAG_BLOCKING ) ? \
2421  max( 30, netStream->timeout ) : netStream->timeout;
2422  int bytesToWrite, byteCount = 0, iterationCount, status;
2423 
2424  assert( isWritePtr( stream, sizeof( STREAM ) ) );
2425  assert( isReadPtr( buffer, maxLength ) );
2426  assert( isWritePtr( length, sizeof( int ) ) );
2427 
2428  REQUIRES_S( stream->type == STREAM_TYPE_NETWORK );
2429  REQUIRES_S( maxLength > 0 && maxLength < MAX_INTLENGTH );
2430  REQUIRES_S( ( ( flags & TRANSPORT_FLAG_NONBLOCKING ) && \
2431  timeout == 0 ) || \
2432  ( !( flags & TRANSPORT_FLAG_NONBLOCKING ) && \
2433  timeout >= 0 && timeout < MAX_INTLENGTH ) );
2434  REQUIRES_S( flags == TRANSPORT_FLAG_NONE || \
2435  flags == TRANSPORT_FLAG_NONBLOCKING || \
2436  flags == TRANSPORT_FLAG_BLOCKING );
2437 
2438  /* Clear return value */
2439  *length = 0;
2440 
2441  /* Send data to the remote system. As with the receive-data code we
2442  have to work around a large number of quirks and socket
2443  implementation bugs, although most of the systems that exhibited
2444  these are now extinct or close to it. Some very old Winsock stacks
2445  (Win3.x and early Win95 era) would almost always indicate that a
2446  socket was writeable even when it wasn't. Even older (mid-1980s)
2447  Berkeley-derived implementations could return EWOULDBLOCK on a
2448  blocking socket if they couldn't get required mbufs so that even if
2449  select() indicated that the socket was writeable, an actual attempt
2450  to write would return an error since there were no mbufs available.
2451  Under Win95 select() can fail to block on a non-blocking socket, so
2452  that the send() returns EWOULDBLOCK. One possible reason (related to
2453  the mbuf problem) is that another thread may grab memory between the
2454  select() and the send() so that there's no buffer space available
2455  when send() needs it, although this should really return WSAENOBUFS
2456  rather than WSAEWOULDBLOCK. There's also a known bug in Win95 (and
2457  possibly Win98 as well, Q177346) under which a select() indicates
2458  writeability but send() returns EWOULDBLOCK. Another select()
2459  executed after the failed send() then causes select() to suddenly
2460  realise that the socket is non-writeable (accidit in puncto, quod
2461  non seperatur in anno). Finally, in some cases send() can return an
2462  error but WSAGetLastError() indicates that there's no error, so we
2463  treat it as noise and try again */
2464  status = setMonoTimer( &timerInfo, timeout );
2465  if( cryptStatusError( status ) )
2466  return( status );
2467  for( bytesToWrite = maxLength, iterationCount = 0;
2468  bytesToWrite > 0 && \
2469  ( timeout <= 0 || !checkMonoTimerExpired( &timerInfo ) ) && \
2470  iterationCount < FAILSAFE_ITERATIONS_MAX;
2471  iterationCount++ )
2472  {
2473  int bytesWritten;
2474 
2475  /* Wait for the socket to become available */
2476  status = ioWait( netStream, timeout,
2477  ( byteCount > 0 ) ? TRUE : FALSE, IOWAIT_WRITE );
2478  if( status == OK_SPECIAL )
2479  {
2480  /* We got a timeout but either there's already data present from
2481  a previous read or it's a nonblocking wait, so this isn't an
2482  error */
2483  if( byteCount > 0 )
2484  {
2485  *length = byteCount;
2486 
2487  return( CRYPT_OK );
2488  }
2489  return( CRYPT_OK );
2490  }
2491  if( cryptStatusError( status ) )
2492  return( status );
2493 
2494  /* Write the data */
2495  bytesWritten = send( netStream->netSocket, bufPtr, bytesToWrite,
2496  MSG_NOSIGNAL );
2497  if( isSocketError( bytesWritten ) )
2498  {
2499  int dummy;
2500 
2501  /* If it's a restartable write due to something like an
2502  interrupted system call (or a sockets bug), retry the
2503  write */
2504  if( isRestartableError() )
2505  {
2506  assert( !"Restartable write, send() indicated error" );
2507  continue;
2508  }
2509 
2510 #ifdef __WINDOWS__
2511  /* If it's a Winsock bug, treat it as a restartable write */
2512  if( WSAGetLastError() < WSABASEERR )
2513  {
2514  assert( !"send() failed but WSAGetLastError() indicated no "
2515  "error, ignoring" );
2516  continue;
2517  }
2518 #endif /* __WINDOWS__ */
2519 
2520  /* There was a problem with the write */
2521  return( getSocketError( netStream, CRYPT_ERROR_WRITE, &dummy ) );
2522  }
2523  bufPtr += bytesWritten;
2524  bytesToWrite -= bytesWritten;
2525  byteCount += bytesWritten;
2526  ENSURES_S( bytesToWrite >= 0 && bytesToWrite < MAX_INTLENGTH );
2527  ENSURES_S( byteCount > 0 && byteCount < MAX_INTLENGTH );
2528  }
2529  ENSURES_S( iterationCount < FAILSAFE_ITERATIONS_MAX );
2530  *length = byteCount;
2531 
2532  return( CRYPT_OK );
2533  }
2534 
2535 STDC_NONNULL_ARG( ( 1 ) ) \
2536 void setAccessMethodTCP( INOUT NET_STREAM_INFO *netStream )
2537  {
2538  assert( isWritePtr( netStream, sizeof( NET_STREAM_INFO ) ) );
2539 
2540  /* Set the access method pointers */
2541  netStream->transportConnectFunction = openSocketFunction;
2542  netStream->transportDisconnectFunction = closeSocketFunction;
2543  netStream->transportReadFunction = readSocketFunction;
2544  netStream->transportWriteFunction = writeSocketFunction;
2545  netStream->transportOKFunction = transportOKFunction;
2546  netStream->transportCheckFunction = checkSocketFunction;
2547  }
2548 #endif /* USE_TCP */