vtls.c 61 KB

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