1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117: #define _BSD_SOURCE 1
118:
119:
120: #include <assert.h>
121: #include <errno.h>
122: #include <limits.h>
123: #include <stdio.h>
124: #include <stdlib.h>
125: #include <string.h>
126: #include <time.h>
127:
128: #define USE_SOCKETS
129: #include "e_os.h"
130:
131: #define _XOPEN_SOURCE 500
132:
133: #include <ctype.h>
134:
135: #include <openssl/bio.h>
136: #include <openssl/crypto.h>
137: #include <openssl/evp.h>
138: #include <openssl/x509.h>
139: #include <openssl/x509v3.h>
140: #include <openssl/ssl.h>
141: #ifndef OPENSSL_NO_ENGINE
142: #include <openssl/engine.h>
143: #endif
144: #include <openssl/err.h>
145: #include <openssl/rand.h>
146: #ifndef OPENSSL_NO_RSA
147: #include <openssl/rsa.h>
148: #endif
149: #ifndef OPENSSL_NO_DSA
150: #include <openssl/dsa.h>
151: #endif
152: #ifndef OPENSSL_NO_DH
153: #include <openssl/dh.h>
154: #endif
155: #include <openssl/bn.h>
156:
157: #define _XOPEN_SOURCE_EXTENDED 1
158:
159:
160:
161:
162:
163: #ifdef OPENSSL_SYS_WINDOWS
164: #include <winsock.h>
165: #else
166: #include OPENSSL_UNISTD
167: #endif
168:
169: #ifdef OPENSSL_SYS_VMS
170: # define TEST_SERVER_CERT "SYS$DISK:[-.APPS]SERVER.PEM"
171: # define TEST_CLIENT_CERT "SYS$DISK:[-.APPS]CLIENT.PEM"
172: #elif defined(OPENSSL_SYS_WINCE)
173: # define TEST_SERVER_CERT "\\OpenSSL\\server.pem"
174: # define TEST_CLIENT_CERT "\\OpenSSL\\client.pem"
175: #elif defined(OPENSSL_SYS_NETWARE)
176: # define TEST_SERVER_CERT "\\openssl\\apps\\server.pem"
177: # define TEST_CLIENT_CERT "\\openssl\\apps\\client.pem"
178: #else
179: # define TEST_SERVER_CERT "../apps/server.pem"
180: # define TEST_CLIENT_CERT "../apps/client.pem"
181: #endif
182:
183:
184:
185: #define COMP_RLE 255
186: #define COMP_ZLIB 1
187:
188: static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
189: #ifndef OPENSSL_NO_RSA
190: static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);
191: static void free_tmp_rsa(void);
192: #endif
193: static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg);
194: #define APP_CALLBACK_STRING "Test Callback Argument"
195: struct app_verify_arg
196: {
197: char *string;
198: int app_verify;
199: int allow_proxy_certs;
200: char *proxy_auth;
201: char *proxy_cond;
202: };
203:
204: #ifndef OPENSSL_NO_DH
205: static DH *get_dh512(void);
206: static DH *get_dh1024(void);
207: static DH *get_dh1024dsa(void);
208: #endif
209:
210: static BIO *bio_err=NULL;
211: static BIO *bio_stdout=NULL;
212:
213: static char *cipher=NULL;
214: static int verbose=0;
215: static int debug=0;
216: #if 0
217:
218: #ifdef FIONBIO
219: static int s_nbio=0;
220: #endif
221: #endif
222:
223: static const char rnd_seed[] = "string to make the random number generator think it has entropy";
224:
225: int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes,clock_t *s_time,clock_t *c_time);
226: int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
227: static int do_test_cipherlist(void);
228: static void sv_usage(void)
229: {
230: fprintf(stderr,"usage: ssltest [args ...]\n");
231: fprintf(stderr,"\n");
232: fprintf(stderr," -server_auth - check server certificate\n");
233: fprintf(stderr," -client_auth - do client authentication\n");
234: fprintf(stderr," -proxy - allow proxy certificates\n");
235: fprintf(stderr," -proxy_auth <val> - set proxy policy rights\n");
236: fprintf(stderr," -proxy_cond <val> - experssion to test proxy policy rights\n");
237: fprintf(stderr," -v - more output\n");
238: fprintf(stderr," -d - debug output\n");
239: fprintf(stderr," -reuse - use session-id reuse\n");
240: fprintf(stderr," -num <val> - number of connections to perform\n");
241: fprintf(stderr," -bytes <val> - number of bytes to swap between client/server\n");
242: #ifndef OPENSSL_NO_DH
243: fprintf(stderr," -dhe1024 - use 1024 bit key (safe prime) for DHE\n");
244: fprintf(stderr," -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
245: fprintf(stderr," -no_dhe - disable DHE\n");
246: #endif
247: #ifndef OPENSSL_NO_ECDH
248: fprintf(stderr," -no_ecdhe - disable ECDHE\n");
249: #endif
250: #ifndef OPENSSL_NO_SSL2
251: fprintf(stderr," -ssl2 - use SSLv2\n");
252: #endif
253: #ifndef OPENSSL_NO_SSL3
254: fprintf(stderr," -ssl3 - use SSLv3\n");
255: #endif
256: #ifndef OPENSSL_NO_TLS1
257: fprintf(stderr," -tls1 - use TLSv1\n");
258: #endif
259: fprintf(stderr," -CApath arg - PEM format directory of CA's\n");
260: fprintf(stderr," -CAfile arg - PEM format file of CA's\n");
261: fprintf(stderr," -cert arg - Server certificate file\n");
262: fprintf(stderr," -key arg - Server key file (default: same as -cert)\n");
263: fprintf(stderr," -c_cert arg - Client certificate file\n");
264: fprintf(stderr," -c_key arg - Client key file (default: same as -c_cert)\n");
265: fprintf(stderr," -cipher arg - The cipher list\n");
266: fprintf(stderr," -bio_pair - Use BIO pairs\n");
267: fprintf(stderr," -f - Test even cases that can't work\n");
268: fprintf(stderr," -time - measure processor time used by client and server\n");
269: fprintf(stderr," -zlib - use zlib compression\n");
270: fprintf(stderr," -rle - use rle compression\n");
271: #ifndef OPENSSL_NO_ECDH
272: fprintf(stderr," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
273: " Use \"openssl ecparam -list_curves\" for all names\n" \
274: " (default is sect163r2).\n");
275: #endif
276: fprintf(stderr," -test_cipherlist - verifies the order of the ssl cipher lists\n");
277: }
278:
279: static void print_details(SSL *c_ssl, const char *prefix)
280: {
281: SSL_CIPHER *ciph;
282: X509 *cert;
283:
284: ciph=SSL_get_current_cipher(c_ssl);
285: BIO_printf(bio_stdout,"%s%s, cipher %s %s",
286: prefix,
287: SSL_get_version(c_ssl),
288: SSL_CIPHER_get_version(ciph),
289: SSL_CIPHER_get_name(ciph));
290: cert=SSL_get_peer_certificate(c_ssl);
291: if (cert != NULL)
292: {
293: EVP_PKEY *pkey = X509_get_pubkey(cert);
294: if (pkey != NULL)
295: {
296: if (0)
297: ;
298: #ifndef OPENSSL_NO_RSA
299: else if (pkey->type == EVP_PKEY_RSA && pkey->pkey.rsa != NULL
300: && pkey->pkey.rsa->n != NULL)
301: {
302: BIO_printf(bio_stdout, ", %d bit RSA",
303: BN_num_bits(pkey->pkey.rsa->n));
304: }
305: #endif
306: #ifndef OPENSSL_NO_DSA
307: else if (pkey->type == EVP_PKEY_DSA && pkey->pkey.dsa != NULL
308: && pkey->pkey.dsa->p != NULL)
309: {
310: BIO_printf(bio_stdout, ", %d bit DSA",
311: BN_num_bits(pkey->pkey.dsa->p));
312: }
313: #endif
314: EVP_PKEY_free(pkey);
315: }
316: X509_free(cert);
317: }
318:
319:
320: BIO_printf(bio_stdout,"\n");
321: }
322:
323: static void lock_dbg_cb(int mode, int type, const char *file, int line)
324: {
325: static int modes[CRYPTO_NUM_LOCKS];
326: const char *errstr = NULL;
327: int rw;
328:
329: rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
330: if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
331: {
332: errstr = "invalid mode";
333: goto err;
334: }
335:
336: if (type < 0 || type >= CRYPTO_NUM_LOCKS)
337: {
338: errstr = "type out of bounds";
339: goto err;
340: }
341:
342: if (mode & CRYPTO_LOCK)
343: {
344: if (modes[type])
345: {
346: errstr = "already locked";
347:
348:
349: goto err;
350: }
351:
352: modes[type] = rw;
353: }
354: else if (mode & CRYPTO_UNLOCK)
355: {
356: if (!modes[type])
357: {
358: errstr = "not locked";
359: goto err;
360: }
361:
362: if (modes[type] != rw)
363: {
364: errstr = (rw == CRYPTO_READ) ?
365: "CRYPTO_r_unlock on write lock" :
366: "CRYPTO_w_unlock on read lock";
367: }
368:
369: modes[type] = 0;
370: }
371: else
372: {
373: errstr = "invalid mode";
374: goto err;
375: }
376:
377: err:
378: if (errstr)
379: {
380:
381: fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
382: errstr, mode, type, file, line);
383: }
384: }
385:
386:
387: int main(int argc, char *argv[])
388: {
389: char *CApath=NULL,*CAfile=NULL;
390: int badop=0;
391: int bio_pair=0;
392: int force=0;
393: int tls1=0,ssl2=0,ssl3=0,ret=1;
394: int client_auth=0;
395: int server_auth=0,i;
396: struct app_verify_arg app_verify_arg =
397: { APP_CALLBACK_STRING, 0, 0, NULL, NULL };
398: char *server_cert=TEST_SERVER_CERT;
399: char *server_key=NULL;
400: char *client_cert=TEST_CLIENT_CERT;
401: char *client_key=NULL;
402: #ifndef OPENSSL_NO_ECDH
403: char *named_curve = NULL;
404: #endif
405: SSL_CTX *s_ctx=NULL;
406: SSL_CTX *c_ctx=NULL;
407: SSL_METHOD *meth=NULL;
408: SSL *c_ssl,*s_ssl;
409: int number=1,reuse=0;
410: long bytes=256L;
411: #ifndef OPENSSL_NO_DH
412: DH *dh;
413: int dhe1024 = 0, dhe1024dsa = 0;
414: #endif
415: #ifndef OPENSSL_NO_ECDH
416: EC_KEY *ecdh = NULL;
417: #endif
418: int no_dhe = 0;
419: int no_ecdhe = 0;
420: int print_time = 0;
421: clock_t s_time = 0, c_time = 0;
422: int comp = 0;
423: #ifndef OPENSSL_NO_COMP
424: COMP_METHOD *cm = NULL;
425: #endif
426: STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
427: int test_cipherlist = 0;
428:
429: verbose = 0;
430: debug = 0;
431: cipher = 0;
432:
433: bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
434:
435: CRYPTO_set_locking_callback(lock_dbg_cb);
436:
437:
438: if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
439: {
440: CRYPTO_malloc_debug_init();
441: CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
442: }
443: else
444: {
445:
446: CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
447: }
448: CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
449:
450: RAND_seed(rnd_seed, sizeof rnd_seed);
451:
452: bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
453:
454: argc--;
455: argv++;
456:
457: while (argc >= 1)
458: {
459: if (strcmp(*argv,"-server_auth") == 0)
460: server_auth=1;
461: else if (strcmp(*argv,"-client_auth") == 0)
462: client_auth=1;
463: else if (strcmp(*argv,"-proxy_auth") == 0)
464: {
465: if (--argc < 1) goto bad;
466: app_verify_arg.proxy_auth= *(++argv);
467: }
468: else if (strcmp(*argv,"-proxy_cond") == 0)
469: {
470: if (--argc < 1) goto bad;
471: app_verify_arg.proxy_cond= *(++argv);
472: }
473: else if (strcmp(*argv,"-v") == 0)
474: verbose=1;
475: else if (strcmp(*argv,"-d") == 0)
476: debug=1;
477: else if (strcmp(*argv,"-reuse") == 0)
478: reuse=1;
479: else if (strcmp(*argv,"-dhe1024") == 0)
480: {
481: #ifndef OPENSSL_NO_DH
482: dhe1024=1;
483: #else
484: fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
485: #endif
486: }
487: else if (strcmp(*argv,"-dhe1024dsa") == 0)
488: {
489: #ifndef OPENSSL_NO_DH
490: dhe1024dsa=1;
491: #else
492: fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
493: #endif
494: }
495: else if (strcmp(*argv,"-no_dhe") == 0)
496: no_dhe=1;
497: else if (strcmp(*argv,"-no_ecdhe") == 0)
498: no_ecdhe=1;
499: else if (strcmp(*argv,"-ssl2") == 0)
500: ssl2=1;
501: else if (strcmp(*argv,"-tls1") == 0)
502: tls1=1;
503: else if (strcmp(*argv,"-ssl3") == 0)
504: ssl3=1;
505: else if (strncmp(*argv,"-num",4) == 0)
506: {
507: if (--argc < 1) goto bad;
508: number= atoi(*(++argv));
509: if (number == 0) number=1;
510: }
511: else if (strcmp(*argv,"-bytes") == 0)
512: {
513: if (--argc < 1) goto bad;
514: bytes= atol(*(++argv));
515: if (bytes == 0L) bytes=1L;
516: i=strlen(argv[0]);
517: if (argv[0][i-1] == 'k') bytes*=1024L;
518: if (argv[0][i-1] == 'm') bytes*=1024L*1024L;
519: }
520: else if (strcmp(*argv,"-cert") == 0)
521: {
522: if (--argc < 1) goto bad;
523: server_cert= *(++argv);
524: }
525: else if (strcmp(*argv,"-s_cert") == 0)
526: {
527: if (--argc < 1) goto bad;
528: server_cert= *(++argv);