2
0

ssltest_old.c 99 KB

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