ssltest.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. /* ssl/ssltest.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. * ECC cipher suite support in OpenSSL originally developed by
  114. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  115. */
  116. #define _BSD_SOURCE 1 /* Or gethostname won't be declared properly
  117. on Linux and GNU platforms. */
  118. #include <assert.h>
  119. #include <errno.h>
  120. #include <limits.h>
  121. #include <stdio.h>
  122. #include <stdlib.h>
  123. #include <string.h>
  124. #include <time.h>
  125. #define USE_SOCKETS
  126. #include "e_os.h"
  127. #include <openssl/bio.h>
  128. #include <openssl/crypto.h>
  129. #include <openssl/evp.h>
  130. #include <openssl/x509.h>
  131. #include <openssl/ssl.h>
  132. #ifndef OPENSSL_NO_ENGINE
  133. #include <openssl/engine.h>
  134. #endif
  135. #include <openssl/err.h>
  136. #include <openssl/rand.h>
  137. #include <openssl/rsa.h>
  138. #include <openssl/dsa.h>
  139. #include <openssl/dh.h>
  140. #define _XOPEN_SOURCE_EXTENDED 1 /* Or gethostname won't be declared properly
  141. on Compaq platforms (at least with DEC C).
  142. Do not try to put it earlier, or IPv6 includes
  143. get screwed...
  144. */
  145. #ifdef OPENSSL_SYS_WINDOWS
  146. #include <winsock.h>
  147. #else
  148. #include OPENSSL_UNISTD
  149. #endif
  150. #ifdef OPENSSL_SYS_VMS
  151. # define TEST_SERVER_CERT "SYS$DISK:[-.APPS]SERVER.PEM"
  152. # define TEST_CLIENT_CERT "SYS$DISK:[-.APPS]CLIENT.PEM"
  153. #elif defined(OPENSSL_SYS_WINCE)
  154. # define TEST_SERVER_CERT "\\OpenSSL\\server.pem"
  155. # define TEST_CLIENT_CERT "\\OpenSSL\\client.pem"
  156. #elif defined(OPENSSL_SYS_NETWARE)
  157. # define TEST_SERVER_CERT "\\openssl\\apps\\server.pem"
  158. # define TEST_CLIENT_CERT "\\openssl\\apps\\client.pem"
  159. #else
  160. # define TEST_SERVER_CERT "../apps/server.pem"
  161. # define TEST_CLIENT_CERT "../apps/client.pem"
  162. #endif
  163. /* There is really no standard for this, so let's assign some tentative
  164. numbers. In any case, these numbers are only for this test */
  165. #define COMP_RLE 255
  166. #define COMP_ZLIB 1
  167. static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
  168. #ifndef OPENSSL_NO_RSA
  169. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);
  170. static void free_tmp_rsa(void);
  171. #endif
  172. static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg);
  173. #define APP_CALLBACK "Test Callback Argument"
  174. static char *app_verify_arg = APP_CALLBACK;
  175. #ifndef OPENSSL_NO_DH
  176. static DH *get_dh512(void);
  177. static DH *get_dh1024(void);
  178. static DH *get_dh1024dsa(void);
  179. #endif
  180. static BIO *bio_err=NULL;
  181. static BIO *bio_stdout=NULL;
  182. static char *cipher=NULL;
  183. static int verbose=0;
  184. static int debug=0;
  185. #if 0
  186. /* Not used yet. */
  187. #ifdef FIONBIO
  188. static int s_nbio=0;
  189. #endif
  190. #endif
  191. static const char rnd_seed[] = "string to make the random number generator think it has entropy";
  192. int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes,clock_t *s_time,clock_t *c_time);
  193. int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
  194. static void sv_usage(void)
  195. {
  196. fprintf(stderr,"usage: ssltest [args ...]\n");
  197. fprintf(stderr,"\n");
  198. fprintf(stderr," -server_auth - check server certificate\n");
  199. fprintf(stderr," -client_auth - do client authentication\n");
  200. fprintf(stderr," -v - more output\n");
  201. fprintf(stderr," -d - debug output\n");
  202. fprintf(stderr," -reuse - use session-id reuse\n");
  203. fprintf(stderr," -num <val> - number of connections to perform\n");
  204. fprintf(stderr," -bytes <val> - number of bytes to swap between client/server\n");
  205. #ifndef OPENSSL_NO_DH
  206. fprintf(stderr," -dhe1024 - use 1024 bit key (safe prime) for DHE\n");
  207. fprintf(stderr," -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
  208. fprintf(stderr," -no_dhe - disable DHE\n");
  209. #endif
  210. #ifndef OPENSSL_NO_ECDH
  211. fprintf(stderr," -no_ecdhe - disable ECDHE\n");
  212. #endif
  213. #ifndef OPENSSL_NO_SSL2
  214. fprintf(stderr," -ssl2 - use SSLv2\n");
  215. #endif
  216. #ifndef OPENSSL_NO_SSL3
  217. fprintf(stderr," -ssl3 - use SSLv3\n");
  218. #endif
  219. #ifndef OPENSSL_NO_TLS1
  220. fprintf(stderr," -tls1 - use TLSv1\n");
  221. #endif
  222. fprintf(stderr," -CApath arg - PEM format directory of CA's\n");
  223. fprintf(stderr," -CAfile arg - PEM format file of CA's\n");
  224. fprintf(stderr," -cert arg - Server certificate file\n");
  225. fprintf(stderr," -key arg - Server key file (default: same as -cert)\n");
  226. fprintf(stderr," -c_cert arg - Client certificate file\n");
  227. fprintf(stderr," -c_key arg - Client key file (default: same as -c_cert)\n");
  228. fprintf(stderr," -cipher arg - The cipher list\n");
  229. fprintf(stderr," -bio_pair - Use BIO pairs\n");
  230. fprintf(stderr," -f - Test even cases that can't work\n");
  231. fprintf(stderr," -time - measure processor time used by client and server\n");
  232. fprintf(stderr," -zlib - use zlib compression\n");
  233. fprintf(stderr," -rle - use rle compression\n");
  234. #ifndef OPENSSL_NO_ECDH
  235. fprintf(stderr," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
  236. " Use \"openssl ecparam -list_curves\" for all names\n" \
  237. " (default is sect163r2).\n");
  238. #endif
  239. }
  240. static void print_details(SSL *c_ssl, const char *prefix)
  241. {
  242. SSL_CIPHER *ciph;
  243. X509 *cert;
  244. ciph=SSL_get_current_cipher(c_ssl);
  245. BIO_printf(bio_stdout,"%s%s, cipher %s %s",
  246. prefix,
  247. SSL_get_version(c_ssl),
  248. SSL_CIPHER_get_version(ciph),
  249. SSL_CIPHER_get_name(ciph));
  250. cert=SSL_get_peer_certificate(c_ssl);
  251. if (cert != NULL)
  252. {
  253. EVP_PKEY *pkey = X509_get_pubkey(cert);
  254. if (pkey != NULL)
  255. {
  256. if (0)
  257. ;
  258. #ifndef OPENSSL_NO_RSA
  259. else if (pkey->type == EVP_PKEY_RSA && pkey->pkey.rsa != NULL
  260. && pkey->pkey.rsa->n != NULL)
  261. {
  262. BIO_printf(bio_stdout, ", %d bit RSA",
  263. BN_num_bits(pkey->pkey.rsa->n));
  264. }
  265. #endif
  266. #ifndef OPENSSL_NO_DSA
  267. else if (pkey->type == EVP_PKEY_DSA && pkey->pkey.dsa != NULL
  268. && pkey->pkey.dsa->p != NULL)
  269. {
  270. BIO_printf(bio_stdout, ", %d bit DSA",
  271. BN_num_bits(pkey->pkey.dsa->p));
  272. }
  273. #endif
  274. EVP_PKEY_free(pkey);
  275. }
  276. X509_free(cert);
  277. }
  278. /* The SSL API does not allow us to look at temporary RSA/DH keys,
  279. * otherwise we should print their lengths too */
  280. BIO_printf(bio_stdout,"\n");
  281. }
  282. static void lock_dbg_cb(int mode, int type, const char *file, int line)
  283. {
  284. static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
  285. const char *errstr = NULL;
  286. int rw;
  287. rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
  288. if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
  289. {
  290. errstr = "invalid mode";
  291. goto err;
  292. }
  293. if (type < 0 || type >= CRYPTO_NUM_LOCKS)
  294. {
  295. errstr = "type out of bounds";
  296. goto err;
  297. }
  298. if (mode & CRYPTO_LOCK)
  299. {
  300. if (modes[type])
  301. {
  302. errstr = "already locked";
  303. /* must not happen in a single-threaded program
  304. * (would deadlock) */
  305. goto err;
  306. }
  307. modes[type] = rw;
  308. }
  309. else if (mode & CRYPTO_UNLOCK)
  310. {
  311. if (!modes[type])
  312. {
  313. errstr = "not locked";
  314. goto err;
  315. }
  316. if (modes[type] != rw)
  317. {
  318. errstr = (rw == CRYPTO_READ) ?
  319. "CRYPTO_r_unlock on write lock" :
  320. "CRYPTO_w_unlock on read lock";
  321. }
  322. modes[type] = 0;
  323. }
  324. else
  325. {
  326. errstr = "invalid mode";
  327. goto err;
  328. }
  329. err:
  330. if (errstr)
  331. {
  332. /* we cannot use bio_err here */
  333. fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
  334. errstr, mode, type, file, line);
  335. }
  336. }
  337. int main(int argc, char *argv[])
  338. {
  339. char *CApath=NULL,*CAfile=NULL;
  340. int badop=0;
  341. int bio_pair=0;
  342. int force=0;
  343. int tls1=0,ssl2=0,ssl3=0,ret=1;
  344. int client_auth=0;
  345. int server_auth=0,i;
  346. int app_verify=0;
  347. char *server_cert=TEST_SERVER_CERT;
  348. char *server_key=NULL;
  349. char *client_cert=TEST_CLIENT_CERT;
  350. char *client_key=NULL;
  351. char *named_curve = NULL;
  352. SSL_CTX *s_ctx=NULL;
  353. SSL_CTX *c_ctx=NULL;
  354. SSL_METHOD *meth=NULL;
  355. SSL *c_ssl,*s_ssl;
  356. int number=1,reuse=0;
  357. long bytes=256L;
  358. #ifndef OPENSSL_NO_DH
  359. DH *dh;
  360. int dhe1024 = 0, dhe1024dsa = 0;
  361. #endif
  362. #ifndef OPENSSL_NO_ECDH
  363. EC_KEY *ecdh = NULL;
  364. #endif
  365. int no_dhe = 0;
  366. int no_ecdhe = 0;
  367. int print_time = 0;
  368. clock_t s_time = 0, c_time = 0;
  369. int comp = 0;
  370. COMP_METHOD *cm = NULL;
  371. STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
  372. verbose = 0;
  373. debug = 0;
  374. cipher = 0;
  375. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  376. CRYPTO_set_locking_callback(lock_dbg_cb);
  377. /* enable memory leak checking unless explicitly disabled */
  378. if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
  379. {
  380. CRYPTO_malloc_debug_init();
  381. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  382. }
  383. else
  384. {
  385. /* OPENSSL_DEBUG_MEMORY=off */
  386. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  387. }
  388. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  389. RAND_seed(rnd_seed, sizeof rnd_seed);
  390. bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
  391. argc--;
  392. argv++;
  393. while (argc >= 1)
  394. {
  395. if (strcmp(*argv,"-server_auth") == 0)
  396. server_auth=1;
  397. else if (strcmp(*argv,"-client_auth") == 0)
  398. client_auth=1;
  399. else if (strcmp(*argv,"-v") == 0)
  400. verbose=1;
  401. else if (strcmp(*argv,"-d") == 0)
  402. debug=1;
  403. else if (strcmp(*argv,"-reuse") == 0)
  404. reuse=1;
  405. else if (strcmp(*argv,"-dhe1024") == 0)
  406. {
  407. #ifndef OPENSSL_NO_DH
  408. dhe1024=1;
  409. #else
  410. fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
  411. #endif
  412. }
  413. else if (strcmp(*argv,"-dhe1024dsa") == 0)
  414. {
  415. #ifndef OPENSSL_NO_DH
  416. dhe1024dsa=1;
  417. #else
  418. fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
  419. #endif
  420. }
  421. else if (strcmp(*argv,"-no_dhe") == 0)
  422. no_dhe=1;
  423. else if (strcmp(*argv,"-no_ecdhe") == 0)
  424. no_ecdhe=1;
  425. else if (strcmp(*argv,"-ssl2") == 0)
  426. ssl2=1;
  427. else if (strcmp(*argv,"-tls1") == 0)
  428. tls1=1;
  429. else if (strcmp(*argv,"-ssl3") == 0)
  430. ssl3=1;
  431. else if (strncmp(*argv,"-num",4) == 0)
  432. {
  433. if (--argc < 1) goto bad;
  434. number= atoi(*(++argv));
  435. if (number == 0) number=1;
  436. }
  437. else if (strcmp(*argv,"-bytes") == 0)
  438. {
  439. if (--argc < 1) goto bad;
  440. bytes= atol(*(++argv));
  441. if (bytes == 0L) bytes=1L;
  442. i=strlen(argv[0]);
  443. if (argv[0][i-1] == 'k') bytes*=1024L;
  444. if (argv[0][i-1] == 'm') bytes*=1024L*1024L;
  445. }
  446. else if (strcmp(*argv,"-cert") == 0)
  447. {
  448. if (--argc < 1) goto bad;
  449. server_cert= *(++argv);
  450. }
  451. else if (strcmp(*argv,"-s_cert") == 0)
  452. {
  453. if (--argc < 1) goto bad;
  454. server_cert= *(++argv);
  455. }
  456. else if (strcmp(*argv,"-key") == 0)
  457. {
  458. if (--argc < 1) goto bad;
  459. server_key= *(++argv);
  460. }
  461. else if (strcmp(*argv,"-s_key") == 0)
  462. {
  463. if (--argc < 1) goto bad;
  464. server_key= *(++argv);
  465. }
  466. else if (strcmp(*argv,"-c_cert") == 0)
  467. {
  468. if (--argc < 1) goto bad;
  469. client_cert= *(++argv);
  470. }
  471. else if (strcmp(*argv,"-c_key") == 0)
  472. {
  473. if (--argc < 1) goto bad;
  474. client_key= *(++argv);
  475. }
  476. else if (strcmp(*argv,"-cipher") == 0)
  477. {
  478. if (--argc < 1) goto bad;
  479. cipher= *(++argv);
  480. }
  481. else if (strcmp(*argv,"-CApath") == 0)
  482. {
  483. if (--argc < 1) goto bad;
  484. CApath= *(++argv);
  485. }
  486. else if (strcmp(*argv,"-CAfile") == 0)
  487. {
  488. if (--argc < 1) goto bad;
  489. CAfile= *(++argv);
  490. }
  491. else if (strcmp(*argv,"-bio_pair") == 0)
  492. {
  493. bio_pair = 1;
  494. }
  495. else if (strcmp(*argv,"-f") == 0)
  496. {
  497. force = 1;
  498. }
  499. else if (strcmp(*argv,"-time") == 0)
  500. {
  501. print_time = 1;
  502. }
  503. else if (strcmp(*argv,"-zlib") == 0)
  504. {
  505. comp = COMP_ZLIB;
  506. }
  507. else if (strcmp(*argv,"-rle") == 0)
  508. {
  509. comp = COMP_RLE;
  510. }
  511. else if (strcmp(*argv,"-named_curve") == 0)
  512. {
  513. if (--argc < 1) goto bad;
  514. #ifndef OPENSSL_NO_ECDH
  515. named_curve = *(++argv);
  516. #else
  517. fprintf(stderr,"ignoring -named_curve, since I'm compiled without ECDH\n");
  518. ++argv;
  519. #endif
  520. }
  521. else if (strcmp(*argv,"-app_verify") == 0)
  522. {
  523. app_verify = 1;
  524. }
  525. else
  526. {
  527. fprintf(stderr,"unknown option %s\n",*argv);
  528. badop=1;
  529. break;
  530. }
  531. argc--;
  532. argv++;
  533. }
  534. if (badop)
  535. {
  536. bad:
  537. sv_usage();
  538. goto end;
  539. }
  540. if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force)
  541. {
  542. fprintf(stderr, "This case cannot work. Use -f to perform "
  543. "the test anyway (and\n-d to see what happens), "
  544. "or add one of -ssl2, -ssl3, -tls1, -reuse\n"
  545. "to avoid protocol mismatch.\n");
  546. EXIT(1);
  547. }
  548. if (print_time)
  549. {
  550. if (!bio_pair)
  551. {
  552. fprintf(stderr, "Using BIO pair (-bio_pair)\n");
  553. bio_pair = 1;
  554. }
  555. if (number < 50 && !force)
  556. fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
  557. }
  558. /* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
  559. SSL_library_init();
  560. SSL_load_error_strings();
  561. if (comp == COMP_ZLIB) cm = COMP_zlib();
  562. if (comp == COMP_RLE) cm = COMP_rle();
  563. if (cm != NULL)
  564. {
  565. if (cm->type != NID_undef)
  566. {
  567. if (SSL_COMP_add_compression_method(comp, cm) != 0)
  568. {
  569. fprintf(stderr,
  570. "Failed to add compression method\n");
  571. ERR_print_errors_fp(stderr);
  572. }
  573. }
  574. else
  575. {
  576. fprintf(stderr,
  577. "Warning: %s compression not supported\n",
  578. (comp == COMP_RLE ? "rle" :
  579. (comp == COMP_ZLIB ? "zlib" :
  580. "unknown")));
  581. ERR_print_errors_fp(stderr);
  582. }
  583. }
  584. ssl_comp_methods = SSL_COMP_get_compression_methods();
  585. fprintf(stderr, "Available compression methods:\n");
  586. {
  587. int j, n = sk_SSL_COMP_num(ssl_comp_methods);
  588. if (n == 0)
  589. fprintf(stderr, " NONE\n");
  590. else
  591. for (j = 0; j < n; j++)
  592. {
  593. SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);
  594. fprintf(stderr, " %d: %s\n", c->id, c->name);
  595. }
  596. }
  597. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  598. if (ssl2)
  599. meth=SSLv2_method();
  600. else
  601. if (tls1)
  602. meth=TLSv1_method();
  603. else
  604. if (ssl3)
  605. meth=SSLv3_method();
  606. else
  607. meth=SSLv23_method();
  608. #else
  609. #ifdef OPENSSL_NO_SSL2
  610. meth=SSLv3_method();
  611. #else
  612. meth=SSLv2_method();
  613. #endif
  614. #endif
  615. c_ctx=SSL_CTX_new(meth);
  616. s_ctx=SSL_CTX_new(meth);
  617. if ((c_ctx == NULL) || (s_ctx == NULL))
  618. {
  619. ERR_print_errors(bio_err);
  620. goto end;
  621. }
  622. if (cipher != NULL)
  623. {
  624. SSL_CTX_set_cipher_list(c_ctx,cipher);
  625. SSL_CTX_set_cipher_list(s_ctx,cipher);
  626. }
  627. #ifndef OPENSSL_NO_DH
  628. if (!no_dhe)
  629. {
  630. if (dhe1024dsa)
  631. {
  632. /* use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */
  633. SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);
  634. dh=get_dh1024dsa();
  635. }
  636. else if (dhe1024)
  637. dh=get_dh1024();
  638. else
  639. dh=get_dh512();
  640. SSL_CTX_set_tmp_dh(s_ctx,dh);
  641. DH_free(dh);
  642. }
  643. #else
  644. (void)no_dhe;
  645. #endif
  646. #ifndef OPENSSL_NO_ECDH
  647. if (!no_ecdhe)
  648. {
  649. ecdh = EC_KEY_new();
  650. if (ecdh != NULL)
  651. {
  652. if (named_curve)
  653. {
  654. int nid = OBJ_sn2nid(named_curve);
  655. if (nid == 0)
  656. {
  657. BIO_printf(bio_err, "unknown curve name (%s)\n", named_curve);
  658. EC_KEY_free(ecdh);
  659. goto end;
  660. }
  661. ecdh->group = EC_GROUP_new_by_nid(nid);
  662. if (ecdh->group == NULL)
  663. {
  664. BIO_printf(bio_err, "unable to create curve (%s)\n", named_curve);
  665. EC_KEY_free(ecdh);
  666. goto end;
  667. }
  668. }
  669. if (ecdh->group == NULL)
  670. ecdh->group=EC_GROUP_new_by_nid(NID_sect163r2);
  671. SSL_CTX_set_tmp_ecdh(s_ctx, ecdh);
  672. SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_ECDH_USE);
  673. EC_KEY_free(ecdh);
  674. }
  675. }
  676. #else
  677. (void)no_ecdhe;
  678. #endif
  679. #ifndef OPENSSL_NO_RSA
  680. SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);
  681. #endif
  682. if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))
  683. {
  684. ERR_print_errors(bio_err);
  685. }
  686. else if (!SSL_CTX_use_PrivateKey_file(s_ctx,
  687. (server_key?server_key:server_cert), SSL_FILETYPE_PEM))
  688. {
  689. ERR_print_errors(bio_err);
  690. goto end;
  691. }
  692. if (client_auth)
  693. {
  694. SSL_CTX_use_certificate_file(c_ctx,client_cert,
  695. SSL_FILETYPE_PEM);
  696. SSL_CTX_use_PrivateKey_file(c_ctx,
  697. (client_key?client_key:client_cert),
  698. SSL_FILETYPE_PEM);
  699. }
  700. if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
  701. (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
  702. (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
  703. (!SSL_CTX_set_default_verify_paths(c_ctx)))
  704. {
  705. /* fprintf(stderr,"SSL_load_verify_locations\n"); */
  706. ERR_print_errors(bio_err);
  707. /* goto end; */
  708. }
  709. if (client_auth)
  710. {
  711. BIO_printf(bio_err,"client authentication\n");
  712. SSL_CTX_set_verify(s_ctx,
  713. SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  714. verify_callback);
  715. if (app_verify)
  716. {
  717. SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, app_verify_arg);
  718. }
  719. }
  720. if (server_auth)
  721. {
  722. BIO_printf(bio_err,"server authentication\n");
  723. SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
  724. verify_callback);
  725. if (app_verify)
  726. {
  727. SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, app_verify_arg);
  728. }
  729. }
  730. {
  731. int session_id_context = 0;
  732. SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context);
  733. }
  734. c_ssl=SSL_new(c_ctx);
  735. s_ssl=SSL_new(s_ctx);
  736. #ifndef OPENSSL_NO_KRB5
  737. if (c_ssl && c_ssl->kssl_ctx)
  738. {
  739. char localhost[MAXHOSTNAMELEN+2];
  740. if (gethostname(localhost, sizeof localhost-1) == 0)
  741. {
  742. localhost[sizeof localhost-1]='\0';
  743. if(strlen(localhost) == sizeof localhost-1)
  744. {
  745. BIO_printf(bio_err,"localhost name too long\n");
  746. goto end;
  747. }
  748. kssl_ctx_setstring(c_ssl->kssl_ctx, KSSL_SERVER,
  749. localhost);
  750. }
  751. }
  752. #endif /* OPENSSL_NO_KRB5 */
  753. for (i=0; i<number; i++)
  754. {
  755. if (!reuse) SSL_set_session(c_ssl,NULL);
  756. if (bio_pair)
  757. ret=doit_biopair(s_ssl,c_ssl,bytes,&s_time,&c_time);
  758. else
  759. ret=doit(s_ssl,c_ssl,bytes);
  760. }
  761. if (!verbose)
  762. {
  763. print_details(c_ssl, "");
  764. }
  765. if ((number > 1) || (bytes > 1L))
  766. BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n",number,bytes);
  767. if (print_time)
  768. {
  769. #ifdef CLOCKS_PER_SEC
  770. /* "To determine the time in seconds, the value returned
  771. * by the clock function should be divided by the value
  772. * of the macro CLOCKS_PER_SEC."
  773. * -- ISO/IEC 9899 */
  774. BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
  775. "Approximate total client time: %6.2f s\n",
  776. (double)s_time/CLOCKS_PER_SEC,
  777. (double)c_time/CLOCKS_PER_SEC);
  778. #else
  779. /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
  780. * -- cc on NeXTstep/OpenStep */
  781. BIO_printf(bio_stdout,
  782. "Approximate total server time: %6.2f units\n"
  783. "Approximate total client time: %6.2f units\n",
  784. (double)s_time,
  785. (double)c_time);
  786. #endif
  787. }
  788. SSL_free(s_ssl);
  789. SSL_free(c_ssl);
  790. end:
  791. if (s_ctx != NULL) SSL_CTX_free(s_ctx);
  792. if (c_ctx != NULL) SSL_CTX_free(c_ctx);
  793. if (bio_stdout != NULL) BIO_free(bio_stdout);
  794. #ifndef OPENSSL_NO_RSA
  795. free_tmp_rsa();
  796. #endif
  797. #ifndef OPENSSL_NO_ENGINE
  798. ENGINE_cleanup();
  799. #endif
  800. CRYPTO_cleanup_all_ex_data();
  801. ERR_free_strings();
  802. ERR_remove_state(0);
  803. EVP_cleanup();
  804. CRYPTO_mem_leaks(bio_err);
  805. if (bio_err != NULL) BIO_free(bio_err);
  806. EXIT(ret);
  807. }
  808. int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
  809. clock_t *s_time, clock_t *c_time)
  810. {
  811. long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
  812. BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
  813. BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
  814. int ret = 1;
  815. size_t bufsiz = 256; /* small buffer for testing */
  816. if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
  817. goto err;
  818. if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
  819. goto err;
  820. s_ssl_bio = BIO_new(BIO_f_ssl());
  821. if (!s_ssl_bio)
  822. goto err;
  823. c_ssl_bio = BIO_new(BIO_f_ssl());
  824. if (!c_ssl_bio)
  825. goto err;
  826. SSL_set_connect_state(c_ssl);
  827. SSL_set_bio(c_ssl, client, client);
  828. (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
  829. SSL_set_accept_state(s_ssl);
  830. SSL_set_bio(s_ssl, server, server);
  831. (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
  832. do
  833. {
  834. /* c_ssl_bio: SSL filter BIO
  835. *
  836. * client: pseudo-I/O for SSL library
  837. *
  838. * client_io: client's SSL communication; usually to be
  839. * relayed over some I/O facility, but in this
  840. * test program, we're the server, too:
  841. *
  842. * server_io: server's SSL communication
  843. *
  844. * server: pseudo-I/O for SSL library
  845. *
  846. * s_ssl_bio: SSL filter BIO
  847. *
  848. * The client and the server each employ a "BIO pair":
  849. * client + client_io, server + server_io.
  850. * BIO pairs are symmetric. A BIO pair behaves similar
  851. * to a non-blocking socketpair (but both endpoints must
  852. * be handled by the same thread).
  853. * [Here we could connect client and server to the ends
  854. * of a single BIO pair, but then this code would be less
  855. * suitable as an example for BIO pairs in general.]
  856. *
  857. * Useful functions for querying the state of BIO pair endpoints:
  858. *
  859. * BIO_ctrl_pending(bio) number of bytes we can read now
  860. * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfil
  861. * other side's read attempt
  862. * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
  863. *
  864. * ..._read_request is never more than ..._write_guarantee;
  865. * it depends on the application which one you should use.
  866. */
  867. /* We have non-blocking behaviour throughout this test program, but
  868. * can be sure that there is *some* progress in each iteration; so
  869. * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE
  870. * -- we just try everything in each iteration
  871. */
  872. {
  873. /* CLIENT */
  874. MS_STATIC char cbuf[1024*8];
  875. int i, r;
  876. clock_t c_clock = clock();
  877. memset(cbuf, 0, sizeof(cbuf));
  878. if (debug)
  879. if (SSL_in_init(c_ssl))
  880. printf("client waiting in SSL_connect - %s\n",
  881. SSL_state_string_long(c_ssl));
  882. if (cw_num > 0)
  883. {
  884. /* Write to server. */
  885. if (cw_num > (long)sizeof cbuf)
  886. i = sizeof cbuf;
  887. else
  888. i = (int)cw_num;
  889. r = BIO_write(c_ssl_bio, cbuf, i);
  890. if (r < 0)
  891. {
  892. if (!BIO_should_retry(c_ssl_bio))
  893. {
  894. fprintf(stderr,"ERROR in CLIENT\n");
  895. goto err;
  896. }
  897. /* BIO_should_retry(...) can just be ignored here.
  898. * The library expects us to call BIO_write with
  899. * the same arguments again, and that's what we will
  900. * do in the next iteration. */
  901. }
  902. else if (r == 0)
  903. {
  904. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  905. goto err;
  906. }
  907. else
  908. {
  909. if (debug)
  910. printf("client wrote %d\n", r);
  911. cw_num -= r;
  912. }
  913. }
  914. if (cr_num > 0)
  915. {
  916. /* Read from server. */
  917. r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
  918. if (r < 0)
  919. {
  920. if (!BIO_should_retry(c_ssl_bio))
  921. {
  922. fprintf(stderr,"ERROR in CLIENT\n");
  923. goto err;
  924. }
  925. /* Again, "BIO_should_retry" can be ignored. */
  926. }
  927. else if (r == 0)
  928. {
  929. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  930. goto err;
  931. }
  932. else
  933. {
  934. if (debug)
  935. printf("client read %d\n", r);
  936. cr_num -= r;
  937. }
  938. }
  939. /* c_time and s_time increments will typically be very small
  940. * (depending on machine speed and clock tick intervals),
  941. * but sampling over a large number of connections should
  942. * result in fairly accurate figures. We cannot guarantee
  943. * a lot, however -- if each connection lasts for exactly
  944. * one clock tick, it will be counted only for the client
  945. * or only for the server or even not at all.
  946. */
  947. *c_time += (clock() - c_clock);
  948. }
  949. {
  950. /* SERVER */
  951. MS_STATIC char sbuf[1024*8];
  952. int i, r;
  953. clock_t s_clock = clock();
  954. memset(sbuf, 0, sizeof(sbuf));
  955. if (debug)
  956. if (SSL_in_init(s_ssl))
  957. printf("server waiting in SSL_accept - %s\n",
  958. SSL_state_string_long(s_ssl));
  959. if (sw_num > 0)
  960. {
  961. /* Write to client. */
  962. if (sw_num > (long)sizeof sbuf)
  963. i = sizeof sbuf;
  964. else
  965. i = (int)sw_num;
  966. r = BIO_write(s_ssl_bio, sbuf, i);
  967. if (r < 0)
  968. {
  969. if (!BIO_should_retry(s_ssl_bio))
  970. {
  971. fprintf(stderr,"ERROR in SERVER\n");
  972. goto err;
  973. }
  974. /* Ignore "BIO_should_retry". */
  975. }
  976. else if (r == 0)
  977. {
  978. fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
  979. goto err;
  980. }
  981. else
  982. {
  983. if (debug)
  984. printf("server wrote %d\n", r);
  985. sw_num -= r;
  986. }
  987. }
  988. if (sr_num > 0)
  989. {
  990. /* Read from client. */
  991. r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
  992. if (r < 0)
  993. {
  994. if (!BIO_should_retry(s_ssl_bio))
  995. {
  996. fprintf(stderr,"ERROR in SERVER\n");
  997. goto err;
  998. }
  999. /* blah, blah */
  1000. }
  1001. else if (r == 0)
  1002. {
  1003. fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
  1004. goto err;
  1005. }
  1006. else
  1007. {
  1008. if (debug)
  1009. printf("server read %d\n", r);
  1010. sr_num -= r;
  1011. }
  1012. }
  1013. *s_time += (clock() - s_clock);
  1014. }
  1015. {
  1016. /* "I/O" BETWEEN CLIENT AND SERVER. */
  1017. size_t r1, r2;
  1018. BIO *io1 = server_io, *io2 = client_io;
  1019. /* we use the non-copying interface for io1
  1020. * and the standard BIO_write/BIO_read interface for io2
  1021. */
  1022. static int prev_progress = 1;
  1023. int progress = 0;
  1024. /* io1 to io2 */
  1025. do
  1026. {
  1027. size_t num;
  1028. int r;
  1029. r1 = BIO_ctrl_pending(io1);
  1030. r2 = BIO_ctrl_get_write_guarantee(io2);
  1031. num = r1;
  1032. if (r2 < num)
  1033. num = r2;
  1034. if (num)
  1035. {
  1036. char *dataptr;
  1037. if (INT_MAX < num) /* yeah, right */
  1038. num = INT_MAX;
  1039. r = BIO_nread(io1, &dataptr, (int)num);
  1040. assert(r > 0);
  1041. assert(r <= (int)num);
  1042. /* possibly r < num (non-contiguous data) */
  1043. num = r;
  1044. r = BIO_write(io2, dataptr, (int)num);
  1045. if (r != (int)num) /* can't happen */
  1046. {
  1047. fprintf(stderr, "ERROR: BIO_write could not write "
  1048. "BIO_ctrl_get_write_guarantee() bytes");
  1049. goto err;
  1050. }
  1051. progress = 1;
  1052. if (debug)
  1053. printf((io1 == client_io) ?
  1054. "C->S relaying: %d bytes\n" :
  1055. "S->C relaying: %d bytes\n",
  1056. (int)num);
  1057. }
  1058. }
  1059. while (r1 && r2);
  1060. /* io2 to io1 */
  1061. {
  1062. size_t num;
  1063. int r;
  1064. r1 = BIO_ctrl_pending(io2);
  1065. r2 = BIO_ctrl_get_read_request(io1);
  1066. /* here we could use ..._get_write_guarantee instead of
  1067. * ..._get_read_request, but by using the latter
  1068. * we test restartability of the SSL implementation
  1069. * more thoroughly */
  1070. num = r1;
  1071. if (r2 < num)
  1072. num = r2;
  1073. if (num)
  1074. {
  1075. char *dataptr;
  1076. if (INT_MAX < num)
  1077. num = INT_MAX;
  1078. if (num > 1)
  1079. --num; /* test restartability even more thoroughly */
  1080. r = BIO_nwrite0(io1, &dataptr);
  1081. assert(r > 0);
  1082. if (r < (int)num)
  1083. num = r;
  1084. r = BIO_read(io2, dataptr, (int)num);
  1085. if (r != (int)num) /* can't happen */
  1086. {
  1087. fprintf(stderr, "ERROR: BIO_read could not read "
  1088. "BIO_ctrl_pending() bytes");
  1089. goto err;
  1090. }
  1091. progress = 1;
  1092. r = BIO_nwrite(io1, &dataptr, (int)num);
  1093. if (r != (int)num) /* can't happen */
  1094. {
  1095. fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
  1096. "BIO_nwrite0() bytes");
  1097. goto err;
  1098. }
  1099. if (debug)
  1100. printf((io2 == client_io) ?
  1101. "C->S relaying: %d bytes\n" :
  1102. "S->C relaying: %d bytes\n",
  1103. (int)num);
  1104. }
  1105. } /* no loop, BIO_ctrl_get_read_request now returns 0 anyway */
  1106. if (!progress && !prev_progress)
  1107. if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0)
  1108. {
  1109. fprintf(stderr, "ERROR: got stuck\n");
  1110. if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0)
  1111. {
  1112. fprintf(stderr, "This can happen for SSL2 because "
  1113. "CLIENT-FINISHED and SERVER-VERIFY are written \n"
  1114. "concurrently ...");
  1115. if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0
  1116. && strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0)
  1117. {
  1118. fprintf(stderr, " ok.\n");
  1119. goto end;
  1120. }
  1121. }
  1122. fprintf(stderr, " ERROR.\n");
  1123. goto err;
  1124. }
  1125. prev_progress = progress;
  1126. }
  1127. }
  1128. while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
  1129. if (verbose)
  1130. print_details(c_ssl, "DONE via BIO pair: ");
  1131. end:
  1132. ret = 0;
  1133. err:
  1134. ERR_print_errors(bio_err);
  1135. if (server)
  1136. BIO_free(server);
  1137. if (server_io)
  1138. BIO_free(server_io);
  1139. if (client)
  1140. BIO_free(client);
  1141. if (client_io)
  1142. BIO_free(client_io);
  1143. if (s_ssl_bio)
  1144. BIO_free(s_ssl_bio);
  1145. if (c_ssl_bio)
  1146. BIO_free(c_ssl_bio);
  1147. return ret;
  1148. }
  1149. #define W_READ 1
  1150. #define W_WRITE 2
  1151. #define C_DONE 1
  1152. #define S_DONE 2
  1153. int doit(SSL *s_ssl, SSL *c_ssl, long count)
  1154. {
  1155. MS_STATIC char cbuf[1024*8],sbuf[1024*8];
  1156. long cw_num=count,cr_num=count;
  1157. long sw_num=count,sr_num=count;
  1158. int ret=1;
  1159. BIO *c_to_s=NULL;
  1160. BIO *s_to_c=NULL;
  1161. BIO *c_bio=NULL;
  1162. BIO *s_bio=NULL;
  1163. int c_r,c_w,s_r,s_w;
  1164. int c_want,s_want;
  1165. int i,j;
  1166. int done=0;
  1167. int c_write,s_write;
  1168. int do_server=0,do_client=0;
  1169. memset(cbuf,0,sizeof(cbuf));
  1170. memset(sbuf,0,sizeof(sbuf));
  1171. c_to_s=BIO_new(BIO_s_mem());
  1172. s_to_c=BIO_new(BIO_s_mem());
  1173. if ((s_to_c == NULL) || (c_to_s == NULL))
  1174. {
  1175. ERR_print_errors(bio_err);
  1176. goto err;
  1177. }
  1178. c_bio=BIO_new(BIO_f_ssl());
  1179. s_bio=BIO_new(BIO_f_ssl());
  1180. if ((c_bio == NULL) || (s_bio == NULL))
  1181. {
  1182. ERR_print_errors(bio_err);
  1183. goto err;
  1184. }
  1185. SSL_set_connect_state(c_ssl);
  1186. SSL_set_bio(c_ssl,s_to_c,c_to_s);
  1187. BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);
  1188. SSL_set_accept_state(s_ssl);
  1189. SSL_set_bio(s_ssl,c_to_s,s_to_c);
  1190. BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);
  1191. c_r=0; s_r=1;
  1192. c_w=1; s_w=0;
  1193. c_want=W_WRITE;
  1194. s_want=0;
  1195. c_write=1,s_write=0;
  1196. /* We can always do writes */
  1197. for (;;)
  1198. {
  1199. do_server=0;
  1200. do_client=0;
  1201. i=(int)BIO_pending(s_bio);
  1202. if ((i && s_r) || s_w) do_server=1;
  1203. i=(int)BIO_pending(c_bio);
  1204. if ((i && c_r) || c_w) do_client=1;
  1205. if (do_server && debug)
  1206. {
  1207. if (SSL_in_init(s_ssl))
  1208. printf("server waiting in SSL_accept - %s\n",
  1209. SSL_state_string_long(s_ssl));
  1210. /* else if (s_write)
  1211. printf("server:SSL_write()\n");
  1212. else
  1213. printf("server:SSL_read()\n"); */
  1214. }
  1215. if (do_client && debug)
  1216. {
  1217. if (SSL_in_init(c_ssl))
  1218. printf("client waiting in SSL_connect - %s\n",
  1219. SSL_state_string_long(c_ssl));
  1220. /* else if (c_write)
  1221. printf("client:SSL_write()\n");
  1222. else
  1223. printf("client:SSL_read()\n"); */
  1224. }
  1225. if (!do_client && !do_server)
  1226. {
  1227. fprintf(stdout,"ERROR IN STARTUP\n");
  1228. ERR_print_errors(bio_err);
  1229. break;
  1230. }
  1231. if (do_client && !(done & C_DONE))
  1232. {
  1233. if (c_write)
  1234. {
  1235. j = (cw_num > (long)sizeof(cbuf)) ?
  1236. (int)sizeof(cbuf) : (int)cw_num;
  1237. i=BIO_write(c_bio,cbuf,j);
  1238. if (i < 0)
  1239. {
  1240. c_r=0;
  1241. c_w=0;
  1242. if (BIO_should_retry(c_bio))
  1243. {
  1244. if (BIO_should_read(c_bio))
  1245. c_r=1;
  1246. if (BIO_should_write(c_bio))
  1247. c_w=1;
  1248. }
  1249. else
  1250. {
  1251. fprintf(stderr,"ERROR in CLIENT\n");
  1252. ERR_print_errors(bio_err);
  1253. goto err;
  1254. }
  1255. }
  1256. else if (i == 0)
  1257. {
  1258. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1259. goto err;
  1260. }
  1261. else
  1262. {
  1263. if (debug)
  1264. printf("client wrote %d\n",i);
  1265. /* ok */
  1266. s_r=1;
  1267. c_write=0;
  1268. cw_num-=i;
  1269. }
  1270. }
  1271. else
  1272. {
  1273. i=BIO_read(c_bio,cbuf,sizeof(cbuf));
  1274. if (i < 0)
  1275. {
  1276. c_r=0;
  1277. c_w=0;
  1278. if (BIO_should_retry(c_bio))
  1279. {
  1280. if (BIO_should_read(c_bio))
  1281. c_r=1;
  1282. if (BIO_should_write(c_bio))
  1283. c_w=1;
  1284. }
  1285. else
  1286. {
  1287. fprintf(stderr,"ERROR in CLIENT\n");
  1288. ERR_print_errors(bio_err);
  1289. goto err;
  1290. }
  1291. }
  1292. else if (i == 0)
  1293. {
  1294. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1295. goto err;
  1296. }
  1297. else
  1298. {
  1299. if (debug)
  1300. printf("client read %d\n",i);
  1301. cr_num-=i;
  1302. if (sw_num > 0)
  1303. {
  1304. s_write=1;
  1305. s_w=1;
  1306. }
  1307. if (cr_num <= 0)
  1308. {
  1309. s_write=1;
  1310. s_w=1;
  1311. done=S_DONE|C_DONE;
  1312. }
  1313. }
  1314. }
  1315. }
  1316. if (do_server && !(done & S_DONE))
  1317. {
  1318. if (!s_write)
  1319. {
  1320. i=BIO_read(s_bio,sbuf,sizeof(cbuf));
  1321. if (i < 0)
  1322. {
  1323. s_r=0;
  1324. s_w=0;
  1325. if (BIO_should_retry(s_bio))
  1326. {
  1327. if (BIO_should_read(s_bio))
  1328. s_r=1;
  1329. if (BIO_should_write(s_bio))
  1330. s_w=1;
  1331. }
  1332. else
  1333. {
  1334. fprintf(stderr,"ERROR in SERVER\n");
  1335. ERR_print_errors(bio_err);
  1336. goto err;
  1337. }
  1338. }
  1339. else if (i == 0)
  1340. {
  1341. ERR_print_errors(bio_err);
  1342. fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");
  1343. goto err;
  1344. }
  1345. else
  1346. {
  1347. if (debug)
  1348. printf("server read %d\n",i);
  1349. sr_num-=i;
  1350. if (cw_num > 0)
  1351. {
  1352. c_write=1;
  1353. c_w=1;
  1354. }
  1355. if (sr_num <= 0)
  1356. {
  1357. s_write=1;
  1358. s_w=1;
  1359. c_write=0;
  1360. }
  1361. }
  1362. }
  1363. else
  1364. {
  1365. j = (sw_num > (long)sizeof(sbuf)) ?
  1366. (int)sizeof(sbuf) : (int)sw_num;
  1367. i=BIO_write(s_bio,sbuf,j);
  1368. if (i < 0)
  1369. {
  1370. s_r=0;
  1371. s_w=0;
  1372. if (BIO_should_retry(s_bio))
  1373. {
  1374. if (BIO_should_read(s_bio))
  1375. s_r=1;
  1376. if (BIO_should_write(s_bio))
  1377. s_w=1;
  1378. }
  1379. else
  1380. {
  1381. fprintf(stderr,"ERROR in SERVER\n");
  1382. ERR_print_errors(bio_err);
  1383. goto err;
  1384. }
  1385. }
  1386. else if (i == 0)
  1387. {
  1388. ERR_print_errors(bio_err);
  1389. fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
  1390. goto err;
  1391. }
  1392. else
  1393. {
  1394. if (debug)
  1395. printf("server wrote %d\n",i);
  1396. sw_num-=i;
  1397. s_write=0;
  1398. c_r=1;
  1399. if (sw_num <= 0)
  1400. done|=S_DONE;
  1401. }
  1402. }
  1403. }
  1404. if ((done & S_DONE) && (done & C_DONE)) break;
  1405. }
  1406. if (verbose)
  1407. print_details(c_ssl, "DONE: ");
  1408. ret=0;
  1409. err:
  1410. /* We have to set the BIO's to NULL otherwise they will be
  1411. * OPENSSL_free()ed twice. Once when th s_ssl is SSL_free()ed and
  1412. * again when c_ssl is SSL_free()ed.
  1413. * This is a hack required because s_ssl and c_ssl are sharing the same
  1414. * BIO structure and SSL_set_bio() and SSL_free() automatically
  1415. * BIO_free non NULL entries.
  1416. * You should not normally do this or be required to do this */
  1417. if (s_ssl != NULL)
  1418. {
  1419. s_ssl->rbio=NULL;
  1420. s_ssl->wbio=NULL;
  1421. }
  1422. if (c_ssl != NULL)
  1423. {
  1424. c_ssl->rbio=NULL;
  1425. c_ssl->wbio=NULL;
  1426. }
  1427. if (c_to_s != NULL) BIO_free(c_to_s);
  1428. if (s_to_c != NULL) BIO_free(s_to_c);
  1429. if (c_bio != NULL) BIO_free_all(c_bio);
  1430. if (s_bio != NULL) BIO_free_all(s_bio);
  1431. return(ret);
  1432. }
  1433. static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
  1434. {
  1435. char *s,buf[256];
  1436. s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,
  1437. sizeof buf);
  1438. if (s != NULL)
  1439. {
  1440. if (ok)
  1441. fprintf(stderr,"depth=%d %s\n",ctx->error_depth,buf);
  1442. else
  1443. fprintf(stderr,"depth=%d error=%d %s\n",
  1444. ctx->error_depth,ctx->error,buf);
  1445. }
  1446. if (ok == 0)
  1447. {
  1448. switch (ctx->error)
  1449. {
  1450. case X509_V_ERR_CERT_NOT_YET_VALID:
  1451. case X509_V_ERR_CERT_HAS_EXPIRED:
  1452. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  1453. ok=1;
  1454. }
  1455. }
  1456. return(ok);
  1457. }
  1458. static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg)
  1459. {
  1460. char *s = NULL,buf[256];
  1461. int ok=1;
  1462. fprintf(stderr, "In app_verify_callback, allowing cert. ");
  1463. fprintf(stderr, "Arg is: %s\n", (char *)arg);
  1464. fprintf(stderr, "Finished printing do we have a context? 0x%x a cert? 0x%x\n",
  1465. (unsigned int)ctx, (unsigned int)ctx->cert);
  1466. if (ctx->cert)
  1467. s=X509_NAME_oneline(X509_get_subject_name(ctx->cert),buf,256);
  1468. if (s != NULL)
  1469. {
  1470. fprintf(stderr,"cert depth=%d %s\n",ctx->error_depth,buf);
  1471. }
  1472. return(ok);
  1473. }
  1474. #ifndef OPENSSL_NO_RSA
  1475. static RSA *rsa_tmp=NULL;
  1476. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  1477. {
  1478. if (rsa_tmp == NULL)
  1479. {
  1480. rsa_tmp = RSA_new();
  1481. if(!rsa_tmp)
  1482. {
  1483. BIO_printf(bio_err, "Memory error...");
  1484. goto end;
  1485. }
  1486. BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  1487. (void)BIO_flush(bio_err);
  1488. if(!RSA_generate_key_ex(rsa_tmp,keylength,RSA_F4,NULL))
  1489. {
  1490. BIO_printf(bio_err, "Error generating key.");
  1491. RSA_free(rsa_tmp);
  1492. rsa_tmp = NULL;
  1493. }
  1494. end:
  1495. BIO_printf(bio_err,"\n");
  1496. (void)BIO_flush(bio_err);
  1497. }
  1498. return(rsa_tmp);
  1499. }
  1500. static void free_tmp_rsa(void)
  1501. {
  1502. if (rsa_tmp != NULL)
  1503. {
  1504. RSA_free(rsa_tmp);
  1505. rsa_tmp = NULL;
  1506. }
  1507. }
  1508. #endif
  1509. #ifndef OPENSSL_NO_DH
  1510. /* These DH parameters have been generated as follows:
  1511. * $ openssl dhparam -C -noout 512
  1512. * $ openssl dhparam -C -noout 1024
  1513. * $ openssl dhparam -C -noout -dsaparam 1024
  1514. * (The third function has been renamed to avoid name conflicts.)
  1515. */
  1516. static DH *get_dh512()
  1517. {
  1518. static unsigned char dh512_p[]={
  1519. 0xCB,0xC8,0xE1,0x86,0xD0,0x1F,0x94,0x17,0xA6,0x99,0xF0,0xC6,
  1520. 0x1F,0x0D,0xAC,0xB6,0x25,0x3E,0x06,0x39,0xCA,0x72,0x04,0xB0,
  1521. 0x6E,0xDA,0xC0,0x61,0xE6,0x7A,0x77,0x25,0xE8,0x3B,0xB9,0x5F,
  1522. 0x9A,0xB6,0xB5,0xFE,0x99,0x0B,0xA1,0x93,0x4E,0x35,0x33,0xB8,
  1523. 0xE1,0xF1,0x13,0x4F,0x59,0x1A,0xD2,0x57,0xC0,0x26,0x21,0x33,
  1524. 0x02,0xC5,0xAE,0x23,
  1525. };
  1526. static unsigned char dh512_g[]={
  1527. 0x02,
  1528. };
  1529. DH *dh;
  1530. if ((dh=DH_new()) == NULL) return(NULL);
  1531. dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  1532. dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  1533. if ((dh->p == NULL) || (dh->g == NULL))
  1534. { DH_free(dh); return(NULL); }
  1535. return(dh);
  1536. }
  1537. static DH *get_dh1024()
  1538. {
  1539. static unsigned char dh1024_p[]={
  1540. 0xF8,0x81,0x89,0x7D,0x14,0x24,0xC5,0xD1,0xE6,0xF7,0xBF,0x3A,
  1541. 0xE4,0x90,0xF4,0xFC,0x73,0xFB,0x34,0xB5,0xFA,0x4C,0x56,0xA2,
  1542. 0xEA,0xA7,0xE9,0xC0,0xC0,0xCE,0x89,0xE1,0xFA,0x63,0x3F,0xB0,
  1543. 0x6B,0x32,0x66,0xF1,0xD1,0x7B,0xB0,0x00,0x8F,0xCA,0x87,0xC2,
  1544. 0xAE,0x98,0x89,0x26,0x17,0xC2,0x05,0xD2,0xEC,0x08,0xD0,0x8C,
  1545. 0xFF,0x17,0x52,0x8C,0xC5,0x07,0x93,0x03,0xB1,0xF6,0x2F,0xB8,
  1546. 0x1C,0x52,0x47,0x27,0x1B,0xDB,0xD1,0x8D,0x9D,0x69,0x1D,0x52,
  1547. 0x4B,0x32,0x81,0xAA,0x7F,0x00,0xC8,0xDC,0xE6,0xD9,0xCC,0xC1,
  1548. 0x11,0x2D,0x37,0x34,0x6C,0xEA,0x02,0x97,0x4B,0x0E,0xBB,0xB1,
  1549. 0x71,0x33,0x09,0x15,0xFD,0xDD,0x23,0x87,0x07,0x5E,0x89,0xAB,
  1550. 0x6B,0x7C,0x5F,0xEC,0xA6,0x24,0xDC,0x53,
  1551. };
  1552. static unsigned char dh1024_g[]={
  1553. 0x02,
  1554. };
  1555. DH *dh;
  1556. if ((dh=DH_new()) == NULL) return(NULL);
  1557. dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
  1558. dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
  1559. if ((dh->p == NULL) || (dh->g == NULL))
  1560. { DH_free(dh); return(NULL); }
  1561. return(dh);
  1562. }
  1563. static DH *get_dh1024dsa()
  1564. {
  1565. static unsigned char dh1024_p[]={
  1566. 0xC8,0x00,0xF7,0x08,0x07,0x89,0x4D,0x90,0x53,0xF3,0xD5,0x00,
  1567. 0x21,0x1B,0xF7,0x31,0xA6,0xA2,0xDA,0x23,0x9A,0xC7,0x87,0x19,
  1568. 0x3B,0x47,0xB6,0x8C,0x04,0x6F,0xFF,0xC6,0x9B,0xB8,0x65,0xD2,
  1569. 0xC2,0x5F,0x31,0x83,0x4A,0xA7,0x5F,0x2F,0x88,0x38,0xB6,0x55,
  1570. 0xCF,0xD9,0x87,0x6D,0x6F,0x9F,0xDA,0xAC,0xA6,0x48,0xAF,0xFC,
  1571. 0x33,0x84,0x37,0x5B,0x82,0x4A,0x31,0x5D,0xE7,0xBD,0x52,0x97,
  1572. 0xA1,0x77,0xBF,0x10,0x9E,0x37,0xEA,0x64,0xFA,0xCA,0x28,0x8D,
  1573. 0x9D,0x3B,0xD2,0x6E,0x09,0x5C,0x68,0xC7,0x45,0x90,0xFD,0xBB,
  1574. 0x70,0xC9,0x3A,0xBB,0xDF,0xD4,0x21,0x0F,0xC4,0x6A,0x3C,0xF6,
  1575. 0x61,0xCF,0x3F,0xD6,0x13,0xF1,0x5F,0xBC,0xCF,0xBC,0x26,0x9E,
  1576. 0xBC,0x0B,0xBD,0xAB,0x5D,0xC9,0x54,0x39,
  1577. };
  1578. static unsigned char dh1024_g[]={
  1579. 0x3B,0x40,0x86,0xE7,0xF3,0x6C,0xDE,0x67,0x1C,0xCC,0x80,0x05,
  1580. 0x5A,0xDF,0xFE,0xBD,0x20,0x27,0x74,0x6C,0x24,0xC9,0x03,0xF3,
  1581. 0xE1,0x8D,0xC3,0x7D,0x98,0x27,0x40,0x08,0xB8,0x8C,0x6A,0xE9,
  1582. 0xBB,0x1A,0x3A,0xD6,0x86,0x83,0x5E,0x72,0x41,0xCE,0x85,0x3C,
  1583. 0xD2,0xB3,0xFC,0x13,0xCE,0x37,0x81,0x9E,0x4C,0x1C,0x7B,0x65,
  1584. 0xD3,0xE6,0xA6,0x00,0xF5,0x5A,0x95,0x43,0x5E,0x81,0xCF,0x60,
  1585. 0xA2,0x23,0xFC,0x36,0xA7,0x5D,0x7A,0x4C,0x06,0x91,0x6E,0xF6,
  1586. 0x57,0xEE,0x36,0xCB,0x06,0xEA,0xF5,0x3D,0x95,0x49,0xCB,0xA7,
  1587. 0xDD,0x81,0xDF,0x80,0x09,0x4A,0x97,0x4D,0xA8,0x22,0x72,0xA1,
  1588. 0x7F,0xC4,0x70,0x56,0x70,0xE8,0x20,0x10,0x18,0x8F,0x2E,0x60,
  1589. 0x07,0xE7,0x68,0x1A,0x82,0x5D,0x32,0xA2,
  1590. };
  1591. DH *dh;
  1592. if ((dh=DH_new()) == NULL) return(NULL);
  1593. dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
  1594. dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
  1595. if ((dh->p == NULL) || (dh->g == NULL))
  1596. { DH_free(dh); return(NULL); }
  1597. dh->length = 160;
  1598. return(dh);
  1599. }
  1600. #endif