ssltest.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  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. #define _XOPEN_SOURCE 500 /* Or isascii won't be declared properly on
  128. VMS (at least with DECompHP C). */
  129. #include <ctype.h>
  130. #include <openssl/bio.h>
  131. #include <openssl/crypto.h>
  132. #include <openssl/evp.h>
  133. #include <openssl/x509.h>
  134. #include <openssl/x509v3.h>
  135. #include <openssl/ssl.h>
  136. #ifndef OPENSSL_NO_ENGINE
  137. #include <openssl/engine.h>
  138. #endif
  139. #include <openssl/err.h>
  140. #include <openssl/rand.h>
  141. #ifndef OPENSSL_NO_RSA
  142. #include <openssl/rsa.h>
  143. #endif
  144. #ifndef OPENSSL_NO_DSA
  145. #include <openssl/dsa.h>
  146. #endif
  147. #ifndef OPENSSL_NO_DH
  148. #include <openssl/dh.h>
  149. #endif
  150. #include <openssl/bn.h>
  151. #define _XOPEN_SOURCE_EXTENDED 1 /* Or gethostname won't be declared properly
  152. on Compaq platforms (at least with DEC C).
  153. Do not try to put it earlier, or IPv6 includes
  154. get screwed...
  155. */
  156. #ifdef OPENSSL_SYS_WINDOWS
  157. #include <winsock.h>
  158. #else
  159. #include OPENSSL_UNISTD
  160. #endif
  161. #ifdef OPENSSL_SYS_VMS
  162. # define TEST_SERVER_CERT "SYS$DISK:[-.APPS]SERVER.PEM"
  163. # define TEST_CLIENT_CERT "SYS$DISK:[-.APPS]CLIENT.PEM"
  164. #elif defined(OPENSSL_SYS_WINCE)
  165. # define TEST_SERVER_CERT "\\OpenSSL\\server.pem"
  166. # define TEST_CLIENT_CERT "\\OpenSSL\\client.pem"
  167. #elif defined(OPENSSL_SYS_NETWARE)
  168. # define TEST_SERVER_CERT "\\openssl\\apps\\server.pem"
  169. # define TEST_CLIENT_CERT "\\openssl\\apps\\client.pem"
  170. #else
  171. # define TEST_SERVER_CERT "../apps/server.pem"
  172. # define TEST_CLIENT_CERT "../apps/client.pem"
  173. #endif
  174. /* There is really no standard for this, so let's assign some tentative
  175. numbers. In any case, these numbers are only for this test */
  176. #define COMP_RLE 255
  177. #define COMP_ZLIB 1
  178. static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
  179. #ifndef OPENSSL_NO_RSA
  180. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);
  181. static void free_tmp_rsa(void);
  182. #endif
  183. static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg);
  184. #define APP_CALLBACK_STRING "Test Callback Argument"
  185. struct app_verify_arg
  186. {
  187. char *string;
  188. int app_verify;
  189. int allow_proxy_certs;
  190. char *proxy_auth;
  191. char *proxy_cond;
  192. };
  193. #ifndef OPENSSL_NO_DH
  194. static DH *get_dh512(void);
  195. static DH *get_dh1024(void);
  196. static DH *get_dh1024dsa(void);
  197. #endif
  198. static BIO *bio_err=NULL;
  199. static BIO *bio_stdout=NULL;
  200. static char *cipher=NULL;
  201. static int verbose=0;
  202. static int debug=0;
  203. #if 0
  204. /* Not used yet. */
  205. #ifdef FIONBIO
  206. static int s_nbio=0;
  207. #endif
  208. #endif
  209. static const char rnd_seed[] = "string to make the random number generator think it has entropy";
  210. int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes,clock_t *s_time,clock_t *c_time);
  211. int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
  212. static int do_test_cipherlist(void);
  213. static void sv_usage(void)
  214. {
  215. fprintf(stderr,"usage: ssltest [args ...]\n");
  216. fprintf(stderr,"\n");
  217. fprintf(stderr," -server_auth - check server certificate\n");
  218. fprintf(stderr," -client_auth - do client authentication\n");
  219. fprintf(stderr," -proxy - allow proxy certificates\n");
  220. fprintf(stderr," -proxy_auth <val> - set proxy policy rights\n");
  221. fprintf(stderr," -proxy_cond <val> - experssion to test proxy policy rights\n");
  222. fprintf(stderr," -v - more output\n");
  223. fprintf(stderr," -d - debug output\n");
  224. fprintf(stderr," -reuse - use session-id reuse\n");
  225. fprintf(stderr," -num <val> - number of connections to perform\n");
  226. fprintf(stderr," -bytes <val> - number of bytes to swap between client/server\n");
  227. #ifndef OPENSSL_NO_DH
  228. fprintf(stderr," -dhe1024 - use 1024 bit key (safe prime) for DHE\n");
  229. fprintf(stderr," -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
  230. fprintf(stderr," -no_dhe - disable DHE\n");
  231. #endif
  232. #ifndef OPENSSL_NO_ECDH
  233. fprintf(stderr," -no_ecdhe - disable ECDHE\n");
  234. #endif
  235. #ifndef OPENSSL_NO_SSL2
  236. fprintf(stderr," -ssl2 - use SSLv2\n");
  237. #endif
  238. #ifndef OPENSSL_NO_SSL3
  239. fprintf(stderr," -ssl3 - use SSLv3\n");
  240. #endif
  241. #ifndef OPENSSL_NO_TLS1
  242. fprintf(stderr," -tls1 - use TLSv1\n");
  243. #endif
  244. fprintf(stderr," -CApath arg - PEM format directory of CA's\n");
  245. fprintf(stderr," -CAfile arg - PEM format file of CA's\n");
  246. fprintf(stderr," -cert arg - Server certificate file\n");
  247. fprintf(stderr," -key arg - Server key file (default: same as -cert)\n");
  248. fprintf(stderr," -c_cert arg - Client certificate file\n");
  249. fprintf(stderr," -c_key arg - Client key file (default: same as -c_cert)\n");
  250. fprintf(stderr," -cipher arg - The cipher list\n");
  251. fprintf(stderr," -bio_pair - Use BIO pairs\n");
  252. fprintf(stderr," -f - Test even cases that can't work\n");
  253. fprintf(stderr," -time - measure processor time used by client and server\n");
  254. fprintf(stderr," -zlib - use zlib compression\n");
  255. fprintf(stderr," -rle - use rle compression\n");
  256. #ifndef OPENSSL_NO_ECDH
  257. fprintf(stderr," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
  258. " Use \"openssl ecparam -list_curves\" for all names\n" \
  259. " (default is sect163r2).\n");
  260. #endif
  261. fprintf(stderr," -test_cipherlist - verifies the order of the ssl cipher lists\n");
  262. }
  263. static void print_details(SSL *c_ssl, const char *prefix)
  264. {
  265. SSL_CIPHER *ciph;
  266. X509 *cert;
  267. ciph=SSL_get_current_cipher(c_ssl);
  268. BIO_printf(bio_stdout,"%s%s, cipher %s %s",
  269. prefix,
  270. SSL_get_version(c_ssl),
  271. SSL_CIPHER_get_version(ciph),
  272. SSL_CIPHER_get_name(ciph));
  273. cert=SSL_get_peer_certificate(c_ssl);
  274. if (cert != NULL)
  275. {
  276. EVP_PKEY *pkey = X509_get_pubkey(cert);
  277. if (pkey != NULL)
  278. {
  279. if (0)
  280. ;
  281. #ifndef OPENSSL_NO_RSA
  282. else if (pkey->type == EVP_PKEY_RSA && pkey->pkey.rsa != NULL
  283. && pkey->pkey.rsa->n != NULL)
  284. {
  285. BIO_printf(bio_stdout, ", %d bit RSA",
  286. BN_num_bits(pkey->pkey.rsa->n));
  287. }
  288. #endif
  289. #ifndef OPENSSL_NO_DSA
  290. else if (pkey->type == EVP_PKEY_DSA && pkey->pkey.dsa != NULL
  291. && pkey->pkey.dsa->p != NULL)
  292. {
  293. BIO_printf(bio_stdout, ", %d bit DSA",
  294. BN_num_bits(pkey->pkey.dsa->p));
  295. }
  296. #endif
  297. EVP_PKEY_free(pkey);
  298. }
  299. X509_free(cert);
  300. }
  301. /* The SSL API does not allow us to look at temporary RSA/DH keys,
  302. * otherwise we should print their lengths too */
  303. BIO_printf(bio_stdout,"\n");
  304. }
  305. static void lock_dbg_cb(int mode, int type, const char *file, int line)
  306. {
  307. static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
  308. const char *errstr = NULL;
  309. int rw;
  310. rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
  311. if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
  312. {
  313. errstr = "invalid mode";
  314. goto err;
  315. }
  316. if (type < 0 || type >= CRYPTO_NUM_LOCKS)
  317. {
  318. errstr = "type out of bounds";
  319. goto err;
  320. }
  321. if (mode & CRYPTO_LOCK)
  322. {
  323. if (modes[type])
  324. {
  325. errstr = "already locked";
  326. /* must not happen in a single-threaded program
  327. * (would deadlock) */
  328. goto err;
  329. }
  330. modes[type] = rw;
  331. }
  332. else if (mode & CRYPTO_UNLOCK)
  333. {
  334. if (!modes[type])
  335. {
  336. errstr = "not locked";
  337. goto err;
  338. }
  339. if (modes[type] != rw)
  340. {
  341. errstr = (rw == CRYPTO_READ) ?
  342. "CRYPTO_r_unlock on write lock" :
  343. "CRYPTO_w_unlock on read lock";
  344. }
  345. modes[type] = 0;
  346. }
  347. else
  348. {
  349. errstr = "invalid mode";
  350. goto err;
  351. }
  352. err:
  353. if (errstr)
  354. {
  355. /* we cannot use bio_err here */
  356. fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
  357. errstr, mode, type, file, line);
  358. }
  359. }
  360. int main(int argc, char *argv[])
  361. {
  362. char *CApath=NULL,*CAfile=NULL;
  363. int badop=0;
  364. int bio_pair=0;
  365. int force=0;
  366. int tls1=0,ssl2=0,ssl3=0,ret=1;
  367. int client_auth=0;
  368. int server_auth=0,i;
  369. struct app_verify_arg app_verify_arg =
  370. { APP_CALLBACK_STRING, 0, 0, NULL, NULL };
  371. char *server_cert=TEST_SERVER_CERT;
  372. char *server_key=NULL;
  373. char *client_cert=TEST_CLIENT_CERT;
  374. char *client_key=NULL;
  375. #ifndef OPENSSL_NO_ECDH
  376. char *named_curve = NULL;
  377. #endif
  378. SSL_CTX *s_ctx=NULL;
  379. SSL_CTX *c_ctx=NULL;
  380. SSL_METHOD *meth=NULL;
  381. SSL *c_ssl,*s_ssl;
  382. int number=1,reuse=0;
  383. long bytes=256L;
  384. #ifndef OPENSSL_NO_DH
  385. DH *dh;
  386. int dhe1024 = 0, dhe1024dsa = 0;
  387. #endif
  388. #ifndef OPENSSL_NO_ECDH
  389. EC_KEY *ecdh = NULL;
  390. #endif
  391. int no_dhe = 0;
  392. int no_ecdhe = 0;
  393. int print_time = 0;
  394. clock_t s_time = 0, c_time = 0;
  395. int comp = 0;
  396. #ifndef OPENSSL_NO_COMP
  397. COMP_METHOD *cm = NULL;
  398. #endif
  399. STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
  400. int test_cipherlist = 0;
  401. verbose = 0;
  402. debug = 0;
  403. cipher = 0;
  404. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  405. CRYPTO_set_locking_callback(lock_dbg_cb);
  406. /* enable memory leak checking unless explicitly disabled */
  407. if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
  408. {
  409. CRYPTO_malloc_debug_init();
  410. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  411. }
  412. else
  413. {
  414. /* OPENSSL_DEBUG_MEMORY=off */
  415. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  416. }
  417. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  418. RAND_seed(rnd_seed, sizeof rnd_seed);
  419. bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
  420. argc--;
  421. argv++;
  422. while (argc >= 1)
  423. {
  424. if (strcmp(*argv,"-server_auth") == 0)
  425. server_auth=1;
  426. else if (strcmp(*argv,"-client_auth") == 0)
  427. client_auth=1;
  428. else if (strcmp(*argv,"-proxy_auth") == 0)
  429. {
  430. if (--argc < 1) goto bad;
  431. app_verify_arg.proxy_auth= *(++argv);
  432. }
  433. else if (strcmp(*argv,"-proxy_cond") == 0)
  434. {
  435. if (--argc < 1) goto bad;
  436. app_verify_arg.proxy_cond= *(++argv);
  437. }
  438. else if (strcmp(*argv,"-v") == 0)
  439. verbose=1;
  440. else if (strcmp(*argv,"-d") == 0)
  441. debug=1;
  442. else if (strcmp(*argv,"-reuse") == 0)
  443. reuse=1;
  444. else if (strcmp(*argv,"-dhe1024") == 0)
  445. {
  446. #ifndef OPENSSL_NO_DH
  447. dhe1024=1;
  448. #else
  449. fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
  450. #endif
  451. }
  452. else if (strcmp(*argv,"-dhe1024dsa") == 0)
  453. {
  454. #ifndef OPENSSL_NO_DH
  455. dhe1024dsa=1;
  456. #else
  457. fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
  458. #endif
  459. }
  460. else if (strcmp(*argv,"-no_dhe") == 0)
  461. no_dhe=1;
  462. else if (strcmp(*argv,"-no_ecdhe") == 0)
  463. no_ecdhe=1;
  464. else if (strcmp(*argv,"-ssl2") == 0)
  465. ssl2=1;
  466. else if (strcmp(*argv,"-tls1") == 0)
  467. tls1=1;
  468. else if (strcmp(*argv,"-ssl3") == 0)
  469. ssl3=1;
  470. else if (strncmp(*argv,"-num",4) == 0)
  471. {
  472. if (--argc < 1) goto bad;
  473. number= atoi(*(++argv));
  474. if (number == 0) number=1;
  475. }
  476. else if (strcmp(*argv,"-bytes") == 0)
  477. {
  478. if (--argc < 1) goto bad;
  479. bytes= atol(*(++argv));
  480. if (bytes == 0L) bytes=1L;
  481. i=strlen(argv[0]);
  482. if (argv[0][i-1] == 'k') bytes*=1024L;
  483. if (argv[0][i-1] == 'm') bytes*=1024L*1024L;
  484. }
  485. else if (strcmp(*argv,"-cert") == 0)
  486. {
  487. if (--argc < 1) goto bad;
  488. server_cert= *(++argv);
  489. }
  490. else if (strcmp(*argv,"-s_cert") == 0)
  491. {
  492. if (--argc < 1) goto bad;
  493. server_cert= *(++argv);
  494. }
  495. else if (strcmp(*argv,"-key") == 0)
  496. {
  497. if (--argc < 1) goto bad;
  498. server_key= *(++argv);
  499. }
  500. else if (strcmp(*argv,"-s_key") == 0)
  501. {
  502. if (--argc < 1) goto bad;
  503. server_key= *(++argv);
  504. }
  505. else if (strcmp(*argv,"-c_cert") == 0)
  506. {
  507. if (--argc < 1) goto bad;
  508. client_cert= *(++argv);
  509. }
  510. else if (strcmp(*argv,"-c_key") == 0)
  511. {
  512. if (--argc < 1) goto bad;
  513. client_key= *(++argv);
  514. }
  515. else if (strcmp(*argv,"-cipher") == 0)
  516. {
  517. if (--argc < 1) goto bad;
  518. cipher= *(++argv);
  519. }
  520. else if (strcmp(*argv,"-CApath") == 0)
  521. {
  522. if (--argc < 1) goto bad;
  523. CApath= *(++argv);
  524. }
  525. else if (strcmp(*argv,"-CAfile") == 0)
  526. {
  527. if (--argc < 1) goto bad;
  528. CAfile= *(++argv);
  529. }
  530. else if (strcmp(*argv,"-bio_pair") == 0)
  531. {
  532. bio_pair = 1;
  533. }
  534. else if (strcmp(*argv,"-f") == 0)
  535. {
  536. force = 1;
  537. }
  538. else if (strcmp(*argv,"-time") == 0)
  539. {
  540. print_time = 1;
  541. }
  542. else if (strcmp(*argv,"-zlib") == 0)
  543. {
  544. comp = COMP_ZLIB;
  545. }
  546. else if (strcmp(*argv,"-rle") == 0)
  547. {
  548. comp = COMP_RLE;
  549. }
  550. else if (strcmp(*argv,"-named_curve") == 0)
  551. {
  552. if (--argc < 1) goto bad;
  553. #ifndef OPENSSL_NO_ECDH
  554. named_curve = *(++argv);
  555. #else
  556. fprintf(stderr,"ignoring -named_curve, since I'm compiled without ECDH\n");
  557. ++argv;
  558. #endif
  559. }
  560. else if (strcmp(*argv,"-app_verify") == 0)
  561. {
  562. app_verify_arg.app_verify = 1;
  563. }
  564. else if (strcmp(*argv,"-proxy") == 0)
  565. {
  566. app_verify_arg.allow_proxy_certs = 1;
  567. }
  568. else if (strcmp(*argv,"-test_cipherlist") == 0)
  569. {
  570. test_cipherlist = 1;
  571. }
  572. else
  573. {
  574. fprintf(stderr,"unknown option %s\n",*argv);
  575. badop=1;
  576. break;
  577. }
  578. argc--;
  579. argv++;
  580. }
  581. if (badop)
  582. {
  583. bad:
  584. sv_usage();
  585. goto end;
  586. }
  587. if (test_cipherlist == 1)
  588. {
  589. /* ensure that the cipher list are correctly sorted and exit */
  590. if (do_test_cipherlist() == 0)
  591. EXIT(1);
  592. ret = 0;
  593. goto end;
  594. }
  595. if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force)
  596. {
  597. fprintf(stderr, "This case cannot work. Use -f to perform "
  598. "the test anyway (and\n-d to see what happens), "
  599. "or add one of -ssl2, -ssl3, -tls1, -reuse\n"
  600. "to avoid protocol mismatch.\n");
  601. EXIT(1);
  602. }
  603. if (print_time)
  604. {
  605. if (!bio_pair)
  606. {
  607. fprintf(stderr, "Using BIO pair (-bio_pair)\n");
  608. bio_pair = 1;
  609. }
  610. if (number < 50 && !force)
  611. fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
  612. }
  613. /* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
  614. SSL_library_init();
  615. SSL_load_error_strings();
  616. #ifndef OPENSSL_NO_COMP
  617. if (comp == COMP_ZLIB) cm = COMP_zlib();
  618. if (comp == COMP_RLE) cm = COMP_rle();
  619. if (cm != NULL)
  620. {
  621. if (cm->type != NID_undef)
  622. {
  623. if (SSL_COMP_add_compression_method(comp, cm) != 0)
  624. {
  625. fprintf(stderr,
  626. "Failed to add compression method\n");
  627. ERR_print_errors_fp(stderr);
  628. }
  629. }
  630. else
  631. {
  632. fprintf(stderr,
  633. "Warning: %s compression not supported\n",
  634. (comp == COMP_RLE ? "rle" :
  635. (comp == COMP_ZLIB ? "zlib" :
  636. "unknown")));
  637. ERR_print_errors_fp(stderr);
  638. }
  639. }
  640. ssl_comp_methods = SSL_COMP_get_compression_methods();
  641. fprintf(stderr, "Available compression methods:\n");
  642. {
  643. int j, n = sk_SSL_COMP_num(ssl_comp_methods);
  644. if (n == 0)
  645. fprintf(stderr, " NONE\n");
  646. else
  647. for (j = 0; j < n; j++)
  648. {
  649. SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);
  650. fprintf(stderr, " %d: %s\n", c->id, c->name);
  651. }
  652. }
  653. #endif
  654. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  655. if (ssl2)
  656. meth=SSLv2_method();
  657. else
  658. if (tls1)
  659. meth=TLSv1_method();
  660. else
  661. if (ssl3)
  662. meth=SSLv3_method();
  663. else
  664. meth=SSLv23_method();
  665. #else
  666. #ifdef OPENSSL_NO_SSL2
  667. meth=SSLv3_method();
  668. #else
  669. meth=SSLv2_method();
  670. #endif
  671. #endif
  672. c_ctx=SSL_CTX_new(meth);
  673. s_ctx=SSL_CTX_new(meth);
  674. if ((c_ctx == NULL) || (s_ctx == NULL))
  675. {
  676. ERR_print_errors(bio_err);
  677. goto end;
  678. }
  679. if (cipher != NULL)
  680. {
  681. SSL_CTX_set_cipher_list(c_ctx,cipher);
  682. SSL_CTX_set_cipher_list(s_ctx,cipher);
  683. }
  684. #ifndef OPENSSL_NO_DH
  685. if (!no_dhe)
  686. {
  687. if (dhe1024dsa)
  688. {
  689. /* use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */
  690. SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);
  691. dh=get_dh1024dsa();
  692. }
  693. else if (dhe1024)
  694. dh=get_dh1024();
  695. else
  696. dh=get_dh512();
  697. SSL_CTX_set_tmp_dh(s_ctx,dh);
  698. DH_free(dh);
  699. }
  700. #else
  701. (void)no_dhe;
  702. #endif
  703. #ifndef OPENSSL_NO_ECDH
  704. if (!no_ecdhe)
  705. {
  706. int nid;
  707. if (named_curve != NULL)
  708. {
  709. nid = OBJ_sn2nid(named_curve);
  710. if (nid == 0)
  711. {
  712. BIO_printf(bio_err, "unknown curve name (%s)\n", named_curve);
  713. goto end;
  714. }
  715. }
  716. else
  717. nid = NID_sect163r2;
  718. ecdh = EC_KEY_new_by_curve_name(nid);
  719. if (ecdh == NULL)
  720. {
  721. BIO_printf(bio_err, "unable to create curve\n");
  722. goto end;
  723. }
  724. SSL_CTX_set_tmp_ecdh(s_ctx, ecdh);
  725. SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_ECDH_USE);
  726. EC_KEY_free(ecdh);
  727. }
  728. #else
  729. (void)no_ecdhe;
  730. #endif
  731. #ifndef OPENSSL_NO_RSA
  732. SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);
  733. #endif
  734. if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))
  735. {
  736. ERR_print_errors(bio_err);
  737. }
  738. else if (!SSL_CTX_use_PrivateKey_file(s_ctx,
  739. (server_key?server_key:server_cert), SSL_FILETYPE_PEM))
  740. {
  741. ERR_print_errors(bio_err);
  742. goto end;
  743. }
  744. if (client_auth)
  745. {
  746. SSL_CTX_use_certificate_file(c_ctx,client_cert,
  747. SSL_FILETYPE_PEM);
  748. SSL_CTX_use_PrivateKey_file(c_ctx,
  749. (client_key?client_key:client_cert),
  750. SSL_FILETYPE_PEM);
  751. }
  752. if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
  753. (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
  754. (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
  755. (!SSL_CTX_set_default_verify_paths(c_ctx)))
  756. {
  757. /* fprintf(stderr,"SSL_load_verify_locations\n"); */
  758. ERR_print_errors(bio_err);
  759. /* goto end; */
  760. }
  761. if (client_auth)
  762. {
  763. BIO_printf(bio_err,"client authentication\n");
  764. SSL_CTX_set_verify(s_ctx,
  765. SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  766. verify_callback);
  767. SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, &app_verify_arg);
  768. }
  769. if (server_auth)
  770. {
  771. BIO_printf(bio_err,"server authentication\n");
  772. SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
  773. verify_callback);
  774. SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback, &app_verify_arg);
  775. }
  776. {
  777. int session_id_context = 0;
  778. SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context);
  779. }
  780. c_ssl=SSL_new(c_ctx);
  781. s_ssl=SSL_new(s_ctx);
  782. #ifndef OPENSSL_NO_KRB5
  783. if (c_ssl && c_ssl->kssl_ctx)
  784. {
  785. char localhost[MAXHOSTNAMELEN+2];
  786. if (gethostname(localhost, sizeof localhost-1) == 0)
  787. {
  788. localhost[sizeof localhost-1]='\0';
  789. if(strlen(localhost) == sizeof localhost-1)
  790. {
  791. BIO_printf(bio_err,"localhost name too long\n");
  792. goto end;
  793. }
  794. kssl_ctx_setstring(c_ssl->kssl_ctx, KSSL_SERVER,
  795. localhost);
  796. }
  797. }
  798. #endif /* OPENSSL_NO_KRB5 */
  799. for (i=0; i<number; i++)
  800. {
  801. if (!reuse) SSL_set_session(c_ssl,NULL);
  802. if (bio_pair)
  803. ret=doit_biopair(s_ssl,c_ssl,bytes,&s_time,&c_time);
  804. else
  805. ret=doit(s_ssl,c_ssl,bytes);
  806. }
  807. if (!verbose)
  808. {
  809. print_details(c_ssl, "");
  810. }
  811. if ((number > 1) || (bytes > 1L))
  812. BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n",number,bytes);
  813. if (print_time)
  814. {
  815. #ifdef CLOCKS_PER_SEC
  816. /* "To determine the time in seconds, the value returned
  817. * by the clock function should be divided by the value
  818. * of the macro CLOCKS_PER_SEC."
  819. * -- ISO/IEC 9899 */
  820. BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
  821. "Approximate total client time: %6.2f s\n",
  822. (double)s_time/CLOCKS_PER_SEC,
  823. (double)c_time/CLOCKS_PER_SEC);
  824. #else
  825. /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
  826. * -- cc on NeXTstep/OpenStep */
  827. BIO_printf(bio_stdout,
  828. "Approximate total server time: %6.2f units\n"
  829. "Approximate total client time: %6.2f units\n",
  830. (double)s_time,
  831. (double)c_time);
  832. #endif
  833. }
  834. SSL_free(s_ssl);
  835. SSL_free(c_ssl);
  836. end:
  837. if (s_ctx != NULL) SSL_CTX_free(s_ctx);
  838. if (c_ctx != NULL) SSL_CTX_free(c_ctx);
  839. if (bio_stdout != NULL) BIO_free(bio_stdout);
  840. #ifndef OPENSSL_NO_RSA
  841. free_tmp_rsa();
  842. #endif
  843. #ifndef OPENSSL_NO_ENGINE
  844. ENGINE_cleanup();
  845. #endif
  846. CRYPTO_cleanup_all_ex_data();
  847. ERR_free_strings();
  848. ERR_remove_state(0);
  849. EVP_cleanup();
  850. CRYPTO_mem_leaks(bio_err);
  851. if (bio_err != NULL) BIO_free(bio_err);
  852. EXIT(ret);
  853. return ret;
  854. }
  855. int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
  856. clock_t *s_time, clock_t *c_time)
  857. {
  858. long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
  859. BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
  860. BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
  861. int ret = 1;
  862. size_t bufsiz = 256; /* small buffer for testing */
  863. if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
  864. goto err;
  865. if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
  866. goto err;
  867. s_ssl_bio = BIO_new(BIO_f_ssl());
  868. if (!s_ssl_bio)
  869. goto err;
  870. c_ssl_bio = BIO_new(BIO_f_ssl());
  871. if (!c_ssl_bio)
  872. goto err;
  873. SSL_set_connect_state(c_ssl);
  874. SSL_set_bio(c_ssl, client, client);
  875. (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
  876. SSL_set_accept_state(s_ssl);
  877. SSL_set_bio(s_ssl, server, server);
  878. (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
  879. do
  880. {
  881. /* c_ssl_bio: SSL filter BIO
  882. *
  883. * client: pseudo-I/O for SSL library
  884. *
  885. * client_io: client's SSL communication; usually to be
  886. * relayed over some I/O facility, but in this
  887. * test program, we're the server, too:
  888. *
  889. * server_io: server's SSL communication
  890. *
  891. * server: pseudo-I/O for SSL library
  892. *
  893. * s_ssl_bio: SSL filter BIO
  894. *
  895. * The client and the server each employ a "BIO pair":
  896. * client + client_io, server + server_io.
  897. * BIO pairs are symmetric. A BIO pair behaves similar
  898. * to a non-blocking socketpair (but both endpoints must
  899. * be handled by the same thread).
  900. * [Here we could connect client and server to the ends
  901. * of a single BIO pair, but then this code would be less
  902. * suitable as an example for BIO pairs in general.]
  903. *
  904. * Useful functions for querying the state of BIO pair endpoints:
  905. *
  906. * BIO_ctrl_pending(bio) number of bytes we can read now
  907. * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfil
  908. * other side's read attempt
  909. * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
  910. *
  911. * ..._read_request is never more than ..._write_guarantee;
  912. * it depends on the application which one you should use.
  913. */
  914. /* We have non-blocking behaviour throughout this test program, but
  915. * can be sure that there is *some* progress in each iteration; so
  916. * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE
  917. * -- we just try everything in each iteration
  918. */
  919. {
  920. /* CLIENT */
  921. MS_STATIC char cbuf[1024*8];
  922. int i, r;
  923. clock_t c_clock = clock();
  924. memset(cbuf, 0, sizeof(cbuf));
  925. if (debug)
  926. if (SSL_in_init(c_ssl))
  927. printf("client waiting in SSL_connect - %s\n",
  928. SSL_state_string_long(c_ssl));
  929. if (cw_num > 0)
  930. {
  931. /* Write to server. */
  932. if (cw_num > (long)sizeof cbuf)
  933. i = sizeof cbuf;
  934. else
  935. i = (int)cw_num;
  936. r = BIO_write(c_ssl_bio, cbuf, i);
  937. if (r < 0)
  938. {
  939. if (!BIO_should_retry(c_ssl_bio))
  940. {
  941. fprintf(stderr,"ERROR in CLIENT\n");
  942. goto err;
  943. }
  944. /* BIO_should_retry(...) can just be ignored here.
  945. * The library expects us to call BIO_write with
  946. * the same arguments again, and that's what we will
  947. * do in the next iteration. */
  948. }
  949. else if (r == 0)
  950. {
  951. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  952. goto err;
  953. }
  954. else
  955. {
  956. if (debug)
  957. printf("client wrote %d\n", r);
  958. cw_num -= r;
  959. }
  960. }
  961. if (cr_num > 0)
  962. {
  963. /* Read from server. */
  964. r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
  965. if (r < 0)
  966. {
  967. if (!BIO_should_retry(c_ssl_bio))
  968. {
  969. fprintf(stderr,"ERROR in CLIENT\n");
  970. goto err;
  971. }
  972. /* Again, "BIO_should_retry" can be ignored. */
  973. }
  974. else if (r == 0)
  975. {
  976. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  977. goto err;
  978. }
  979. else
  980. {
  981. if (debug)
  982. printf("client read %d\n", r);
  983. cr_num -= r;
  984. }
  985. }
  986. /* c_time and s_time increments will typically be very small
  987. * (depending on machine speed and clock tick intervals),
  988. * but sampling over a large number of connections should
  989. * result in fairly accurate figures. We cannot guarantee
  990. * a lot, however -- if each connection lasts for exactly
  991. * one clock tick, it will be counted only for the client
  992. * or only for the server or even not at all.
  993. */
  994. *c_time += (clock() - c_clock);
  995. }
  996. {
  997. /* SERVER */
  998. MS_STATIC char sbuf[1024*8];
  999. int i, r;
  1000. clock_t s_clock = clock();
  1001. memset(sbuf, 0, sizeof(sbuf));
  1002. if (debug)
  1003. if (SSL_in_init(s_ssl))
  1004. printf("server waiting in SSL_accept - %s\n",
  1005. SSL_state_string_long(s_ssl));
  1006. if (sw_num > 0)
  1007. {
  1008. /* Write to client. */
  1009. if (sw_num > (long)sizeof sbuf)
  1010. i = sizeof sbuf;
  1011. else
  1012. i = (int)sw_num;
  1013. r = BIO_write(s_ssl_bio, sbuf, i);
  1014. if (r < 0)
  1015. {
  1016. if (!BIO_should_retry(s_ssl_bio))
  1017. {
  1018. fprintf(stderr,"ERROR in SERVER\n");
  1019. goto err;
  1020. }
  1021. /* Ignore "BIO_should_retry". */
  1022. }
  1023. else if (r == 0)
  1024. {
  1025. fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
  1026. goto err;
  1027. }
  1028. else
  1029. {
  1030. if (debug)
  1031. printf("server wrote %d\n", r);
  1032. sw_num -= r;
  1033. }
  1034. }
  1035. if (sr_num > 0)
  1036. {
  1037. /* Read from client. */
  1038. r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
  1039. if (r < 0)
  1040. {
  1041. if (!BIO_should_retry(s_ssl_bio))
  1042. {
  1043. fprintf(stderr,"ERROR in SERVER\n");
  1044. goto err;
  1045. }
  1046. /* blah, blah */
  1047. }
  1048. else if (r == 0)
  1049. {
  1050. fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
  1051. goto err;
  1052. }
  1053. else
  1054. {
  1055. if (debug)
  1056. printf("server read %d\n", r);
  1057. sr_num -= r;
  1058. }
  1059. }
  1060. *s_time += (clock() - s_clock);
  1061. }
  1062. {
  1063. /* "I/O" BETWEEN CLIENT AND SERVER. */
  1064. size_t r1, r2;
  1065. BIO *io1 = server_io, *io2 = client_io;
  1066. /* we use the non-copying interface for io1
  1067. * and the standard BIO_write/BIO_read interface for io2
  1068. */
  1069. static int prev_progress = 1;
  1070. int progress = 0;
  1071. /* io1 to io2 */
  1072. do
  1073. {
  1074. size_t num;
  1075. int r;
  1076. r1 = BIO_ctrl_pending(io1);
  1077. r2 = BIO_ctrl_get_write_guarantee(io2);
  1078. num = r1;
  1079. if (r2 < num)
  1080. num = r2;
  1081. if (num)
  1082. {
  1083. char *dataptr;
  1084. if (INT_MAX < num) /* yeah, right */
  1085. num = INT_MAX;
  1086. r = BIO_nread(io1, &dataptr, (int)num);
  1087. assert(r > 0);
  1088. assert(r <= (int)num);
  1089. /* possibly r < num (non-contiguous data) */
  1090. num = r;
  1091. r = BIO_write(io2, dataptr, (int)num);
  1092. if (r != (int)num) /* can't happen */
  1093. {
  1094. fprintf(stderr, "ERROR: BIO_write could not write "
  1095. "BIO_ctrl_get_write_guarantee() bytes");
  1096. goto err;
  1097. }
  1098. progress = 1;
  1099. if (debug)
  1100. printf((io1 == client_io) ?
  1101. "C->S relaying: %d bytes\n" :
  1102. "S->C relaying: %d bytes\n",
  1103. (int)num);
  1104. }
  1105. }
  1106. while (r1 && r2);
  1107. /* io2 to io1 */
  1108. {
  1109. size_t num;
  1110. int r;
  1111. r1 = BIO_ctrl_pending(io2);
  1112. r2 = BIO_ctrl_get_read_request(io1);
  1113. /* here we could use ..._get_write_guarantee instead of
  1114. * ..._get_read_request, but by using the latter
  1115. * we test restartability of the SSL implementation
  1116. * more thoroughly */
  1117. num = r1;
  1118. if (r2 < num)
  1119. num = r2;
  1120. if (num)
  1121. {
  1122. char *dataptr;
  1123. if (INT_MAX < num)
  1124. num = INT_MAX;
  1125. if (num > 1)
  1126. --num; /* test restartability even more thoroughly */
  1127. r = BIO_nwrite0(io1, &dataptr);
  1128. assert(r > 0);
  1129. if (r < (int)num)
  1130. num = r;
  1131. r = BIO_read(io2, dataptr, (int)num);
  1132. if (r != (int)num) /* can't happen */
  1133. {
  1134. fprintf(stderr, "ERROR: BIO_read could not read "
  1135. "BIO_ctrl_pending() bytes");
  1136. goto err;
  1137. }
  1138. progress = 1;
  1139. r = BIO_nwrite(io1, &dataptr, (int)num);
  1140. if (r != (int)num) /* can't happen */
  1141. {
  1142. fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
  1143. "BIO_nwrite0() bytes");
  1144. goto err;
  1145. }
  1146. if (debug)
  1147. printf((io2 == client_io) ?
  1148. "C->S relaying: %d bytes\n" :
  1149. "S->C relaying: %d bytes\n",
  1150. (int)num);
  1151. }
  1152. } /* no loop, BIO_ctrl_get_read_request now returns 0 anyway */
  1153. if (!progress && !prev_progress)
  1154. if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0)
  1155. {
  1156. fprintf(stderr, "ERROR: got stuck\n");
  1157. if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0)
  1158. {
  1159. fprintf(stderr, "This can happen for SSL2 because "
  1160. "CLIENT-FINISHED and SERVER-VERIFY are written \n"
  1161. "concurrently ...");
  1162. if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0
  1163. && strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0)
  1164. {
  1165. fprintf(stderr, " ok.\n");
  1166. goto end;
  1167. }
  1168. }
  1169. fprintf(stderr, " ERROR.\n");
  1170. goto err;
  1171. }
  1172. prev_progress = progress;
  1173. }
  1174. }
  1175. while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
  1176. if (verbose)
  1177. print_details(c_ssl, "DONE via BIO pair: ");
  1178. end:
  1179. ret = 0;
  1180. err:
  1181. ERR_print_errors(bio_err);
  1182. if (server)
  1183. BIO_free(server);
  1184. if (server_io)
  1185. BIO_free(server_io);
  1186. if (client)
  1187. BIO_free(client);
  1188. if (client_io)
  1189. BIO_free(client_io);
  1190. if (s_ssl_bio)
  1191. BIO_free(s_ssl_bio);
  1192. if (c_ssl_bio)
  1193. BIO_free(c_ssl_bio);
  1194. return ret;
  1195. }
  1196. #define W_READ 1
  1197. #define W_WRITE 2
  1198. #define C_DONE 1
  1199. #define S_DONE 2
  1200. int doit(SSL *s_ssl, SSL *c_ssl, long count)
  1201. {
  1202. MS_STATIC char cbuf[1024*8],sbuf[1024*8];
  1203. long cw_num=count,cr_num=count;
  1204. long sw_num=count,sr_num=count;
  1205. int ret=1;
  1206. BIO *c_to_s=NULL;
  1207. BIO *s_to_c=NULL;
  1208. BIO *c_bio=NULL;
  1209. BIO *s_bio=NULL;
  1210. int c_r,c_w,s_r,s_w;
  1211. int c_want,s_want;
  1212. int i,j;
  1213. int done=0;
  1214. int c_write,s_write;
  1215. int do_server=0,do_client=0;
  1216. memset(cbuf,0,sizeof(cbuf));
  1217. memset(sbuf,0,sizeof(sbuf));
  1218. c_to_s=BIO_new(BIO_s_mem());
  1219. s_to_c=BIO_new(BIO_s_mem());
  1220. if ((s_to_c == NULL) || (c_to_s == NULL))
  1221. {
  1222. ERR_print_errors(bio_err);
  1223. goto err;
  1224. }
  1225. c_bio=BIO_new(BIO_f_ssl());
  1226. s_bio=BIO_new(BIO_f_ssl());
  1227. if ((c_bio == NULL) || (s_bio == NULL))
  1228. {
  1229. ERR_print_errors(bio_err);
  1230. goto err;
  1231. }
  1232. SSL_set_connect_state(c_ssl);
  1233. SSL_set_bio(c_ssl,s_to_c,c_to_s);
  1234. BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);
  1235. SSL_set_accept_state(s_ssl);
  1236. SSL_set_bio(s_ssl,c_to_s,s_to_c);
  1237. BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);
  1238. c_r=0; s_r=1;
  1239. c_w=1; s_w=0;
  1240. c_want=W_WRITE;
  1241. s_want=0;
  1242. c_write=1,s_write=0;
  1243. /* We can always do writes */
  1244. for (;;)
  1245. {
  1246. do_server=0;
  1247. do_client=0;
  1248. i=(int)BIO_pending(s_bio);
  1249. if ((i && s_r) || s_w) do_server=1;
  1250. i=(int)BIO_pending(c_bio);
  1251. if ((i && c_r) || c_w) do_client=1;
  1252. if (do_server && debug)
  1253. {
  1254. if (SSL_in_init(s_ssl))
  1255. printf("server waiting in SSL_accept - %s\n",
  1256. SSL_state_string_long(s_ssl));
  1257. /* else if (s_write)
  1258. printf("server:SSL_write()\n");
  1259. else
  1260. printf("server:SSL_read()\n"); */
  1261. }
  1262. if (do_client && debug)
  1263. {
  1264. if (SSL_in_init(c_ssl))
  1265. printf("client waiting in SSL_connect - %s\n",
  1266. SSL_state_string_long(c_ssl));
  1267. /* else if (c_write)
  1268. printf("client:SSL_write()\n");
  1269. else
  1270. printf("client:SSL_read()\n"); */
  1271. }
  1272. if (!do_client && !do_server)
  1273. {
  1274. fprintf(stdout,"ERROR IN STARTUP\n");
  1275. ERR_print_errors(bio_err);
  1276. break;
  1277. }
  1278. if (do_client && !(done & C_DONE))
  1279. {
  1280. if (c_write)
  1281. {
  1282. j = (cw_num > (long)sizeof(cbuf)) ?
  1283. (int)sizeof(cbuf) : (int)cw_num;
  1284. i=BIO_write(c_bio,cbuf,j);
  1285. if (i < 0)
  1286. {
  1287. c_r=0;
  1288. c_w=0;
  1289. if (BIO_should_retry(c_bio))
  1290. {
  1291. if (BIO_should_read(c_bio))
  1292. c_r=1;
  1293. if (BIO_should_write(c_bio))
  1294. c_w=1;
  1295. }
  1296. else
  1297. {
  1298. fprintf(stderr,"ERROR in CLIENT\n");
  1299. ERR_print_errors(bio_err);
  1300. goto err;
  1301. }
  1302. }
  1303. else if (i == 0)
  1304. {
  1305. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1306. goto err;
  1307. }
  1308. else
  1309. {
  1310. if (debug)
  1311. printf("client wrote %d\n",i);
  1312. /* ok */
  1313. s_r=1;
  1314. c_write=0;
  1315. cw_num-=i;
  1316. }
  1317. }
  1318. else
  1319. {
  1320. i=BIO_read(c_bio,cbuf,sizeof(cbuf));
  1321. if (i < 0)
  1322. {
  1323. c_r=0;
  1324. c_w=0;
  1325. if (BIO_should_retry(c_bio))
  1326. {
  1327. if (BIO_should_read(c_bio))
  1328. c_r=1;
  1329. if (BIO_should_write(c_bio))
  1330. c_w=1;
  1331. }
  1332. else
  1333. {
  1334. fprintf(stderr,"ERROR in CLIENT\n");
  1335. ERR_print_errors(bio_err);
  1336. goto err;
  1337. }
  1338. }
  1339. else if (i == 0)
  1340. {
  1341. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1342. goto err;
  1343. }
  1344. else
  1345. {
  1346. if (debug)
  1347. printf("client read %d\n",i);
  1348. cr_num-=i;
  1349. if (sw_num > 0)
  1350. {
  1351. s_write=1;
  1352. s_w=1;
  1353. }
  1354. if (cr_num <= 0)
  1355. {
  1356. s_write=1;
  1357. s_w=1;
  1358. done=S_DONE|C_DONE;
  1359. }
  1360. }
  1361. }
  1362. }
  1363. if (do_server && !(done & S_DONE))
  1364. {
  1365. if (!s_write)
  1366. {
  1367. i=BIO_read(s_bio,sbuf,sizeof(cbuf));
  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_read\n");
  1390. goto err;
  1391. }
  1392. else
  1393. {
  1394. if (debug)
  1395. printf("server read %d\n",i);
  1396. sr_num-=i;
  1397. if (cw_num > 0)
  1398. {
  1399. c_write=1;
  1400. c_w=1;
  1401. }
  1402. if (sr_num <= 0)
  1403. {
  1404. s_write=1;
  1405. s_w=1;
  1406. c_write=0;
  1407. }
  1408. }
  1409. }
  1410. else
  1411. {
  1412. j = (sw_num > (long)sizeof(sbuf)) ?
  1413. (int)sizeof(sbuf) : (int)sw_num;
  1414. i=BIO_write(s_bio,sbuf,j);
  1415. if (i < 0)
  1416. {
  1417. s_r=0;
  1418. s_w=0;
  1419. if (BIO_should_retry(s_bio))
  1420. {
  1421. if (BIO_should_read(s_bio))
  1422. s_r=1;
  1423. if (BIO_should_write(s_bio))
  1424. s_w=1;
  1425. }
  1426. else
  1427. {
  1428. fprintf(stderr,"ERROR in SERVER\n");
  1429. ERR_print_errors(bio_err);
  1430. goto err;
  1431. }
  1432. }
  1433. else if (i == 0)
  1434. {
  1435. ERR_print_errors(bio_err);
  1436. fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
  1437. goto err;
  1438. }
  1439. else
  1440. {
  1441. if (debug)
  1442. printf("server wrote %d\n",i);
  1443. sw_num-=i;
  1444. s_write=0;
  1445. c_r=1;
  1446. if (sw_num <= 0)
  1447. done|=S_DONE;
  1448. }
  1449. }
  1450. }
  1451. if ((done & S_DONE) && (done & C_DONE)) break;
  1452. }
  1453. if (verbose)
  1454. print_details(c_ssl, "DONE: ");
  1455. ret=0;
  1456. err:
  1457. /* We have to set the BIO's to NULL otherwise they will be
  1458. * OPENSSL_free()ed twice. Once when th s_ssl is SSL_free()ed and
  1459. * again when c_ssl is SSL_free()ed.
  1460. * This is a hack required because s_ssl and c_ssl are sharing the same
  1461. * BIO structure and SSL_set_bio() and SSL_free() automatically
  1462. * BIO_free non NULL entries.
  1463. * You should not normally do this or be required to do this */
  1464. if (s_ssl != NULL)
  1465. {
  1466. s_ssl->rbio=NULL;
  1467. s_ssl->wbio=NULL;
  1468. }
  1469. if (c_ssl != NULL)
  1470. {
  1471. c_ssl->rbio=NULL;
  1472. c_ssl->wbio=NULL;
  1473. }
  1474. if (c_to_s != NULL) BIO_free(c_to_s);
  1475. if (s_to_c != NULL) BIO_free(s_to_c);
  1476. if (c_bio != NULL) BIO_free_all(c_bio);
  1477. if (s_bio != NULL) BIO_free_all(s_bio);
  1478. return(ret);
  1479. }
  1480. static int get_proxy_auth_ex_data_idx(void)
  1481. {
  1482. static volatile int idx = -1;
  1483. if (idx < 0)
  1484. {
  1485. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  1486. if (idx < 0)
  1487. {
  1488. idx = X509_STORE_CTX_get_ex_new_index(0,
  1489. "SSLtest for verify callback", NULL,NULL,NULL);
  1490. }
  1491. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  1492. }
  1493. return idx;
  1494. }
  1495. static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
  1496. {
  1497. char *s,buf[256];
  1498. s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,
  1499. sizeof buf);
  1500. if (s != NULL)
  1501. {
  1502. if (ok)
  1503. fprintf(stderr,"depth=%d %s\n",
  1504. ctx->error_depth,buf);
  1505. else
  1506. {
  1507. fprintf(stderr,"depth=%d error=%d %s\n",
  1508. ctx->error_depth,ctx->error,buf);
  1509. }
  1510. }
  1511. if (ok == 0)
  1512. {
  1513. fprintf(stderr,"Error string: %s\n",
  1514. X509_verify_cert_error_string(ctx->error));
  1515. switch (ctx->error)
  1516. {
  1517. case X509_V_ERR_CERT_NOT_YET_VALID:
  1518. case X509_V_ERR_CERT_HAS_EXPIRED:
  1519. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  1520. fprintf(stderr," ... ignored.\n");
  1521. ok=1;
  1522. }
  1523. }
  1524. if (ok == 1)
  1525. {
  1526. X509 *xs = ctx->current_cert;
  1527. #if 0
  1528. X509 *xi = ctx->current_issuer;
  1529. #endif
  1530. if (xs->ex_flags & EXFLAG_PROXY)
  1531. {
  1532. unsigned int *letters =
  1533. X509_STORE_CTX_get_ex_data(ctx,
  1534. get_proxy_auth_ex_data_idx());
  1535. if (letters)
  1536. {
  1537. int found_any = 0;
  1538. int i;
  1539. PROXY_CERT_INFO_EXTENSION *pci =
  1540. X509_get_ext_d2i(xs, NID_proxyCertInfo,
  1541. NULL, NULL);
  1542. switch (OBJ_obj2nid(pci->proxyPolicy->policyLanguage))
  1543. {
  1544. case NID_Independent:
  1545. /* Completely meaningless in this
  1546. program, as there's no way to
  1547. grant explicit rights to a
  1548. specific PrC. Basically, using
  1549. id-ppl-Independent is the perfect
  1550. way to grant no rights at all. */
  1551. fprintf(stderr, " Independent proxy certificate");
  1552. for (i = 0; i < 26; i++)
  1553. letters[i] = 0;
  1554. break;
  1555. case NID_id_ppl_inheritAll:
  1556. /* This is basically a NOP, we
  1557. simply let the current rights
  1558. stand as they are. */
  1559. fprintf(stderr, " Proxy certificate inherits all");
  1560. break;
  1561. default:
  1562. s = (char *)
  1563. pci->proxyPolicy->policy->data;
  1564. i = pci->proxyPolicy->policy->length;
  1565. /* The algorithm works as follows:
  1566. it is assumed that previous
  1567. iterations or the initial granted
  1568. rights has already set some elements
  1569. of `letters'. What we need to do is
  1570. to clear those that weren't granted
  1571. by the current PrC as well. The
  1572. easiest way to do this is to add 1
  1573. to all the elements whose letters
  1574. are given with the current policy.
  1575. That way, all elements that are set
  1576. by the current policy and were
  1577. already set by earlier policies and
  1578. through the original grant of rights
  1579. will get the value 2 or higher.
  1580. The last thing to do is to sweep
  1581. through `letters' and keep the
  1582. elements having the value 2 as set,
  1583. and clear all the others. */
  1584. fprintf(stderr, " Certificate proxy rights = %*.*s", i, i, s);
  1585. while(i-- > 0)
  1586. {
  1587. int c = *s++;
  1588. if (isascii(c) && isalpha(c))
  1589. {
  1590. if (islower(c))
  1591. c = toupper(c);
  1592. letters[c - 'A']++;
  1593. }
  1594. }
  1595. for (i = 0; i < 26; i++)
  1596. if (letters[i] < 2)
  1597. letters[i] = 0;
  1598. else
  1599. letters[i] = 1;
  1600. }
  1601. found_any = 0;
  1602. fprintf(stderr,
  1603. ", resulting proxy rights = ");
  1604. for(i = 0; i < 26; i++)
  1605. if (letters[i])
  1606. {
  1607. fprintf(stderr, "%c", i + 'A');
  1608. found_any = 1;
  1609. }
  1610. if (!found_any)
  1611. fprintf(stderr, "none");
  1612. fprintf(stderr, "\n");
  1613. PROXY_CERT_INFO_EXTENSION_free(pci);
  1614. }
  1615. }
  1616. }
  1617. return(ok);
  1618. }
  1619. static void process_proxy_debug(int indent, const char *format, ...)
  1620. {
  1621. static const char indentation[] =
  1622. ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
  1623. ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"; /* That's 80 > */
  1624. char my_format[256];
  1625. va_list args;
  1626. BIO_snprintf(my_format, sizeof(my_format), "%*.*s %s",
  1627. indent, indent, indentation, format);
  1628. va_start(args, format);
  1629. vfprintf(stderr, my_format, args);
  1630. va_end(args);
  1631. }
  1632. /* Priority levels:
  1633. 0 [!]var, ()
  1634. 1 & ^
  1635. 2 |
  1636. */
  1637. static int process_proxy_cond_adders(unsigned int letters[26],
  1638. const char *cond, const char **cond_end, int *pos, int indent);
  1639. static int process_proxy_cond_val(unsigned int letters[26],
  1640. const char *cond, const char **cond_end, int *pos, int indent)
  1641. {
  1642. int c;
  1643. int ok = 1;
  1644. int negate = 0;
  1645. while(isspace((int)*cond))
  1646. {
  1647. cond++; (*pos)++;
  1648. }
  1649. c = *cond;
  1650. if (debug)
  1651. process_proxy_debug(indent,
  1652. "Start process_proxy_cond_val at position %d: %s\n",
  1653. *pos, cond);
  1654. while(c == '!')
  1655. {
  1656. negate = !negate;
  1657. cond++; (*pos)++;
  1658. while(isspace((int)*cond))
  1659. {
  1660. cond++; (*pos)++;
  1661. }
  1662. c = *cond;
  1663. }
  1664. if (c == '(')
  1665. {
  1666. cond++; (*pos)++;
  1667. ok = process_proxy_cond_adders(letters, cond, cond_end, pos,
  1668. indent + 1);
  1669. cond = *cond_end;
  1670. if (ok < 0)
  1671. goto end;
  1672. while(isspace((int)*cond))
  1673. {
  1674. cond++; (*pos)++;
  1675. }
  1676. c = *cond;
  1677. if (c != ')')
  1678. {
  1679. fprintf(stderr,
  1680. "Weird condition character in position %d: "
  1681. "%c\n", *pos, c);
  1682. ok = -1;
  1683. goto end;
  1684. }
  1685. cond++; (*pos)++;
  1686. }
  1687. else if (isascii(c) && isalpha(c))
  1688. {
  1689. if (islower(c))
  1690. c = toupper(c);
  1691. ok = letters[c - 'A'];
  1692. cond++; (*pos)++;
  1693. }
  1694. else
  1695. {
  1696. fprintf(stderr,
  1697. "Weird condition character in position %d: "
  1698. "%c\n", *pos, c);
  1699. ok = -1;
  1700. goto end;
  1701. }
  1702. end:
  1703. *cond_end = cond;
  1704. if (ok >= 0 && negate)
  1705. ok = !ok;
  1706. if (debug)
  1707. process_proxy_debug(indent,
  1708. "End process_proxy_cond_val at position %d: %s, returning %d\n",
  1709. *pos, cond, ok);
  1710. return ok;
  1711. }
  1712. static int process_proxy_cond_multipliers(unsigned int letters[26],
  1713. const char *cond, const char **cond_end, int *pos, int indent)
  1714. {
  1715. int ok;
  1716. char c;
  1717. if (debug)
  1718. process_proxy_debug(indent,
  1719. "Start process_proxy_cond_multipliers at position %d: %s\n",
  1720. *pos, cond);
  1721. ok = process_proxy_cond_val(letters, cond, cond_end, pos, indent + 1);
  1722. cond = *cond_end;
  1723. if (ok < 0)
  1724. goto end;
  1725. while(ok >= 0)
  1726. {
  1727. while(isspace((int)*cond))
  1728. {
  1729. cond++; (*pos)++;
  1730. }
  1731. c = *cond;
  1732. switch(c)
  1733. {
  1734. case '&':
  1735. case '^':
  1736. {
  1737. int save_ok = ok;
  1738. cond++; (*pos)++;
  1739. ok = process_proxy_cond_val(letters,
  1740. cond, cond_end, pos, indent + 1);
  1741. cond = *cond_end;
  1742. if (ok < 0)
  1743. break;
  1744. switch(c)
  1745. {
  1746. case '&':
  1747. ok &= save_ok;
  1748. break;
  1749. case '^':
  1750. ok ^= save_ok;
  1751. break;
  1752. default:
  1753. fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!"
  1754. " STOPPING\n");
  1755. EXIT(1);
  1756. }
  1757. }
  1758. break;
  1759. default:
  1760. goto end;
  1761. }
  1762. }
  1763. end:
  1764. if (debug)
  1765. process_proxy_debug(indent,
  1766. "End process_proxy_cond_multipliers at position %d: %s, returning %d\n",
  1767. *pos, cond, ok);
  1768. *cond_end = cond;
  1769. return ok;
  1770. }
  1771. static int process_proxy_cond_adders(unsigned int letters[26],
  1772. const char *cond, const char **cond_end, int *pos, int indent)
  1773. {
  1774. int ok;
  1775. char c;
  1776. if (debug)
  1777. process_proxy_debug(indent,
  1778. "Start process_proxy_cond_adders at position %d: %s\n",
  1779. *pos, cond);
  1780. ok = process_proxy_cond_multipliers(letters, cond, cond_end, pos,
  1781. indent + 1);
  1782. cond = *cond_end;
  1783. if (ok < 0)
  1784. goto end;
  1785. while(ok >= 0)
  1786. {
  1787. while(isspace((int)*cond))
  1788. {
  1789. cond++; (*pos)++;
  1790. }
  1791. c = *cond;
  1792. switch(c)
  1793. {
  1794. case '|':
  1795. {
  1796. int save_ok = ok;
  1797. cond++; (*pos)++;
  1798. ok = process_proxy_cond_multipliers(letters,
  1799. cond, cond_end, pos, indent + 1);
  1800. cond = *cond_end;
  1801. if (ok < 0)
  1802. break;
  1803. switch(c)
  1804. {
  1805. case '|':
  1806. ok |= save_ok;
  1807. break;
  1808. default:
  1809. fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!"
  1810. " STOPPING\n");
  1811. EXIT(1);
  1812. }
  1813. }
  1814. break;
  1815. default:
  1816. goto end;
  1817. }
  1818. }
  1819. end:
  1820. if (debug)
  1821. process_proxy_debug(indent,
  1822. "End process_proxy_cond_adders at position %d: %s, returning %d\n",
  1823. *pos, cond, ok);
  1824. *cond_end = cond;
  1825. return ok;
  1826. }
  1827. static int process_proxy_cond(unsigned int letters[26],
  1828. const char *cond, const char **cond_end)
  1829. {
  1830. int pos = 1;
  1831. return process_proxy_cond_adders(letters, cond, cond_end, &pos, 1);
  1832. }
  1833. static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg)
  1834. {
  1835. int ok=1;
  1836. struct app_verify_arg *cb_arg = arg;
  1837. unsigned int letters[26]; /* only used with proxy_auth */
  1838. if (cb_arg->app_verify)
  1839. {
  1840. char *s = NULL,buf[256];
  1841. fprintf(stderr, "In app_verify_callback, allowing cert. ");
  1842. fprintf(stderr, "Arg is: %s\n", cb_arg->string);
  1843. fprintf(stderr, "Finished printing do we have a context? 0x%p a cert? 0x%p\n",
  1844. (void *)ctx, (void *)ctx->cert);
  1845. if (ctx->cert)
  1846. s=X509_NAME_oneline(X509_get_subject_name(ctx->cert),buf,256);
  1847. if (s != NULL)
  1848. {
  1849. fprintf(stderr,"cert depth=%d %s\n",ctx->error_depth,buf);
  1850. }
  1851. return(1);
  1852. }
  1853. if (cb_arg->proxy_auth)
  1854. {
  1855. int found_any = 0, i;
  1856. char *sp;
  1857. for(i = 0; i < 26; i++)
  1858. letters[i] = 0;
  1859. for(sp = cb_arg->proxy_auth; *sp; sp++)
  1860. {
  1861. int c = *sp;
  1862. if (isascii(c) && isalpha(c))
  1863. {
  1864. if (islower(c))
  1865. c = toupper(c);
  1866. letters[c - 'A'] = 1;
  1867. }
  1868. }
  1869. fprintf(stderr,
  1870. " Initial proxy rights = ");
  1871. for(i = 0; i < 26; i++)
  1872. if (letters[i])
  1873. {
  1874. fprintf(stderr, "%c", i + 'A');
  1875. found_any = 1;
  1876. }
  1877. if (!found_any)
  1878. fprintf(stderr, "none");
  1879. fprintf(stderr, "\n");
  1880. X509_STORE_CTX_set_ex_data(ctx,
  1881. get_proxy_auth_ex_data_idx(),letters);
  1882. }
  1883. if (cb_arg->allow_proxy_certs)
  1884. {
  1885. X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS);
  1886. }
  1887. #ifndef OPENSSL_NO_X509_VERIFY
  1888. # ifdef OPENSSL_FIPS
  1889. if(s->version == TLS1_VERSION)
  1890. FIPS_allow_md5(1);
  1891. # endif
  1892. ok = X509_verify_cert(ctx);
  1893. # ifdef OPENSSL_FIPS
  1894. if(s->version == TLS1_VERSION)
  1895. FIPS_allow_md5(0);
  1896. # endif
  1897. #endif
  1898. if (cb_arg->proxy_auth)
  1899. {
  1900. if (ok)
  1901. {
  1902. const char *cond_end = NULL;
  1903. ok = process_proxy_cond(letters,
  1904. cb_arg->proxy_cond, &cond_end);
  1905. if (ok < 0)
  1906. EXIT(3);
  1907. if (*cond_end)
  1908. {
  1909. fprintf(stderr, "Stopped processing condition before it's end.\n");
  1910. ok = 0;
  1911. }
  1912. if (!ok)
  1913. fprintf(stderr, "Proxy rights check with condition '%s' proved invalid\n",
  1914. cb_arg->proxy_cond);
  1915. else
  1916. fprintf(stderr, "Proxy rights check with condition '%s' proved valid\n",
  1917. cb_arg->proxy_cond);
  1918. }
  1919. }
  1920. return(ok);
  1921. }
  1922. #ifndef OPENSSL_NO_RSA
  1923. static RSA *rsa_tmp=NULL;
  1924. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  1925. {
  1926. BIGNUM *bn = NULL;
  1927. if (rsa_tmp == NULL)
  1928. {
  1929. bn = BN_new();
  1930. rsa_tmp = RSA_new();
  1931. if(!bn || !rsa_tmp || !BN_set_word(bn, RSA_F4))
  1932. {
  1933. BIO_printf(bio_err, "Memory error...");
  1934. goto end;
  1935. }
  1936. BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  1937. (void)BIO_flush(bio_err);
  1938. if(!RSA_generate_key_ex(rsa_tmp,keylength,bn,NULL))
  1939. {
  1940. BIO_printf(bio_err, "Error generating key.");
  1941. RSA_free(rsa_tmp);
  1942. rsa_tmp = NULL;
  1943. }
  1944. end:
  1945. BIO_printf(bio_err,"\n");
  1946. (void)BIO_flush(bio_err);
  1947. }
  1948. if(bn) BN_free(bn);
  1949. return(rsa_tmp);
  1950. }
  1951. static void free_tmp_rsa(void)
  1952. {
  1953. if (rsa_tmp != NULL)
  1954. {
  1955. RSA_free(rsa_tmp);
  1956. rsa_tmp = NULL;
  1957. }
  1958. }
  1959. #endif
  1960. #ifndef OPENSSL_NO_DH
  1961. /* These DH parameters have been generated as follows:
  1962. * $ openssl dhparam -C -noout 512
  1963. * $ openssl dhparam -C -noout 1024
  1964. * $ openssl dhparam -C -noout -dsaparam 1024
  1965. * (The third function has been renamed to avoid name conflicts.)
  1966. */
  1967. static DH *get_dh512()
  1968. {
  1969. static unsigned char dh512_p[]={
  1970. 0xCB,0xC8,0xE1,0x86,0xD0,0x1F,0x94,0x17,0xA6,0x99,0xF0,0xC6,
  1971. 0x1F,0x0D,0xAC,0xB6,0x25,0x3E,0x06,0x39,0xCA,0x72,0x04,0xB0,
  1972. 0x6E,0xDA,0xC0,0x61,0xE6,0x7A,0x77,0x25,0xE8,0x3B,0xB9,0x5F,
  1973. 0x9A,0xB6,0xB5,0xFE,0x99,0x0B,0xA1,0x93,0x4E,0x35,0x33,0xB8,
  1974. 0xE1,0xF1,0x13,0x4F,0x59,0x1A,0xD2,0x57,0xC0,0x26,0x21,0x33,
  1975. 0x02,0xC5,0xAE,0x23,
  1976. };
  1977. static unsigned char dh512_g[]={
  1978. 0x02,
  1979. };
  1980. DH *dh;
  1981. if ((dh=DH_new()) == NULL) return(NULL);
  1982. dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  1983. dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  1984. if ((dh->p == NULL) || (dh->g == NULL))
  1985. { DH_free(dh); return(NULL); }
  1986. return(dh);
  1987. }
  1988. static DH *get_dh1024()
  1989. {
  1990. static unsigned char dh1024_p[]={
  1991. 0xF8,0x81,0x89,0x7D,0x14,0x24,0xC5,0xD1,0xE6,0xF7,0xBF,0x3A,
  1992. 0xE4,0x90,0xF4,0xFC,0x73,0xFB,0x34,0xB5,0xFA,0x4C,0x56,0xA2,
  1993. 0xEA,0xA7,0xE9,0xC0,0xC0,0xCE,0x89,0xE1,0xFA,0x63,0x3F,0xB0,
  1994. 0x6B,0x32,0x66,0xF1,0xD1,0x7B,0xB0,0x00,0x8F,0xCA,0x87,0xC2,
  1995. 0xAE,0x98,0x89,0x26,0x17,0xC2,0x05,0xD2,0xEC,0x08,0xD0,0x8C,
  1996. 0xFF,0x17,0x52,0x8C,0xC5,0x07,0x93,0x03,0xB1,0xF6,0x2F,0xB8,
  1997. 0x1C,0x52,0x47,0x27,0x1B,0xDB,0xD1,0x8D,0x9D,0x69,0x1D,0x52,
  1998. 0x4B,0x32,0x81,0xAA,0x7F,0x00,0xC8,0xDC,0xE6,0xD9,0xCC,0xC1,
  1999. 0x11,0x2D,0x37,0x34,0x6C,0xEA,0x02,0x97,0x4B,0x0E,0xBB,0xB1,
  2000. 0x71,0x33,0x09,0x15,0xFD,0xDD,0x23,0x87,0x07,0x5E,0x89,0xAB,
  2001. 0x6B,0x7C,0x5F,0xEC,0xA6,0x24,0xDC,0x53,
  2002. };
  2003. static unsigned char dh1024_g[]={
  2004. 0x02,
  2005. };
  2006. DH *dh;
  2007. if ((dh=DH_new()) == NULL) return(NULL);
  2008. dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
  2009. dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
  2010. if ((dh->p == NULL) || (dh->g == NULL))
  2011. { DH_free(dh); return(NULL); }
  2012. return(dh);
  2013. }
  2014. static DH *get_dh1024dsa()
  2015. {
  2016. static unsigned char dh1024_p[]={
  2017. 0xC8,0x00,0xF7,0x08,0x07,0x89,0x4D,0x90,0x53,0xF3,0xD5,0x00,
  2018. 0x21,0x1B,0xF7,0x31,0xA6,0xA2,0xDA,0x23,0x9A,0xC7,0x87,0x19,
  2019. 0x3B,0x47,0xB6,0x8C,0x04,0x6F,0xFF,0xC6,0x9B,0xB8,0x65,0xD2,
  2020. 0xC2,0x5F,0x31,0x83,0x4A,0xA7,0x5F,0x2F,0x88,0x38,0xB6,0x55,
  2021. 0xCF,0xD9,0x87,0x6D,0x6F,0x9F,0xDA,0xAC,0xA6,0x48,0xAF,0xFC,
  2022. 0x33,0x84,0x37,0x5B,0x82,0x4A,0x31,0x5D,0xE7,0xBD,0x52,0x97,
  2023. 0xA1,0x77,0xBF,0x10,0x9E,0x37,0xEA,0x64,0xFA,0xCA,0x28,0x8D,
  2024. 0x9D,0x3B,0xD2,0x6E,0x09,0x5C,0x68,0xC7,0x45,0x90,0xFD,0xBB,
  2025. 0x70,0xC9,0x3A,0xBB,0xDF,0xD4,0x21,0x0F,0xC4,0x6A,0x3C,0xF6,
  2026. 0x61,0xCF,0x3F,0xD6,0x13,0xF1,0x5F,0xBC,0xCF,0xBC,0x26,0x9E,
  2027. 0xBC,0x0B,0xBD,0xAB,0x5D,0xC9,0x54,0x39,
  2028. };
  2029. static unsigned char dh1024_g[]={
  2030. 0x3B,0x40,0x86,0xE7,0xF3,0x6C,0xDE,0x67,0x1C,0xCC,0x80,0x05,
  2031. 0x5A,0xDF,0xFE,0xBD,0x20,0x27,0x74,0x6C,0x24,0xC9,0x03,0xF3,
  2032. 0xE1,0x8D,0xC3,0x7D,0x98,0x27,0x40,0x08,0xB8,0x8C,0x6A,0xE9,
  2033. 0xBB,0x1A,0x3A,0xD6,0x86,0x83,0x5E,0x72,0x41,0xCE,0x85,0x3C,
  2034. 0xD2,0xB3,0xFC,0x13,0xCE,0x37,0x81,0x9E,0x4C,0x1C,0x7B,0x65,
  2035. 0xD3,0xE6,0xA6,0x00,0xF5,0x5A,0x95,0x43,0x5E,0x81,0xCF,0x60,
  2036. 0xA2,0x23,0xFC,0x36,0xA7,0x5D,0x7A,0x4C,0x06,0x91,0x6E,0xF6,
  2037. 0x57,0xEE,0x36,0xCB,0x06,0xEA,0xF5,0x3D,0x95,0x49,0xCB,0xA7,
  2038. 0xDD,0x81,0xDF,0x80,0x09,0x4A,0x97,0x4D,0xA8,0x22,0x72,0xA1,
  2039. 0x7F,0xC4,0x70,0x56,0x70,0xE8,0x20,0x10,0x18,0x8F,0x2E,0x60,
  2040. 0x07,0xE7,0x68,0x1A,0x82,0x5D,0x32,0xA2,
  2041. };
  2042. DH *dh;
  2043. if ((dh=DH_new()) == NULL) return(NULL);
  2044. dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
  2045. dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
  2046. if ((dh->p == NULL) || (dh->g == NULL))
  2047. { DH_free(dh); return(NULL); }
  2048. dh->length = 160;
  2049. return(dh);
  2050. }
  2051. #endif
  2052. static int do_test_cipherlist(void)
  2053. {
  2054. int i = 0;
  2055. const SSL_METHOD *meth;
  2056. SSL_CIPHER *ci, *tci = NULL;
  2057. #ifndef OPENSSL_NO_SSL2
  2058. fprintf(stderr, "testing SSLv2 cipher list order: ");
  2059. meth = SSLv2_method();
  2060. while ((ci = meth->get_cipher(i++)) != NULL)
  2061. {
  2062. if (tci != NULL)
  2063. if (ci->id >= tci->id)
  2064. {
  2065. fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id);
  2066. return 0;
  2067. }
  2068. tci = ci;
  2069. }
  2070. fprintf(stderr, "ok\n");
  2071. #endif
  2072. #ifndef OPENSSL_NO_SSL3
  2073. fprintf(stderr, "testing SSLv3 cipher list order: ");
  2074. meth = SSLv3_method();
  2075. tci = NULL;
  2076. while ((ci = meth->get_cipher(i++)) != NULL)
  2077. {
  2078. if (tci != NULL)
  2079. if (ci->id >= tci->id)
  2080. {
  2081. fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id);
  2082. return 0;
  2083. }
  2084. tci = ci;
  2085. }
  2086. fprintf(stderr, "ok\n");
  2087. #endif
  2088. #ifndef OPENSSL_NO_TLS1
  2089. fprintf(stderr, "testing TLSv1 cipher list order: ");
  2090. meth = TLSv1_method();
  2091. tci = NULL;
  2092. while ((ci = meth->get_cipher(i++)) != NULL)
  2093. {
  2094. if (tci != NULL)
  2095. if (ci->id >= tci->id)
  2096. {
  2097. fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id);
  2098. return 0;
  2099. }
  2100. tci = ci;
  2101. }
  2102. fprintf(stderr, "ok\n");
  2103. #endif
  2104. return 1;
  2105. }