ssl_old_test.c 99 KB

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