xref: /minix3/crypto/external/bsd/openssl/dist/apps/s_time.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* apps/s_time.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc #define NO_SHUTDOWN
60ebfedea0SLionel Sambuc 
61*0a6a1f1dSLionel Sambuc /* ----------------------------------------
62ebfedea0SLionel Sambuc    s_time - SSL client connection timer program
63ebfedea0SLionel Sambuc    Written and donated by Larry Streepy <streepy@healthcare.com>
64ebfedea0SLionel Sambuc   -----------------------------------------*/
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc #include <stdio.h>
67ebfedea0SLionel Sambuc #include <stdlib.h>
68ebfedea0SLionel Sambuc #include <string.h>
69ebfedea0SLionel Sambuc 
70ebfedea0SLionel Sambuc #define USE_SOCKETS
71ebfedea0SLionel Sambuc #include "apps.h"
72ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_STDIO
73ebfedea0SLionel Sambuc # define APPS_WIN16
74ebfedea0SLionel Sambuc #endif
75ebfedea0SLionel Sambuc #include <openssl/x509.h>
76ebfedea0SLionel Sambuc #include <openssl/ssl.h>
77ebfedea0SLionel Sambuc #include <openssl/pem.h>
78ebfedea0SLionel Sambuc #include "s_apps.h"
79ebfedea0SLionel Sambuc #include <openssl/err.h>
80ebfedea0SLionel Sambuc #ifdef WIN32_STUFF
81ebfedea0SLionel Sambuc # include "winmain.h"
82ebfedea0SLionel Sambuc # include "wintext.h"
83ebfedea0SLionel Sambuc #endif
84ebfedea0SLionel Sambuc #if !defined(OPENSSL_SYS_MSDOS)
85ebfedea0SLionel Sambuc # include <unistd.h>
86ebfedea0SLionel Sambuc #endif
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc #undef PROG
89ebfedea0SLionel Sambuc #define PROG s_time_main
90ebfedea0SLionel Sambuc 
91ebfedea0SLionel Sambuc #undef ioctl
92ebfedea0SLionel Sambuc #define ioctl ioctlsocket
93ebfedea0SLionel Sambuc 
94ebfedea0SLionel Sambuc #define SSL_CONNECT_NAME        "localhost:4433"
95ebfedea0SLionel Sambuc 
96*0a6a1f1dSLionel Sambuc /* no default cert. */
97*0a6a1f1dSLionel Sambuc /*
98*0a6a1f1dSLionel Sambuc  * #define TEST_CERT "client.pem"
99*0a6a1f1dSLionel Sambuc  */
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc #undef BUFSIZZ
102ebfedea0SLionel Sambuc #define BUFSIZZ 1024*10
103ebfedea0SLionel Sambuc 
104ebfedea0SLionel Sambuc #define MYBUFSIZ 1024*8
105ebfedea0SLionel Sambuc 
106ebfedea0SLionel Sambuc #undef min
107ebfedea0SLionel Sambuc #undef max
108ebfedea0SLionel Sambuc #define min(a,b) (((a) < (b)) ? (a) : (b))
109ebfedea0SLionel Sambuc #define max(a,b) (((a) > (b)) ? (a) : (b))
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc #undef SECONDS
112ebfedea0SLionel Sambuc #define SECONDS 30
113ebfedea0SLionel Sambuc extern int verify_depth;
114ebfedea0SLionel Sambuc extern int verify_error;
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc static void s_time_usage(void);
117ebfedea0SLionel Sambuc static int parseArgs(int argc, char **argv);
118ebfedea0SLionel Sambuc static SSL *doConnection(SSL *scon);
119ebfedea0SLionel Sambuc static void s_time_init(void);
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc /***********************************************************************
122ebfedea0SLionel Sambuc  * Static data declarations
123ebfedea0SLionel Sambuc  */
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc /* static char *port=PORT_STR;*/
126ebfedea0SLionel Sambuc static char *host = SSL_CONNECT_NAME;
127ebfedea0SLionel Sambuc static char *t_cert_file = NULL;
128ebfedea0SLionel Sambuc static char *t_key_file = NULL;
129ebfedea0SLionel Sambuc static char *CApath = NULL;
130ebfedea0SLionel Sambuc static char *CAfile = NULL;
131ebfedea0SLionel Sambuc static char *tm_cipher = NULL;
132ebfedea0SLionel Sambuc static int tm_verify = SSL_VERIFY_NONE;
133ebfedea0SLionel Sambuc static int maxTime = SECONDS;
134ebfedea0SLionel Sambuc static SSL_CTX *tm_ctx = NULL;
135ebfedea0SLionel Sambuc static const SSL_METHOD *s_time_meth = NULL;
136ebfedea0SLionel Sambuc static char *s_www_path = NULL;
137ebfedea0SLionel Sambuc static long bytes_read = 0;
138ebfedea0SLionel Sambuc static int st_bugs = 0;
139ebfedea0SLionel Sambuc static int perform = 0;
140ebfedea0SLionel Sambuc #ifdef FIONBIO
141ebfedea0SLionel Sambuc static int t_nbio = 0;
142ebfedea0SLionel Sambuc #endif
143ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_WIN32
144ebfedea0SLionel Sambuc static int exitNow = 0;         /* Set when it's time to exit main */
145ebfedea0SLionel Sambuc #endif
146ebfedea0SLionel Sambuc 
s_time_init(void)147ebfedea0SLionel Sambuc static void s_time_init(void)
148ebfedea0SLionel Sambuc {
149ebfedea0SLionel Sambuc     host = SSL_CONNECT_NAME;
150ebfedea0SLionel Sambuc     t_cert_file = NULL;
151ebfedea0SLionel Sambuc     t_key_file = NULL;
152ebfedea0SLionel Sambuc     CApath = NULL;
153ebfedea0SLionel Sambuc     CAfile = NULL;
154ebfedea0SLionel Sambuc     tm_cipher = NULL;
155ebfedea0SLionel Sambuc     tm_verify = SSL_VERIFY_NONE;
156ebfedea0SLionel Sambuc     maxTime = SECONDS;
157ebfedea0SLionel Sambuc     tm_ctx = NULL;
158ebfedea0SLionel Sambuc     s_time_meth = NULL;
159ebfedea0SLionel Sambuc     s_www_path = NULL;
160ebfedea0SLionel Sambuc     bytes_read = 0;
161ebfedea0SLionel Sambuc     st_bugs = 0;
162ebfedea0SLionel Sambuc     perform = 0;
163ebfedea0SLionel Sambuc 
164ebfedea0SLionel Sambuc #ifdef FIONBIO
165ebfedea0SLionel Sambuc     t_nbio = 0;
166ebfedea0SLionel Sambuc #endif
167ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_WIN32
168ebfedea0SLionel Sambuc     exitNow = 0;                /* Set when it's time to exit main */
169ebfedea0SLionel Sambuc #endif
170ebfedea0SLionel Sambuc }
171ebfedea0SLionel Sambuc 
172ebfedea0SLionel Sambuc /***********************************************************************
173ebfedea0SLionel Sambuc  * usage - display usage message
174ebfedea0SLionel Sambuc  */
s_time_usage(void)175ebfedea0SLionel Sambuc static void s_time_usage(void)
176ebfedea0SLionel Sambuc {
177ebfedea0SLionel Sambuc     static const char umsg[] = "\
178ebfedea0SLionel Sambuc -time arg     - max number of seconds to collect data, default %d\n\
179ebfedea0SLionel Sambuc -verify arg   - turn on peer certificate verification, arg == depth\n\
180ebfedea0SLionel Sambuc -cert arg     - certificate file to use, PEM format assumed\n\
181ebfedea0SLionel Sambuc -key arg      - RSA file to use, PEM format assumed, key is in cert file\n\
182ebfedea0SLionel Sambuc                 file if not specified by this option\n\
183ebfedea0SLionel Sambuc -CApath arg   - PEM format directory of CA's\n\
184ebfedea0SLionel Sambuc -CAfile arg   - PEM format file of CA's\n\
185ebfedea0SLionel Sambuc -cipher       - preferred cipher to use, play with 'openssl ciphers'\n\n";
186ebfedea0SLionel Sambuc 
187ebfedea0SLionel Sambuc     printf("usage: s_time <args>\n\n");
188ebfedea0SLionel Sambuc 
189*0a6a1f1dSLionel Sambuc     printf("-connect host:port - host:port to connect to (default is %s)\n",
190*0a6a1f1dSLionel Sambuc            SSL_CONNECT_NAME);
191ebfedea0SLionel Sambuc #ifdef FIONBIO
192ebfedea0SLionel Sambuc     printf("-nbio         - Run with non-blocking IO\n");
193ebfedea0SLionel Sambuc     printf("-ssl2         - Just use SSLv2\n");
194ebfedea0SLionel Sambuc     printf("-ssl3         - Just use SSLv3\n");
195ebfedea0SLionel Sambuc     printf("-bugs         - Turn on SSL bug compatibility\n");
196ebfedea0SLionel Sambuc     printf("-new          - Just time new connections\n");
197ebfedea0SLionel Sambuc     printf("-reuse        - Just time connection reuse\n");
198ebfedea0SLionel Sambuc     printf("-www page     - Retrieve 'page' from the site\n");
199ebfedea0SLionel Sambuc #endif
200ebfedea0SLionel Sambuc     printf(umsg, SECONDS);
201ebfedea0SLionel Sambuc }
202ebfedea0SLionel Sambuc 
203ebfedea0SLionel Sambuc /***********************************************************************
204ebfedea0SLionel Sambuc  * parseArgs - Parse command line arguments and initialize data
205ebfedea0SLionel Sambuc  *
206ebfedea0SLionel Sambuc  * Returns 0 if ok, -1 on bad args
207ebfedea0SLionel Sambuc  */
parseArgs(int argc,char ** argv)208ebfedea0SLionel Sambuc static int parseArgs(int argc, char **argv)
209ebfedea0SLionel Sambuc {
210ebfedea0SLionel Sambuc     int badop = 0;
211ebfedea0SLionel Sambuc 
212ebfedea0SLionel Sambuc     verify_depth = 0;
213ebfedea0SLionel Sambuc     verify_error = X509_V_OK;
214ebfedea0SLionel Sambuc 
215ebfedea0SLionel Sambuc     argc--;
216ebfedea0SLionel Sambuc     argv++;
217ebfedea0SLionel Sambuc 
218ebfedea0SLionel Sambuc     while (argc >= 1) {
219*0a6a1f1dSLionel Sambuc         if (strcmp(*argv, "-connect") == 0) {
220*0a6a1f1dSLionel Sambuc             if (--argc < 1)
221*0a6a1f1dSLionel Sambuc                 goto bad;
222ebfedea0SLionel Sambuc             host = *(++argv);
223ebfedea0SLionel Sambuc         }
224ebfedea0SLionel Sambuc #if 0
225*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-host") == 0) {
226*0a6a1f1dSLionel Sambuc             if (--argc < 1)
227*0a6a1f1dSLionel Sambuc                 goto bad;
228ebfedea0SLionel Sambuc             host = *(++argv);
229*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-port") == 0) {
230*0a6a1f1dSLionel Sambuc             if (--argc < 1)
231*0a6a1f1dSLionel Sambuc                 goto bad;
232ebfedea0SLionel Sambuc             port = *(++argv);
233ebfedea0SLionel Sambuc         }
234ebfedea0SLionel Sambuc #endif
235ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-reuse") == 0)
236ebfedea0SLionel Sambuc             perform = 2;
237ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-new") == 0)
238ebfedea0SLionel Sambuc             perform = 1;
239ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-verify") == 0) {
240ebfedea0SLionel Sambuc 
241ebfedea0SLionel Sambuc             tm_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
242*0a6a1f1dSLionel Sambuc             if (--argc < 1)
243*0a6a1f1dSLionel Sambuc                 goto bad;
244ebfedea0SLionel Sambuc             verify_depth = atoi(*(++argv));
245ebfedea0SLionel Sambuc             BIO_printf(bio_err, "verify depth is %d\n", verify_depth);
246ebfedea0SLionel Sambuc 
247ebfedea0SLionel Sambuc         } else if (strcmp(*argv, "-cert") == 0) {
248ebfedea0SLionel Sambuc 
249*0a6a1f1dSLionel Sambuc             if (--argc < 1)
250*0a6a1f1dSLionel Sambuc                 goto bad;
251ebfedea0SLionel Sambuc             t_cert_file = *(++argv);
252ebfedea0SLionel Sambuc 
253ebfedea0SLionel Sambuc         } else if (strcmp(*argv, "-key") == 0) {
254ebfedea0SLionel Sambuc 
255*0a6a1f1dSLionel Sambuc             if (--argc < 1)
256*0a6a1f1dSLionel Sambuc                 goto bad;
257ebfedea0SLionel Sambuc             t_key_file = *(++argv);
258ebfedea0SLionel Sambuc 
259ebfedea0SLionel Sambuc         } else if (strcmp(*argv, "-CApath") == 0) {
260ebfedea0SLionel Sambuc 
261*0a6a1f1dSLionel Sambuc             if (--argc < 1)
262*0a6a1f1dSLionel Sambuc                 goto bad;
263ebfedea0SLionel Sambuc             CApath = *(++argv);
264ebfedea0SLionel Sambuc 
265ebfedea0SLionel Sambuc         } else if (strcmp(*argv, "-CAfile") == 0) {
266ebfedea0SLionel Sambuc 
267*0a6a1f1dSLionel Sambuc             if (--argc < 1)
268*0a6a1f1dSLionel Sambuc                 goto bad;
269ebfedea0SLionel Sambuc             CAfile = *(++argv);
270ebfedea0SLionel Sambuc 
271ebfedea0SLionel Sambuc         } else if (strcmp(*argv, "-cipher") == 0) {
272ebfedea0SLionel Sambuc 
273*0a6a1f1dSLionel Sambuc             if (--argc < 1)
274*0a6a1f1dSLionel Sambuc                 goto bad;
275ebfedea0SLionel Sambuc             tm_cipher = *(++argv);
276ebfedea0SLionel Sambuc         }
277ebfedea0SLionel Sambuc #ifdef FIONBIO
278ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-nbio") == 0) {
279ebfedea0SLionel Sambuc             t_nbio = 1;
280ebfedea0SLionel Sambuc         }
281ebfedea0SLionel Sambuc #endif
282*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-www") == 0) {
283*0a6a1f1dSLionel Sambuc             if (--argc < 1)
284*0a6a1f1dSLionel Sambuc                 goto bad;
285ebfedea0SLionel Sambuc             s_www_path = *(++argv);
286*0a6a1f1dSLionel Sambuc             if (strlen(s_www_path) > MYBUFSIZ - 100) {
287ebfedea0SLionel Sambuc                 BIO_printf(bio_err, "-www option too long\n");
288ebfedea0SLionel Sambuc                 badop = 1;
289ebfedea0SLionel Sambuc             }
290*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-bugs") == 0)
291ebfedea0SLionel Sambuc             st_bugs = 1;
292ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SSL2
293ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-ssl2") == 0)
294ebfedea0SLionel Sambuc             s_time_meth = SSLv2_client_method();
295ebfedea0SLionel Sambuc #endif
296ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SSL3
297ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-ssl3") == 0)
298ebfedea0SLionel Sambuc             s_time_meth = SSLv3_client_method();
299ebfedea0SLionel Sambuc #endif
300ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-time") == 0) {
301ebfedea0SLionel Sambuc 
302*0a6a1f1dSLionel Sambuc             if (--argc < 1)
303*0a6a1f1dSLionel Sambuc                 goto bad;
304ebfedea0SLionel Sambuc             maxTime = atoi(*(++argv));
305*0a6a1f1dSLionel Sambuc             if (maxTime <= 0) {
306*0a6a1f1dSLionel Sambuc                 BIO_printf(bio_err, "time must be > 0\n");
307*0a6a1f1dSLionel Sambuc                 badop = 1;
308ebfedea0SLionel Sambuc             }
309*0a6a1f1dSLionel Sambuc         } else {
310ebfedea0SLionel Sambuc             BIO_printf(bio_err, "unknown option %s\n", *argv);
311ebfedea0SLionel Sambuc             badop = 1;
312ebfedea0SLionel Sambuc             break;
313ebfedea0SLionel Sambuc         }
314ebfedea0SLionel Sambuc 
315ebfedea0SLionel Sambuc         argc--;
316ebfedea0SLionel Sambuc         argv++;
317ebfedea0SLionel Sambuc     }
318ebfedea0SLionel Sambuc 
319*0a6a1f1dSLionel Sambuc     if (perform == 0)
320*0a6a1f1dSLionel Sambuc         perform = 3;
321ebfedea0SLionel Sambuc 
322ebfedea0SLionel Sambuc     if (badop) {
323ebfedea0SLionel Sambuc  bad:
324ebfedea0SLionel Sambuc         s_time_usage();
325ebfedea0SLionel Sambuc         return -1;
326ebfedea0SLionel Sambuc     }
327ebfedea0SLionel Sambuc 
328ebfedea0SLionel Sambuc     return 0;                   /* Valid args */
329ebfedea0SLionel Sambuc }
330ebfedea0SLionel Sambuc 
331ebfedea0SLionel Sambuc /***********************************************************************
332ebfedea0SLionel Sambuc  * TIME - time functions
333ebfedea0SLionel Sambuc  */
334ebfedea0SLionel Sambuc #define START   0
335ebfedea0SLionel Sambuc #define STOP    1
336ebfedea0SLionel Sambuc 
tm_Time_F(int s)337ebfedea0SLionel Sambuc static double tm_Time_F(int s)
338ebfedea0SLionel Sambuc {
339ebfedea0SLionel Sambuc     return app_tminterval(s, 1);
340ebfedea0SLionel Sambuc }
341ebfedea0SLionel Sambuc 
342ebfedea0SLionel Sambuc /***********************************************************************
343ebfedea0SLionel Sambuc  * MAIN - main processing area for client
344ebfedea0SLionel Sambuc  *                      real name depends on MONOLITH
345ebfedea0SLionel Sambuc  */
346ebfedea0SLionel Sambuc int MAIN(int, char **);
347ebfedea0SLionel Sambuc 
MAIN(int argc,char ** argv)348ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
349ebfedea0SLionel Sambuc {
350ebfedea0SLionel Sambuc     double totalTime = 0.0;
351ebfedea0SLionel Sambuc     int nConn = 0;
352ebfedea0SLionel Sambuc     SSL *scon = NULL;
353ebfedea0SLionel Sambuc     long finishtime = 0;
354ebfedea0SLionel Sambuc     int ret = 1, i;
355ebfedea0SLionel Sambuc     MS_STATIC char buf[1024 * 8];
356ebfedea0SLionel Sambuc     int ver;
357ebfedea0SLionel Sambuc 
358ebfedea0SLionel Sambuc     apps_startup();
359ebfedea0SLionel Sambuc     s_time_init();
360ebfedea0SLionel Sambuc 
361ebfedea0SLionel Sambuc     if (bio_err == NULL)
362ebfedea0SLionel Sambuc         bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
363ebfedea0SLionel Sambuc 
364ebfedea0SLionel Sambuc     s_time_meth = SSLv23_client_method();
365ebfedea0SLionel Sambuc 
366ebfedea0SLionel Sambuc     /* parse the command line arguments */
367ebfedea0SLionel Sambuc     if (parseArgs(argc, argv) < 0)
368ebfedea0SLionel Sambuc         goto end;
369ebfedea0SLionel Sambuc 
370ebfedea0SLionel Sambuc     OpenSSL_add_ssl_algorithms();
371*0a6a1f1dSLionel Sambuc     if ((tm_ctx = SSL_CTX_new(s_time_meth)) == NULL)
372*0a6a1f1dSLionel Sambuc         return (1);
373ebfedea0SLionel Sambuc 
374ebfedea0SLionel Sambuc     SSL_CTX_set_quiet_shutdown(tm_ctx, 1);
375ebfedea0SLionel Sambuc 
376*0a6a1f1dSLionel Sambuc     if (st_bugs)
377*0a6a1f1dSLionel Sambuc         SSL_CTX_set_options(tm_ctx, SSL_OP_ALL);
378*0a6a1f1dSLionel Sambuc     if (!SSL_CTX_set_cipher_list(tm_ctx, tm_cipher)) {
379*0a6a1f1dSLionel Sambuc 	/* BIO_printf(bio_err, "Error in cipher list\n"); */
380*0a6a1f1dSLionel Sambuc 	goto end;
381*0a6a1f1dSLionel Sambuc     }
382*0a6a1f1dSLionel Sambuc 
383ebfedea0SLionel Sambuc     if (!set_cert_stuff(tm_ctx, t_cert_file, t_key_file))
384ebfedea0SLionel Sambuc         goto end;
385ebfedea0SLionel Sambuc 
386ebfedea0SLionel Sambuc     SSL_load_error_strings();
387ebfedea0SLionel Sambuc 
388ebfedea0SLionel Sambuc     if ((!SSL_CTX_load_verify_locations(tm_ctx, CAfile, CApath)) ||
389*0a6a1f1dSLionel Sambuc         (!SSL_CTX_set_default_verify_paths(tm_ctx))) {
390*0a6a1f1dSLionel Sambuc         /*
391*0a6a1f1dSLionel Sambuc          * BIO_printf(bio_err,"error setting default verify locations\n");
392*0a6a1f1dSLionel Sambuc          */
393ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
394ebfedea0SLionel Sambuc         /* goto end; */
395ebfedea0SLionel Sambuc     }
396ebfedea0SLionel Sambuc 
397ebfedea0SLionel Sambuc     if (tm_cipher == NULL)
398ebfedea0SLionel Sambuc         tm_cipher = getenv("SSL_CIPHER");
399ebfedea0SLionel Sambuc 
400ebfedea0SLionel Sambuc     if (tm_cipher == NULL) {
401ebfedea0SLionel Sambuc         fprintf(stderr, "No CIPHER specified\n");
402ebfedea0SLionel Sambuc     }
403ebfedea0SLionel Sambuc 
404*0a6a1f1dSLionel Sambuc     if (!(perform & 1))
405*0a6a1f1dSLionel Sambuc         goto next;
406ebfedea0SLionel Sambuc     printf("Collecting connection statistics for %d seconds\n", maxTime);
407ebfedea0SLionel Sambuc 
408ebfedea0SLionel Sambuc     /* Loop and time how long it takes to make connections */
409ebfedea0SLionel Sambuc 
410ebfedea0SLionel Sambuc     bytes_read = 0;
411ebfedea0SLionel Sambuc     finishtime = (long)time(NULL) + maxTime;
412ebfedea0SLionel Sambuc     tm_Time_F(START);
413*0a6a1f1dSLionel Sambuc     for (;;) {
414*0a6a1f1dSLionel Sambuc         if (finishtime < (long)time(NULL))
415*0a6a1f1dSLionel Sambuc             break;
416ebfedea0SLionel Sambuc #ifdef WIN32_STUFF
417ebfedea0SLionel Sambuc 
418ebfedea0SLionel Sambuc         if (flushWinMsgs(0) == -1)
419ebfedea0SLionel Sambuc             goto end;
420ebfedea0SLionel Sambuc 
421ebfedea0SLionel Sambuc         if (waitingToDie || exitNow) /* we're dead */
422ebfedea0SLionel Sambuc             goto end;
423ebfedea0SLionel Sambuc #endif
424ebfedea0SLionel Sambuc 
425ebfedea0SLionel Sambuc         if ((scon = doConnection(NULL)) == NULL)
426ebfedea0SLionel Sambuc             goto end;
427ebfedea0SLionel Sambuc 
428*0a6a1f1dSLionel Sambuc         if (s_www_path != NULL) {
429*0a6a1f1dSLionel Sambuc             BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
430*0a6a1f1dSLionel Sambuc                          s_www_path);
431ebfedea0SLionel Sambuc             SSL_write(scon, buf, strlen(buf));
432ebfedea0SLionel Sambuc             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
433ebfedea0SLionel Sambuc                 bytes_read += i;
434ebfedea0SLionel Sambuc         }
435ebfedea0SLionel Sambuc #ifdef NO_SHUTDOWN
436ebfedea0SLionel Sambuc         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
437ebfedea0SLionel Sambuc #else
438ebfedea0SLionel Sambuc         SSL_shutdown(scon);
439ebfedea0SLionel Sambuc #endif
440ebfedea0SLionel Sambuc         SHUTDOWN2(SSL_get_fd(scon));
441ebfedea0SLionel Sambuc 
442ebfedea0SLionel Sambuc         nConn += 1;
443ebfedea0SLionel Sambuc         if (SSL_session_reused(scon))
444ebfedea0SLionel Sambuc             ver = 'r';
445*0a6a1f1dSLionel Sambuc         else {
446ebfedea0SLionel Sambuc             ver = SSL_version(scon);
447ebfedea0SLionel Sambuc             if (ver == TLS1_VERSION)
448ebfedea0SLionel Sambuc                 ver = 't';
449ebfedea0SLionel Sambuc             else if (ver == SSL3_VERSION)
450ebfedea0SLionel Sambuc                 ver = '3';
451ebfedea0SLionel Sambuc             else if (ver == SSL2_VERSION)
452ebfedea0SLionel Sambuc                 ver = '2';
453ebfedea0SLionel Sambuc             else
454ebfedea0SLionel Sambuc                 ver = '*';
455ebfedea0SLionel Sambuc         }
456ebfedea0SLionel Sambuc         fputc(ver, stdout);
457ebfedea0SLionel Sambuc         fflush(stdout);
458ebfedea0SLionel Sambuc 
459ebfedea0SLionel Sambuc         SSL_free(scon);
460ebfedea0SLionel Sambuc         scon = NULL;
461ebfedea0SLionel Sambuc     }
462ebfedea0SLionel Sambuc     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
463ebfedea0SLionel Sambuc 
464ebfedea0SLionel Sambuc     i = (int)((long)time(NULL) - finishtime + maxTime);
465*0a6a1f1dSLionel Sambuc     printf
466*0a6a1f1dSLionel Sambuc         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
467*0a6a1f1dSLionel Sambuc          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
468*0a6a1f1dSLionel Sambuc     printf
469*0a6a1f1dSLionel Sambuc         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
470*0a6a1f1dSLionel Sambuc          nConn, (long)time(NULL) - finishtime + maxTime, bytes_read / nConn);
471ebfedea0SLionel Sambuc 
472*0a6a1f1dSLionel Sambuc     /*
473*0a6a1f1dSLionel Sambuc      * Now loop and time connections using the same session id over and over
474*0a6a1f1dSLionel Sambuc      */
475ebfedea0SLionel Sambuc 
476ebfedea0SLionel Sambuc  next:
477*0a6a1f1dSLionel Sambuc     if (!(perform & 2))
478*0a6a1f1dSLionel Sambuc         goto end;
479ebfedea0SLionel Sambuc     printf("\n\nNow timing with session id reuse.\n");
480ebfedea0SLionel Sambuc 
481ebfedea0SLionel Sambuc     /* Get an SSL object so we can reuse the session id */
482*0a6a1f1dSLionel Sambuc     if ((scon = doConnection(NULL)) == NULL) {
483ebfedea0SLionel Sambuc         fprintf(stderr, "Unable to get connection\n");
484ebfedea0SLionel Sambuc         goto end;
485ebfedea0SLionel Sambuc     }
486ebfedea0SLionel Sambuc 
487*0a6a1f1dSLionel Sambuc     if (s_www_path != NULL) {
488ebfedea0SLionel Sambuc         BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n", s_www_path);
489ebfedea0SLionel Sambuc         SSL_write(scon, buf, strlen(buf));
490*0a6a1f1dSLionel Sambuc         while (SSL_read(scon, buf, sizeof(buf)) > 0) ;
491ebfedea0SLionel Sambuc     }
492ebfedea0SLionel Sambuc #ifdef NO_SHUTDOWN
493ebfedea0SLionel Sambuc     SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
494ebfedea0SLionel Sambuc #else
495ebfedea0SLionel Sambuc     SSL_shutdown(scon);
496ebfedea0SLionel Sambuc #endif
497ebfedea0SLionel Sambuc     SHUTDOWN2(SSL_get_fd(scon));
498ebfedea0SLionel Sambuc 
499ebfedea0SLionel Sambuc     nConn = 0;
500ebfedea0SLionel Sambuc     totalTime = 0.0;
501ebfedea0SLionel Sambuc 
502ebfedea0SLionel Sambuc     finishtime = (long)time(NULL) + maxTime;
503ebfedea0SLionel Sambuc 
504ebfedea0SLionel Sambuc     printf("starting\n");
505ebfedea0SLionel Sambuc     bytes_read = 0;
506ebfedea0SLionel Sambuc     tm_Time_F(START);
507ebfedea0SLionel Sambuc 
508*0a6a1f1dSLionel Sambuc     for (;;) {
509*0a6a1f1dSLionel Sambuc         if (finishtime < (long)time(NULL))
510*0a6a1f1dSLionel Sambuc             break;
511ebfedea0SLionel Sambuc 
512ebfedea0SLionel Sambuc #ifdef WIN32_STUFF
513ebfedea0SLionel Sambuc         if (flushWinMsgs(0) == -1)
514ebfedea0SLionel Sambuc             goto end;
515ebfedea0SLionel Sambuc 
516ebfedea0SLionel Sambuc         if (waitingToDie || exitNow) /* we're dead */
517ebfedea0SLionel Sambuc             goto end;
518ebfedea0SLionel Sambuc #endif
519ebfedea0SLionel Sambuc 
520ebfedea0SLionel Sambuc         if ((doConnection(scon)) == NULL)
521ebfedea0SLionel Sambuc             goto end;
522ebfedea0SLionel Sambuc 
523*0a6a1f1dSLionel Sambuc         if (s_www_path) {
524*0a6a1f1dSLionel Sambuc             BIO_snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\n\r\n",
525*0a6a1f1dSLionel Sambuc                          s_www_path);
526ebfedea0SLionel Sambuc             SSL_write(scon, buf, strlen(buf));
527ebfedea0SLionel Sambuc             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
528ebfedea0SLionel Sambuc                 bytes_read += i;
529ebfedea0SLionel Sambuc         }
530ebfedea0SLionel Sambuc #ifdef NO_SHUTDOWN
531ebfedea0SLionel Sambuc         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
532ebfedea0SLionel Sambuc #else
533ebfedea0SLionel Sambuc         SSL_shutdown(scon);
534ebfedea0SLionel Sambuc #endif
535ebfedea0SLionel Sambuc         SHUTDOWN2(SSL_get_fd(scon));
536ebfedea0SLionel Sambuc 
537ebfedea0SLionel Sambuc         nConn += 1;
538ebfedea0SLionel Sambuc         if (SSL_session_reused(scon))
539ebfedea0SLionel Sambuc             ver = 'r';
540*0a6a1f1dSLionel Sambuc         else {
541ebfedea0SLionel Sambuc             ver = SSL_version(scon);
542ebfedea0SLionel Sambuc             if (ver == TLS1_VERSION)
543ebfedea0SLionel Sambuc                 ver = 't';
544ebfedea0SLionel Sambuc             else if (ver == SSL3_VERSION)
545ebfedea0SLionel Sambuc                 ver = '3';
546ebfedea0SLionel Sambuc             else if (ver == SSL2_VERSION)
547ebfedea0SLionel Sambuc                 ver = '2';
548ebfedea0SLionel Sambuc             else
549ebfedea0SLionel Sambuc                 ver = '*';
550ebfedea0SLionel Sambuc         }
551ebfedea0SLionel Sambuc         fputc(ver, stdout);
552ebfedea0SLionel Sambuc         fflush(stdout);
553ebfedea0SLionel Sambuc     }
554ebfedea0SLionel Sambuc     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
555ebfedea0SLionel Sambuc 
556*0a6a1f1dSLionel Sambuc     printf
557*0a6a1f1dSLionel Sambuc         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
558*0a6a1f1dSLionel Sambuc          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
559*0a6a1f1dSLionel Sambuc     printf
560*0a6a1f1dSLionel Sambuc         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
561*0a6a1f1dSLionel Sambuc          nConn, (long)time(NULL) - finishtime + maxTime,
562*0a6a1f1dSLionel Sambuc          bytes_read / (nConn?nConn:1));
563ebfedea0SLionel Sambuc 
564ebfedea0SLionel Sambuc     ret = 0;
565ebfedea0SLionel Sambuc  end:
566*0a6a1f1dSLionel Sambuc     if (scon != NULL)
567*0a6a1f1dSLionel Sambuc         SSL_free(scon);
568ebfedea0SLionel Sambuc 
569*0a6a1f1dSLionel Sambuc     if (tm_ctx != NULL) {
570ebfedea0SLionel Sambuc         SSL_CTX_free(tm_ctx);
571ebfedea0SLionel Sambuc         tm_ctx = NULL;
572ebfedea0SLionel Sambuc     }
573ebfedea0SLionel Sambuc     apps_shutdown();
574ebfedea0SLionel Sambuc     OPENSSL_EXIT(ret);
575ebfedea0SLionel Sambuc }
576ebfedea0SLionel Sambuc 
577*0a6a1f1dSLionel Sambuc /*-
578ebfedea0SLionel Sambuc  * doConnection - make a connection
579ebfedea0SLionel Sambuc  * Args:
580ebfedea0SLionel Sambuc  *              scon    = earlier ssl connection for session id, or NULL
581ebfedea0SLionel Sambuc  * Returns:
582ebfedea0SLionel Sambuc  *              SSL *   = the connection pointer.
583ebfedea0SLionel Sambuc  */
doConnection(SSL * scon)584ebfedea0SLionel Sambuc static SSL *doConnection(SSL *scon)
585ebfedea0SLionel Sambuc {
586ebfedea0SLionel Sambuc     BIO *conn;
587ebfedea0SLionel Sambuc     SSL *serverCon;
588ebfedea0SLionel Sambuc     int width, i;
589ebfedea0SLionel Sambuc     fd_set readfds;
590ebfedea0SLionel Sambuc 
591ebfedea0SLionel Sambuc     if ((conn = BIO_new(BIO_s_connect())) == NULL)
592ebfedea0SLionel Sambuc         return (NULL);
593ebfedea0SLionel Sambuc 
594ebfedea0SLionel Sambuc /*      BIO_set_conn_port(conn,port);*/
595ebfedea0SLionel Sambuc     BIO_set_conn_hostname(conn, host);
596ebfedea0SLionel Sambuc 
597ebfedea0SLionel Sambuc     if (scon == NULL)
598ebfedea0SLionel Sambuc         serverCon = SSL_new(tm_ctx);
599*0a6a1f1dSLionel Sambuc     else {
600ebfedea0SLionel Sambuc         serverCon = scon;
601ebfedea0SLionel Sambuc         SSL_set_connect_state(serverCon);
602ebfedea0SLionel Sambuc     }
603ebfedea0SLionel Sambuc 
604ebfedea0SLionel Sambuc     SSL_set_bio(serverCon, conn, conn);
605ebfedea0SLionel Sambuc 
606ebfedea0SLionel Sambuc #if 0
607ebfedea0SLionel Sambuc     if (scon != NULL)
608ebfedea0SLionel Sambuc         SSL_set_session(serverCon, SSL_get_session(scon));
609ebfedea0SLionel Sambuc #endif
610ebfedea0SLionel Sambuc 
611ebfedea0SLionel Sambuc     /* ok, lets connect */
612ebfedea0SLionel Sambuc     for (;;) {
613ebfedea0SLionel Sambuc         i = SSL_connect(serverCon);
614*0a6a1f1dSLionel Sambuc         if (BIO_sock_should_retry(i)) {
615ebfedea0SLionel Sambuc             BIO_printf(bio_err, "DELAY\n");
616ebfedea0SLionel Sambuc 
617ebfedea0SLionel Sambuc             i = SSL_get_fd(serverCon);
618ebfedea0SLionel Sambuc             width = i + 1;
619ebfedea0SLionel Sambuc             FD_ZERO(&readfds);
620ebfedea0SLionel Sambuc             openssl_fdset(i, &readfds);
621*0a6a1f1dSLionel Sambuc             /*
622*0a6a1f1dSLionel Sambuc              * Note: under VMS with SOCKETSHR the 2nd parameter is currently
623*0a6a1f1dSLionel Sambuc              * of type (int *) whereas under other systems it is (void *) if
624*0a6a1f1dSLionel Sambuc              * you don't have a cast it will choke the compiler: if you do
625*0a6a1f1dSLionel Sambuc              * have a cast then you can either go for (int *) or (void *).
626ebfedea0SLionel Sambuc              */
627ebfedea0SLionel Sambuc             select(width, (void *)&readfds, NULL, NULL, NULL);
628ebfedea0SLionel Sambuc             continue;
629ebfedea0SLionel Sambuc         }
630ebfedea0SLionel Sambuc         break;
631ebfedea0SLionel Sambuc     }
632*0a6a1f1dSLionel Sambuc     if (i <= 0) {
633ebfedea0SLionel Sambuc         BIO_printf(bio_err, "ERROR\n");
634ebfedea0SLionel Sambuc         if (verify_error != X509_V_OK)
635ebfedea0SLionel Sambuc             BIO_printf(bio_err, "verify error:%s\n",
636ebfedea0SLionel Sambuc                        X509_verify_cert_error_string(verify_error));
637ebfedea0SLionel Sambuc         else
638ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
639ebfedea0SLionel Sambuc         if (scon == NULL)
640ebfedea0SLionel Sambuc             SSL_free(serverCon);
641ebfedea0SLionel Sambuc         return NULL;
642ebfedea0SLionel Sambuc     }
643ebfedea0SLionel Sambuc 
644ebfedea0SLionel Sambuc     return serverCon;
645ebfedea0SLionel Sambuc }
646