ssltest.c 42 KB

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