ssltest_old.c 103 KB

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