ssltest_old.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045
  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 OpenSSL license (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include "e_os.h"
  12. /* Or gethostname won't be declared properly on Linux and GNU platforms. */
  13. #ifndef _BSD_SOURCE
  14. # define _BSD_SOURCE 1
  15. #endif
  16. #ifndef _DEFAULT_SOURCE
  17. # define _DEFAULT_SOURCE 1
  18. #endif
  19. #include <assert.h>
  20. #include <errno.h>
  21. #include <limits.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include "internal/nelem.h"
  27. #ifdef OPENSSL_SYS_VMS
  28. /*
  29. * Or isascii won't be declared properly on VMS (at least with DECompHP C).
  30. */
  31. # define _XOPEN_SOURCE 500
  32. #endif
  33. #include <ctype.h>
  34. #include <openssl/bio.h>
  35. #include <openssl/crypto.h>
  36. #include <openssl/evp.h>
  37. #include <openssl/x509.h>
  38. #include <openssl/x509v3.h>
  39. #include <openssl/ssl.h>
  40. #include <openssl/err.h>
  41. #include <openssl/rand.h>
  42. #ifndef OPENSSL_NO_RSA
  43. # include <openssl/rsa.h>
  44. #endif
  45. #ifndef OPENSSL_NO_DSA
  46. # include <openssl/dsa.h>
  47. #endif
  48. #ifndef OPENSSL_NO_DH
  49. # include <openssl/dh.h>
  50. #endif
  51. #include <openssl/bn.h>
  52. #ifndef OPENSSL_NO_CT
  53. # include <openssl/ct.h>
  54. #endif
  55. /*
  56. * Or gethostname won't be declared properly
  57. * on Compaq platforms (at least with DEC C).
  58. * Do not try to put it earlier, or IPv6 includes
  59. * get screwed...
  60. */
  61. #define _XOPEN_SOURCE_EXTENDED 1
  62. #ifdef OPENSSL_SYS_WINDOWS
  63. # include <winsock.h>
  64. #else
  65. # include OPENSSL_UNISTD
  66. #endif
  67. static SSL_CTX *s_ctx = NULL;
  68. static SSL_CTX *s_ctx2 = NULL;
  69. /*
  70. * There is really no standard for this, so let's assign something
  71. * only for this test
  72. */
  73. #define COMP_ZLIB 1
  74. static int verify_callback(int ok, X509_STORE_CTX *ctx);
  75. static int app_verify_callback(X509_STORE_CTX *ctx, void *arg);
  76. #define APP_CALLBACK_STRING "Test Callback Argument"
  77. struct app_verify_arg {
  78. char *string;
  79. int app_verify;
  80. };
  81. #ifndef OPENSSL_NO_DH
  82. static DH *get_dh512(void);
  83. static DH *get_dh1024(void);
  84. static DH *get_dh1024dsa(void);
  85. #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()
  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_server_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 = SSL3_VERSION;
  1230. max_version = TLS_MAX_VERSION;
  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 = DTLS_MIN_VERSION;
  1244. max_version = DTLS_MAX_VERSION;
  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 (!SSL_CTX_set_cipher_list(c_ctx, cipher)
  1277. || !SSL_CTX_set_cipher_list(s_ctx, cipher)
  1278. || !SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
  1279. ERR_print_errors(bio_err);
  1280. goto end;
  1281. }
  1282. }
  1283. if (ciphersuites != NULL) {
  1284. if (!SSL_CTX_set_ciphersuites(c_ctx, ciphersuites)
  1285. || !SSL_CTX_set_ciphersuites(s_ctx, ciphersuites)
  1286. || !SSL_CTX_set_ciphersuites(s_ctx2, ciphersuites)) {
  1287. ERR_print_errors(bio_err);
  1288. goto end;
  1289. }
  1290. }
  1291. #ifndef OPENSSL_NO_CT
  1292. if (ct_validation &&
  1293. !SSL_CTX_enable_ct(c_ctx, SSL_CT_VALIDATION_STRICT)) {
  1294. ERR_print_errors(bio_err);
  1295. goto end;
  1296. }
  1297. #endif
  1298. /* Process SSL_CONF arguments */
  1299. SSL_CONF_CTX_set_ssl_ctx(c_cctx, c_ctx);
  1300. SSL_CONF_CTX_set_ssl_ctx(s_cctx, s_ctx);
  1301. SSL_CONF_CTX_set_ssl_ctx(s_cctx2, s_ctx2);
  1302. for (i = 0; i < sk_OPENSSL_STRING_num(conf_args); i += 2) {
  1303. int rv;
  1304. arg = sk_OPENSSL_STRING_value(conf_args, i);
  1305. argn = sk_OPENSSL_STRING_value(conf_args, i + 1);
  1306. rv = SSL_CONF_cmd(c_cctx, arg, argn);
  1307. /* If not recognised use server context */
  1308. if (rv == -2) {
  1309. rv = SSL_CONF_cmd(s_cctx2, arg, argn);
  1310. if (rv > 0)
  1311. rv = SSL_CONF_cmd(s_cctx, arg, argn);
  1312. }
  1313. if (rv <= 0) {
  1314. BIO_printf(bio_err, "Error processing %s %s\n",
  1315. arg, argn ? argn : "");
  1316. ERR_print_errors(bio_err);
  1317. goto end;
  1318. }
  1319. }
  1320. if (!SSL_CONF_CTX_finish(s_cctx) || !SSL_CONF_CTX_finish(c_cctx) || !SSL_CONF_CTX_finish(s_cctx2)) {
  1321. BIO_puts(bio_err, "Error finishing context\n");
  1322. ERR_print_errors(bio_err);
  1323. goto end;
  1324. }
  1325. #ifndef OPENSSL_NO_DH
  1326. if (!no_dhe) {
  1327. if (dhe1024dsa) {
  1328. dh = get_dh1024dsa();
  1329. } else if (dhe512)
  1330. dh = get_dh512();
  1331. else
  1332. dh = get_dh1024();
  1333. SSL_CTX_set_tmp_dh(s_ctx, dh);
  1334. SSL_CTX_set_tmp_dh(s_ctx2, dh);
  1335. DH_free(dh);
  1336. }
  1337. #else
  1338. (void)no_dhe;
  1339. #endif
  1340. if ((!SSL_CTX_load_verify_locations(s_ctx, CAfile, CApath)) ||
  1341. (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
  1342. (!SSL_CTX_load_verify_locations(s_ctx2, CAfile, CApath)) ||
  1343. (!SSL_CTX_set_default_verify_paths(s_ctx2)) ||
  1344. (!SSL_CTX_load_verify_locations(c_ctx, CAfile, CApath)) ||
  1345. (!SSL_CTX_set_default_verify_paths(c_ctx))) {
  1346. ERR_print_errors(bio_err);
  1347. }
  1348. #ifndef OPENSSL_NO_CT
  1349. if (!SSL_CTX_set_default_ctlog_list_file(s_ctx) ||
  1350. !SSL_CTX_set_default_ctlog_list_file(s_ctx2) ||
  1351. !SSL_CTX_set_default_ctlog_list_file(c_ctx)) {
  1352. ERR_print_errors(bio_err);
  1353. }
  1354. #endif
  1355. if (client_auth) {
  1356. printf("client authentication\n");
  1357. SSL_CTX_set_verify(s_ctx,
  1358. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  1359. verify_callback);
  1360. SSL_CTX_set_verify(s_ctx2,
  1361. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  1362. verify_callback);
  1363. SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback,
  1364. &app_verify_arg);
  1365. SSL_CTX_set_cert_verify_callback(s_ctx2, app_verify_callback,
  1366. &app_verify_arg);
  1367. }
  1368. if (server_auth) {
  1369. printf("server authentication\n");
  1370. SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
  1371. SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback,
  1372. &app_verify_arg);
  1373. }
  1374. {
  1375. int session_id_context = 0;
  1376. if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context,
  1377. sizeof(session_id_context)) ||
  1378. !SSL_CTX_set_session_id_context(s_ctx2, (void *)&session_id_context,
  1379. sizeof(session_id_context))) {
  1380. ERR_print_errors(bio_err);
  1381. goto end;
  1382. }
  1383. }
  1384. /* Use PSK only if PSK key is given */
  1385. if (psk_key != NULL) {
  1386. /*
  1387. * no_psk is used to avoid putting psk command to openssl tool
  1388. */
  1389. if (no_psk) {
  1390. /*
  1391. * if PSK is not compiled in and psk key is given, do nothing and
  1392. * exit successfully
  1393. */
  1394. ret = EXIT_SUCCESS;
  1395. goto end;
  1396. }
  1397. #ifndef OPENSSL_NO_PSK
  1398. SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback);
  1399. SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback);
  1400. SSL_CTX_set_psk_server_callback(s_ctx2, psk_server_callback);
  1401. if (debug)
  1402. BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n");
  1403. if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint") ||
  1404. !SSL_CTX_use_psk_identity_hint(s_ctx2, "ctx server identity_hint")) {
  1405. BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n");
  1406. ERR_print_errors(bio_err);
  1407. goto end;
  1408. }
  1409. #endif
  1410. }
  1411. #ifndef OPENSSL_NO_NEXTPROTONEG
  1412. if (npn_client) {
  1413. SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL);
  1414. }
  1415. if (npn_server) {
  1416. if (npn_server_reject) {
  1417. BIO_printf(bio_err,
  1418. "Can't have both -npn_server and -npn_server_reject\n");
  1419. goto end;
  1420. }
  1421. SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_npn, NULL);
  1422. SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_npn, NULL);
  1423. }
  1424. if (npn_server_reject) {
  1425. SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_rejects_npn, NULL);
  1426. SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_rejects_npn, NULL);
  1427. }
  1428. #endif
  1429. if (serverinfo_sct) {
  1430. if (!SSL_CTX_add_client_custom_ext(c_ctx,
  1431. TLSEXT_TYPE_signed_certificate_timestamp,
  1432. NULL, NULL, NULL,
  1433. serverinfo_cli_parse_cb, NULL)) {
  1434. BIO_printf(bio_err, "Error adding SCT extension\n");
  1435. goto end;
  1436. }
  1437. }
  1438. if (serverinfo_tack) {
  1439. if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE,
  1440. NULL, NULL, NULL,
  1441. serverinfo_cli_parse_cb, NULL)) {
  1442. BIO_printf(bio_err, "Error adding TACK extension\n");
  1443. goto end;
  1444. }
  1445. }
  1446. if (serverinfo_file)
  1447. if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file) ||
  1448. !SSL_CTX_use_serverinfo_file(s_ctx2, serverinfo_file)) {
  1449. BIO_printf(bio_err, "missing serverinfo file\n");
  1450. goto end;
  1451. }
  1452. if (custom_ext) {
  1453. if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0,
  1454. custom_ext_0_cli_add_cb,
  1455. NULL, NULL,
  1456. custom_ext_0_cli_parse_cb, NULL)
  1457. || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1,
  1458. custom_ext_1_cli_add_cb,
  1459. NULL, NULL,
  1460. custom_ext_1_cli_parse_cb, NULL)
  1461. || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2,
  1462. custom_ext_2_cli_add_cb,
  1463. NULL, NULL,
  1464. custom_ext_2_cli_parse_cb, NULL)
  1465. || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3,
  1466. custom_ext_3_cli_add_cb,
  1467. NULL, NULL,
  1468. custom_ext_3_cli_parse_cb, NULL)
  1469. || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0,
  1470. custom_ext_0_srv_add_cb,
  1471. NULL, NULL,
  1472. custom_ext_0_srv_parse_cb, NULL)
  1473. || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_0,
  1474. custom_ext_0_srv_add_cb,
  1475. NULL, NULL,
  1476. custom_ext_0_srv_parse_cb, NULL)
  1477. || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1,
  1478. custom_ext_1_srv_add_cb,
  1479. NULL, NULL,
  1480. custom_ext_1_srv_parse_cb, NULL)
  1481. || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_1,
  1482. custom_ext_1_srv_add_cb,
  1483. NULL, NULL,
  1484. custom_ext_1_srv_parse_cb, NULL)
  1485. || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2,
  1486. custom_ext_2_srv_add_cb,
  1487. NULL, NULL,
  1488. custom_ext_2_srv_parse_cb, NULL)
  1489. || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_2,
  1490. custom_ext_2_srv_add_cb,
  1491. NULL, NULL,
  1492. custom_ext_2_srv_parse_cb, NULL)
  1493. || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3,
  1494. custom_ext_3_srv_add_cb,
  1495. NULL, NULL,
  1496. custom_ext_3_srv_parse_cb, NULL)
  1497. || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_3,
  1498. custom_ext_3_srv_add_cb,
  1499. NULL, NULL,
  1500. custom_ext_3_srv_parse_cb, NULL)) {
  1501. BIO_printf(bio_err, "Error setting custom extensions\n");
  1502. goto end;
  1503. }
  1504. }
  1505. if (alpn_server)
  1506. SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, alpn_server);
  1507. if (alpn_server2)
  1508. SSL_CTX_set_alpn_select_cb(s_ctx2, cb_server_alpn, alpn_server2);
  1509. if (alpn_client) {
  1510. size_t alpn_len;
  1511. unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client);
  1512. if (alpn == NULL) {
  1513. BIO_printf(bio_err, "Error parsing -alpn_client argument\n");
  1514. goto end;
  1515. }
  1516. /* Returns 0 on success!! */
  1517. if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) {
  1518. BIO_printf(bio_err, "Error setting ALPN\n");
  1519. OPENSSL_free(alpn);
  1520. goto end;
  1521. }
  1522. OPENSSL_free(alpn);
  1523. }
  1524. if (server_sess_in != NULL) {
  1525. server_sess = read_session(server_sess_in);
  1526. if (server_sess == NULL)
  1527. goto end;
  1528. }
  1529. if (client_sess_in != NULL) {
  1530. client_sess = read_session(client_sess_in);
  1531. if (client_sess == NULL)
  1532. goto end;
  1533. }
  1534. if (server_sess_out != NULL || server_sess_in != NULL) {
  1535. char *keys;
  1536. long size;
  1537. /* Use a fixed key so that we can decrypt the ticket. */
  1538. size = SSL_CTX_set_tlsext_ticket_keys(s_ctx, NULL, 0);
  1539. keys = OPENSSL_zalloc(size);
  1540. SSL_CTX_set_tlsext_ticket_keys(s_ctx, keys, size);
  1541. OPENSSL_free(keys);
  1542. }
  1543. if (sn_server1 != NULL || sn_server2 != NULL)
  1544. SSL_CTX_set_tlsext_servername_callback(s_ctx, servername_cb);
  1545. c_ssl = SSL_new(c_ctx);
  1546. s_ssl = SSL_new(s_ctx);
  1547. if (sn_client)
  1548. SSL_set_tlsext_host_name(c_ssl, sn_client);
  1549. if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
  1550. goto end;
  1551. if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
  1552. goto end;
  1553. if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
  1554. goto end;
  1555. if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
  1556. goto end;
  1557. if (server_sess) {
  1558. if (SSL_CTX_add_session(s_ctx, server_sess) == 0) {
  1559. BIO_printf(bio_err, "Can't add server session\n");
  1560. ERR_print_errors(bio_err);
  1561. goto end;
  1562. }
  1563. }
  1564. BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes);
  1565. for (i = 0; i < number; i++) {
  1566. if (!reuse) {
  1567. if (!SSL_set_session(c_ssl, NULL)) {
  1568. BIO_printf(bio_err, "Failed to set session\n");
  1569. goto end;
  1570. }
  1571. }
  1572. if (client_sess_in != NULL) {
  1573. if (SSL_set_session(c_ssl, client_sess) == 0) {
  1574. BIO_printf(bio_err, "Can't set client session\n");
  1575. ERR_print_errors(bio_err);
  1576. goto end;
  1577. }
  1578. }
  1579. switch (bio_type) {
  1580. case BIO_MEM:
  1581. ret = doit(s_ssl, c_ssl, bytes);
  1582. break;
  1583. case BIO_PAIR:
  1584. ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time);
  1585. break;
  1586. #ifndef OPENSSL_NO_SOCK
  1587. case BIO_IPV4:
  1588. ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4,
  1589. bytes, &s_time, &c_time);
  1590. break;
  1591. case BIO_IPV6:
  1592. ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6,
  1593. bytes, &s_time, &c_time);
  1594. break;
  1595. #else
  1596. case BIO_IPV4:
  1597. case BIO_IPV6:
  1598. ret = EXIT_FAILURE;
  1599. goto err;
  1600. #endif
  1601. }
  1602. if (ret != EXIT_SUCCESS) break;
  1603. }
  1604. if (should_negotiate && ret == EXIT_SUCCESS &&
  1605. strcmp(should_negotiate, "fail-server") != 0 &&
  1606. strcmp(should_negotiate, "fail-client") != 0) {
  1607. int version = protocol_from_string(should_negotiate);
  1608. if (version < 0) {
  1609. BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate);
  1610. ret = EXIT_FAILURE;
  1611. goto err;
  1612. }
  1613. if (SSL_version(c_ssl) != version) {
  1614. BIO_printf(bio_err, "Unexpected version negotiated. "
  1615. "Expected: %s, got %s\n", should_negotiate, SSL_get_version(c_ssl));
  1616. ret = EXIT_FAILURE;
  1617. goto err;
  1618. }
  1619. }
  1620. if (should_reuse != -1) {
  1621. if (SSL_session_reused(s_ssl) != should_reuse ||
  1622. SSL_session_reused(c_ssl) != should_reuse) {
  1623. BIO_printf(bio_err, "Unexpected session reuse state. "
  1624. "Expected: %d, server: %d, client: %d\n", should_reuse,
  1625. SSL_session_reused(s_ssl), SSL_session_reused(c_ssl));
  1626. ret = EXIT_FAILURE;
  1627. goto err;
  1628. }
  1629. }
  1630. if (server_sess_out != NULL) {
  1631. if (write_session(server_sess_out, SSL_get_session(s_ssl)) == 0) {
  1632. ret = EXIT_FAILURE;
  1633. goto err;
  1634. }
  1635. }
  1636. if (client_sess_out != NULL) {
  1637. if (write_session(client_sess_out, SSL_get_session(c_ssl)) == 0) {
  1638. ret = EXIT_FAILURE;
  1639. goto err;
  1640. }
  1641. }
  1642. if (!verbose) {
  1643. print_details(c_ssl, "");
  1644. }
  1645. if (print_time) {
  1646. #ifdef CLOCKS_PER_SEC
  1647. /*
  1648. * "To determine the time in seconds, the value returned by the clock
  1649. * function should be divided by the value of the macro
  1650. * CLOCKS_PER_SEC." -- ISO/IEC 9899
  1651. */
  1652. BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
  1653. "Approximate total client time: %6.2f s\n",
  1654. (double)s_time / CLOCKS_PER_SEC,
  1655. (double)c_time / CLOCKS_PER_SEC);
  1656. #else
  1657. BIO_printf(bio_stdout,
  1658. "Approximate total server time: %6.2f units\n"
  1659. "Approximate total client time: %6.2f units\n",
  1660. (double)s_time, (double)c_time);
  1661. #endif
  1662. }
  1663. err:
  1664. SSL_free(s_ssl);
  1665. SSL_free(c_ssl);
  1666. end:
  1667. SSL_CTX_free(s_ctx);
  1668. SSL_CTX_free(s_ctx2);
  1669. SSL_CTX_free(c_ctx);
  1670. SSL_CONF_CTX_free(s_cctx);
  1671. SSL_CONF_CTX_free(s_cctx2);
  1672. SSL_CONF_CTX_free(c_cctx);
  1673. sk_OPENSSL_STRING_free(conf_args);
  1674. BIO_free(bio_stdout);
  1675. SSL_SESSION_free(server_sess);
  1676. SSL_SESSION_free(client_sess);
  1677. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  1678. if (CRYPTO_mem_leaks(bio_err) <= 0)
  1679. ret = EXIT_FAILURE;
  1680. #endif
  1681. BIO_free(bio_err);
  1682. EXIT(ret);
  1683. }
  1684. #ifndef OPENSSL_NO_SOCK
  1685. int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count,
  1686. clock_t *s_time, clock_t *c_time)
  1687. {
  1688. long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
  1689. BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
  1690. BIO *acpt = NULL, *server = NULL, *client = NULL;
  1691. char addr_str[40];
  1692. int ret = EXIT_FAILURE;
  1693. int err_in_client = 0;
  1694. int err_in_server = 0;
  1695. acpt = BIO_new_accept("0");
  1696. if (acpt == NULL)
  1697. goto err;
  1698. BIO_set_accept_ip_family(acpt, family);
  1699. BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR);
  1700. if (BIO_do_accept(acpt) <= 0)
  1701. goto err;
  1702. BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt));
  1703. client = BIO_new_connect(addr_str);
  1704. BIO_set_conn_ip_family(client, family);
  1705. if (!client)
  1706. goto err;
  1707. if (BIO_set_nbio(client, 1) <= 0)
  1708. goto err;
  1709. if (BIO_set_nbio(acpt, 1) <= 0)
  1710. goto err;
  1711. {
  1712. int st_connect = 0, st_accept = 0;
  1713. while(!st_connect || !st_accept) {
  1714. if (!st_connect) {
  1715. if (BIO_do_connect(client) <= 0) {
  1716. if (!BIO_should_retry(client))
  1717. goto err;
  1718. } else {
  1719. st_connect = 1;
  1720. }
  1721. }
  1722. if (!st_accept) {
  1723. if (BIO_do_accept(acpt) <= 0) {
  1724. if (!BIO_should_retry(acpt))
  1725. goto err;
  1726. } else {
  1727. st_accept = 1;
  1728. }
  1729. }
  1730. }
  1731. }
  1732. /* We're not interested in accepting further connects */
  1733. server = BIO_pop(acpt);
  1734. BIO_free_all(acpt);
  1735. acpt = NULL;
  1736. s_ssl_bio = BIO_new(BIO_f_ssl());
  1737. if (!s_ssl_bio)
  1738. goto err;
  1739. c_ssl_bio = BIO_new(BIO_f_ssl());
  1740. if (!c_ssl_bio)
  1741. goto err;
  1742. SSL_set_connect_state(c_ssl);
  1743. SSL_set_bio(c_ssl, client, client);
  1744. (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
  1745. SSL_set_accept_state(s_ssl);
  1746. SSL_set_bio(s_ssl, server, server);
  1747. (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
  1748. do {
  1749. /*-
  1750. * c_ssl_bio: SSL filter BIO
  1751. *
  1752. * client: I/O for SSL library
  1753. *
  1754. *
  1755. * server: I/O for SSL library
  1756. *
  1757. * s_ssl_bio: SSL filter BIO
  1758. */
  1759. /*
  1760. * We have non-blocking behaviour throughout this test program, but
  1761. * can be sure that there is *some* progress in each iteration; so we
  1762. * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
  1763. * we just try everything in each iteration
  1764. */
  1765. {
  1766. /* CLIENT */
  1767. char cbuf[1024 * 8];
  1768. int i, r;
  1769. clock_t c_clock = clock();
  1770. memset(cbuf, 0, sizeof(cbuf));
  1771. if (debug)
  1772. if (SSL_in_init(c_ssl))
  1773. printf("client waiting in SSL_connect - %s\n",
  1774. SSL_state_string_long(c_ssl));
  1775. if (cw_num > 0) {
  1776. /* Write to server. */
  1777. if (cw_num > (long)sizeof(cbuf))
  1778. i = sizeof(cbuf);
  1779. else
  1780. i = (int)cw_num;
  1781. r = BIO_write(c_ssl_bio, cbuf, i);
  1782. if (r < 0) {
  1783. if (!BIO_should_retry(c_ssl_bio)) {
  1784. fprintf(stderr, "ERROR in CLIENT\n");
  1785. err_in_client = 1;
  1786. goto err;
  1787. }
  1788. /*
  1789. * BIO_should_retry(...) can just be ignored here. The
  1790. * library expects us to call BIO_write with the same
  1791. * arguments again, and that's what we will do in the
  1792. * next iteration.
  1793. */
  1794. } else if (r == 0) {
  1795. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  1796. goto err;
  1797. } else {
  1798. if (debug)
  1799. printf("client wrote %d\n", r);
  1800. cw_num -= r;
  1801. }
  1802. }
  1803. if (cr_num > 0) {
  1804. /* Read from server. */
  1805. r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
  1806. if (r < 0) {
  1807. if (!BIO_should_retry(c_ssl_bio)) {
  1808. fprintf(stderr, "ERROR in CLIENT\n");
  1809. err_in_client = 1;
  1810. goto err;
  1811. }
  1812. /*
  1813. * Again, "BIO_should_retry" can be ignored.
  1814. */
  1815. } else if (r == 0) {
  1816. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  1817. goto err;
  1818. } else {
  1819. if (debug)
  1820. printf("client read %d\n", r);
  1821. cr_num -= r;
  1822. }
  1823. }
  1824. /*
  1825. * c_time and s_time increments will typically be very small
  1826. * (depending on machine speed and clock tick intervals), but
  1827. * sampling over a large number of connections should result in
  1828. * fairly accurate figures. We cannot guarantee a lot, however
  1829. * -- if each connection lasts for exactly one clock tick, it
  1830. * will be counted only for the client or only for the server or
  1831. * even not at all.
  1832. */
  1833. *c_time += (clock() - c_clock);
  1834. }
  1835. {
  1836. /* SERVER */
  1837. char sbuf[1024 * 8];
  1838. int i, r;
  1839. clock_t s_clock = clock();
  1840. memset(sbuf, 0, sizeof(sbuf));
  1841. if (debug)
  1842. if (SSL_in_init(s_ssl))
  1843. printf("server waiting in SSL_accept - %s\n",
  1844. SSL_state_string_long(s_ssl));
  1845. if (sw_num > 0) {
  1846. /* Write to client. */
  1847. if (sw_num > (long)sizeof(sbuf))
  1848. i = sizeof(sbuf);
  1849. else
  1850. i = (int)sw_num;
  1851. r = BIO_write(s_ssl_bio, sbuf, i);
  1852. if (r < 0) {
  1853. if (!BIO_should_retry(s_ssl_bio)) {
  1854. fprintf(stderr, "ERROR in SERVER\n");
  1855. err_in_server = 1;
  1856. goto err;
  1857. }
  1858. /* Ignore "BIO_should_retry". */
  1859. } else if (r == 0) {
  1860. fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
  1861. goto err;
  1862. } else {
  1863. if (debug)
  1864. printf("server wrote %d\n", r);
  1865. sw_num -= r;
  1866. }
  1867. }
  1868. if (sr_num > 0) {
  1869. /* Read from client. */
  1870. r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
  1871. if (r < 0) {
  1872. if (!BIO_should_retry(s_ssl_bio)) {
  1873. fprintf(stderr, "ERROR in SERVER\n");
  1874. err_in_server = 1;
  1875. goto err;
  1876. }
  1877. /* blah, blah */
  1878. } else if (r == 0) {
  1879. fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
  1880. goto err;
  1881. } else {
  1882. if (debug)
  1883. printf("server read %d\n", r);
  1884. sr_num -= r;
  1885. }
  1886. }
  1887. *s_time += (clock() - s_clock);
  1888. }
  1889. }
  1890. while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
  1891. if (verbose)
  1892. print_details(c_ssl, "DONE via TCP connect: ");
  1893. # ifndef OPENSSL_NO_NEXTPROTONEG
  1894. if (verify_npn(c_ssl, s_ssl) < 0)
  1895. goto end;
  1896. # endif
  1897. if (verify_serverinfo() < 0) {
  1898. fprintf(stderr, "Server info verify error\n");
  1899. goto err;
  1900. }
  1901. if (verify_alpn(c_ssl, s_ssl) < 0
  1902. || verify_servername(c_ssl, s_ssl) < 0)
  1903. goto err;
  1904. if (custom_ext_error) {
  1905. fprintf(stderr, "Custom extension error\n");
  1906. goto err;
  1907. }
  1908. # ifndef OPENSSL_NO_NEXTPROTONEG
  1909. end:
  1910. # endif
  1911. ret = EXIT_SUCCESS;
  1912. err:
  1913. ERR_print_errors(bio_err);
  1914. BIO_free_all(acpt);
  1915. BIO_free(server);
  1916. BIO_free(client);
  1917. BIO_free(s_ssl_bio);
  1918. BIO_free(c_ssl_bio);
  1919. if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
  1920. ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  1921. else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
  1922. ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  1923. return ret;
  1924. }
  1925. #endif
  1926. int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
  1927. clock_t *s_time, clock_t *c_time)
  1928. {
  1929. long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
  1930. BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
  1931. BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
  1932. int ret = EXIT_FAILURE;
  1933. int err_in_client = 0;
  1934. int err_in_server = 0;
  1935. size_t bufsiz = 256; /* small buffer for testing */
  1936. if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
  1937. goto err;
  1938. if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
  1939. goto err;
  1940. s_ssl_bio = BIO_new(BIO_f_ssl());
  1941. if (!s_ssl_bio)
  1942. goto err;
  1943. c_ssl_bio = BIO_new(BIO_f_ssl());
  1944. if (!c_ssl_bio)
  1945. goto err;
  1946. SSL_set_connect_state(c_ssl);
  1947. SSL_set_bio(c_ssl, client, client);
  1948. (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
  1949. SSL_set_accept_state(s_ssl);
  1950. SSL_set_bio(s_ssl, server, server);
  1951. (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
  1952. do {
  1953. /*-
  1954. * c_ssl_bio: SSL filter BIO
  1955. *
  1956. * client: pseudo-I/O for SSL library
  1957. *
  1958. * client_io: client's SSL communication; usually to be
  1959. * relayed over some I/O facility, but in this
  1960. * test program, we're the server, too:
  1961. *
  1962. * server_io: server's SSL communication
  1963. *
  1964. * server: pseudo-I/O for SSL library
  1965. *
  1966. * s_ssl_bio: SSL filter BIO
  1967. *
  1968. * The client and the server each employ a "BIO pair":
  1969. * client + client_io, server + server_io.
  1970. * BIO pairs are symmetric. A BIO pair behaves similar
  1971. * to a non-blocking socketpair (but both endpoints must
  1972. * be handled by the same thread).
  1973. * [Here we could connect client and server to the ends
  1974. * of a single BIO pair, but then this code would be less
  1975. * suitable as an example for BIO pairs in general.]
  1976. *
  1977. * Useful functions for querying the state of BIO pair endpoints:
  1978. *
  1979. * BIO_ctrl_pending(bio) number of bytes we can read now
  1980. * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfill
  1981. * other side's read attempt
  1982. * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
  1983. *
  1984. * ..._read_request is never more than ..._write_guarantee;
  1985. * it depends on the application which one you should use.
  1986. */
  1987. /*
  1988. * We have non-blocking behaviour throughout this test program, but
  1989. * can be sure that there is *some* progress in each iteration; so we
  1990. * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
  1991. * we just try everything in each iteration
  1992. */
  1993. {
  1994. /* CLIENT */
  1995. char cbuf[1024 * 8];
  1996. int i, r;
  1997. clock_t c_clock = clock();
  1998. memset(cbuf, 0, sizeof(cbuf));
  1999. if (debug)
  2000. if (SSL_in_init(c_ssl))
  2001. printf("client waiting in SSL_connect - %s\n",
  2002. SSL_state_string_long(c_ssl));
  2003. if (cw_num > 0) {
  2004. /* Write to server. */
  2005. if (cw_num > (long)sizeof(cbuf))
  2006. i = sizeof(cbuf);
  2007. else
  2008. i = (int)cw_num;
  2009. r = BIO_write(c_ssl_bio, cbuf, i);
  2010. if (r < 0) {
  2011. if (!BIO_should_retry(c_ssl_bio)) {
  2012. fprintf(stderr, "ERROR in CLIENT\n");
  2013. err_in_client = 1;
  2014. goto err;
  2015. }
  2016. /*
  2017. * BIO_should_retry(...) can just be ignored here. The
  2018. * library expects us to call BIO_write with the same
  2019. * arguments again, and that's what we will do in the
  2020. * next iteration.
  2021. */
  2022. } else if (r == 0) {
  2023. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  2024. goto err;
  2025. } else {
  2026. if (debug)
  2027. printf("client wrote %d\n", r);
  2028. cw_num -= r;
  2029. }
  2030. }
  2031. if (cr_num > 0) {
  2032. /* Read from server. */
  2033. r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
  2034. if (r < 0) {
  2035. if (!BIO_should_retry(c_ssl_bio)) {
  2036. fprintf(stderr, "ERROR in CLIENT\n");
  2037. err_in_client = 1;
  2038. goto err;
  2039. }
  2040. /*
  2041. * Again, "BIO_should_retry" can be ignored.
  2042. */
  2043. } else if (r == 0) {
  2044. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  2045. goto err;
  2046. } else {
  2047. if (debug)
  2048. printf("client read %d\n", r);
  2049. cr_num -= r;
  2050. }
  2051. }
  2052. /*
  2053. * c_time and s_time increments will typically be very small
  2054. * (depending on machine speed and clock tick intervals), but
  2055. * sampling over a large number of connections should result in
  2056. * fairly accurate figures. We cannot guarantee a lot, however
  2057. * -- if each connection lasts for exactly one clock tick, it
  2058. * will be counted only for the client or only for the server or
  2059. * even not at all.
  2060. */
  2061. *c_time += (clock() - c_clock);
  2062. }
  2063. {
  2064. /* SERVER */
  2065. char sbuf[1024 * 8];
  2066. int i, r;
  2067. clock_t s_clock = clock();
  2068. memset(sbuf, 0, sizeof(sbuf));
  2069. if (debug)
  2070. if (SSL_in_init(s_ssl))
  2071. printf("server waiting in SSL_accept - %s\n",
  2072. SSL_state_string_long(s_ssl));
  2073. if (sw_num > 0) {
  2074. /* Write to client. */
  2075. if (sw_num > (long)sizeof(sbuf))
  2076. i = sizeof(sbuf);
  2077. else
  2078. i = (int)sw_num;
  2079. r = BIO_write(s_ssl_bio, sbuf, i);
  2080. if (r < 0) {
  2081. if (!BIO_should_retry(s_ssl_bio)) {
  2082. fprintf(stderr, "ERROR in SERVER\n");
  2083. err_in_server = 1;
  2084. goto err;
  2085. }
  2086. /* Ignore "BIO_should_retry". */
  2087. } else if (r == 0) {
  2088. fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
  2089. goto err;
  2090. } else {
  2091. if (debug)
  2092. printf("server wrote %d\n", r);
  2093. sw_num -= r;
  2094. }
  2095. }
  2096. if (sr_num > 0) {
  2097. /* Read from client. */
  2098. r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
  2099. if (r < 0) {
  2100. if (!BIO_should_retry(s_ssl_bio)) {
  2101. fprintf(stderr, "ERROR in SERVER\n");
  2102. err_in_server = 1;
  2103. goto err;
  2104. }
  2105. /* blah, blah */
  2106. } else if (r == 0) {
  2107. fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
  2108. goto err;
  2109. } else {
  2110. if (debug)
  2111. printf("server read %d\n", r);
  2112. sr_num -= r;
  2113. }
  2114. }
  2115. *s_time += (clock() - s_clock);
  2116. }
  2117. {
  2118. /* "I/O" BETWEEN CLIENT AND SERVER. */
  2119. size_t r1, r2;
  2120. BIO *io1 = server_io, *io2 = client_io;
  2121. /*
  2122. * we use the non-copying interface for io1 and the standard
  2123. * BIO_write/BIO_read interface for io2
  2124. */
  2125. static int prev_progress = 1;
  2126. int progress = 0;
  2127. /* io1 to io2 */
  2128. do {
  2129. size_t num;
  2130. int r;
  2131. r1 = BIO_ctrl_pending(io1);
  2132. r2 = BIO_ctrl_get_write_guarantee(io2);
  2133. num = r1;
  2134. if (r2 < num)
  2135. num = r2;
  2136. if (num) {
  2137. char *dataptr;
  2138. if (INT_MAX < num) /* yeah, right */
  2139. num = INT_MAX;
  2140. r = BIO_nread(io1, &dataptr, (int)num);
  2141. assert(r > 0);
  2142. assert(r <= (int)num);
  2143. /*
  2144. * possibly r < num (non-contiguous data)
  2145. */
  2146. num = r;
  2147. r = BIO_write(io2, dataptr, (int)num);
  2148. if (r != (int)num) { /* can't happen */
  2149. fprintf(stderr, "ERROR: BIO_write could not write "
  2150. "BIO_ctrl_get_write_guarantee() bytes");
  2151. goto err;
  2152. }
  2153. progress = 1;
  2154. if (debug)
  2155. printf((io1 == client_io) ?
  2156. "C->S relaying: %d bytes\n" :
  2157. "S->C relaying: %d bytes\n", (int)num);
  2158. }
  2159. }
  2160. while (r1 && r2);
  2161. /* io2 to io1 */
  2162. {
  2163. size_t num;
  2164. int r;
  2165. r1 = BIO_ctrl_pending(io2);
  2166. r2 = BIO_ctrl_get_read_request(io1);
  2167. /*
  2168. * here we could use ..._get_write_guarantee instead of
  2169. * ..._get_read_request, but by using the latter we test
  2170. * restartability of the SSL implementation more thoroughly
  2171. */
  2172. num = r1;
  2173. if (r2 < num)
  2174. num = r2;
  2175. if (num) {
  2176. char *dataptr;
  2177. if (INT_MAX < num)
  2178. num = INT_MAX;
  2179. if (num > 1)
  2180. --num; /* test restartability even more thoroughly */
  2181. r = BIO_nwrite0(io1, &dataptr);
  2182. assert(r > 0);
  2183. if (r < (int)num)
  2184. num = r;
  2185. r = BIO_read(io2, dataptr, (int)num);
  2186. if (r != (int)num) { /* can't happen */
  2187. fprintf(stderr, "ERROR: BIO_read could not read "
  2188. "BIO_ctrl_pending() bytes");
  2189. goto err;
  2190. }
  2191. progress = 1;
  2192. r = BIO_nwrite(io1, &dataptr, (int)num);
  2193. if (r != (int)num) { /* can't happen */
  2194. fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
  2195. "BIO_nwrite0() bytes");
  2196. goto err;
  2197. }
  2198. if (debug)
  2199. printf((io2 == client_io) ?
  2200. "C->S relaying: %d bytes\n" :
  2201. "S->C relaying: %d bytes\n", (int)num);
  2202. }
  2203. } /* no loop, BIO_ctrl_get_read_request now
  2204. * returns 0 anyway */
  2205. if (!progress && !prev_progress)
  2206. if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) {
  2207. fprintf(stderr, "ERROR: got stuck\n");
  2208. fprintf(stderr, " ERROR.\n");
  2209. goto err;
  2210. }
  2211. prev_progress = progress;
  2212. }
  2213. }
  2214. while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
  2215. if (verbose)
  2216. print_details(c_ssl, "DONE via BIO pair: ");
  2217. #ifndef OPENSSL_NO_NEXTPROTONEG
  2218. if (verify_npn(c_ssl, s_ssl) < 0)
  2219. goto end;
  2220. #endif
  2221. if (verify_serverinfo() < 0) {
  2222. fprintf(stderr, "Server info verify error\n");
  2223. goto err;
  2224. }
  2225. if (verify_alpn(c_ssl, s_ssl) < 0
  2226. || verify_servername(c_ssl, s_ssl) < 0)
  2227. goto err;
  2228. if (custom_ext_error) {
  2229. fprintf(stderr, "Custom extension error\n");
  2230. goto err;
  2231. }
  2232. #ifndef OPENSSL_NO_NEXTPROTONEG
  2233. end:
  2234. #endif
  2235. ret = EXIT_SUCCESS;
  2236. err:
  2237. ERR_print_errors(bio_err);
  2238. BIO_free(server);
  2239. BIO_free(server_io);
  2240. BIO_free(client);
  2241. BIO_free(client_io);
  2242. BIO_free(s_ssl_bio);
  2243. BIO_free(c_ssl_bio);
  2244. if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
  2245. ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  2246. else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
  2247. ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  2248. return ret;
  2249. }
  2250. #define W_READ 1
  2251. #define W_WRITE 2
  2252. #define C_DONE 1
  2253. #define S_DONE 2
  2254. int doit(SSL *s_ssl, SSL *c_ssl, long count)
  2255. {
  2256. char *cbuf = NULL, *sbuf = NULL;
  2257. long bufsiz;
  2258. long cw_num = count, cr_num = count;
  2259. long sw_num = count, sr_num = count;
  2260. int ret = EXIT_FAILURE;
  2261. BIO *c_to_s = NULL;
  2262. BIO *s_to_c = NULL;
  2263. BIO *c_bio = NULL;
  2264. BIO *s_bio = NULL;
  2265. int c_r, c_w, s_r, s_w;
  2266. int i, j;
  2267. int done = 0;
  2268. int c_write, s_write;
  2269. int do_server = 0, do_client = 0;
  2270. int max_frag = 5 * 1024;
  2271. int err_in_client = 0;
  2272. int err_in_server = 0;
  2273. bufsiz = count > 40 * 1024 ? 40 * 1024 : count;
  2274. if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL)
  2275. goto err;
  2276. if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL)
  2277. goto err;
  2278. c_to_s = BIO_new(BIO_s_mem());
  2279. s_to_c = BIO_new(BIO_s_mem());
  2280. if ((s_to_c == NULL) || (c_to_s == NULL)) {
  2281. ERR_print_errors(bio_err);
  2282. goto err;
  2283. }
  2284. c_bio = BIO_new(BIO_f_ssl());
  2285. s_bio = BIO_new(BIO_f_ssl());
  2286. if ((c_bio == NULL) || (s_bio == NULL)) {
  2287. ERR_print_errors(bio_err);
  2288. goto err;
  2289. }
  2290. SSL_set_connect_state(c_ssl);
  2291. SSL_set_bio(c_ssl, s_to_c, c_to_s);
  2292. SSL_set_max_send_fragment(c_ssl, max_frag);
  2293. BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE);
  2294. /*
  2295. * We've just given our ref to these BIOs to c_ssl. We need another one to
  2296. * give to s_ssl
  2297. */
  2298. if (!BIO_up_ref(c_to_s)) {
  2299. /* c_to_s and s_to_c will get freed when we free c_ssl */
  2300. c_to_s = NULL;
  2301. s_to_c = NULL;
  2302. goto err;
  2303. }
  2304. if (!BIO_up_ref(s_to_c)) {
  2305. /* s_to_c will get freed when we free c_ssl */
  2306. s_to_c = NULL;
  2307. goto err;
  2308. }
  2309. SSL_set_accept_state(s_ssl);
  2310. SSL_set_bio(s_ssl, c_to_s, s_to_c);
  2311. /* We've used up all our refs to these now */
  2312. c_to_s = NULL;
  2313. s_to_c = NULL;
  2314. SSL_set_max_send_fragment(s_ssl, max_frag);
  2315. BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE);
  2316. c_r = 0;
  2317. s_r = 1;
  2318. c_w = 1;
  2319. s_w = 0;
  2320. c_write = 1, s_write = 0;
  2321. /* We can always do writes */
  2322. for (;;) {
  2323. do_server = 0;
  2324. do_client = 0;
  2325. i = (int)BIO_pending(s_bio);
  2326. if ((i && s_r) || s_w)
  2327. do_server = 1;
  2328. i = (int)BIO_pending(c_bio);
  2329. if ((i && c_r) || c_w)
  2330. do_client = 1;
  2331. if (do_server && debug) {
  2332. if (SSL_in_init(s_ssl))
  2333. printf("server waiting in SSL_accept - %s\n",
  2334. SSL_state_string_long(s_ssl));
  2335. }
  2336. if (do_client && debug) {
  2337. if (SSL_in_init(c_ssl))
  2338. printf("client waiting in SSL_connect - %s\n",
  2339. SSL_state_string_long(c_ssl));
  2340. }
  2341. if (!do_client && !do_server) {
  2342. fprintf(stdout, "ERROR IN STARTUP\n");
  2343. ERR_print_errors(bio_err);
  2344. goto err;
  2345. }
  2346. if (do_client && !(done & C_DONE)) {
  2347. if (c_write) {
  2348. j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num;
  2349. i = BIO_write(c_bio, cbuf, j);
  2350. if (i < 0) {
  2351. c_r = 0;
  2352. c_w = 0;
  2353. if (BIO_should_retry(c_bio)) {
  2354. if (BIO_should_read(c_bio))
  2355. c_r = 1;
  2356. if (BIO_should_write(c_bio))
  2357. c_w = 1;
  2358. } else {
  2359. fprintf(stderr, "ERROR in CLIENT\n");
  2360. err_in_client = 1;
  2361. ERR_print_errors(bio_err);
  2362. goto err;
  2363. }
  2364. } else if (i == 0) {
  2365. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  2366. goto err;
  2367. } else {
  2368. if (debug)
  2369. printf("client wrote %d\n", i);
  2370. /* ok */
  2371. s_r = 1;
  2372. c_write = 0;
  2373. cw_num -= i;
  2374. if (max_frag > 1029)
  2375. SSL_set_max_send_fragment(c_ssl, max_frag -= 5);
  2376. }
  2377. } else {
  2378. i = BIO_read(c_bio, cbuf, bufsiz);
  2379. if (i < 0) {
  2380. c_r = 0;
  2381. c_w = 0;
  2382. if (BIO_should_retry(c_bio)) {
  2383. if (BIO_should_read(c_bio))
  2384. c_r = 1;
  2385. if (BIO_should_write(c_bio))
  2386. c_w = 1;
  2387. } else {
  2388. fprintf(stderr, "ERROR in CLIENT\n");
  2389. err_in_client = 1;
  2390. ERR_print_errors(bio_err);
  2391. goto err;
  2392. }
  2393. } else if (i == 0) {
  2394. fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
  2395. goto err;
  2396. } else {
  2397. if (debug)
  2398. printf("client read %d\n", i);
  2399. cr_num -= i;
  2400. if (sw_num > 0) {
  2401. s_write = 1;
  2402. s_w = 1;
  2403. }
  2404. if (cr_num <= 0) {
  2405. s_write = 1;
  2406. s_w = 1;
  2407. done = S_DONE | C_DONE;
  2408. }
  2409. }
  2410. }
  2411. }
  2412. if (do_server && !(done & S_DONE)) {
  2413. if (!s_write) {
  2414. i = BIO_read(s_bio, sbuf, bufsiz);
  2415. if (i < 0) {
  2416. s_r = 0;
  2417. s_w = 0;
  2418. if (BIO_should_retry(s_bio)) {
  2419. if (BIO_should_read(s_bio))
  2420. s_r = 1;
  2421. if (BIO_should_write(s_bio))
  2422. s_w = 1;
  2423. } else {
  2424. fprintf(stderr, "ERROR in SERVER\n");
  2425. err_in_server = 1;
  2426. ERR_print_errors(bio_err);
  2427. goto err;
  2428. }
  2429. } else if (i == 0) {
  2430. ERR_print_errors(bio_err);
  2431. fprintf(stderr,
  2432. "SSL SERVER STARTUP FAILED in SSL_read\n");
  2433. goto err;
  2434. } else {
  2435. if (debug)
  2436. printf("server read %d\n", i);
  2437. sr_num -= i;
  2438. if (cw_num > 0) {
  2439. c_write = 1;
  2440. c_w = 1;
  2441. }
  2442. if (sr_num <= 0) {
  2443. s_write = 1;
  2444. s_w = 1;
  2445. c_write = 0;
  2446. }
  2447. }
  2448. } else {
  2449. j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num;
  2450. i = BIO_write(s_bio, sbuf, j);
  2451. if (i < 0) {
  2452. s_r = 0;
  2453. s_w = 0;
  2454. if (BIO_should_retry(s_bio)) {
  2455. if (BIO_should_read(s_bio))
  2456. s_r = 1;
  2457. if (BIO_should_write(s_bio))
  2458. s_w = 1;
  2459. } else {
  2460. fprintf(stderr, "ERROR in SERVER\n");
  2461. err_in_server = 1;
  2462. ERR_print_errors(bio_err);
  2463. goto err;
  2464. }
  2465. } else if (i == 0) {
  2466. ERR_print_errors(bio_err);
  2467. fprintf(stderr,
  2468. "SSL SERVER STARTUP FAILED in SSL_write\n");
  2469. goto err;
  2470. } else {
  2471. if (debug)
  2472. printf("server wrote %d\n", i);
  2473. sw_num -= i;
  2474. s_write = 0;
  2475. c_r = 1;
  2476. if (sw_num <= 0)
  2477. done |= S_DONE;
  2478. if (max_frag > 1029)
  2479. SSL_set_max_send_fragment(s_ssl, max_frag -= 5);
  2480. }
  2481. }
  2482. }
  2483. if ((done & S_DONE) && (done & C_DONE))
  2484. break;
  2485. }
  2486. if (verbose)
  2487. print_details(c_ssl, "DONE: ");
  2488. #ifndef OPENSSL_NO_NEXTPROTONEG
  2489. if (verify_npn(c_ssl, s_ssl) < 0)
  2490. goto err;
  2491. #endif
  2492. if (verify_serverinfo() < 0) {
  2493. fprintf(stderr, "Server info verify error\n");
  2494. goto err;
  2495. }
  2496. if (custom_ext_error) {
  2497. fprintf(stderr, "Custom extension error\n");
  2498. goto err;
  2499. }
  2500. ret = EXIT_SUCCESS;
  2501. err:
  2502. BIO_free(c_to_s);
  2503. BIO_free(s_to_c);
  2504. BIO_free_all(c_bio);
  2505. BIO_free_all(s_bio);
  2506. OPENSSL_free(cbuf);
  2507. OPENSSL_free(sbuf);
  2508. if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
  2509. ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  2510. else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
  2511. ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  2512. return ret;
  2513. }
  2514. static int verify_callback(int ok, X509_STORE_CTX *ctx)
  2515. {
  2516. char *s, buf[256];
  2517. s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)),
  2518. buf, sizeof(buf));
  2519. if (s != NULL) {
  2520. if (ok)
  2521. printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf);
  2522. else {
  2523. fprintf(stderr, "depth=%d error=%d %s\n",
  2524. X509_STORE_CTX_get_error_depth(ctx),
  2525. X509_STORE_CTX_get_error(ctx), buf);
  2526. }
  2527. }
  2528. if (ok == 0) {
  2529. int i = X509_STORE_CTX_get_error(ctx);
  2530. switch (i) {
  2531. default:
  2532. fprintf(stderr, "Error string: %s\n",
  2533. X509_verify_cert_error_string(i));
  2534. break;
  2535. case X509_V_ERR_CERT_NOT_YET_VALID:
  2536. case X509_V_ERR_CERT_HAS_EXPIRED:
  2537. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  2538. ok = 1;
  2539. break;
  2540. }
  2541. }
  2542. return ok;
  2543. }
  2544. static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)
  2545. {
  2546. int ok = 1;
  2547. struct app_verify_arg *cb_arg = arg;
  2548. if (cb_arg->app_verify) {
  2549. char *s = NULL, buf[256];
  2550. X509 *c = X509_STORE_CTX_get0_cert(ctx);
  2551. printf("In app_verify_callback, allowing cert. ");
  2552. printf("Arg is: %s\n", cb_arg->string);
  2553. printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
  2554. (void *)ctx, (void *)c);
  2555. if (c)
  2556. s = X509_NAME_oneline(X509_get_subject_name(c), buf, 256);
  2557. if (s != NULL) {
  2558. printf("cert depth=%d %s\n",
  2559. X509_STORE_CTX_get_error_depth(ctx), buf);
  2560. }
  2561. return 1;
  2562. }
  2563. ok = X509_verify_cert(ctx);
  2564. return ok;
  2565. }
  2566. #ifndef OPENSSL_NO_DH
  2567. /*-
  2568. * These DH parameters have been generated as follows:
  2569. * $ openssl dhparam -C -noout 512
  2570. * $ openssl dhparam -C -noout 1024
  2571. * $ openssl dhparam -C -noout -dsaparam 1024
  2572. * (The third function has been renamed to avoid name conflicts.)
  2573. */
  2574. static DH *get_dh512()
  2575. {
  2576. static unsigned char dh512_p[] = {
  2577. 0xCB, 0xC8, 0xE1, 0x86, 0xD0, 0x1F, 0x94, 0x17, 0xA6, 0x99, 0xF0,
  2578. 0xC6,
  2579. 0x1F, 0x0D, 0xAC, 0xB6, 0x25, 0x3E, 0x06, 0x39, 0xCA, 0x72, 0x04,
  2580. 0xB0,
  2581. 0x6E, 0xDA, 0xC0, 0x61, 0xE6, 0x7A, 0x77, 0x25, 0xE8, 0x3B, 0xB9,
  2582. 0x5F,
  2583. 0x9A, 0xB6, 0xB5, 0xFE, 0x99, 0x0B, 0xA1, 0x93, 0x4E, 0x35, 0x33,
  2584. 0xB8,
  2585. 0xE1, 0xF1, 0x13, 0x4F, 0x59, 0x1A, 0xD2, 0x57, 0xC0, 0x26, 0x21,
  2586. 0x33,
  2587. 0x02, 0xC5, 0xAE, 0x23,
  2588. };
  2589. static unsigned char dh512_g[] = {
  2590. 0x02,
  2591. };
  2592. DH *dh;
  2593. BIGNUM *p, *g;
  2594. if ((dh = DH_new()) == NULL)
  2595. return NULL;
  2596. p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL);
  2597. g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL);
  2598. if ((p == NULL) || (g == NULL) || !DH_set0_pqg(dh, p, NULL, g)) {
  2599. DH_free(dh);
  2600. BN_free(p);
  2601. BN_free(g);
  2602. return NULL;
  2603. }
  2604. return dh;
  2605. }
  2606. static DH *get_dh1024()
  2607. {
  2608. static unsigned char dh1024_p[] = {
  2609. 0xF8, 0x81, 0x89, 0x7D, 0x14, 0x24, 0xC5, 0xD1, 0xE6, 0xF7, 0xBF,
  2610. 0x3A,
  2611. 0xE4, 0x90, 0xF4, 0xFC, 0x73, 0xFB, 0x34, 0xB5, 0xFA, 0x4C, 0x56,
  2612. 0xA2,
  2613. 0xEA, 0xA7, 0xE9, 0xC0, 0xC0, 0xCE, 0x89, 0xE1, 0xFA, 0x63, 0x3F,
  2614. 0xB0,
  2615. 0x6B, 0x32, 0x66, 0xF1, 0xD1, 0x7B, 0xB0, 0x00, 0x8F, 0xCA, 0x87,
  2616. 0xC2,
  2617. 0xAE, 0x98, 0x89, 0x26, 0x17, 0xC2, 0x05, 0xD2, 0xEC, 0x08, 0xD0,
  2618. 0x8C,
  2619. 0xFF, 0x17, 0x52, 0x8C, 0xC5, 0x07, 0x93, 0x03, 0xB1, 0xF6, 0x2F,
  2620. 0xB8,
  2621. 0x1C, 0x52, 0x47, 0x27, 0x1B, 0xDB, 0xD1, 0x8D, 0x9D, 0x69, 0x1D,
  2622. 0x52,
  2623. 0x4B, 0x32, 0x81, 0xAA, 0x7F, 0x00, 0xC8, 0xDC, 0xE6, 0xD9, 0xCC,
  2624. 0xC1,
  2625. 0x11, 0x2D, 0x37, 0x34, 0x6C, 0xEA, 0x02, 0x97, 0x4B, 0x0E, 0xBB,
  2626. 0xB1,
  2627. 0x71, 0x33, 0x09, 0x15, 0xFD, 0xDD, 0x23, 0x87, 0x07, 0x5E, 0x89,
  2628. 0xAB,
  2629. 0x6B, 0x7C, 0x5F, 0xEC, 0xA6, 0x24, 0xDC, 0x53,
  2630. };
  2631. static unsigned char dh1024_g[] = {
  2632. 0x02,
  2633. };
  2634. DH *dh;
  2635. BIGNUM *p, *g;
  2636. if ((dh = DH_new()) == NULL)
  2637. return NULL;
  2638. p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
  2639. g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
  2640. if ((p == NULL) || (g == NULL) || !DH_set0_pqg(dh, p, NULL, g)) {
  2641. DH_free(dh);
  2642. BN_free(p);
  2643. BN_free(g);
  2644. return NULL;
  2645. }
  2646. return dh;
  2647. }
  2648. static DH *get_dh1024dsa()
  2649. {
  2650. static unsigned char dh1024_p[] = {
  2651. 0xC8, 0x00, 0xF7, 0x08, 0x07, 0x89, 0x4D, 0x90, 0x53, 0xF3, 0xD5,
  2652. 0x00,
  2653. 0x21, 0x1B, 0xF7, 0x31, 0xA6, 0xA2, 0xDA, 0x23, 0x9A, 0xC7, 0x87,
  2654. 0x19,
  2655. 0x3B, 0x47, 0xB6, 0x8C, 0x04, 0x6F, 0xFF, 0xC6, 0x9B, 0xB8, 0x65,
  2656. 0xD2,
  2657. 0xC2, 0x5F, 0x31, 0x83, 0x4A, 0xA7, 0x5F, 0x2F, 0x88, 0x38, 0xB6,
  2658. 0x55,
  2659. 0xCF, 0xD9, 0x87, 0x6D, 0x6F, 0x9F, 0xDA, 0xAC, 0xA6, 0x48, 0xAF,
  2660. 0xFC,
  2661. 0x33, 0x84, 0x37, 0x5B, 0x82, 0x4A, 0x31, 0x5D, 0xE7, 0xBD, 0x52,
  2662. 0x97,
  2663. 0xA1, 0x77, 0xBF, 0x10, 0x9E, 0x37, 0xEA, 0x64, 0xFA, 0xCA, 0x28,
  2664. 0x8D,
  2665. 0x9D, 0x3B, 0xD2, 0x6E, 0x09, 0x5C, 0x68, 0xC7, 0x45, 0x90, 0xFD,
  2666. 0xBB,
  2667. 0x70, 0xC9, 0x3A, 0xBB, 0xDF, 0xD4, 0x21, 0x0F, 0xC4, 0x6A, 0x3C,
  2668. 0xF6,
  2669. 0x61, 0xCF, 0x3F, 0xD6, 0x13, 0xF1, 0x5F, 0xBC, 0xCF, 0xBC, 0x26,
  2670. 0x9E,
  2671. 0xBC, 0x0B, 0xBD, 0xAB, 0x5D, 0xC9, 0x54, 0x39,
  2672. };
  2673. static unsigned char dh1024_g[] = {
  2674. 0x3B, 0x40, 0x86, 0xE7, 0xF3, 0x6C, 0xDE, 0x67, 0x1C, 0xCC, 0x80,
  2675. 0x05,
  2676. 0x5A, 0xDF, 0xFE, 0xBD, 0x20, 0x27, 0x74, 0x6C, 0x24, 0xC9, 0x03,
  2677. 0xF3,
  2678. 0xE1, 0x8D, 0xC3, 0x7D, 0x98, 0x27, 0x40, 0x08, 0xB8, 0x8C, 0x6A,
  2679. 0xE9,
  2680. 0xBB, 0x1A, 0x3A, 0xD6, 0x86, 0x83, 0x5E, 0x72, 0x41, 0xCE, 0x85,
  2681. 0x3C,
  2682. 0xD2, 0xB3, 0xFC, 0x13, 0xCE, 0x37, 0x81, 0x9E, 0x4C, 0x1C, 0x7B,
  2683. 0x65,
  2684. 0xD3, 0xE6, 0xA6, 0x00, 0xF5, 0x5A, 0x95, 0x43, 0x5E, 0x81, 0xCF,
  2685. 0x60,
  2686. 0xA2, 0x23, 0xFC, 0x36, 0xA7, 0x5D, 0x7A, 0x4C, 0x06, 0x91, 0x6E,
  2687. 0xF6,
  2688. 0x57, 0xEE, 0x36, 0xCB, 0x06, 0xEA, 0xF5, 0x3D, 0x95, 0x49, 0xCB,
  2689. 0xA7,
  2690. 0xDD, 0x81, 0xDF, 0x80, 0x09, 0x4A, 0x97, 0x4D, 0xA8, 0x22, 0x72,
  2691. 0xA1,
  2692. 0x7F, 0xC4, 0x70, 0x56, 0x70, 0xE8, 0x20, 0x10, 0x18, 0x8F, 0x2E,
  2693. 0x60,
  2694. 0x07, 0xE7, 0x68, 0x1A, 0x82, 0x5D, 0x32, 0xA2,
  2695. };
  2696. DH *dh;
  2697. BIGNUM *p, *g;
  2698. if ((dh = DH_new()) == NULL)
  2699. return NULL;
  2700. p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
  2701. g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
  2702. if ((p == NULL) || (g == NULL) || !DH_set0_pqg(dh, p, NULL, g)) {
  2703. DH_free(dh);
  2704. BN_free(p);
  2705. BN_free(g);
  2706. return NULL;
  2707. }
  2708. DH_set_length(dh, 160);
  2709. return dh;
  2710. }
  2711. #endif
  2712. #ifndef OPENSSL_NO_PSK
  2713. /* convert the PSK key (psk_key) in ascii to binary (psk) */
  2714. static int psk_key2bn(const char *pskkey, unsigned char *psk,
  2715. unsigned int max_psk_len)
  2716. {
  2717. int ret;
  2718. BIGNUM *bn = NULL;
  2719. ret = BN_hex2bn(&bn, pskkey);
  2720. if (!ret) {
  2721. BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
  2722. pskkey);
  2723. BN_free(bn);
  2724. return 0;
  2725. }
  2726. if (BN_num_bytes(bn) > (int)max_psk_len) {
  2727. BIO_printf(bio_err,
  2728. "psk buffer of callback is too small (%d) for key (%d)\n",
  2729. max_psk_len, BN_num_bytes(bn));
  2730. BN_free(bn);
  2731. return 0;
  2732. }
  2733. ret = BN_bn2bin(bn, psk);
  2734. BN_free(bn);
  2735. return ret;
  2736. }
  2737. static unsigned int psk_client_callback(SSL *ssl, const char *hint,
  2738. char *identity,
  2739. unsigned int max_identity_len,
  2740. unsigned char *psk,
  2741. unsigned int max_psk_len)
  2742. {
  2743. int ret;
  2744. unsigned int psk_len = 0;
  2745. ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
  2746. if (ret < 0)
  2747. goto out_err;
  2748. if (debug)
  2749. fprintf(stderr, "client: created identity '%s' len=%d\n", identity,
  2750. ret);
  2751. ret = psk_key2bn(psk_key, psk, max_psk_len);
  2752. if (ret < 0)
  2753. goto out_err;
  2754. psk_len = ret;
  2755. out_err:
  2756. return psk_len;
  2757. }
  2758. static unsigned int psk_server_callback(SSL *ssl, const char *identity,
  2759. unsigned char *psk,
  2760. unsigned int max_psk_len)
  2761. {
  2762. unsigned int psk_len = 0;
  2763. if (strcmp(identity, "Client_identity") != 0) {
  2764. BIO_printf(bio_err, "server: PSK error: client identity not found\n");
  2765. return 0;
  2766. }
  2767. psk_len = psk_key2bn(psk_key, psk, max_psk_len);
  2768. return psk_len;
  2769. }
  2770. #endif