ssl_old_test.c 99 KB

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