2
0

ssltest.c 109 KB

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