ssltest.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463
  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. #define _XOPEN_SOURCE 500 /* Or isascii won't be declared properly on
  145. VMS (at least with DECompHP C). */
  146. #include <assert.h>
  147. #include <errno.h>
  148. #include <limits.h>
  149. #include <stdio.h>
  150. #include <stdlib.h>
  151. #include <string.h>
  152. #include <time.h>
  153. #define USE_SOCKETS
  154. #include "e_os.h"
  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 > 0)
  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. }