ssltest_old.c 102 KB

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