ssltest_old.c 101 KB

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