ssl_old_test.c 96 KB

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