ssl_old_test.c 99 KB

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