ssltest.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE.
  140. */
  141. /* Or gethostname won't be declared properly on Linux and GNU platforms. */
  142. #define _BSD_SOURCE 1
  143. #define _DEFAULT_SOURCE 1
  144. #include <assert.h>
  145. #include <errno.h>
  146. #include <limits.h>
  147. #include <stdio.h>
  148. #include <stdlib.h>
  149. #include <string.h>
  150. #include <time.h>
  151. #define USE_SOCKETS
  152. #include "e_os.h"
  153. #ifdef OPENSSL_SYS_VMS
  154. /*
  155. * Or isascii won't be declared properly on VMS (at least with DECompHP C).
  156. */
  157. # define _XOPEN_SOURCE 500
  158. #endif
  159. #include <ctype.h>
  160. #include <openssl/bio.h>
  161. #include <openssl/crypto.h>
  162. #include <openssl/evp.h>
  163. #include <openssl/x509.h>
  164. #include <openssl/x509v3.h>
  165. #include <openssl/ssl.h>
  166. #ifndef OPENSSL_NO_ENGINE
  167. # include <openssl/engine.h>
  168. #endif
  169. #include <openssl/err.h>
  170. #include <openssl/rand.h>
  171. #ifndef OPENSSL_NO_RSA
  172. # include <openssl/rsa.h>
  173. #endif
  174. #ifndef OPENSSL_NO_DSA
  175. # include <openssl/dsa.h>
  176. #endif
  177. #ifndef OPENSSL_NO_DH
  178. # include <openssl/dh.h>
  179. #endif
  180. #ifndef OPENSSL_NO_SRP
  181. # include <openssl/srp.h>
  182. #endif
  183. #include <openssl/bn.h>
  184. #include "../ssl/ssl_locl.h"
  185. /*
  186. * Or gethostname won't be declared properly
  187. * on Compaq platforms (at least with DEC C).
  188. * Do not try to put it earlier, or IPv6 includes
  189. * get screwed...
  190. */
  191. #define _XOPEN_SOURCE_EXTENDED 1
  192. #ifdef OPENSSL_SYS_WINDOWS
  193. # include <winsock.h>
  194. #else
  195. # include OPENSSL_UNISTD
  196. #endif
  197. /*
  198. * There is really no standard for this, so let's assign something
  199. * only for this test
  200. */
  201. #define COMP_ZLIB 1
  202. static int verify_callback(int ok, X509_STORE_CTX *ctx);
  203. static int app_verify_callback(X509_STORE_CTX *ctx, void *arg);
  204. #define APP_CALLBACK_STRING "Test Callback Argument"
  205. struct app_verify_arg {
  206. char *string;
  207. int app_verify;
  208. int allow_proxy_certs;
  209. char *proxy_auth;
  210. char *proxy_cond;
  211. };
  212. #ifndef OPENSSL_NO_DH
  213. static DH *get_dh512(void);
  214. static DH *get_dh1024(void);
  215. static DH *get_dh1024dsa(void);
  216. #endif
  217. static char *psk_key = NULL; /* by default PSK is not used */
  218. #ifndef OPENSSL_NO_PSK
  219. static unsigned int psk_client_callback(SSL *ssl, const char *hint,
  220. char *identity,
  221. unsigned int max_identity_len,
  222. unsigned char *psk,
  223. unsigned int max_psk_len);
  224. static unsigned int psk_server_callback(SSL *ssl, const char *identity,
  225. unsigned char *psk,
  226. unsigned int max_psk_len);
  227. #endif
  228. #ifndef OPENSSL_NO_SRP
  229. /* SRP client */
  230. /* This is a context that we pass to all callbacks */
  231. typedef struct srp_client_arg_st {
  232. char *srppassin;
  233. char *srplogin;
  234. } SRP_CLIENT_ARG;
  235. # define PWD_STRLEN 1024
  236. static char *ssl_give_srp_client_pwd_cb(SSL *s, void *arg)
  237. {
  238. SRP_CLIENT_ARG *srp_client_arg = (SRP_CLIENT_ARG *)arg;
  239. return OPENSSL_strdup((char *)srp_client_arg->srppassin);
  240. }
  241. /* SRP server */
  242. /* This is a context that we pass to SRP server callbacks */
  243. typedef struct srp_server_arg_st {
  244. char *expected_user;
  245. char *pass;
  246. } SRP_SERVER_ARG;
  247. static int ssl_srp_server_param_cb(SSL *s, int *ad, void *arg)
  248. {
  249. SRP_SERVER_ARG *p = (SRP_SERVER_ARG *)arg;
  250. if (strcmp(p->expected_user, SSL_get_srp_username(s)) != 0) {
  251. fprintf(stderr, "User %s doesn't exist\n", SSL_get_srp_username(s));
  252. return SSL3_AL_FATAL;
  253. }
  254. if (SSL_set_srp_server_param_pw(s, p->expected_user, p->pass, "1024") < 0) {
  255. *ad = SSL_AD_INTERNAL_ERROR;
  256. return SSL3_AL_FATAL;
  257. }
  258. return SSL_ERROR_NONE;
  259. }
  260. #endif
  261. static BIO *bio_err = NULL;
  262. static BIO *bio_stdout = NULL;
  263. #ifndef OPENSSL_NO_NEXTPROTONEG
  264. /* Note that this code assumes that this is only a one element list: */
  265. static const char NEXT_PROTO_STRING[] = "\x09testproto";
  266. static int npn_client = 0;
  267. static int npn_server = 0;
  268. static int npn_server_reject = 0;
  269. static int cb_client_npn(SSL *s, unsigned char **out, unsigned char *outlen,
  270. const unsigned char *in, unsigned int inlen,
  271. void *arg)
  272. {
  273. /*
  274. * This callback only returns the protocol string, rather than a length
  275. * prefixed set. We assume that NEXT_PROTO_STRING is a one element list
  276. * and remove the first byte to chop off the length prefix.
  277. */
  278. *out = (unsigned char *)NEXT_PROTO_STRING + 1;
  279. *outlen = sizeof(NEXT_PROTO_STRING) - 2;
  280. return SSL_TLSEXT_ERR_OK;
  281. }
  282. static int cb_server_npn(SSL *s, const unsigned char **data,
  283. unsigned int *len, void *arg)
  284. {
  285. *data = (const unsigned char *)NEXT_PROTO_STRING;
  286. *len = sizeof(NEXT_PROTO_STRING) - 1;
  287. return SSL_TLSEXT_ERR_OK;
  288. }
  289. static int cb_server_rejects_npn(SSL *s, const unsigned char **data,
  290. unsigned int *len, void *arg)
  291. {
  292. return SSL_TLSEXT_ERR_NOACK;
  293. }
  294. static int verify_npn(SSL *client, SSL *server)
  295. {
  296. const unsigned char *client_s;
  297. unsigned client_len;
  298. const unsigned char *server_s;
  299. unsigned server_len;
  300. SSL_get0_next_proto_negotiated(client, &client_s, &client_len);
  301. SSL_get0_next_proto_negotiated(server, &server_s, &server_len);
  302. if (client_len) {
  303. BIO_printf(bio_stdout, "Client NPN: ");
  304. BIO_write(bio_stdout, client_s, client_len);
  305. BIO_printf(bio_stdout, "\n");
  306. }
  307. if (server_len) {
  308. BIO_printf(bio_stdout, "Server NPN: ");
  309. BIO_write(bio_stdout, server_s, server_len);
  310. BIO_printf(bio_stdout, "\n");
  311. }
  312. /*
  313. * If an NPN string was returned, it must be the protocol that we
  314. * expected to negotiate.
  315. */
  316. if (client_len && (client_len != sizeof(NEXT_PROTO_STRING) - 2 ||
  317. memcmp(client_s, NEXT_PROTO_STRING + 1, client_len)))
  318. return -1;
  319. if (server_len && (server_len != sizeof(NEXT_PROTO_STRING) - 2 ||
  320. memcmp(server_s, NEXT_PROTO_STRING + 1, server_len)))
  321. return -1;
  322. if (!npn_client && client_len)
  323. return -1;
  324. if (!npn_server && server_len)
  325. return -1;
  326. if (npn_server_reject && server_len)
  327. return -1;
  328. if (npn_client && npn_server && (!client_len || !server_len))
  329. return -1;
  330. return 0;
  331. }
  332. #endif
  333. static const char *alpn_client;
  334. static const char *alpn_server;
  335. static const char *alpn_expected;
  336. static unsigned char *alpn_selected;
  337. static const char *server_min_proto;
  338. static const char *server_max_proto;
  339. static const char *client_min_proto;
  340. static const char *client_max_proto;
  341. static const char *should_negotiate;
  342. /*-
  343. * next_protos_parse parses a comma separated list of strings into a string
  344. * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
  345. * outlen: (output) set to the length of the resulting buffer on success.
  346. * err: (maybe NULL) on failure, an error message line is written to this BIO.
  347. * in: a NUL terminated string like "abc,def,ghi"
  348. *
  349. * returns: a malloced buffer or NULL on failure.
  350. */
  351. static unsigned char *next_protos_parse(unsigned short *outlen,
  352. const char *in)
  353. {
  354. size_t len;
  355. unsigned char *out;
  356. size_t i, start = 0;
  357. len = strlen(in);
  358. if (len >= 65535)
  359. return NULL;
  360. out = OPENSSL_malloc(strlen(in) + 1);
  361. if (!out)
  362. return NULL;
  363. for (i = 0; i <= len; ++i) {
  364. if (i == len || in[i] == ',') {
  365. if (i - start > 255) {
  366. OPENSSL_free(out);
  367. return NULL;
  368. }
  369. out[start] = i - start;
  370. start = i + 1;
  371. } else
  372. out[i + 1] = in[i];
  373. }
  374. *outlen = len + 1;
  375. return out;
  376. }
  377. static int cb_server_alpn(SSL *s, const unsigned char **out,
  378. unsigned char *outlen, const unsigned char *in,
  379. unsigned int inlen, void *arg)
  380. {
  381. unsigned char *protos;
  382. unsigned short protos_len;
  383. protos = next_protos_parse(&protos_len, alpn_server);
  384. if (protos == NULL) {
  385. fprintf(stderr, "failed to parser ALPN server protocol string: %s\n",
  386. alpn_server);
  387. abort();
  388. }
  389. if (SSL_select_next_proto
  390. ((unsigned char **)out, outlen, protos, protos_len, in,
  391. inlen) != OPENSSL_NPN_NEGOTIATED) {
  392. OPENSSL_free(protos);
  393. return SSL_TLSEXT_ERR_NOACK;
  394. }
  395. /*
  396. * Make a copy of the selected protocol which will be freed in
  397. * verify_alpn.
  398. */
  399. alpn_selected = OPENSSL_malloc(*outlen);
  400. memcpy(alpn_selected, *out, *outlen);
  401. *out = alpn_selected;
  402. OPENSSL_free(protos);
  403. return SSL_TLSEXT_ERR_OK;
  404. }
  405. static int verify_alpn(SSL *client, SSL *server)
  406. {
  407. const unsigned char *client_proto, *server_proto;
  408. unsigned int client_proto_len = 0, server_proto_len = 0;
  409. SSL_get0_alpn_selected(client, &client_proto, &client_proto_len);
  410. SSL_get0_alpn_selected(server, &server_proto, &server_proto_len);
  411. OPENSSL_free(alpn_selected);
  412. alpn_selected = NULL;
  413. if (client_proto_len != server_proto_len) {
  414. BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
  415. goto err;
  416. }
  417. if (client_proto != NULL &&
  418. memcmp(client_proto, server_proto, client_proto_len) != 0) {
  419. BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
  420. goto err;
  421. }
  422. if (client_proto_len > 0 && alpn_expected == NULL) {
  423. BIO_printf(bio_stdout, "ALPN unexpectedly negotiated\n");
  424. goto err;
  425. }
  426. if (alpn_expected != NULL &&
  427. (client_proto_len != strlen(alpn_expected) ||
  428. memcmp(client_proto, alpn_expected, client_proto_len) != 0)) {
  429. BIO_printf(bio_stdout,
  430. "ALPN selected protocols not equal to expected protocol: %s\n",
  431. alpn_expected);
  432. goto err;
  433. }
  434. return 0;
  435. err:
  436. BIO_printf(bio_stdout, "ALPN results: client: '");
  437. BIO_write(bio_stdout, client_proto, client_proto_len);
  438. BIO_printf(bio_stdout, "', server: '");
  439. BIO_write(bio_stdout, server_proto, server_proto_len);
  440. BIO_printf(bio_stdout, "'\n");
  441. BIO_printf(bio_stdout, "ALPN configured: client: '%s', server: '%s'\n",
  442. alpn_client, alpn_server);
  443. return -1;
  444. }
  445. #define SCT_EXT_TYPE 18
  446. /*
  447. * WARNING : below extension types are *NOT* IETF assigned, and could
  448. * conflict if these types are reassigned and handled specially by OpenSSL
  449. * in the future
  450. */
  451. #define TACK_EXT_TYPE 62208
  452. #define CUSTOM_EXT_TYPE_0 1000
  453. #define CUSTOM_EXT_TYPE_1 1001
  454. #define CUSTOM_EXT_TYPE_2 1002
  455. #define CUSTOM_EXT_TYPE_3 1003
  456. static const char custom_ext_cli_string[] = "abc";
  457. static const char custom_ext_srv_string[] = "defg";
  458. /* These set from cmdline */
  459. static char *serverinfo_file = NULL;
  460. static int serverinfo_sct = 0;
  461. static int serverinfo_tack = 0;
  462. /* These set based on extension callbacks */
  463. static int serverinfo_sct_seen = 0;
  464. static int serverinfo_tack_seen = 0;
  465. static int serverinfo_other_seen = 0;
  466. /* This set from cmdline */
  467. static int custom_ext = 0;
  468. /* This set based on extension callbacks */
  469. static int custom_ext_error = 0;
  470. static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
  471. const unsigned char *in, size_t inlen,
  472. int *al, void *arg)
  473. {
  474. if (ext_type == SCT_EXT_TYPE)
  475. serverinfo_sct_seen++;
  476. else if (ext_type == TACK_EXT_TYPE)
  477. serverinfo_tack_seen++;
  478. else
  479. serverinfo_other_seen++;
  480. return 1;
  481. }
  482. static int verify_serverinfo()
  483. {
  484. if (serverinfo_sct != serverinfo_sct_seen)
  485. return -1;
  486. if (serverinfo_tack != serverinfo_tack_seen)
  487. return -1;
  488. if (serverinfo_other_seen)
  489. return -1;
  490. return 0;
  491. }
  492. /*-
  493. * Four test cases for custom extensions:
  494. * 0 - no ClientHello extension or ServerHello response
  495. * 1 - ClientHello with "abc", no response
  496. * 2 - ClientHello with "abc", empty response
  497. * 3 - ClientHello with "abc", "defg" response
  498. */
  499. static int custom_ext_0_cli_add_cb(SSL *s, unsigned int ext_type,
  500. const unsigned char **out,
  501. size_t *outlen, int *al, void *arg)
  502. {
  503. if (ext_type != CUSTOM_EXT_TYPE_0)
  504. custom_ext_error = 1;
  505. return 0; /* Don't send an extension */
  506. }
  507. static int custom_ext_0_cli_parse_cb(SSL *s, unsigned int ext_type,
  508. const unsigned char *in,
  509. size_t inlen, int *al, void *arg)
  510. {
  511. return 1;
  512. }
  513. static int custom_ext_1_cli_add_cb(SSL *s, unsigned int ext_type,
  514. const unsigned char **out,
  515. size_t *outlen, int *al, void *arg)
  516. {
  517. if (ext_type != CUSTOM_EXT_TYPE_1)
  518. custom_ext_error = 1;
  519. *out = (const unsigned char *)custom_ext_cli_string;
  520. *outlen = strlen(custom_ext_cli_string);
  521. return 1; /* Send "abc" */
  522. }
  523. static int custom_ext_1_cli_parse_cb(SSL *s, unsigned int ext_type,
  524. const unsigned char *in,
  525. size_t inlen, int *al, void *arg)
  526. {
  527. return 1;
  528. }
  529. static int custom_ext_2_cli_add_cb(SSL *s, unsigned int ext_type,
  530. const unsigned char **out,
  531. size_t *outlen, int *al, void *arg)
  532. {
  533. if (ext_type != CUSTOM_EXT_TYPE_2)
  534. custom_ext_error = 1;
  535. *out = (const unsigned char *)custom_ext_cli_string;
  536. *outlen = strlen(custom_ext_cli_string);
  537. return 1; /* Send "abc" */
  538. }
  539. static int custom_ext_2_cli_parse_cb(SSL *s, unsigned int ext_type,
  540. const unsigned char *in,
  541. size_t inlen, int *al, void *arg)
  542. {
  543. if (ext_type != CUSTOM_EXT_TYPE_2)
  544. custom_ext_error = 1;
  545. if (inlen != 0)
  546. custom_ext_error = 1; /* Should be empty response */
  547. return 1;
  548. }
  549. static int custom_ext_3_cli_add_cb(SSL *s, unsigned int ext_type,
  550. const unsigned char **out,
  551. size_t *outlen, int *al, void *arg)
  552. {
  553. if (ext_type != CUSTOM_EXT_TYPE_3)
  554. custom_ext_error = 1;
  555. *out = (const unsigned char *)custom_ext_cli_string;
  556. *outlen = strlen(custom_ext_cli_string);
  557. return 1; /* Send "abc" */
  558. }
  559. static int custom_ext_3_cli_parse_cb(SSL *s, unsigned int ext_type,
  560. const unsigned char *in,
  561. size_t inlen, int *al, void *arg)
  562. {
  563. if (ext_type != CUSTOM_EXT_TYPE_3)
  564. custom_ext_error = 1;
  565. if (inlen != strlen(custom_ext_srv_string))
  566. custom_ext_error = 1;
  567. if (memcmp(custom_ext_srv_string, in, inlen) != 0)
  568. custom_ext_error = 1; /* Check for "defg" */
  569. return 1;
  570. }
  571. /*
  572. * custom_ext_0_cli_add_cb returns 0 - the server won't receive a callback
  573. * for this extension
  574. */
  575. static int custom_ext_0_srv_parse_cb(SSL *s, unsigned int ext_type,
  576. const unsigned char *in,
  577. size_t inlen, int *al, void *arg)
  578. {
  579. custom_ext_error = 1;
  580. return 1;
  581. }
  582. /* 'add' callbacks are only called if the 'parse' callback is called */
  583. static int custom_ext_0_srv_add_cb(SSL *s, unsigned int ext_type,
  584. const unsigned char **out,
  585. size_t *outlen, int *al, void *arg)
  586. {
  587. /* Error: should not have been called */
  588. custom_ext_error = 1;
  589. return 0; /* Don't send an extension */
  590. }
  591. static int custom_ext_1_srv_parse_cb(SSL *s, unsigned int ext_type,
  592. const unsigned char *in,
  593. size_t inlen, int *al, void *arg)
  594. {
  595. if (ext_type != CUSTOM_EXT_TYPE_1)
  596. custom_ext_error = 1;
  597. /* Check for "abc" */
  598. if (inlen != strlen(custom_ext_cli_string))
  599. custom_ext_error = 1;
  600. if (memcmp(in, custom_ext_cli_string, inlen) != 0)
  601. custom_ext_error = 1;
  602. return 1;
  603. }
  604. static int custom_ext_1_srv_add_cb(SSL *s, unsigned int ext_type,
  605. const unsigned char **out,
  606. size_t *outlen, int *al, void *arg)
  607. {
  608. return 0; /* Don't send an extension */
  609. }
  610. static int custom_ext_2_srv_parse_cb(SSL *s, unsigned int ext_type,
  611. const unsigned char *in,
  612. size_t inlen, int *al, void *arg)
  613. {
  614. if (ext_type != CUSTOM_EXT_TYPE_2)
  615. custom_ext_error = 1;
  616. /* Check for "abc" */
  617. if (inlen != strlen(custom_ext_cli_string))
  618. custom_ext_error = 1;
  619. if (memcmp(in, custom_ext_cli_string, inlen) != 0)
  620. custom_ext_error = 1;
  621. return 1;
  622. }
  623. static int custom_ext_2_srv_add_cb(SSL *s, unsigned int ext_type,
  624. const unsigned char **out,
  625. size_t *outlen, int *al, void *arg)
  626. {
  627. *out = NULL;
  628. *outlen = 0;
  629. return 1; /* Send empty extension */
  630. }
  631. static int custom_ext_3_srv_parse_cb(SSL *s, unsigned int ext_type,
  632. const unsigned char *in,
  633. size_t inlen, int *al, void *arg)
  634. {
  635. if (ext_type != CUSTOM_EXT_TYPE_3)
  636. custom_ext_error = 1;
  637. /* Check for "abc" */
  638. if (inlen != strlen(custom_ext_cli_string))
  639. custom_ext_error = 1;
  640. if (memcmp(in, custom_ext_cli_string, inlen) != 0)
  641. custom_ext_error = 1;
  642. return 1;
  643. }
  644. static int custom_ext_3_srv_add_cb(SSL *s, unsigned int ext_type,
  645. const unsigned char **out,
  646. size_t *outlen, int *al, void *arg)
  647. {
  648. *out = (const unsigned char *)custom_ext_srv_string;
  649. *outlen = strlen(custom_ext_srv_string);
  650. return 1; /* Send "defg" */
  651. }
  652. static char *cipher = NULL;
  653. static int verbose = 0;
  654. static int debug = 0;
  655. static const char rnd_seed[] =
  656. "string to make the random number generator think it has entropy";
  657. int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family,
  658. long bytes, clock_t *s_time, clock_t *c_time);
  659. int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time,
  660. clock_t *c_time);
  661. int doit(SSL *s_ssl, SSL *c_ssl, long bytes);
  662. static int do_test_cipherlist(void);
  663. static void sv_usage(void)
  664. {
  665. fprintf(stderr, "usage: ssltest [args ...]\n");
  666. fprintf(stderr, "\n");
  667. #ifdef OPENSSL_FIPS
  668. fprintf(stderr, "-F - run test in FIPS mode\n");
  669. #endif
  670. fprintf(stderr, " -server_auth - check server certificate\n");
  671. fprintf(stderr, " -client_auth - do client authentication\n");
  672. fprintf(stderr, " -proxy - allow proxy certificates\n");
  673. fprintf(stderr, " -proxy_auth <val> - set proxy policy rights\n");
  674. fprintf(stderr,
  675. " -proxy_cond <val> - expression to test proxy policy rights\n");
  676. fprintf(stderr, " -v - more output\n");
  677. fprintf(stderr, " -d - debug output\n");
  678. fprintf(stderr, " -reuse - use session-id reuse\n");
  679. fprintf(stderr, " -num <val> - number of connections to perform\n");
  680. fprintf(stderr,
  681. " -bytes <val> - number of bytes to swap between client/server\n");
  682. #ifndef OPENSSL_NO_DH
  683. fprintf(stderr,
  684. " -dhe512 - use 512 bit key for DHE (to test failure)\n");
  685. fprintf(stderr,
  686. " -dhe1024 - use 1024 bit key (safe prime) for DHE (default, no-op)\n");
  687. fprintf(stderr,
  688. " -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
  689. fprintf(stderr, " -no_dhe - disable DHE\n");
  690. #endif
  691. #ifndef OPENSSL_NO_EC
  692. fprintf(stderr, " -no_ecdhe - disable ECDHE\n");
  693. #endif
  694. #ifndef OPENSSL_NO_PSK
  695. fprintf(stderr, " -psk arg - PSK in hex (without 0x)\n");
  696. #endif
  697. #ifndef OPENSSL_NO_SRP
  698. fprintf(stderr, " -srpuser user - SRP username to use\n");
  699. fprintf(stderr, " -srppass arg - password for 'user'\n");
  700. #endif
  701. #ifndef OPENSSL_NO_SSL3
  702. fprintf(stderr, " -ssl3 - use SSLv3\n");
  703. #endif
  704. #ifndef OPENSSL_NO_TLS1
  705. fprintf(stderr, " -tls1 - use TLSv1\n");
  706. #endif
  707. #ifndef OPENSSL_NO_DTLS
  708. fprintf(stderr, " -dtls - use DTLS\n");
  709. #ifndef OPENSSL_NO_DTLS1
  710. fprintf(stderr, " -dtls1 - use DTLSv1\n");
  711. #endif
  712. #ifndef OPENSSL_NO_DTLS1_2
  713. fprintf(stderr, " -dtls12 - use DTLSv1.2\n");
  714. #endif
  715. #endif
  716. fprintf(stderr, " -CApath arg - PEM format directory of CA's\n");
  717. fprintf(stderr, " -CAfile arg - PEM format file of CA's\n");
  718. fprintf(stderr, " -cert arg - Server certificate file\n");
  719. fprintf(stderr,
  720. " -key arg - Server key file (default: same as -cert)\n");
  721. fprintf(stderr, " -c_cert arg - Client certificate file\n");
  722. fprintf(stderr,
  723. " -c_key arg - Client key file (default: same as -c_cert)\n");
  724. fprintf(stderr, " -cipher arg - The cipher list\n");
  725. fprintf(stderr, " -bio_pair - Use BIO pairs\n");
  726. fprintf(stderr, " -ipv4 - Use IPv4 connection on localhost\n");
  727. fprintf(stderr, " -ipv6 - Use IPv6 connection on localhost\n");
  728. fprintf(stderr, " -f - Test even cases that can't work\n");
  729. fprintf(stderr,
  730. " -time - measure processor time used by client and server\n");
  731. fprintf(stderr, " -zlib - use zlib compression\n");
  732. #ifndef OPENSSL_NO_EC
  733. fprintf(stderr,
  734. " -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n"
  735. " Use \"openssl ecparam -list_curves\" for all names\n"
  736. " (default is sect163r2).\n");
  737. #endif
  738. fprintf(stderr,
  739. " -test_cipherlist - Verifies the order of the ssl cipher lists.\n"
  740. " When this option is requested, the cipherlist\n"
  741. " tests are run instead of handshake tests.\n");
  742. #ifndef OPENSSL_NO_NEXTPROTONEG
  743. fprintf(stderr, " -npn_client - have client side offer NPN\n");
  744. fprintf(stderr, " -npn_server - have server side offer NPN\n");
  745. fprintf(stderr, " -npn_server_reject - have server reject NPN\n");
  746. #endif
  747. fprintf(stderr, " -serverinfo_file file - have server use this file\n");
  748. fprintf(stderr, " -serverinfo_sct - have client offer and expect SCT\n");
  749. fprintf(stderr,
  750. " -serverinfo_tack - have client offer and expect TACK\n");
  751. fprintf(stderr,
  752. " -custom_ext - try various custom extension callbacks\n");
  753. fprintf(stderr, " -alpn_client <string> - have client side offer ALPN\n");
  754. fprintf(stderr, " -alpn_server <string> - have server side offer ALPN\n");
  755. fprintf(stderr,
  756. " -alpn_expected <string> - the ALPN protocol that should be negotiated\n");
  757. fprintf(stderr, " -server_min_proto <string> - Minimum version the server should support\n");
  758. fprintf(stderr, " -server_max_proto <string> - Maximum version the server should support\n");
  759. fprintf(stderr, " -client_min_proto <string> - Minimum version the client should support\n");
  760. fprintf(stderr, " -client_max_proto <string> - Maximum version the client should support\n");
  761. fprintf(stderr, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n");
  762. }
  763. static void print_key_details(BIO *out, EVP_PKEY *key)
  764. {
  765. int keyid = EVP_PKEY_id(key);
  766. #ifndef OPENSSL_NO_EC
  767. if (keyid == EVP_PKEY_EC) {
  768. EC_KEY *ec = EVP_PKEY_get1_EC_KEY(key);
  769. int nid;
  770. const char *cname;
  771. nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  772. EC_KEY_free(ec);
  773. cname = EC_curve_nid2nist(nid);
  774. if (!cname)
  775. cname = OBJ_nid2sn(nid);
  776. BIO_printf(out, "%d bits EC (%s)", EVP_PKEY_bits(key), cname);
  777. } else
  778. #endif
  779. {
  780. const char *algname;
  781. switch (keyid) {
  782. case EVP_PKEY_RSA:
  783. algname = "RSA";
  784. break;
  785. case EVP_PKEY_DSA:
  786. algname = "DSA";
  787. break;
  788. case EVP_PKEY_DH:
  789. algname = "DH";
  790. break;
  791. default:
  792. algname = OBJ_nid2sn(keyid);
  793. break;
  794. }
  795. BIO_printf(out, "%d bits %s", EVP_PKEY_bits(key), algname);
  796. }
  797. }
  798. static void print_details(SSL *c_ssl, const char *prefix)
  799. {
  800. const SSL_CIPHER *ciph;
  801. int mdnid;
  802. X509 *cert;
  803. EVP_PKEY *pkey;
  804. ciph = SSL_get_current_cipher(c_ssl);
  805. BIO_printf(bio_stdout, "%s%s, cipher %s %s",
  806. prefix,
  807. SSL_get_version(c_ssl),
  808. SSL_CIPHER_get_version(ciph), SSL_CIPHER_get_name(ciph));
  809. cert = SSL_get_peer_certificate(c_ssl);
  810. if (cert != NULL) {
  811. pkey = X509_get_pubkey(cert);
  812. if (pkey != NULL) {
  813. BIO_puts(bio_stdout, ", ");
  814. print_key_details(bio_stdout, pkey);
  815. EVP_PKEY_free(pkey);
  816. }
  817. X509_free(cert);
  818. }
  819. if (SSL_get_server_tmp_key(c_ssl, &pkey)) {
  820. BIO_puts(bio_stdout, ", temp key: ");
  821. print_key_details(bio_stdout, pkey);
  822. EVP_PKEY_free(pkey);
  823. }
  824. if (SSL_get_peer_signature_nid(c_ssl, &mdnid))
  825. BIO_printf(bio_stdout, ", digest=%s", OBJ_nid2sn(mdnid));
  826. BIO_printf(bio_stdout, "\n");
  827. }
  828. static void lock_dbg_cb(int mode, int type, const char *file, int line)
  829. {
  830. static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
  831. const char *errstr = NULL;
  832. int rw;
  833. rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
  834. if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) {
  835. errstr = "invalid mode";
  836. goto err;
  837. }
  838. if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
  839. errstr = "type out of bounds";
  840. goto err;
  841. }
  842. if (mode & CRYPTO_LOCK) {
  843. if (modes[type]) {
  844. errstr = "already locked";
  845. /*
  846. * must not happen in a single-threaded program (would deadlock)
  847. */
  848. goto err;
  849. }
  850. modes[type] = rw;
  851. } else if (mode & CRYPTO_UNLOCK) {
  852. if (!modes[type]) {
  853. errstr = "not locked";
  854. goto err;
  855. }
  856. if (modes[type] != rw) {
  857. errstr = (rw == CRYPTO_READ) ?
  858. "CRYPTO_r_unlock on write lock" :
  859. "CRYPTO_w_unlock on read lock";
  860. }
  861. modes[type] = 0;
  862. } else {
  863. errstr = "invalid mode";
  864. goto err;
  865. }
  866. err:
  867. if (errstr) {
  868. /* we cannot use bio_err here */
  869. fprintf(stderr,
  870. "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
  871. errstr, mode, type, file, line);
  872. }
  873. }
  874. /*
  875. * protocol_from_string - converts a protocol version string to a number
  876. *
  877. * Returns -1 on failure or the version on success
  878. */
  879. static int protocol_from_string(const char *value)
  880. {
  881. struct protocol_versions {
  882. const char *name;
  883. int version;
  884. };
  885. static const struct protocol_versions versions[] = {
  886. {"ssl3", SSL3_VERSION},
  887. {"tls1", TLS1_VERSION},
  888. {"tls1.1", TLS1_1_VERSION},
  889. {"tls1.2", TLS1_2_VERSION},
  890. {"dtls1", DTLS1_VERSION},
  891. {"dtls1.2", DTLS1_2_VERSION}};
  892. size_t i;
  893. size_t n = OSSL_NELEM(versions);
  894. for (i = 0; i < n; i++)
  895. if (strcmp(versions[i].name, value) == 0)
  896. return versions[i].version;
  897. return -1;
  898. }
  899. /*
  900. * set_protocol_version - Sets protocol version minimum or maximum
  901. *
  902. * Returns 0 on failure and 1 on success
  903. */
  904. static int set_protocol_version(const char *version, SSL *ssl, int setting)
  905. {
  906. if (version != NULL) {
  907. int ver = protocol_from_string(version);
  908. if (ver < 0) {
  909. BIO_printf(bio_err, "Error parsing: %s\n", version);
  910. return 0;
  911. }
  912. return SSL_ctrl(ssl, setting, ver, NULL);
  913. }
  914. return 1;
  915. }
  916. int main(int argc, char *argv[])
  917. {
  918. char *CApath = NULL, *CAfile = NULL;
  919. int badop = 0;
  920. enum { BIO_MEM, BIO_PAIR, BIO_IPV4, BIO_IPV6 } bio_type = BIO_MEM;
  921. int force = 0;
  922. int dtls1 = 0, dtls12 = 0, dtls = 0, tls1 = 0, ssl3 = 0, ret = 1;
  923. int client_auth = 0;
  924. int server_auth = 0, i;
  925. struct app_verify_arg app_verify_arg =
  926. { APP_CALLBACK_STRING, 0, 0, NULL, NULL };
  927. char *p;
  928. #ifndef OPENSSL_NO_EC
  929. char *named_curve = NULL;
  930. #endif
  931. SSL_CTX *s_ctx = NULL;
  932. SSL_CTX *c_ctx = NULL;
  933. const SSL_METHOD *meth = NULL;
  934. SSL *c_ssl, *s_ssl;
  935. int number = 1, reuse = 0;
  936. long bytes = 256L;
  937. #ifndef OPENSSL_NO_DH
  938. DH *dh;
  939. int dhe512 = 0, dhe1024dsa = 0;
  940. #endif
  941. #ifndef OPENSSL_NO_EC
  942. EC_KEY *ecdh = NULL;
  943. #endif
  944. #ifndef OPENSSL_NO_SRP
  945. /* client */
  946. SRP_CLIENT_ARG srp_client_arg = { NULL, NULL };
  947. /* server */
  948. SRP_SERVER_ARG srp_server_arg = { NULL, NULL };
  949. #endif
  950. int no_dhe = 0;
  951. int no_ecdhe = 0;
  952. int no_psk = 0;
  953. int print_time = 0;
  954. clock_t s_time = 0, c_time = 0;
  955. #ifndef OPENSSL_NO_COMP
  956. int n, comp = 0;
  957. COMP_METHOD *cm = NULL;
  958. STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
  959. #endif
  960. int test_cipherlist = 0;
  961. #ifdef OPENSSL_FIPS
  962. int fips_mode = 0;
  963. #endif
  964. int no_protocol;
  965. SSL_CONF_CTX *s_cctx = NULL, *c_cctx = NULL;
  966. STACK_OF(OPENSSL_STRING) *conf_args = NULL;
  967. char *arg = NULL, *argn = NULL;
  968. verbose = 0;
  969. debug = 0;
  970. cipher = 0;
  971. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  972. CRYPTO_set_locking_callback(lock_dbg_cb);
  973. p = getenv("OPENSSL_DEBUG_MEMORY");
  974. if (p != NULL && strcmp(p, "on") == 0)
  975. CRYPTO_set_mem_debug(1);
  976. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  977. RAND_seed(rnd_seed, sizeof rnd_seed);
  978. bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
  979. s_cctx = SSL_CONF_CTX_new();
  980. c_cctx = SSL_CONF_CTX_new();
  981. if (!s_cctx || !c_cctx) {
  982. ERR_print_errors(bio_err);
  983. goto end;
  984. }
  985. SSL_CONF_CTX_set_flags(s_cctx,
  986. SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
  987. SSL_CONF_FLAG_CERTIFICATE |
  988. SSL_CONF_FLAG_REQUIRE_PRIVATE);
  989. if (!SSL_CONF_CTX_set1_prefix(s_cctx, "-s_")) {
  990. ERR_print_errors(bio_err);
  991. goto end;
  992. }
  993. SSL_CONF_CTX_set_flags(c_cctx,
  994. SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_CLIENT |
  995. SSL_CONF_FLAG_CERTIFICATE |
  996. SSL_CONF_FLAG_REQUIRE_PRIVATE);
  997. if (!SSL_CONF_CTX_set1_prefix(c_cctx, "-c_")) {
  998. ERR_print_errors(bio_err);
  999. goto end;
  1000. }
  1001. argc--;
  1002. argv++;
  1003. while (argc >= 1) {
  1004. if (strcmp(*argv, "-F") == 0) {
  1005. #ifdef OPENSSL_FIPS
  1006. fips_mode = 1;
  1007. #else
  1008. fprintf(stderr,
  1009. "not compiled with FIPS support, so exiting without running.\n");
  1010. EXIT(0);
  1011. #endif
  1012. } else if (strcmp(*argv, "-server_auth") == 0)
  1013. server_auth = 1;
  1014. else if (strcmp(*argv, "-client_auth") == 0)
  1015. client_auth = 1;
  1016. else if (strcmp(*argv, "-proxy_auth") == 0) {
  1017. if (--argc < 1)
  1018. goto bad;
  1019. app_verify_arg.proxy_auth = *(++argv);
  1020. } else if (strcmp(*argv, "-proxy_cond") == 0) {
  1021. if (--argc < 1)
  1022. goto bad;
  1023. app_verify_arg.proxy_cond = *(++argv);
  1024. } else if (strcmp(*argv, "-v") == 0)
  1025. verbose = 1;
  1026. else if (strcmp(*argv, "-d") == 0)
  1027. debug = 1;
  1028. else if (strcmp(*argv, "-reuse") == 0)
  1029. reuse = 1;
  1030. else if (strcmp(*argv, "-dhe512") == 0) {
  1031. #ifndef OPENSSL_NO_DH
  1032. dhe512 = 1;
  1033. #else
  1034. fprintf(stderr,
  1035. "ignoring -dhe512, since I'm compiled without DH\n");
  1036. #endif
  1037. } else if (strcmp(*argv, "-dhe1024dsa") == 0) {
  1038. #ifndef OPENSSL_NO_DH
  1039. dhe1024dsa = 1;
  1040. #else
  1041. fprintf(stderr,
  1042. "ignoring -dhe1024dsa, since I'm compiled without DH\n");
  1043. #endif
  1044. } else if (strcmp(*argv, "-no_dhe") == 0)
  1045. no_dhe = 1;
  1046. else if (strcmp(*argv, "-no_ecdhe") == 0)
  1047. no_ecdhe = 1;
  1048. else if (strcmp(*argv, "-psk") == 0) {
  1049. if (--argc < 1)
  1050. goto bad;
  1051. psk_key = *(++argv);
  1052. #ifndef OPENSSL_NO_PSK
  1053. if (strspn(psk_key, "abcdefABCDEF1234567890") != strlen(psk_key)) {
  1054. BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
  1055. goto bad;
  1056. }
  1057. #else
  1058. no_psk = 1;
  1059. #endif
  1060. }
  1061. #ifndef OPENSSL_NO_SRP
  1062. else if (strcmp(*argv, "-srpuser") == 0) {
  1063. if (--argc < 1)
  1064. goto bad;
  1065. srp_server_arg.expected_user = srp_client_arg.srplogin =
  1066. *(++argv);
  1067. tls1 = 1;
  1068. } else if (strcmp(*argv, "-srppass") == 0) {
  1069. if (--argc < 1)
  1070. goto bad;
  1071. srp_server_arg.pass = srp_client_arg.srppassin = *(++argv);
  1072. tls1 = 1;
  1073. }
  1074. #endif
  1075. else if (strcmp(*argv, "-tls1") == 0) {
  1076. tls1 = 1;
  1077. } else if (strcmp(*argv, "-ssl3") == 0) {
  1078. ssl3 = 1;
  1079. } else if (strcmp(*argv, "-dtls1") == 0) {
  1080. dtls1 = 1;
  1081. } else if (strcmp(*argv, "-dtls12") == 0) {
  1082. dtls12 = 1;
  1083. } else if (strcmp(*argv, "-dtls") == 0) {
  1084. dtls = 1;
  1085. } else if (strncmp(*argv, "-num", 4) == 0) {
  1086. if (--argc < 1)
  1087. goto bad;
  1088. number = atoi(*(++argv));
  1089. if (number == 0)
  1090. number = 1;
  1091. } else if (strcmp(*argv, "-bytes") == 0) {
  1092. if (--argc < 1)
  1093. goto bad;
  1094. bytes = atol(*(++argv));
  1095. if (bytes == 0L)
  1096. bytes = 1L;
  1097. i = strlen(argv[0]);
  1098. if (argv[0][i - 1] == 'k')
  1099. bytes *= 1024L;
  1100. if (argv[0][i - 1] == 'm')
  1101. bytes *= 1024L * 1024L;
  1102. } else if (strcmp(*argv, "-cipher") == 0) {
  1103. if (--argc < 1)
  1104. goto bad;
  1105. cipher = *(++argv);
  1106. } else if (strcmp(*argv, "-CApath") == 0) {
  1107. if (--argc < 1)
  1108. goto bad;
  1109. CApath = *(++argv);
  1110. } else if (strcmp(*argv, "-CAfile") == 0) {
  1111. if (--argc < 1)
  1112. goto bad;
  1113. CAfile = *(++argv);
  1114. } else if (strcmp(*argv, "-bio_pair") == 0) {
  1115. bio_type = BIO_PAIR;
  1116. } else if (strcmp(*argv, "-ipv4") == 0) {
  1117. bio_type = BIO_IPV4;
  1118. } else if (strcmp(*argv, "-ipv6") == 0) {
  1119. bio_type = BIO_IPV6;
  1120. } else if (strcmp(*argv, "-f") == 0) {
  1121. force = 1;
  1122. } else if (strcmp(*argv, "-time") == 0) {
  1123. print_time = 1;
  1124. }
  1125. #ifndef OPENSSL_NO_COMP
  1126. else if (strcmp(*argv, "-zlib") == 0) {
  1127. comp = COMP_ZLIB;
  1128. }
  1129. #endif
  1130. else if (strcmp(*argv, "-named_curve") == 0) {
  1131. if (--argc < 1)
  1132. goto bad;
  1133. #ifndef OPENSSL_NO_EC
  1134. named_curve = *(++argv);
  1135. #else
  1136. fprintf(stderr,
  1137. "ignoring -named_curve, since I'm compiled without ECDH\n");
  1138. ++argv;
  1139. #endif
  1140. } else if (strcmp(*argv, "-app_verify") == 0) {
  1141. app_verify_arg.app_verify = 1;
  1142. } else if (strcmp(*argv, "-proxy") == 0) {
  1143. app_verify_arg.allow_proxy_certs = 1;
  1144. } else if (strcmp(*argv, "-test_cipherlist") == 0) {
  1145. test_cipherlist = 1;
  1146. }
  1147. #ifndef OPENSSL_NO_NEXTPROTONEG
  1148. else if (strcmp(*argv, "-npn_client") == 0) {
  1149. npn_client = 1;
  1150. } else if (strcmp(*argv, "-npn_server") == 0) {
  1151. npn_server = 1;
  1152. } else if (strcmp(*argv, "-npn_server_reject") == 0) {
  1153. npn_server_reject = 1;
  1154. }
  1155. #endif
  1156. else if (strcmp(*argv, "-serverinfo_sct") == 0) {
  1157. serverinfo_sct = 1;
  1158. } else if (strcmp(*argv, "-serverinfo_tack") == 0) {
  1159. serverinfo_tack = 1;
  1160. } else if (strcmp(*argv, "-serverinfo_file") == 0) {
  1161. if (--argc < 1)
  1162. goto bad;
  1163. serverinfo_file = *(++argv);
  1164. } else if (strcmp(*argv, "-custom_ext") == 0) {
  1165. custom_ext = 1;
  1166. } else if (strcmp(*argv, "-alpn_client") == 0) {
  1167. if (--argc < 1)
  1168. goto bad;
  1169. alpn_client = *(++argv);
  1170. } else if (strcmp(*argv, "-alpn_server") == 0) {
  1171. if (--argc < 1)
  1172. goto bad;
  1173. alpn_server = *(++argv);
  1174. } else if (strcmp(*argv, "-alpn_expected") == 0) {
  1175. if (--argc < 1)
  1176. goto bad;
  1177. alpn_expected = *(++argv);
  1178. } else if (strcmp(*argv, "-server_min_proto") == 0) {
  1179. if (--argc < 1)
  1180. goto bad;
  1181. server_min_proto = *(++argv);
  1182. } else if (strcmp(*argv, "-server_max_proto") == 0) {
  1183. if (--argc < 1)
  1184. goto bad;
  1185. server_max_proto = *(++argv);
  1186. } else if (strcmp(*argv, "-client_min_proto") == 0) {
  1187. if (--argc < 1)
  1188. goto bad;
  1189. client_min_proto = *(++argv);
  1190. } else if (strcmp(*argv, "-client_max_proto") == 0) {
  1191. if (--argc < 1)
  1192. goto bad;
  1193. client_max_proto = *(++argv);
  1194. } else if (strcmp(*argv, "-should_negotiate") == 0) {
  1195. if (--argc < 1)
  1196. goto bad;
  1197. should_negotiate = *(++argv);
  1198. } else {
  1199. int rv;
  1200. arg = argv[0];
  1201. argn = argv[1];
  1202. /* Try to process command using SSL_CONF */
  1203. rv = SSL_CONF_cmd_argv(c_cctx, &argc, &argv);
  1204. /* If not processed try server */
  1205. if (rv == 0)
  1206. rv = SSL_CONF_cmd_argv(s_cctx, &argc, &argv);
  1207. /* Recognised: store it for later use */
  1208. if (rv > 0) {
  1209. if (rv == 1)
  1210. argn = NULL;
  1211. if (!conf_args) {
  1212. conf_args = sk_OPENSSL_STRING_new_null();
  1213. if (!conf_args)
  1214. goto end;
  1215. }
  1216. if (!sk_OPENSSL_STRING_push(conf_args, arg))
  1217. goto end;
  1218. if (!sk_OPENSSL_STRING_push(conf_args, argn))
  1219. goto end;
  1220. continue;
  1221. }
  1222. if (rv == -3)
  1223. BIO_printf(bio_err, "Missing argument for %s\n", arg);
  1224. else if (rv < 0)
  1225. BIO_printf(bio_err, "Error with command %s\n", arg);
  1226. else if (rv == 0)
  1227. BIO_printf(bio_err, "unknown option %s\n", arg);
  1228. badop = 1;
  1229. break;
  1230. }
  1231. argc--;
  1232. argv++;
  1233. }
  1234. if (badop) {
  1235. bad:
  1236. sv_usage();
  1237. goto end;
  1238. }
  1239. /*
  1240. * test_cipherlist prevails over protocol switch: we test the cipherlist
  1241. * for all enabled protocols.
  1242. */
  1243. if (test_cipherlist == 1) {
  1244. /*
  1245. * ensure that the cipher list are correctly sorted and exit
  1246. */
  1247. fprintf(stdout, "Testing cipherlist order only. Ignoring all "
  1248. "other options.\n");
  1249. if (do_test_cipherlist() == 0)
  1250. EXIT(1);
  1251. ret = 0;
  1252. goto end;
  1253. }
  1254. if (ssl3 + tls1 + dtls + dtls1 + dtls12 > 1) {
  1255. fprintf(stderr, "At most one of -ssl3, -tls1, -dtls, -dtls1 or -dtls12 should "
  1256. "be requested.\n");
  1257. EXIT(1);
  1258. }
  1259. #ifdef OPENSSL_NO_SSL3
  1260. if (ssl3)
  1261. no_protocol = 1;
  1262. else
  1263. #endif
  1264. #ifdef OPENSSL_NO_TLS1
  1265. if (tls1)
  1266. no_protocol = 1;
  1267. else
  1268. #endif
  1269. #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1)
  1270. if (dtls1)
  1271. no_protocol = 1;
  1272. else
  1273. #endif
  1274. #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1_2)
  1275. if (dtls12)
  1276. no_protocol = 1;
  1277. else
  1278. #endif
  1279. no_protocol = 0;
  1280. /*
  1281. * Testing was requested for a compiled-out protocol (e.g. SSLv3).
  1282. * Ideally, we would error out, but the generic test wrapper can't know
  1283. * when to expect failure. So we do nothing and return success.
  1284. */
  1285. if (no_protocol) {
  1286. fprintf(stderr, "Testing was requested for a disabled protocol. "
  1287. "Skipping tests.\n");
  1288. ret = 0;
  1289. goto end;
  1290. }
  1291. if (!ssl3 && !tls1 && !dtls && !dtls1 && !dtls12 && number > 1 && !reuse && !force) {
  1292. fprintf(stderr, "This case cannot work. Use -f to perform "
  1293. "the test anyway (and\n-d to see what happens), "
  1294. "or add one of -ssl3, -tls1, -dtls, -dtls1, -dtls12, -reuse\n"
  1295. "to avoid protocol mismatch.\n");
  1296. EXIT(1);
  1297. }
  1298. #ifdef OPENSSL_FIPS
  1299. if (fips_mode) {
  1300. if (!FIPS_mode_set(1)) {
  1301. ERR_print_errors(bio_err);
  1302. EXIT(1);
  1303. } else
  1304. fprintf(stderr, "*** IN FIPS MODE ***\n");
  1305. }
  1306. #endif
  1307. if (print_time) {
  1308. if (bio_type != BIO_PAIR) {
  1309. fprintf(stderr, "Using BIO pair (-bio_pair)\n");
  1310. bio_type = BIO_PAIR;
  1311. }
  1312. if (number < 50 && !force)
  1313. fprintf(stderr,
  1314. "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
  1315. }
  1316. /* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
  1317. #ifndef OPENSSL_NO_COMP
  1318. if (comp == COMP_ZLIB)
  1319. cm = COMP_zlib();
  1320. if (cm != NULL) {
  1321. if (COMP_get_type(cm) != NID_undef) {
  1322. if (SSL_COMP_add_compression_method(comp, cm) != 0) {
  1323. fprintf(stderr, "Failed to add compression method\n");
  1324. ERR_print_errors_fp(stderr);
  1325. }
  1326. } else {
  1327. fprintf(stderr,
  1328. "Warning: %s compression not supported\n",
  1329. comp == COMP_ZLIB ? "zlib" : "unknown");
  1330. ERR_print_errors_fp(stderr);
  1331. }
  1332. }
  1333. ssl_comp_methods = SSL_COMP_get_compression_methods();
  1334. n = sk_SSL_COMP_num(ssl_comp_methods);
  1335. if (n) {
  1336. int j;
  1337. printf("Available compression methods:");
  1338. for (j = 0; j < n; j++) {
  1339. SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);
  1340. printf(" %s:%d", c->name, c->id);
  1341. }
  1342. printf("\n");
  1343. }
  1344. #endif
  1345. /*
  1346. * At this point, ssl3/tls1 is only set if the protocol is available.
  1347. * (Otherwise we exit early.) However the compiler doesn't know this, so
  1348. * we ifdef.
  1349. */
  1350. #ifndef OPENSSL_NO_DTLS
  1351. #ifndef OPENSSL_NO_DTLS1
  1352. if (dtls1)
  1353. meth = DTLSv1_method();
  1354. else
  1355. #endif
  1356. #ifndef OPENSSL_NO_DTLS1_2
  1357. if (dtls12)
  1358. meth = DTLSv1_2_method();
  1359. else
  1360. #endif
  1361. if (dtls)
  1362. meth = DTLS_method();
  1363. else
  1364. #endif
  1365. #ifndef OPENSSL_NO_SSL3
  1366. if (ssl3)
  1367. meth = SSLv3_method();
  1368. else
  1369. #endif
  1370. #ifndef OPENSSL_NO_TLS1
  1371. if (tls1)
  1372. meth = TLSv1_method();
  1373. else
  1374. #endif
  1375. meth = TLS_method();
  1376. c_ctx = SSL_CTX_new(meth);
  1377. s_ctx = SSL_CTX_new(meth);
  1378. if ((c_ctx == NULL) || (s_ctx == NULL)) {
  1379. ERR_print_errors(bio_err);
  1380. goto end;
  1381. }
  1382. /*
  1383. * Since we will use low security ciphersuites and keys for testing set
  1384. * security level to zero by default. Tests can override this by adding
  1385. * "@SECLEVEL=n" to the cipher string.
  1386. */
  1387. SSL_CTX_set_security_level(c_ctx, 0);
  1388. SSL_CTX_set_security_level(s_ctx, 0);
  1389. if (cipher != NULL) {
  1390. if (!SSL_CTX_set_cipher_list(c_ctx, cipher)
  1391. || !SSL_CTX_set_cipher_list(s_ctx, cipher)) {
  1392. ERR_print_errors(bio_err);
  1393. goto end;
  1394. }
  1395. }
  1396. /* Process SSL_CONF arguments */
  1397. SSL_CONF_CTX_set_ssl_ctx(c_cctx, c_ctx);
  1398. SSL_CONF_CTX_set_ssl_ctx(s_cctx, s_ctx);
  1399. for (i = 0; i < sk_OPENSSL_STRING_num(conf_args); i += 2) {
  1400. int rv;
  1401. arg = sk_OPENSSL_STRING_value(conf_args, i);
  1402. argn = sk_OPENSSL_STRING_value(conf_args, i + 1);
  1403. rv = SSL_CONF_cmd(c_cctx, arg, argn);
  1404. /* If not recognised use server context */
  1405. if (rv == -2)
  1406. rv = SSL_CONF_cmd(s_cctx, arg, argn);
  1407. if (rv <= 0) {
  1408. BIO_printf(bio_err, "Error processing %s %s\n",
  1409. arg, argn ? argn : "");
  1410. ERR_print_errors(bio_err);
  1411. goto end;
  1412. }
  1413. }
  1414. if (!SSL_CONF_CTX_finish(s_cctx) || !SSL_CONF_CTX_finish(c_cctx)) {
  1415. BIO_puts(bio_err, "Error finishing context\n");
  1416. ERR_print_errors(bio_err);
  1417. goto end;
  1418. }
  1419. #ifndef OPENSSL_NO_DH
  1420. if (!no_dhe) {
  1421. if (dhe1024dsa) {
  1422. /*
  1423. * use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks
  1424. */
  1425. SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);
  1426. dh = get_dh1024dsa();
  1427. } else if (dhe512)
  1428. dh = get_dh512();
  1429. else
  1430. dh = get_dh1024();
  1431. SSL_CTX_set_tmp_dh(s_ctx, dh);
  1432. DH_free(dh);
  1433. }
  1434. #else
  1435. (void)no_dhe;
  1436. #endif
  1437. #ifndef OPENSSL_NO_EC
  1438. if (!no_ecdhe) {
  1439. int nid;
  1440. if (named_curve != NULL) {
  1441. nid = OBJ_sn2nid(named_curve);
  1442. if (nid == 0) {
  1443. BIO_printf(bio_err, "unknown curve name (%s)\n", named_curve);
  1444. goto end;
  1445. }
  1446. } else {
  1447. nid = NID_X9_62_prime256v1;
  1448. }
  1449. ecdh = EC_KEY_new_by_curve_name(nid);
  1450. if (ecdh == NULL) {
  1451. BIO_printf(bio_err, "unable to create curve\n");
  1452. goto end;
  1453. }
  1454. SSL_CTX_set_tmp_ecdh(s_ctx, ecdh);
  1455. SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_ECDH_USE);
  1456. EC_KEY_free(ecdh);
  1457. }
  1458. #else
  1459. (void)no_ecdhe;
  1460. #endif
  1461. if ((!SSL_CTX_load_verify_locations(s_ctx, CAfile, CApath)) ||
  1462. (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
  1463. (!SSL_CTX_load_verify_locations(c_ctx, CAfile, CApath)) ||
  1464. (!SSL_CTX_set_default_verify_paths(c_ctx))) {
  1465. /* fprintf(stderr,"SSL_load_verify_locations\n"); */
  1466. ERR_print_errors(bio_err);
  1467. /* goto end; */
  1468. }
  1469. if (client_auth) {
  1470. printf("client authentication\n");
  1471. SSL_CTX_set_verify(s_ctx,
  1472. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  1473. verify_callback);
  1474. SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback,
  1475. &app_verify_arg);
  1476. }
  1477. if (server_auth) {
  1478. printf("server authentication\n");
  1479. SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
  1480. SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback,
  1481. &app_verify_arg);
  1482. }
  1483. {
  1484. int session_id_context = 0;
  1485. if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context,
  1486. sizeof session_id_context)) {
  1487. ERR_print_errors(bio_err);
  1488. goto end;
  1489. }
  1490. }
  1491. /* Use PSK only if PSK key is given */
  1492. if (psk_key != NULL) {
  1493. /*
  1494. * no_psk is used to avoid putting psk command to openssl tool
  1495. */
  1496. if (no_psk) {
  1497. /*
  1498. * if PSK is not compiled in and psk key is given, do nothing and
  1499. * exit successfully
  1500. */
  1501. ret = 0;
  1502. goto end;
  1503. }
  1504. #ifndef OPENSSL_NO_PSK
  1505. SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback);
  1506. SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback);
  1507. if (debug)
  1508. BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n");
  1509. if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint")) {
  1510. BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n");
  1511. ERR_print_errors(bio_err);
  1512. goto end;
  1513. }
  1514. #endif
  1515. }
  1516. #ifndef OPENSSL_NO_SRP
  1517. if (srp_client_arg.srplogin) {
  1518. if (!SSL_CTX_set_srp_username(c_ctx, srp_client_arg.srplogin)) {
  1519. BIO_printf(bio_err, "Unable to set SRP username\n");
  1520. goto end;
  1521. }
  1522. SSL_CTX_set_srp_cb_arg(c_ctx, &srp_client_arg);
  1523. SSL_CTX_set_srp_client_pwd_callback(c_ctx,
  1524. ssl_give_srp_client_pwd_cb);
  1525. /*
  1526. * SSL_CTX_set_srp_strength(c_ctx, srp_client_arg.strength);
  1527. */
  1528. }
  1529. if (srp_server_arg.expected_user != NULL) {
  1530. SSL_CTX_set_verify(s_ctx, SSL_VERIFY_NONE, verify_callback);
  1531. SSL_CTX_set_srp_cb_arg(s_ctx, &srp_server_arg);
  1532. SSL_CTX_set_srp_username_callback(s_ctx, ssl_srp_server_param_cb);
  1533. }
  1534. #endif
  1535. #ifndef OPENSSL_NO_NEXTPROTONEG
  1536. if (npn_client) {
  1537. SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL);
  1538. }
  1539. if (npn_server) {
  1540. if (npn_server_reject) {
  1541. BIO_printf(bio_err,
  1542. "Can't have both -npn_server and -npn_server_reject\n");
  1543. goto end;
  1544. }
  1545. SSL_CTX_set_next_protos_advertised_cb(s_ctx, cb_server_npn, NULL);
  1546. }
  1547. if (npn_server_reject) {
  1548. SSL_CTX_set_next_protos_advertised_cb(s_ctx, cb_server_rejects_npn,
  1549. NULL);
  1550. }
  1551. #endif
  1552. if (serverinfo_sct) {
  1553. if (!SSL_CTX_add_client_custom_ext(c_ctx, SCT_EXT_TYPE,
  1554. NULL, NULL, NULL,
  1555. serverinfo_cli_parse_cb, NULL)) {
  1556. BIO_printf(bio_err, "Error adding SCT extension\n");
  1557. goto end;
  1558. }
  1559. }
  1560. if (serverinfo_tack) {
  1561. if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE,
  1562. NULL, NULL, NULL,
  1563. serverinfo_cli_parse_cb, NULL)) {
  1564. BIO_printf(bio_err, "Error adding TACK extension\n");
  1565. goto end;
  1566. }
  1567. }
  1568. if (serverinfo_file)
  1569. if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file)) {
  1570. BIO_printf(bio_err, "missing serverinfo file\n");
  1571. goto end;
  1572. }
  1573. if (custom_ext) {
  1574. if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0,
  1575. custom_ext_0_cli_add_cb,
  1576. NULL, NULL,
  1577. custom_ext_0_cli_parse_cb, NULL)
  1578. || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1,
  1579. custom_ext_1_cli_add_cb,
  1580. NULL, NULL,
  1581. custom_ext_1_cli_parse_cb, NULL)
  1582. || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2,
  1583. custom_ext_2_cli_add_cb,
  1584. NULL, NULL,
  1585. custom_ext_2_cli_parse_cb, NULL)
  1586. || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3,
  1587. custom_ext_3_cli_add_cb,
  1588. NULL, NULL,
  1589. custom_ext_3_cli_parse_cb, NULL)
  1590. || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0,
  1591. custom_ext_0_srv_add_cb,
  1592. NULL, NULL,
  1593. custom_ext_0_srv_parse_cb, NULL)
  1594. || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1,
  1595. custom_ext_1_srv_add_cb,
  1596. NULL, NULL,
  1597. custom_ext_1_srv_parse_cb, NULL)
  1598. || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2,
  1599. custom_ext_2_srv_add_cb,
  1600. NULL, NULL,
  1601. custom_ext_2_srv_parse_cb, NULL)
  1602. || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3,
  1603. custom_ext_3_srv_add_cb,
  1604. NULL, NULL,
  1605. custom_ext_3_srv_parse_cb, NULL)) {
  1606. BIO_printf(bio_err, "Error setting custom extensions\n");
  1607. goto end;
  1608. }
  1609. }
  1610. if (alpn_server)
  1611. SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, NULL);
  1612. if (alpn_client) {
  1613. unsigned short alpn_len;
  1614. unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client);
  1615. if (alpn == NULL) {
  1616. BIO_printf(bio_err, "Error parsing -alpn_client argument\n");
  1617. goto end;
  1618. }
  1619. /* Returns 0 on success!! */
  1620. if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) {
  1621. BIO_printf(bio_err, "Error setting ALPN\n");
  1622. OPENSSL_free(alpn);
  1623. goto end;
  1624. }
  1625. OPENSSL_free(alpn);
  1626. }
  1627. c_ssl = SSL_new(c_ctx);
  1628. s_ssl = SSL_new(s_ctx);
  1629. if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
  1630. goto end;
  1631. if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
  1632. goto end;
  1633. if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
  1634. goto end;
  1635. if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
  1636. goto end;
  1637. BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes);
  1638. for (i = 0; i < number; i++) {
  1639. if (!reuse) {
  1640. if (!SSL_set_session(c_ssl, NULL)) {
  1641. BIO_printf(bio_err, "Failed to set session\n");
  1642. goto end;
  1643. }
  1644. }
  1645. switch (bio_type) {
  1646. case BIO_MEM:
  1647. ret = doit(s_ssl, c_ssl, bytes);
  1648. break;
  1649. case BIO_PAIR:
  1650. ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time);
  1651. break;
  1652. case BIO_IPV4:
  1653. ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4,
  1654. bytes, &s_time, &c_time);
  1655. break;
  1656. case BIO_IPV6:
  1657. ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6,
  1658. bytes, &s_time, &c_time);
  1659. break;
  1660. }
  1661. if (ret) break;
  1662. }
  1663. if (should_negotiate && ret == 0 &&
  1664. strcmp(should_negotiate, "fail-server") != 0 &&
  1665. strcmp(should_negotiate, "fail-client") != 0) {
  1666. int version = protocol_from_string(should_negotiate);
  1667. if (version < 0) {
  1668. BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate);
  1669. ret = 1;
  1670. goto err;
  1671. }
  1672. if (SSL_version(c_ssl) != version) {
  1673. BIO_printf(bio_err, "Unxpected version negotiated. "
  1674. "Expected: %s, got %s\n", should_negotiate, SSL_get_version(c_ssl));
  1675. ret = 1;
  1676. goto err;
  1677. }
  1678. }
  1679. if (!verbose) {
  1680. print_details(c_ssl, "");
  1681. }
  1682. if (print_time) {
  1683. #ifdef CLOCKS_PER_SEC
  1684. /*
  1685. * "To determine the time in seconds, the value returned by the clock
  1686. * function should be divided by the value of the macro
  1687. * CLOCKS_PER_SEC." -- ISO/IEC 9899
  1688. */
  1689. BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
  1690. "Approximate total client time: %6.2f s\n",
  1691. (double)s_time / CLOCKS_PER_SEC,
  1692. (double)c_time / CLOCKS_PER_SEC);
  1693. #else
  1694. BIO_printf(bio_stdout,
  1695. "Approximate total server time: %6.2f units\n"
  1696. "Approximate total client time: %6.2f units\n",
  1697. (double)s_time, (double)c_time);
  1698. #endif
  1699. }
  1700. err:
  1701. SSL_free(s_ssl);
  1702. SSL_free(c_ssl);
  1703. end:
  1704. SSL_CTX_free(s_ctx);
  1705. SSL_CTX_free(c_ctx);
  1706. SSL_CONF_CTX_free(s_cctx);
  1707. SSL_CONF_CTX_free(c_cctx);
  1708. sk_OPENSSL_STRING_free(conf_args);
  1709. BIO_free(bio_stdout);
  1710. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  1711. if (CRYPTO_mem_leaks(bio_err) <= 0)
  1712. ret = 1;
  1713. #endif
  1714. BIO_free(bio_err);
  1715. EXIT(ret);
  1716. }
  1717. int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count,
  1718. clock_t *s_time, clock_t *c_time)
  1719. {
  1720. long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
  1721. BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
  1722. BIO *acpt = NULL, *server = NULL, *client = NULL;
  1723. char addr_str[40];
  1724. int ret = 1;
  1725. int err_in_client = 0;
  1726. int err_in_server = 0;
  1727. acpt = BIO_new_accept("0");
  1728. if (acpt == NULL)
  1729. goto err;
  1730. BIO_set_accept_ip_family(acpt, family);
  1731. BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR);
  1732. if (BIO_do_accept(acpt) <= 0)
  1733. goto err;
  1734. BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt));
  1735. client = BIO_new_connect(addr_str);
  1736. BIO_set_conn_ip_family(client, family);
  1737. if (!client)
  1738. goto err;
  1739. if (BIO_set_nbio(client, 1) <= 0)
  1740. goto err;
  1741. if (BIO_set_nbio(acpt, 1) <= 0)
  1742. goto err;
  1743. {
  1744. int st_connect = 0, st_accept = 0;
  1745. while(!st_connect || !st_accept) {
  1746. if (!st_connect) {
  1747. if (BIO_do_connect(client) <= 0) {
  1748. if (!BIO_should_retry(client))
  1749. goto err;
  1750. } else {
  1751. st_connect = 1;
  1752. }
  1753. }
  1754. if (!st_accept) {
  1755. if (BIO_do_accept(acpt) <= 0) {
  1756. if (!BIO_should_retry(acpt))
  1757. goto err;
  1758. } else {
  1759. st_accept = 1;
  1760. }
  1761. }
  1762. }
  1763. }
  1764. /* We're not interested in accepting further connects */
  1765. server = BIO_pop(acpt);
  1766. BIO_free_all(acpt);
  1767. acpt = NULL;
  1768. s_ssl_bio = BIO_new(BIO_f_ssl());
  1769. if (!s_ssl_bio)
  1770. goto err;
  1771. c_ssl_bio = BIO_new(BIO_f_ssl());
  1772. if (!c_ssl_bio)
  1773. goto err;
  1774. SSL_set_connect_state(c_ssl);
  1775. SSL_set_bio(c_ssl, client, client);
  1776. (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
  1777. SSL_set_accept_state(s_ssl);
  1778. SSL_set_bio(s_ssl, server, server);
  1779. (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
  1780. do {
  1781. /*-
  1782. * c_ssl_bio: SSL filter BIO
  1783. *
  1784. * client: I/O for SSL library
  1785. *
  1786. *
  1787. * server: I/O for SSL library
  1788. *
  1789. * s_ssl_bio: SSL filter BIO
  1790. */
  1791. /*
  1792. * We have non-blocking behaviour throughout this test program, but
  1793. * can be sure that there is *some* progress in each iteration; so we
  1794. * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
  1795. * we just try everything in each iteration
  1796. */
  1797. {
  1798. /* CLIENT */
  1799. char cbuf[1024 * 8];
  1800. int i, r;
  1801. clock_t c_clock = clock();
  1802. memset(cbuf, 0, sizeof(cbuf));
  1803. if (debug)
  1804. if (SSL_in_init(c_ssl))
  1805. printf("client waiting in SSL_connect - %s\n",
  1806. SSL_state_string_long(c_ssl));
  1807. if (cw_num > 0) {
  1808. /* Write to server. */
  1809. if (cw_num > (long)sizeof cbuf)
  1810. i = sizeof cbuf;
  1811. else
  1812. i = (int)cw_num;
  1813. r = BIO_write(c_ssl_bio, cbuf, i);
  1814. if (r < 0) {
  1815. if (!BIO_should_retry(c_ssl_bio)) {
  1816. fprintf(stderr, "ERROR in CLIENT\n");
  1817. err_in_client = 1;
  1818. goto err;
  1819. }
  1820. /*
  1821. * BIO_should_retry(...) can just be ignored here. The
  1822. * library expects us to call BIO_write with the same
  1823. * arguments again, and that's what we will do in the
  1824. * next iteration.
  1825. */
  1826. } else if (r == 0) {
  1827. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  1828. goto err;
  1829. } else {
  1830. if (debug)
  1831. printf("client wrote %d\n", r);
  1832. cw_num -= r;
  1833. }
  1834. }
  1835. if (cr_num > 0) {
  1836. /* Read from server. */
  1837. r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
  1838. if (r < 0) {
  1839. if (!BIO_should_retry(c_ssl_bio)) {
  1840. fprintf(stderr, "ERROR in CLIENT\n");
  1841. err_in_client = 1;
  1842. goto err;
  1843. }
  1844. /*
  1845. * Again, "BIO_should_retry" can be ignored.
  1846. */
  1847. } else if (r == 0) {
  1848. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  1849. goto err;
  1850. } else {
  1851. if (debug)
  1852. printf("client read %d\n", r);
  1853. cr_num -= r;
  1854. }
  1855. }
  1856. /*
  1857. * c_time and s_time increments will typically be very small
  1858. * (depending on machine speed and clock tick intervals), but
  1859. * sampling over a large number of connections should result in
  1860. * fairly accurate figures. We cannot guarantee a lot, however
  1861. * -- if each connection lasts for exactly one clock tick, it
  1862. * will be counted only for the client or only for the server or
  1863. * even not at all.
  1864. */
  1865. *c_time += (clock() - c_clock);
  1866. }
  1867. {
  1868. /* SERVER */
  1869. char sbuf[1024 * 8];
  1870. int i, r;
  1871. clock_t s_clock = clock();
  1872. memset(sbuf, 0, sizeof(sbuf));
  1873. if (debug)
  1874. if (SSL_in_init(s_ssl))
  1875. printf("server waiting in SSL_accept - %s\n",
  1876. SSL_state_string_long(s_ssl));
  1877. if (sw_num > 0) {
  1878. /* Write to client. */
  1879. if (sw_num > (long)sizeof sbuf)
  1880. i = sizeof sbuf;
  1881. else
  1882. i = (int)sw_num;
  1883. r = BIO_write(s_ssl_bio, sbuf, i);
  1884. if (r < 0) {
  1885. if (!BIO_should_retry(s_ssl_bio)) {
  1886. fprintf(stderr, "ERROR in SERVER\n");
  1887. err_in_server = 1;
  1888. goto err;
  1889. }
  1890. /* Ignore "BIO_should_retry". */
  1891. } else if (r == 0) {
  1892. fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
  1893. goto err;
  1894. } else {
  1895. if (debug)
  1896. printf("server wrote %d\n", r);
  1897. sw_num -= r;
  1898. }
  1899. }
  1900. if (sr_num > 0) {
  1901. /* Read from client. */
  1902. r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
  1903. if (r < 0) {
  1904. if (!BIO_should_retry(s_ssl_bio)) {
  1905. fprintf(stderr, "ERROR in SERVER\n");
  1906. err_in_server = 1;
  1907. goto err;
  1908. }
  1909. /* blah, blah */
  1910. } else if (r == 0) {
  1911. fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
  1912. goto err;
  1913. } else {
  1914. if (debug)
  1915. printf("server read %d\n", r);
  1916. sr_num -= r;
  1917. }
  1918. }
  1919. *s_time += (clock() - s_clock);
  1920. }
  1921. }
  1922. while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
  1923. if (verbose)
  1924. print_details(c_ssl, "DONE via TCP connect: ");
  1925. #ifndef OPENSSL_NO_NEXTPROTONEG
  1926. if (verify_npn(c_ssl, s_ssl) < 0) {
  1927. ret = 1;
  1928. goto end;
  1929. }
  1930. #endif
  1931. if (verify_serverinfo() < 0) {
  1932. fprintf(stderr, "Server info verify error\n");
  1933. ret = 1;
  1934. goto err;
  1935. }
  1936. if (verify_alpn(c_ssl, s_ssl) < 0) {
  1937. ret = 1;
  1938. goto err;
  1939. }
  1940. if (custom_ext_error) {
  1941. fprintf(stderr, "Custom extension error\n");
  1942. ret = 1;
  1943. goto err;
  1944. }
  1945. end:
  1946. ret = 0;
  1947. err:
  1948. ERR_print_errors(bio_err);
  1949. BIO_free_all(acpt);
  1950. BIO_free(server);
  1951. BIO_free(client);
  1952. BIO_free(s_ssl_bio);
  1953. BIO_free(c_ssl_bio);
  1954. if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
  1955. ret = (err_in_client != 0) ? 0 : 1;
  1956. else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
  1957. ret = (err_in_server != 0) ? 0 : 1;
  1958. return ret;
  1959. }
  1960. int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
  1961. clock_t *s_time, clock_t *c_time)
  1962. {
  1963. long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
  1964. BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
  1965. BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
  1966. int ret = 1;
  1967. int err_in_client = 0;
  1968. int err_in_server = 0;
  1969. size_t bufsiz = 256; /* small buffer for testing */
  1970. if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
  1971. goto err;
  1972. if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
  1973. goto err;
  1974. s_ssl_bio = BIO_new(BIO_f_ssl());
  1975. if (!s_ssl_bio)
  1976. goto err;
  1977. c_ssl_bio = BIO_new(BIO_f_ssl());
  1978. if (!c_ssl_bio)
  1979. goto err;
  1980. SSL_set_connect_state(c_ssl);
  1981. SSL_set_bio(c_ssl, client, client);
  1982. (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
  1983. SSL_set_accept_state(s_ssl);
  1984. SSL_set_bio(s_ssl, server, server);
  1985. (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
  1986. do {
  1987. /*-
  1988. * c_ssl_bio: SSL filter BIO
  1989. *
  1990. * client: pseudo-I/O for SSL library
  1991. *
  1992. * client_io: client's SSL communication; usually to be
  1993. * relayed over some I/O facility, but in this
  1994. * test program, we're the server, too:
  1995. *
  1996. * server_io: server's SSL communication
  1997. *
  1998. * server: pseudo-I/O for SSL library
  1999. *
  2000. * s_ssl_bio: SSL filter BIO
  2001. *
  2002. * The client and the server each employ a "BIO pair":
  2003. * client + client_io, server + server_io.
  2004. * BIO pairs are symmetric. A BIO pair behaves similar
  2005. * to a non-blocking socketpair (but both endpoints must
  2006. * be handled by the same thread).
  2007. * [Here we could connect client and server to the ends
  2008. * of a single BIO pair, but then this code would be less
  2009. * suitable as an example for BIO pairs in general.]
  2010. *
  2011. * Useful functions for querying the state of BIO pair endpoints:
  2012. *
  2013. * BIO_ctrl_pending(bio) number of bytes we can read now
  2014. * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfil
  2015. * other side's read attempt
  2016. * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
  2017. *
  2018. * ..._read_request is never more than ..._write_guarantee;
  2019. * it depends on the application which one you should use.
  2020. */
  2021. /*
  2022. * We have non-blocking behaviour throughout this test program, but
  2023. * can be sure that there is *some* progress in each iteration; so we
  2024. * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
  2025. * we just try everything in each iteration
  2026. */
  2027. {
  2028. /* CLIENT */
  2029. char cbuf[1024 * 8];
  2030. int i, r;
  2031. clock_t c_clock = clock();
  2032. memset(cbuf, 0, sizeof(cbuf));
  2033. if (debug)
  2034. if (SSL_in_init(c_ssl))
  2035. printf("client waiting in SSL_connect - %s\n",
  2036. SSL_state_string_long(c_ssl));
  2037. if (cw_num > 0) {
  2038. /* Write to server. */
  2039. if (cw_num > (long)sizeof cbuf)
  2040. i = sizeof cbuf;
  2041. else
  2042. i = (int)cw_num;
  2043. r = BIO_write(c_ssl_bio, cbuf, i);
  2044. if (r < 0) {
  2045. if (!BIO_should_retry(c_ssl_bio)) {
  2046. fprintf(stderr, "ERROR in CLIENT\n");
  2047. err_in_client = 1;
  2048. goto err;
  2049. }
  2050. /*
  2051. * BIO_should_retry(...) can just be ignored here. The
  2052. * library expects us to call BIO_write with the same
  2053. * arguments again, and that's what we will do in the
  2054. * next iteration.
  2055. */
  2056. } else if (r == 0) {
  2057. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  2058. goto err;
  2059. } else {
  2060. if (debug)
  2061. printf("client wrote %d\n", r);
  2062. cw_num -= r;
  2063. }
  2064. }
  2065. if (cr_num > 0) {
  2066. /* Read from server. */
  2067. r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
  2068. if (r < 0) {
  2069. if (!BIO_should_retry(c_ssl_bio)) {
  2070. fprintf(stderr, "ERROR in CLIENT\n");
  2071. err_in_client = 1;
  2072. goto err;
  2073. }
  2074. /*
  2075. * Again, "BIO_should_retry" can be ignored.
  2076. */
  2077. } else if (r == 0) {
  2078. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  2079. goto err;
  2080. } else {
  2081. if (debug)
  2082. printf("client read %d\n", r);
  2083. cr_num -= r;
  2084. }
  2085. }
  2086. /*
  2087. * c_time and s_time increments will typically be very small
  2088. * (depending on machine speed and clock tick intervals), but
  2089. * sampling over a large number of connections should result in
  2090. * fairly accurate figures. We cannot guarantee a lot, however
  2091. * -- if each connection lasts for exactly one clock tick, it
  2092. * will be counted only for the client or only for the server or
  2093. * even not at all.
  2094. */
  2095. *c_time += (clock() - c_clock);
  2096. }
  2097. {
  2098. /* SERVER */
  2099. char sbuf[1024 * 8];
  2100. int i, r;
  2101. clock_t s_clock = clock();
  2102. memset(sbuf, 0, sizeof(sbuf));
  2103. if (debug)
  2104. if (SSL_in_init(s_ssl))
  2105. printf("server waiting in SSL_accept - %s\n",
  2106. SSL_state_string_long(s_ssl));
  2107. if (sw_num > 0) {
  2108. /* Write to client. */
  2109. if (sw_num > (long)sizeof sbuf)
  2110. i = sizeof sbuf;
  2111. else
  2112. i = (int)sw_num;
  2113. r = BIO_write(s_ssl_bio, sbuf, i);
  2114. if (r < 0) {
  2115. if (!BIO_should_retry(s_ssl_bio)) {
  2116. fprintf(stderr, "ERROR in SERVER\n");
  2117. err_in_server = 1;
  2118. goto err;
  2119. }
  2120. /* Ignore "BIO_should_retry". */
  2121. } else if (r == 0) {
  2122. fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
  2123. goto err;
  2124. } else {
  2125. if (debug)
  2126. printf("server wrote %d\n", r);
  2127. sw_num -= r;
  2128. }
  2129. }
  2130. if (sr_num > 0) {
  2131. /* Read from client. */
  2132. r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
  2133. if (r < 0) {
  2134. if (!BIO_should_retry(s_ssl_bio)) {
  2135. fprintf(stderr, "ERROR in SERVER\n");
  2136. err_in_server = 1;
  2137. goto err;
  2138. }
  2139. /* blah, blah */
  2140. } else if (r == 0) {
  2141. fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
  2142. goto err;
  2143. } else {
  2144. if (debug)
  2145. printf("server read %d\n", r);
  2146. sr_num -= r;
  2147. }
  2148. }
  2149. *s_time += (clock() - s_clock);
  2150. }
  2151. {
  2152. /* "I/O" BETWEEN CLIENT AND SERVER. */
  2153. size_t r1, r2;
  2154. BIO *io1 = server_io, *io2 = client_io;
  2155. /*
  2156. * we use the non-copying interface for io1 and the standard
  2157. * BIO_write/BIO_read interface for io2
  2158. */
  2159. static int prev_progress = 1;
  2160. int progress = 0;
  2161. /* io1 to io2 */
  2162. do {
  2163. size_t num;
  2164. int r;
  2165. r1 = BIO_ctrl_pending(io1);
  2166. r2 = BIO_ctrl_get_write_guarantee(io2);
  2167. num = r1;
  2168. if (r2 < num)
  2169. num = r2;
  2170. if (num) {
  2171. char *dataptr;
  2172. if (INT_MAX < num) /* yeah, right */
  2173. num = INT_MAX;
  2174. r = BIO_nread(io1, &dataptr, (int)num);
  2175. assert(r > 0);
  2176. assert(r <= (int)num);
  2177. /*
  2178. * possibly r < num (non-contiguous data)
  2179. */
  2180. num = r;
  2181. r = BIO_write(io2, dataptr, (int)num);
  2182. if (r != (int)num) { /* can't happen */
  2183. fprintf(stderr, "ERROR: BIO_write could not write "
  2184. "BIO_ctrl_get_write_guarantee() bytes");
  2185. goto err;
  2186. }
  2187. progress = 1;
  2188. if (debug)
  2189. printf((io1 == client_io) ?
  2190. "C->S relaying: %d bytes\n" :
  2191. "S->C relaying: %d bytes\n", (int)num);
  2192. }
  2193. }
  2194. while (r1 && r2);
  2195. /* io2 to io1 */
  2196. {
  2197. size_t num;
  2198. int r;
  2199. r1 = BIO_ctrl_pending(io2);
  2200. r2 = BIO_ctrl_get_read_request(io1);
  2201. /*
  2202. * here we could use ..._get_write_guarantee instead of
  2203. * ..._get_read_request, but by using the latter we test
  2204. * restartability of the SSL implementation more thoroughly
  2205. */
  2206. num = r1;
  2207. if (r2 < num)
  2208. num = r2;
  2209. if (num) {
  2210. char *dataptr;
  2211. if (INT_MAX < num)
  2212. num = INT_MAX;
  2213. if (num > 1)
  2214. --num; /* test restartability even more thoroughly */
  2215. r = BIO_nwrite0(io1, &dataptr);
  2216. assert(r > 0);
  2217. if (r < (int)num)
  2218. num = r;
  2219. r = BIO_read(io2, dataptr, (int)num);
  2220. if (r != (int)num) { /* can't happen */
  2221. fprintf(stderr, "ERROR: BIO_read could not read "
  2222. "BIO_ctrl_pending() bytes");
  2223. goto err;
  2224. }
  2225. progress = 1;
  2226. r = BIO_nwrite(io1, &dataptr, (int)num);
  2227. if (r != (int)num) { /* can't happen */
  2228. fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
  2229. "BIO_nwrite0() bytes");
  2230. goto err;
  2231. }
  2232. if (debug)
  2233. printf((io2 == client_io) ?
  2234. "C->S relaying: %d bytes\n" :
  2235. "S->C relaying: %d bytes\n", (int)num);
  2236. }
  2237. } /* no loop, BIO_ctrl_get_read_request now
  2238. * returns 0 anyway */
  2239. if (!progress && !prev_progress)
  2240. if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) {
  2241. fprintf(stderr, "ERROR: got stuck\n");
  2242. fprintf(stderr, " ERROR.\n");
  2243. goto err;
  2244. }
  2245. prev_progress = progress;
  2246. }
  2247. }
  2248. while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
  2249. if (verbose)
  2250. print_details(c_ssl, "DONE via BIO pair: ");
  2251. #ifndef OPENSSL_NO_NEXTPROTONEG
  2252. if (verify_npn(c_ssl, s_ssl) < 0) {
  2253. ret = 1;
  2254. goto end;
  2255. }
  2256. #endif
  2257. if (verify_serverinfo() < 0) {
  2258. fprintf(stderr, "Server info verify error\n");
  2259. ret = 1;
  2260. goto err;
  2261. }
  2262. if (verify_alpn(c_ssl, s_ssl) < 0) {
  2263. ret = 1;
  2264. goto err;
  2265. }
  2266. if (custom_ext_error) {
  2267. fprintf(stderr, "Custom extension error\n");
  2268. ret = 1;
  2269. goto err;
  2270. }
  2271. end:
  2272. ret = 0;
  2273. err:
  2274. ERR_print_errors(bio_err);
  2275. BIO_free(server);
  2276. BIO_free(server_io);
  2277. BIO_free(client);
  2278. BIO_free(client_io);
  2279. BIO_free(s_ssl_bio);
  2280. BIO_free(c_ssl_bio);
  2281. if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
  2282. ret = (err_in_client != 0) ? 0 : 1;
  2283. else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
  2284. ret = (err_in_server != 0) ? 0 : 1;
  2285. return ret;
  2286. }
  2287. #define W_READ 1
  2288. #define W_WRITE 2
  2289. #define C_DONE 1
  2290. #define S_DONE 2
  2291. int doit(SSL *s_ssl, SSL *c_ssl, long count)
  2292. {
  2293. char *cbuf = NULL, *sbuf = NULL;
  2294. long bufsiz;
  2295. long cw_num = count, cr_num = count;
  2296. long sw_num = count, sr_num = count;
  2297. int ret = 1;
  2298. BIO *c_to_s = NULL;
  2299. BIO *s_to_c = NULL;
  2300. BIO *c_bio = NULL;
  2301. BIO *s_bio = NULL;
  2302. int c_r, c_w, s_r, s_w;
  2303. int i, j;
  2304. int done = 0;
  2305. int c_write, s_write;
  2306. int do_server = 0, do_client = 0;
  2307. int max_frag = 5 * 1024;
  2308. int err_in_client = 0;
  2309. int err_in_server = 0;
  2310. bufsiz = count > 40 * 1024 ? 40 * 1024 : count;
  2311. if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL)
  2312. goto err;
  2313. if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL)
  2314. goto err;
  2315. c_to_s = BIO_new(BIO_s_mem());
  2316. s_to_c = BIO_new(BIO_s_mem());
  2317. if ((s_to_c == NULL) || (c_to_s == NULL)) {
  2318. ERR_print_errors(bio_err);
  2319. goto err;
  2320. }
  2321. c_bio = BIO_new(BIO_f_ssl());
  2322. s_bio = BIO_new(BIO_f_ssl());
  2323. if ((c_bio == NULL) || (s_bio == NULL)) {
  2324. ERR_print_errors(bio_err);
  2325. goto err;
  2326. }
  2327. SSL_set_connect_state(c_ssl);
  2328. SSL_set_bio(c_ssl, s_to_c, c_to_s);
  2329. SSL_set_max_send_fragment(c_ssl, max_frag);
  2330. BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE);
  2331. SSL_set_accept_state(s_ssl);
  2332. SSL_set_bio(s_ssl, c_to_s, s_to_c);
  2333. SSL_set_max_send_fragment(s_ssl, max_frag);
  2334. BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE);
  2335. c_r = 0;
  2336. s_r = 1;
  2337. c_w = 1;
  2338. s_w = 0;
  2339. c_write = 1, s_write = 0;
  2340. /* We can always do writes */
  2341. for (;;) {
  2342. do_server = 0;
  2343. do_client = 0;
  2344. i = (int)BIO_pending(s_bio);
  2345. if ((i && s_r) || s_w)
  2346. do_server = 1;
  2347. i = (int)BIO_pending(c_bio);
  2348. if ((i && c_r) || c_w)
  2349. do_client = 1;
  2350. if (do_server && debug) {
  2351. if (SSL_in_init(s_ssl))
  2352. printf("server waiting in SSL_accept - %s\n",
  2353. SSL_state_string_long(s_ssl));
  2354. /*-
  2355. else if (s_write)
  2356. printf("server:SSL_write()\n");
  2357. else
  2358. printf("server:SSL_read()\n"); */
  2359. }
  2360. if (do_client && debug) {
  2361. if (SSL_in_init(c_ssl))
  2362. printf("client waiting in SSL_connect - %s\n",
  2363. SSL_state_string_long(c_ssl));
  2364. /*-
  2365. else if (c_write)
  2366. printf("client:SSL_write()\n");
  2367. else
  2368. printf("client:SSL_read()\n"); */
  2369. }
  2370. if (!do_client && !do_server) {
  2371. fprintf(stdout, "ERROR IN STARTUP\n");
  2372. ERR_print_errors(bio_err);
  2373. goto err;
  2374. }
  2375. if (do_client && !(done & C_DONE)) {
  2376. if (c_write) {
  2377. j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num;
  2378. i = BIO_write(c_bio, cbuf, j);
  2379. if (i < 0) {
  2380. c_r = 0;
  2381. c_w = 0;
  2382. if (BIO_should_retry(c_bio)) {
  2383. if (BIO_should_read(c_bio))
  2384. c_r = 1;
  2385. if (BIO_should_write(c_bio))
  2386. c_w = 1;
  2387. } else {
  2388. fprintf(stderr, "ERROR in CLIENT\n");
  2389. err_in_client = 1;
  2390. ERR_print_errors(bio_err);
  2391. goto err;
  2392. }
  2393. } else if (i == 0) {
  2394. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  2395. goto err;
  2396. } else {
  2397. if (debug)
  2398. printf("client wrote %d\n", i);
  2399. /* ok */
  2400. s_r = 1;
  2401. c_write = 0;
  2402. cw_num -= i;
  2403. if (max_frag > 1029)
  2404. SSL_set_max_send_fragment(c_ssl, max_frag -= 5);
  2405. }
  2406. } else {
  2407. i = BIO_read(c_bio, cbuf, bufsiz);
  2408. if (i < 0) {
  2409. c_r = 0;
  2410. c_w = 0;
  2411. if (BIO_should_retry(c_bio)) {
  2412. if (BIO_should_read(c_bio))
  2413. c_r = 1;
  2414. if (BIO_should_write(c_bio))
  2415. c_w = 1;
  2416. } else {
  2417. fprintf(stderr, "ERROR in CLIENT\n");
  2418. err_in_client = 1;
  2419. ERR_print_errors(bio_err);
  2420. goto err;
  2421. }
  2422. } else if (i == 0) {
  2423. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  2424. goto err;
  2425. } else {
  2426. if (debug)
  2427. printf("client read %d\n", i);
  2428. cr_num -= i;
  2429. if (sw_num > 0) {
  2430. s_write = 1;
  2431. s_w = 1;
  2432. }
  2433. if (cr_num <= 0) {
  2434. s_write = 1;
  2435. s_w = 1;
  2436. done = S_DONE | C_DONE;
  2437. }
  2438. }
  2439. }
  2440. }
  2441. if (do_server && !(done & S_DONE)) {
  2442. if (!s_write) {
  2443. i = BIO_read(s_bio, sbuf, bufsiz);
  2444. if (i < 0) {
  2445. s_r = 0;
  2446. s_w = 0;
  2447. if (BIO_should_retry(s_bio)) {
  2448. if (BIO_should_read(s_bio))
  2449. s_r = 1;
  2450. if (BIO_should_write(s_bio))
  2451. s_w = 1;
  2452. } else {
  2453. fprintf(stderr, "ERROR in SERVER\n");
  2454. err_in_server = 1;
  2455. ERR_print_errors(bio_err);
  2456. goto err;
  2457. }
  2458. } else if (i == 0) {
  2459. ERR_print_errors(bio_err);
  2460. fprintf(stderr,
  2461. "SSL SERVER STARTUP FAILED in SSL_read\n");
  2462. goto err;
  2463. } else {
  2464. if (debug)
  2465. printf("server read %d\n", i);
  2466. sr_num -= i;
  2467. if (cw_num > 0) {
  2468. c_write = 1;
  2469. c_w = 1;
  2470. }
  2471. if (sr_num <= 0) {
  2472. s_write = 1;
  2473. s_w = 1;
  2474. c_write = 0;
  2475. }
  2476. }
  2477. } else {
  2478. j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num;
  2479. i = BIO_write(s_bio, sbuf, j);
  2480. if (i < 0) {
  2481. s_r = 0;
  2482. s_w = 0;
  2483. if (BIO_should_retry(s_bio)) {
  2484. if (BIO_should_read(s_bio))
  2485. s_r = 1;
  2486. if (BIO_should_write(s_bio))
  2487. s_w = 1;
  2488. } else {
  2489. fprintf(stderr, "ERROR in SERVER\n");
  2490. err_in_server = 1;
  2491. ERR_print_errors(bio_err);
  2492. goto err;
  2493. }
  2494. } else if (i == 0) {
  2495. ERR_print_errors(bio_err);
  2496. fprintf(stderr,
  2497. "SSL SERVER STARTUP FAILED in SSL_write\n");
  2498. goto err;
  2499. } else {
  2500. if (debug)
  2501. printf("server wrote %d\n", i);
  2502. sw_num -= i;
  2503. s_write = 0;
  2504. c_r = 1;
  2505. if (sw_num <= 0)
  2506. done |= S_DONE;
  2507. if (max_frag > 1029)
  2508. SSL_set_max_send_fragment(s_ssl, max_frag -= 5);
  2509. }
  2510. }
  2511. }
  2512. if ((done & S_DONE) && (done & C_DONE))
  2513. break;
  2514. }
  2515. if (verbose)
  2516. print_details(c_ssl, "DONE: ");
  2517. #ifndef OPENSSL_NO_NEXTPROTONEG
  2518. if (verify_npn(c_ssl, s_ssl) < 0) {
  2519. ret = 1;
  2520. goto err;
  2521. }
  2522. #endif
  2523. if (verify_serverinfo() < 0) {
  2524. fprintf(stderr, "Server info verify error\n");
  2525. ret = 1;
  2526. goto err;
  2527. }
  2528. if (custom_ext_error) {
  2529. fprintf(stderr, "Custom extension error\n");
  2530. ret = 1;
  2531. goto err;
  2532. }
  2533. ret = 0;
  2534. err:
  2535. /*
  2536. * We have to set the BIO's to NULL otherwise they will be
  2537. * OPENSSL_free()ed twice. Once when th s_ssl is SSL_free()ed and again
  2538. * when c_ssl is SSL_free()ed. This is a hack required because s_ssl and
  2539. * c_ssl are sharing the same BIO structure and SSL_set_bio() and
  2540. * SSL_free() automatically BIO_free non NULL entries. You should not
  2541. * normally do this or be required to do this
  2542. */
  2543. if (s_ssl != NULL) {
  2544. s_ssl->rbio = NULL;
  2545. s_ssl->wbio = NULL;
  2546. }
  2547. if (c_ssl != NULL) {
  2548. c_ssl->rbio = NULL;
  2549. c_ssl->wbio = NULL;
  2550. }
  2551. BIO_free(c_to_s);
  2552. BIO_free(s_to_c);
  2553. BIO_free_all(c_bio);
  2554. BIO_free_all(s_bio);
  2555. OPENSSL_free(cbuf);
  2556. OPENSSL_free(sbuf);
  2557. if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
  2558. ret = (err_in_client != 0) ? 0 : 1;
  2559. else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
  2560. ret = (err_in_server != 0) ? 0 : 1;
  2561. return (ret);
  2562. }
  2563. static int get_proxy_auth_ex_data_idx(void)
  2564. {
  2565. static volatile int idx = -1;
  2566. if (idx < 0) {
  2567. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  2568. if (idx < 0) {
  2569. idx = X509_STORE_CTX_get_ex_new_index(0,
  2570. "SSLtest for verify callback",
  2571. NULL, NULL, NULL);
  2572. }
  2573. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  2574. }
  2575. return idx;
  2576. }
  2577. static int verify_callback(int ok, X509_STORE_CTX *ctx)
  2578. {
  2579. char *s, buf[256];
  2580. s = X509_NAME_oneline(X509_get_subject_name(ctx->current_cert), buf,
  2581. sizeof buf);
  2582. if (s != NULL) {
  2583. if (ok)
  2584. printf("depth=%d %s\n", ctx->error_depth, buf);
  2585. else {
  2586. fprintf(stderr, "depth=%d error=%d %s\n",
  2587. ctx->error_depth, ctx->error, buf);
  2588. }
  2589. }
  2590. if (ok == 0) {
  2591. switch (ctx->error) {
  2592. default:
  2593. fprintf(stderr, "Error string: %s\n",
  2594. X509_verify_cert_error_string(ctx->error));
  2595. break;
  2596. case X509_V_ERR_CERT_NOT_YET_VALID:
  2597. case X509_V_ERR_CERT_HAS_EXPIRED:
  2598. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  2599. ok = 1;
  2600. }
  2601. }
  2602. if (ok == 1) {
  2603. X509 *xs = ctx->current_cert;
  2604. if (X509_get_extension_flags(xs) & EXFLAG_PROXY) {
  2605. unsigned int *letters = X509_STORE_CTX_get_ex_data(ctx,
  2606. get_proxy_auth_ex_data_idx
  2607. ());
  2608. if (letters) {
  2609. int found_any = 0;
  2610. int i;
  2611. PROXY_CERT_INFO_EXTENSION *pci =
  2612. X509_get_ext_d2i(xs, NID_proxyCertInfo,
  2613. NULL, NULL);
  2614. switch (OBJ_obj2nid(pci->proxyPolicy->policyLanguage)) {
  2615. case NID_Independent:
  2616. /*
  2617. * Completely meaningless in this program, as there's no
  2618. * way to grant explicit rights to a specific PrC.
  2619. * Basically, using id-ppl-Independent is the perfect way
  2620. * to grant no rights at all.
  2621. */
  2622. fprintf(stderr, " Independent proxy certificate");
  2623. for (i = 0; i < 26; i++)
  2624. letters[i] = 0;
  2625. break;
  2626. case NID_id_ppl_inheritAll:
  2627. /*
  2628. * This is basically a NOP, we simply let the current
  2629. * rights stand as they are.
  2630. */
  2631. fprintf(stderr, " Proxy certificate inherits all");
  2632. break;
  2633. default:
  2634. s = (char *)
  2635. pci->proxyPolicy->policy->data;
  2636. i = pci->proxyPolicy->policy->length;
  2637. /*
  2638. * The algorithm works as follows: it is assumed that
  2639. * previous iterations or the initial granted rights has
  2640. * already set some elements of `letters'. What we need
  2641. * to do is to clear those that weren't granted by the
  2642. * current PrC as well. The easiest way to do this is to
  2643. * add 1 to all the elements whose letters are given with
  2644. * the current policy. That way, all elements that are
  2645. * set by the current policy and were already set by
  2646. * earlier policies and through the original grant of
  2647. * rights will get the value 2 or higher. The last thing
  2648. * to do is to sweep through `letters' and keep the
  2649. * elements having the value 2 as set, and clear all the
  2650. * others.
  2651. */
  2652. printf(" Certificate proxy rights = %*.*s", i,
  2653. i, s);
  2654. while (i-- > 0) {
  2655. int c = *s++;
  2656. if (isascii(c) && isalpha(c)) {
  2657. if (islower(c))
  2658. c = toupper(c);
  2659. letters[c - 'A']++;
  2660. }
  2661. }
  2662. for (i = 0; i < 26; i++)
  2663. if (letters[i] < 2)
  2664. letters[i] = 0;
  2665. else
  2666. letters[i] = 1;
  2667. }
  2668. found_any = 0;
  2669. printf(", resulting proxy rights = ");
  2670. for (i = 0; i < 26; i++)
  2671. if (letters[i]) {
  2672. printf("%c", i + 'A');
  2673. found_any = 1;
  2674. }
  2675. if (!found_any)
  2676. printf("none");
  2677. printf("\n");
  2678. PROXY_CERT_INFO_EXTENSION_free(pci);
  2679. }
  2680. }
  2681. }
  2682. return (ok);
  2683. }
  2684. static void process_proxy_debug(int indent, const char *format, ...)
  2685. {
  2686. /* That's 80 > */
  2687. static const char indentation[] =
  2688. ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
  2689. ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
  2690. char my_format[256];
  2691. va_list args;
  2692. BIO_snprintf(my_format, sizeof(my_format), "%*.*s %s",
  2693. indent, indent, indentation, format);
  2694. va_start(args, format);
  2695. vfprintf(stderr, my_format, args);
  2696. va_end(args);
  2697. }
  2698. /*-
  2699. * Priority levels:
  2700. * 0 [!]var, ()
  2701. * 1 & ^
  2702. * 2 |
  2703. */
  2704. static int process_proxy_cond_adders(unsigned int letters[26],
  2705. const char *cond, const char **cond_end,
  2706. int *pos, int indent);
  2707. static int process_proxy_cond_val(unsigned int letters[26], const char *cond,
  2708. const char **cond_end, int *pos, int indent)
  2709. {
  2710. int c;
  2711. int ok = 1;
  2712. int negate = 0;
  2713. while (isspace((int)*cond)) {
  2714. cond++;
  2715. (*pos)++;
  2716. }
  2717. c = *cond;
  2718. if (debug)
  2719. process_proxy_debug(indent,
  2720. "Start process_proxy_cond_val at position %d: %s\n",
  2721. *pos, cond);
  2722. while (c == '!') {
  2723. negate = !negate;
  2724. cond++;
  2725. (*pos)++;
  2726. while (isspace((int)*cond)) {
  2727. cond++;
  2728. (*pos)++;
  2729. }
  2730. c = *cond;
  2731. }
  2732. if (c == '(') {
  2733. cond++;
  2734. (*pos)++;
  2735. ok = process_proxy_cond_adders(letters, cond, cond_end, pos,
  2736. indent + 1);
  2737. cond = *cond_end;
  2738. if (ok < 0)
  2739. goto end;
  2740. while (isspace((int)*cond)) {
  2741. cond++;
  2742. (*pos)++;
  2743. }
  2744. c = *cond;
  2745. if (c != ')') {
  2746. fprintf(stderr,
  2747. "Weird condition character in position %d: "
  2748. "%c\n", *pos, c);
  2749. ok = -1;
  2750. goto end;
  2751. }
  2752. cond++;
  2753. (*pos)++;
  2754. } else if (isascii(c) && isalpha(c)) {
  2755. if (islower(c))
  2756. c = toupper(c);
  2757. ok = letters[c - 'A'];
  2758. cond++;
  2759. (*pos)++;
  2760. } else {
  2761. fprintf(stderr,
  2762. "Weird condition character in position %d: " "%c\n", *pos, c);
  2763. ok = -1;
  2764. goto end;
  2765. }
  2766. end:
  2767. *cond_end = cond;
  2768. if (ok >= 0 && negate)
  2769. ok = !ok;
  2770. if (debug)
  2771. process_proxy_debug(indent,
  2772. "End process_proxy_cond_val at position %d: %s, returning %d\n",
  2773. *pos, cond, ok);
  2774. return ok;
  2775. }
  2776. static int process_proxy_cond_multipliers(unsigned int letters[26],
  2777. const char *cond,
  2778. const char **cond_end, int *pos,
  2779. int indent)
  2780. {
  2781. int ok;
  2782. char c;
  2783. if (debug)
  2784. process_proxy_debug(indent,
  2785. "Start process_proxy_cond_multipliers at position %d: %s\n",
  2786. *pos, cond);
  2787. ok = process_proxy_cond_val(letters, cond, cond_end, pos, indent + 1);
  2788. cond = *cond_end;
  2789. if (ok < 0)
  2790. goto end;
  2791. while (ok >= 0) {
  2792. while (isspace((int)*cond)) {
  2793. cond++;
  2794. (*pos)++;
  2795. }
  2796. c = *cond;
  2797. switch (c) {
  2798. case '&':
  2799. case '^':
  2800. {
  2801. int save_ok = ok;
  2802. cond++;
  2803. (*pos)++;
  2804. ok = process_proxy_cond_val(letters,
  2805. cond, cond_end, pos, indent + 1);
  2806. cond = *cond_end;
  2807. if (ok < 0)
  2808. break;
  2809. switch (c) {
  2810. case '&':
  2811. ok &= save_ok;
  2812. break;
  2813. case '^':
  2814. ok ^= save_ok;
  2815. break;
  2816. default:
  2817. fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!"
  2818. " STOPPING\n");
  2819. EXIT(1);
  2820. }
  2821. }
  2822. break;
  2823. default:
  2824. goto end;
  2825. }
  2826. }
  2827. end:
  2828. if (debug)
  2829. process_proxy_debug(indent,
  2830. "End process_proxy_cond_multipliers at position %d: %s, returning %d\n",
  2831. *pos, cond, ok);
  2832. *cond_end = cond;
  2833. return ok;
  2834. }
  2835. static int process_proxy_cond_adders(unsigned int letters[26],
  2836. const char *cond, const char **cond_end,
  2837. int *pos, int indent)
  2838. {
  2839. int ok;
  2840. char c;
  2841. if (debug)
  2842. process_proxy_debug(indent,
  2843. "Start process_proxy_cond_adders at position %d: %s\n",
  2844. *pos, cond);
  2845. ok = process_proxy_cond_multipliers(letters, cond, cond_end, pos,
  2846. indent + 1);
  2847. cond = *cond_end;
  2848. if (ok < 0)
  2849. goto end;
  2850. while (ok >= 0) {
  2851. while (isspace((int)*cond)) {
  2852. cond++;
  2853. (*pos)++;
  2854. }
  2855. c = *cond;
  2856. switch (c) {
  2857. case '|':
  2858. {
  2859. int save_ok = ok;
  2860. cond++;
  2861. (*pos)++;
  2862. ok = process_proxy_cond_multipliers(letters,
  2863. cond, cond_end, pos,
  2864. indent + 1);
  2865. cond = *cond_end;
  2866. if (ok < 0)
  2867. break;
  2868. switch (c) {
  2869. case '|':
  2870. ok |= save_ok;
  2871. break;
  2872. default:
  2873. fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!"
  2874. " STOPPING\n");
  2875. EXIT(1);
  2876. }
  2877. }
  2878. break;
  2879. default:
  2880. goto end;
  2881. }
  2882. }
  2883. end:
  2884. if (debug)
  2885. process_proxy_debug(indent,
  2886. "End process_proxy_cond_adders at position %d: %s, returning %d\n",
  2887. *pos, cond, ok);
  2888. *cond_end = cond;
  2889. return ok;
  2890. }
  2891. static int process_proxy_cond(unsigned int letters[26],
  2892. const char *cond, const char **cond_end)
  2893. {
  2894. int pos = 1;
  2895. return process_proxy_cond_adders(letters, cond, cond_end, &pos, 1);
  2896. }
  2897. static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)
  2898. {
  2899. int ok = 1;
  2900. struct app_verify_arg *cb_arg = arg;
  2901. unsigned int letters[26]; /* only used with proxy_auth */
  2902. if (cb_arg->app_verify) {
  2903. char *s = NULL, buf[256];
  2904. printf("In app_verify_callback, allowing cert. ");
  2905. printf("Arg is: %s\n", cb_arg->string);
  2906. printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
  2907. (void *)ctx, (void *)ctx->cert);
  2908. if (ctx->cert)
  2909. s = X509_NAME_oneline(X509_get_subject_name(ctx->cert), buf, 256);
  2910. if (s != NULL) {
  2911. printf("cert depth=%d %s\n", ctx->error_depth, buf);
  2912. }
  2913. return (1);
  2914. }
  2915. if (cb_arg->proxy_auth) {
  2916. int found_any = 0, i;
  2917. char *sp;
  2918. for (i = 0; i < 26; i++)
  2919. letters[i] = 0;
  2920. for (sp = cb_arg->proxy_auth; *sp; sp++) {
  2921. int c = *sp;
  2922. if (isascii(c) && isalpha(c)) {
  2923. if (islower(c))
  2924. c = toupper(c);
  2925. letters[c - 'A'] = 1;
  2926. }
  2927. }
  2928. printf(" Initial proxy rights = ");
  2929. for (i = 0; i < 26; i++)
  2930. if (letters[i]) {
  2931. printf("%c", i + 'A');
  2932. found_any = 1;
  2933. }
  2934. if (!found_any)
  2935. printf("none");
  2936. printf("\n");
  2937. X509_STORE_CTX_set_ex_data(ctx,
  2938. get_proxy_auth_ex_data_idx(), letters);
  2939. }
  2940. if (cb_arg->allow_proxy_certs) {
  2941. X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS);
  2942. }
  2943. ok = X509_verify_cert(ctx);
  2944. if (cb_arg->proxy_auth) {
  2945. if (ok > 0) {
  2946. const char *cond_end = NULL;
  2947. ok = process_proxy_cond(letters, cb_arg->proxy_cond, &cond_end);
  2948. if (ok < 0)
  2949. EXIT(3);
  2950. if (*cond_end) {
  2951. fprintf(stderr,
  2952. "Stopped processing condition before it's end.\n");
  2953. ok = 0;
  2954. }
  2955. if (!ok)
  2956. fprintf(stderr,
  2957. "Proxy rights check with condition '%s' invalid\n",
  2958. cb_arg->proxy_cond);
  2959. else
  2960. printf("Proxy rights check with condition '%s' ok\n",
  2961. cb_arg->proxy_cond);
  2962. }
  2963. }
  2964. return (ok);
  2965. }
  2966. #ifndef OPENSSL_NO_DH
  2967. /*-
  2968. * These DH parameters have been generated as follows:
  2969. * $ openssl dhparam -C -noout 512
  2970. * $ openssl dhparam -C -noout 1024
  2971. * $ openssl dhparam -C -noout -dsaparam 1024
  2972. * (The third function has been renamed to avoid name conflicts.)
  2973. */
  2974. static DH *get_dh512()
  2975. {
  2976. static unsigned char dh512_p[] = {
  2977. 0xCB, 0xC8, 0xE1, 0x86, 0xD0, 0x1F, 0x94, 0x17, 0xA6, 0x99, 0xF0,
  2978. 0xC6,
  2979. 0x1F, 0x0D, 0xAC, 0xB6, 0x25, 0x3E, 0x06, 0x39, 0xCA, 0x72, 0x04,
  2980. 0xB0,
  2981. 0x6E, 0xDA, 0xC0, 0x61, 0xE6, 0x7A, 0x77, 0x25, 0xE8, 0x3B, 0xB9,
  2982. 0x5F,
  2983. 0x9A, 0xB6, 0xB5, 0xFE, 0x99, 0x0B, 0xA1, 0x93, 0x4E, 0x35, 0x33,
  2984. 0xB8,
  2985. 0xE1, 0xF1, 0x13, 0x4F, 0x59, 0x1A, 0xD2, 0x57, 0xC0, 0x26, 0x21,
  2986. 0x33,
  2987. 0x02, 0xC5, 0xAE, 0x23,
  2988. };
  2989. static unsigned char dh512_g[] = {
  2990. 0x02,
  2991. };
  2992. DH *dh;
  2993. if ((dh = DH_new()) == NULL)
  2994. return (NULL);
  2995. dh->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL);
  2996. dh->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL);
  2997. if ((dh->p == NULL) || (dh->g == NULL)) {
  2998. DH_free(dh);
  2999. return (NULL);
  3000. }
  3001. return (dh);
  3002. }
  3003. static DH *get_dh1024()
  3004. {
  3005. static unsigned char dh1024_p[] = {
  3006. 0xF8, 0x81, 0x89, 0x7D, 0x14, 0x24, 0xC5, 0xD1, 0xE6, 0xF7, 0xBF,
  3007. 0x3A,
  3008. 0xE4, 0x90, 0xF4, 0xFC, 0x73, 0xFB, 0x34, 0xB5, 0xFA, 0x4C, 0x56,
  3009. 0xA2,
  3010. 0xEA, 0xA7, 0xE9, 0xC0, 0xC0, 0xCE, 0x89, 0xE1, 0xFA, 0x63, 0x3F,
  3011. 0xB0,
  3012. 0x6B, 0x32, 0x66, 0xF1, 0xD1, 0x7B, 0xB0, 0x00, 0x8F, 0xCA, 0x87,
  3013. 0xC2,
  3014. 0xAE, 0x98, 0x89, 0x26, 0x17, 0xC2, 0x05, 0xD2, 0xEC, 0x08, 0xD0,
  3015. 0x8C,
  3016. 0xFF, 0x17, 0x52, 0x8C, 0xC5, 0x07, 0x93, 0x03, 0xB1, 0xF6, 0x2F,
  3017. 0xB8,
  3018. 0x1C, 0x52, 0x47, 0x27, 0x1B, 0xDB, 0xD1, 0x8D, 0x9D, 0x69, 0x1D,
  3019. 0x52,
  3020. 0x4B, 0x32, 0x81, 0xAA, 0x7F, 0x00, 0xC8, 0xDC, 0xE6, 0xD9, 0xCC,
  3021. 0xC1,
  3022. 0x11, 0x2D, 0x37, 0x34, 0x6C, 0xEA, 0x02, 0x97, 0x4B, 0x0E, 0xBB,
  3023. 0xB1,
  3024. 0x71, 0x33, 0x09, 0x15, 0xFD, 0xDD, 0x23, 0x87, 0x07, 0x5E, 0x89,
  3025. 0xAB,
  3026. 0x6B, 0x7C, 0x5F, 0xEC, 0xA6, 0x24, 0xDC, 0x53,
  3027. };
  3028. static unsigned char dh1024_g[] = {
  3029. 0x02,
  3030. };
  3031. DH *dh;
  3032. if ((dh = DH_new()) == NULL)
  3033. return (NULL);
  3034. dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
  3035. dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
  3036. if ((dh->p == NULL) || (dh->g == NULL)) {
  3037. DH_free(dh);
  3038. return (NULL);
  3039. }
  3040. return (dh);
  3041. }
  3042. static DH *get_dh1024dsa()
  3043. {
  3044. static unsigned char dh1024_p[] = {
  3045. 0xC8, 0x00, 0xF7, 0x08, 0x07, 0x89, 0x4D, 0x90, 0x53, 0xF3, 0xD5,
  3046. 0x00,
  3047. 0x21, 0x1B, 0xF7, 0x31, 0xA6, 0xA2, 0xDA, 0x23, 0x9A, 0xC7, 0x87,
  3048. 0x19,
  3049. 0x3B, 0x47, 0xB6, 0x8C, 0x04, 0x6F, 0xFF, 0xC6, 0x9B, 0xB8, 0x65,
  3050. 0xD2,
  3051. 0xC2, 0x5F, 0x31, 0x83, 0x4A, 0xA7, 0x5F, 0x2F, 0x88, 0x38, 0xB6,
  3052. 0x55,
  3053. 0xCF, 0xD9, 0x87, 0x6D, 0x6F, 0x9F, 0xDA, 0xAC, 0xA6, 0x48, 0xAF,
  3054. 0xFC,
  3055. 0x33, 0x84, 0x37, 0x5B, 0x82, 0x4A, 0x31, 0x5D, 0xE7, 0xBD, 0x52,
  3056. 0x97,
  3057. 0xA1, 0x77, 0xBF, 0x10, 0x9E, 0x37, 0xEA, 0x64, 0xFA, 0xCA, 0x28,
  3058. 0x8D,
  3059. 0x9D, 0x3B, 0xD2, 0x6E, 0x09, 0x5C, 0x68, 0xC7, 0x45, 0x90, 0xFD,
  3060. 0xBB,
  3061. 0x70, 0xC9, 0x3A, 0xBB, 0xDF, 0xD4, 0x21, 0x0F, 0xC4, 0x6A, 0x3C,
  3062. 0xF6,
  3063. 0x61, 0xCF, 0x3F, 0xD6, 0x13, 0xF1, 0x5F, 0xBC, 0xCF, 0xBC, 0x26,
  3064. 0x9E,
  3065. 0xBC, 0x0B, 0xBD, 0xAB, 0x5D, 0xC9, 0x54, 0x39,
  3066. };
  3067. static unsigned char dh1024_g[] = {
  3068. 0x3B, 0x40, 0x86, 0xE7, 0xF3, 0x6C, 0xDE, 0x67, 0x1C, 0xCC, 0x80,
  3069. 0x05,
  3070. 0x5A, 0xDF, 0xFE, 0xBD, 0x20, 0x27, 0x74, 0x6C, 0x24, 0xC9, 0x03,
  3071. 0xF3,
  3072. 0xE1, 0x8D, 0xC3, 0x7D, 0x98, 0x27, 0x40, 0x08, 0xB8, 0x8C, 0x6A,
  3073. 0xE9,
  3074. 0xBB, 0x1A, 0x3A, 0xD6, 0x86, 0x83, 0x5E, 0x72, 0x41, 0xCE, 0x85,
  3075. 0x3C,
  3076. 0xD2, 0xB3, 0xFC, 0x13, 0xCE, 0x37, 0x81, 0x9E, 0x4C, 0x1C, 0x7B,
  3077. 0x65,
  3078. 0xD3, 0xE6, 0xA6, 0x00, 0xF5, 0x5A, 0x95, 0x43, 0x5E, 0x81, 0xCF,
  3079. 0x60,
  3080. 0xA2, 0x23, 0xFC, 0x36, 0xA7, 0x5D, 0x7A, 0x4C, 0x06, 0x91, 0x6E,
  3081. 0xF6,
  3082. 0x57, 0xEE, 0x36, 0xCB, 0x06, 0xEA, 0xF5, 0x3D, 0x95, 0x49, 0xCB,
  3083. 0xA7,
  3084. 0xDD, 0x81, 0xDF, 0x80, 0x09, 0x4A, 0x97, 0x4D, 0xA8, 0x22, 0x72,
  3085. 0xA1,
  3086. 0x7F, 0xC4, 0x70, 0x56, 0x70, 0xE8, 0x20, 0x10, 0x18, 0x8F, 0x2E,
  3087. 0x60,
  3088. 0x07, 0xE7, 0x68, 0x1A, 0x82, 0x5D, 0x32, 0xA2,
  3089. };
  3090. DH *dh;
  3091. if ((dh = DH_new()) == NULL)
  3092. return (NULL);
  3093. dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
  3094. dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
  3095. if ((dh->p == NULL) || (dh->g == NULL)) {
  3096. DH_free(dh);
  3097. return (NULL);
  3098. }
  3099. dh->length = 160;
  3100. return (dh);
  3101. }
  3102. #endif
  3103. #ifndef OPENSSL_NO_PSK
  3104. /* convert the PSK key (psk_key) in ascii to binary (psk) */
  3105. static int psk_key2bn(const char *pskkey, unsigned char *psk,
  3106. unsigned int max_psk_len)
  3107. {
  3108. int ret;
  3109. BIGNUM *bn = NULL;
  3110. ret = BN_hex2bn(&bn, pskkey);
  3111. if (!ret) {
  3112. BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
  3113. pskkey);
  3114. BN_free(bn);
  3115. return 0;
  3116. }
  3117. if (BN_num_bytes(bn) > (int)max_psk_len) {
  3118. BIO_printf(bio_err,
  3119. "psk buffer of callback is too small (%d) for key (%d)\n",
  3120. max_psk_len, BN_num_bytes(bn));
  3121. BN_free(bn);
  3122. return 0;
  3123. }
  3124. ret = BN_bn2bin(bn, psk);
  3125. BN_free(bn);
  3126. return ret;
  3127. }
  3128. static unsigned int psk_client_callback(SSL *ssl, const char *hint,
  3129. char *identity,
  3130. unsigned int max_identity_len,
  3131. unsigned char *psk,
  3132. unsigned int max_psk_len)
  3133. {
  3134. int ret;
  3135. unsigned int psk_len = 0;
  3136. ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
  3137. if (ret < 0)
  3138. goto out_err;
  3139. if (debug)
  3140. fprintf(stderr, "client: created identity '%s' len=%d\n", identity,
  3141. ret);
  3142. ret = psk_key2bn(psk_key, psk, max_psk_len);
  3143. if (ret < 0)
  3144. goto out_err;
  3145. psk_len = ret;
  3146. out_err:
  3147. return psk_len;
  3148. }
  3149. static unsigned int psk_server_callback(SSL *ssl, const char *identity,
  3150. unsigned char *psk,
  3151. unsigned int max_psk_len)
  3152. {
  3153. unsigned int psk_len = 0;
  3154. if (strcmp(identity, "Client_identity") != 0) {
  3155. BIO_printf(bio_err, "server: PSK error: client identity not found\n");
  3156. return 0;
  3157. }
  3158. psk_len = psk_key2bn(psk_key, psk, max_psk_len);
  3159. return psk_len;
  3160. }
  3161. #endif
  3162. static int do_test_cipherlist(void)
  3163. {
  3164. #if !defined(OPENSSL_NO_SSL3) || !defined(OPENSSL_NO_TLS1)
  3165. int i = 0;
  3166. const SSL_METHOD *meth;
  3167. const SSL_CIPHER *ci, *tci = NULL;
  3168. #endif
  3169. #ifndef OPENSSL_NO_SSL3
  3170. meth = SSLv3_method();
  3171. tci = NULL;
  3172. while ((ci = meth->get_cipher(i++)) != NULL) {
  3173. if (tci != NULL)
  3174. if (ci->id >= tci->id) {
  3175. fprintf(stderr, "testing SSLv3 cipher list order: ");
  3176. fprintf(stderr, "failed %x vs. %x\n", ci->id, tci->id);
  3177. return 0;
  3178. }
  3179. tci = ci;
  3180. }
  3181. #endif
  3182. #ifndef OPENSSL_NO_TLS1
  3183. meth = TLSv1_method();
  3184. tci = NULL;
  3185. while ((ci = meth->get_cipher(i++)) != NULL) {
  3186. if (tci != NULL)
  3187. if (ci->id >= tci->id) {
  3188. fprintf(stderr, "testing TLSv1 cipher list order: ");
  3189. fprintf(stderr, "failed %x vs. %x\n", ci->id, tci->id);
  3190. return 0;
  3191. }
  3192. tci = ci;
  3193. }
  3194. #endif
  3195. return 1;
  3196. }