vtls.c 64 KB

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