ssltest_old.c 102 KB

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