2
0

vtls.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* This file is for implementing all "generic" SSL functions that all libcurl
  25. internals should use. It is then responsible for calling the proper
  26. "backend" function.
  27. SSL-functions in libcurl should call functions in this source file, and not
  28. to any specific SSL-layer.
  29. Curl_ssl_ - prefix for generic ones
  30. Note that this source code uses the functions of the configured SSL
  31. backend via the global Curl_ssl instance.
  32. "SSL/TLS Strong Encryption: An Introduction"
  33. https://httpd.apache.org/docs/2.0/ssl/ssl_intro.html
  34. */
  35. #include "curl_setup.h"
  36. #ifdef HAVE_SYS_TYPES_H
  37. #include <sys/types.h>
  38. #endif
  39. #ifdef HAVE_SYS_STAT_H
  40. #include <sys/stat.h>
  41. #endif
  42. #ifdef HAVE_FCNTL_H
  43. #include <fcntl.h>
  44. #endif
  45. #include "urldata.h"
  46. #include "cfilters.h"
  47. #include "vtls.h" /* generic SSL protos etc */
  48. #include "vtls_int.h"
  49. #include "slist.h"
  50. #include "sendf.h"
  51. #include "strcase.h"
  52. #include "url.h"
  53. #include "progress.h"
  54. #include "share.h"
  55. #include "multiif.h"
  56. #include "timeval.h"
  57. #include "curl_md5.h"
  58. #include "warnless.h"
  59. #include "curl_base64.h"
  60. #include "curl_printf.h"
  61. #include "inet_pton.h"
  62. #include "connect.h"
  63. #include "select.h"
  64. #include "strdup.h"
  65. /* The last #include files should be: */
  66. #include "curl_memory.h"
  67. #include "memdebug.h"
  68. /* convenience macro to check if this handle is using a shared SSL session */
  69. #define SSLSESSION_SHARED(data) (data->share && \
  70. (data->share->specifier & \
  71. (1<<CURL_LOCK_DATA_SSL_SESSION)))
  72. #define CLONE_STRING(var) \
  73. do { \
  74. if(source->var) { \
  75. dest->var = strdup(source->var); \
  76. if(!dest->var) \
  77. return FALSE; \
  78. } \
  79. else \
  80. dest->var = NULL; \
  81. } while(0)
  82. #define CLONE_BLOB(var) \
  83. do { \
  84. if(blobdup(&dest->var, source->var)) \
  85. return FALSE; \
  86. } while(0)
  87. static CURLcode blobdup(struct curl_blob **dest,
  88. struct curl_blob *src)
  89. {
  90. DEBUGASSERT(dest);
  91. DEBUGASSERT(!*dest);
  92. if(src) {
  93. /* only if there is data to dupe! */
  94. struct curl_blob *d;
  95. d = malloc(sizeof(struct curl_blob) + src->len);
  96. if(!d)
  97. return CURLE_OUT_OF_MEMORY;
  98. d->len = src->len;
  99. /* Always duplicate because the connection may survive longer than the
  100. handle that passed in the blob. */
  101. d->flags = CURL_BLOB_COPY;
  102. d->data = (void *)((char *)d + sizeof(struct curl_blob));
  103. memcpy(d->data, src->data, src->len);
  104. *dest = d;
  105. }
  106. return CURLE_OK;
  107. }
  108. /* returns TRUE if the blobs are identical */
  109. static bool blobcmp(struct curl_blob *first, struct curl_blob *second)
  110. {
  111. if(!first && !second) /* both are NULL */
  112. return TRUE;
  113. if(!first || !second) /* one is NULL */
  114. return FALSE;
  115. if(first->len != second->len) /* different sizes */
  116. return FALSE;
  117. return !memcmp(first->data, second->data, first->len); /* same data */
  118. }
  119. #ifdef USE_SSL
  120. static const struct alpn_spec ALPN_SPEC_H11 = {
  121. { ALPN_HTTP_1_1 }, 1
  122. };
  123. #ifdef USE_HTTP2
  124. static const struct alpn_spec ALPN_SPEC_H2_H11 = {
  125. { ALPN_H2, ALPN_HTTP_1_1 }, 2
  126. };
  127. #endif
  128. static const struct alpn_spec *alpn_get_spec(int httpwant, bool use_alpn)
  129. {
  130. if(!use_alpn)
  131. return NULL;
  132. #ifdef USE_HTTP2
  133. if(httpwant >= CURL_HTTP_VERSION_2)
  134. return &ALPN_SPEC_H2_H11;
  135. #else
  136. (void)httpwant;
  137. #endif
  138. /* Use the ALPN protocol "http/1.1" for HTTP/1.x.
  139. Avoid "http/1.0" because some servers do not support it. */
  140. return &ALPN_SPEC_H11;
  141. }
  142. #endif /* USE_SSL */
  143. void Curl_ssl_easy_config_init(struct Curl_easy *data)
  144. {
  145. /*
  146. * libcurl 7.10 introduced SSL verification *by default*! This needs to be
  147. * switched off unless wanted.
  148. */
  149. data->set.ssl.primary.verifypeer = TRUE;
  150. data->set.ssl.primary.verifyhost = TRUE;
  151. data->set.ssl.primary.cache_session = TRUE; /* caching by default */
  152. #ifndef CURL_DISABLE_PROXY
  153. data->set.proxy_ssl = data->set.ssl;
  154. #endif
  155. }
  156. static bool
  157. match_ssl_primary_config(struct Curl_easy *data,
  158. struct ssl_primary_config *c1,
  159. struct ssl_primary_config *c2)
  160. {
  161. (void)data;
  162. if((c1->version == c2->version) &&
  163. (c1->version_max == c2->version_max) &&
  164. (c1->ssl_options == c2->ssl_options) &&
  165. (c1->verifypeer == c2->verifypeer) &&
  166. (c1->verifyhost == c2->verifyhost) &&
  167. (c1->verifystatus == c2->verifystatus) &&
  168. blobcmp(c1->cert_blob, c2->cert_blob) &&
  169. blobcmp(c1->ca_info_blob, c2->ca_info_blob) &&
  170. blobcmp(c1->issuercert_blob, c2->issuercert_blob) &&
  171. Curl_safecmp(c1->CApath, c2->CApath) &&
  172. Curl_safecmp(c1->CAfile, c2->CAfile) &&
  173. Curl_safecmp(c1->issuercert, c2->issuercert) &&
  174. Curl_safecmp(c1->clientcert, c2->clientcert) &&
  175. #ifdef USE_TLS_SRP
  176. !Curl_timestrcmp(c1->username, c2->username) &&
  177. !Curl_timestrcmp(c1->password, c2->password) &&
  178. #endif
  179. strcasecompare(c1->cipher_list, c2->cipher_list) &&
  180. strcasecompare(c1->cipher_list13, c2->cipher_list13) &&
  181. strcasecompare(c1->curves, c2->curves) &&
  182. strcasecompare(c1->CRLfile, c2->CRLfile) &&
  183. strcasecompare(c1->pinned_key, c2->pinned_key))
  184. return TRUE;
  185. return FALSE;
  186. }
  187. bool Curl_ssl_conn_config_match(struct Curl_easy *data,
  188. struct connectdata *candidate,
  189. bool proxy)
  190. {
  191. #ifndef CURL_DISABLE_PROXY
  192. if(proxy)
  193. return match_ssl_primary_config(data, &data->set.proxy_ssl.primary,
  194. &candidate->proxy_ssl_config);
  195. #else
  196. (void)proxy;
  197. #endif
  198. return match_ssl_primary_config(data, &data->set.ssl.primary,
  199. &candidate->ssl_config);
  200. }
  201. static bool clone_ssl_primary_config(struct ssl_primary_config *source,
  202. struct ssl_primary_config *dest)
  203. {
  204. dest->version = source->version;
  205. dest->version_max = source->version_max;
  206. dest->verifypeer = source->verifypeer;
  207. dest->verifyhost = source->verifyhost;
  208. dest->verifystatus = source->verifystatus;
  209. dest->cache_session = source->cache_session;
  210. dest->ssl_options = source->ssl_options;
  211. CLONE_BLOB(cert_blob);
  212. CLONE_BLOB(ca_info_blob);
  213. CLONE_BLOB(issuercert_blob);
  214. CLONE_STRING(CApath);
  215. CLONE_STRING(CAfile);
  216. CLONE_STRING(issuercert);
  217. CLONE_STRING(clientcert);
  218. CLONE_STRING(cipher_list);
  219. CLONE_STRING(cipher_list13);
  220. CLONE_STRING(pinned_key);
  221. CLONE_STRING(curves);
  222. CLONE_STRING(CRLfile);
  223. #ifdef USE_TLS_SRP
  224. CLONE_STRING(username);
  225. CLONE_STRING(password);
  226. #endif
  227. return TRUE;
  228. }
  229. static void Curl_free_primary_ssl_config(struct ssl_primary_config *sslc)
  230. {
  231. Curl_safefree(sslc->CApath);
  232. Curl_safefree(sslc->CAfile);
  233. Curl_safefree(sslc->issuercert);
  234. Curl_safefree(sslc->clientcert);
  235. Curl_safefree(sslc->cipher_list);
  236. Curl_safefree(sslc->cipher_list13);
  237. Curl_safefree(sslc->pinned_key);
  238. Curl_safefree(sslc->cert_blob);
  239. Curl_safefree(sslc->ca_info_blob);
  240. Curl_safefree(sslc->issuercert_blob);
  241. Curl_safefree(sslc->curves);
  242. Curl_safefree(sslc->CRLfile);
  243. #ifdef USE_TLS_SRP
  244. Curl_safefree(sslc->username);
  245. Curl_safefree(sslc->password);
  246. #endif
  247. }
  248. CURLcode Curl_ssl_easy_config_complete(struct Curl_easy *data)
  249. {
  250. data->set.ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH];
  251. data->set.ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE];
  252. data->set.ssl.primary.CRLfile = data->set.str[STRING_SSL_CRLFILE];
  253. data->set.ssl.primary.issuercert = data->set.str[STRING_SSL_ISSUERCERT];
  254. data->set.ssl.primary.issuercert_blob = data->set.blobs[BLOB_SSL_ISSUERCERT];
  255. data->set.ssl.primary.cipher_list =
  256. data->set.str[STRING_SSL_CIPHER_LIST];
  257. data->set.ssl.primary.cipher_list13 =
  258. data->set.str[STRING_SSL_CIPHER13_LIST];
  259. data->set.ssl.primary.pinned_key =
  260. data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  261. data->set.ssl.primary.cert_blob = data->set.blobs[BLOB_CERT];
  262. data->set.ssl.primary.ca_info_blob = data->set.blobs[BLOB_CAINFO];
  263. data->set.ssl.primary.curves = data->set.str[STRING_SSL_EC_CURVES];
  264. #ifdef USE_TLS_SRP
  265. data->set.ssl.primary.username = data->set.str[STRING_TLSAUTH_USERNAME];
  266. data->set.ssl.primary.password = data->set.str[STRING_TLSAUTH_PASSWORD];
  267. #endif
  268. data->set.ssl.cert_type = data->set.str[STRING_CERT_TYPE];
  269. data->set.ssl.key = data->set.str[STRING_KEY];
  270. data->set.ssl.key_type = data->set.str[STRING_KEY_TYPE];
  271. data->set.ssl.key_passwd = data->set.str[STRING_KEY_PASSWD];
  272. data->set.ssl.primary.clientcert = data->set.str[STRING_CERT];
  273. data->set.ssl.key_blob = data->set.blobs[BLOB_KEY];
  274. #ifndef CURL_DISABLE_PROXY
  275. data->set.proxy_ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_PROXY];
  276. data->set.proxy_ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_PROXY];
  277. data->set.proxy_ssl.primary.cipher_list =
  278. data->set.str[STRING_SSL_CIPHER_LIST_PROXY];
  279. data->set.proxy_ssl.primary.cipher_list13 =
  280. data->set.str[STRING_SSL_CIPHER13_LIST_PROXY];
  281. data->set.proxy_ssl.primary.pinned_key =
  282. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY];
  283. data->set.proxy_ssl.primary.cert_blob = data->set.blobs[BLOB_CERT_PROXY];
  284. data->set.proxy_ssl.primary.ca_info_blob =
  285. data->set.blobs[BLOB_CAINFO_PROXY];
  286. data->set.proxy_ssl.primary.issuercert =
  287. data->set.str[STRING_SSL_ISSUERCERT_PROXY];
  288. data->set.proxy_ssl.primary.issuercert_blob =
  289. data->set.blobs[BLOB_SSL_ISSUERCERT_PROXY];
  290. data->set.proxy_ssl.primary.CRLfile =
  291. data->set.str[STRING_SSL_CRLFILE_PROXY];
  292. data->set.proxy_ssl.cert_type = data->set.str[STRING_CERT_TYPE_PROXY];
  293. data->set.proxy_ssl.key = data->set.str[STRING_KEY_PROXY];
  294. data->set.proxy_ssl.key_type = data->set.str[STRING_KEY_TYPE_PROXY];
  295. data->set.proxy_ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_PROXY];
  296. data->set.proxy_ssl.primary.clientcert = data->set.str[STRING_CERT_PROXY];
  297. data->set.proxy_ssl.key_blob = data->set.blobs[BLOB_KEY_PROXY];
  298. #ifdef USE_TLS_SRP
  299. data->set.proxy_ssl.primary.username =
  300. data->set.str[STRING_TLSAUTH_USERNAME_PROXY];
  301. data->set.proxy_ssl.primary.password =
  302. data->set.str[STRING_TLSAUTH_PASSWORD_PROXY];
  303. #endif
  304. #endif /* CURL_DISABLE_PROXY */
  305. return CURLE_OK;
  306. }
  307. CURLcode Curl_ssl_conn_config_init(struct Curl_easy *data,
  308. struct connectdata *conn)
  309. {
  310. /* Clone "primary" SSL configurations from the esay handle to
  311. * the connection. They are used for connection cache matching and
  312. * probably outlive the easy handle */
  313. if(!clone_ssl_primary_config(&data->set.ssl.primary, &conn->ssl_config))
  314. return CURLE_OUT_OF_MEMORY;
  315. #ifndef CURL_DISABLE_PROXY
  316. if(!clone_ssl_primary_config(&data->set.proxy_ssl.primary,
  317. &conn->proxy_ssl_config))
  318. return CURLE_OUT_OF_MEMORY;
  319. #endif
  320. return CURLE_OK;
  321. }
  322. void Curl_ssl_conn_config_cleanup(struct connectdata *conn)
  323. {
  324. Curl_free_primary_ssl_config(&conn->ssl_config);
  325. #ifndef CURL_DISABLE_PROXY
  326. Curl_free_primary_ssl_config(&conn->proxy_ssl_config);
  327. #endif
  328. }
  329. void Curl_ssl_conn_config_update(struct Curl_easy *data, bool for_proxy)
  330. {
  331. /* May be called on an easy that has no connection yet */
  332. if(data->conn) {
  333. struct ssl_primary_config *src, *dest;
  334. #ifndef CURL_DISABLE_PROXY
  335. src = for_proxy? &data->set.proxy_ssl.primary : &data->set.ssl.primary;
  336. dest = for_proxy? &data->conn->proxy_ssl_config : &data->conn->ssl_config;
  337. #else
  338. (void)for_proxy;
  339. src = &data->set.ssl.primary;
  340. dest = &data->conn->ssl_config;
  341. #endif
  342. dest->verifyhost = src->verifyhost;
  343. dest->verifypeer = src->verifypeer;
  344. dest->verifystatus = src->verifystatus;
  345. }
  346. }
  347. #ifdef USE_SSL
  348. static int multissl_setup(const struct Curl_ssl *backend);
  349. #endif
  350. curl_sslbackend Curl_ssl_backend(void)
  351. {
  352. #ifdef USE_SSL
  353. multissl_setup(NULL);
  354. return Curl_ssl->info.id;
  355. #else
  356. return CURLSSLBACKEND_NONE;
  357. #endif
  358. }
  359. #ifdef USE_SSL
  360. /* "global" init done? */
  361. static bool init_ssl = FALSE;
  362. /**
  363. * Global SSL init
  364. *
  365. * @retval 0 error initializing SSL
  366. * @retval 1 SSL initialized successfully
  367. */
  368. int Curl_ssl_init(void)
  369. {
  370. /* make sure this is only done once */
  371. if(init_ssl)
  372. return 1;
  373. init_ssl = TRUE; /* never again */
  374. return Curl_ssl->init();
  375. }
  376. static bool ssl_prefs_check(struct Curl_easy *data)
  377. {
  378. /* check for CURLOPT_SSLVERSION invalid parameter value */
  379. const unsigned char sslver = data->set.ssl.primary.version;
  380. if(sslver >= CURL_SSLVERSION_LAST) {
  381. failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION");
  382. return FALSE;
  383. }
  384. switch(data->set.ssl.primary.version_max) {
  385. case CURL_SSLVERSION_MAX_NONE:
  386. case CURL_SSLVERSION_MAX_DEFAULT:
  387. break;
  388. default:
  389. if((data->set.ssl.primary.version_max >> 16) < sslver) {
  390. failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION");
  391. return FALSE;
  392. }
  393. }
  394. return TRUE;
  395. }
  396. static struct ssl_connect_data *cf_ctx_new(struct Curl_easy *data,
  397. const struct alpn_spec *alpn)
  398. {
  399. struct ssl_connect_data *ctx;
  400. (void)data;
  401. ctx = calloc(1, sizeof(*ctx));
  402. if(!ctx)
  403. return NULL;
  404. ctx->alpn = alpn;
  405. ctx->backend = calloc(1, Curl_ssl->sizeof_ssl_backend_data);
  406. if(!ctx->backend) {
  407. free(ctx);
  408. return NULL;
  409. }
  410. return ctx;
  411. }
  412. static void cf_ctx_free(struct ssl_connect_data *ctx)
  413. {
  414. if(ctx) {
  415. free(ctx->backend);
  416. free(ctx);
  417. }
  418. }
  419. static CURLcode ssl_connect(struct Curl_cfilter *cf, struct Curl_easy *data)
  420. {
  421. struct ssl_connect_data *connssl = cf->ctx;
  422. CURLcode result;
  423. if(!ssl_prefs_check(data))
  424. return CURLE_SSL_CONNECT_ERROR;
  425. /* mark this is being ssl-enabled from here on. */
  426. connssl->state = ssl_connection_negotiating;
  427. result = Curl_ssl->connect_blocking(cf, data);
  428. if(!result) {
  429. DEBUGASSERT(connssl->state == ssl_connection_complete);
  430. }
  431. return result;
  432. }
  433. static CURLcode
  434. ssl_connect_nonblocking(struct Curl_cfilter *cf, struct Curl_easy *data,
  435. bool *done)
  436. {
  437. if(!ssl_prefs_check(data))
  438. return CURLE_SSL_CONNECT_ERROR;
  439. /* mark this is being ssl requested from here on. */
  440. return Curl_ssl->connect_nonblocking(cf, data, done);
  441. }
  442. /*
  443. * Lock shared SSL session data
  444. */
  445. void Curl_ssl_sessionid_lock(struct Curl_easy *data)
  446. {
  447. if(SSLSESSION_SHARED(data))
  448. Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
  449. }
  450. /*
  451. * Unlock shared SSL session data
  452. */
  453. void Curl_ssl_sessionid_unlock(struct Curl_easy *data)
  454. {
  455. if(SSLSESSION_SHARED(data))
  456. Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
  457. }
  458. /*
  459. * Check if there is a session ID for the given connection in the cache, and if
  460. * there is one suitable, it is provided. Returns TRUE when no entry matched.
  461. */
  462. bool Curl_ssl_getsessionid(struct Curl_cfilter *cf,
  463. struct Curl_easy *data,
  464. const struct ssl_peer *peer,
  465. void **ssl_sessionid,
  466. size_t *idsize) /* set 0 if unknown */
  467. {
  468. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  469. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  470. struct Curl_ssl_session *check;
  471. size_t i;
  472. long *general_age;
  473. bool no_match = TRUE;
  474. *ssl_sessionid = NULL;
  475. if(!ssl_config)
  476. return TRUE;
  477. DEBUGASSERT(ssl_config->primary.cache_session);
  478. if(!ssl_config->primary.cache_session || !data->state.session)
  479. /* session ID reuse is disabled or the session cache has not been
  480. setup */
  481. return TRUE;
  482. /* Lock if shared */
  483. if(SSLSESSION_SHARED(data))
  484. general_age = &data->share->sessionage;
  485. else
  486. general_age = &data->state.sessionage;
  487. for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) {
  488. check = &data->state.session[i];
  489. if(!check->sessionid)
  490. /* not session ID means blank entry */
  491. continue;
  492. if(strcasecompare(peer->hostname, check->name) &&
  493. ((!cf->conn->bits.conn_to_host && !check->conn_to_host) ||
  494. (cf->conn->bits.conn_to_host && check->conn_to_host &&
  495. strcasecompare(cf->conn->conn_to_host.name, check->conn_to_host))) &&
  496. ((!cf->conn->bits.conn_to_port && check->conn_to_port == -1) ||
  497. (cf->conn->bits.conn_to_port && check->conn_to_port != -1 &&
  498. cf->conn->conn_to_port == check->conn_to_port)) &&
  499. (peer->port == check->remote_port) &&
  500. (peer->transport == check->transport) &&
  501. strcasecompare(cf->conn->handler->scheme, check->scheme) &&
  502. match_ssl_primary_config(data, conn_config, &check->ssl_config)) {
  503. /* yes, we have a session ID! */
  504. (*general_age)++; /* increase general age */
  505. check->age = *general_age; /* set this as used in this age */
  506. *ssl_sessionid = check->sessionid;
  507. if(idsize)
  508. *idsize = check->idsize;
  509. no_match = FALSE;
  510. break;
  511. }
  512. }
  513. CURL_TRC_CF(data, cf, "%s cached session ID for %s://%s:%d",
  514. no_match? "No": "Found",
  515. cf->conn->handler->scheme, peer->hostname, peer->port);
  516. return no_match;
  517. }
  518. /*
  519. * Kill a single session ID entry in the cache.
  520. */
  521. void Curl_ssl_kill_session(struct Curl_ssl_session *session)
  522. {
  523. if(session->sessionid) {
  524. /* defensive check */
  525. /* free the ID the SSL-layer specific way */
  526. session->sessionid_free(session->sessionid, session->idsize);
  527. session->sessionid = NULL;
  528. session->sessionid_free = NULL;
  529. session->age = 0; /* fresh */
  530. Curl_free_primary_ssl_config(&session->ssl_config);
  531. Curl_safefree(session->name);
  532. Curl_safefree(session->conn_to_host);
  533. }
  534. }
  535. /*
  536. * Delete the given session ID from the cache.
  537. */
  538. void Curl_ssl_delsessionid(struct Curl_easy *data, void *ssl_sessionid)
  539. {
  540. size_t i;
  541. for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) {
  542. struct Curl_ssl_session *check = &data->state.session[i];
  543. if(check->sessionid == ssl_sessionid) {
  544. Curl_ssl_kill_session(check);
  545. break;
  546. }
  547. }
  548. }
  549. CURLcode Curl_ssl_set_sessionid(struct Curl_cfilter *cf,
  550. struct Curl_easy *data,
  551. const struct ssl_peer *peer,
  552. void *ssl_sessionid,
  553. size_t idsize,
  554. Curl_ssl_sessionid_dtor *sessionid_free_cb)
  555. {
  556. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  557. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  558. size_t i;
  559. struct Curl_ssl_session *store;
  560. long oldest_age;
  561. char *clone_host = NULL;
  562. char *clone_conn_to_host = NULL;
  563. int conn_to_port;
  564. long *general_age;
  565. void *old_sessionid;
  566. size_t old_size;
  567. CURLcode result = CURLE_OUT_OF_MEMORY;
  568. DEBUGASSERT(ssl_sessionid);
  569. DEBUGASSERT(sessionid_free_cb);
  570. if(!data->state.session) {
  571. sessionid_free_cb(ssl_sessionid, idsize);
  572. return CURLE_OK;
  573. }
  574. if(!Curl_ssl_getsessionid(cf, data, peer, &old_sessionid, &old_size)) {
  575. if((old_size == idsize) &&
  576. ((old_sessionid == ssl_sessionid) ||
  577. (idsize && !memcmp(old_sessionid, ssl_sessionid, idsize)))) {
  578. /* the very same */
  579. sessionid_free_cb(ssl_sessionid, idsize);
  580. return CURLE_OK;
  581. }
  582. Curl_ssl_delsessionid(data, old_sessionid);
  583. }
  584. store = &data->state.session[0];
  585. oldest_age = data->state.session[0].age; /* zero if unused */
  586. DEBUGASSERT(ssl_config->primary.cache_session);
  587. (void)ssl_config;
  588. clone_host = strdup(peer->hostname);
  589. if(!clone_host)
  590. goto out;
  591. if(cf->conn->bits.conn_to_host) {
  592. clone_conn_to_host = strdup(cf->conn->conn_to_host.name);
  593. if(!clone_conn_to_host)
  594. goto out;
  595. }
  596. if(cf->conn->bits.conn_to_port)
  597. conn_to_port = cf->conn->conn_to_port;
  598. else
  599. conn_to_port = -1;
  600. /* Now we should add the session ID and the hostname to the cache, (remove
  601. the oldest if necessary) */
  602. /* If using shared SSL session, lock! */
  603. if(SSLSESSION_SHARED(data)) {
  604. general_age = &data->share->sessionage;
  605. }
  606. else {
  607. general_age = &data->state.sessionage;
  608. }
  609. /* find an empty slot for us, or find the oldest */
  610. for(i = 1; (i < data->set.general_ssl.max_ssl_sessions) &&
  611. data->state.session[i].sessionid; i++) {
  612. if(data->state.session[i].age < oldest_age) {
  613. oldest_age = data->state.session[i].age;
  614. store = &data->state.session[i];
  615. }
  616. }
  617. if(i == data->set.general_ssl.max_ssl_sessions)
  618. /* cache is full, we must "kill" the oldest entry! */
  619. Curl_ssl_kill_session(store);
  620. else
  621. store = &data->state.session[i]; /* use this slot */
  622. /* now init the session struct wisely */
  623. if(!clone_ssl_primary_config(conn_config, &store->ssl_config)) {
  624. Curl_free_primary_ssl_config(&store->ssl_config);
  625. store->sessionid = NULL; /* let caller free sessionid */
  626. goto out;
  627. }
  628. store->sessionid = ssl_sessionid;
  629. store->idsize = idsize;
  630. store->sessionid_free = sessionid_free_cb;
  631. store->age = *general_age; /* set current age */
  632. /* free it if there is one already present */
  633. free(store->name);
  634. free(store->conn_to_host);
  635. store->name = clone_host; /* clone hostname */
  636. clone_host = NULL;
  637. store->conn_to_host = clone_conn_to_host; /* clone connect to hostname */
  638. clone_conn_to_host = NULL;
  639. store->conn_to_port = conn_to_port; /* connect to port number */
  640. /* port number */
  641. store->remote_port = peer->port;
  642. store->scheme = cf->conn->handler->scheme;
  643. store->transport = peer->transport;
  644. result = CURLE_OK;
  645. out:
  646. free(clone_host);
  647. free(clone_conn_to_host);
  648. if(result) {
  649. failf(data, "Failed to add Session ID to cache for %s://%s:%d [%s]",
  650. store->scheme, store->name, store->remote_port,
  651. Curl_ssl_cf_is_proxy(cf) ? "PROXY" : "server");
  652. sessionid_free_cb(ssl_sessionid, idsize);
  653. return result;
  654. }
  655. CURL_TRC_CF(data, cf, "Added Session ID to cache for %s://%s:%d [%s]",
  656. store->scheme, store->name, store->remote_port,
  657. Curl_ssl_cf_is_proxy(cf) ? "PROXY" : "server");
  658. return CURLE_OK;
  659. }
  660. void Curl_ssl_close_all(struct Curl_easy *data)
  661. {
  662. /* kill the session ID cache if not shared */
  663. if(data->state.session && !SSLSESSION_SHARED(data)) {
  664. size_t i;
  665. for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++)
  666. /* the single-killer function handles empty table slots */
  667. Curl_ssl_kill_session(&data->state.session[i]);
  668. /* free the cache data */
  669. Curl_safefree(data->state.session);
  670. }
  671. Curl_ssl->close_all(data);
  672. }
  673. void Curl_ssl_adjust_pollset(struct Curl_cfilter *cf, struct Curl_easy *data,
  674. struct easy_pollset *ps)
  675. {
  676. struct ssl_connect_data *connssl = cf->ctx;
  677. if(connssl->io_need) {
  678. curl_socket_t sock = Curl_conn_cf_get_socket(cf->next, data);
  679. if(sock != CURL_SOCKET_BAD) {
  680. if(connssl->io_need & CURL_SSL_IO_NEED_SEND) {
  681. Curl_pollset_set_out_only(data, ps, sock);
  682. CURL_TRC_CF(data, cf, "adjust_pollset, POLLOUT fd=%"
  683. CURL_FORMAT_SOCKET_T, sock);
  684. }
  685. else {
  686. Curl_pollset_set_in_only(data, ps, sock);
  687. CURL_TRC_CF(data, cf, "adjust_pollset, POLLIN fd=%"
  688. CURL_FORMAT_SOCKET_T, sock);
  689. }
  690. }
  691. }
  692. }
  693. /* Selects an SSL crypto engine
  694. */
  695. CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine)
  696. {
  697. return Curl_ssl->set_engine(data, engine);
  698. }
  699. /* Selects the default SSL crypto engine
  700. */
  701. CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data)
  702. {
  703. return Curl_ssl->set_engine_default(data);
  704. }
  705. /* Return list of OpenSSL crypto engine names. */
  706. struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data)
  707. {
  708. return Curl_ssl->engines_list(data);
  709. }
  710. /*
  711. * This sets up a session ID cache to the specified size. Make sure this code
  712. * is agnostic to what underlying SSL technology we use.
  713. */
  714. CURLcode Curl_ssl_initsessions(struct Curl_easy *data, size_t amount)
  715. {
  716. struct Curl_ssl_session *session;
  717. if(data->state.session)
  718. /* this is just a precaution to prevent multiple inits */
  719. return CURLE_OK;
  720. session = calloc(amount, sizeof(struct Curl_ssl_session));
  721. if(!session)
  722. return CURLE_OUT_OF_MEMORY;
  723. /* store the info in the SSL section */
  724. data->set.general_ssl.max_ssl_sessions = amount;
  725. data->state.session = session;
  726. data->state.sessionage = 1; /* this is brand new */
  727. return CURLE_OK;
  728. }
  729. static size_t multissl_version(char *buffer, size_t size);
  730. void Curl_ssl_version(char *buffer, size_t size)
  731. {
  732. #ifdef CURL_WITH_MULTI_SSL
  733. (void)multissl_version(buffer, size);
  734. #else
  735. (void)Curl_ssl->version(buffer, size);
  736. #endif
  737. }
  738. void Curl_ssl_free_certinfo(struct Curl_easy *data)
  739. {
  740. struct curl_certinfo *ci = &data->info.certs;
  741. if(ci->num_of_certs) {
  742. /* free all individual lists used */
  743. int i;
  744. for(i = 0; i<ci->num_of_certs; i++) {
  745. curl_slist_free_all(ci->certinfo[i]);
  746. ci->certinfo[i] = NULL;
  747. }
  748. free(ci->certinfo); /* free the actual array too */
  749. ci->certinfo = NULL;
  750. ci->num_of_certs = 0;
  751. }
  752. }
  753. CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num)
  754. {
  755. struct curl_certinfo *ci = &data->info.certs;
  756. struct curl_slist **table;
  757. /* Free any previous certificate information structures */
  758. Curl_ssl_free_certinfo(data);
  759. /* Allocate the required certificate information structures */
  760. table = calloc((size_t) num, sizeof(struct curl_slist *));
  761. if(!table)
  762. return CURLE_OUT_OF_MEMORY;
  763. ci->num_of_certs = num;
  764. ci->certinfo = table;
  765. return CURLE_OK;
  766. }
  767. /*
  768. * 'value' is NOT a null-terminated string
  769. */
  770. CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data,
  771. int certnum,
  772. const char *label,
  773. const char *value,
  774. size_t valuelen)
  775. {
  776. struct curl_certinfo *ci = &data->info.certs;
  777. struct curl_slist *nl;
  778. CURLcode result = CURLE_OK;
  779. struct dynbuf build;
  780. Curl_dyn_init(&build, CURL_X509_STR_MAX);
  781. if(Curl_dyn_add(&build, label) ||
  782. Curl_dyn_addn(&build, ":", 1) ||
  783. Curl_dyn_addn(&build, value, valuelen))
  784. return CURLE_OUT_OF_MEMORY;
  785. nl = Curl_slist_append_nodup(ci->certinfo[certnum],
  786. Curl_dyn_ptr(&build));
  787. if(!nl) {
  788. Curl_dyn_free(&build);
  789. curl_slist_free_all(ci->certinfo[certnum]);
  790. result = CURLE_OUT_OF_MEMORY;
  791. }
  792. ci->certinfo[certnum] = nl;
  793. return result;
  794. }
  795. CURLcode Curl_ssl_random(struct Curl_easy *data,
  796. unsigned char *entropy,
  797. size_t length)
  798. {
  799. return Curl_ssl->random(data, entropy, length);
  800. }
  801. /*
  802. * Public key pem to der conversion
  803. */
  804. static CURLcode pubkey_pem_to_der(const char *pem,
  805. unsigned char **der, size_t *der_len)
  806. {
  807. char *stripped_pem, *begin_pos, *end_pos;
  808. size_t pem_count, stripped_pem_count = 0, pem_len;
  809. CURLcode result;
  810. /* if no pem, exit. */
  811. if(!pem)
  812. return CURLE_BAD_CONTENT_ENCODING;
  813. begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----");
  814. if(!begin_pos)
  815. return CURLE_BAD_CONTENT_ENCODING;
  816. pem_count = begin_pos - pem;
  817. /* Invalid if not at beginning AND not directly following \n */
  818. if(0 != pem_count && '\n' != pem[pem_count - 1])
  819. return CURLE_BAD_CONTENT_ENCODING;
  820. /* 26 is length of "-----BEGIN PUBLIC KEY-----" */
  821. pem_count += 26;
  822. /* Invalid if not directly following \n */
  823. end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----");
  824. if(!end_pos)
  825. return CURLE_BAD_CONTENT_ENCODING;
  826. pem_len = end_pos - pem;
  827. stripped_pem = malloc(pem_len - pem_count + 1);
  828. if(!stripped_pem)
  829. return CURLE_OUT_OF_MEMORY;
  830. /*
  831. * Here we loop through the pem array one character at a time between the
  832. * correct indices, and place each character that is not '\n' or '\r'
  833. * into the stripped_pem array, which should represent the raw base64 string
  834. */
  835. while(pem_count < pem_len) {
  836. if('\n' != pem[pem_count] && '\r' != pem[pem_count])
  837. stripped_pem[stripped_pem_count++] = pem[pem_count];
  838. ++pem_count;
  839. }
  840. /* Place the null terminator in the correct place */
  841. stripped_pem[stripped_pem_count] = '\0';
  842. result = Curl_base64_decode(stripped_pem, der, der_len);
  843. Curl_safefree(stripped_pem);
  844. return result;
  845. }
  846. /*
  847. * Generic pinned public key check.
  848. */
  849. CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data,
  850. const char *pinnedpubkey,
  851. const unsigned char *pubkey, size_t pubkeylen)
  852. {
  853. FILE *fp;
  854. unsigned char *buf = NULL, *pem_ptr = NULL;
  855. CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  856. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  857. (void)data;
  858. #endif
  859. /* if a path was not specified, do not pin */
  860. if(!pinnedpubkey)
  861. return CURLE_OK;
  862. if(!pubkey || !pubkeylen)
  863. return result;
  864. /* only do this if pinnedpubkey starts with "sha256//", length 8 */
  865. if(strncmp(pinnedpubkey, "sha256//", 8) == 0) {
  866. CURLcode encode;
  867. size_t encodedlen = 0;
  868. char *encoded = NULL, *pinkeycopy, *begin_pos, *end_pos;
  869. unsigned char *sha256sumdigest;
  870. if(!Curl_ssl->sha256sum) {
  871. /* without sha256 support, this cannot match */
  872. return result;
  873. }
  874. /* compute sha256sum of public key */
  875. sha256sumdigest = malloc(CURL_SHA256_DIGEST_LENGTH);
  876. if(!sha256sumdigest)
  877. return CURLE_OUT_OF_MEMORY;
  878. encode = Curl_ssl->sha256sum(pubkey, pubkeylen,
  879. sha256sumdigest, CURL_SHA256_DIGEST_LENGTH);
  880. if(!encode)
  881. encode = Curl_base64_encode((char *)sha256sumdigest,
  882. CURL_SHA256_DIGEST_LENGTH, &encoded,
  883. &encodedlen);
  884. Curl_safefree(sha256sumdigest);
  885. if(encode)
  886. return encode;
  887. infof(data, " public key hash: sha256//%s", encoded);
  888. /* it starts with sha256//, copy so we can modify it */
  889. pinkeycopy = strdup(pinnedpubkey);
  890. if(!pinkeycopy) {
  891. Curl_safefree(encoded);
  892. return CURLE_OUT_OF_MEMORY;
  893. }
  894. /* point begin_pos to the copy, and start extracting keys */
  895. begin_pos = pinkeycopy;
  896. do {
  897. end_pos = strstr(begin_pos, ";sha256//");
  898. /*
  899. * if there is an end_pos, null terminate,
  900. * otherwise it will go to the end of the original string
  901. */
  902. if(end_pos)
  903. end_pos[0] = '\0';
  904. /* compare base64 sha256 digests, 8 is the length of "sha256//" */
  905. if(encodedlen == strlen(begin_pos + 8) &&
  906. !memcmp(encoded, begin_pos + 8, encodedlen)) {
  907. result = CURLE_OK;
  908. break;
  909. }
  910. /*
  911. * change back the null-terminator we changed earlier,
  912. * and look for next begin
  913. */
  914. if(end_pos) {
  915. end_pos[0] = ';';
  916. begin_pos = strstr(end_pos, "sha256//");
  917. }
  918. } while(end_pos && begin_pos);
  919. Curl_safefree(encoded);
  920. Curl_safefree(pinkeycopy);
  921. return result;
  922. }
  923. fp = fopen(pinnedpubkey, "rb");
  924. if(!fp)
  925. return result;
  926. do {
  927. long filesize;
  928. size_t size, pem_len;
  929. CURLcode pem_read;
  930. /* Determine the file's size */
  931. if(fseek(fp, 0, SEEK_END))
  932. break;
  933. filesize = ftell(fp);
  934. if(fseek(fp, 0, SEEK_SET))
  935. break;
  936. if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE)
  937. break;
  938. /*
  939. * if the size of our certificate is bigger than the file
  940. * size then it cannot match
  941. */
  942. size = curlx_sotouz((curl_off_t) filesize);
  943. if(pubkeylen > size)
  944. break;
  945. /*
  946. * Allocate buffer for the pinned key
  947. * With 1 additional byte for null terminator in case of PEM key
  948. */
  949. buf = malloc(size + 1);
  950. if(!buf)
  951. break;
  952. /* Returns number of elements read, which should be 1 */
  953. if((int) fread(buf, size, 1, fp) != 1)
  954. break;
  955. /* If the sizes are the same, it cannot be base64 encoded, must be der */
  956. if(pubkeylen == size) {
  957. if(!memcmp(pubkey, buf, pubkeylen))
  958. result = CURLE_OK;
  959. break;
  960. }
  961. /*
  962. * Otherwise we will assume it is PEM and try to decode it
  963. * after placing null terminator
  964. */
  965. buf[size] = '\0';
  966. pem_read = pubkey_pem_to_der((const char *)buf, &pem_ptr, &pem_len);
  967. /* if it was not read successfully, exit */
  968. if(pem_read)
  969. break;
  970. /*
  971. * if the size of our certificate does not match the size of
  972. * the decoded file, they cannot be the same, otherwise compare
  973. */
  974. if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen))
  975. result = CURLE_OK;
  976. } while(0);
  977. Curl_safefree(buf);
  978. Curl_safefree(pem_ptr);
  979. fclose(fp);
  980. return result;
  981. }
  982. /*
  983. * Check whether the SSL backend supports the status_request extension.
  984. */
  985. bool Curl_ssl_cert_status_request(void)
  986. {
  987. return Curl_ssl->cert_status_request();
  988. }
  989. /*
  990. * Check whether the SSL backend supports false start.
  991. */
  992. bool Curl_ssl_false_start(struct Curl_easy *data)
  993. {
  994. (void)data;
  995. return Curl_ssl->false_start();
  996. }
  997. /*
  998. * Default implementations for unsupported functions.
  999. */
  1000. int Curl_none_init(void)
  1001. {
  1002. return 1;
  1003. }
  1004. void Curl_none_cleanup(void)
  1005. { }
  1006. CURLcode Curl_none_shutdown(struct Curl_cfilter *cf UNUSED_PARAM,
  1007. struct Curl_easy *data UNUSED_PARAM,
  1008. bool send_shutdown UNUSED_PARAM,
  1009. bool *done)
  1010. {
  1011. (void)data;
  1012. (void)cf;
  1013. (void)send_shutdown;
  1014. /* Every SSL backend should have a shutdown implementation. Until we
  1015. * have implemented that, we put this fake in place. */
  1016. *done = TRUE;
  1017. return CURLE_OK;
  1018. }
  1019. int Curl_none_check_cxn(struct Curl_cfilter *cf, struct Curl_easy *data)
  1020. {
  1021. (void)cf;
  1022. (void)data;
  1023. return -1;
  1024. }
  1025. CURLcode Curl_none_random(struct Curl_easy *data UNUSED_PARAM,
  1026. unsigned char *entropy UNUSED_PARAM,
  1027. size_t length UNUSED_PARAM)
  1028. {
  1029. (void)data;
  1030. (void)entropy;
  1031. (void)length;
  1032. return CURLE_NOT_BUILT_IN;
  1033. }
  1034. void Curl_none_close_all(struct Curl_easy *data UNUSED_PARAM)
  1035. {
  1036. (void)data;
  1037. }
  1038. void Curl_none_session_free(void *ptr UNUSED_PARAM)
  1039. {
  1040. (void)ptr;
  1041. }
  1042. bool Curl_none_data_pending(struct Curl_cfilter *cf UNUSED_PARAM,
  1043. const struct Curl_easy *data UNUSED_PARAM)
  1044. {
  1045. (void)cf;
  1046. (void)data;
  1047. return 0;
  1048. }
  1049. bool Curl_none_cert_status_request(void)
  1050. {
  1051. return FALSE;
  1052. }
  1053. CURLcode Curl_none_set_engine(struct Curl_easy *data UNUSED_PARAM,
  1054. const char *engine UNUSED_PARAM)
  1055. {
  1056. (void)data;
  1057. (void)engine;
  1058. return CURLE_NOT_BUILT_IN;
  1059. }
  1060. CURLcode Curl_none_set_engine_default(struct Curl_easy *data UNUSED_PARAM)
  1061. {
  1062. (void)data;
  1063. return CURLE_NOT_BUILT_IN;
  1064. }
  1065. struct curl_slist *Curl_none_engines_list(struct Curl_easy *data UNUSED_PARAM)
  1066. {
  1067. (void)data;
  1068. return (struct curl_slist *)NULL;
  1069. }
  1070. bool Curl_none_false_start(void)
  1071. {
  1072. return FALSE;
  1073. }
  1074. static int multissl_init(void)
  1075. {
  1076. if(multissl_setup(NULL))
  1077. return 1;
  1078. return Curl_ssl->init();
  1079. }
  1080. static CURLcode multissl_connect(struct Curl_cfilter *cf,
  1081. struct Curl_easy *data)
  1082. {
  1083. if(multissl_setup(NULL))
  1084. return CURLE_FAILED_INIT;
  1085. return Curl_ssl->connect_blocking(cf, data);
  1086. }
  1087. static CURLcode multissl_connect_nonblocking(struct Curl_cfilter *cf,
  1088. struct Curl_easy *data,
  1089. bool *done)
  1090. {
  1091. if(multissl_setup(NULL))
  1092. return CURLE_FAILED_INIT;
  1093. return Curl_ssl->connect_nonblocking(cf, data, done);
  1094. }
  1095. static void multissl_adjust_pollset(struct Curl_cfilter *cf,
  1096. struct Curl_easy *data,
  1097. struct easy_pollset *ps)
  1098. {
  1099. if(multissl_setup(NULL))
  1100. return;
  1101. Curl_ssl->adjust_pollset(cf, data, ps);
  1102. }
  1103. static void *multissl_get_internals(struct ssl_connect_data *connssl,
  1104. CURLINFO info)
  1105. {
  1106. if(multissl_setup(NULL))
  1107. return NULL;
  1108. return Curl_ssl->get_internals(connssl, info);
  1109. }
  1110. static void multissl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1111. {
  1112. if(multissl_setup(NULL))
  1113. return;
  1114. Curl_ssl->close(cf, data);
  1115. }
  1116. static ssize_t multissl_recv_plain(struct Curl_cfilter *cf,
  1117. struct Curl_easy *data,
  1118. char *buf, size_t len, CURLcode *code)
  1119. {
  1120. if(multissl_setup(NULL))
  1121. return CURLE_FAILED_INIT;
  1122. return Curl_ssl->recv_plain(cf, data, buf, len, code);
  1123. }
  1124. static ssize_t multissl_send_plain(struct Curl_cfilter *cf,
  1125. struct Curl_easy *data,
  1126. const void *mem, size_t len,
  1127. CURLcode *code)
  1128. {
  1129. if(multissl_setup(NULL))
  1130. return CURLE_FAILED_INIT;
  1131. return Curl_ssl->send_plain(cf, data, mem, len, code);
  1132. }
  1133. static const struct Curl_ssl Curl_ssl_multi = {
  1134. { CURLSSLBACKEND_NONE, "multi" }, /* info */
  1135. 0, /* supports nothing */
  1136. (size_t)-1, /* something insanely large to be on the safe side */
  1137. multissl_init, /* init */
  1138. Curl_none_cleanup, /* cleanup */
  1139. multissl_version, /* version */
  1140. Curl_none_check_cxn, /* check_cxn */
  1141. Curl_none_shutdown, /* shutdown */
  1142. Curl_none_data_pending, /* data_pending */
  1143. Curl_none_random, /* random */
  1144. Curl_none_cert_status_request, /* cert_status_request */
  1145. multissl_connect, /* connect */
  1146. multissl_connect_nonblocking, /* connect_nonblocking */
  1147. multissl_adjust_pollset, /* adjust_pollset */
  1148. multissl_get_internals, /* get_internals */
  1149. multissl_close, /* close_one */
  1150. Curl_none_close_all, /* close_all */
  1151. Curl_none_set_engine, /* set_engine */
  1152. Curl_none_set_engine_default, /* set_engine_default */
  1153. Curl_none_engines_list, /* engines_list */
  1154. Curl_none_false_start, /* false_start */
  1155. NULL, /* sha256sum */
  1156. NULL, /* associate_connection */
  1157. NULL, /* disassociate_connection */
  1158. multissl_recv_plain, /* recv decrypted data */
  1159. multissl_send_plain, /* send data to encrypt */
  1160. };
  1161. const struct Curl_ssl *Curl_ssl =
  1162. #if defined(CURL_WITH_MULTI_SSL)
  1163. &Curl_ssl_multi;
  1164. #elif defined(USE_WOLFSSL)
  1165. &Curl_ssl_wolfssl;
  1166. #elif defined(USE_GNUTLS)
  1167. &Curl_ssl_gnutls;
  1168. #elif defined(USE_MBEDTLS)
  1169. &Curl_ssl_mbedtls;
  1170. #elif defined(USE_RUSTLS)
  1171. &Curl_ssl_rustls;
  1172. #elif defined(USE_OPENSSL)
  1173. &Curl_ssl_openssl;
  1174. #elif defined(USE_SECTRANSP)
  1175. &Curl_ssl_sectransp;
  1176. #elif defined(USE_SCHANNEL)
  1177. &Curl_ssl_schannel;
  1178. #elif defined(USE_BEARSSL)
  1179. &Curl_ssl_bearssl;
  1180. #else
  1181. #error "Missing struct Curl_ssl for selected SSL backend"
  1182. #endif
  1183. static const struct Curl_ssl *available_backends[] = {
  1184. #if defined(USE_WOLFSSL)
  1185. &Curl_ssl_wolfssl,
  1186. #endif
  1187. #if defined(USE_GNUTLS)
  1188. &Curl_ssl_gnutls,
  1189. #endif
  1190. #if defined(USE_MBEDTLS)
  1191. &Curl_ssl_mbedtls,
  1192. #endif
  1193. #if defined(USE_OPENSSL)
  1194. &Curl_ssl_openssl,
  1195. #endif
  1196. #if defined(USE_SECTRANSP)
  1197. &Curl_ssl_sectransp,
  1198. #endif
  1199. #if defined(USE_SCHANNEL)
  1200. &Curl_ssl_schannel,
  1201. #endif
  1202. #if defined(USE_BEARSSL)
  1203. &Curl_ssl_bearssl,
  1204. #endif
  1205. #if defined(USE_RUSTLS)
  1206. &Curl_ssl_rustls,
  1207. #endif
  1208. NULL
  1209. };
  1210. /* Global cleanup */
  1211. void Curl_ssl_cleanup(void)
  1212. {
  1213. if(init_ssl) {
  1214. /* only cleanup if we did a previous init */
  1215. Curl_ssl->cleanup();
  1216. #if defined(CURL_WITH_MULTI_SSL)
  1217. Curl_ssl = &Curl_ssl_multi;
  1218. #endif
  1219. init_ssl = FALSE;
  1220. }
  1221. }
  1222. static size_t multissl_version(char *buffer, size_t size)
  1223. {
  1224. static const struct Curl_ssl *selected;
  1225. static char backends[200];
  1226. static size_t backends_len;
  1227. const struct Curl_ssl *current;
  1228. current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl;
  1229. if(current != selected) {
  1230. char *p = backends;
  1231. char *end = backends + sizeof(backends);
  1232. int i;
  1233. selected = current;
  1234. backends[0] = '\0';
  1235. for(i = 0; available_backends[i]; ++i) {
  1236. char vb[200];
  1237. bool paren = (selected != available_backends[i]);
  1238. if(available_backends[i]->version(vb, sizeof(vb))) {
  1239. p += msnprintf(p, end - p, "%s%s%s%s", (p != backends ? " " : ""),
  1240. (paren ? "(" : ""), vb, (paren ? ")" : ""));
  1241. }
  1242. }
  1243. backends_len = p - backends;
  1244. }
  1245. if(size) {
  1246. if(backends_len < size)
  1247. strcpy(buffer, backends);
  1248. else
  1249. *buffer = 0; /* did not fit */
  1250. }
  1251. return 0;
  1252. }
  1253. static int multissl_setup(const struct Curl_ssl *backend)
  1254. {
  1255. const char *env;
  1256. char *env_tmp;
  1257. if(Curl_ssl != &Curl_ssl_multi)
  1258. return 1;
  1259. if(backend) {
  1260. Curl_ssl = backend;
  1261. return 0;
  1262. }
  1263. if(!available_backends[0])
  1264. return 1;
  1265. env = env_tmp = curl_getenv("CURL_SSL_BACKEND");
  1266. #ifdef CURL_DEFAULT_SSL_BACKEND
  1267. if(!env)
  1268. env = CURL_DEFAULT_SSL_BACKEND;
  1269. #endif
  1270. if(env) {
  1271. int i;
  1272. for(i = 0; available_backends[i]; i++) {
  1273. if(strcasecompare(env, available_backends[i]->info.name)) {
  1274. Curl_ssl = available_backends[i];
  1275. free(env_tmp);
  1276. return 0;
  1277. }
  1278. }
  1279. }
  1280. /* Fall back to first available backend */
  1281. Curl_ssl = available_backends[0];
  1282. free(env_tmp);
  1283. return 0;
  1284. }
  1285. /* This function is used to select the SSL backend to use. It is called by
  1286. curl_global_sslset (easy.c) which uses the global init lock. */
  1287. CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name,
  1288. const curl_ssl_backend ***avail)
  1289. {
  1290. int i;
  1291. if(avail)
  1292. *avail = (const curl_ssl_backend **)&available_backends;
  1293. if(Curl_ssl != &Curl_ssl_multi)
  1294. return id == Curl_ssl->info.id ||
  1295. (name && strcasecompare(name, Curl_ssl->info.name)) ?
  1296. CURLSSLSET_OK :
  1297. #if defined(CURL_WITH_MULTI_SSL)
  1298. CURLSSLSET_TOO_LATE;
  1299. #else
  1300. CURLSSLSET_UNKNOWN_BACKEND;
  1301. #endif
  1302. for(i = 0; available_backends[i]; i++) {
  1303. if(available_backends[i]->info.id == id ||
  1304. (name && strcasecompare(available_backends[i]->info.name, name))) {
  1305. multissl_setup(available_backends[i]);
  1306. return CURLSSLSET_OK;
  1307. }
  1308. }
  1309. return CURLSSLSET_UNKNOWN_BACKEND;
  1310. }
  1311. #else /* USE_SSL */
  1312. CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name,
  1313. const curl_ssl_backend ***avail)
  1314. {
  1315. (void)id;
  1316. (void)name;
  1317. (void)avail;
  1318. return CURLSSLSET_NO_BACKENDS;
  1319. }
  1320. #endif /* !USE_SSL */
  1321. #ifdef USE_SSL
  1322. void Curl_ssl_peer_cleanup(struct ssl_peer *peer)
  1323. {
  1324. if(peer->dispname != peer->hostname)
  1325. free(peer->dispname);
  1326. free(peer->sni);
  1327. free(peer->hostname);
  1328. peer->hostname = peer->sni = peer->dispname = NULL;
  1329. peer->type = CURL_SSL_PEER_DNS;
  1330. }
  1331. static void cf_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1332. {
  1333. struct ssl_connect_data *connssl = cf->ctx;
  1334. if(connssl) {
  1335. Curl_ssl->close(cf, data);
  1336. connssl->state = ssl_connection_none;
  1337. Curl_ssl_peer_cleanup(&connssl->peer);
  1338. }
  1339. cf->connected = FALSE;
  1340. }
  1341. static ssl_peer_type get_peer_type(const char *hostname)
  1342. {
  1343. if(hostname && hostname[0]) {
  1344. #ifdef USE_IPV6
  1345. struct in6_addr addr;
  1346. #else
  1347. struct in_addr addr;
  1348. #endif
  1349. if(Curl_inet_pton(AF_INET, hostname, &addr))
  1350. return CURL_SSL_PEER_IPV4;
  1351. #ifdef USE_IPV6
  1352. else if(Curl_inet_pton(AF_INET6, hostname, &addr)) {
  1353. return CURL_SSL_PEER_IPV6;
  1354. }
  1355. #endif
  1356. }
  1357. return CURL_SSL_PEER_DNS;
  1358. }
  1359. CURLcode Curl_ssl_peer_init(struct ssl_peer *peer, struct Curl_cfilter *cf,
  1360. int transport)
  1361. {
  1362. const char *ehostname, *edispname;
  1363. int eport;
  1364. /* We need the hostname for SNI negotiation. Once handshaked, this remains
  1365. * the SNI hostname for the TLS connection. When the connection is reused,
  1366. * the settings in cf->conn might change. We keep a copy of the hostname we
  1367. * use for SNI.
  1368. */
  1369. #ifndef CURL_DISABLE_PROXY
  1370. if(Curl_ssl_cf_is_proxy(cf)) {
  1371. ehostname = cf->conn->http_proxy.host.name;
  1372. edispname = cf->conn->http_proxy.host.dispname;
  1373. eport = cf->conn->http_proxy.port;
  1374. }
  1375. else
  1376. #endif
  1377. {
  1378. ehostname = cf->conn->host.name;
  1379. edispname = cf->conn->host.dispname;
  1380. eport = cf->conn->remote_port;
  1381. }
  1382. /* change if ehostname changed */
  1383. DEBUGASSERT(!ehostname || ehostname[0]);
  1384. if(ehostname && (!peer->hostname
  1385. || strcmp(ehostname, peer->hostname))) {
  1386. Curl_ssl_peer_cleanup(peer);
  1387. peer->hostname = strdup(ehostname);
  1388. if(!peer->hostname) {
  1389. Curl_ssl_peer_cleanup(peer);
  1390. return CURLE_OUT_OF_MEMORY;
  1391. }
  1392. if(!edispname || !strcmp(ehostname, edispname))
  1393. peer->dispname = peer->hostname;
  1394. else {
  1395. peer->dispname = strdup(edispname);
  1396. if(!peer->dispname) {
  1397. Curl_ssl_peer_cleanup(peer);
  1398. return CURLE_OUT_OF_MEMORY;
  1399. }
  1400. }
  1401. peer->type = get_peer_type(peer->hostname);
  1402. if(peer->type == CURL_SSL_PEER_DNS && peer->hostname[0]) {
  1403. /* not an IP address, normalize according to RCC 6066 ch. 3,
  1404. * max len of SNI is 2^16-1, no trailing dot */
  1405. size_t len = strlen(peer->hostname);
  1406. if(len && (peer->hostname[len-1] == '.'))
  1407. len--;
  1408. if(len < USHRT_MAX) {
  1409. peer->sni = calloc(1, len + 1);
  1410. if(!peer->sni) {
  1411. Curl_ssl_peer_cleanup(peer);
  1412. return CURLE_OUT_OF_MEMORY;
  1413. }
  1414. Curl_strntolower(peer->sni, peer->hostname, len);
  1415. peer->sni[len] = 0;
  1416. }
  1417. }
  1418. }
  1419. peer->port = eport;
  1420. peer->transport = transport;
  1421. return CURLE_OK;
  1422. }
  1423. static void ssl_cf_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  1424. {
  1425. struct cf_call_data save;
  1426. CF_DATA_SAVE(save, cf, data);
  1427. cf_close(cf, data);
  1428. CF_DATA_RESTORE(cf, save);
  1429. cf_ctx_free(cf->ctx);
  1430. cf->ctx = NULL;
  1431. }
  1432. static void ssl_cf_close(struct Curl_cfilter *cf,
  1433. struct Curl_easy *data)
  1434. {
  1435. struct cf_call_data save;
  1436. CF_DATA_SAVE(save, cf, data);
  1437. cf_close(cf, data);
  1438. if(cf->next)
  1439. cf->next->cft->do_close(cf->next, data);
  1440. CF_DATA_RESTORE(cf, save);
  1441. }
  1442. static CURLcode ssl_cf_connect(struct Curl_cfilter *cf,
  1443. struct Curl_easy *data,
  1444. bool blocking, bool *done)
  1445. {
  1446. struct ssl_connect_data *connssl = cf->ctx;
  1447. struct cf_call_data save;
  1448. CURLcode result;
  1449. if(cf->connected) {
  1450. *done = TRUE;
  1451. return CURLE_OK;
  1452. }
  1453. CF_DATA_SAVE(save, cf, data);
  1454. CURL_TRC_CF(data, cf, "cf_connect()");
  1455. (void)connssl;
  1456. DEBUGASSERT(data->conn);
  1457. DEBUGASSERT(data->conn == cf->conn);
  1458. DEBUGASSERT(connssl);
  1459. DEBUGASSERT(cf->conn->host.name);
  1460. result = cf->next->cft->do_connect(cf->next, data, blocking, done);
  1461. if(result || !*done)
  1462. goto out;
  1463. *done = FALSE;
  1464. result = Curl_ssl_peer_init(&connssl->peer, cf, TRNSPRT_TCP);
  1465. if(result)
  1466. goto out;
  1467. if(blocking) {
  1468. result = ssl_connect(cf, data);
  1469. *done = (result == CURLE_OK);
  1470. }
  1471. else {
  1472. result = ssl_connect_nonblocking(cf, data, done);
  1473. }
  1474. if(!result && *done) {
  1475. cf->connected = TRUE;
  1476. connssl->handshake_done = Curl_now();
  1477. DEBUGASSERT(connssl->state == ssl_connection_complete);
  1478. }
  1479. out:
  1480. CURL_TRC_CF(data, cf, "cf_connect() -> %d, done=%d", result, *done);
  1481. CF_DATA_RESTORE(cf, save);
  1482. return result;
  1483. }
  1484. static bool ssl_cf_data_pending(struct Curl_cfilter *cf,
  1485. const struct Curl_easy *data)
  1486. {
  1487. struct cf_call_data save;
  1488. bool result;
  1489. CF_DATA_SAVE(save, cf, data);
  1490. if(Curl_ssl->data_pending(cf, data))
  1491. result = TRUE;
  1492. else
  1493. result = cf->next->cft->has_data_pending(cf->next, data);
  1494. CF_DATA_RESTORE(cf, save);
  1495. return result;
  1496. }
  1497. static ssize_t ssl_cf_send(struct Curl_cfilter *cf,
  1498. struct Curl_easy *data, const void *buf, size_t len,
  1499. CURLcode *err)
  1500. {
  1501. struct cf_call_data save;
  1502. ssize_t nwritten;
  1503. CF_DATA_SAVE(save, cf, data);
  1504. *err = CURLE_OK;
  1505. nwritten = Curl_ssl->send_plain(cf, data, buf, len, err);
  1506. CF_DATA_RESTORE(cf, save);
  1507. return nwritten;
  1508. }
  1509. static ssize_t ssl_cf_recv(struct Curl_cfilter *cf,
  1510. struct Curl_easy *data, char *buf, size_t len,
  1511. CURLcode *err)
  1512. {
  1513. struct cf_call_data save;
  1514. ssize_t nread;
  1515. CF_DATA_SAVE(save, cf, data);
  1516. *err = CURLE_OK;
  1517. nread = Curl_ssl->recv_plain(cf, data, buf, len, err);
  1518. if(nread > 0) {
  1519. DEBUGASSERT((size_t)nread <= len);
  1520. }
  1521. else if(nread == 0) {
  1522. /* eof */
  1523. *err = CURLE_OK;
  1524. }
  1525. CURL_TRC_CF(data, cf, "cf_recv(len=%zu) -> %zd, %d", len,
  1526. nread, *err);
  1527. CF_DATA_RESTORE(cf, save);
  1528. return nread;
  1529. }
  1530. static CURLcode ssl_cf_shutdown(struct Curl_cfilter *cf,
  1531. struct Curl_easy *data,
  1532. bool *done)
  1533. {
  1534. CURLcode result = CURLE_OK;
  1535. *done = TRUE;
  1536. if(!cf->shutdown) {
  1537. struct cf_call_data save;
  1538. CF_DATA_SAVE(save, cf, data);
  1539. result = Curl_ssl->shut_down(cf, data, TRUE, done);
  1540. CURL_TRC_CF(data, cf, "cf_shutdown -> %d, done=%d", result, *done);
  1541. CF_DATA_RESTORE(cf, save);
  1542. cf->shutdown = (result || *done);
  1543. }
  1544. return result;
  1545. }
  1546. static void ssl_cf_adjust_pollset(struct Curl_cfilter *cf,
  1547. struct Curl_easy *data,
  1548. struct easy_pollset *ps)
  1549. {
  1550. struct cf_call_data save;
  1551. CF_DATA_SAVE(save, cf, data);
  1552. Curl_ssl->adjust_pollset(cf, data, ps);
  1553. CF_DATA_RESTORE(cf, save);
  1554. }
  1555. static CURLcode ssl_cf_cntrl(struct Curl_cfilter *cf,
  1556. struct Curl_easy *data,
  1557. int event, int arg1, void *arg2)
  1558. {
  1559. struct cf_call_data save;
  1560. (void)arg1;
  1561. (void)arg2;
  1562. switch(event) {
  1563. case CF_CTRL_DATA_ATTACH:
  1564. if(Curl_ssl->attach_data) {
  1565. CF_DATA_SAVE(save, cf, data);
  1566. Curl_ssl->attach_data(cf, data);
  1567. CF_DATA_RESTORE(cf, save);
  1568. }
  1569. break;
  1570. case CF_CTRL_DATA_DETACH:
  1571. if(Curl_ssl->detach_data) {
  1572. CF_DATA_SAVE(save, cf, data);
  1573. Curl_ssl->detach_data(cf, data);
  1574. CF_DATA_RESTORE(cf, save);
  1575. }
  1576. break;
  1577. default:
  1578. break;
  1579. }
  1580. return CURLE_OK;
  1581. }
  1582. static CURLcode ssl_cf_query(struct Curl_cfilter *cf,
  1583. struct Curl_easy *data,
  1584. int query, int *pres1, void *pres2)
  1585. {
  1586. struct ssl_connect_data *connssl = cf->ctx;
  1587. switch(query) {
  1588. case CF_QUERY_TIMER_APPCONNECT: {
  1589. struct curltime *when = pres2;
  1590. if(cf->connected && !Curl_ssl_cf_is_proxy(cf))
  1591. *when = connssl->handshake_done;
  1592. return CURLE_OK;
  1593. }
  1594. default:
  1595. break;
  1596. }
  1597. return cf->next?
  1598. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  1599. CURLE_UNKNOWN_OPTION;
  1600. }
  1601. static bool cf_ssl_is_alive(struct Curl_cfilter *cf, struct Curl_easy *data,
  1602. bool *input_pending)
  1603. {
  1604. struct cf_call_data save;
  1605. int result;
  1606. /*
  1607. * This function tries to determine connection status.
  1608. *
  1609. * Return codes:
  1610. * 1 means the connection is still in place
  1611. * 0 means the connection has been closed
  1612. * -1 means the connection status is unknown
  1613. */
  1614. CF_DATA_SAVE(save, cf, data);
  1615. result = Curl_ssl->check_cxn(cf, data);
  1616. CF_DATA_RESTORE(cf, save);
  1617. if(result > 0) {
  1618. *input_pending = TRUE;
  1619. return TRUE;
  1620. }
  1621. if(result == 0) {
  1622. *input_pending = FALSE;
  1623. return FALSE;
  1624. }
  1625. /* ssl backend does not know */
  1626. return cf->next?
  1627. cf->next->cft->is_alive(cf->next, data, input_pending) :
  1628. FALSE; /* pessimistic in absence of data */
  1629. }
  1630. struct Curl_cftype Curl_cft_ssl = {
  1631. "SSL",
  1632. CF_TYPE_SSL,
  1633. CURL_LOG_LVL_NONE,
  1634. ssl_cf_destroy,
  1635. ssl_cf_connect,
  1636. ssl_cf_close,
  1637. ssl_cf_shutdown,
  1638. Curl_cf_def_get_host,
  1639. ssl_cf_adjust_pollset,
  1640. ssl_cf_data_pending,
  1641. ssl_cf_send,
  1642. ssl_cf_recv,
  1643. ssl_cf_cntrl,
  1644. cf_ssl_is_alive,
  1645. Curl_cf_def_conn_keep_alive,
  1646. ssl_cf_query,
  1647. };
  1648. #ifndef CURL_DISABLE_PROXY
  1649. struct Curl_cftype Curl_cft_ssl_proxy = {
  1650. "SSL-PROXY",
  1651. CF_TYPE_SSL|CF_TYPE_PROXY,
  1652. CURL_LOG_LVL_NONE,
  1653. ssl_cf_destroy,
  1654. ssl_cf_connect,
  1655. ssl_cf_close,
  1656. ssl_cf_shutdown,
  1657. Curl_cf_def_get_host,
  1658. ssl_cf_adjust_pollset,
  1659. ssl_cf_data_pending,
  1660. ssl_cf_send,
  1661. ssl_cf_recv,
  1662. ssl_cf_cntrl,
  1663. cf_ssl_is_alive,
  1664. Curl_cf_def_conn_keep_alive,
  1665. Curl_cf_def_query,
  1666. };
  1667. #endif /* !CURL_DISABLE_PROXY */
  1668. static CURLcode cf_ssl_create(struct Curl_cfilter **pcf,
  1669. struct Curl_easy *data,
  1670. struct connectdata *conn)
  1671. {
  1672. struct Curl_cfilter *cf = NULL;
  1673. struct ssl_connect_data *ctx;
  1674. CURLcode result;
  1675. DEBUGASSERT(data->conn);
  1676. ctx = cf_ctx_new(data, alpn_get_spec(data->state.httpwant,
  1677. conn->bits.tls_enable_alpn));
  1678. if(!ctx) {
  1679. result = CURLE_OUT_OF_MEMORY;
  1680. goto out;
  1681. }
  1682. result = Curl_cf_create(&cf, &Curl_cft_ssl, ctx);
  1683. out:
  1684. if(result)
  1685. cf_ctx_free(ctx);
  1686. *pcf = result? NULL : cf;
  1687. return result;
  1688. }
  1689. CURLcode Curl_ssl_cfilter_add(struct Curl_easy *data,
  1690. struct connectdata *conn,
  1691. int sockindex)
  1692. {
  1693. struct Curl_cfilter *cf;
  1694. CURLcode result;
  1695. result = cf_ssl_create(&cf, data, conn);
  1696. if(!result)
  1697. Curl_conn_cf_add(data, conn, sockindex, cf);
  1698. return result;
  1699. }
  1700. CURLcode Curl_cf_ssl_insert_after(struct Curl_cfilter *cf_at,
  1701. struct Curl_easy *data)
  1702. {
  1703. struct Curl_cfilter *cf;
  1704. CURLcode result;
  1705. result = cf_ssl_create(&cf, data, cf_at->conn);
  1706. if(!result)
  1707. Curl_conn_cf_insert_after(cf_at, cf);
  1708. return result;
  1709. }
  1710. #ifndef CURL_DISABLE_PROXY
  1711. static CURLcode cf_ssl_proxy_create(struct Curl_cfilter **pcf,
  1712. struct Curl_easy *data,
  1713. struct connectdata *conn)
  1714. {
  1715. struct Curl_cfilter *cf = NULL;
  1716. struct ssl_connect_data *ctx;
  1717. CURLcode result;
  1718. bool use_alpn = conn->bits.tls_enable_alpn;
  1719. int httpwant = CURL_HTTP_VERSION_1_1;
  1720. #ifdef USE_HTTP2
  1721. if(conn->http_proxy.proxytype == CURLPROXY_HTTPS2) {
  1722. use_alpn = TRUE;
  1723. httpwant = CURL_HTTP_VERSION_2;
  1724. }
  1725. #endif
  1726. ctx = cf_ctx_new(data, alpn_get_spec(httpwant, use_alpn));
  1727. if(!ctx) {
  1728. result = CURLE_OUT_OF_MEMORY;
  1729. goto out;
  1730. }
  1731. result = Curl_cf_create(&cf, &Curl_cft_ssl_proxy, ctx);
  1732. out:
  1733. if(result)
  1734. cf_ctx_free(ctx);
  1735. *pcf = result? NULL : cf;
  1736. return result;
  1737. }
  1738. CURLcode Curl_cf_ssl_proxy_insert_after(struct Curl_cfilter *cf_at,
  1739. struct Curl_easy *data)
  1740. {
  1741. struct Curl_cfilter *cf;
  1742. CURLcode result;
  1743. result = cf_ssl_proxy_create(&cf, data, cf_at->conn);
  1744. if(!result)
  1745. Curl_conn_cf_insert_after(cf_at, cf);
  1746. return result;
  1747. }
  1748. #endif /* !CURL_DISABLE_PROXY */
  1749. bool Curl_ssl_supports(struct Curl_easy *data, unsigned int ssl_option)
  1750. {
  1751. (void)data;
  1752. return (Curl_ssl->supports & ssl_option)? TRUE : FALSE;
  1753. }
  1754. static struct Curl_cfilter *get_ssl_filter(struct Curl_cfilter *cf)
  1755. {
  1756. for(; cf; cf = cf->next) {
  1757. if(cf->cft == &Curl_cft_ssl)
  1758. return cf;
  1759. #ifndef CURL_DISABLE_PROXY
  1760. if(cf->cft == &Curl_cft_ssl_proxy)
  1761. return cf;
  1762. #endif
  1763. }
  1764. return NULL;
  1765. }
  1766. void *Curl_ssl_get_internals(struct Curl_easy *data, int sockindex,
  1767. CURLINFO info, int n)
  1768. {
  1769. void *result = NULL;
  1770. (void)n;
  1771. if(data->conn) {
  1772. struct Curl_cfilter *cf;
  1773. /* get first SSL filter in chain, if any is present */
  1774. cf = get_ssl_filter(data->conn->cfilter[sockindex]);
  1775. if(cf) {
  1776. struct cf_call_data save;
  1777. CF_DATA_SAVE(save, cf, data);
  1778. result = Curl_ssl->get_internals(cf->ctx, info);
  1779. CF_DATA_RESTORE(cf, save);
  1780. }
  1781. }
  1782. return result;
  1783. }
  1784. static CURLcode vtls_shutdown_blocking(struct Curl_cfilter *cf,
  1785. struct Curl_easy *data,
  1786. bool send_shutdown, bool *done)
  1787. {
  1788. struct ssl_connect_data *connssl = cf->ctx;
  1789. struct cf_call_data save;
  1790. CURLcode result = CURLE_OK;
  1791. timediff_t timeout_ms;
  1792. int what, loop = 10;
  1793. if(cf->shutdown) {
  1794. *done = TRUE;
  1795. return CURLE_OK;
  1796. }
  1797. CF_DATA_SAVE(save, cf, data);
  1798. *done = FALSE;
  1799. while(!result && !*done && loop--) {
  1800. timeout_ms = Curl_shutdown_timeleft(cf->conn, cf->sockindex, NULL);
  1801. if(timeout_ms < 0) {
  1802. /* no need to continue if time is already up */
  1803. failf(data, "SSL shutdown timeout");
  1804. return CURLE_OPERATION_TIMEDOUT;
  1805. }
  1806. result = Curl_ssl->shut_down(cf, data, send_shutdown, done);
  1807. if(result ||*done)
  1808. goto out;
  1809. if(connssl->io_need) {
  1810. what = Curl_conn_cf_poll(cf, data, timeout_ms);
  1811. if(what < 0) {
  1812. /* fatal error */
  1813. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1814. result = CURLE_RECV_ERROR;
  1815. goto out;
  1816. }
  1817. else if(0 == what) {
  1818. /* timeout */
  1819. failf(data, "SSL shutdown timeout");
  1820. result = CURLE_OPERATION_TIMEDOUT;
  1821. goto out;
  1822. }
  1823. /* socket is readable or writable */
  1824. }
  1825. }
  1826. out:
  1827. CF_DATA_RESTORE(cf, save);
  1828. cf->shutdown = (result || *done);
  1829. return result;
  1830. }
  1831. CURLcode Curl_ssl_cfilter_remove(struct Curl_easy *data,
  1832. int sockindex, bool send_shutdown)
  1833. {
  1834. struct Curl_cfilter *cf, *head;
  1835. CURLcode result = CURLE_OK;
  1836. head = data->conn? data->conn->cfilter[sockindex] : NULL;
  1837. for(cf = head; cf; cf = cf->next) {
  1838. if(cf->cft == &Curl_cft_ssl) {
  1839. bool done;
  1840. CURL_TRC_CF(data, cf, "shutdown and remove SSL, start");
  1841. Curl_shutdown_start(data, sockindex, NULL);
  1842. result = vtls_shutdown_blocking(cf, data, send_shutdown, &done);
  1843. Curl_shutdown_clear(data, sockindex);
  1844. if(!result && !done) /* blocking failed? */
  1845. result = CURLE_SSL_SHUTDOWN_FAILED;
  1846. Curl_conn_cf_discard_sub(head, cf, data, FALSE);
  1847. CURL_TRC_CF(data, cf, "shutdown and remove SSL, done -> %d", result);
  1848. break;
  1849. }
  1850. }
  1851. return result;
  1852. }
  1853. bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf)
  1854. {
  1855. return (cf->cft->flags & CF_TYPE_SSL) && (cf->cft->flags & CF_TYPE_PROXY);
  1856. }
  1857. struct ssl_config_data *
  1858. Curl_ssl_cf_get_config(struct Curl_cfilter *cf, struct Curl_easy *data)
  1859. {
  1860. #ifdef CURL_DISABLE_PROXY
  1861. (void)cf;
  1862. return &data->set.ssl;
  1863. #else
  1864. return Curl_ssl_cf_is_proxy(cf)? &data->set.proxy_ssl : &data->set.ssl;
  1865. #endif
  1866. }
  1867. struct ssl_primary_config *
  1868. Curl_ssl_cf_get_primary_config(struct Curl_cfilter *cf)
  1869. {
  1870. #ifdef CURL_DISABLE_PROXY
  1871. return &cf->conn->ssl_config;
  1872. #else
  1873. return Curl_ssl_cf_is_proxy(cf)?
  1874. &cf->conn->proxy_ssl_config : &cf->conn->ssl_config;
  1875. #endif
  1876. }
  1877. CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf,
  1878. const struct alpn_spec *spec)
  1879. {
  1880. size_t i, len;
  1881. int off = 0;
  1882. unsigned char blen;
  1883. memset(buf, 0, sizeof(*buf));
  1884. for(i = 0; spec && i < spec->count; ++i) {
  1885. len = strlen(spec->entries[i]);
  1886. if(len >= ALPN_NAME_MAX)
  1887. return CURLE_FAILED_INIT;
  1888. blen = (unsigned char)len;
  1889. if(off + blen + 1 >= (int)sizeof(buf->data))
  1890. return CURLE_FAILED_INIT;
  1891. buf->data[off++] = blen;
  1892. memcpy(buf->data + off, spec->entries[i], blen);
  1893. off += blen;
  1894. }
  1895. buf->len = off;
  1896. return CURLE_OK;
  1897. }
  1898. CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf,
  1899. const struct alpn_spec *spec)
  1900. {
  1901. size_t i, len;
  1902. size_t off = 0;
  1903. memset(buf, 0, sizeof(*buf));
  1904. for(i = 0; spec && i < spec->count; ++i) {
  1905. len = strlen(spec->entries[i]);
  1906. if(len >= ALPN_NAME_MAX)
  1907. return CURLE_FAILED_INIT;
  1908. if(off + len + 2 >= sizeof(buf->data))
  1909. return CURLE_FAILED_INIT;
  1910. if(off)
  1911. buf->data[off++] = ',';
  1912. memcpy(buf->data + off, spec->entries[i], len);
  1913. off += len;
  1914. }
  1915. buf->data[off] = '\0';
  1916. buf->len = (int)off;
  1917. return CURLE_OK;
  1918. }
  1919. CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
  1920. struct Curl_easy *data,
  1921. const unsigned char *proto,
  1922. size_t proto_len)
  1923. {
  1924. int can_multi = 0;
  1925. unsigned char *palpn =
  1926. #ifndef CURL_DISABLE_PROXY
  1927. (cf->conn->bits.tunnel_proxy && Curl_ssl_cf_is_proxy(cf))?
  1928. &cf->conn->proxy_alpn : &cf->conn->alpn
  1929. #else
  1930. &cf->conn->alpn
  1931. #endif
  1932. ;
  1933. if(proto && proto_len) {
  1934. if(proto_len == ALPN_HTTP_1_1_LENGTH &&
  1935. !memcmp(ALPN_HTTP_1_1, proto, ALPN_HTTP_1_1_LENGTH)) {
  1936. *palpn = CURL_HTTP_VERSION_1_1;
  1937. }
  1938. #ifdef USE_HTTP2
  1939. else if(proto_len == ALPN_H2_LENGTH &&
  1940. !memcmp(ALPN_H2, proto, ALPN_H2_LENGTH)) {
  1941. *palpn = CURL_HTTP_VERSION_2;
  1942. can_multi = 1;
  1943. }
  1944. #endif
  1945. #ifdef USE_HTTP3
  1946. else if(proto_len == ALPN_H3_LENGTH &&
  1947. !memcmp(ALPN_H3, proto, ALPN_H3_LENGTH)) {
  1948. *palpn = CURL_HTTP_VERSION_3;
  1949. can_multi = 1;
  1950. }
  1951. #endif
  1952. else {
  1953. *palpn = CURL_HTTP_VERSION_NONE;
  1954. failf(data, "unsupported ALPN protocol: '%.*s'", (int)proto_len, proto);
  1955. /* TODO: do we want to fail this? Previous code just ignored it and
  1956. * some vtls backends even ignore the return code of this function. */
  1957. /* return CURLE_NOT_BUILT_IN; */
  1958. goto out;
  1959. }
  1960. infof(data, VTLS_INFOF_ALPN_ACCEPTED_LEN_1STR, (int)proto_len, proto);
  1961. }
  1962. else {
  1963. *palpn = CURL_HTTP_VERSION_NONE;
  1964. infof(data, VTLS_INFOF_NO_ALPN);
  1965. }
  1966. out:
  1967. if(!Curl_ssl_cf_is_proxy(cf))
  1968. Curl_multiuse_state(data, can_multi?
  1969. BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
  1970. return CURLE_OK;
  1971. }
  1972. #endif /* USE_SSL */