ssltest_old.c 100 KB

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