ssl_old_test.c 97 KB

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