s_client.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2005 Nokia. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include "internal/e_os.h"
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <openssl/e_os2.h>
  17. #ifndef OPENSSL_NO_SOCK
  18. /*
  19. * With IPv6, it looks like Digital has mixed up the proper order of
  20. * recursive header file inclusion, resulting in the compiler complaining
  21. * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
  22. * needed to have fileno() declared correctly... So let's define u_int
  23. */
  24. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  25. # define __U_INT
  26. typedef unsigned int u_int;
  27. #endif
  28. #include "apps.h"
  29. #include "progs.h"
  30. #include <openssl/x509.h>
  31. #include <openssl/ssl.h>
  32. #include <openssl/err.h>
  33. #include <openssl/pem.h>
  34. #include <openssl/rand.h>
  35. #include <openssl/ocsp.h>
  36. #include <openssl/bn.h>
  37. #include <openssl/trace.h>
  38. #include <openssl/async.h>
  39. #ifndef OPENSSL_NO_CT
  40. # include <openssl/ct.h>
  41. #endif
  42. #include "s_apps.h"
  43. #include "timeouts.h"
  44. #include "internal/sockets.h"
  45. #if defined(__has_feature)
  46. # if __has_feature(memory_sanitizer)
  47. # include <sanitizer/msan_interface.h>
  48. # endif
  49. #endif
  50. #undef BUFSIZZ
  51. #define BUFSIZZ 1024*8
  52. #define S_CLIENT_IRC_READ_TIMEOUT 8
  53. static char *prog;
  54. static int c_debug = 0;
  55. static int c_showcerts = 0;
  56. static char *keymatexportlabel = NULL;
  57. static int keymatexportlen = 20;
  58. static BIO *bio_c_out = NULL;
  59. static int c_quiet = 0;
  60. static char *sess_out = NULL;
  61. static SSL_SESSION *psksess = NULL;
  62. static void print_stuff(BIO *berr, SSL *con, int full);
  63. #ifndef OPENSSL_NO_OCSP
  64. static int ocsp_resp_cb(SSL *s, void *arg);
  65. #endif
  66. static int ldap_ExtendedResponse_parse(const char *buf, long rem);
  67. static int is_dNS_name(const char *host);
  68. static int saved_errno;
  69. static void save_errno(void)
  70. {
  71. saved_errno = errno;
  72. errno = 0;
  73. }
  74. static int restore_errno(void)
  75. {
  76. int ret = errno;
  77. errno = saved_errno;
  78. return ret;
  79. }
  80. /* Default PSK identity and key */
  81. static char *psk_identity = "Client_identity";
  82. #ifndef OPENSSL_NO_PSK
  83. static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
  84. unsigned int max_identity_len,
  85. unsigned char *psk,
  86. unsigned int max_psk_len)
  87. {
  88. int ret;
  89. long key_len;
  90. unsigned char *key;
  91. if (c_debug)
  92. BIO_printf(bio_c_out, "psk_client_cb\n");
  93. if (!hint) {
  94. /* no ServerKeyExchange message */
  95. if (c_debug)
  96. BIO_printf(bio_c_out,
  97. "NULL received PSK identity hint, continuing anyway\n");
  98. } else if (c_debug) {
  99. BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint);
  100. }
  101. /*
  102. * lookup PSK identity and PSK key based on the given identity hint here
  103. */
  104. ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity);
  105. if (ret < 0 || (unsigned int)ret > max_identity_len)
  106. goto out_err;
  107. if (c_debug)
  108. BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity,
  109. ret);
  110. /* convert the PSK key to binary */
  111. key = OPENSSL_hexstr2buf(psk_key, &key_len);
  112. if (key == NULL) {
  113. BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
  114. psk_key);
  115. return 0;
  116. }
  117. if (max_psk_len > INT_MAX || key_len > (long)max_psk_len) {
  118. BIO_printf(bio_err,
  119. "psk buffer of callback is too small (%d) for key (%ld)\n",
  120. max_psk_len, key_len);
  121. OPENSSL_free(key);
  122. return 0;
  123. }
  124. memcpy(psk, key, key_len);
  125. OPENSSL_free(key);
  126. if (c_debug)
  127. BIO_printf(bio_c_out, "created PSK len=%ld\n", key_len);
  128. return key_len;
  129. out_err:
  130. if (c_debug)
  131. BIO_printf(bio_err, "Error in PSK client callback\n");
  132. return 0;
  133. }
  134. #endif
  135. const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
  136. const unsigned char tls13_aes256gcmsha384_id[] = { 0x13, 0x02 };
  137. static int psk_use_session_cb(SSL *s, const EVP_MD *md,
  138. const unsigned char **id, size_t *idlen,
  139. SSL_SESSION **sess)
  140. {
  141. SSL_SESSION *usesess = NULL;
  142. const SSL_CIPHER *cipher = NULL;
  143. if (psksess != NULL) {
  144. SSL_SESSION_up_ref(psksess);
  145. usesess = psksess;
  146. } else {
  147. long key_len;
  148. unsigned char *key = OPENSSL_hexstr2buf(psk_key, &key_len);
  149. if (key == NULL) {
  150. BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
  151. psk_key);
  152. return 0;
  153. }
  154. /* We default to SHA-256 */
  155. cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id);
  156. if (cipher == NULL) {
  157. BIO_printf(bio_err, "Error finding suitable ciphersuite\n");
  158. OPENSSL_free(key);
  159. return 0;
  160. }
  161. usesess = SSL_SESSION_new();
  162. if (usesess == NULL
  163. || !SSL_SESSION_set1_master_key(usesess, key, key_len)
  164. || !SSL_SESSION_set_cipher(usesess, cipher)
  165. || !SSL_SESSION_set_protocol_version(usesess, TLS1_3_VERSION)) {
  166. OPENSSL_free(key);
  167. goto err;
  168. }
  169. OPENSSL_free(key);
  170. }
  171. cipher = SSL_SESSION_get0_cipher(usesess);
  172. if (cipher == NULL)
  173. goto err;
  174. if (md != NULL && SSL_CIPHER_get_handshake_digest(cipher) != md) {
  175. /* PSK not usable, ignore it */
  176. *id = NULL;
  177. *idlen = 0;
  178. *sess = NULL;
  179. SSL_SESSION_free(usesess);
  180. } else {
  181. *sess = usesess;
  182. *id = (unsigned char *)psk_identity;
  183. *idlen = strlen(psk_identity);
  184. }
  185. return 1;
  186. err:
  187. SSL_SESSION_free(usesess);
  188. return 0;
  189. }
  190. /* This is a context that we pass to callbacks */
  191. typedef struct tlsextctx_st {
  192. BIO *biodebug;
  193. int ack;
  194. } tlsextctx;
  195. static int ssl_servername_cb(SSL *s, int *ad, void *arg)
  196. {
  197. tlsextctx *p = (tlsextctx *) arg;
  198. const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  199. if (SSL_get_servername_type(s) != -1)
  200. p->ack = !SSL_session_reused(s) && hn != NULL;
  201. else
  202. BIO_printf(bio_err, "Can't use SSL_get_servername\n");
  203. return SSL_TLSEXT_ERR_OK;
  204. }
  205. #ifndef OPENSSL_NO_NEXTPROTONEG
  206. /* This the context that we pass to next_proto_cb */
  207. typedef struct tlsextnextprotoctx_st {
  208. unsigned char *data;
  209. size_t len;
  210. int status;
  211. } tlsextnextprotoctx;
  212. static tlsextnextprotoctx next_proto;
  213. static int next_proto_cb(SSL *s, unsigned char **out, unsigned char *outlen,
  214. const unsigned char *in, unsigned int inlen,
  215. void *arg)
  216. {
  217. tlsextnextprotoctx *ctx = arg;
  218. if (!c_quiet) {
  219. /* We can assume that |in| is syntactically valid. */
  220. unsigned i;
  221. BIO_printf(bio_c_out, "Protocols advertised by server: ");
  222. for (i = 0; i < inlen;) {
  223. if (i)
  224. BIO_write(bio_c_out, ", ", 2);
  225. BIO_write(bio_c_out, &in[i + 1], in[i]);
  226. i += in[i] + 1;
  227. }
  228. BIO_write(bio_c_out, "\n", 1);
  229. }
  230. ctx->status =
  231. SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len);
  232. return SSL_TLSEXT_ERR_OK;
  233. }
  234. #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
  235. static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
  236. const unsigned char *in, size_t inlen,
  237. int *al, void *arg)
  238. {
  239. char pem_name[100];
  240. unsigned char ext_buf[4 + 65536];
  241. /* Reconstruct the type/len fields prior to extension data */
  242. inlen &= 0xffff; /* for formal memcmpy correctness */
  243. ext_buf[0] = (unsigned char)(ext_type >> 8);
  244. ext_buf[1] = (unsigned char)(ext_type);
  245. ext_buf[2] = (unsigned char)(inlen >> 8);
  246. ext_buf[3] = (unsigned char)(inlen);
  247. memcpy(ext_buf + 4, in, inlen);
  248. BIO_snprintf(pem_name, sizeof(pem_name), "SERVERINFO FOR EXTENSION %d",
  249. ext_type);
  250. PEM_write_bio(bio_c_out, pem_name, "", ext_buf, 4 + inlen);
  251. return 1;
  252. }
  253. /*
  254. * Hex decoder that tolerates optional whitespace. Returns number of bytes
  255. * produced, advances inptr to end of input string.
  256. */
  257. static ossl_ssize_t hexdecode(const char **inptr, void *result)
  258. {
  259. unsigned char **out = (unsigned char **)result;
  260. const char *in = *inptr;
  261. unsigned char *ret = app_malloc(strlen(in) / 2, "hexdecode");
  262. unsigned char *cp = ret;
  263. uint8_t byte;
  264. int nibble = 0;
  265. if (ret == NULL)
  266. return -1;
  267. for (byte = 0; *in; ++in) {
  268. int x;
  269. if (isspace(_UC(*in)))
  270. continue;
  271. x = OPENSSL_hexchar2int(*in);
  272. if (x < 0) {
  273. OPENSSL_free(ret);
  274. return 0;
  275. }
  276. byte |= (char)x;
  277. if ((nibble ^= 1) == 0) {
  278. *cp++ = byte;
  279. byte = 0;
  280. } else {
  281. byte <<= 4;
  282. }
  283. }
  284. if (nibble != 0) {
  285. OPENSSL_free(ret);
  286. return 0;
  287. }
  288. *inptr = in;
  289. return cp - (*out = ret);
  290. }
  291. /*
  292. * Decode unsigned 0..255, returns 1 on success, <= 0 on failure. Advances
  293. * inptr to next field skipping leading whitespace.
  294. */
  295. static ossl_ssize_t checked_uint8(const char **inptr, void *out)
  296. {
  297. uint8_t *result = (uint8_t *)out;
  298. const char *in = *inptr;
  299. char *endp;
  300. long v;
  301. int e;
  302. save_errno();
  303. v = strtol(in, &endp, 10);
  304. e = restore_errno();
  305. if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
  306. endp == in || !isspace(_UC(*endp)) ||
  307. v != (*result = (uint8_t) v)) {
  308. return -1;
  309. }
  310. for (in = endp; isspace(_UC(*in)); ++in)
  311. continue;
  312. *inptr = in;
  313. return 1;
  314. }
  315. struct tlsa_field {
  316. void *var;
  317. const char *name;
  318. ossl_ssize_t (*parser)(const char **, void *);
  319. };
  320. static int tlsa_import_rr(SSL *con, const char *rrdata)
  321. {
  322. /* Not necessary to re-init these values; the "parsers" do that. */
  323. static uint8_t usage;
  324. static uint8_t selector;
  325. static uint8_t mtype;
  326. static unsigned char *data;
  327. static struct tlsa_field tlsa_fields[] = {
  328. { &usage, "usage", checked_uint8 },
  329. { &selector, "selector", checked_uint8 },
  330. { &mtype, "mtype", checked_uint8 },
  331. { &data, "data", hexdecode },
  332. { NULL, }
  333. };
  334. struct tlsa_field *f;
  335. int ret;
  336. const char *cp = rrdata;
  337. ossl_ssize_t len = 0;
  338. for (f = tlsa_fields; f->var; ++f) {
  339. /* Returns number of bytes produced, advances cp to next field */
  340. if ((len = f->parser(&cp, f->var)) <= 0) {
  341. BIO_printf(bio_err, "%s: warning: bad TLSA %s field in: %s\n",
  342. prog, f->name, rrdata);
  343. return 0;
  344. }
  345. }
  346. /* The data field is last, so len is its length */
  347. ret = SSL_dane_tlsa_add(con, usage, selector, mtype, data, len);
  348. OPENSSL_free(data);
  349. if (ret == 0) {
  350. ERR_print_errors(bio_err);
  351. BIO_printf(bio_err, "%s: warning: unusable TLSA rrdata: %s\n",
  352. prog, rrdata);
  353. return 0;
  354. }
  355. if (ret < 0) {
  356. ERR_print_errors(bio_err);
  357. BIO_printf(bio_err, "%s: warning: error loading TLSA rrdata: %s\n",
  358. prog, rrdata);
  359. return 0;
  360. }
  361. return ret;
  362. }
  363. static int tlsa_import_rrset(SSL *con, STACK_OF(OPENSSL_STRING) *rrset)
  364. {
  365. int num = sk_OPENSSL_STRING_num(rrset);
  366. int count = 0;
  367. int i;
  368. for (i = 0; i < num; ++i) {
  369. char *rrdata = sk_OPENSSL_STRING_value(rrset, i);
  370. if (tlsa_import_rr(con, rrdata) > 0)
  371. ++count;
  372. }
  373. return count > 0;
  374. }
  375. typedef enum OPTION_choice {
  376. OPT_COMMON,
  377. OPT_4, OPT_6, OPT_HOST, OPT_PORT, OPT_CONNECT, OPT_BIND, OPT_UNIX,
  378. OPT_XMPPHOST, OPT_VERIFY, OPT_NAMEOPT,
  379. OPT_CERT, OPT_CRL, OPT_CRL_DOWNLOAD, OPT_SESS_OUT, OPT_SESS_IN,
  380. OPT_CERTFORM, OPT_CRLFORM, OPT_VERIFY_RET_ERROR, OPT_VERIFY_QUIET,
  381. OPT_BRIEF, OPT_PREXIT, OPT_NO_INTERACTIVE, OPT_CRLF, OPT_QUIET, OPT_NBIO,
  382. OPT_SSL_CLIENT_ENGINE, OPT_IGN_EOF, OPT_NO_IGN_EOF,
  383. OPT_DEBUG, OPT_TLSEXTDEBUG, OPT_STATUS, OPT_WDEBUG,
  384. OPT_MSG, OPT_MSGFILE, OPT_ENGINE, OPT_TRACE, OPT_SECURITY_DEBUG,
  385. OPT_SECURITY_DEBUG_VERBOSE, OPT_SHOWCERTS, OPT_NBIO_TEST, OPT_STATE,
  386. OPT_PSK_IDENTITY, OPT_PSK, OPT_PSK_SESS,
  387. #ifndef OPENSSL_NO_SRP
  388. OPT_SRPUSER, OPT_SRPPASS, OPT_SRP_STRENGTH, OPT_SRP_LATEUSER,
  389. OPT_SRP_MOREGROUPS,
  390. #endif
  391. OPT_SSL3, OPT_SSL_CONFIG,
  392. OPT_TLS1_3, OPT_TLS1_2, OPT_TLS1_1, OPT_TLS1, OPT_DTLS, OPT_DTLS1,
  393. OPT_DTLS1_2, OPT_SCTP, OPT_TIMEOUT, OPT_MTU, OPT_KEYFORM, OPT_PASS,
  394. OPT_CERT_CHAIN, OPT_KEY, OPT_RECONNECT, OPT_BUILD_CHAIN,
  395. OPT_NEXTPROTONEG, OPT_ALPN,
  396. OPT_CAPATH, OPT_NOCAPATH, OPT_CHAINCAPATH, OPT_VERIFYCAPATH,
  397. OPT_CAFILE, OPT_NOCAFILE, OPT_CHAINCAFILE, OPT_VERIFYCAFILE,
  398. OPT_CASTORE, OPT_NOCASTORE, OPT_CHAINCASTORE, OPT_VERIFYCASTORE,
  399. OPT_SERVERINFO, OPT_STARTTLS, OPT_SERVERNAME, OPT_NOSERVERNAME, OPT_ASYNC,
  400. OPT_USE_SRTP, OPT_KEYMATEXPORT, OPT_KEYMATEXPORTLEN, OPT_PROTOHOST,
  401. OPT_MAXFRAGLEN, OPT_MAX_SEND_FRAG, OPT_SPLIT_SEND_FRAG, OPT_MAX_PIPELINES,
  402. OPT_READ_BUF, OPT_KEYLOG_FILE, OPT_EARLY_DATA, OPT_REQCAFILE,
  403. OPT_TFO,
  404. OPT_V_ENUM,
  405. OPT_X_ENUM,
  406. OPT_S_ENUM, OPT_IGNORE_UNEXPECTED_EOF,
  407. OPT_FALLBACKSCSV, OPT_NOCMDS, OPT_PROXY, OPT_PROXY_USER, OPT_PROXY_PASS,
  408. OPT_DANE_TLSA_DOMAIN,
  409. #ifndef OPENSSL_NO_CT
  410. OPT_CT, OPT_NOCT, OPT_CTLOG_FILE,
  411. #endif
  412. OPT_DANE_TLSA_RRDATA, OPT_DANE_EE_NO_NAME,
  413. OPT_ENABLE_PHA,
  414. OPT_SCTP_LABEL_BUG,
  415. OPT_KTLS,
  416. OPT_R_ENUM, OPT_PROV_ENUM
  417. } OPTION_CHOICE;
  418. const OPTIONS s_client_options[] = {
  419. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [host:port]\n"},
  420. OPT_SECTION("General"),
  421. {"help", OPT_HELP, '-', "Display this summary"},
  422. #ifndef OPENSSL_NO_ENGINE
  423. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  424. {"ssl_client_engine", OPT_SSL_CLIENT_ENGINE, 's',
  425. "Specify engine to be used for client certificate operations"},
  426. #endif
  427. {"ssl_config", OPT_SSL_CONFIG, 's', "Use specified section for SSL_CTX configuration"},
  428. #ifndef OPENSSL_NO_CT
  429. {"ct", OPT_CT, '-', "Request and parse SCTs (also enables OCSP stapling)"},
  430. {"noct", OPT_NOCT, '-', "Do not request or parse SCTs (default)"},
  431. {"ctlogfile", OPT_CTLOG_FILE, '<', "CT log list CONF file"},
  432. #endif
  433. OPT_SECTION("Network"),
  434. {"host", OPT_HOST, 's', "Use -connect instead"},
  435. {"port", OPT_PORT, 'p', "Use -connect instead"},
  436. {"connect", OPT_CONNECT, 's',
  437. "TCP/IP where to connect; default: " PORT ")"},
  438. {"bind", OPT_BIND, 's', "bind local address for connection"},
  439. {"proxy", OPT_PROXY, 's',
  440. "Connect to via specified proxy to the real server"},
  441. {"proxy_user", OPT_PROXY_USER, 's', "UserID for proxy authentication"},
  442. {"proxy_pass", OPT_PROXY_PASS, 's', "Proxy authentication password source"},
  443. #ifdef AF_UNIX
  444. {"unix", OPT_UNIX, 's', "Connect over the specified Unix-domain socket"},
  445. #endif
  446. {"4", OPT_4, '-', "Use IPv4 only"},
  447. #ifdef AF_INET6
  448. {"6", OPT_6, '-', "Use IPv6 only"},
  449. #endif
  450. {"maxfraglen", OPT_MAXFRAGLEN, 'p',
  451. "Enable Maximum Fragment Length Negotiation (len values: 512, 1024, 2048 and 4096)"},
  452. {"max_send_frag", OPT_MAX_SEND_FRAG, 'p', "Maximum Size of send frames "},
  453. {"split_send_frag", OPT_SPLIT_SEND_FRAG, 'p',
  454. "Size used to split data for encrypt pipelines"},
  455. {"max_pipelines", OPT_MAX_PIPELINES, 'p',
  456. "Maximum number of encrypt/decrypt pipelines to be used"},
  457. {"read_buf", OPT_READ_BUF, 'p',
  458. "Default read buffer size to be used for connections"},
  459. {"fallback_scsv", OPT_FALLBACKSCSV, '-', "Send the fallback SCSV"},
  460. OPT_SECTION("Identity"),
  461. {"cert", OPT_CERT, '<', "Client certificate file to use"},
  462. {"certform", OPT_CERTFORM, 'F',
  463. "Client certificate file format (PEM/DER/P12); has no effect"},
  464. {"cert_chain", OPT_CERT_CHAIN, '<',
  465. "Client certificate chain file (in PEM format)"},
  466. {"build_chain", OPT_BUILD_CHAIN, '-', "Build client certificate chain"},
  467. {"key", OPT_KEY, 's', "Private key file to use; default: -cert file"},
  468. {"keyform", OPT_KEYFORM, 'E', "Key format (ENGINE, other values ignored)"},
  469. {"pass", OPT_PASS, 's', "Private key and cert file pass phrase source"},
  470. {"verify", OPT_VERIFY, 'p', "Turn on peer certificate verification"},
  471. {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
  472. {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
  473. {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"},
  474. {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
  475. {"no-CAfile", OPT_NOCAFILE, '-',
  476. "Do not load the default certificates file"},
  477. {"no-CApath", OPT_NOCAPATH, '-',
  478. "Do not load certificates from the default certificates directory"},
  479. {"no-CAstore", OPT_NOCASTORE, '-',
  480. "Do not load certificates from the default certificates store"},
  481. {"requestCAfile", OPT_REQCAFILE, '<',
  482. "PEM format file of CA names to send to the server"},
  483. #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
  484. {"tfo", OPT_TFO, '-', "Connect using TCP Fast Open"},
  485. #endif
  486. {"dane_tlsa_domain", OPT_DANE_TLSA_DOMAIN, 's', "DANE TLSA base domain"},
  487. {"dane_tlsa_rrdata", OPT_DANE_TLSA_RRDATA, 's',
  488. "DANE TLSA rrdata presentation form"},
  489. {"dane_ee_no_namechecks", OPT_DANE_EE_NO_NAME, '-',
  490. "Disable name checks when matching DANE-EE(3) TLSA records"},
  491. {"psk_identity", OPT_PSK_IDENTITY, 's', "PSK identity"},
  492. {"psk", OPT_PSK, 's', "PSK in hex (without 0x)"},
  493. {"psk_session", OPT_PSK_SESS, '<', "File to read PSK SSL session from"},
  494. {"name", OPT_PROTOHOST, 's',
  495. "Hostname to use for \"-starttls lmtp\", \"-starttls smtp\" or \"-starttls xmpp[-server]\""},
  496. OPT_SECTION("Session"),
  497. {"reconnect", OPT_RECONNECT, '-',
  498. "Drop and re-make the connection with the same Session-ID"},
  499. {"sess_out", OPT_SESS_OUT, '>', "File to write SSL session to"},
  500. {"sess_in", OPT_SESS_IN, '<', "File to read SSL session from"},
  501. OPT_SECTION("Input/Output"),
  502. {"crlf", OPT_CRLF, '-', "Convert LF from terminal into CRLF"},
  503. {"quiet", OPT_QUIET, '-', "No s_client output"},
  504. {"ign_eof", OPT_IGN_EOF, '-', "Ignore input eof (default when -quiet)"},
  505. {"no_ign_eof", OPT_NO_IGN_EOF, '-', "Don't ignore input eof"},
  506. {"starttls", OPT_STARTTLS, 's',
  507. "Use the appropriate STARTTLS command before starting TLS"},
  508. {"xmpphost", OPT_XMPPHOST, 's',
  509. "Alias of -name option for \"-starttls xmpp[-server]\""},
  510. {"brief", OPT_BRIEF, '-',
  511. "Restrict output to brief summary of connection parameters"},
  512. {"prexit", OPT_PREXIT, '-',
  513. "Print session information when the program exits"},
  514. {"no-interactive", OPT_NO_INTERACTIVE, '-',
  515. "Don't run the client in the interactive mode"},
  516. OPT_SECTION("Debug"),
  517. {"showcerts", OPT_SHOWCERTS, '-',
  518. "Show all certificates sent by the server"},
  519. {"debug", OPT_DEBUG, '-', "Extra output"},
  520. {"msg", OPT_MSG, '-', "Show protocol messages"},
  521. {"msgfile", OPT_MSGFILE, '>',
  522. "File to send output of -msg or -trace, instead of stdout"},
  523. {"nbio_test", OPT_NBIO_TEST, '-', "More ssl protocol testing"},
  524. {"state", OPT_STATE, '-', "Print the ssl states"},
  525. {"keymatexport", OPT_KEYMATEXPORT, 's',
  526. "Export keying material using label"},
  527. {"keymatexportlen", OPT_KEYMATEXPORTLEN, 'p',
  528. "Export len bytes of keying material; default 20"},
  529. {"security_debug", OPT_SECURITY_DEBUG, '-',
  530. "Enable security debug messages"},
  531. {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE, '-',
  532. "Output more security debug output"},
  533. #ifndef OPENSSL_NO_SSL_TRACE
  534. {"trace", OPT_TRACE, '-', "Show trace output of protocol messages"},
  535. #endif
  536. #ifdef WATT32
  537. {"wdebug", OPT_WDEBUG, '-', "WATT-32 tcp debugging"},
  538. #endif
  539. {"keylogfile", OPT_KEYLOG_FILE, '>', "Write TLS secrets to file"},
  540. {"nocommands", OPT_NOCMDS, '-', "Do not use interactive command letters"},
  541. {"servername", OPT_SERVERNAME, 's',
  542. "Set TLS extension servername (SNI) in ClientHello (default)"},
  543. {"noservername", OPT_NOSERVERNAME, '-',
  544. "Do not send the server name (SNI) extension in the ClientHello"},
  545. {"tlsextdebug", OPT_TLSEXTDEBUG, '-',
  546. "Hex dump of all TLS extensions received"},
  547. {"ignore_unexpected_eof", OPT_IGNORE_UNEXPECTED_EOF, '-',
  548. "Do not treat lack of close_notify from a peer as an error"},
  549. #ifndef OPENSSL_NO_OCSP
  550. {"status", OPT_STATUS, '-', "Request certificate status from server"},
  551. #endif
  552. {"serverinfo", OPT_SERVERINFO, 's',
  553. "types Send empty ClientHello extensions (comma-separated numbers)"},
  554. {"alpn", OPT_ALPN, 's',
  555. "Enable ALPN extension, considering named protocols supported (comma-separated list)"},
  556. {"async", OPT_ASYNC, '-', "Support asynchronous operation"},
  557. {"nbio", OPT_NBIO, '-', "Use non-blocking IO"},
  558. OPT_SECTION("Protocol and version"),
  559. #ifndef OPENSSL_NO_SSL3
  560. {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
  561. #endif
  562. #ifndef OPENSSL_NO_TLS1
  563. {"tls1", OPT_TLS1, '-', "Just use TLSv1"},
  564. #endif
  565. #ifndef OPENSSL_NO_TLS1_1
  566. {"tls1_1", OPT_TLS1_1, '-', "Just use TLSv1.1"},
  567. #endif
  568. #ifndef OPENSSL_NO_TLS1_2
  569. {"tls1_2", OPT_TLS1_2, '-', "Just use TLSv1.2"},
  570. #endif
  571. #ifndef OPENSSL_NO_TLS1_3
  572. {"tls1_3", OPT_TLS1_3, '-', "Just use TLSv1.3"},
  573. #endif
  574. #ifndef OPENSSL_NO_DTLS
  575. {"dtls", OPT_DTLS, '-', "Use any version of DTLS"},
  576. {"timeout", OPT_TIMEOUT, '-',
  577. "Enable send/receive timeout on DTLS connections"},
  578. {"mtu", OPT_MTU, 'p', "Set the link layer MTU"},
  579. #endif
  580. #ifndef OPENSSL_NO_DTLS1
  581. {"dtls1", OPT_DTLS1, '-', "Just use DTLSv1"},
  582. #endif
  583. #ifndef OPENSSL_NO_DTLS1_2
  584. {"dtls1_2", OPT_DTLS1_2, '-', "Just use DTLSv1.2"},
  585. #endif
  586. #ifndef OPENSSL_NO_SCTP
  587. {"sctp", OPT_SCTP, '-', "Use SCTP"},
  588. {"sctp_label_bug", OPT_SCTP_LABEL_BUG, '-', "Enable SCTP label length bug"},
  589. #endif
  590. #ifndef OPENSSL_NO_NEXTPROTONEG
  591. {"nextprotoneg", OPT_NEXTPROTONEG, 's',
  592. "Enable NPN extension, considering named protocols supported (comma-separated list)"},
  593. #endif
  594. {"early_data", OPT_EARLY_DATA, '<', "File to send as early data"},
  595. {"enable_pha", OPT_ENABLE_PHA, '-', "Enable post-handshake-authentication"},
  596. #ifndef OPENSSL_NO_SRTP
  597. {"use_srtp", OPT_USE_SRTP, 's',
  598. "Offer SRTP key management with a colon-separated profile list"},
  599. #endif
  600. #ifndef OPENSSL_NO_SRP
  601. {"srpuser", OPT_SRPUSER, 's', "(deprecated) SRP authentication for 'user'"},
  602. {"srppass", OPT_SRPPASS, 's', "(deprecated) Password for 'user'"},
  603. {"srp_lateuser", OPT_SRP_LATEUSER, '-',
  604. "(deprecated) SRP username into second ClientHello message"},
  605. {"srp_moregroups", OPT_SRP_MOREGROUPS, '-',
  606. "(deprecated) Tolerate other than the known g N values."},
  607. {"srp_strength", OPT_SRP_STRENGTH, 'p',
  608. "(deprecated) Minimal length in bits for N"},
  609. #endif
  610. #ifndef OPENSSL_NO_KTLS
  611. {"ktls", OPT_KTLS, '-', "Enable Kernel TLS for sending and receiving"},
  612. #endif
  613. OPT_R_OPTIONS,
  614. OPT_S_OPTIONS,
  615. OPT_V_OPTIONS,
  616. {"CRL", OPT_CRL, '<', "CRL file to use"},
  617. {"crl_download", OPT_CRL_DOWNLOAD, '-', "Download CRL from distribution points"},
  618. {"CRLform", OPT_CRLFORM, 'F', "CRL format (PEM or DER); default PEM"},
  619. {"verify_return_error", OPT_VERIFY_RET_ERROR, '-',
  620. "Close connection on verification error"},
  621. {"verify_quiet", OPT_VERIFY_QUIET, '-', "Restrict verify output to errors"},
  622. {"chainCAfile", OPT_CHAINCAFILE, '<',
  623. "CA file for certificate chain (PEM format)"},
  624. {"chainCApath", OPT_CHAINCAPATH, '/',
  625. "Use dir as certificate store path to build CA certificate chain"},
  626. {"chainCAstore", OPT_CHAINCASTORE, ':',
  627. "CA store URI for certificate chain"},
  628. {"verifyCAfile", OPT_VERIFYCAFILE, '<',
  629. "CA file for certificate verification (PEM format)"},
  630. {"verifyCApath", OPT_VERIFYCAPATH, '/',
  631. "Use dir as certificate store path to verify CA certificate"},
  632. {"verifyCAstore", OPT_VERIFYCASTORE, ':',
  633. "CA store URI for certificate verification"},
  634. OPT_X_OPTIONS,
  635. OPT_PROV_OPTIONS,
  636. OPT_PARAMETERS(),
  637. {"host:port", 0, 0, "Where to connect; same as -connect option"},
  638. {NULL}
  639. };
  640. typedef enum PROTOCOL_choice {
  641. PROTO_OFF,
  642. PROTO_SMTP,
  643. PROTO_POP3,
  644. PROTO_IMAP,
  645. PROTO_FTP,
  646. PROTO_TELNET,
  647. PROTO_XMPP,
  648. PROTO_XMPP_SERVER,
  649. PROTO_CONNECT,
  650. PROTO_IRC,
  651. PROTO_MYSQL,
  652. PROTO_POSTGRES,
  653. PROTO_LMTP,
  654. PROTO_NNTP,
  655. PROTO_SIEVE,
  656. PROTO_LDAP
  657. } PROTOCOL_CHOICE;
  658. static const OPT_PAIR services[] = {
  659. {"smtp", PROTO_SMTP},
  660. {"pop3", PROTO_POP3},
  661. {"imap", PROTO_IMAP},
  662. {"ftp", PROTO_FTP},
  663. {"xmpp", PROTO_XMPP},
  664. {"xmpp-server", PROTO_XMPP_SERVER},
  665. {"telnet", PROTO_TELNET},
  666. {"irc", PROTO_IRC},
  667. {"mysql", PROTO_MYSQL},
  668. {"postgres", PROTO_POSTGRES},
  669. {"lmtp", PROTO_LMTP},
  670. {"nntp", PROTO_NNTP},
  671. {"sieve", PROTO_SIEVE},
  672. {"ldap", PROTO_LDAP},
  673. {NULL, 0}
  674. };
  675. #define IS_INET_FLAG(o) \
  676. (o == OPT_4 || o == OPT_6 || o == OPT_HOST || o == OPT_PORT || o == OPT_CONNECT)
  677. #define IS_UNIX_FLAG(o) (o == OPT_UNIX)
  678. #define IS_PROT_FLAG(o) \
  679. (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
  680. || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
  681. /* Free |*dest| and optionally set it to a copy of |source|. */
  682. static void freeandcopy(char **dest, const char *source)
  683. {
  684. OPENSSL_free(*dest);
  685. *dest = NULL;
  686. if (source != NULL)
  687. *dest = OPENSSL_strdup(source);
  688. }
  689. static int new_session_cb(SSL *s, SSL_SESSION *sess)
  690. {
  691. if (sess_out != NULL) {
  692. BIO *stmp = BIO_new_file(sess_out, "w");
  693. if (stmp == NULL) {
  694. BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
  695. } else {
  696. PEM_write_bio_SSL_SESSION(stmp, sess);
  697. BIO_free(stmp);
  698. }
  699. }
  700. /*
  701. * Session data gets dumped on connection for TLSv1.2 and below, and on
  702. * arrival of the NewSessionTicket for TLSv1.3.
  703. */
  704. if (SSL_version(s) == TLS1_3_VERSION) {
  705. BIO_printf(bio_c_out,
  706. "---\nPost-Handshake New Session Ticket arrived:\n");
  707. SSL_SESSION_print(bio_c_out, sess);
  708. BIO_printf(bio_c_out, "---\n");
  709. }
  710. /*
  711. * We always return a "fail" response so that the session gets freed again
  712. * because we haven't used the reference.
  713. */
  714. return 0;
  715. }
  716. int s_client_main(int argc, char **argv)
  717. {
  718. BIO *sbio;
  719. EVP_PKEY *key = NULL;
  720. SSL *con = NULL;
  721. SSL_CTX *ctx = NULL;
  722. STACK_OF(X509) *chain = NULL;
  723. X509 *cert = NULL;
  724. X509_VERIFY_PARAM *vpm = NULL;
  725. SSL_EXCERT *exc = NULL;
  726. SSL_CONF_CTX *cctx = NULL;
  727. STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
  728. char *dane_tlsa_domain = NULL;
  729. STACK_OF(OPENSSL_STRING) *dane_tlsa_rrset = NULL;
  730. int dane_ee_no_name = 0;
  731. STACK_OF(X509_CRL) *crls = NULL;
  732. const SSL_METHOD *meth = TLS_client_method();
  733. const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
  734. char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL;
  735. char *proxystr = NULL, *proxyuser = NULL;
  736. char *proxypassarg = NULL, *proxypass = NULL;
  737. char *connectstr = NULL, *bindstr = NULL;
  738. char *cert_file = NULL, *key_file = NULL, *chain_file = NULL;
  739. char *chCApath = NULL, *chCAfile = NULL, *chCAstore = NULL, *host = NULL;
  740. char *thost = NULL, *tport = NULL;
  741. char *port = NULL;
  742. char *bindhost = NULL, *bindport = NULL;
  743. char *passarg = NULL, *pass = NULL;
  744. char *vfyCApath = NULL, *vfyCAfile = NULL, *vfyCAstore = NULL;
  745. char *ReqCAfile = NULL;
  746. char *sess_in = NULL, *crl_file = NULL, *p;
  747. const char *protohost = NULL;
  748. struct timeval timeout, *timeoutp;
  749. fd_set readfds, writefds;
  750. int noCApath = 0, noCAfile = 0, noCAstore = 0;
  751. int build_chain = 0, cbuf_len, cbuf_off, cert_format = FORMAT_UNDEF;
  752. int key_format = FORMAT_UNDEF, crlf = 0, full_log = 1, mbuf_len = 0;
  753. int prexit = 0;
  754. int nointeractive = 0;
  755. int sdebug = 0;
  756. int reconnect = 0, verify = SSL_VERIFY_NONE, vpmtouched = 0;
  757. int ret = 1, in_init = 1, i, nbio_test = 0, sock = -1, k, width, state = 0;
  758. int sbuf_len, sbuf_off, cmdletters = 1;
  759. int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM, protocol = 0;
  760. int starttls_proto = PROTO_OFF, crl_format = FORMAT_UNDEF, crl_download = 0;
  761. int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
  762. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
  763. int at_eof = 0;
  764. #endif
  765. int read_buf_len = 0;
  766. int fallback_scsv = 0;
  767. OPTION_CHOICE o;
  768. #ifndef OPENSSL_NO_DTLS
  769. int enable_timeouts = 0;
  770. long socket_mtu = 0;
  771. #endif
  772. #ifndef OPENSSL_NO_ENGINE
  773. ENGINE *ssl_client_engine = NULL;
  774. #endif
  775. ENGINE *e = NULL;
  776. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  777. struct timeval tv;
  778. #endif
  779. const char *servername = NULL;
  780. char *sname_alloc = NULL;
  781. int noservername = 0;
  782. const char *alpn_in = NULL;
  783. tlsextctx tlsextcbp = { NULL, 0 };
  784. const char *ssl_config = NULL;
  785. #define MAX_SI_TYPES 100
  786. unsigned short serverinfo_types[MAX_SI_TYPES];
  787. int serverinfo_count = 0, start = 0, len;
  788. #ifndef OPENSSL_NO_NEXTPROTONEG
  789. const char *next_proto_neg_in = NULL;
  790. #endif
  791. #ifndef OPENSSL_NO_SRP
  792. char *srppass = NULL;
  793. int srp_lateuser = 0;
  794. SRP_ARG srp_arg = { NULL, NULL, 0, 0, 0, 1024 };
  795. #endif
  796. #ifndef OPENSSL_NO_SRTP
  797. char *srtp_profiles = NULL;
  798. #endif
  799. #ifndef OPENSSL_NO_CT
  800. char *ctlog_file = NULL;
  801. int ct_validation = 0;
  802. #endif
  803. int min_version = 0, max_version = 0, prot_opt = 0, no_prot_opt = 0;
  804. int async = 0;
  805. unsigned int max_send_fragment = 0;
  806. unsigned int split_send_fragment = 0, max_pipelines = 0;
  807. enum { use_inet, use_unix, use_unknown } connect_type = use_unknown;
  808. int count4or6 = 0;
  809. uint8_t maxfraglen = 0;
  810. int c_nbio = 0, c_msg = 0, c_ign_eof = 0, c_brief = 0;
  811. int c_tlsextdebug = 0;
  812. #ifndef OPENSSL_NO_OCSP
  813. int c_status_req = 0;
  814. #endif
  815. BIO *bio_c_msg = NULL;
  816. const char *keylog_file = NULL, *early_data_file = NULL;
  817. #ifndef OPENSSL_NO_DTLS
  818. int isdtls = 0;
  819. #endif
  820. char *psksessf = NULL;
  821. int enable_pha = 0;
  822. #ifndef OPENSSL_NO_SCTP
  823. int sctp_label_bug = 0;
  824. #endif
  825. int ignore_unexpected_eof = 0;
  826. #ifndef OPENSSL_NO_KTLS
  827. int enable_ktls = 0;
  828. #endif
  829. int tfo = 0;
  830. BIO_ADDR *tfo_addr = NULL;
  831. FD_ZERO(&readfds);
  832. FD_ZERO(&writefds);
  833. /* Known false-positive of MemorySanitizer. */
  834. #if defined(__has_feature)
  835. # if __has_feature(memory_sanitizer)
  836. __msan_unpoison(&readfds, sizeof(readfds));
  837. __msan_unpoison(&writefds, sizeof(writefds));
  838. # endif
  839. #endif
  840. c_quiet = 0;
  841. c_debug = 0;
  842. c_showcerts = 0;
  843. c_nbio = 0;
  844. port = OPENSSL_strdup(PORT);
  845. vpm = X509_VERIFY_PARAM_new();
  846. cctx = SSL_CONF_CTX_new();
  847. if (port == NULL || vpm == NULL || cctx == NULL) {
  848. BIO_printf(bio_err, "%s: out of memory\n", opt_getprog());
  849. goto end;
  850. }
  851. cbuf = app_malloc(BUFSIZZ, "cbuf");
  852. sbuf = app_malloc(BUFSIZZ, "sbuf");
  853. mbuf = app_malloc(BUFSIZZ, "mbuf");
  854. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_CMDLINE);
  855. prog = opt_init(argc, argv, s_client_options);
  856. while ((o = opt_next()) != OPT_EOF) {
  857. /* Check for intermixing flags. */
  858. if (connect_type == use_unix && IS_INET_FLAG(o)) {
  859. BIO_printf(bio_err,
  860. "%s: Intermixed protocol flags (unix and internet domains)\n",
  861. prog);
  862. goto end;
  863. }
  864. if (connect_type == use_inet && IS_UNIX_FLAG(o)) {
  865. BIO_printf(bio_err,
  866. "%s: Intermixed protocol flags (internet and unix domains)\n",
  867. prog);
  868. goto end;
  869. }
  870. if (IS_PROT_FLAG(o) && ++prot_opt > 1) {
  871. BIO_printf(bio_err, "Cannot supply multiple protocol flags\n");
  872. goto end;
  873. }
  874. if (IS_NO_PROT_FLAG(o))
  875. no_prot_opt++;
  876. if (prot_opt == 1 && no_prot_opt) {
  877. BIO_printf(bio_err,
  878. "Cannot supply both a protocol flag and '-no_<prot>'\n");
  879. goto end;
  880. }
  881. switch (o) {
  882. case OPT_EOF:
  883. case OPT_ERR:
  884. opthelp:
  885. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  886. goto end;
  887. case OPT_HELP:
  888. opt_help(s_client_options);
  889. ret = 0;
  890. goto end;
  891. case OPT_4:
  892. connect_type = use_inet;
  893. socket_family = AF_INET;
  894. count4or6++;
  895. break;
  896. #ifdef AF_INET6
  897. case OPT_6:
  898. connect_type = use_inet;
  899. socket_family = AF_INET6;
  900. count4or6++;
  901. break;
  902. #endif
  903. case OPT_HOST:
  904. connect_type = use_inet;
  905. freeandcopy(&host, opt_arg());
  906. break;
  907. case OPT_PORT:
  908. connect_type = use_inet;
  909. freeandcopy(&port, opt_arg());
  910. break;
  911. case OPT_CONNECT:
  912. connect_type = use_inet;
  913. freeandcopy(&connectstr, opt_arg());
  914. break;
  915. case OPT_BIND:
  916. freeandcopy(&bindstr, opt_arg());
  917. break;
  918. case OPT_PROXY:
  919. proxystr = opt_arg();
  920. starttls_proto = PROTO_CONNECT;
  921. break;
  922. case OPT_PROXY_USER:
  923. proxyuser = opt_arg();
  924. break;
  925. case OPT_PROXY_PASS:
  926. proxypassarg = opt_arg();
  927. break;
  928. #ifdef AF_UNIX
  929. case OPT_UNIX:
  930. connect_type = use_unix;
  931. socket_family = AF_UNIX;
  932. freeandcopy(&host, opt_arg());
  933. break;
  934. #endif
  935. case OPT_XMPPHOST:
  936. /* fall through, since this is an alias */
  937. case OPT_PROTOHOST:
  938. protohost = opt_arg();
  939. break;
  940. case OPT_VERIFY:
  941. verify = SSL_VERIFY_PEER;
  942. verify_args.depth = atoi(opt_arg());
  943. if (!c_quiet)
  944. BIO_printf(bio_err, "verify depth is %d\n", verify_args.depth);
  945. break;
  946. case OPT_CERT:
  947. cert_file = opt_arg();
  948. break;
  949. case OPT_NAMEOPT:
  950. if (!set_nameopt(opt_arg()))
  951. goto end;
  952. break;
  953. case OPT_CRL:
  954. crl_file = opt_arg();
  955. break;
  956. case OPT_CRL_DOWNLOAD:
  957. crl_download = 1;
  958. break;
  959. case OPT_SESS_OUT:
  960. sess_out = opt_arg();
  961. break;
  962. case OPT_SESS_IN:
  963. sess_in = opt_arg();
  964. break;
  965. case OPT_CERTFORM:
  966. if (!opt_format(opt_arg(), OPT_FMT_ANY, &cert_format))
  967. goto opthelp;
  968. break;
  969. case OPT_CRLFORM:
  970. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))
  971. goto opthelp;
  972. break;
  973. case OPT_VERIFY_RET_ERROR:
  974. verify = SSL_VERIFY_PEER;
  975. verify_args.return_error = 1;
  976. break;
  977. case OPT_VERIFY_QUIET:
  978. verify_args.quiet = 1;
  979. break;
  980. case OPT_BRIEF:
  981. c_brief = verify_args.quiet = c_quiet = 1;
  982. break;
  983. case OPT_S_CASES:
  984. if (ssl_args == NULL)
  985. ssl_args = sk_OPENSSL_STRING_new_null();
  986. if (ssl_args == NULL
  987. || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())
  988. || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {
  989. BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
  990. goto end;
  991. }
  992. break;
  993. case OPT_V_CASES:
  994. if (!opt_verify(o, vpm))
  995. goto end;
  996. vpmtouched++;
  997. break;
  998. case OPT_X_CASES:
  999. if (!args_excert(o, &exc))
  1000. goto end;
  1001. break;
  1002. case OPT_IGNORE_UNEXPECTED_EOF:
  1003. ignore_unexpected_eof = 1;
  1004. break;
  1005. case OPT_PREXIT:
  1006. prexit = 1;
  1007. break;
  1008. case OPT_NO_INTERACTIVE:
  1009. nointeractive = 1;
  1010. break;
  1011. case OPT_CRLF:
  1012. crlf = 1;
  1013. break;
  1014. case OPT_QUIET:
  1015. c_quiet = c_ign_eof = 1;
  1016. break;
  1017. case OPT_NBIO:
  1018. c_nbio = 1;
  1019. break;
  1020. case OPT_NOCMDS:
  1021. cmdletters = 0;
  1022. break;
  1023. case OPT_ENGINE:
  1024. e = setup_engine(opt_arg(), 1);
  1025. break;
  1026. case OPT_SSL_CLIENT_ENGINE:
  1027. #ifndef OPENSSL_NO_ENGINE
  1028. ssl_client_engine = setup_engine(opt_arg(), 0);
  1029. if (ssl_client_engine == NULL) {
  1030. BIO_printf(bio_err, "Error getting client auth engine\n");
  1031. goto opthelp;
  1032. }
  1033. #endif
  1034. break;
  1035. case OPT_R_CASES:
  1036. if (!opt_rand(o))
  1037. goto end;
  1038. break;
  1039. case OPT_PROV_CASES:
  1040. if (!opt_provider(o))
  1041. goto end;
  1042. break;
  1043. case OPT_IGN_EOF:
  1044. c_ign_eof = 1;
  1045. break;
  1046. case OPT_NO_IGN_EOF:
  1047. c_ign_eof = 0;
  1048. break;
  1049. case OPT_DEBUG:
  1050. c_debug = 1;
  1051. break;
  1052. case OPT_TLSEXTDEBUG:
  1053. c_tlsextdebug = 1;
  1054. break;
  1055. case OPT_STATUS:
  1056. #ifndef OPENSSL_NO_OCSP
  1057. c_status_req = 1;
  1058. #endif
  1059. break;
  1060. case OPT_WDEBUG:
  1061. #ifdef WATT32
  1062. dbug_init();
  1063. #endif
  1064. break;
  1065. case OPT_MSG:
  1066. c_msg = 1;
  1067. break;
  1068. case OPT_MSGFILE:
  1069. bio_c_msg = BIO_new_file(opt_arg(), "w");
  1070. if (bio_c_msg == NULL) {
  1071. BIO_printf(bio_err, "Error writing file %s\n", opt_arg());
  1072. goto end;
  1073. }
  1074. break;
  1075. case OPT_TRACE:
  1076. #ifndef OPENSSL_NO_SSL_TRACE
  1077. c_msg = 2;
  1078. #endif
  1079. break;
  1080. case OPT_SECURITY_DEBUG:
  1081. sdebug = 1;
  1082. break;
  1083. case OPT_SECURITY_DEBUG_VERBOSE:
  1084. sdebug = 2;
  1085. break;
  1086. case OPT_SHOWCERTS:
  1087. c_showcerts = 1;
  1088. break;
  1089. case OPT_NBIO_TEST:
  1090. nbio_test = 1;
  1091. break;
  1092. case OPT_STATE:
  1093. state = 1;
  1094. break;
  1095. case OPT_PSK_IDENTITY:
  1096. psk_identity = opt_arg();
  1097. break;
  1098. case OPT_PSK:
  1099. for (p = psk_key = opt_arg(); *p; p++) {
  1100. if (isxdigit(_UC(*p)))
  1101. continue;
  1102. BIO_printf(bio_err, "Not a hex number '%s'\n", psk_key);
  1103. goto end;
  1104. }
  1105. break;
  1106. case OPT_PSK_SESS:
  1107. psksessf = opt_arg();
  1108. break;
  1109. #ifndef OPENSSL_NO_SRP
  1110. case OPT_SRPUSER:
  1111. srp_arg.srplogin = opt_arg();
  1112. if (min_version < TLS1_VERSION)
  1113. min_version = TLS1_VERSION;
  1114. break;
  1115. case OPT_SRPPASS:
  1116. srppass = opt_arg();
  1117. if (min_version < TLS1_VERSION)
  1118. min_version = TLS1_VERSION;
  1119. break;
  1120. case OPT_SRP_STRENGTH:
  1121. srp_arg.strength = atoi(opt_arg());
  1122. BIO_printf(bio_err, "SRP minimal length for N is %d\n",
  1123. srp_arg.strength);
  1124. if (min_version < TLS1_VERSION)
  1125. min_version = TLS1_VERSION;
  1126. break;
  1127. case OPT_SRP_LATEUSER:
  1128. srp_lateuser = 1;
  1129. if (min_version < TLS1_VERSION)
  1130. min_version = TLS1_VERSION;
  1131. break;
  1132. case OPT_SRP_MOREGROUPS:
  1133. srp_arg.amp = 1;
  1134. if (min_version < TLS1_VERSION)
  1135. min_version = TLS1_VERSION;
  1136. break;
  1137. #endif
  1138. case OPT_SSL_CONFIG:
  1139. ssl_config = opt_arg();
  1140. break;
  1141. case OPT_SSL3:
  1142. min_version = SSL3_VERSION;
  1143. max_version = SSL3_VERSION;
  1144. socket_type = SOCK_STREAM;
  1145. #ifndef OPENSSL_NO_DTLS
  1146. isdtls = 0;
  1147. #endif
  1148. break;
  1149. case OPT_TLS1_3:
  1150. min_version = TLS1_3_VERSION;
  1151. max_version = TLS1_3_VERSION;
  1152. socket_type = SOCK_STREAM;
  1153. #ifndef OPENSSL_NO_DTLS
  1154. isdtls = 0;
  1155. #endif
  1156. break;
  1157. case OPT_TLS1_2:
  1158. min_version = TLS1_2_VERSION;
  1159. max_version = TLS1_2_VERSION;
  1160. socket_type = SOCK_STREAM;
  1161. #ifndef OPENSSL_NO_DTLS
  1162. isdtls = 0;
  1163. #endif
  1164. break;
  1165. case OPT_TLS1_1:
  1166. min_version = TLS1_1_VERSION;
  1167. max_version = TLS1_1_VERSION;
  1168. socket_type = SOCK_STREAM;
  1169. #ifndef OPENSSL_NO_DTLS
  1170. isdtls = 0;
  1171. #endif
  1172. break;
  1173. case OPT_TLS1:
  1174. min_version = TLS1_VERSION;
  1175. max_version = TLS1_VERSION;
  1176. socket_type = SOCK_STREAM;
  1177. #ifndef OPENSSL_NO_DTLS
  1178. isdtls = 0;
  1179. #endif
  1180. break;
  1181. case OPT_DTLS:
  1182. #ifndef OPENSSL_NO_DTLS
  1183. meth = DTLS_client_method();
  1184. socket_type = SOCK_DGRAM;
  1185. isdtls = 1;
  1186. #endif
  1187. break;
  1188. case OPT_DTLS1:
  1189. #ifndef OPENSSL_NO_DTLS1
  1190. meth = DTLS_client_method();
  1191. min_version = DTLS1_VERSION;
  1192. max_version = DTLS1_VERSION;
  1193. socket_type = SOCK_DGRAM;
  1194. isdtls = 1;
  1195. #endif
  1196. break;
  1197. case OPT_DTLS1_2:
  1198. #ifndef OPENSSL_NO_DTLS1_2
  1199. meth = DTLS_client_method();
  1200. min_version = DTLS1_2_VERSION;
  1201. max_version = DTLS1_2_VERSION;
  1202. socket_type = SOCK_DGRAM;
  1203. isdtls = 1;
  1204. #endif
  1205. break;
  1206. case OPT_SCTP:
  1207. #ifndef OPENSSL_NO_SCTP
  1208. protocol = IPPROTO_SCTP;
  1209. #endif
  1210. break;
  1211. case OPT_SCTP_LABEL_BUG:
  1212. #ifndef OPENSSL_NO_SCTP
  1213. sctp_label_bug = 1;
  1214. #endif
  1215. break;
  1216. case OPT_TIMEOUT:
  1217. #ifndef OPENSSL_NO_DTLS
  1218. enable_timeouts = 1;
  1219. #endif
  1220. break;
  1221. case OPT_MTU:
  1222. #ifndef OPENSSL_NO_DTLS
  1223. socket_mtu = atol(opt_arg());
  1224. #endif
  1225. break;
  1226. case OPT_FALLBACKSCSV:
  1227. fallback_scsv = 1;
  1228. break;
  1229. case OPT_KEYFORM:
  1230. if (!opt_format(opt_arg(), OPT_FMT_ANY, &key_format))
  1231. goto opthelp;
  1232. break;
  1233. case OPT_PASS:
  1234. passarg = opt_arg();
  1235. break;
  1236. case OPT_CERT_CHAIN:
  1237. chain_file = opt_arg();
  1238. break;
  1239. case OPT_KEY:
  1240. key_file = opt_arg();
  1241. break;
  1242. case OPT_RECONNECT:
  1243. reconnect = 5;
  1244. break;
  1245. case OPT_CAPATH:
  1246. CApath = opt_arg();
  1247. break;
  1248. case OPT_NOCAPATH:
  1249. noCApath = 1;
  1250. break;
  1251. case OPT_CHAINCAPATH:
  1252. chCApath = opt_arg();
  1253. break;
  1254. case OPT_VERIFYCAPATH:
  1255. vfyCApath = opt_arg();
  1256. break;
  1257. case OPT_BUILD_CHAIN:
  1258. build_chain = 1;
  1259. break;
  1260. case OPT_REQCAFILE:
  1261. ReqCAfile = opt_arg();
  1262. break;
  1263. case OPT_CAFILE:
  1264. CAfile = opt_arg();
  1265. break;
  1266. case OPT_NOCAFILE:
  1267. noCAfile = 1;
  1268. break;
  1269. #ifndef OPENSSL_NO_CT
  1270. case OPT_NOCT:
  1271. ct_validation = 0;
  1272. break;
  1273. case OPT_CT:
  1274. ct_validation = 1;
  1275. break;
  1276. case OPT_CTLOG_FILE:
  1277. ctlog_file = opt_arg();
  1278. break;
  1279. #endif
  1280. case OPT_CHAINCAFILE:
  1281. chCAfile = opt_arg();
  1282. break;
  1283. case OPT_VERIFYCAFILE:
  1284. vfyCAfile = opt_arg();
  1285. break;
  1286. case OPT_CASTORE:
  1287. CAstore = opt_arg();
  1288. break;
  1289. case OPT_NOCASTORE:
  1290. noCAstore = 1;
  1291. break;
  1292. case OPT_CHAINCASTORE:
  1293. chCAstore = opt_arg();
  1294. break;
  1295. case OPT_VERIFYCASTORE:
  1296. vfyCAstore = opt_arg();
  1297. break;
  1298. case OPT_DANE_TLSA_DOMAIN:
  1299. dane_tlsa_domain = opt_arg();
  1300. break;
  1301. case OPT_DANE_TLSA_RRDATA:
  1302. if (dane_tlsa_rrset == NULL)
  1303. dane_tlsa_rrset = sk_OPENSSL_STRING_new_null();
  1304. if (dane_tlsa_rrset == NULL ||
  1305. !sk_OPENSSL_STRING_push(dane_tlsa_rrset, opt_arg())) {
  1306. BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
  1307. goto end;
  1308. }
  1309. break;
  1310. case OPT_DANE_EE_NO_NAME:
  1311. dane_ee_no_name = 1;
  1312. break;
  1313. case OPT_NEXTPROTONEG:
  1314. #ifndef OPENSSL_NO_NEXTPROTONEG
  1315. next_proto_neg_in = opt_arg();
  1316. #endif
  1317. break;
  1318. case OPT_ALPN:
  1319. alpn_in = opt_arg();
  1320. break;
  1321. case OPT_SERVERINFO:
  1322. p = opt_arg();
  1323. len = strlen(p);
  1324. for (start = 0, i = 0; i <= len; ++i) {
  1325. if (i == len || p[i] == ',') {
  1326. serverinfo_types[serverinfo_count] = atoi(p + start);
  1327. if (++serverinfo_count == MAX_SI_TYPES)
  1328. break;
  1329. start = i + 1;
  1330. }
  1331. }
  1332. break;
  1333. case OPT_STARTTLS:
  1334. if (!opt_pair(opt_arg(), services, &starttls_proto))
  1335. goto end;
  1336. break;
  1337. case OPT_TFO:
  1338. tfo = 1;
  1339. break;
  1340. case OPT_SERVERNAME:
  1341. servername = opt_arg();
  1342. break;
  1343. case OPT_NOSERVERNAME:
  1344. noservername = 1;
  1345. break;
  1346. case OPT_USE_SRTP:
  1347. #ifndef OPENSSL_NO_SRTP
  1348. srtp_profiles = opt_arg();
  1349. #endif
  1350. break;
  1351. case OPT_KEYMATEXPORT:
  1352. keymatexportlabel = opt_arg();
  1353. break;
  1354. case OPT_KEYMATEXPORTLEN:
  1355. keymatexportlen = atoi(opt_arg());
  1356. break;
  1357. case OPT_ASYNC:
  1358. async = 1;
  1359. break;
  1360. case OPT_MAXFRAGLEN:
  1361. len = atoi(opt_arg());
  1362. switch (len) {
  1363. case 512:
  1364. maxfraglen = TLSEXT_max_fragment_length_512;
  1365. break;
  1366. case 1024:
  1367. maxfraglen = TLSEXT_max_fragment_length_1024;
  1368. break;
  1369. case 2048:
  1370. maxfraglen = TLSEXT_max_fragment_length_2048;
  1371. break;
  1372. case 4096:
  1373. maxfraglen = TLSEXT_max_fragment_length_4096;
  1374. break;
  1375. default:
  1376. BIO_printf(bio_err,
  1377. "%s: Max Fragment Len %u is out of permitted values",
  1378. prog, len);
  1379. goto opthelp;
  1380. }
  1381. break;
  1382. case OPT_MAX_SEND_FRAG:
  1383. max_send_fragment = atoi(opt_arg());
  1384. break;
  1385. case OPT_SPLIT_SEND_FRAG:
  1386. split_send_fragment = atoi(opt_arg());
  1387. break;
  1388. case OPT_MAX_PIPELINES:
  1389. max_pipelines = atoi(opt_arg());
  1390. break;
  1391. case OPT_READ_BUF:
  1392. read_buf_len = atoi(opt_arg());
  1393. break;
  1394. case OPT_KEYLOG_FILE:
  1395. keylog_file = opt_arg();
  1396. break;
  1397. case OPT_EARLY_DATA:
  1398. early_data_file = opt_arg();
  1399. break;
  1400. case OPT_ENABLE_PHA:
  1401. enable_pha = 1;
  1402. break;
  1403. case OPT_KTLS:
  1404. #ifndef OPENSSL_NO_KTLS
  1405. enable_ktls = 1;
  1406. #endif
  1407. break;
  1408. }
  1409. }
  1410. /* Optional argument is connect string if -connect not used. */
  1411. if (opt_num_rest() == 1) {
  1412. /* Don't allow -connect and a separate argument. */
  1413. if (connectstr != NULL) {
  1414. BIO_printf(bio_err,
  1415. "%s: cannot provide both -connect option and target parameter\n",
  1416. prog);
  1417. goto opthelp;
  1418. }
  1419. connect_type = use_inet;
  1420. freeandcopy(&connectstr, *opt_rest());
  1421. } else if (!opt_check_rest_arg(NULL)) {
  1422. goto opthelp;
  1423. }
  1424. if (!app_RAND_load())
  1425. goto end;
  1426. if (count4or6 >= 2) {
  1427. BIO_printf(bio_err, "%s: Can't use both -4 and -6\n", prog);
  1428. goto opthelp;
  1429. }
  1430. if (noservername) {
  1431. if (servername != NULL) {
  1432. BIO_printf(bio_err,
  1433. "%s: Can't use -servername and -noservername together\n",
  1434. prog);
  1435. goto opthelp;
  1436. }
  1437. if (dane_tlsa_domain != NULL) {
  1438. BIO_printf(bio_err,
  1439. "%s: Can't use -dane_tlsa_domain and -noservername together\n",
  1440. prog);
  1441. goto opthelp;
  1442. }
  1443. }
  1444. #ifndef OPENSSL_NO_NEXTPROTONEG
  1445. if (min_version == TLS1_3_VERSION && next_proto_neg_in != NULL) {
  1446. BIO_printf(bio_err, "Cannot supply -nextprotoneg with TLSv1.3\n");
  1447. goto opthelp;
  1448. }
  1449. #endif
  1450. if (connectstr != NULL) {
  1451. int res;
  1452. char *tmp_host = host, *tmp_port = port;
  1453. res = BIO_parse_hostserv(connectstr, &host, &port, BIO_PARSE_PRIO_HOST);
  1454. if (tmp_host != host)
  1455. OPENSSL_free(tmp_host);
  1456. if (tmp_port != port)
  1457. OPENSSL_free(tmp_port);
  1458. if (!res) {
  1459. BIO_printf(bio_err,
  1460. "%s: -connect argument or target parameter malformed or ambiguous\n",
  1461. prog);
  1462. goto end;
  1463. }
  1464. }
  1465. if (proxystr != NULL) {
  1466. int res;
  1467. char *tmp_host = host, *tmp_port = port;
  1468. if (host == NULL || port == NULL) {
  1469. BIO_printf(bio_err, "%s: -proxy requires use of -connect or target parameter\n", prog);
  1470. goto opthelp;
  1471. }
  1472. if (servername == NULL && !noservername) {
  1473. servername = sname_alloc = OPENSSL_strdup(host);
  1474. if (sname_alloc == NULL) {
  1475. BIO_printf(bio_err, "%s: out of memory\n", prog);
  1476. goto end;
  1477. }
  1478. }
  1479. /* Retain the original target host:port for use in the HTTP proxy connect string */
  1480. thost = OPENSSL_strdup(host);
  1481. tport = OPENSSL_strdup(port);
  1482. if (thost == NULL || tport == NULL) {
  1483. BIO_printf(bio_err, "%s: out of memory\n", prog);
  1484. goto end;
  1485. }
  1486. res = BIO_parse_hostserv(proxystr, &host, &port, BIO_PARSE_PRIO_HOST);
  1487. if (tmp_host != host)
  1488. OPENSSL_free(tmp_host);
  1489. if (tmp_port != port)
  1490. OPENSSL_free(tmp_port);
  1491. if (!res) {
  1492. BIO_printf(bio_err,
  1493. "%s: -proxy argument malformed or ambiguous\n", prog);
  1494. goto end;
  1495. }
  1496. }
  1497. if (bindstr != NULL) {
  1498. int res;
  1499. res = BIO_parse_hostserv(bindstr, &bindhost, &bindport,
  1500. BIO_PARSE_PRIO_HOST);
  1501. if (!res) {
  1502. BIO_printf(bio_err,
  1503. "%s: -bind argument parameter malformed or ambiguous\n",
  1504. prog);
  1505. goto end;
  1506. }
  1507. }
  1508. #ifdef AF_UNIX
  1509. if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) {
  1510. BIO_printf(bio_err,
  1511. "Can't use unix sockets and datagrams together\n");
  1512. goto end;
  1513. }
  1514. #endif
  1515. #ifndef OPENSSL_NO_SCTP
  1516. if (protocol == IPPROTO_SCTP) {
  1517. if (socket_type != SOCK_DGRAM) {
  1518. BIO_printf(bio_err, "Can't use -sctp without DTLS\n");
  1519. goto end;
  1520. }
  1521. /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
  1522. socket_type = SOCK_STREAM;
  1523. }
  1524. #endif
  1525. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  1526. next_proto.status = -1;
  1527. if (next_proto_neg_in) {
  1528. next_proto.data =
  1529. next_protos_parse(&next_proto.len, next_proto_neg_in);
  1530. if (next_proto.data == NULL) {
  1531. BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n");
  1532. goto end;
  1533. }
  1534. } else
  1535. next_proto.data = NULL;
  1536. #endif
  1537. if (!app_passwd(passarg, NULL, &pass, NULL)) {
  1538. BIO_printf(bio_err, "Error getting private key password\n");
  1539. goto end;
  1540. }
  1541. if (!app_passwd(proxypassarg, NULL, &proxypass, NULL)) {
  1542. BIO_printf(bio_err, "Error getting proxy password\n");
  1543. goto end;
  1544. }
  1545. if (proxypass != NULL && proxyuser == NULL) {
  1546. BIO_printf(bio_err, "Error: Must specify proxy_user with proxy_pass\n");
  1547. goto end;
  1548. }
  1549. if (key_file == NULL)
  1550. key_file = cert_file;
  1551. if (key_file != NULL) {
  1552. key = load_key(key_file, key_format, 0, pass, e,
  1553. "client certificate private key");
  1554. if (key == NULL)
  1555. goto end;
  1556. }
  1557. if (cert_file != NULL) {
  1558. cert = load_cert_pass(cert_file, cert_format, 1, pass,
  1559. "client certificate");
  1560. if (cert == NULL)
  1561. goto end;
  1562. }
  1563. if (chain_file != NULL) {
  1564. if (!load_certs(chain_file, 0, &chain, pass, "client certificate chain"))
  1565. goto end;
  1566. }
  1567. if (crl_file != NULL) {
  1568. X509_CRL *crl;
  1569. crl = load_crl(crl_file, crl_format, 0, "CRL");
  1570. if (crl == NULL)
  1571. goto end;
  1572. crls = sk_X509_CRL_new_null();
  1573. if (crls == NULL || !sk_X509_CRL_push(crls, crl)) {
  1574. BIO_puts(bio_err, "Error adding CRL\n");
  1575. ERR_print_errors(bio_err);
  1576. X509_CRL_free(crl);
  1577. goto end;
  1578. }
  1579. }
  1580. if (!load_excert(&exc))
  1581. goto end;
  1582. if (bio_c_out == NULL) {
  1583. if (c_quiet && !c_debug) {
  1584. bio_c_out = BIO_new(BIO_s_null());
  1585. if (c_msg && bio_c_msg == NULL) {
  1586. bio_c_msg = dup_bio_out(FORMAT_TEXT);
  1587. if (bio_c_msg == NULL) {
  1588. BIO_printf(bio_err, "Out of memory\n");
  1589. goto end;
  1590. }
  1591. }
  1592. } else {
  1593. bio_c_out = dup_bio_out(FORMAT_TEXT);
  1594. }
  1595. if (bio_c_out == NULL) {
  1596. BIO_printf(bio_err, "Unable to create BIO\n");
  1597. goto end;
  1598. }
  1599. }
  1600. #ifndef OPENSSL_NO_SRP
  1601. if (!app_passwd(srppass, NULL, &srp_arg.srppassin, NULL)) {
  1602. BIO_printf(bio_err, "Error getting password\n");
  1603. goto end;
  1604. }
  1605. #endif
  1606. ctx = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth);
  1607. if (ctx == NULL) {
  1608. ERR_print_errors(bio_err);
  1609. goto end;
  1610. }
  1611. SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY);
  1612. if (sdebug)
  1613. ssl_ctx_security_debug(ctx, sdebug);
  1614. if (!config_ctx(cctx, ssl_args, ctx))
  1615. goto end;
  1616. if (ssl_config != NULL) {
  1617. if (SSL_CTX_config(ctx, ssl_config) == 0) {
  1618. BIO_printf(bio_err, "Error using configuration \"%s\"\n",
  1619. ssl_config);
  1620. ERR_print_errors(bio_err);
  1621. goto end;
  1622. }
  1623. }
  1624. #ifndef OPENSSL_NO_SCTP
  1625. if (protocol == IPPROTO_SCTP && sctp_label_bug == 1)
  1626. SSL_CTX_set_mode(ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG);
  1627. #endif
  1628. if (min_version != 0
  1629. && SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
  1630. goto end;
  1631. if (max_version != 0
  1632. && SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
  1633. goto end;
  1634. if (ignore_unexpected_eof)
  1635. SSL_CTX_set_options(ctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
  1636. #ifndef OPENSSL_NO_KTLS
  1637. if (enable_ktls)
  1638. SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS);
  1639. #endif
  1640. if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {
  1641. BIO_printf(bio_err, "Error setting verify params\n");
  1642. ERR_print_errors(bio_err);
  1643. goto end;
  1644. }
  1645. if (async) {
  1646. SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC);
  1647. }
  1648. if (max_send_fragment > 0
  1649. && !SSL_CTX_set_max_send_fragment(ctx, max_send_fragment)) {
  1650. BIO_printf(bio_err, "%s: Max send fragment size %u is out of permitted range\n",
  1651. prog, max_send_fragment);
  1652. goto end;
  1653. }
  1654. if (split_send_fragment > 0
  1655. && !SSL_CTX_set_split_send_fragment(ctx, split_send_fragment)) {
  1656. BIO_printf(bio_err, "%s: Split send fragment size %u is out of permitted range\n",
  1657. prog, split_send_fragment);
  1658. goto end;
  1659. }
  1660. if (max_pipelines > 0
  1661. && !SSL_CTX_set_max_pipelines(ctx, max_pipelines)) {
  1662. BIO_printf(bio_err, "%s: Max pipelines %u is out of permitted range\n",
  1663. prog, max_pipelines);
  1664. goto end;
  1665. }
  1666. if (read_buf_len > 0) {
  1667. SSL_CTX_set_default_read_buffer_len(ctx, read_buf_len);
  1668. }
  1669. if (maxfraglen > 0
  1670. && !SSL_CTX_set_tlsext_max_fragment_length(ctx, maxfraglen)) {
  1671. BIO_printf(bio_err,
  1672. "%s: Max Fragment Length code %u is out of permitted values"
  1673. "\n", prog, maxfraglen);
  1674. goto end;
  1675. }
  1676. if (!ssl_load_stores(ctx,
  1677. vfyCApath, vfyCAfile, vfyCAstore,
  1678. chCApath, chCAfile, chCAstore,
  1679. crls, crl_download)) {
  1680. BIO_printf(bio_err, "Error loading store locations\n");
  1681. ERR_print_errors(bio_err);
  1682. goto end;
  1683. }
  1684. if (ReqCAfile != NULL) {
  1685. STACK_OF(X509_NAME) *nm = sk_X509_NAME_new_null();
  1686. if (nm == NULL || !SSL_add_file_cert_subjects_to_stack(nm, ReqCAfile)) {
  1687. sk_X509_NAME_pop_free(nm, X509_NAME_free);
  1688. BIO_printf(bio_err, "Error loading CA names\n");
  1689. ERR_print_errors(bio_err);
  1690. goto end;
  1691. }
  1692. SSL_CTX_set0_CA_list(ctx, nm);
  1693. }
  1694. #ifndef OPENSSL_NO_ENGINE
  1695. if (ssl_client_engine) {
  1696. if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) {
  1697. BIO_puts(bio_err, "Error setting client auth engine\n");
  1698. ERR_print_errors(bio_err);
  1699. release_engine(ssl_client_engine);
  1700. goto end;
  1701. }
  1702. release_engine(ssl_client_engine);
  1703. }
  1704. #endif
  1705. #ifndef OPENSSL_NO_PSK
  1706. if (psk_key != NULL) {
  1707. if (c_debug)
  1708. BIO_printf(bio_c_out, "PSK key given, setting client callback\n");
  1709. SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
  1710. }
  1711. #endif
  1712. if (psksessf != NULL) {
  1713. BIO *stmp = BIO_new_file(psksessf, "r");
  1714. if (stmp == NULL) {
  1715. BIO_printf(bio_err, "Can't open PSK session file %s\n", psksessf);
  1716. ERR_print_errors(bio_err);
  1717. goto end;
  1718. }
  1719. psksess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
  1720. BIO_free(stmp);
  1721. if (psksess == NULL) {
  1722. BIO_printf(bio_err, "Can't read PSK session file %s\n", psksessf);
  1723. ERR_print_errors(bio_err);
  1724. goto end;
  1725. }
  1726. }
  1727. if (psk_key != NULL || psksess != NULL)
  1728. SSL_CTX_set_psk_use_session_callback(ctx, psk_use_session_cb);
  1729. #ifndef OPENSSL_NO_SRTP
  1730. if (srtp_profiles != NULL) {
  1731. /* Returns 0 on success! */
  1732. if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {
  1733. BIO_printf(bio_err, "Error setting SRTP profile\n");
  1734. ERR_print_errors(bio_err);
  1735. goto end;
  1736. }
  1737. }
  1738. #endif
  1739. if (exc != NULL)
  1740. ssl_ctx_set_excert(ctx, exc);
  1741. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  1742. if (next_proto.data != NULL)
  1743. SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);
  1744. #endif
  1745. if (alpn_in) {
  1746. size_t alpn_len;
  1747. unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in);
  1748. if (alpn == NULL) {
  1749. BIO_printf(bio_err, "Error parsing -alpn argument\n");
  1750. goto end;
  1751. }
  1752. /* Returns 0 on success! */
  1753. if (SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len) != 0) {
  1754. BIO_printf(bio_err, "Error setting ALPN\n");
  1755. goto end;
  1756. }
  1757. OPENSSL_free(alpn);
  1758. }
  1759. for (i = 0; i < serverinfo_count; i++) {
  1760. if (!SSL_CTX_add_client_custom_ext(ctx,
  1761. serverinfo_types[i],
  1762. NULL, NULL, NULL,
  1763. serverinfo_cli_parse_cb, NULL)) {
  1764. BIO_printf(bio_err,
  1765. "Warning: Unable to add custom extension %u, skipping\n",
  1766. serverinfo_types[i]);
  1767. }
  1768. }
  1769. if (state)
  1770. SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
  1771. #ifndef OPENSSL_NO_CT
  1772. /* Enable SCT processing, without early connection termination */
  1773. if (ct_validation &&
  1774. !SSL_CTX_enable_ct(ctx, SSL_CT_VALIDATION_PERMISSIVE)) {
  1775. ERR_print_errors(bio_err);
  1776. goto end;
  1777. }
  1778. if (!ctx_set_ctlog_list_file(ctx, ctlog_file)) {
  1779. if (ct_validation) {
  1780. ERR_print_errors(bio_err);
  1781. goto end;
  1782. }
  1783. /*
  1784. * If CT validation is not enabled, the log list isn't needed so don't
  1785. * show errors or abort. We try to load it regardless because then we
  1786. * can show the names of the logs any SCTs came from (SCTs may be seen
  1787. * even with validation disabled).
  1788. */
  1789. ERR_clear_error();
  1790. }
  1791. #endif
  1792. SSL_CTX_set_verify(ctx, verify, verify_callback);
  1793. if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath,
  1794. CAstore, noCAstore)) {
  1795. ERR_print_errors(bio_err);
  1796. goto end;
  1797. }
  1798. ssl_ctx_add_crls(ctx, crls, crl_download);
  1799. if (!set_cert_key_stuff(ctx, cert, key, chain, build_chain))
  1800. goto end;
  1801. if (!noservername) {
  1802. tlsextcbp.biodebug = bio_err;
  1803. SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
  1804. SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
  1805. }
  1806. #ifndef OPENSSL_NO_SRP
  1807. if (srp_arg.srplogin != NULL
  1808. && !set_up_srp_arg(ctx, &srp_arg, srp_lateuser, c_msg, c_debug))
  1809. goto end;
  1810. # endif
  1811. if (dane_tlsa_domain != NULL) {
  1812. if (SSL_CTX_dane_enable(ctx) <= 0) {
  1813. BIO_printf(bio_err,
  1814. "%s: Error enabling DANE TLSA authentication.\n",
  1815. prog);
  1816. ERR_print_errors(bio_err);
  1817. goto end;
  1818. }
  1819. }
  1820. /*
  1821. * In TLSv1.3 NewSessionTicket messages arrive after the handshake and can
  1822. * come at any time. Therefore we use a callback to write out the session
  1823. * when we know about it. This approach works for < TLSv1.3 as well.
  1824. */
  1825. SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT
  1826. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1827. SSL_CTX_sess_set_new_cb(ctx, new_session_cb);
  1828. if (set_keylog_file(ctx, keylog_file))
  1829. goto end;
  1830. con = SSL_new(ctx);
  1831. if (con == NULL)
  1832. goto end;
  1833. if (enable_pha)
  1834. SSL_set_post_handshake_auth(con, 1);
  1835. if (sess_in != NULL) {
  1836. SSL_SESSION *sess;
  1837. BIO *stmp = BIO_new_file(sess_in, "r");
  1838. if (stmp == NULL) {
  1839. BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
  1840. ERR_print_errors(bio_err);
  1841. goto end;
  1842. }
  1843. sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
  1844. BIO_free(stmp);
  1845. if (sess == NULL) {
  1846. BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
  1847. ERR_print_errors(bio_err);
  1848. goto end;
  1849. }
  1850. if (!SSL_set_session(con, sess)) {
  1851. BIO_printf(bio_err, "Can't set session\n");
  1852. ERR_print_errors(bio_err);
  1853. goto end;
  1854. }
  1855. SSL_SESSION_free(sess);
  1856. }
  1857. if (fallback_scsv)
  1858. SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV);
  1859. if (!noservername && (servername != NULL || dane_tlsa_domain == NULL)) {
  1860. if (servername == NULL) {
  1861. if (host == NULL || is_dNS_name(host))
  1862. servername = (host == NULL) ? "localhost" : host;
  1863. }
  1864. if (servername != NULL && !SSL_set_tlsext_host_name(con, servername)) {
  1865. BIO_printf(bio_err, "Unable to set TLS servername extension.\n");
  1866. ERR_print_errors(bio_err);
  1867. goto end;
  1868. }
  1869. }
  1870. if (dane_tlsa_domain != NULL) {
  1871. if (SSL_dane_enable(con, dane_tlsa_domain) <= 0) {
  1872. BIO_printf(bio_err, "%s: Error enabling DANE TLSA "
  1873. "authentication.\n", prog);
  1874. ERR_print_errors(bio_err);
  1875. goto end;
  1876. }
  1877. if (dane_tlsa_rrset == NULL) {
  1878. BIO_printf(bio_err, "%s: DANE TLSA authentication requires at "
  1879. "least one -dane_tlsa_rrdata option.\n", prog);
  1880. goto end;
  1881. }
  1882. if (tlsa_import_rrset(con, dane_tlsa_rrset) <= 0) {
  1883. BIO_printf(bio_err, "%s: Failed to import any TLSA "
  1884. "records.\n", prog);
  1885. goto end;
  1886. }
  1887. if (dane_ee_no_name)
  1888. SSL_dane_set_flags(con, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
  1889. } else if (dane_tlsa_rrset != NULL) {
  1890. BIO_printf(bio_err, "%s: DANE TLSA authentication requires the "
  1891. "-dane_tlsa_domain option.\n", prog);
  1892. goto end;
  1893. }
  1894. #ifndef OPENSSL_NO_DTLS
  1895. if (isdtls && tfo) {
  1896. BIO_printf(bio_err, "%s: DTLS does not support the -tfo option\n", prog);
  1897. goto end;
  1898. }
  1899. #endif
  1900. if (tfo)
  1901. BIO_printf(bio_c_out, "Connecting via TFO\n");
  1902. re_start:
  1903. if (init_client(&sock, host, port, bindhost, bindport, socket_family,
  1904. socket_type, protocol, tfo, &tfo_addr) == 0) {
  1905. BIO_printf(bio_err, "connect:errno=%d\n", get_last_socket_error());
  1906. BIO_closesocket(sock);
  1907. goto end;
  1908. }
  1909. BIO_printf(bio_c_out, "CONNECTED(%08X)\n", sock);
  1910. if (c_nbio) {
  1911. if (!BIO_socket_nbio(sock, 1)) {
  1912. ERR_print_errors(bio_err);
  1913. goto end;
  1914. }
  1915. BIO_printf(bio_c_out, "Turned on non blocking io\n");
  1916. }
  1917. #ifndef OPENSSL_NO_DTLS
  1918. if (isdtls) {
  1919. union BIO_sock_info_u peer_info;
  1920. #ifndef OPENSSL_NO_SCTP
  1921. if (protocol == IPPROTO_SCTP)
  1922. sbio = BIO_new_dgram_sctp(sock, BIO_NOCLOSE);
  1923. else
  1924. #endif
  1925. sbio = BIO_new_dgram(sock, BIO_NOCLOSE);
  1926. if (sbio == NULL || (peer_info.addr = BIO_ADDR_new()) == NULL) {
  1927. BIO_printf(bio_err, "memory allocation failure\n");
  1928. BIO_free(sbio);
  1929. BIO_closesocket(sock);
  1930. goto end;
  1931. }
  1932. if (!BIO_sock_info(sock, BIO_SOCK_INFO_ADDRESS, &peer_info)) {
  1933. BIO_printf(bio_err, "getsockname:errno=%d\n",
  1934. get_last_socket_error());
  1935. BIO_free(sbio);
  1936. BIO_ADDR_free(peer_info.addr);
  1937. BIO_closesocket(sock);
  1938. goto end;
  1939. }
  1940. (void)BIO_ctrl_set_connected(sbio, peer_info.addr);
  1941. BIO_ADDR_free(peer_info.addr);
  1942. peer_info.addr = NULL;
  1943. if (enable_timeouts) {
  1944. timeout.tv_sec = 0;
  1945. timeout.tv_usec = DGRAM_RCV_TIMEOUT;
  1946. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
  1947. timeout.tv_sec = 0;
  1948. timeout.tv_usec = DGRAM_SND_TIMEOUT;
  1949. BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
  1950. }
  1951. if (socket_mtu) {
  1952. if (socket_mtu < DTLS_get_link_min_mtu(con)) {
  1953. BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
  1954. DTLS_get_link_min_mtu(con));
  1955. BIO_free(sbio);
  1956. goto shut;
  1957. }
  1958. SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
  1959. if (!DTLS_set_link_mtu(con, socket_mtu)) {
  1960. BIO_printf(bio_err, "Failed to set MTU\n");
  1961. BIO_free(sbio);
  1962. goto shut;
  1963. }
  1964. } else {
  1965. /* want to do MTU discovery */
  1966. BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
  1967. }
  1968. } else
  1969. #endif /* OPENSSL_NO_DTLS */
  1970. sbio = BIO_new_socket(sock, BIO_NOCLOSE);
  1971. if (sbio == NULL) {
  1972. BIO_printf(bio_err, "Unable to create BIO\n");
  1973. ERR_print_errors(bio_err);
  1974. BIO_closesocket(sock);
  1975. goto end;
  1976. }
  1977. /* Now that we're using a BIO... */
  1978. if (tfo_addr != NULL)
  1979. (void)BIO_set_conn_address(sbio, tfo_addr);
  1980. if (tfo)
  1981. (void)BIO_set_tfo(sbio, 1);
  1982. if (nbio_test) {
  1983. BIO *test;
  1984. test = BIO_new(BIO_f_nbio_test());
  1985. if (test == NULL) {
  1986. BIO_printf(bio_err, "Unable to create BIO\n");
  1987. BIO_free(sbio);
  1988. goto shut;
  1989. }
  1990. sbio = BIO_push(test, sbio);
  1991. }
  1992. if (c_debug) {
  1993. BIO_set_callback_ex(sbio, bio_dump_callback);
  1994. BIO_set_callback_arg(sbio, (char *)bio_c_out);
  1995. }
  1996. if (c_msg) {
  1997. #ifndef OPENSSL_NO_SSL_TRACE
  1998. if (c_msg == 2)
  1999. SSL_set_msg_callback(con, SSL_trace);
  2000. else
  2001. #endif
  2002. SSL_set_msg_callback(con, msg_cb);
  2003. SSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out);
  2004. }
  2005. if (c_tlsextdebug) {
  2006. SSL_set_tlsext_debug_callback(con, tlsext_cb);
  2007. SSL_set_tlsext_debug_arg(con, bio_c_out);
  2008. }
  2009. #ifndef OPENSSL_NO_OCSP
  2010. if (c_status_req) {
  2011. SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
  2012. SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
  2013. SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
  2014. }
  2015. #endif
  2016. SSL_set_bio(con, sbio, sbio);
  2017. SSL_set_connect_state(con);
  2018. /* ok, lets connect */
  2019. if (fileno_stdin() > SSL_get_fd(con))
  2020. width = fileno_stdin() + 1;
  2021. else
  2022. width = SSL_get_fd(con) + 1;
  2023. read_tty = 1;
  2024. write_tty = 0;
  2025. tty_on = 0;
  2026. read_ssl = 1;
  2027. write_ssl = 1;
  2028. cbuf_len = 0;
  2029. cbuf_off = 0;
  2030. sbuf_len = 0;
  2031. sbuf_off = 0;
  2032. switch ((PROTOCOL_CHOICE) starttls_proto) {
  2033. case PROTO_OFF:
  2034. break;
  2035. case PROTO_LMTP:
  2036. case PROTO_SMTP:
  2037. {
  2038. /*
  2039. * This is an ugly hack that does a lot of assumptions. We do
  2040. * have to handle multi-line responses which may come in a single
  2041. * packet or not. We therefore have to use BIO_gets() which does
  2042. * need a buffering BIO. So during the initial chitchat we do
  2043. * push a buffering BIO into the chain that is removed again
  2044. * later on to not disturb the rest of the s_client operation.
  2045. */
  2046. int foundit = 0;
  2047. BIO *fbio = BIO_new(BIO_f_buffer());
  2048. if (fbio == NULL) {
  2049. BIO_printf(bio_err, "Unable to create BIO\n");
  2050. goto shut;
  2051. }
  2052. BIO_push(fbio, sbio);
  2053. /* Wait for multi-line response to end from LMTP or SMTP */
  2054. do {
  2055. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  2056. } while (mbuf_len > 3 && mbuf[3] == '-');
  2057. if (protohost == NULL)
  2058. protohost = "mail.example.com";
  2059. if (starttls_proto == (int)PROTO_LMTP)
  2060. BIO_printf(fbio, "LHLO %s\r\n", protohost);
  2061. else
  2062. BIO_printf(fbio, "EHLO %s\r\n", protohost);
  2063. (void)BIO_flush(fbio);
  2064. /*
  2065. * Wait for multi-line response to end LHLO LMTP or EHLO SMTP
  2066. * response.
  2067. */
  2068. do {
  2069. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  2070. if (strstr(mbuf, "STARTTLS"))
  2071. foundit = 1;
  2072. } while (mbuf_len > 3 && mbuf[3] == '-');
  2073. (void)BIO_flush(fbio);
  2074. BIO_pop(fbio);
  2075. BIO_free(fbio);
  2076. if (!foundit)
  2077. BIO_printf(bio_err,
  2078. "Didn't find STARTTLS in server response,"
  2079. " trying anyway...\n");
  2080. BIO_printf(sbio, "STARTTLS\r\n");
  2081. BIO_read(sbio, sbuf, BUFSIZZ);
  2082. }
  2083. break;
  2084. case PROTO_POP3:
  2085. {
  2086. BIO_read(sbio, mbuf, BUFSIZZ);
  2087. BIO_printf(sbio, "STLS\r\n");
  2088. mbuf_len = BIO_read(sbio, sbuf, BUFSIZZ);
  2089. if (mbuf_len < 0) {
  2090. BIO_printf(bio_err, "BIO_read failed\n");
  2091. goto end;
  2092. }
  2093. }
  2094. break;
  2095. case PROTO_IMAP:
  2096. {
  2097. int foundit = 0;
  2098. BIO *fbio = BIO_new(BIO_f_buffer());
  2099. if (fbio == NULL) {
  2100. BIO_printf(bio_err, "Unable to create BIO\n");
  2101. goto shut;
  2102. }
  2103. BIO_push(fbio, sbio);
  2104. BIO_gets(fbio, mbuf, BUFSIZZ);
  2105. /* STARTTLS command requires CAPABILITY... */
  2106. BIO_printf(fbio, ". CAPABILITY\r\n");
  2107. (void)BIO_flush(fbio);
  2108. /* wait for multi-line CAPABILITY response */
  2109. do {
  2110. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  2111. if (strstr(mbuf, "STARTTLS"))
  2112. foundit = 1;
  2113. }
  2114. while (mbuf_len > 3 && mbuf[0] != '.');
  2115. (void)BIO_flush(fbio);
  2116. BIO_pop(fbio);
  2117. BIO_free(fbio);
  2118. if (!foundit)
  2119. BIO_printf(bio_err,
  2120. "Didn't find STARTTLS in server response,"
  2121. " trying anyway...\n");
  2122. BIO_printf(sbio, ". STARTTLS\r\n");
  2123. BIO_read(sbio, sbuf, BUFSIZZ);
  2124. }
  2125. break;
  2126. case PROTO_FTP:
  2127. {
  2128. BIO *fbio = BIO_new(BIO_f_buffer());
  2129. if (fbio == NULL) {
  2130. BIO_printf(bio_err, "Unable to create BIO\n");
  2131. goto shut;
  2132. }
  2133. BIO_push(fbio, sbio);
  2134. /* wait for multi-line response to end from FTP */
  2135. do {
  2136. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  2137. }
  2138. while (mbuf_len > 3 && (!isdigit(mbuf[0]) || !isdigit(mbuf[1]) || !isdigit(mbuf[2]) || mbuf[3] != ' '));
  2139. (void)BIO_flush(fbio);
  2140. BIO_pop(fbio);
  2141. BIO_free(fbio);
  2142. BIO_printf(sbio, "AUTH TLS\r\n");
  2143. BIO_read(sbio, sbuf, BUFSIZZ);
  2144. }
  2145. break;
  2146. case PROTO_XMPP:
  2147. case PROTO_XMPP_SERVER:
  2148. {
  2149. int seen = 0;
  2150. BIO_printf(sbio, "<stream:stream "
  2151. "xmlns:stream='http://etherx.jabber.org/streams' "
  2152. "xmlns='jabber:%s' to='%s' version='1.0'>",
  2153. starttls_proto == PROTO_XMPP ? "client" : "server",
  2154. protohost ? protohost : host);
  2155. seen = BIO_read(sbio, mbuf, BUFSIZZ);
  2156. if (seen < 0) {
  2157. BIO_printf(bio_err, "BIO_read failed\n");
  2158. goto end;
  2159. }
  2160. mbuf[seen] = '\0';
  2161. while (!strstr
  2162. (mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'")
  2163. && !strstr(mbuf,
  2164. "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\""))
  2165. {
  2166. seen = BIO_read(sbio, mbuf, BUFSIZZ);
  2167. if (seen <= 0)
  2168. goto shut;
  2169. mbuf[seen] = '\0';
  2170. }
  2171. BIO_printf(sbio,
  2172. "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
  2173. seen = BIO_read(sbio, sbuf, BUFSIZZ);
  2174. if (seen < 0) {
  2175. BIO_printf(bio_err, "BIO_read failed\n");
  2176. goto shut;
  2177. }
  2178. sbuf[seen] = '\0';
  2179. if (!strstr(sbuf, "<proceed"))
  2180. goto shut;
  2181. mbuf[0] = '\0';
  2182. }
  2183. break;
  2184. case PROTO_TELNET:
  2185. {
  2186. static const unsigned char tls_do[] = {
  2187. /* IAC DO START_TLS */
  2188. 255, 253, 46
  2189. };
  2190. static const unsigned char tls_will[] = {
  2191. /* IAC WILL START_TLS */
  2192. 255, 251, 46
  2193. };
  2194. static const unsigned char tls_follows[] = {
  2195. /* IAC SB START_TLS FOLLOWS IAC SE */
  2196. 255, 250, 46, 1, 255, 240
  2197. };
  2198. int bytes;
  2199. /* Telnet server should demand we issue START_TLS */
  2200. bytes = BIO_read(sbio, mbuf, BUFSIZZ);
  2201. if (bytes != 3 || memcmp(mbuf, tls_do, 3) != 0)
  2202. goto shut;
  2203. /* Agree to issue START_TLS and send the FOLLOWS sub-command */
  2204. BIO_write(sbio, tls_will, 3);
  2205. BIO_write(sbio, tls_follows, 6);
  2206. (void)BIO_flush(sbio);
  2207. /* Telnet server also sent the FOLLOWS sub-command */
  2208. bytes = BIO_read(sbio, mbuf, BUFSIZZ);
  2209. if (bytes != 6 || memcmp(mbuf, tls_follows, 6) != 0)
  2210. goto shut;
  2211. }
  2212. break;
  2213. case PROTO_CONNECT:
  2214. /* Here we must use the connect string target host & port */
  2215. if (!OSSL_HTTP_proxy_connect(sbio, thost, tport, proxyuser, proxypass,
  2216. 0 /* no timeout */, bio_err, prog))
  2217. goto shut;
  2218. break;
  2219. case PROTO_IRC:
  2220. {
  2221. int numeric;
  2222. BIO *fbio = BIO_new(BIO_f_buffer());
  2223. if (fbio == NULL) {
  2224. BIO_printf(bio_err, "Unable to create BIO\n");
  2225. goto end;
  2226. }
  2227. BIO_push(fbio, sbio);
  2228. BIO_printf(fbio, "STARTTLS\r\n");
  2229. (void)BIO_flush(fbio);
  2230. width = SSL_get_fd(con) + 1;
  2231. do {
  2232. numeric = 0;
  2233. FD_ZERO(&readfds);
  2234. openssl_fdset(SSL_get_fd(con), &readfds);
  2235. timeout.tv_sec = S_CLIENT_IRC_READ_TIMEOUT;
  2236. timeout.tv_usec = 0;
  2237. /*
  2238. * If the IRCd doesn't respond within
  2239. * S_CLIENT_IRC_READ_TIMEOUT seconds, assume
  2240. * it doesn't support STARTTLS. Many IRCds
  2241. * will not give _any_ sort of response to a
  2242. * STARTTLS command when it's not supported.
  2243. */
  2244. if (!BIO_get_buffer_num_lines(fbio)
  2245. && !BIO_pending(fbio)
  2246. && !BIO_pending(sbio)
  2247. && select(width, (void *)&readfds, NULL, NULL,
  2248. &timeout) < 1) {
  2249. BIO_printf(bio_err,
  2250. "Timeout waiting for response (%d seconds).\n",
  2251. S_CLIENT_IRC_READ_TIMEOUT);
  2252. break;
  2253. }
  2254. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  2255. if (mbuf_len < 1 || sscanf(mbuf, "%*s %d", &numeric) != 1)
  2256. break;
  2257. /* :example.net 451 STARTTLS :You have not registered */
  2258. /* :example.net 421 STARTTLS :Unknown command */
  2259. if ((numeric == 451 || numeric == 421)
  2260. && strstr(mbuf, "STARTTLS") != NULL) {
  2261. BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf);
  2262. break;
  2263. }
  2264. if (numeric == 691) {
  2265. BIO_printf(bio_err, "STARTTLS negotiation failed: ");
  2266. ERR_print_errors(bio_err);
  2267. break;
  2268. }
  2269. } while (numeric != 670);
  2270. (void)BIO_flush(fbio);
  2271. BIO_pop(fbio);
  2272. BIO_free(fbio);
  2273. if (numeric != 670) {
  2274. BIO_printf(bio_err, "Server does not support STARTTLS.\n");
  2275. ret = 1;
  2276. goto shut;
  2277. }
  2278. }
  2279. break;
  2280. case PROTO_MYSQL:
  2281. {
  2282. /* SSL request packet */
  2283. static const unsigned char ssl_req[] = {
  2284. /* payload_length, sequence_id */
  2285. 0x20, 0x00, 0x00, 0x01,
  2286. /* payload */
  2287. /* capability flags, CLIENT_SSL always set */
  2288. 0x85, 0xae, 0x7f, 0x00,
  2289. /* max-packet size */
  2290. 0x00, 0x00, 0x00, 0x01,
  2291. /* character set */
  2292. 0x21,
  2293. /* string[23] reserved (all [0]) */
  2294. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2295. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2296. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2297. };
  2298. int bytes = 0;
  2299. int ssl_flg = 0x800;
  2300. int pos;
  2301. const unsigned char *packet = (const unsigned char *)sbuf;
  2302. /* Receiving Initial Handshake packet. */
  2303. bytes = BIO_read(sbio, (void *)packet, BUFSIZZ);
  2304. if (bytes < 0) {
  2305. BIO_printf(bio_err, "BIO_read failed\n");
  2306. goto shut;
  2307. /* Packet length[3], Packet number[1] + minimum payload[17] */
  2308. } else if (bytes < 21) {
  2309. BIO_printf(bio_err, "MySQL packet too short.\n");
  2310. goto shut;
  2311. } else if (bytes != (4 + packet[0] +
  2312. (packet[1] << 8) +
  2313. (packet[2] << 16))) {
  2314. BIO_printf(bio_err, "MySQL packet length does not match.\n");
  2315. goto shut;
  2316. /* protocol version[1] */
  2317. } else if (packet[4] != 0xA) {
  2318. BIO_printf(bio_err,
  2319. "Only MySQL protocol version 10 is supported.\n");
  2320. goto shut;
  2321. }
  2322. pos = 5;
  2323. /* server version[string+NULL] */
  2324. for (;;) {
  2325. if (pos >= bytes) {
  2326. BIO_printf(bio_err, "Cannot confirm server version. ");
  2327. goto shut;
  2328. } else if (packet[pos++] == '\0') {
  2329. break;
  2330. }
  2331. }
  2332. /* make sure we have at least 15 bytes left in the packet */
  2333. if (pos + 15 > bytes) {
  2334. BIO_printf(bio_err,
  2335. "MySQL server handshake packet is broken.\n");
  2336. goto shut;
  2337. }
  2338. pos += 12; /* skip over conn id[4] + SALT[8] */
  2339. if (packet[pos++] != '\0') { /* verify filler */
  2340. BIO_printf(bio_err,
  2341. "MySQL packet is broken.\n");
  2342. goto shut;
  2343. }
  2344. /* capability flags[2] */
  2345. if (!((packet[pos] + (packet[pos + 1] << 8)) & ssl_flg)) {
  2346. BIO_printf(bio_err, "MySQL server does not support SSL.\n");
  2347. goto shut;
  2348. }
  2349. /* Sending SSL Handshake packet. */
  2350. BIO_write(sbio, ssl_req, sizeof(ssl_req));
  2351. (void)BIO_flush(sbio);
  2352. }
  2353. break;
  2354. case PROTO_POSTGRES:
  2355. {
  2356. static const unsigned char ssl_request[] = {
  2357. /* Length SSLRequest */
  2358. 0, 0, 0, 8, 4, 210, 22, 47
  2359. };
  2360. int bytes;
  2361. /* Send SSLRequest packet */
  2362. BIO_write(sbio, ssl_request, 8);
  2363. (void)BIO_flush(sbio);
  2364. /* Reply will be a single S if SSL is enabled */
  2365. bytes = BIO_read(sbio, sbuf, BUFSIZZ);
  2366. if (bytes != 1 || sbuf[0] != 'S')
  2367. goto shut;
  2368. }
  2369. break;
  2370. case PROTO_NNTP:
  2371. {
  2372. int foundit = 0;
  2373. BIO *fbio = BIO_new(BIO_f_buffer());
  2374. if (fbio == NULL) {
  2375. BIO_printf(bio_err, "Unable to create BIO\n");
  2376. goto end;
  2377. }
  2378. BIO_push(fbio, sbio);
  2379. BIO_gets(fbio, mbuf, BUFSIZZ);
  2380. /* STARTTLS command requires CAPABILITIES... */
  2381. BIO_printf(fbio, "CAPABILITIES\r\n");
  2382. (void)BIO_flush(fbio);
  2383. BIO_gets(fbio, mbuf, BUFSIZZ);
  2384. /* no point in trying to parse the CAPABILITIES response if there is none */
  2385. if (strstr(mbuf, "101") != NULL) {
  2386. /* wait for multi-line CAPABILITIES response */
  2387. do {
  2388. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  2389. if (strstr(mbuf, "STARTTLS"))
  2390. foundit = 1;
  2391. } while (mbuf_len > 1 && mbuf[0] != '.');
  2392. }
  2393. (void)BIO_flush(fbio);
  2394. BIO_pop(fbio);
  2395. BIO_free(fbio);
  2396. if (!foundit)
  2397. BIO_printf(bio_err,
  2398. "Didn't find STARTTLS in server response,"
  2399. " trying anyway...\n");
  2400. BIO_printf(sbio, "STARTTLS\r\n");
  2401. mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
  2402. if (mbuf_len < 0) {
  2403. BIO_printf(bio_err, "BIO_read failed\n");
  2404. goto end;
  2405. }
  2406. mbuf[mbuf_len] = '\0';
  2407. if (strstr(mbuf, "382") == NULL) {
  2408. BIO_printf(bio_err, "STARTTLS failed: %s", mbuf);
  2409. goto shut;
  2410. }
  2411. }
  2412. break;
  2413. case PROTO_SIEVE:
  2414. {
  2415. int foundit = 0;
  2416. BIO *fbio = BIO_new(BIO_f_buffer());
  2417. if (fbio == NULL) {
  2418. BIO_printf(bio_err, "Unable to create BIO\n");
  2419. goto end;
  2420. }
  2421. BIO_push(fbio, sbio);
  2422. /* wait for multi-line response to end from Sieve */
  2423. do {
  2424. mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
  2425. /*
  2426. * According to RFC 5804 § 1.7, capability
  2427. * is case-insensitive, make it uppercase
  2428. */
  2429. if (mbuf_len > 1 && mbuf[0] == '"') {
  2430. make_uppercase(mbuf);
  2431. if (HAS_PREFIX(mbuf, "\"STARTTLS\""))
  2432. foundit = 1;
  2433. }
  2434. } while (mbuf_len > 1 && mbuf[0] == '"');
  2435. (void)BIO_flush(fbio);
  2436. BIO_pop(fbio);
  2437. BIO_free(fbio);
  2438. if (!foundit)
  2439. BIO_printf(bio_err,
  2440. "Didn't find STARTTLS in server response,"
  2441. " trying anyway...\n");
  2442. BIO_printf(sbio, "STARTTLS\r\n");
  2443. mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
  2444. if (mbuf_len < 0) {
  2445. BIO_printf(bio_err, "BIO_read failed\n");
  2446. goto end;
  2447. }
  2448. mbuf[mbuf_len] = '\0';
  2449. if (mbuf_len < 2) {
  2450. BIO_printf(bio_err, "STARTTLS failed: %s", mbuf);
  2451. goto shut;
  2452. }
  2453. /*
  2454. * According to RFC 5804 § 2.2, response codes are case-
  2455. * insensitive, make it uppercase but preserve the response.
  2456. */
  2457. strncpy(sbuf, mbuf, 2);
  2458. make_uppercase(sbuf);
  2459. if (!HAS_PREFIX(sbuf, "OK")) {
  2460. BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf);
  2461. goto shut;
  2462. }
  2463. }
  2464. break;
  2465. case PROTO_LDAP:
  2466. {
  2467. /* StartTLS Operation according to RFC 4511 */
  2468. static char ldap_tls_genconf[] = "asn1=SEQUENCE:LDAPMessage\n"
  2469. "[LDAPMessage]\n"
  2470. "messageID=INTEGER:1\n"
  2471. "extendedReq=EXPLICIT:23A,IMPLICIT:0C,"
  2472. "FORMAT:ASCII,OCT:1.3.6.1.4.1.1466.20037\n";
  2473. long errline = -1;
  2474. char *genstr = NULL;
  2475. int result = -1;
  2476. ASN1_TYPE *atyp = NULL;
  2477. BIO *ldapbio = BIO_new(BIO_s_mem());
  2478. CONF *cnf = NCONF_new(NULL);
  2479. if (ldapbio == NULL || cnf == NULL) {
  2480. BIO_free(ldapbio);
  2481. NCONF_free(cnf);
  2482. goto end;
  2483. }
  2484. BIO_puts(ldapbio, ldap_tls_genconf);
  2485. if (NCONF_load_bio(cnf, ldapbio, &errline) <= 0) {
  2486. BIO_free(ldapbio);
  2487. NCONF_free(cnf);
  2488. if (errline <= 0) {
  2489. BIO_printf(bio_err, "NCONF_load_bio failed\n");
  2490. goto end;
  2491. } else {
  2492. BIO_printf(bio_err, "Error on line %ld\n", errline);
  2493. goto end;
  2494. }
  2495. }
  2496. BIO_free(ldapbio);
  2497. genstr = NCONF_get_string(cnf, "default", "asn1");
  2498. if (genstr == NULL) {
  2499. NCONF_free(cnf);
  2500. BIO_printf(bio_err, "NCONF_get_string failed\n");
  2501. goto end;
  2502. }
  2503. atyp = ASN1_generate_nconf(genstr, cnf);
  2504. if (atyp == NULL) {
  2505. NCONF_free(cnf);
  2506. BIO_printf(bio_err, "ASN1_generate_nconf failed\n");
  2507. goto end;
  2508. }
  2509. NCONF_free(cnf);
  2510. /* Send SSLRequest packet */
  2511. BIO_write(sbio, atyp->value.sequence->data,
  2512. atyp->value.sequence->length);
  2513. (void)BIO_flush(sbio);
  2514. ASN1_TYPE_free(atyp);
  2515. mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
  2516. if (mbuf_len < 0) {
  2517. BIO_printf(bio_err, "BIO_read failed\n");
  2518. goto end;
  2519. }
  2520. result = ldap_ExtendedResponse_parse(mbuf, mbuf_len);
  2521. if (result < 0) {
  2522. BIO_printf(bio_err, "ldap_ExtendedResponse_parse failed\n");
  2523. goto shut;
  2524. } else if (result > 0) {
  2525. BIO_printf(bio_err, "STARTTLS failed, LDAP Result Code: %i\n",
  2526. result);
  2527. goto shut;
  2528. }
  2529. mbuf_len = 0;
  2530. }
  2531. break;
  2532. }
  2533. if (early_data_file != NULL
  2534. && ((SSL_get0_session(con) != NULL
  2535. && SSL_SESSION_get_max_early_data(SSL_get0_session(con)) > 0)
  2536. || (psksess != NULL
  2537. && SSL_SESSION_get_max_early_data(psksess) > 0))) {
  2538. BIO *edfile = BIO_new_file(early_data_file, "r");
  2539. size_t readbytes, writtenbytes;
  2540. int finish = 0;
  2541. if (edfile == NULL) {
  2542. BIO_printf(bio_err, "Cannot open early data file\n");
  2543. goto shut;
  2544. }
  2545. while (!finish) {
  2546. if (!BIO_read_ex(edfile, cbuf, BUFSIZZ, &readbytes))
  2547. finish = 1;
  2548. while (!SSL_write_early_data(con, cbuf, readbytes, &writtenbytes)) {
  2549. switch (SSL_get_error(con, 0)) {
  2550. case SSL_ERROR_WANT_WRITE:
  2551. case SSL_ERROR_WANT_ASYNC:
  2552. case SSL_ERROR_WANT_READ:
  2553. /* Just keep trying - busy waiting */
  2554. continue;
  2555. default:
  2556. BIO_printf(bio_err, "Error writing early data\n");
  2557. BIO_free(edfile);
  2558. ERR_print_errors(bio_err);
  2559. goto shut;
  2560. }
  2561. }
  2562. }
  2563. BIO_free(edfile);
  2564. }
  2565. for (;;) {
  2566. FD_ZERO(&readfds);
  2567. FD_ZERO(&writefds);
  2568. if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout))
  2569. timeoutp = &timeout;
  2570. else
  2571. timeoutp = NULL;
  2572. if (!SSL_is_init_finished(con) && SSL_total_renegotiations(con) == 0
  2573. && SSL_get_key_update_type(con) == SSL_KEY_UPDATE_NONE) {
  2574. in_init = 1;
  2575. tty_on = 0;
  2576. } else {
  2577. tty_on = 1;
  2578. if (in_init) {
  2579. in_init = 0;
  2580. if (c_brief) {
  2581. BIO_puts(bio_err, "CONNECTION ESTABLISHED\n");
  2582. print_ssl_summary(con);
  2583. }
  2584. print_stuff(bio_c_out, con, full_log);
  2585. if (full_log > 0)
  2586. full_log--;
  2587. if (starttls_proto) {
  2588. BIO_write(bio_err, mbuf, mbuf_len);
  2589. /* We don't need to know any more */
  2590. if (!reconnect)
  2591. starttls_proto = PROTO_OFF;
  2592. }
  2593. if (reconnect) {
  2594. reconnect--;
  2595. BIO_printf(bio_c_out,
  2596. "drop connection and then reconnect\n");
  2597. do_ssl_shutdown(con);
  2598. SSL_set_connect_state(con);
  2599. BIO_closesocket(SSL_get_fd(con));
  2600. goto re_start;
  2601. }
  2602. }
  2603. }
  2604. ssl_pending = read_ssl && SSL_has_pending(con);
  2605. if (!ssl_pending) {
  2606. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
  2607. if (tty_on) {
  2608. /*
  2609. * Note that select() returns when read _would not block_,
  2610. * and EOF satisfies that. To avoid a CPU-hogging loop,
  2611. * set the flag so we exit.
  2612. */
  2613. if (read_tty && !at_eof)
  2614. openssl_fdset(fileno_stdin(), &readfds);
  2615. #if !defined(OPENSSL_SYS_VMS)
  2616. if (write_tty)
  2617. openssl_fdset(fileno_stdout(), &writefds);
  2618. #endif
  2619. }
  2620. if (read_ssl)
  2621. openssl_fdset(SSL_get_fd(con), &readfds);
  2622. if (write_ssl)
  2623. openssl_fdset(SSL_get_fd(con), &writefds);
  2624. #else
  2625. if (!tty_on || !write_tty) {
  2626. if (read_ssl)
  2627. openssl_fdset(SSL_get_fd(con), &readfds);
  2628. if (write_ssl)
  2629. openssl_fdset(SSL_get_fd(con), &writefds);
  2630. }
  2631. #endif
  2632. /*
  2633. * Note: under VMS with SOCKETSHR the second parameter is
  2634. * currently of type (int *) whereas under other systems it is
  2635. * (void *) if you don't have a cast it will choke the compiler:
  2636. * if you do have a cast then you can either go for (int *) or
  2637. * (void *).
  2638. */
  2639. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  2640. /*
  2641. * Under Windows/DOS we make the assumption that we can always
  2642. * write to the tty: therefore if we need to write to the tty we
  2643. * just fall through. Otherwise we timeout the select every
  2644. * second and see if there are any keypresses. Note: this is a
  2645. * hack, in a proper Windows application we wouldn't do this.
  2646. */
  2647. i = 0;
  2648. if (!write_tty) {
  2649. if (read_tty) {
  2650. tv.tv_sec = 1;
  2651. tv.tv_usec = 0;
  2652. i = select(width, (void *)&readfds, (void *)&writefds,
  2653. NULL, &tv);
  2654. if (!i && (!has_stdin_waiting() || !read_tty))
  2655. continue;
  2656. } else
  2657. i = select(width, (void *)&readfds, (void *)&writefds,
  2658. NULL, timeoutp);
  2659. }
  2660. #else
  2661. i = select(width, (void *)&readfds, (void *)&writefds,
  2662. NULL, timeoutp);
  2663. #endif
  2664. if (i < 0) {
  2665. BIO_printf(bio_err, "bad select %d\n",
  2666. get_last_socket_error());
  2667. goto shut;
  2668. }
  2669. }
  2670. if (SSL_is_dtls(con) && DTLSv1_handle_timeout(con) > 0)
  2671. BIO_printf(bio_err, "TIMEOUT occurred\n");
  2672. if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) {
  2673. k = SSL_write(con, &(cbuf[cbuf_off]), (unsigned int)cbuf_len);
  2674. switch (SSL_get_error(con, k)) {
  2675. case SSL_ERROR_NONE:
  2676. cbuf_off += k;
  2677. cbuf_len -= k;
  2678. if (k <= 0)
  2679. goto end;
  2680. /* we have done a write(con,NULL,0); */
  2681. if (cbuf_len <= 0) {
  2682. read_tty = 1;
  2683. write_ssl = 0;
  2684. } else { /* if (cbuf_len > 0) */
  2685. read_tty = 0;
  2686. write_ssl = 1;
  2687. }
  2688. break;
  2689. case SSL_ERROR_WANT_WRITE:
  2690. BIO_printf(bio_c_out, "write W BLOCK\n");
  2691. write_ssl = 1;
  2692. read_tty = 0;
  2693. break;
  2694. case SSL_ERROR_WANT_ASYNC:
  2695. BIO_printf(bio_c_out, "write A BLOCK\n");
  2696. wait_for_async(con);
  2697. write_ssl = 1;
  2698. read_tty = 0;
  2699. break;
  2700. case SSL_ERROR_WANT_READ:
  2701. BIO_printf(bio_c_out, "write R BLOCK\n");
  2702. write_tty = 0;
  2703. read_ssl = 1;
  2704. write_ssl = 0;
  2705. break;
  2706. case SSL_ERROR_WANT_X509_LOOKUP:
  2707. BIO_printf(bio_c_out, "write X BLOCK\n");
  2708. break;
  2709. case SSL_ERROR_ZERO_RETURN:
  2710. if (cbuf_len != 0) {
  2711. BIO_printf(bio_c_out, "shutdown\n");
  2712. ret = 0;
  2713. goto shut;
  2714. } else {
  2715. read_tty = 1;
  2716. write_ssl = 0;
  2717. break;
  2718. }
  2719. case SSL_ERROR_SYSCALL:
  2720. if ((k != 0) || (cbuf_len != 0)) {
  2721. int sockerr = get_last_socket_error();
  2722. if (!tfo || sockerr != EISCONN) {
  2723. BIO_printf(bio_err, "write:errno=%d\n", sockerr);
  2724. goto shut;
  2725. }
  2726. } else {
  2727. read_tty = 1;
  2728. write_ssl = 0;
  2729. }
  2730. break;
  2731. case SSL_ERROR_WANT_ASYNC_JOB:
  2732. /* This shouldn't ever happen in s_client - treat as an error */
  2733. case SSL_ERROR_SSL:
  2734. ERR_print_errors(bio_err);
  2735. goto shut;
  2736. }
  2737. }
  2738. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VMS)
  2739. /* Assume Windows/DOS/BeOS can always write */
  2740. else if (!ssl_pending && write_tty)
  2741. #else
  2742. else if (!ssl_pending && FD_ISSET(fileno_stdout(), &writefds))
  2743. #endif
  2744. {
  2745. #ifdef CHARSET_EBCDIC
  2746. ascii2ebcdic(&(sbuf[sbuf_off]), &(sbuf[sbuf_off]), sbuf_len);
  2747. #endif
  2748. i = raw_write_stdout(&(sbuf[sbuf_off]), sbuf_len);
  2749. if (i <= 0) {
  2750. BIO_printf(bio_c_out, "DONE\n");
  2751. ret = 0;
  2752. goto shut;
  2753. }
  2754. sbuf_len -= i;
  2755. sbuf_off += i;
  2756. if (sbuf_len <= 0) {
  2757. read_ssl = 1;
  2758. write_tty = 0;
  2759. }
  2760. } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) {
  2761. #ifdef RENEG
  2762. {
  2763. static int iiii;
  2764. if (++iiii == 52) {
  2765. SSL_renegotiate(con);
  2766. iiii = 0;
  2767. }
  2768. }
  2769. #endif
  2770. k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
  2771. switch (SSL_get_error(con, k)) {
  2772. case SSL_ERROR_NONE:
  2773. if (k <= 0)
  2774. goto end;
  2775. sbuf_off = 0;
  2776. sbuf_len = k;
  2777. read_ssl = 0;
  2778. write_tty = 1;
  2779. break;
  2780. case SSL_ERROR_WANT_ASYNC:
  2781. BIO_printf(bio_c_out, "read A BLOCK\n");
  2782. wait_for_async(con);
  2783. write_tty = 0;
  2784. read_ssl = 1;
  2785. if ((read_tty == 0) && (write_ssl == 0))
  2786. write_ssl = 1;
  2787. break;
  2788. case SSL_ERROR_WANT_WRITE:
  2789. BIO_printf(bio_c_out, "read W BLOCK\n");
  2790. write_ssl = 1;
  2791. read_tty = 0;
  2792. break;
  2793. case SSL_ERROR_WANT_READ:
  2794. BIO_printf(bio_c_out, "read R BLOCK\n");
  2795. write_tty = 0;
  2796. read_ssl = 1;
  2797. if ((read_tty == 0) && (write_ssl == 0))
  2798. write_ssl = 1;
  2799. break;
  2800. case SSL_ERROR_WANT_X509_LOOKUP:
  2801. BIO_printf(bio_c_out, "read X BLOCK\n");
  2802. break;
  2803. case SSL_ERROR_SYSCALL:
  2804. ret = get_last_socket_error();
  2805. if (c_brief)
  2806. BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\n");
  2807. else
  2808. BIO_printf(bio_err, "read:errno=%d\n", ret);
  2809. goto shut;
  2810. case SSL_ERROR_ZERO_RETURN:
  2811. BIO_printf(bio_c_out, "closed\n");
  2812. ret = 0;
  2813. goto shut;
  2814. case SSL_ERROR_WANT_ASYNC_JOB:
  2815. /* This shouldn't ever happen in s_client. Treat as an error */
  2816. case SSL_ERROR_SSL:
  2817. ERR_print_errors(bio_err);
  2818. goto shut;
  2819. }
  2820. }
  2821. /* don't wait for client input in the non-interactive mode */
  2822. else if (nointeractive) {
  2823. ret = 0;
  2824. goto shut;
  2825. }
  2826. /* OPENSSL_SYS_MSDOS includes OPENSSL_SYS_WINDOWS */
  2827. #if defined(OPENSSL_SYS_MSDOS)
  2828. else if (has_stdin_waiting())
  2829. #else
  2830. else if (FD_ISSET(fileno_stdin(), &readfds))
  2831. #endif
  2832. {
  2833. if (crlf) {
  2834. int j, lf_num;
  2835. i = raw_read_stdin(cbuf, BUFSIZZ / 2);
  2836. lf_num = 0;
  2837. /* both loops are skipped when i <= 0 */
  2838. for (j = 0; j < i; j++)
  2839. if (cbuf[j] == '\n')
  2840. lf_num++;
  2841. for (j = i - 1; j >= 0; j--) {
  2842. cbuf[j + lf_num] = cbuf[j];
  2843. if (cbuf[j] == '\n') {
  2844. lf_num--;
  2845. i++;
  2846. cbuf[j + lf_num] = '\r';
  2847. }
  2848. }
  2849. assert(lf_num == 0);
  2850. } else
  2851. i = raw_read_stdin(cbuf, BUFSIZZ);
  2852. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
  2853. if (i == 0)
  2854. at_eof = 1;
  2855. #endif
  2856. if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q' && cmdletters))) {
  2857. BIO_printf(bio_err, "DONE\n");
  2858. ret = 0;
  2859. goto shut;
  2860. }
  2861. if ((!c_ign_eof) && (cbuf[0] == 'R' && cmdletters)) {
  2862. BIO_printf(bio_err, "RENEGOTIATING\n");
  2863. SSL_renegotiate(con);
  2864. cbuf_len = 0;
  2865. } else if (!c_ign_eof && (cbuf[0] == 'K' || cbuf[0] == 'k')
  2866. && cmdletters) {
  2867. BIO_printf(bio_err, "KEYUPDATE\n");
  2868. SSL_key_update(con,
  2869. cbuf[0] == 'K' ? SSL_KEY_UPDATE_REQUESTED
  2870. : SSL_KEY_UPDATE_NOT_REQUESTED);
  2871. cbuf_len = 0;
  2872. } else {
  2873. cbuf_len = i;
  2874. cbuf_off = 0;
  2875. #ifdef CHARSET_EBCDIC
  2876. ebcdic2ascii(cbuf, cbuf, i);
  2877. #endif
  2878. }
  2879. write_ssl = 1;
  2880. read_tty = 0;
  2881. }
  2882. }
  2883. shut:
  2884. if (in_init)
  2885. print_stuff(bio_c_out, con, full_log);
  2886. do_ssl_shutdown(con);
  2887. /*
  2888. * If we ended with an alert being sent, but still with data in the
  2889. * network buffer to be read, then calling BIO_closesocket() will
  2890. * result in a TCP-RST being sent. On some platforms (notably
  2891. * Windows) then this will result in the peer immediately abandoning
  2892. * the connection including any buffered alert data before it has
  2893. * had a chance to be read. Shutting down the sending side first,
  2894. * and then closing the socket sends TCP-FIN first followed by
  2895. * TCP-RST. This seems to allow the peer to read the alert data.
  2896. */
  2897. shutdown(SSL_get_fd(con), 1); /* SHUT_WR */
  2898. /*
  2899. * We just said we have nothing else to say, but it doesn't mean that
  2900. * the other side has nothing. It's even recommended to consume incoming
  2901. * data. [In testing context this ensures that alerts are passed on...]
  2902. */
  2903. timeout.tv_sec = 0;
  2904. timeout.tv_usec = 500000; /* some extreme round-trip */
  2905. do {
  2906. FD_ZERO(&readfds);
  2907. openssl_fdset(sock, &readfds);
  2908. } while (select(sock + 1, &readfds, NULL, NULL, &timeout) > 0
  2909. && BIO_read(sbio, sbuf, BUFSIZZ) > 0);
  2910. BIO_closesocket(SSL_get_fd(con));
  2911. end:
  2912. if (con != NULL) {
  2913. if (prexit != 0)
  2914. print_stuff(bio_c_out, con, 1);
  2915. SSL_free(con);
  2916. }
  2917. SSL_SESSION_free(psksess);
  2918. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  2919. OPENSSL_free(next_proto.data);
  2920. #endif
  2921. SSL_CTX_free(ctx);
  2922. set_keylog_file(NULL, NULL);
  2923. X509_free(cert);
  2924. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  2925. EVP_PKEY_free(key);
  2926. OSSL_STACK_OF_X509_free(chain);
  2927. OPENSSL_free(pass);
  2928. #ifndef OPENSSL_NO_SRP
  2929. OPENSSL_free(srp_arg.srppassin);
  2930. #endif
  2931. OPENSSL_free(sname_alloc);
  2932. BIO_ADDR_free(tfo_addr);
  2933. OPENSSL_free(connectstr);
  2934. OPENSSL_free(bindstr);
  2935. OPENSSL_free(bindhost);
  2936. OPENSSL_free(bindport);
  2937. OPENSSL_free(host);
  2938. OPENSSL_free(port);
  2939. OPENSSL_free(thost);
  2940. OPENSSL_free(tport);
  2941. X509_VERIFY_PARAM_free(vpm);
  2942. ssl_excert_free(exc);
  2943. sk_OPENSSL_STRING_free(ssl_args);
  2944. sk_OPENSSL_STRING_free(dane_tlsa_rrset);
  2945. SSL_CONF_CTX_free(cctx);
  2946. OPENSSL_clear_free(cbuf, BUFSIZZ);
  2947. OPENSSL_clear_free(sbuf, BUFSIZZ);
  2948. OPENSSL_clear_free(mbuf, BUFSIZZ);
  2949. clear_free(proxypass);
  2950. release_engine(e);
  2951. BIO_free(bio_c_out);
  2952. bio_c_out = NULL;
  2953. BIO_free(bio_c_msg);
  2954. bio_c_msg = NULL;
  2955. return ret;
  2956. }
  2957. static void print_stuff(BIO *bio, SSL *s, int full)
  2958. {
  2959. X509 *peer = NULL;
  2960. STACK_OF(X509) *sk;
  2961. const SSL_CIPHER *c;
  2962. EVP_PKEY *public_key;
  2963. int i, istls13 = (SSL_version(s) == TLS1_3_VERSION);
  2964. long verify_result;
  2965. #ifndef OPENSSL_NO_COMP
  2966. const COMP_METHOD *comp, *expansion;
  2967. #endif
  2968. unsigned char *exportedkeymat;
  2969. #ifndef OPENSSL_NO_CT
  2970. const SSL_CTX *ctx = SSL_get_SSL_CTX(s);
  2971. #endif
  2972. if (full) {
  2973. int got_a_chain = 0;
  2974. sk = SSL_get_peer_cert_chain(s);
  2975. if (sk != NULL) {
  2976. got_a_chain = 1;
  2977. BIO_printf(bio, "---\nCertificate chain\n");
  2978. for (i = 0; i < sk_X509_num(sk); i++) {
  2979. BIO_printf(bio, "%2d s:", i);
  2980. X509_NAME_print_ex(bio, X509_get_subject_name(sk_X509_value(sk, i)), 0, get_nameopt());
  2981. BIO_puts(bio, "\n");
  2982. BIO_printf(bio, " i:");
  2983. X509_NAME_print_ex(bio, X509_get_issuer_name(sk_X509_value(sk, i)), 0, get_nameopt());
  2984. BIO_puts(bio, "\n");
  2985. public_key = X509_get_pubkey(sk_X509_value(sk, i));
  2986. if (public_key != NULL) {
  2987. BIO_printf(bio, " a:PKEY: %s, %d (bit); sigalg: %s\n",
  2988. OBJ_nid2sn(EVP_PKEY_get_base_id(public_key)),
  2989. EVP_PKEY_get_bits(public_key),
  2990. OBJ_nid2sn(X509_get_signature_nid(sk_X509_value(sk, i))));
  2991. EVP_PKEY_free(public_key);
  2992. }
  2993. BIO_printf(bio, " v:NotBefore: ");
  2994. ASN1_TIME_print(bio, X509_get0_notBefore(sk_X509_value(sk, i)));
  2995. BIO_printf(bio, "; NotAfter: ");
  2996. ASN1_TIME_print(bio, X509_get0_notAfter(sk_X509_value(sk, i)));
  2997. BIO_puts(bio, "\n");
  2998. if (c_showcerts)
  2999. PEM_write_bio_X509(bio, sk_X509_value(sk, i));
  3000. }
  3001. }
  3002. BIO_printf(bio, "---\n");
  3003. peer = SSL_get0_peer_certificate(s);
  3004. if (peer != NULL) {
  3005. BIO_printf(bio, "Server certificate\n");
  3006. /* Redundant if we showed the whole chain */
  3007. if (!(c_showcerts && got_a_chain))
  3008. PEM_write_bio_X509(bio, peer);
  3009. dump_cert_text(bio, peer);
  3010. } else {
  3011. BIO_printf(bio, "no peer certificate available\n");
  3012. }
  3013. print_ca_names(bio, s);
  3014. ssl_print_sigalgs(bio, s);
  3015. ssl_print_tmp_key(bio, s);
  3016. #ifndef OPENSSL_NO_CT
  3017. /*
  3018. * When the SSL session is anonymous, or resumed via an abbreviated
  3019. * handshake, no SCTs are provided as part of the handshake. While in
  3020. * a resumed session SCTs may be present in the session's certificate,
  3021. * no callbacks are invoked to revalidate these, and in any case that
  3022. * set of SCTs may be incomplete. Thus it makes little sense to
  3023. * attempt to display SCTs from a resumed session's certificate, and of
  3024. * course none are associated with an anonymous peer.
  3025. */
  3026. if (peer != NULL && !SSL_session_reused(s) && SSL_ct_is_enabled(s)) {
  3027. const STACK_OF(SCT) *scts = SSL_get0_peer_scts(s);
  3028. int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
  3029. BIO_printf(bio, "---\nSCTs present (%i)\n", sct_count);
  3030. if (sct_count > 0) {
  3031. const CTLOG_STORE *log_store = SSL_CTX_get0_ctlog_store(ctx);
  3032. BIO_printf(bio, "---\n");
  3033. for (i = 0; i < sct_count; ++i) {
  3034. SCT *sct = sk_SCT_value(scts, i);
  3035. BIO_printf(bio, "SCT validation status: %s\n",
  3036. SCT_validation_status_string(sct));
  3037. SCT_print(sct, bio, 0, log_store);
  3038. if (i < sct_count - 1)
  3039. BIO_printf(bio, "\n---\n");
  3040. }
  3041. BIO_printf(bio, "\n");
  3042. }
  3043. }
  3044. #endif
  3045. BIO_printf(bio,
  3046. "---\nSSL handshake has read %ju bytes "
  3047. "and written %ju bytes\n",
  3048. BIO_number_read(SSL_get_rbio(s)),
  3049. BIO_number_written(SSL_get_wbio(s)));
  3050. }
  3051. print_verify_detail(s, bio);
  3052. BIO_printf(bio, (SSL_session_reused(s) ? "---\nReused, " : "---\nNew, "));
  3053. c = SSL_get_current_cipher(s);
  3054. BIO_printf(bio, "%s, Cipher is %s\n",
  3055. SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
  3056. if (peer != NULL) {
  3057. EVP_PKEY *pktmp;
  3058. pktmp = X509_get0_pubkey(peer);
  3059. BIO_printf(bio, "Server public key is %d bit\n",
  3060. EVP_PKEY_get_bits(pktmp));
  3061. }
  3062. ssl_print_secure_renegotiation_notes(bio, s);
  3063. #ifndef OPENSSL_NO_COMP
  3064. comp = SSL_get_current_compression(s);
  3065. expansion = SSL_get_current_expansion(s);
  3066. BIO_printf(bio, "Compression: %s\n",
  3067. comp ? SSL_COMP_get_name(comp) : "NONE");
  3068. BIO_printf(bio, "Expansion: %s\n",
  3069. expansion ? SSL_COMP_get_name(expansion) : "NONE");
  3070. #endif
  3071. #ifndef OPENSSL_NO_KTLS
  3072. if (BIO_get_ktls_send(SSL_get_wbio(s)))
  3073. BIO_printf(bio_err, "Using Kernel TLS for sending\n");
  3074. if (BIO_get_ktls_recv(SSL_get_rbio(s)))
  3075. BIO_printf(bio_err, "Using Kernel TLS for receiving\n");
  3076. #endif
  3077. if (OSSL_TRACE_ENABLED(TLS)) {
  3078. /* Print out local port of connection: useful for debugging */
  3079. int sock;
  3080. union BIO_sock_info_u info;
  3081. sock = SSL_get_fd(s);
  3082. if ((info.addr = BIO_ADDR_new()) != NULL
  3083. && BIO_sock_info(sock, BIO_SOCK_INFO_ADDRESS, &info)) {
  3084. BIO_printf(bio_c_out, "LOCAL PORT is %u\n",
  3085. ntohs(BIO_ADDR_rawport(info.addr)));
  3086. }
  3087. BIO_ADDR_free(info.addr);
  3088. }
  3089. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  3090. if (next_proto.status != -1) {
  3091. const unsigned char *proto;
  3092. unsigned int proto_len;
  3093. SSL_get0_next_proto_negotiated(s, &proto, &proto_len);
  3094. BIO_printf(bio, "Next protocol: (%d) ", next_proto.status);
  3095. BIO_write(bio, proto, proto_len);
  3096. BIO_write(bio, "\n", 1);
  3097. }
  3098. #endif
  3099. {
  3100. const unsigned char *proto;
  3101. unsigned int proto_len;
  3102. SSL_get0_alpn_selected(s, &proto, &proto_len);
  3103. if (proto_len > 0) {
  3104. BIO_printf(bio, "ALPN protocol: ");
  3105. BIO_write(bio, proto, proto_len);
  3106. BIO_write(bio, "\n", 1);
  3107. } else
  3108. BIO_printf(bio, "No ALPN negotiated\n");
  3109. }
  3110. #ifndef OPENSSL_NO_SRTP
  3111. {
  3112. SRTP_PROTECTION_PROFILE *srtp_profile =
  3113. SSL_get_selected_srtp_profile(s);
  3114. if (srtp_profile)
  3115. BIO_printf(bio, "SRTP Extension negotiated, profile=%s\n",
  3116. srtp_profile->name);
  3117. }
  3118. #endif
  3119. if (istls13) {
  3120. switch (SSL_get_early_data_status(s)) {
  3121. case SSL_EARLY_DATA_NOT_SENT:
  3122. BIO_printf(bio, "Early data was not sent\n");
  3123. break;
  3124. case SSL_EARLY_DATA_REJECTED:
  3125. BIO_printf(bio, "Early data was rejected\n");
  3126. break;
  3127. case SSL_EARLY_DATA_ACCEPTED:
  3128. BIO_printf(bio, "Early data was accepted\n");
  3129. break;
  3130. }
  3131. /*
  3132. * We also print the verify results when we dump session information,
  3133. * but in TLSv1.3 we may not get that right away (or at all) depending
  3134. * on when we get a NewSessionTicket. Therefore we print it now as well.
  3135. */
  3136. verify_result = SSL_get_verify_result(s);
  3137. BIO_printf(bio, "Verify return code: %ld (%s)\n", verify_result,
  3138. X509_verify_cert_error_string(verify_result));
  3139. } else {
  3140. /* In TLSv1.3 we do this on arrival of a NewSessionTicket */
  3141. SSL_SESSION_print(bio, SSL_get_session(s));
  3142. }
  3143. if (SSL_get_session(s) != NULL && keymatexportlabel != NULL) {
  3144. BIO_printf(bio, "Keying material exporter:\n");
  3145. BIO_printf(bio, " Label: '%s'\n", keymatexportlabel);
  3146. BIO_printf(bio, " Length: %i bytes\n", keymatexportlen);
  3147. exportedkeymat = app_malloc(keymatexportlen, "export key");
  3148. if (SSL_export_keying_material(s, exportedkeymat,
  3149. keymatexportlen,
  3150. keymatexportlabel,
  3151. strlen(keymatexportlabel),
  3152. NULL, 0, 0) <= 0) {
  3153. BIO_printf(bio, " Error\n");
  3154. } else {
  3155. BIO_printf(bio, " Keying material: ");
  3156. for (i = 0; i < keymatexportlen; i++)
  3157. BIO_printf(bio, "%02X", exportedkeymat[i]);
  3158. BIO_printf(bio, "\n");
  3159. }
  3160. OPENSSL_free(exportedkeymat);
  3161. }
  3162. BIO_printf(bio, "---\n");
  3163. /* flush, or debugging output gets mixed with http response */
  3164. (void)BIO_flush(bio);
  3165. }
  3166. # ifndef OPENSSL_NO_OCSP
  3167. static int ocsp_resp_cb(SSL *s, void *arg)
  3168. {
  3169. const unsigned char *p;
  3170. int len;
  3171. OCSP_RESPONSE *rsp;
  3172. len = SSL_get_tlsext_status_ocsp_resp(s, &p);
  3173. BIO_puts(arg, "OCSP response: ");
  3174. if (p == NULL) {
  3175. BIO_puts(arg, "no response sent\n");
  3176. return 1;
  3177. }
  3178. rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
  3179. if (rsp == NULL) {
  3180. BIO_puts(arg, "response parse error\n");
  3181. BIO_dump_indent(arg, (char *)p, len, 4);
  3182. return 0;
  3183. }
  3184. BIO_puts(arg, "\n======================================\n");
  3185. OCSP_RESPONSE_print(arg, rsp, 0);
  3186. BIO_puts(arg, "======================================\n");
  3187. OCSP_RESPONSE_free(rsp);
  3188. return 1;
  3189. }
  3190. # endif
  3191. static int ldap_ExtendedResponse_parse(const char *buf, long rem)
  3192. {
  3193. const unsigned char *cur, *end;
  3194. long len;
  3195. int tag, xclass, inf, ret = -1;
  3196. cur = (const unsigned char *)buf;
  3197. end = cur + rem;
  3198. /*
  3199. * From RFC 4511:
  3200. *
  3201. * LDAPMessage ::= SEQUENCE {
  3202. * messageID MessageID,
  3203. * protocolOp CHOICE {
  3204. * ...
  3205. * extendedResp ExtendedResponse,
  3206. * ... },
  3207. * controls [0] Controls OPTIONAL }
  3208. *
  3209. * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
  3210. * COMPONENTS OF LDAPResult,
  3211. * responseName [10] LDAPOID OPTIONAL,
  3212. * responseValue [11] OCTET STRING OPTIONAL }
  3213. *
  3214. * LDAPResult ::= SEQUENCE {
  3215. * resultCode ENUMERATED {
  3216. * success (0),
  3217. * ...
  3218. * other (80),
  3219. * ... },
  3220. * matchedDN LDAPDN,
  3221. * diagnosticMessage LDAPString,
  3222. * referral [3] Referral OPTIONAL }
  3223. */
  3224. /* pull SEQUENCE */
  3225. inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
  3226. if (inf != V_ASN1_CONSTRUCTED || tag != V_ASN1_SEQUENCE ||
  3227. (rem = end - cur, len > rem)) {
  3228. BIO_printf(bio_err, "Unexpected LDAP response\n");
  3229. goto end;
  3230. }
  3231. rem = len; /* ensure that we don't overstep the SEQUENCE */
  3232. /* pull MessageID */
  3233. inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
  3234. if (inf != V_ASN1_UNIVERSAL || tag != V_ASN1_INTEGER ||
  3235. (rem = end - cur, len > rem)) {
  3236. BIO_printf(bio_err, "No MessageID\n");
  3237. goto end;
  3238. }
  3239. cur += len; /* shall we check for MessageId match or just skip? */
  3240. /* pull [APPLICATION 24] */
  3241. rem = end - cur;
  3242. inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
  3243. if (inf != V_ASN1_CONSTRUCTED || xclass != V_ASN1_APPLICATION ||
  3244. tag != 24) {
  3245. BIO_printf(bio_err, "Not ExtendedResponse\n");
  3246. goto end;
  3247. }
  3248. /* pull resultCode */
  3249. rem = end - cur;
  3250. inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
  3251. if (inf != V_ASN1_UNIVERSAL || tag != V_ASN1_ENUMERATED || len == 0 ||
  3252. (rem = end - cur, len > rem)) {
  3253. BIO_printf(bio_err, "Not LDAPResult\n");
  3254. goto end;
  3255. }
  3256. /* len should always be one, but just in case... */
  3257. for (ret = 0, inf = 0; inf < len; inf++) {
  3258. ret <<= 8;
  3259. ret |= cur[inf];
  3260. }
  3261. /* There is more data, but we don't care... */
  3262. end:
  3263. return ret;
  3264. }
  3265. /*
  3266. * Host dNS Name verifier: used for checking that the hostname is in dNS format
  3267. * before setting it as SNI
  3268. */
  3269. static int is_dNS_name(const char *host)
  3270. {
  3271. const size_t MAX_LABEL_LENGTH = 63;
  3272. size_t i;
  3273. int isdnsname = 0;
  3274. size_t length = strlen(host);
  3275. size_t label_length = 0;
  3276. int all_numeric = 1;
  3277. /*
  3278. * Deviation from strict DNS name syntax, also check names with '_'
  3279. * Check DNS name syntax, any '-' or '.' must be internal,
  3280. * and on either side of each '.' we can't have a '-' or '.'.
  3281. *
  3282. * If the name has just one label, we don't consider it a DNS name.
  3283. */
  3284. for (i = 0; i < length && label_length < MAX_LABEL_LENGTH; ++i) {
  3285. char c = host[i];
  3286. if ((c >= 'a' && c <= 'z')
  3287. || (c >= 'A' && c <= 'Z')
  3288. || c == '_') {
  3289. label_length += 1;
  3290. all_numeric = 0;
  3291. continue;
  3292. }
  3293. if (c >= '0' && c <= '9') {
  3294. label_length += 1;
  3295. continue;
  3296. }
  3297. /* Dot and hyphen cannot be first or last. */
  3298. if (i > 0 && i < length - 1) {
  3299. if (c == '-') {
  3300. label_length += 1;
  3301. continue;
  3302. }
  3303. /*
  3304. * Next to a dot the preceding and following characters must not be
  3305. * another dot or a hyphen. Otherwise, record that the name is
  3306. * plausible, since it has two or more labels.
  3307. */
  3308. if (c == '.'
  3309. && host[i + 1] != '.'
  3310. && host[i - 1] != '-'
  3311. && host[i + 1] != '-') {
  3312. label_length = 0;
  3313. isdnsname = 1;
  3314. continue;
  3315. }
  3316. }
  3317. isdnsname = 0;
  3318. break;
  3319. }
  3320. /* dNS name must not be all numeric and labels must be shorter than 64 characters. */
  3321. isdnsname &= !all_numeric && !(label_length == MAX_LABEL_LENGTH);
  3322. return isdnsname;
  3323. }
  3324. #endif /* OPENSSL_NO_SOCK */