ssl_old_test.c 99 KB

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