cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
ssl.c
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * cryptlib SSL/TLS Routines *
4 * Copyright Peter Gutmann 1998-2011 *
5 * *
6 ****************************************************************************/
7 
8 #include "cryptlib.h"
9 #include "test/test.h"
10 
11 #if defined( __MVS__ ) || defined( __VMCMS__ )
12  /* Suspend conversion of literals to ASCII. */
13  #pragma convlit( suspend )
14 #endif /* IBM big iron */
15 #if defined( __ILEC400__ )
16  #pragma convert( 0 )
17 #endif /* IBM medium iron */
18 
19 /* SSL/TLS gets a bit complicated because in the presence of the session
20  cache every session after the first one will be a resumed session. To
21  deal with this, the VC++ 6 debug build disables the client-side session
22  cache, while every other version just ends up going through a series
23  of session resumes */
24 
25 #if defined( __WINDOWS__ ) && defined( _MSC_VER ) && ( _MSC_VER == 1200 ) && \
26  !defined( NDEBUG ) && 1
27  #define NO_SESSION_CACHE
28 #endif /* VC++ 6.0 debug build */
29 
30 /* We can run the SSL/TLS self-test with a large variety of options, rather
31  than using dozens of boolean option flags to control them all we define
32  various test classes that exercise each option type.
33 
34  Two of the tests aren't run as part of the normal self-test since their
35  use of random threads results in somewhat nondeterministic behaviour that
36  would require extensive extra locking to resolve. SSL_TEST_DUALTHREAD
37  starts the SSL server with one thread and has the server session return
38  control to the caller for the password check. The initial server thread
39  then exits and a second thread takes over for the rest of the connect.
40 
41  SSL_TEST_MULTITHREAD is just a multithreaded client and server test.
42  This is even more nondeterministic, with thread pileups possible due to
43  the lack of extensive locking on the client side.
44 
45  For SSL_TEST_CLIENTCERT against the Windows interop server, the test
46  server client-auth key needs to be converted to PKCS #15 format since it
47  uses a too-short password in the original Microsoft-provided file. To do
48  this, in mechs/mech_drv.c, initDSP() add the following kludge:
49 
50  *( ( int * ) &keyLength ) = 1;
51 
52  and then use the following code:
53 
54  {
55  CRYPT_KEYSET cryptKeyset;
56  CRYPT_CONTEXT cryptContext;
57 
58  cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
59  "r:/woodgrove.p12", CRYPT_KEYOPT_READONLY );
60  cryptGetPrivateKey( cryptKeyset, &cryptContext, CRYPT_KEYID_NAME,
61  TEXT( "test" ), TEXT( "11" ) );
62  cryptKeysetClose( cryptKeyset );
63  cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_FILE,
64  TEST_PRIVKEY_TMP_FILE, CRYPT_KEYOPT_CREATE );
65  cryptAddPrivateKey( cryptKeyset, cryptContext, TEST_PRIVKEY_PASSWORD );
66  cryptKeysetClose( cryptKeyset );
67  cryptDestroyContext( cryptContext );
68  } */
69 
70 typedef enum {
71  SSL_TEST_NORMAL, /* Standard SSL/TLS test */
72  SSL_TEST_BULKTRANSER, /* Bulk data transfer */
73  SSL_TEST_CLIENTCERT, /* User auth.with client certificate */
74  SSL_TEST_PSK, /* User auth.with shared key */
75  SSL_TEST_PSK_SVRONLY, /* Client = no PSK, server = TLS-PSK */
76  SSL_TEST_PSK_CLIONLY, /* Client = TLS-PSK, server = no PSK */
77  SSL_TEST_PSK_WRONGKEY, /* User auth.with incorrect shared key */
78  SSL_TEST_ECC, /* Use ECC instead of RSA/DH */
79  SSL_TEST_ECC_P384, /* Use ECC P384 instead of P256 */
80  SSL_TEST_STARTTLS, /* STARTTLS/STLS/AUTH TLS */
81  SSL_TEST_RESUME, /* Session resumption */
82  SSL_TEST_DUALTHREAD, /* Two-phase connect via different threads */
83  SSL_TEST_MULTITHREAD /* Multiple server threads */
84  } SSL_TEST_TYPE;
85 
86 #if defined( TEST_SESSION ) || defined( TEST_SESSION_LOOPBACK )
87 
88 /****************************************************************************
89 * *
90 * SSL/TLS Routines Test *
91 * *
92 ****************************************************************************/
93 
94 /* If we're using local sockets, we have to pull in the winsock defines */
95 
96 #if defined( __WINDOWS__ ) && !defined( _WIN32_WCE )
97  #include <winsock.h>
98 #endif /* __WINDOWS__ && !_WIN32_WCE */
99 
100 /* There are various servers running that we can use for testing, the
101  following remapping allows us to switch between them. Notes:
102 
103  Server 1: Local loopback.
104  Server 2-4: Generic test servers at amazon.com, paypal.com, redhat.com.
105  There have to be three distinct servers in order to force a
106  full handshake rather than just pulling a previous session out
107  of the session cache.
108  Server 5: ~40K data returned.
109  Server 6: Sends zero-length blocks (actually a POP server).
110  Server 7: Novell GroupWise, requires CRYPT_OPTION_CERT_COMPLIANCELEVEL =
111  CRYPT_COMPLIANCELEVEL_OBLIVIOUS due to b0rken certs.
112  Server 8: (Causes MAC failure during handshake when called from PMail,
113  works OK when called here).
114  Server 9: Can only do crippled crypto (not even conventional crippled
115  crypto but RC4-56) and instead of sending an alert for this
116  just drops the connection (this may be caused by the NetApp
117  NetCache it's using). This site is also running an Apache
118  server that claims it's optimised for MSIE, and that the page
119  won't work properly for non-MSIE browsers. The mind
120  boggles...
121  Server 10: Server ("Hitachi Web Server 02-00") can only do SSL, when
122  cryptlib is set to perform a TLS handshake (i.e. cryptlib is
123  told to expect TLS but falls back to SSL), goes through the
124  full handshake, then returns a handshake failure alert. The
125  same occurs for other apps (e.g. MSIE) when TLS is enabled.
126  Server 11: Buggy older IIS that can only do crippled crypto and drops
127  the connection as soon as it sees the client hello
128  advertising strong crypto only.
129  Server 12: Newer IIS (certificate is actually for akamai.net, so the SSL
130  may not be Microsoft's at all).
131  Server 13: IBM (Websphere?).
132  Server 14: Server is running TLS with SSL disabled, drops connection
133  when it sees an SSL handshake. MSIE in its default config
134  (TLS disabled) can't connect to this server.
135  Server 15: GnuTLS.
136  Server 16: GnuTLS test server with TLS 1.1.
137  Server 17: Can only do SSLv2, server hangs when sent an SSLv3 handshake.
138  Server 18: Can't handle TLS 1.1 handshake (drops connection).
139  Server 19: Can't handle TLS 1.1 handshake (drops connection). Both of
140  these servers are sitting behind NetApp NetCaches (see also
141  server #9), which could be the cause of the problem.
142  Server 20: Generic OpenSSL server.
143  Server 21: Crippled crypto using NS Server 3.6.
144  Server 22: Apache with Thawte certs, requires
145  CRYPT_OPTION_CERT_COMPLIANCELEVEL =
146  CRYPT_COMPLIANCELEVEL_REDUCED due to b0rken certs.
147  Server 23: Supports TLS-ext, max-fragment-size extension, session
148  tickets, TLS 1.2, and assorted other odds and ends, but
149  not ECC or GSM, reports info on connect in handy text
150  format. Will also perform client-auth verification if the
151  client sends a client-auth message, accepting any cert and
152  using it to verify the handshake-data signature.
153  Server 24: GnuTLS server supporting all sorts of oddities (PGP certs,
154  SRP, compression, TLS-ext, and others, see
155  http://www.gnu.org/software/gnutls/server.html for details),
156  reports info on connect in HTML table format. Note that this
157  server claims to support TLS 1.2 but returns a TLS 1.1 server
158  hello in response to a TLS 1.2 handshake request for several
159  different TLS 1.2 client implementations.
160  Server 25: Supports SNI extension and reports info on connect.
161  Server 26: Certicom server using ECDSA P256. Returns a server cert with
162  a bizarro X9.62 OID with implied sub-parameters that can't be
163  handled (at least in a sane manner) by the AlgoID read code.
164  Server 27: RedHat server using NSS for ECC support for ECDSA P256. This
165  server doesn't support any non-ECC suites, making it useful
166  for testing handling of the ECC-only case.
167  Server 28: Certicom umbrella server (see #26) that also does TLS 1.2
168  under very restricted circumstances (see below) and GCM.
169  Details at https://tls.secg.org/, transaction log at
170  https://tls.secg.org/index1.php?action=https_log (this log
171  rolls over fairly quickly, requiring opening the last several
172  entries and matching cipher suites to see which one was
173  yours). Note that this server claims to support TLS 1.2 but
174  returns a TLS 1.1 server hello in response to a TLS 1.2
175  handshake request unless you report DHE_DSS as your only
176  available cipher suite. A more standard combination like
177  RSA or DHE_RSA results in the server returning a TLS 1.1
178  response, and an attempt to force matters with a TLS 1.2-only
179  cipher suite like DHE_AES_GCM returns an alert message with
180  the version number set to SSLv3, i.e. { 3, 0 }.
181  Server 29: Microsoft interop test server that does TLS 1.2, ECC, and
182  unlike GnuTLS and Certicom/SECG it actually really does TLS
183  1.2. This is the generic interface with all cipher suites,
184  see Server #30 and #31 for variants. This server also claims
185  to support GCM (although only with ECC, not with RSA/DSA) but
186  in practice closes the connection when sent a client hello
187  with this cipher suite.
188  Server 30: As Server #29 but restricted to 256-bit ECC only, this server
189  does actually support GCM. Requires
190  CRYPT_OPTION_CERT_COMPLIANCELEVEL =
191  CRYPT_COMPLIANCELEVEL_OBLIVIOUS due to b0rken certs.
192  Server 31: As server #29 but restricted to 384-bit ECC only, however it
193  closes the connection when sent a SHA-384 cipher suite.
194  Server 32: As Server #29 but tests rehandshake handling. This is
195  actually meant to test RSA client authentication, i.e.
196  SSL_TEST_CLIENTCERT, but Windows implements this by
197  performning a standard handshake without client-auth and then
198  immediately performing a rehandshake with client-auth, which
199  can be used to test the ability to handle a rehandshake
200  request. In practice the Windows server hangs waiting for
201  the rehandshake, so eventually we exit with a read timeout
202  error.
203  Server 33: RSA interop server, this requires a complex pre-approval
204  application process to enable access which makes it not worth
205  the bother, it's only listed here for completeness */
206 
207 #define SSL_SERVER_NO 2
208 #define TLS_SERVER_NO 3
209 #define TLS11_SERVER_NO 4 /* Use #27 for ECC, otherwise #2 */
210 #define TLS12_SERVER_NO 29 /* Options = #23, #24, #28, #29/30/31
211  (but see above for #24, #28, and some of
212  #29) */
213 #if ( SSL_SERVER_NO == TLS_SERVER_NO ) || \
214  ( SSL_SERVER_NO == TLS11_SERVER_NO ) || \
215  ( TLS_SERVER_NO == TLS11_SERVER_NO )
216  #error SSL/TLS/TLS11 servers must be distinct to avoid tests being no-op'd due to cacheing
217 #endif /* Make sure that servers are distinct */
218 
219 static const struct {
220  const C_STR name;
221  const char FAR_BSS *path;
222  } FAR_BSS sslInfo[] = {
223  { NULL, NULL },
224  /* 1 */ { TEXT( "localhost" ), "/" },
225  /* 2 */ { TEXT( "https://www.amazon.com" ), "/" },
226  /* 3 */ { TEXT( "https://www.paypal.com" ), "/" },
227  /* 4 */ { TEXT( "https://www.redhat.com" ), "/" },
228  /* 5 */ { TEXT( "https://www.cs.berkeley.edu" ), "/~daw/people/crypto.html" },
229  /* 6 */ { TEXT( "pop.web.de:995" ), "/" },
230  /* 7 */ { TEXT( "imap4-gw.uni-regensburg.de:993" ), "/" },
231  /* 8 */ { TEXT( "securepop.t-online.de:995" ), "/" },
232  /* 9 */ { TEXT( "https://homedir.wlv.ac.uk" ), "/" },
233  /* 10 */ { TEXT( "https://www.horaso.com:20443" ), "/" },
234  /* 11 */ { TEXT( "https://homedir.wlv.ac.uk" ), "/" },
235  /* 12 */ { TEXT( "https://www.microsoft.com" ), "/" },
236  /* 13 */ { TEXT( "https://alphaworks.ibm.com/" ), "/" },
237  /* 14 */ { TEXT( "https://webmount.turbulent.ca/" ), "/" },
238  /* 15 */ { TEXT( "https://www.gnutls.org/" ), "/" },
239  /* 16 */ { TEXT( "https://www.gnutls.org:5555/" ), "/" },
240  /* 17 */ { TEXT( "https://www.networksolutions.com/" ), "/" },
241  /* 18 */ { TEXT( "https://olb.westpac.com.au/" ), "/" },
242  /* 19 */ { TEXT( "https://www.hertz.com/" ), "/" },
243  /* 20 */ { TEXT( "https://www.openssl.org/" ), "/" },
244  /* 21 */ { TEXT( "https://secureads.ft.com/" ), "/" },
245  /* 22 */ { TEXT( "https://mail.maine.edu/" ), "/" },
246  /* 23 */ { TEXT( "https://www.mikestoolbox.net/" ), "/" },
247  /* 24 */ { TEXT( "https://test.gnutls.org:5556/" ), "/" },
248  /* 25 */ { TEXT( "https://sni.velox.ch/" ), "/" },
249  /* 26 */ { TEXT( "https://tls.secg.org:40023/connect.php" ), "/" },
250  /* 27 */ { TEXT( "https://ecc.fedora.redhat.com" ), "/" },
251  /* 28 */ { TEXT( "https://tls.secg.org/" ), "/" },
252  /* 29 */ { TEXT( "https://tls.woodgrovebank.com:25000/" ), "/" },
253  /* 30 */ { TEXT( "https://tls.woodgrovebank.com:25002/" ), "/" },
254  /* 31 */ { TEXT( "https://tls.woodgrovebank.com:25003/" ), "/" },
255  /* 32 */ { TEXT( "https://tls.woodgrovebank.com:25005/" ), "/" },
256  /* 33 */ { TEXT( "https://203.166.62.199/" ), "/" },
257  { NULL, NULL }
258  };
259 
260 /* Various servers used for STARTTLS/STLS/AUTH TLS testing. Notes:
261 
262  Server 1: SMTP: mailbox.ucsd.edu:25 (132.239.1.57) requires a client
263  certificate.
264  Server 2: POP: pop.cae.wisc.edu:1110 (144.92.240.11) OK.
265  Server 3: SMTP: smtpauth.cae.wisc.edu:25 (144.92.12.93) requires a
266  client certificate.
267  Server 4: SMTP: send.columbia.edu:25 (128.59.59.23) returns invalid
268  certificate (lower compliance level to fix).
269  Server 5: POP: pop3.myrealbox.com:110 (192.108.102.201) returns invalid
270  certificate (lower compliance level to fix).
271  Server 6: Encrypted POP: securepop.t-online.de:995 (194.25.134.46)
272  direct SSL connect.
273  Server 7: FTP: ftp.windsorchapel.net:21 (68.38.166.195) sends redundant
274  client certificate request with invalid length.
275  Server 8: POP: webmail.chm.tu-dresden.de:110 (141.30.198.37), another
276  GroupWise server (see the server comments above) with b0rken
277  certs.
278 
279  To test FTP with SSL/TLS manually: Disable auto-login with
280  FTP, then send an RFC 2389 FEAT command to check security
281  facilities. If this is supported, one of the responses will
282  be either AUTH SSL or AUTH TLS, use this to turn on SSL/TLS.
283  If FEAT isn't supported, AUTH TLS should usually work:
284 
285  ftp -n ftp.windsorchapel.net
286  quote feat
287  quote auth ssl
288 
289  or just:
290 
291  telnet ftp.windsorchapel.net 21
292  auth ssl
293 
294  Server 9: SMTP: mailer.gwdg.de:25 (134.76.10.26), sends each SSL message
295  as a discrete packet, providing a nice test of cryptlib's on-
296  demand buffer refill.
297  Server 10: Encrypted POP: mrdo.vosn.net:995 (209.151.91.6), direct SSL
298  connect, sends a CA certificate which is also used for
299  encryption, but with no keyUsage flags set.
300  Server 11: POP: pop.gmail.com:110 (64.233.167.111) (moved to 995 as of
301  some time in 2008).
302  Server 12: POP: mail.rochester.edu:995 (128.151.224.17), direct SSL
303  connect (also sends zero-length packets as a kludge for pre-
304  TLS 1.1 chosen-IV attacks).
305  Server 13: SMTP: smtp.umn.edu:465 (134.84.119.35), direct SSL connect.
306  Server 14: POP3: pop3.live.com:995 (65.55.172.253), direct SSL connect,
307  returns a malformed certificate. Can also be accessed via
308  smtp.live.com, port 25 or 587 */
309 
310 #define STARTTLS_SERVER_NO 12
311 
312 typedef enum { PROTOCOL_NONE, PROTOCOL_SMTP, PROTOCOL_SMTP_DIRECT,
313  PROTOCOL_POP, PROTOCOL_IMAP, PROTOCOL_POP_DIRECT,
314  PROTOCOL_FTP
315  } PROTOCOL_TYPE;
316 
317 static const struct {
318  const C_STR name;
319  const int port;
320  PROTOCOL_TYPE protocol;
321  } FAR_BSS starttlsInfo[] = {
322  { NULL, 0 },
323  /* 1 */ { TEXT( "132.239.1.57" ), 25, PROTOCOL_SMTP },
324  /* 2 */ { TEXT( "144.92.240.11" ), 1110, PROTOCOL_POP },
325  /* 3 */ { TEXT( "144.92.12.93" ), 25, PROTOCOL_SMTP },
326  /* 4 */ { TEXT( "128.59.59.23" ), 25, PROTOCOL_SMTP },
327  /* 5 */ { TEXT( "192.108.102.201" ), 110, PROTOCOL_POP },
328  /* 6 */ { TEXT( "194.25.134.46" ), 995, PROTOCOL_POP_DIRECT },
329  /* 7 */ { TEXT( "68.38.166.195" ), 21, PROTOCOL_FTP },
330  /* 8 */ { TEXT( "141.30.198.37" ), 110, PROTOCOL_POP },
331  /* 9 */ { TEXT( "134.76.10.26" ), 25, PROTOCOL_SMTP },
332  /* 10 */ { TEXT( "209.151.91.6" ), 995, PROTOCOL_POP_DIRECT },
333  /* 11 */ { TEXT( "64.233.167.111" ), 110, PROTOCOL_POP },
334  /* 12 */ { TEXT( "128.151.224.17" ), 995, PROTOCOL_POP_DIRECT },
335  /* 13 */ { TEXT( "134.84.119.35" ), 465, PROTOCOL_SMTP_DIRECT },
336  /* 14 */ { TEXT( "65.55.172.253" ), 995, PROTOCOL_POP_DIRECT },
337  { NULL, 0 }
338  };
339 
340 /* Special-case handling for buggy/broken/odd servers */
341 
342 #if ( SSL_SERVER_NO == 7 ) || ( TLS12_SERVER_NO == 30 ) || \
343  ( TLS12_SERVER_NO == 31 ) || ( STARTTLS_SERVER_NO == 8 )
344  #define IS_BROKEN_SERVER
345  #ifdef _MSC_VER
346  #pragma message( " Building with reduced compliance level for buggy SSL/TLS server." )
347  #endif /* VC++ */
348 #endif /* Broken servers */
349 #if ( SSL_SERVER_NO == 3 )
350  #define IS_HIGHVOLUME_SERVER
351 #endif /* Servers with high result volume */
352 
353 /* If we're testing dual-thread handling of sessions, we need to provide a
354  forward declaration of the threading function since it's called from
355  within the SSL connect code */
356 
357 #ifdef WINDOWS_THREADS
358  unsigned __stdcall tlsServerDualThread2( void *dummy );
359 #endif /* WINDOWS_THREADS */
360 
361 /* Large buffer size to test bulk data transfer capability for secure
362  sessions */
363 
364 #if defined( __MSDOS16__ ) || defined( __WIN16__ )
365  #define BULKDATA_BUFFER_SIZE 20000
366 #else
367  #define BULKDATA_BUFFER_SIZE 300000L
368 #endif /* 16-bit VC++ */
369 
370 static int checksumData( const void *data, const int dataLength )
371  {
372  const BYTE *dataPtr = data;
373  int sum1 = 0, sum2 = 0, i;
374 
375  /* Calculate a 16-bit Fletcher-like checksum of the data (it doesn't
376  really matter if it's not exactly right, as long as the behaviour is
377  the same for all data) */
378  for( i = 0; i < dataLength; i++ )
379  {
380  sum1 += dataPtr[ i ];
381  sum2 += sum1;
382  }
383 
384  return( sum2 & 0xFFFF );
385  }
386 
387 static BOOLEAN handleBulkBuffer( BYTE *buffer, const BOOLEAN isInit )
388  {
389  int checkSum, i;
390 
391  /* If we're initialising the buffer, fill it with [0...256]* followed by
392  a checksum of the buffer contents */
393  if( isInit )
394  {
395  for( i = 0; i < BULKDATA_BUFFER_SIZE - 2; i++ )
396  buffer[ i ] = i & 0xFF;
397  checkSum = checksumData( buffer, BULKDATA_BUFFER_SIZE - 2 );
398  buffer[ BULKDATA_BUFFER_SIZE - 2 ] = ( checkSum >> 8 ) & 0xFF;
399  buffer[ BULKDATA_BUFFER_SIZE - 1 ] = checkSum & 0xFF;
400 
401  return( TRUE );
402  }
403 
404  /* We're being sent an initialised buffer, make sure that it's OK */
405  for( i = 0; i < BULKDATA_BUFFER_SIZE - 2; i++ )
406  {
407  if( buffer[ i ] != ( i & 0xFF ) )
408  return( FALSE );
409  }
410  checkSum = checksumData( buffer, BULKDATA_BUFFER_SIZE - 2 );
411  if( buffer[ BULKDATA_BUFFER_SIZE - 2 ] != ( ( checkSum >> 8 ) & 0xFF ) || \
412  buffer[ BULKDATA_BUFFER_SIZE - 1 ] != ( checkSum & 0xFF ) )
413  return( FALSE );
414 
415  return( TRUE );
416  }
417 
418 /* Negotiate through a STARTTLS */
419 
420 #if defined( __WINDOWS__ ) && !( defined( __WIN16__ ) || defined( _WIN32_WCE ) )
421 
422 static int readLine( SOCKET netSocket, char *buffer )
423  {
424  int bufPos, status = CRYPT_OK;
425 
426  for( bufPos = 0; \
427  status >= 0 && bufPos < 1024 && \
428  ( bufPos < 1 || buffer[ bufPos -1 ] != '\n' );
429  bufPos++ )
430  status = recv( netSocket, buffer + bufPos, 1, 0 );
431  while( bufPos > 1 && isspace( buffer[ bufPos - 1 ] ) )
432  bufPos--;
433  if( bufPos >= 3 )
434  {
435  while( bufPos > 1 && isspace( buffer[ bufPos - 1 ] ) )
436  bufPos--;
437  buffer[ min( bufPos, 56 ) ] = '\0';
438  }
439  return( bufPos );
440  }
441 
442 static SOCKET negotiateSTARTTLS( int *protocol )
443  {
444  SOCKET netSocket;
445  struct sockaddr_in serverAddr;
446  char buffer[ 1024 ];
447  int bufPos, status;
448 
449  puts( "Negotiating SMTP/POP/IMAP/FTP session through to TLS start..." );
450  *protocol = starttlsInfo[ STARTTLS_SERVER_NO ].protocol;
451 
452  /* Connect to a generally-available server to test STARTTLS/STLS
453  functionality */
454  memset( &serverAddr, 0, sizeof( struct sockaddr_in ) );
455  serverAddr.sin_family = AF_INET;
456  serverAddr.sin_port = htons( ( u_short ) starttlsInfo[ STARTTLS_SERVER_NO ].port );
457  serverAddr.sin_addr.s_addr = inet_addr( starttlsInfo[ STARTTLS_SERVER_NO ].name );
458  netSocket = socket( PF_INET, SOCK_STREAM, 0 );
459  if( netSocket == INVALID_SOCKET )
460  {
461  printf( "Couldn't create socket, line %d.\n", __LINE__ );
462  return( CRYPT_ERROR_FAILED );
463  }
464  status = connect( netSocket, ( struct sockaddr * ) &serverAddr,
465  sizeof( struct sockaddr_in ) );
466  if( status == SOCKET_ERROR )
467  {
468  closesocket( netSocket );
469  printf( "Couldn't connect socket, line %d.\n", __LINE__ );
470  return( CRYPT_OK );
471  }
472 
473  /* If it's a direct connect, there's nothing left to do */
474  if( *protocol == PROTOCOL_POP_DIRECT )
475  {
476  *protocol = PROTOCOL_POP;
477  return( netSocket );
478  }
479  if( *protocol == PROTOCOL_SMTP_DIRECT )
480  {
481  *protocol = PROTOCOL_SMTP;
482  return( netSocket );
483  }
484 
485  /* Perform (very crude) SMTP/POP/IMAP negotiation to switch to TLS */
486  bufPos = readLine( netSocket, buffer );
487  if( bufPos < 3 || ( strncmp( buffer, "220", 3 ) && \
488  strncmp( buffer, "+OK", 3 ) && \
489  strncmp( buffer, "OK", 2 ) ) )
490  {
491  closesocket( netSocket );
492  printf( "Got response '%s', line %d.\n", buffer, __LINE__ );
493  return( CRYPT_OK );
494  }
495  printf( " Server said: '%s'\n", buffer );
496  assert( ( *protocol == PROTOCOL_SMTP && !strncmp( buffer, "220", 3 ) ) || \
497  ( *protocol == PROTOCOL_POP && !strncmp( buffer, "+OK", 3 ) ) || \
498  ( *protocol == PROTOCOL_IMAP && !strncmp( buffer, "OK", 2 ) ) || \
499  ( *protocol == PROTOCOL_FTP && !strncmp( buffer, "220", 3 ) ) || \
500  *protocol == PROTOCOL_NONE );
501  switch( *protocol )
502  {
503  case PROTOCOL_POP:
504  send( netSocket, "STLS\r\n", 6, 0 );
505  puts( " We said: 'STLS'" );
506  break;
507 
508  case PROTOCOL_IMAP:
509  /* It's possible for some servers that we may need to explicitly
510  send a CAPABILITY command first to enable STARTTLS:
511  a001 CAPABILITY
512  > CAPABILITY IMAP4rev1 STARTTLS LOGINDISABLED
513  > OK CAPABILITY completed */
514  send( netSocket, "a001 STARTTLS\r\n", 15, 0 );
515  puts( " We said: 'STARTTLS'" );
516  break;
517 
518  case PROTOCOL_SMTP:
519  send( netSocket, "EHLO foo.bar.com\r\n", 18, 0 );
520  puts( " We said: 'EHLO foo.bar.com'" );
521  do
522  {
523  bufPos = readLine( netSocket, buffer );
524  if( bufPos < 3 || strncmp( buffer, "250", 3 ) )
525  {
526  closesocket( netSocket );
527  printf( "Got response '%s', line %d.\n", buffer,
528  __LINE__ );
529  return( CRYPT_OK );
530  }
531  printf( " Server said: '%s'\n", buffer );
532  }
533  while( !strncmp( buffer, "250-", 4 ) );
534  send( netSocket, "STARTTLS\r\n", 10, 0 );
535  puts( " We said: 'STARTTLS'" );
536  break;
537 
538  case PROTOCOL_FTP:
539  send( netSocket, "AUTH TLS\r\n", 10, 0 );
540  puts( " We said: 'AUTH TLS'" );
541  break;
542 
543  default:
544  assert( FALSE );
545  }
546  bufPos = readLine( netSocket, buffer );
547  if( bufPos < 3 || ( strncmp( buffer, "220", 3 ) && \
548  strncmp( buffer, "+OK", 3 ) && \
549  strncmp( buffer, "OK", 2 ) && \
550  strncmp( buffer, "234", 3 ) ) )
551  {
552  printf( "Got response '%s', line %d.\n", buffer, __LINE__ );
553  return( CRYPT_OK );
554  }
555  printf( " Server said: '%s'\n", buffer );
556  return( netSocket );
557  }
558 #endif /* Win32 */
559 
560 /* Establish an SSL/TLS session */
561 
562 static int connectSSLTLS( const CRYPT_SESSION_TYPE sessionType,
563  const SSL_TEST_TYPE testType, const int version,
564  const int sessionID, const BOOLEAN localSession )
565  {
566  CRYPT_SESSION cryptSession;
567  const BOOLEAN isServer = ( sessionType == CRYPT_SESSION_SSL_SERVER ) ? \
568  TRUE : FALSE;
569  const char *versionStr[] = { "SSL", "TLS", "TLS 1.1", "TLS 1.2", "TLS 1.3" };
570  const C_STR serverName = ( testType == SSL_TEST_STARTTLS ) ? \
571  starttlsInfo[ STARTTLS_SERVER_NO ].name : \
572  ( version == 0 ) ? \
573  sslInfo[ SSL_SERVER_NO ].name : \
574  ( version == 1 ) ? \
575  sslInfo[ TLS_SERVER_NO ].name : \
576  ( version == 2 ) ? \
577  sslInfo[ TLS11_SERVER_NO ].name : \
578  sslInfo[ TLS12_SERVER_NO ].name;
579  BYTE *bulkBuffer = NULL; /* Needed for bogus uninit-value warnings */
580 #if defined( __WINDOWS__ ) && !( defined( __WIN16__ ) || defined( _WIN32_WCE ) )
581  SOCKET netSocket;
582 #endif /* Win32 */
583  char buffer[ FILEBUFFER_SIZE ];
584 #ifdef IS_BROKEN_SERVER
585  int complianceLevel;
586 #endif /* SSL servers with b0rken certs */
587  int bytesCopied, protocol = PROTOCOL_SMTP, status;
588 
589  /* If this is a local session, synchronise the client and server */
590  if( localSession )
591  {
592  if( isServer )
593  {
594  /* Acquire the init mutex */
595  acquireMutex();
596  }
597  else
598  {
599  /* We're the client, wait for the server to finish initialising */
600  if( waitMutex() == CRYPT_ERROR_TIMEOUT )
601  {
602  printf( "Timed out waiting for server to initialise, "
603  "line %d.\n", __LINE__ );
604  return( FALSE );
605  }
606  }
607  }
608 
609  /* If this is the dual-thread server test and we're the second server
610  thread, skip the portions that have already been handled by the first
611  thread */
612 #ifdef WINDOWS_THREADS
613  if( isServer && testType == SSL_TEST_DUALTHREAD && sessionID == 0 )
614  goto dualThreadContinue;
615 #endif /* WINDOWS_THREADS */
616 
617  if( sessionID != CRYPT_UNUSED )
618  printf( "%02d: ", sessionID );
619  printf( "%sTesting %s%s session%s...\n", isServer ? "SVR: " : "",
620  localSession ? "local " : "", versionStr[ version ],
621  ( testType == SSL_TEST_CLIENTCERT ) ? " with client certs" : \
622  ( testType == SSL_TEST_STARTTLS ) ? " with local socket" : \
623  ( testType == SSL_TEST_BULKTRANSER ) ? " for bulk data transfer" : \
624  ( testType == SSL_TEST_PSK ) ? " with shared key" : \
625  ( testType == SSL_TEST_PSK_CLIONLY ) ? " with client-only PSK" : \
626  ( testType == SSL_TEST_PSK_SVRONLY ) ? " with server-only PSK" : \
627  ( testType == SSL_TEST_ECC ) ? " with P256 ECC crypto" : \
628  ( testType == SSL_TEST_ECC_P384 ) ? " with P384 ECC crypto" : "" );
629  if( !isServer && !localSession )
630  printf( " Remote host: %s.\n", serverName );
631 
632  /* Create the SSL/TLS session */
633  status = cryptCreateSession( &cryptSession, CRYPT_UNUSED, sessionType );
634  if( status == CRYPT_ERROR_PARAM3 ) /* SSL/TLS session access not available */
635  return( CRYPT_ERROR_NOTAVAIL );
636  if( cryptStatusError( status ) )
637  {
638  printf( "cryptCreateSession() failed with error code %d, line %d.\n",
639  status, __LINE__ );
640  return( FALSE );
641  }
642  status = cryptSetAttribute( cryptSession, CRYPT_SESSINFO_VERSION,
643  version );
644  if( cryptStatusError( status ) )
645  {
646  printf( "cryptSetAttribute() failed with error code %d, line %d.\n",
647  status, __LINE__ );
648  return( FALSE );
649  }
650 
651  /* If we're doing a bulk data transfer, set up the necessary buffer */
652  if( testType == SSL_TEST_BULKTRANSER )
653  {
654  if( ( bulkBuffer = malloc( BULKDATA_BUFFER_SIZE ) ) == NULL )
655  {
656  printf( "Failed to allocated %ld bytes, line %d.\n",
657  BULKDATA_BUFFER_SIZE, __LINE__ );
658  return( FALSE );
659  }
660  if( isServer )
661  handleBulkBuffer( bulkBuffer, TRUE );
662  }
663 
664  /* Set up the server information and activate the session */
665  if( isServer )
666  {
667  CRYPT_CONTEXT privateKey;
668 
669  if( !setLocalConnect( cryptSession, 443 ) )
670  return( FALSE );
671  if( testType != SSL_TEST_PSK && \
672  testType != SSL_TEST_PSK_SVRONLY && \
673  testType != SSL_TEST_DUALTHREAD )
674  {
675  char filenameBuffer[ FILENAME_BUFFER_SIZE ];
676 #ifdef UNICODE_STRINGS
677  wchar_t wcBuffer[ FILENAME_BUFFER_SIZE ];
678 #endif /* UNICODE_STRINGS */
679  void *fileNamePtr = filenameBuffer;
680 
681  /* We don't add a private key if we're doing TLS-PSK, to test
682  TLS-PSK's abiltiy to work without a PKC */
683  if( testType == SSL_TEST_ECC || testType == SSL_TEST_ECC_P384 )
684  {
685  filenameFromTemplate( filenameBuffer,
687  ( testType == SSL_TEST_ECC_P384 ) ? \
688  384 : 256 );
689  }
690  else
691  {
692  filenameFromTemplate( filenameBuffer,
694  }
695 #ifdef UNICODE_STRINGS
696  mbstowcs( wcBuffer, filenameBuffer,
697  strlen( filenameBuffer ) + 1 );
698  fileNamePtr = wcBuffer;
699 #endif /* UNICODE_STRINGS */
700  status = getPrivateKey( &privateKey, fileNamePtr,
703  if( cryptStatusOK( status ) )
704  {
705  status = cryptSetAttribute( cryptSession,
707  privateKey );
708  cryptDestroyContext( privateKey );
709  }
710  }
711  if( cryptStatusOK( status ) && testType == SSL_TEST_CLIENTCERT )
712  {
713  CRYPT_KEYSET cryptKeyset;
714 
715  status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED,
718  if( cryptStatusError( status ) )
719  {
720  printf( "SVR: Client certificate keyset open failed with error "
721  "code %d, line %d.\n", status, __LINE__ );
722  return( FALSE );
723  }
724  status = cryptSetAttribute( cryptSession, CRYPT_SESSINFO_KEYSET,
725  cryptKeyset );
726  cryptKeysetClose( cryptKeyset );
727  }
728  }
729  else
730  {
731  if( testType == SSL_TEST_STARTTLS )
732  {
733  /* Testing this fully requires a lot of OS-specific juggling so
734  unless we're running under Windows we just supply the handle
735  to stdin, which will return a read/write error during the
736  connect. This checks that the handle has been assigned
737  corectly without requiring a lot of OS-specific socket
738  handling code. Under Windows, we use a (very cut-down) set
739  of socket calls to set up a minimal socket. Since there's
740  very little error-checking done, we don't treat a failure
741  as fatal */
742 #if defined( __WINDOWS__ ) && !( defined( __WIN16__ ) || defined( _WIN32_WCE ) )
743  WSADATA wsaData;
744 
745  if( WSAStartup( 2, &wsaData ) )
746  {
747  printf( "Couldn't initialise sockets interface, line %d.\n",
748  __LINE__ );
749  return( FALSE );
750  }
751 
752  /* Try and negotiate a STARTTLS session. We don't treat most
753  types of failure as fatal since there are a great many minor
754  things that can go wrong that we don't want to have to handle
755  without writing half an MUA */
756  netSocket = negotiateSTARTTLS( &protocol );
757  if( netSocket <= 0 )
758  {
759  cryptDestroySession( cryptSession );
760  WSACleanup();
761  if( netSocket == CRYPT_OK )
762  {
763  puts( "This is a nonfatal error (a great many other "
764  "things can go wrong while\nnegotiating through "
765  "to the TLS upgrade).\n" );
766  return( TRUE );
767  }
768  return( FALSE );
769  }
770 
771  /* Hand the socket to cryptlib */
772  #if defined( _MSC_VER ) && defined( _M_X64 )
773  status = cryptSetAttribute( cryptSession,
774  CRYPT_SESSINFO_NETWORKSOCKET, ( int ) netSocket );
775  #else
776  status = cryptSetAttribute( cryptSession,
777  CRYPT_SESSINFO_NETWORKSOCKET, netSocket );
778  #endif /* 32- vs. 64-bit Windows */
779 #elif defined( DDNAME_IO )
780  /* The fileno() function doesn't work for DDNAMEs */
781  status = cryptSetAttribute( cryptSession,
783 #elif defined( __WIN16__ ) || defined( _WIN32_WCE )
784  status = cryptSetAttribute( cryptSession,
786 #else
787  status = cryptSetAttribute( cryptSession,
788  CRYPT_SESSINFO_NETWORKSOCKET, fileno( stdin ) );
789 #endif /* OS-specific local socket handling */
790  }
791  else
792  {
793  if( localSession )
794  {
795  if( !setLocalConnect( cryptSession, 443 ) )
796  return( FALSE );
797  }
798  else
799  {
800  status = cryptSetAttributeString( cryptSession,
801  CRYPT_SESSINFO_SERVER_NAME, serverName,
802  paramStrlen( serverName ) );
803  }
804  }
805  if( cryptStatusOK( status ) && testType == SSL_TEST_CLIENTCERT )
806  {
807  CRYPT_CONTEXT privateKey;
808 
809  /* Depending on which server we're testing against we need to
810  use different private keys */
811 #if ( TLS12_SERVER_NO == 30 && 0 )
812  getPrivateKey( &privateKey, SSL_CLI_PRIVKEY_FILE,
813  "cc47650c403654f6fe439e5c88a2e6c2_66335081-ee61-4aa8-862d-a423d58",
815 #else
816  status = getPrivateKey( &privateKey, USER_PRIVKEY_FILE,
818 #endif /* Different keys for different servers */
819  if( cryptStatusOK( status ) )
820  {
821  status = cryptSetAttribute( cryptSession,
822  CRYPT_SESSINFO_PRIVATEKEY, privateKey );
823  cryptDestroyContext( privateKey );
824  }
825  }
826 #if 0 /* Optional proxy for net access */
828  CRYPT_OPTION_NET_HTTP_PROXY, "[Autodetect]",
829  12 );
830 #endif /* 0 */
831  }
832  if( cryptStatusOK( status ) && \
833  ( testType == SSL_TEST_PSK || \
834  ( isServer && testType == SSL_TEST_PSK_SVRONLY ) || \
835  ( !isServer && testType == SSL_TEST_PSK_CLIONLY ) ) )
836  {
837  /* If we're testing the no-PSK handling, only the server is
838  expecting TLS-PSK, so the client isn't supplied with a
839  password */
840  if( cryptStatusOK( status ) && isServer && testType == SSL_TEST_PSK )
841  {
842  /* If we're testing PSK, add several preceding usernames and
843  passwords */
844  cryptSetAttributeString( cryptSession,
845  CRYPT_SESSINFO_USERNAME, TEXT( "before1" ),
846  paramStrlen( TEXT( "before1" ) ) );
847  cryptSetAttributeString( cryptSession,
848  CRYPT_SESSINFO_PASSWORD, TEXT( "before1" ),
849  paramStrlen( TEXT( "before1" ) ) );
850  cryptSetAttributeString( cryptSession,
851  CRYPT_SESSINFO_USERNAME, TEXT( "before2" ),
852  paramStrlen( TEXT( "before2" ) ) );
853  cryptSetAttributeString( cryptSession,
854  CRYPT_SESSINFO_PASSWORD, TEXT( "before2" ),
855  paramStrlen( TEXT( "before2" ) ) );
856  }
857  status = cryptSetAttributeString( cryptSession,
860  if( cryptStatusOK( status ) )
861  status = cryptSetAttributeString( cryptSession,
864  if( cryptStatusOK( status ) && isServer && testType == SSL_TEST_PSK )
865  {
866  /* If we're testing PSK, add several succeeding usernames and
867  passwords */
868  cryptSetAttributeString( cryptSession,
869  CRYPT_SESSINFO_USERNAME, TEXT( "after1" ),
870  paramStrlen( TEXT( "after1" ) ) );
871  cryptSetAttributeString( cryptSession,
872  CRYPT_SESSINFO_PASSWORD, TEXT( "after1" ),
873  paramStrlen( TEXT( "after1" ) ) );
874  cryptSetAttributeString( cryptSession,
875  CRYPT_SESSINFO_USERNAME, TEXT( "after2" ),
876  paramStrlen( TEXT( "after2" ) ) );
877  cryptSetAttributeString( cryptSession,
878  CRYPT_SESSINFO_PASSWORD, TEXT( "after2" ),
879  paramStrlen( TEXT( "after2" ) ) );
880  }
881  }
882  if( cryptStatusError( status ) )
883  {
884  if( testType == SSL_TEST_STARTTLS )
885  {
886 #if defined( __WINDOWS__ ) && !( defined( __WIN16__ ) || defined( _WIN32_WCE ) )
887  closesocket( netSocket );
888  WSACleanup();
889 #else
890  /* Creating a socket in a portable manner is too difficult so
891  we've passed in a stdio handle, this should return an error
892  since it's not a blocking socket */
893  return( TRUE );
894 #endif /* __WINDOWS__ && !_WIN32_WCE */
895  }
896  printf( "cryptSetAttribute/AttributeString() failed with error code "
897  "%d, line %d.\n", status, __LINE__ );
898  return( FALSE );
899  }
900 #ifdef IS_BROKEN_SERVER
902  &complianceLevel );
905 #endif /* SSL servers with b0rken certs */
906  if( localSession )
907  {
908  /* If we're running a local loopback test, display additional
909  information indicating when the session is activated, since
910  the multithreaded tests may not get to this point until long
911  after the threads are started */
912  if( sessionID != CRYPT_UNUSED )
913  printf( "%02d: ", sessionID );
914  printf( "%sActivating %s session...\n", isServer ? "SVR: " : "",
915  versionStr[ version ] );
916 
917  /* For the loopback test we also increase the connection timeout to
918  a higher-than-normal level, since this gives us more time for
919  tracing through the code when debugging */
921  120 );
922  }
923  if( localSession && isServer )
924  {
925  /* Tell the client that we're ready to go */
926  releaseMutex();
927  }
928  status = cryptSetAttribute( cryptSession, CRYPT_SESSINFO_ACTIVE, TRUE );
929 #ifdef IS_BROKEN_SERVER
931  complianceLevel );
932 #endif /* SSL server with b0rken certs */
933  if( isServer && testType != SSL_TEST_PSK_CLIONLY && \
934  testType != SSL_TEST_PSK_SVRONLY )
935  {
936  /* We don't check the return status for this since the session may
937  be disconnected before we get the client info, which would cause
938  us to bail out before we display the error info */
939  if( sessionID != CRYPT_UNUSED )
940  printf( "%02d: ", sessionID );
941  printConnectInfo( cryptSession );
942  }
943 #ifdef WINDOWS_THREADS
944  if( isServer && testType == SSL_TEST_DUALTHREAD && \
945  status == CRYPT_ENVELOPE_RESOURCE )
946  {
947  static CRYPT_SESSION localCryptSession = 0;
948  unsigned threadID;
949 
950  /* Start a second thread to complete the handshake and exit */
951  localCryptSession = cryptSession;
952  _beginthreadex( NULL, 0, tlsServerDualThread2, NULL, 0, &threadID );
953  return( TRUE );
954 
955  /* The second thread continues from here */
956 dualThreadContinue:
957  assert( localSession > 0 );
958  cryptSession = localCryptSession;
959 
960  /* Allow the auth.and complete the handshake */
961  puts( "SVR: Confirming authentication to client..." );
962  status = cryptSetAttribute( cryptSession,
964  if( cryptStatusOK( status ) )
965  status = cryptSetAttribute( cryptSession,
967  }
968 #endif /* WINDOWS_THREADS */
969  if( cryptStatusError( status ) )
970  {
971  char strBuffer[ 128 ];
972 
973  if( testType == SSL_TEST_STARTTLS )
974  {
975 #if defined( __WINDOWS__ ) && !defined( _WIN32_WCE )
976  closesocket( netSocket );
977  WSACleanup();
978 #else
979  /* If we're using a dummy local socket, we'll get a R/W error at
980  this point since it's not connected to anything, so we
981  intercept it before it gets any further */
982  if( status == CRYPT_ERROR_READ || status == CRYPT_ERROR_WRITE )
983  {
984  cryptDestroySession( cryptSession );
985  return( TRUE );
986  }
987 #endif /* __WINDOWS__ && !_WIN32_WCE */
988  }
989  if( sessionID != CRYPT_UNUSED )
990  printf( "%02d: ", sessionID );
991  sprintf( strBuffer, "%sAttempt to activate %s%s session",
992  isServer ? "SVR: " : "", localSession ? "local " : "",
993  versionStr[ version ] );
994  printExtError( cryptSession, strBuffer, status, __LINE__ );
995  cryptDestroySession( cryptSession );
996  if( testType == SSL_TEST_BULKTRANSER )
997  free( bulkBuffer );
998  if( !isServer && \
999  ( status == CRYPT_ERROR_OPEN || status == CRYPT_ERROR_NOTFOUND ) )
1000  {
1001  /* These servers are constantly appearing and disappearing so if
1002  we get a straight connect error we don't treat it as a
1003  serious failure */
1004  puts( " (Server could be down, faking it and continuing...)\n" );
1005  return( CRYPT_ERROR_FAILED );
1006  }
1007  if( testType == SSL_TEST_PSK_CLIONLY || \
1008  testType == SSL_TEST_PSK_SVRONLY )
1009  {
1010  /* The CLIONLY/SVRONLY test is supposed to fail, so if this
1011  happens then the overall test has succeeded */
1012  puts( " (This test checks error handling, so the failure "
1013  "response is correct).\n" );
1014  return( TRUE );
1015  }
1016  return( FALSE );
1017  }
1018 
1019  /* The CLIONLY/SVRONLY test is supposed to fail, if this doesn't happen
1020  then there's a problem */
1021 #ifdef NO_SESSION_CACHE
1022  if( testType == SSL_TEST_PSK_CLIONLY || \
1023  testType == SSL_TEST_PSK_SVRONLY )
1024  {
1025  printf( "%sTLS-PSK handshake without password should have "
1026  "failed but succeeded,\nline %d.\n",
1027  isServer ? "SVR: " : "", __LINE__ );
1028  return( FALSE );
1029  }
1030 #endif /* NO_SESSION_CACHE */
1031 
1032  /* If we're testing session resumption and there's a server key present
1033  then we didn't actually resume the session */
1034 #ifndef NO_SESSION_CACHE
1035  if( testType == SSL_TEST_RESUME )
1036  {
1037  CRYPT_CONTEXT serverKey;
1038 
1039  status = cryptGetAttribute( cryptSession, CRYPT_SESSINFO_RESPONSE,
1040  &serverKey );
1041  if( cryptStatusOK( status ) )
1042  {
1043  cryptDestroyContext( serverKey );
1044  printf( "%sSession resumption didn't actually resume a previous "
1045  "session, line %d.\n", isServer ? "SVR: " : "",
1046  __LINE__ );
1047  return( FALSE );
1048  }
1049  }
1050 #endif /* !NO_SESSION_CACHE */
1051 
1052  /* Report the session security info */
1053  if( testType != SSL_TEST_MULTITHREAD )
1054  {
1055  const BOOLEAN isFirstSession = \
1056  ( testType == SSL_TEST_NORMAL && version == 0 ) ? TRUE : FALSE;
1057  int actualVersion;
1058 
1059 #ifdef NO_SESSION_CACHE
1060  if( !printSecurityInfo( cryptSession, isServer,
1061  ( testType != SSL_TEST_PSK && \
1062  testType != SSL_TEST_RESUME ),
1063  ( !isServer && testType != SSL_TEST_PSK && \
1064  testType != SSL_TEST_RESUME ),
1065  ( isServer && testType == SSL_TEST_CLIENTCERT ) ) )
1066  return( FALSE );
1067 #else
1068  if( !printSecurityInfo( cryptSession, isServer, isFirstSession,
1069  !isServer && isFirstSession, FALSE ) )
1070  return( FALSE );
1071 #endif /* NO_SESSION_CACHE */
1072  status = cryptGetAttribute( cryptSession, CRYPT_SESSINFO_VERSION,
1073  &actualVersion );
1074  if( cryptStatusOK( status ) && actualVersion != version )
1075  {
1076  printf( "Warning: Expected to connect using %s but only "
1077  "connected using %s.\n", versionStr[ version ],
1078  versionStr[ actualVersion ] );
1079  }
1080  }
1081 #ifdef NO_SESSION_CACHE
1082  if( ( !localSession && !isServer ) ||
1083  ( localSession && isServer && testType == SSL_TEST_CLIENTCERT ) )
1084 #else
1085  if( !localSession && !isServer )
1086 #endif /* NO_SESSION_CACHE */
1087  {
1089 
1090  status = cryptGetAttribute( cryptSession, CRYPT_SESSINFO_RESPONSE,
1091  &cryptCertificate );
1092  if( cryptStatusError( status ) )
1093  {
1094  printf( "%sCouldn't get %s certificate, status %d, line %d.\n",
1095  isServer ? "SVR: " : "", isServer ? "client" : "server",
1096  status, __LINE__ );
1097  return( FALSE );
1098  }
1099  puts( localSession ? "SVR: Client certificate details are:" : \
1100  "Server certificate details are:" );
1101  printCertChainInfo( cryptCertificate );
1102  cryptDestroyCert( cryptCertificate );
1103  }
1104  if( isServer && testType == SSL_TEST_PSK )
1105  {
1106  C_CHR userNameBuffer[ CRYPT_MAX_TEXTSIZE + 1 ];
1107  int length;
1108 
1109  status = cryptGetAttributeString( cryptSession,
1111  userNameBuffer, &length );
1112  if( cryptStatusError( status ) )
1113  {
1114  printf( "SVR: Couldn't read client user name, status %d, line "
1115  "%d.\n", status, __LINE__ );
1116  return( FALSE );
1117  }
1118 #ifdef UNICODE_STRINGS
1119  userNameBuffer[ length / sizeof( wchar_t ) ] = TEXT( '\0' );
1120  printf( "SVR: Client user name = '%S'.\n", userNameBuffer );
1121 #else
1122  userNameBuffer[ length ] = '\0';
1123  printf( "SVR: Client user name = '%s'.\n", userNameBuffer );
1124 #endif /* UNICODE_STRINGS */
1125  }
1126 
1127  /* Send data over the SSL/TLS link. If we're doing a bulk transfer
1128  we use fully asynchronous I/O to verify the timeout handling in
1129  the session code */
1130 #ifdef IS_HIGHVOLUME_SERVER
1131  /* This server has a large amount of data on it, used to test high-
1132  latency bulk transfers, so we set a larger timeout for the read */
1133  status = cryptSetAttribute( cryptSession, CRYPT_OPTION_NET_READTIMEOUT,
1134  15 );
1135 #else
1136  status = cryptSetAttribute( cryptSession, CRYPT_OPTION_NET_READTIMEOUT,
1137  ( testType == SSL_TEST_BULKTRANSER ) ? 0 : 5 );
1138 #endif /* IS_HIGHVOLUME_SERVER */
1139  if( cryptStatusError( status ) )
1140  {
1141  printExtError( cryptSession, isServer ? \
1142  "SVR: Session timeout set" : "Session timeout set",
1143  status, __LINE__ );
1144  return( FALSE );
1145  }
1146  if( testType == SSL_TEST_BULKTRANSER )
1147  {
1148  if( isServer )
1149  {
1150  long byteCount = 0;
1151 
1152  do
1153  {
1154  status = cryptPushData( cryptSession, bulkBuffer + byteCount,
1155  BULKDATA_BUFFER_SIZE - byteCount,
1156  &bytesCopied );
1157  byteCount += bytesCopied;
1158  }
1159  while( ( cryptStatusOK( status ) || \
1160  status == CRYPT_ERROR_TIMEOUT ) && \
1161  byteCount < BULKDATA_BUFFER_SIZE );
1162  if( cryptStatusError( status ) )
1163  {
1164  printExtError( cryptSession,
1165  "SVR: Send of bulk data to client", status,
1166  __LINE__ );
1167  return( FALSE );
1168  }
1169  status = cryptFlushData( cryptSession );
1170  if( cryptStatusError( status ) )
1171  {
1172  printExtError( cryptSession,
1173  "SVR: Flush of bulk data to client", status,
1174  __LINE__ );
1175  return( FALSE );
1176  }
1177  if( byteCount != BULKDATA_BUFFER_SIZE )
1178  {
1179  printf( "Only sent %ld of %ld bytes, line %d.\n", byteCount,
1180  BULKDATA_BUFFER_SIZE, __LINE__ );
1181  return( FALSE );
1182  }
1183  }
1184  else
1185  {
1186  long byteCount = 0;
1187 
1188  do
1189  {
1190  status = cryptPopData( cryptSession, bulkBuffer + byteCount,
1191  BULKDATA_BUFFER_SIZE - byteCount,
1192  &bytesCopied );
1193  byteCount += bytesCopied;
1194  }
1195  while( ( cryptStatusOK( status ) || \
1196  status == CRYPT_ERROR_TIMEOUT ) && \
1197  byteCount < BULKDATA_BUFFER_SIZE );
1198  if( cryptStatusError( status ) )
1199  {
1200  char strBuffer[ 256 ];
1201 
1202  sprintf( strBuffer, "Read of bulk data from server aborted "
1203  "after %ld of %ld bytes were read\n(last "
1204  "read = %d bytes), transfer",
1205  byteCount, BULKDATA_BUFFER_SIZE,
1206  bytesCopied );
1207  printExtError( cryptSession, strBuffer, status, __LINE__ );
1208  return( FALSE );
1209  }
1210  if( byteCount != BULKDATA_BUFFER_SIZE )
1211  {
1212  printf( "Only received %ld of %ld bytes, line %d.\n",
1213  byteCount, BULKDATA_BUFFER_SIZE, __LINE__ );
1214  return( FALSE );
1215  }
1216  if( !handleBulkBuffer( bulkBuffer, FALSE ) )
1217  {
1218  printf( "Received buffer contents don't match sent buffer "
1219  "contents, line %d.", __LINE__ );
1220  return( FALSE );
1221  }
1222  }
1223 
1224  free( bulkBuffer );
1225  }
1226  else
1227  {
1228  /* It's a standard transfer, send/receive and HTTP request/response.
1229  We clean up if we exit due to an error, if we're running a local
1230  loopback test the client and server threads can occasionally lose
1231  sync, which isn't a fatal error but can turn into a
1232  CRYPT_ERROR_INCOMPLETE once all the tests are finished */
1233  if( isServer )
1234  {
1235 #if defined( __MVS__ ) || defined( __VMCMS__ )
1236  #pragma convlit( resume )
1237 #endif /* IBM big iron */
1238 #if defined( __ILEC400__ )
1239  #pragma convert( 819 )
1240 #endif /* IBM medium iron */
1241  const char serverReply[] = \
1242  "HTTP/1.0 200 OK\n"
1243  "Date: Fri, 7 June 2005 20:02:07 GMT\n"
1244  "Server: cryptlib SSL/TLS test\n"
1245  "Content-Type: text/html\n"
1246  "Connection: Close\n"
1247  "\n"
1248  "<!DOCTYPE HTML SYSTEM \"html.dtd\">\n"
1249  "<html>\n"
1250  "<head>\n"
1251  "<title>cryptlib SSL/TLS test page</title>\n"
1252  "<body>\n"
1253  "Test message from the cryptlib SSL/TLS server.<p>\n"
1254  "</body>\n"
1255  "</html>\n";
1256 #if defined( __MVS__ ) || defined( __VMCMS__ )
1257  #pragma convlit( suspend )
1258 #endif /* IBM big iron */
1259 #if defined( __ILEC400__ )
1260  #pragma convert( 0 )
1261 #endif /* IBM medium iron */
1262 
1263  /* Print the text of the request from the client */
1264  status = cryptPopData( cryptSession, buffer, FILEBUFFER_SIZE,
1265  &bytesCopied );
1266  if( cryptStatusError( status ) )
1267  {
1268  printExtError( cryptSession, "SVR: Attempt to read data "
1269  "from client", status, __LINE__ );
1270  cryptDestroySession( cryptSession );
1271  return( FALSE );
1272  }
1273  buffer[ bytesCopied ] = '\0';
1274 #if defined( __MVS__ ) || defined( __VMCMS__ )
1275  asciiToEbcdic( buffer, bytesCopied );
1276 #endif /* EBCDIC systems */
1277  if( testType != SSL_TEST_MULTITHREAD )
1278  {
1279  printf( "---- Client sent %d bytes ----\n", bytesCopied );
1280  puts( buffer );
1281  puts( "---- End of output ----" );
1282  }
1283 
1284  /* Send a reply */
1285  status = cryptPushData( cryptSession, serverReply,
1286  sizeof( serverReply ) - 1, &bytesCopied );
1287  if( cryptStatusOK( status ) )
1288  status = cryptFlushData( cryptSession );
1289  if( cryptStatusError( status ) || \
1290  bytesCopied != sizeof( serverReply ) - 1 )
1291  {
1292  printExtError( cryptSession, "Attempt to send data to "
1293  "client", status, __LINE__ );
1294  cryptDestroySession( cryptSession );
1295  return( FALSE );
1296  }
1297 
1298  /* Wait for the data to be flushed through to the client before
1299  we close the session */
1300  delayThread( 1 );
1301  }
1302  else
1303  {
1304  char fetchString[ 128 ];
1305  int fetchStringLen;
1306 
1307  /* Send a fetch request to the server */
1308  if( testType == SSL_TEST_STARTTLS )
1309  {
1310  switch( protocol )
1311  {
1312  case PROTOCOL_SMTP:
1313  strcpy( fetchString, "EHLO foo.bar.com\r\n" );
1314  break;
1315 
1316  case PROTOCOL_POP:
1317  strcpy( fetchString, "CAPA\r\n" );
1318  break;
1319 
1320  case PROTOCOL_IMAP:
1321  strcpy( fetchString, "a003 CAPABILITY\r\n" );
1322  break;
1323 
1324  default:
1325  strcpy( fetchString, "USER test\r\n" );
1326  }
1327  }
1328  else
1329  {
1330  sprintf( fetchString, "GET %s HTTP/1.0\r\n\r\n",
1331  sslInfo[ SSL_SERVER_NO ].path );
1332  }
1333  fetchStringLen = strlen( fetchString );
1334 #if defined( __MVS__ ) || defined( __VMCMS__ )
1335  ebcdicToAscii( fetchString, fetchStringLen );
1336 #endif /* EBCDIC systems */
1337  status = cryptPushData( cryptSession, fetchString,
1338  fetchStringLen, &bytesCopied );
1339  if( cryptStatusOK( status ) )
1340  status = cryptFlushData( cryptSession );
1341  if( cryptStatusError( status ) || bytesCopied != fetchStringLen )
1342  {
1343  printExtError( cryptSession, "Attempt to send data to "
1344  "server", status, __LINE__ );
1345  cryptDestroySession( cryptSession );
1346  return( FALSE );
1347  }
1348 
1349  /* Print the text of the reply from the server */
1350  status = cryptPopData( cryptSession, buffer, FILEBUFFER_SIZE,
1351  &bytesCopied );
1352  if( cryptStatusError( status ) )
1353  {
1354  printExtError( cryptSession, "Attempt to read data from "
1355  "server", status, __LINE__ );
1356  cryptDestroySession( cryptSession );
1357  return( FALSE );
1358  }
1359  if( bytesCopied == 0 && testType != SSL_TEST_STARTTLS )
1360  {
1361  /* We've set a 5s timeout, we should get at least some
1362  data, however we allow this for the STARTTLS tests since
1363  the servers can exhibit all sorts of odd behaviour that
1364  we can't do much about with the partial client that we
1365  have here */
1366  puts( "Server returned no data in response to our request." );
1367  cryptDestroySession( cryptSession );
1368  return( FALSE );
1369  }
1370  buffer[ min( bytesCopied, 4096 ) ] = '\0';
1371 #if defined( __MVS__ ) || defined( __VMCMS__ )
1372  asciiToEbcdic( buffer, bytesCopied );
1373 #endif /* EBCDIC systems */
1374  if( testType != SSL_TEST_MULTITHREAD )
1375  {
1376  printf( "---- Server sent %d bytes ----\n", bytesCopied );
1377  puts( buffer );
1378  if( bytesCopied > 4096 )
1379  printf( " (Further %d bytes data omitted)\n",
1380  bytesCopied - 4096 );
1381  puts( "---- End of output ----" );
1382  }
1383 
1384 #ifdef IS_HIGHVOLUME_SERVER
1385  /* If we're reading a lot of data, more may have arrived in the
1386  meantime */
1387  status = cryptPopData( cryptSession, buffer, FILEBUFFER_SIZE,
1388  &bytesCopied );
1389  if( cryptStatusError( status ) )
1390  {
1391  if( status == CRYPT_ERROR_READ )
1392  {
1393  /* Since this is HTTP, the other side can close the
1394  connection with no further warning, even though SSL
1395  says you shouldn't really do this */
1396  puts( "Remote system closed connection." );
1397  }
1398  else
1399  {
1400  printExtError( cryptSession, "Attempt to read data from "
1401  "server", status, __LINE__ );
1402  cryptDestroySession( cryptSession );
1403  return( FALSE );
1404  }
1405  }
1406  else
1407  {
1408  buffer[ bytesCopied ] = '\0';
1409 #if defined( __MVS__ ) || defined( __VMCMS__ )
1410  asciiToEbcdic( buffer, bytesCopied );
1411 #endif /* EBCDIC systems */
1412  if( testType != SSL_TEST_MULTITHREAD )
1413  {
1414  printf( "---- Server sent further %d bytes ----\n",
1415  bytesCopied );
1416  puts( buffer );
1417  puts( "---- End of output ----" );
1418  }
1419  }
1420 #endif /* IS_HIGHVOLUME_SERVER */
1421 
1422  /* If it's a chatty protocol, exchange some more pleasantries */
1423  if( testType == SSL_TEST_STARTTLS )
1424  {
1425  switch( protocol )
1426  {
1427  case PROTOCOL_SMTP:
1428  strcpy( fetchString, "QUIT\r\n" );
1429  break;
1430 
1431  case PROTOCOL_POP:
1432  strcpy( fetchString, "USER test\r\n" );
1433  break;
1434 
1435  case PROTOCOL_IMAP:
1436  strcpy( fetchString, "a004 LOGIN test\r\n" );
1437  break;
1438 
1439  default:
1440  strcpy( fetchString, "QUIT\r\n" );
1441  }
1442  fetchStringLen = strlen( fetchString );
1443 #if defined( __MVS__ ) || defined( __VMCMS__ )
1444  ebcdicToAscii( fetchString, fetchStringLen );
1445 #endif /* EBCDIC systems */
1446  status = cryptPushData( cryptSession, fetchString,
1447  fetchStringLen, &bytesCopied );
1448  if( cryptStatusOK( status ) )
1449  status = cryptFlushData( cryptSession );
1450  if( cryptStatusError( status ) || bytesCopied != fetchStringLen )
1451  {
1452  printExtError( cryptSession, "Attempt to send data to "
1453  "server", status, __LINE__ );
1454  cryptDestroySession( cryptSession );
1455  return( FALSE );
1456  }
1457  status = cryptPopData( cryptSession, buffer, FILEBUFFER_SIZE,
1458  &bytesCopied );
1459  if( cryptStatusError( status ) )
1460  {
1461  printExtError( cryptSession, "Attempt to read data from "
1462  "server", status, __LINE__ );
1463  cryptDestroySession( cryptSession );
1464  return( FALSE );
1465  }
1466  buffer[ bytesCopied ] = '\0';
1467 #if defined( __MVS__ ) || defined( __VMCMS__ )
1468  asciiToEbcdic( buffer, bytesCopied );
1469 #endif /* EBCDIC systems */
1470  if( testType != SSL_TEST_MULTITHREAD )
1471  {
1472  printf( "---- Server sent %d bytes ----\n", bytesCopied );
1473  puts( buffer );
1474  puts( "---- End of output ----" );
1475  }
1476  }
1477  }
1478  }
1479 
1480  /* Clean up */
1481  status = cryptDestroySession( cryptSession );
1482  if( cryptStatusError( status ) )
1483  {
1484  printf( "cryptDestroySession() failed with error code %d, line %d.\n",
1485  status, __LINE__ );
1486  return( FALSE );
1487  }
1488 #if defined( __WINDOWS__ ) && !defined( _WIN32_WCE )
1489  if( testType == SSL_TEST_STARTTLS )
1490  {
1491  closesocket( netSocket );
1492  WSACleanup();
1493  }
1494 #endif /* __WINDOWS__ && !_WIN32_WCE */
1495 
1496  if( sessionID != CRYPT_UNUSED )
1497  printf( "%02d: ", sessionID );
1498  printf( "%s%s session succeeded.\n", isServer ? "SVR: " : "",
1499  versionStr[ version ] );
1500  if( testType != SSL_TEST_MULTITHREAD )
1501  putchar( '\n' );
1502  return( TRUE );
1503  }
1504 
1505 int testSessionSSL( void )
1506  {
1507  return( connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_NORMAL, 0, CRYPT_UNUSED, FALSE ) );
1508  }
1509 int testSessionSSLLocalSocket( void )
1510  {
1511  return( connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_STARTTLS, 0, CRYPT_UNUSED, FALSE ) );
1512  }
1513 int testSessionSSLClientCert( void )
1514  {
1515  return( connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_CLIENTCERT, 0, CRYPT_UNUSED, FALSE ) );
1516  }
1517 int testSessionSSLSharedKey( void )
1518  {
1519  return( connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_PSK, 0, CRYPT_UNUSED, FALSE ) );
1520  }
1521 
1522 int testSessionSSLServer( void )
1523  {
1524  int status;
1525 
1526  createMutex();
1527  status = connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_NORMAL, 0, CRYPT_UNUSED, FALSE );
1528  destroyMutex();
1529 
1530  return( status );
1531  }
1532 int testSessionSSLServerCached( void )
1533  {
1534  int status;
1535 
1536  /* Run the server twice to check session cacheing. Testing this
1537  requires manual reconnection with a browser to localhost, since it's
1538  too complex to handle easily via a loopback test. Note that with
1539  MSIE this will require three lots of connects rather than two,
1540  because it handles an unknown certificate by doing a resume, which
1541  consumes two lots of sessions, and then the third one is the actual
1542  session resume */
1543  createMutex();
1544  status = connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_NORMAL, 0, CRYPT_UNUSED, FALSE );
1545  if( status > 0 )
1546  status = connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_NORMAL, 0, CRYPT_UNUSED, FALSE );
1547  destroyMutex();
1548 
1549  return( status );
1550  }
1552  {
1553  int status;
1554 
1555  createMutex();
1556  status = connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_CLIENTCERT, 0, CRYPT_UNUSED, FALSE );
1557  destroyMutex();
1558 
1559  return( status );
1560  }
1561 
1562 int testSessionTLS( void )
1563  {
1564  return( connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_NORMAL, 1, CRYPT_UNUSED, FALSE ) );
1565  }
1566 
1567 int testSessionTLSServer( void )
1568  {
1569  int status;
1570 
1571  createMutex();
1572  status = connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_NORMAL, 1, CRYPT_UNUSED, FALSE );
1573  destroyMutex();
1574 
1575  return( status );
1576  }
1578  {
1579  int status;
1580 
1581  createMutex();
1582  status = connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_PSK, 1, CRYPT_UNUSED, FALSE );
1583  destroyMutex();
1584 
1585  return( status );
1586  }
1587 
1588 int testSessionTLS11( void )
1589  {
1590  return( connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_NORMAL, 2, CRYPT_UNUSED, FALSE ) );
1591  }
1592 int testSessionTLS11Server( void )
1593  {
1594  int status;
1595 
1596  createMutex();
1597  status = connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_NORMAL, 2, CRYPT_UNUSED, FALSE );
1598  destroyMutex();
1599 
1600  return( status );
1601  }
1602 
1603 int testSessionTLS12( void )
1604  {
1605  return( connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_NORMAL, 3, CRYPT_UNUSED, FALSE ) );
1606  }
1607 int testSessionTLS12ClientCert( void )
1608  {
1609  return( connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_CLIENTCERT, 3, CRYPT_UNUSED, FALSE ) );
1610  }
1611 int testSessionTLS12Server( void )
1612  {
1613  int status;
1614 
1615  createMutex();
1616  status = connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_NORMAL, 3, CRYPT_UNUSED, FALSE );
1617  destroyMutex();
1618 
1619  return( status );
1620  }
1621 
1622 /* Perform a client/server loopback test */
1623 
1624 #ifdef WINDOWS_THREADS
1625 
1626 unsigned __stdcall sslServerThread( void *dummy )
1627  {
1629  _endthreadex( 0 );
1630  return( 0 );
1631  }
1632 
1633 int testSessionSSLClientServer( void )
1634  {
1635  HANDLE hThread;
1636  unsigned threadID;
1637  int status;
1638 
1639  /* Start the server */
1640  createMutex();
1641  hThread = ( HANDLE ) _beginthreadex( NULL, 0, sslServerThread,
1642  NULL, 0, &threadID );
1643  Sleep( 1000 );
1644 
1645  /* Connect to the local server */
1646  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_NORMAL, 0, CRYPT_UNUSED, TRUE );
1647  waitForThread( hThread );
1648  destroyMutex();
1649  return( status );
1650  }
1651 
1652 unsigned __stdcall sslClientCertServerThread( void *dummy )
1653  {
1655  _endthreadex( 0 );
1656  return( 0 );
1657  }
1658 
1660  {
1661  HANDLE hThread;
1662  unsigned threadID;
1663  int status;
1664 
1665  /* Start the server */
1666  createMutex();
1667  hThread = ( HANDLE ) _beginthreadex( NULL, 0, sslClientCertServerThread,
1668  NULL, 0, &threadID );
1669  Sleep( 1000 );
1670 
1671  /* Connect to the local server */
1672  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_CLIENTCERT, 0, CRYPT_UNUSED, TRUE );
1673  waitForThread( hThread );
1674  destroyMutex();
1675  return( status );
1676  }
1677 
1678 unsigned __stdcall tlsServerThread( void *dummy )
1679  {
1681  _endthreadex( 0 );
1682  return( 0 );
1683  }
1684 
1685 int testSessionTLSClientServer( void )
1686  {
1687  HANDLE hThread;
1688  unsigned threadID;
1689  int status;
1690 
1691  /* Start the server */
1692  createMutex();
1693  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tlsServerThread,
1694  NULL, 0, &threadID );
1695  Sleep( 1000 );
1696 
1697  /* Connect to the local server */
1698  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_NORMAL, 1, CRYPT_UNUSED, TRUE );
1699  waitForThread( hThread );
1700  destroyMutex();
1701  return( status );
1702  }
1703 
1704 unsigned __stdcall tlsSharedKeyServerThread( void *dummy )
1705  {
1706  connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_PSK, 1, CRYPT_UNUSED, TRUE );
1707  _endthreadex( 0 );
1708  return( 0 );
1709  }
1710 
1712  {
1713  HANDLE hThread;
1714  unsigned threadID;
1715  int status;
1716 
1717  /* Start the server */
1718  createMutex();
1719  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tlsSharedKeyServerThread,
1720  NULL, 0, &threadID );
1721  Sleep( 1000 );
1722 
1723  /* Connect to the local server */
1724  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_PSK, 1, CRYPT_UNUSED, TRUE );
1725  waitForThread( hThread );
1726  destroyMutex();
1727  return( status );
1728  }
1729 
1730 unsigned __stdcall tlsNoSharedKeyServerThread( void *arg )
1731  {
1732  int testType = *( ( int * ) arg );
1733 
1734  connectSSLTLS( CRYPT_SESSION_SSL_SERVER, testType, 1, CRYPT_UNUSED, TRUE );
1735  _endthreadex( 0 );
1736  return( 0 );
1737  }
1738 
1740  {
1741  HANDLE hThread;
1742  unsigned threadID;
1743  int arg, status;
1744 
1745  /* Start the server */
1746  createMutex();
1747  arg = SSL_TEST_PSK_CLIONLY;
1748  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tlsNoSharedKeyServerThread,
1749  &arg, 0, &threadID );
1750  Sleep( 1000 );
1751 
1752  /* Connect to the local server */
1753  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_PSK_CLIONLY, 1, CRYPT_UNUSED, TRUE );
1754  waitForThread( hThread );
1755  destroyMutex();
1756  if( status != TRUE )
1757  return( status );
1758 
1759  /* Restart the server */
1760  createMutex();
1761  arg = SSL_TEST_PSK_SVRONLY;
1762  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tlsNoSharedKeyServerThread,
1763  &arg, 0, &threadID );
1764  Sleep( 1000 );
1765 
1766  /* Connect to the local server */
1767  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_PSK_SVRONLY, 1, CRYPT_UNUSED, TRUE );
1768  waitForThread( hThread );
1769  destroyMutex();
1770  return( status );
1771  }
1772 
1773 unsigned __stdcall tlsBulkTransferServerThread( void *dummy )
1774  {
1776  _endthreadex( 0 );
1777  return( 0 );
1778  }
1779 
1781  {
1782  HANDLE hThread;
1783  unsigned threadID;
1784  int status;
1785 
1786  /* Start the server */
1787  createMutex();
1788  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tlsBulkTransferServerThread,
1789  NULL, 0, &threadID );
1790  Sleep( 1000 );
1791 
1792  /* Connect to the local server */
1793  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_BULKTRANSER, 1, CRYPT_UNUSED, TRUE );
1794  waitForThread( hThread );
1795  destroyMutex();
1796  return( status );
1797  }
1798 
1799 unsigned __stdcall tlsResumeServerThread( void *dummy )
1800  {
1802  _endthreadex( 0 );
1803  return( 0 );
1804  }
1805 
1807  {
1808  HANDLE hThread;
1809  unsigned threadID;
1810  int status;
1811 
1812  /* Start the server */
1813  createMutex();
1814  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tlsServerThread,
1815  NULL, 0, &threadID );
1816  Sleep( 1000 );
1817 
1818  /* Connect to the local server */
1819  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_RESUME, 1, CRYPT_UNUSED, TRUE );
1820  waitForThread( hThread );
1821  destroyMutex();
1822  return( status );
1823  }
1824 
1825 unsigned __stdcall tls11ServerThread( void *dummy )
1826  {
1828  _endthreadex( 0 );
1829  return( 0 );
1830  }
1831 
1832 int testSessionTLS11ClientServer( void )
1833  {
1834  HANDLE hThread;
1835  unsigned threadID;
1836  int status;
1837 
1838  /* Start the server */
1839  createMutex();
1840  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tls11ServerThread,
1841  NULL, 0, &threadID );
1842  Sleep( 1000 );
1843 
1844  /* Connect to the local server */
1845  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_NORMAL, 2, CRYPT_UNUSED, TRUE );
1846  waitForThread( hThread );
1847  destroyMutex();
1848  return( status );
1849  }
1850 
1851 unsigned __stdcall tls12ServerThread( void *dummy )
1852  {
1854  _endthreadex( 0 );
1855  return( 0 );
1856  }
1857 
1858 int testSessionTLS12ClientServer( void )
1859  {
1860  HANDLE hThread;
1861  unsigned threadID;
1862  int status;
1863 
1864  /* Start the server */
1865  createMutex();
1866  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tls12ServerThread,
1867  NULL, 0, &threadID );
1868  Sleep( 1000 );
1869 
1870  /* Connect to the local server */
1871  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_NORMAL, 3, CRYPT_UNUSED, TRUE );
1872  waitForThread( hThread );
1873  destroyMutex();
1874  return( status );
1875  }
1876 
1877 unsigned __stdcall tls12ServerEccThread( void *dummy )
1878  {
1879  connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_ECC, 3, CRYPT_UNUSED, TRUE );
1880  _endthreadex( 0 );
1881  return( 0 );
1882  }
1883 
1885  {
1886  HANDLE hThread;
1887  unsigned threadID;
1888  int status;
1889 
1891  NULL ) == CRYPT_ERROR_NOTAVAIL )
1892  {
1893  puts( "ECC is disabled in this build of cryptlib, skipping TLS ECC "
1894  "test." );
1895  return( TRUE );
1896  }
1897 
1898  /* Start the server */
1899  createMutex();
1900  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tls12ServerEccThread,
1901  NULL, 0, &threadID );
1902  Sleep( 1000 );
1903 
1904  /* Connect to the local server */
1905  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_ECC, 3, CRYPT_UNUSED, TRUE );
1906  waitForThread( hThread );
1907  destroyMutex();
1908  return( status );
1909  }
1910 
1911 unsigned __stdcall tls12ServerEcc384Thread( void *dummy )
1912  {
1914  _endthreadex( 0 );
1915  return( 0 );
1916  }
1917 
1919  {
1920  HANDLE hThread;
1921  unsigned threadID;
1922  int status;
1923 
1925  NULL ) == CRYPT_ERROR_NOTAVAIL )
1926  {
1927  puts( "ECC is disabled in this build of cryptlib, skipping TLS ECC "
1928  "test." );
1929  return( TRUE );
1930  }
1931 
1932  /* Start the server */
1933  createMutex();
1934  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tls12ServerEcc384Thread,
1935  NULL, 0, &threadID );
1936  Sleep( 1000 );
1937 
1938  /* Connect to the local server */
1939  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_ECC_P384, 3, CRYPT_UNUSED, TRUE );
1940  waitForThread( hThread );
1941  destroyMutex();
1942  return( status );
1943  }
1944 
1945 unsigned __stdcall tls12ClientCertServerThread( void *dummy )
1946  {
1948  _endthreadex( 0 );
1949  return( 0 );
1950  }
1951 
1953  {
1954  HANDLE hThread;
1955  unsigned threadID;
1956  int status;
1957 
1958  /* Start the server */
1959  createMutex();
1960  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tls12ClientCertServerThread,
1961  NULL, 0, &threadID );
1962  Sleep( 1000 );
1963 
1964  /* Connect to the local server */
1965  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_CLIENTCERT, 3, CRYPT_UNUSED, TRUE );
1966  waitForThread( hThread );
1967  destroyMutex();
1968  return( status );
1969  }
1970 
1971 unsigned __stdcall tlsServerDualThread2( void *dummy )
1972  {
1973  connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_DUALTHREAD, 1, 0, TRUE );
1974  _endthreadex( 0 );
1975  return( 0 );
1976  }
1977 
1978 unsigned __stdcall tlsServerDualThread1( void *dummy )
1979  {
1981  _endthreadex( 0 );
1982  return( 0 );
1983  }
1984 
1986  {
1987  HANDLE hThread;
1988  unsigned threadID;
1989  int status;
1990 
1991  /* Start the server */
1992  createMutex();
1993  hThread = ( HANDLE ) _beginthreadex( NULL, 0, tlsServerDualThread1,
1994  NULL, 0, &threadID );
1995  Sleep( 1000 );
1996 
1997  /* Connect to the local server */
1998  status = connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_PSK, 1, CRYPT_UNUSED, TRUE );
1999  waitForThread( hThread );
2000  destroyMutex();
2001  return( status );
2002  }
2003 
2004 unsigned __stdcall tlsServerMultiThread( void *threadIdPtr )
2005  {
2006  int threadID = *( ( int * ) threadIdPtr );
2007 
2008  connectSSLTLS( CRYPT_SESSION_SSL_SERVER, SSL_TEST_MULTITHREAD, 1, threadID, TRUE );
2009  _endthreadex( 0 );
2010  return( 0 );
2011  }
2012 
2013 unsigned __stdcall tlsClientMultiThread( void *threadIdPtr )
2014  {
2015  int threadID = *( ( int * ) threadIdPtr );
2016 
2017  connectSSLTLS( CRYPT_SESSION_SSL, SSL_TEST_MULTITHREAD, 1, threadID, TRUE );
2018  _endthreadex( 0 );
2019  return( 0 );
2020  }
2021 
2023  {
2024  return( multiThreadDispatch( tlsClientMultiThread,
2025  tlsServerMultiThread, MAX_NO_THREADS ) );
2026  }
2027 #endif /* WINDOWS_THREADS */
2028 
2029 #endif /* TEST_SESSION || TEST_SESSION_LOOPBACK */