ssltest.c 81 KB

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