ssltest_old.c 102 KB

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