ssltest.c 60 KB

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