ssltest_old.c 103 KB

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