ssltest.c 60 KB

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