vtls.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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. ***************************************************************************/
  22. /* This file is for implementing all "generic" SSL functions that all libcurl
  23. internals should use. It is then responsible for calling the proper
  24. "backend" function.
  25. SSL-functions in libcurl should call functions in this source file, and not
  26. to any specific SSL-layer.
  27. Curl_ssl_ - prefix for generic ones
  28. Note that this source code uses the functions of the configured SSL
  29. backend via the global Curl_ssl instance.
  30. "SSL/TLS Strong Encryption: An Introduction"
  31. https://httpd.apache.org/docs/2.0/ssl/ssl_intro.html
  32. */
  33. #include "curl_setup.h"
  34. #ifdef HAVE_SYS_TYPES_H
  35. #include <sys/types.h>
  36. #endif
  37. #ifdef HAVE_SYS_STAT_H
  38. #include <sys/stat.h>
  39. #endif
  40. #ifdef HAVE_FCNTL_H
  41. #include <fcntl.h>
  42. #endif
  43. #include "urldata.h"
  44. #include "vtls.h" /* generic SSL protos etc */
  45. #include "slist.h"
  46. #include "sendf.h"
  47. #include "strcase.h"
  48. #include "url.h"
  49. #include "progress.h"
  50. #include "share.h"
  51. #include "multiif.h"
  52. #include "timeval.h"
  53. #include "curl_md5.h"
  54. #include "warnless.h"
  55. #include "curl_base64.h"
  56. #include "curl_printf.h"
  57. #include "strdup.h"
  58. /* The last #include files should be: */
  59. #include "curl_memory.h"
  60. #include "memdebug.h"
  61. /* convenience macro to check if this handle is using a shared SSL session */
  62. #define SSLSESSION_SHARED(data) (data->share && \
  63. (data->share->specifier & \
  64. (1<<CURL_LOCK_DATA_SSL_SESSION)))
  65. #define CLONE_STRING(var) \
  66. if(source->var) { \
  67. dest->var = strdup(source->var); \
  68. if(!dest->var) \
  69. return FALSE; \
  70. } \
  71. else \
  72. dest->var = NULL;
  73. #define CLONE_BLOB(var) \
  74. if(blobdup(&dest->var, source->var)) \
  75. return FALSE;
  76. static CURLcode blobdup(struct curl_blob **dest,
  77. struct curl_blob *src)
  78. {
  79. DEBUGASSERT(dest);
  80. DEBUGASSERT(!*dest);
  81. if(src) {
  82. /* only if there's data to dupe! */
  83. struct curl_blob *d;
  84. d = malloc(sizeof(struct curl_blob) + src->len);
  85. if(!d)
  86. return CURLE_OUT_OF_MEMORY;
  87. d->len = src->len;
  88. /* Always duplicate because the connection may survive longer than the
  89. handle that passed in the blob. */
  90. d->flags = CURL_BLOB_COPY;
  91. d->data = (void *)((char *)d + sizeof(struct curl_blob));
  92. memcpy(d->data, src->data, src->len);
  93. *dest = d;
  94. }
  95. return CURLE_OK;
  96. }
  97. /* returns TRUE if the blobs are identical */
  98. static bool blobcmp(struct curl_blob *first, struct curl_blob *second)
  99. {
  100. if(!first && !second) /* both are NULL */
  101. return TRUE;
  102. if(!first || !second) /* one is NULL */
  103. return FALSE;
  104. if(first->len != second->len) /* different sizes */
  105. return FALSE;
  106. return !memcmp(first->data, second->data, first->len); /* same data */
  107. }
  108. bool
  109. Curl_ssl_config_matches(struct ssl_primary_config *data,
  110. struct ssl_primary_config *needle)
  111. {
  112. if((data->version == needle->version) &&
  113. (data->version_max == needle->version_max) &&
  114. (data->verifypeer == needle->verifypeer) &&
  115. (data->verifyhost == needle->verifyhost) &&
  116. (data->verifystatus == needle->verifystatus) &&
  117. blobcmp(data->cert_blob, needle->cert_blob) &&
  118. Curl_safe_strcasecompare(data->CApath, needle->CApath) &&
  119. Curl_safe_strcasecompare(data->CAfile, needle->CAfile) &&
  120. Curl_safe_strcasecompare(data->clientcert, needle->clientcert) &&
  121. Curl_safe_strcasecompare(data->random_file, needle->random_file) &&
  122. Curl_safe_strcasecompare(data->egdsocket, needle->egdsocket) &&
  123. Curl_safe_strcasecompare(data->cipher_list, needle->cipher_list) &&
  124. Curl_safe_strcasecompare(data->cipher_list13, needle->cipher_list13) &&
  125. Curl_safe_strcasecompare(data->curves, needle->curves) &&
  126. Curl_safe_strcasecompare(data->pinned_key, needle->pinned_key))
  127. return TRUE;
  128. return FALSE;
  129. }
  130. bool
  131. Curl_clone_primary_ssl_config(struct ssl_primary_config *source,
  132. struct ssl_primary_config *dest)
  133. {
  134. dest->version = source->version;
  135. dest->version_max = source->version_max;
  136. dest->verifypeer = source->verifypeer;
  137. dest->verifyhost = source->verifyhost;
  138. dest->verifystatus = source->verifystatus;
  139. dest->sessionid = source->sessionid;
  140. CLONE_BLOB(cert_blob);
  141. CLONE_STRING(CApath);
  142. CLONE_STRING(CAfile);
  143. CLONE_STRING(clientcert);
  144. CLONE_STRING(random_file);
  145. CLONE_STRING(egdsocket);
  146. CLONE_STRING(cipher_list);
  147. CLONE_STRING(cipher_list13);
  148. CLONE_STRING(pinned_key);
  149. CLONE_STRING(curves);
  150. return TRUE;
  151. }
  152. void Curl_free_primary_ssl_config(struct ssl_primary_config *sslc)
  153. {
  154. Curl_safefree(sslc->CApath);
  155. Curl_safefree(sslc->CAfile);
  156. Curl_safefree(sslc->clientcert);
  157. Curl_safefree(sslc->random_file);
  158. Curl_safefree(sslc->egdsocket);
  159. Curl_safefree(sslc->cipher_list);
  160. Curl_safefree(sslc->cipher_list13);
  161. Curl_safefree(sslc->pinned_key);
  162. Curl_safefree(sslc->cert_blob);
  163. Curl_safefree(sslc->curves);
  164. }
  165. #ifdef USE_SSL
  166. static int multissl_init(const struct Curl_ssl *backend);
  167. #endif
  168. int Curl_ssl_backend(void)
  169. {
  170. #ifdef USE_SSL
  171. multissl_init(NULL);
  172. return Curl_ssl->info.id;
  173. #else
  174. return (int)CURLSSLBACKEND_NONE;
  175. #endif
  176. }
  177. #ifdef USE_SSL
  178. /* "global" init done? */
  179. static bool init_ssl = FALSE;
  180. /**
  181. * Global SSL init
  182. *
  183. * @retval 0 error initializing SSL
  184. * @retval 1 SSL initialized successfully
  185. */
  186. int Curl_ssl_init(void)
  187. {
  188. /* make sure this is only done once */
  189. if(init_ssl)
  190. return 1;
  191. init_ssl = TRUE; /* never again */
  192. return Curl_ssl->init();
  193. }
  194. #if defined(CURL_WITH_MULTI_SSL)
  195. static const struct Curl_ssl Curl_ssl_multi;
  196. #endif
  197. /* Global cleanup */
  198. void Curl_ssl_cleanup(void)
  199. {
  200. if(init_ssl) {
  201. /* only cleanup if we did a previous init */
  202. Curl_ssl->cleanup();
  203. #if defined(CURL_WITH_MULTI_SSL)
  204. Curl_ssl = &Curl_ssl_multi;
  205. #endif
  206. init_ssl = FALSE;
  207. }
  208. }
  209. static bool ssl_prefs_check(struct Curl_easy *data)
  210. {
  211. /* check for CURLOPT_SSLVERSION invalid parameter value */
  212. const long sslver = data->set.ssl.primary.version;
  213. if((sslver < 0) || (sslver >= CURL_SSLVERSION_LAST)) {
  214. failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION");
  215. return FALSE;
  216. }
  217. switch(data->set.ssl.primary.version_max) {
  218. case CURL_SSLVERSION_MAX_NONE:
  219. case CURL_SSLVERSION_MAX_DEFAULT:
  220. break;
  221. default:
  222. if((data->set.ssl.primary.version_max >> 16) < sslver) {
  223. failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION");
  224. return FALSE;
  225. }
  226. }
  227. return TRUE;
  228. }
  229. #ifndef CURL_DISABLE_PROXY
  230. static CURLcode
  231. ssl_connect_init_proxy(struct connectdata *conn, int sockindex)
  232. {
  233. DEBUGASSERT(conn->bits.proxy_ssl_connected[sockindex]);
  234. if(ssl_connection_complete == conn->ssl[sockindex].state &&
  235. !conn->proxy_ssl[sockindex].use) {
  236. struct ssl_backend_data *pbdata;
  237. if(!(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY))
  238. return CURLE_NOT_BUILT_IN;
  239. /* The pointers to the ssl backend data, which is opaque here, are swapped
  240. rather than move the contents. */
  241. pbdata = conn->proxy_ssl[sockindex].backend;
  242. conn->proxy_ssl[sockindex] = conn->ssl[sockindex];
  243. memset(&conn->ssl[sockindex], 0, sizeof(conn->ssl[sockindex]));
  244. memset(pbdata, 0, Curl_ssl->sizeof_ssl_backend_data);
  245. conn->ssl[sockindex].backend = pbdata;
  246. }
  247. return CURLE_OK;
  248. }
  249. #endif
  250. CURLcode
  251. Curl_ssl_connect(struct connectdata *conn, int sockindex)
  252. {
  253. CURLcode result;
  254. #ifndef CURL_DISABLE_PROXY
  255. if(conn->bits.proxy_ssl_connected[sockindex]) {
  256. result = ssl_connect_init_proxy(conn, sockindex);
  257. if(result)
  258. return result;
  259. }
  260. #endif
  261. if(!ssl_prefs_check(conn->data))
  262. return CURLE_SSL_CONNECT_ERROR;
  263. /* mark this is being ssl-enabled from here on. */
  264. conn->ssl[sockindex].use = TRUE;
  265. conn->ssl[sockindex].state = ssl_connection_negotiating;
  266. result = Curl_ssl->connect_blocking(conn, sockindex);
  267. if(!result)
  268. Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
  269. return result;
  270. }
  271. CURLcode
  272. Curl_ssl_connect_nonblocking(struct connectdata *conn, int sockindex,
  273. bool *done)
  274. {
  275. CURLcode result;
  276. #ifndef CURL_DISABLE_PROXY
  277. if(conn->bits.proxy_ssl_connected[sockindex]) {
  278. result = ssl_connect_init_proxy(conn, sockindex);
  279. if(result)
  280. return result;
  281. }
  282. #endif
  283. if(!ssl_prefs_check(conn->data))
  284. return CURLE_SSL_CONNECT_ERROR;
  285. /* mark this is being ssl requested from here on. */
  286. conn->ssl[sockindex].use = TRUE;
  287. result = Curl_ssl->connect_nonblocking(conn, sockindex, done);
  288. if(!result && *done)
  289. Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
  290. return result;
  291. }
  292. /*
  293. * Lock shared SSL session data
  294. */
  295. void Curl_ssl_sessionid_lock(struct connectdata *conn)
  296. {
  297. if(SSLSESSION_SHARED(conn->data))
  298. Curl_share_lock(conn->data,
  299. CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
  300. }
  301. /*
  302. * Unlock shared SSL session data
  303. */
  304. void Curl_ssl_sessionid_unlock(struct connectdata *conn)
  305. {
  306. if(SSLSESSION_SHARED(conn->data))
  307. Curl_share_unlock(conn->data, CURL_LOCK_DATA_SSL_SESSION);
  308. }
  309. /*
  310. * Check if there's a session ID for the given connection in the cache, and if
  311. * there's one suitable, it is provided. Returns TRUE when no entry matched.
  312. */
  313. bool Curl_ssl_getsessionid(struct connectdata *conn,
  314. void **ssl_sessionid,
  315. size_t *idsize, /* set 0 if unknown */
  316. int sockindex)
  317. {
  318. struct curl_ssl_session *check;
  319. struct Curl_easy *data = conn->data;
  320. size_t i;
  321. long *general_age;
  322. bool no_match = TRUE;
  323. #ifndef CURL_DISABLE_PROXY
  324. const bool isProxy = CONNECT_PROXY_SSL();
  325. struct ssl_primary_config * const ssl_config = isProxy ?
  326. &conn->proxy_ssl_config :
  327. &conn->ssl_config;
  328. const char * const name = isProxy ?
  329. conn->http_proxy.host.name : conn->host.name;
  330. int port = isProxy ? (int)conn->port : conn->remote_port;
  331. #else
  332. /* no proxy support */
  333. struct ssl_primary_config * const ssl_config = &conn->ssl_config;
  334. const char * const name = conn->host.name;
  335. int port = conn->remote_port;
  336. (void)sockindex;
  337. #endif
  338. *ssl_sessionid = NULL;
  339. DEBUGASSERT(SSL_SET_OPTION(primary.sessionid));
  340. if(!SSL_SET_OPTION(primary.sessionid))
  341. /* session ID re-use is disabled */
  342. return TRUE;
  343. /* Lock if shared */
  344. if(SSLSESSION_SHARED(data))
  345. general_age = &data->share->sessionage;
  346. else
  347. general_age = &data->state.sessionage;
  348. for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) {
  349. check = &data->state.session[i];
  350. if(!check->sessionid)
  351. /* not session ID means blank entry */
  352. continue;
  353. if(strcasecompare(name, check->name) &&
  354. ((!conn->bits.conn_to_host && !check->conn_to_host) ||
  355. (conn->bits.conn_to_host && check->conn_to_host &&
  356. strcasecompare(conn->conn_to_host.name, check->conn_to_host))) &&
  357. ((!conn->bits.conn_to_port && check->conn_to_port == -1) ||
  358. (conn->bits.conn_to_port && check->conn_to_port != -1 &&
  359. conn->conn_to_port == check->conn_to_port)) &&
  360. (port == check->remote_port) &&
  361. strcasecompare(conn->handler->scheme, check->scheme) &&
  362. Curl_ssl_config_matches(ssl_config, &check->ssl_config)) {
  363. /* yes, we have a session ID! */
  364. (*general_age)++; /* increase general age */
  365. check->age = *general_age; /* set this as used in this age */
  366. *ssl_sessionid = check->sessionid;
  367. if(idsize)
  368. *idsize = check->idsize;
  369. no_match = FALSE;
  370. break;
  371. }
  372. }
  373. return no_match;
  374. }
  375. /*
  376. * Kill a single session ID entry in the cache.
  377. */
  378. void Curl_ssl_kill_session(struct curl_ssl_session *session)
  379. {
  380. if(session->sessionid) {
  381. /* defensive check */
  382. /* free the ID the SSL-layer specific way */
  383. Curl_ssl->session_free(session->sessionid);
  384. session->sessionid = NULL;
  385. session->age = 0; /* fresh */
  386. Curl_free_primary_ssl_config(&session->ssl_config);
  387. Curl_safefree(session->name);
  388. Curl_safefree(session->conn_to_host);
  389. }
  390. }
  391. /*
  392. * Delete the given session ID from the cache.
  393. */
  394. void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid)
  395. {
  396. size_t i;
  397. struct Curl_easy *data = conn->data;
  398. for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) {
  399. struct curl_ssl_session *check = &data->state.session[i];
  400. if(check->sessionid == ssl_sessionid) {
  401. Curl_ssl_kill_session(check);
  402. break;
  403. }
  404. }
  405. }
  406. /*
  407. * Store session id in the session cache. The ID passed on to this function
  408. * must already have been extracted and allocated the proper way for the SSL
  409. * layer. Curl_XXXX_session_free() will be called to free/kill the session ID
  410. * later on.
  411. */
  412. CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
  413. void *ssl_sessionid,
  414. size_t idsize,
  415. int sockindex)
  416. {
  417. size_t i;
  418. struct Curl_easy *data = conn->data; /* the mother of all structs */
  419. struct curl_ssl_session *store = &data->state.session[0];
  420. long oldest_age = data->state.session[0].age; /* zero if unused */
  421. char *clone_host;
  422. char *clone_conn_to_host;
  423. int conn_to_port;
  424. long *general_age;
  425. #ifndef CURL_DISABLE_PROXY
  426. const bool isProxy = CONNECT_PROXY_SSL();
  427. struct ssl_primary_config * const ssl_config = isProxy ?
  428. &conn->proxy_ssl_config :
  429. &conn->ssl_config;
  430. const char *hostname = isProxy ? conn->http_proxy.host.name :
  431. conn->host.name;
  432. #else
  433. /* proxy support disabled */
  434. const bool isProxy = FALSE;
  435. struct ssl_primary_config * const ssl_config = &conn->ssl_config;
  436. const char *hostname = conn->host.name;
  437. (void)sockindex;
  438. #endif
  439. DEBUGASSERT(SSL_SET_OPTION(primary.sessionid));
  440. clone_host = strdup(hostname);
  441. if(!clone_host)
  442. return CURLE_OUT_OF_MEMORY; /* bail out */
  443. if(conn->bits.conn_to_host) {
  444. clone_conn_to_host = strdup(conn->conn_to_host.name);
  445. if(!clone_conn_to_host) {
  446. free(clone_host);
  447. return CURLE_OUT_OF_MEMORY; /* bail out */
  448. }
  449. }
  450. else
  451. clone_conn_to_host = NULL;
  452. if(conn->bits.conn_to_port)
  453. conn_to_port = conn->conn_to_port;
  454. else
  455. conn_to_port = -1;
  456. /* Now we should add the session ID and the host name to the cache, (remove
  457. the oldest if necessary) */
  458. /* If using shared SSL session, lock! */
  459. if(SSLSESSION_SHARED(data)) {
  460. general_age = &data->share->sessionage;
  461. }
  462. else {
  463. general_age = &data->state.sessionage;
  464. }
  465. /* find an empty slot for us, or find the oldest */
  466. for(i = 1; (i < data->set.general_ssl.max_ssl_sessions) &&
  467. data->state.session[i].sessionid; i++) {
  468. if(data->state.session[i].age < oldest_age) {
  469. oldest_age = data->state.session[i].age;
  470. store = &data->state.session[i];
  471. }
  472. }
  473. if(i == data->set.general_ssl.max_ssl_sessions)
  474. /* cache is full, we must "kill" the oldest entry! */
  475. Curl_ssl_kill_session(store);
  476. else
  477. store = &data->state.session[i]; /* use this slot */
  478. /* now init the session struct wisely */
  479. store->sessionid = ssl_sessionid;
  480. store->idsize = idsize;
  481. store->age = *general_age; /* set current age */
  482. /* free it if there's one already present */
  483. free(store->name);
  484. free(store->conn_to_host);
  485. store->name = clone_host; /* clone host name */
  486. store->conn_to_host = clone_conn_to_host; /* clone connect to host name */
  487. store->conn_to_port = conn_to_port; /* connect to port number */
  488. /* port number */
  489. store->remote_port = isProxy ? (int)conn->port : conn->remote_port;
  490. store->scheme = conn->handler->scheme;
  491. if(!Curl_clone_primary_ssl_config(ssl_config, &store->ssl_config)) {
  492. Curl_free_primary_ssl_config(&store->ssl_config);
  493. store->sessionid = NULL; /* let caller free sessionid */
  494. free(clone_host);
  495. free(clone_conn_to_host);
  496. return CURLE_OUT_OF_MEMORY;
  497. }
  498. return CURLE_OK;
  499. }
  500. void Curl_ssl_close_all(struct Curl_easy *data)
  501. {
  502. /* kill the session ID cache if not shared */
  503. if(data->state.session && !SSLSESSION_SHARED(data)) {
  504. size_t i;
  505. for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++)
  506. /* the single-killer function handles empty table slots */
  507. Curl_ssl_kill_session(&data->state.session[i]);
  508. /* free the cache data */
  509. Curl_safefree(data->state.session);
  510. }
  511. Curl_ssl->close_all(data);
  512. }
  513. #if defined(USE_OPENSSL) || defined(USE_GNUTLS) || defined(USE_SCHANNEL) || \
  514. defined(USE_SECTRANSP) || defined(USE_NSS) || \
  515. defined(USE_MBEDTLS) || defined(USE_WOLFSSL) || defined(USE_BEARSSL)
  516. int Curl_ssl_getsock(struct connectdata *conn, curl_socket_t *socks)
  517. {
  518. struct ssl_connect_data *connssl = &conn->ssl[FIRSTSOCKET];
  519. if(connssl->connecting_state == ssl_connect_2_writing) {
  520. /* write mode */
  521. socks[0] = conn->sock[FIRSTSOCKET];
  522. return GETSOCK_WRITESOCK(0);
  523. }
  524. if(connssl->connecting_state == ssl_connect_2_reading) {
  525. /* read mode */
  526. socks[0] = conn->sock[FIRSTSOCKET];
  527. return GETSOCK_READSOCK(0);
  528. }
  529. return GETSOCK_BLANK;
  530. }
  531. #else
  532. int Curl_ssl_getsock(struct connectdata *conn,
  533. curl_socket_t *socks)
  534. {
  535. (void)conn;
  536. (void)socks;
  537. return GETSOCK_BLANK;
  538. }
  539. /* USE_OPENSSL || USE_GNUTLS || USE_SCHANNEL || USE_SECTRANSP || USE_NSS */
  540. #endif
  541. void Curl_ssl_close(struct connectdata *conn, int sockindex)
  542. {
  543. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  544. Curl_ssl->close_one(conn, sockindex);
  545. conn->ssl[sockindex].state = ssl_connection_none;
  546. }
  547. CURLcode Curl_ssl_shutdown(struct connectdata *conn, int sockindex)
  548. {
  549. if(Curl_ssl->shut_down(conn, sockindex))
  550. return CURLE_SSL_SHUTDOWN_FAILED;
  551. conn->ssl[sockindex].use = FALSE; /* get back to ordinary socket usage */
  552. conn->ssl[sockindex].state = ssl_connection_none;
  553. conn->recv[sockindex] = Curl_recv_plain;
  554. conn->send[sockindex] = Curl_send_plain;
  555. return CURLE_OK;
  556. }
  557. /* Selects an SSL crypto engine
  558. */
  559. CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine)
  560. {
  561. return Curl_ssl->set_engine(data, engine);
  562. }
  563. /* Selects the default SSL crypto engine
  564. */
  565. CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data)
  566. {
  567. return Curl_ssl->set_engine_default(data);
  568. }
  569. /* Return list of OpenSSL crypto engine names. */
  570. struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data)
  571. {
  572. return Curl_ssl->engines_list(data);
  573. }
  574. /*
  575. * This sets up a session ID cache to the specified size. Make sure this code
  576. * is agnostic to what underlying SSL technology we use.
  577. */
  578. CURLcode Curl_ssl_initsessions(struct Curl_easy *data, size_t amount)
  579. {
  580. struct curl_ssl_session *session;
  581. if(data->state.session)
  582. /* this is just a precaution to prevent multiple inits */
  583. return CURLE_OK;
  584. session = calloc(amount, sizeof(struct curl_ssl_session));
  585. if(!session)
  586. return CURLE_OUT_OF_MEMORY;
  587. /* store the info in the SSL section */
  588. data->set.general_ssl.max_ssl_sessions = amount;
  589. data->state.session = session;
  590. data->state.sessionage = 1; /* this is brand new */
  591. return CURLE_OK;
  592. }
  593. static size_t Curl_multissl_version(char *buffer, size_t size);
  594. size_t Curl_ssl_version(char *buffer, size_t size)
  595. {
  596. #ifdef CURL_WITH_MULTI_SSL
  597. return Curl_multissl_version(buffer, size);
  598. #else
  599. return Curl_ssl->version(buffer, size);
  600. #endif
  601. }
  602. /*
  603. * This function tries to determine connection status.
  604. *
  605. * Return codes:
  606. * 1 means the connection is still in place
  607. * 0 means the connection has been closed
  608. * -1 means the connection status is unknown
  609. */
  610. int Curl_ssl_check_cxn(struct connectdata *conn)
  611. {
  612. return Curl_ssl->check_cxn(conn);
  613. }
  614. bool Curl_ssl_data_pending(const struct connectdata *conn,
  615. int connindex)
  616. {
  617. return Curl_ssl->data_pending(conn, connindex);
  618. }
  619. void Curl_ssl_free_certinfo(struct Curl_easy *data)
  620. {
  621. struct curl_certinfo *ci = &data->info.certs;
  622. if(ci->num_of_certs) {
  623. /* free all individual lists used */
  624. int i;
  625. for(i = 0; i<ci->num_of_certs; i++) {
  626. curl_slist_free_all(ci->certinfo[i]);
  627. ci->certinfo[i] = NULL;
  628. }
  629. free(ci->certinfo); /* free the actual array too */
  630. ci->certinfo = NULL;
  631. ci->num_of_certs = 0;
  632. }
  633. }
  634. CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num)
  635. {
  636. struct curl_certinfo *ci = &data->info.certs;
  637. struct curl_slist **table;
  638. /* Free any previous certificate information structures */
  639. Curl_ssl_free_certinfo(data);
  640. /* Allocate the required certificate information structures */
  641. table = calloc((size_t) num, sizeof(struct curl_slist *));
  642. if(!table)
  643. return CURLE_OUT_OF_MEMORY;
  644. ci->num_of_certs = num;
  645. ci->certinfo = table;
  646. return CURLE_OK;
  647. }
  648. /*
  649. * 'value' is NOT a null-terminated string
  650. */
  651. CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data,
  652. int certnum,
  653. const char *label,
  654. const char *value,
  655. size_t valuelen)
  656. {
  657. struct curl_certinfo *ci = &data->info.certs;
  658. char *output;
  659. struct curl_slist *nl;
  660. CURLcode result = CURLE_OK;
  661. size_t labellen = strlen(label);
  662. size_t outlen = labellen + 1 + valuelen + 1; /* label:value\0 */
  663. output = malloc(outlen);
  664. if(!output)
  665. return CURLE_OUT_OF_MEMORY;
  666. /* sprintf the label and colon */
  667. msnprintf(output, outlen, "%s:", label);
  668. /* memcpy the value (it might not be null-terminated) */
  669. memcpy(&output[labellen + 1], value, valuelen);
  670. /* null-terminate the output */
  671. output[labellen + 1 + valuelen] = 0;
  672. nl = Curl_slist_append_nodup(ci->certinfo[certnum], output);
  673. if(!nl) {
  674. free(output);
  675. curl_slist_free_all(ci->certinfo[certnum]);
  676. result = CURLE_OUT_OF_MEMORY;
  677. }
  678. ci->certinfo[certnum] = nl;
  679. return result;
  680. }
  681. /*
  682. * This is a convenience function for push_certinfo_len that takes a zero
  683. * terminated value.
  684. */
  685. CURLcode Curl_ssl_push_certinfo(struct Curl_easy *data,
  686. int certnum,
  687. const char *label,
  688. const char *value)
  689. {
  690. size_t valuelen = strlen(value);
  691. return Curl_ssl_push_certinfo_len(data, certnum, label, value, valuelen);
  692. }
  693. CURLcode Curl_ssl_random(struct Curl_easy *data,
  694. unsigned char *entropy,
  695. size_t length)
  696. {
  697. return Curl_ssl->random(data, entropy, length);
  698. }
  699. /*
  700. * Public key pem to der conversion
  701. */
  702. static CURLcode pubkey_pem_to_der(const char *pem,
  703. unsigned char **der, size_t *der_len)
  704. {
  705. char *stripped_pem, *begin_pos, *end_pos;
  706. size_t pem_count, stripped_pem_count = 0, pem_len;
  707. CURLcode result;
  708. /* if no pem, exit. */
  709. if(!pem)
  710. return CURLE_BAD_CONTENT_ENCODING;
  711. begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----");
  712. if(!begin_pos)
  713. return CURLE_BAD_CONTENT_ENCODING;
  714. pem_count = begin_pos - pem;
  715. /* Invalid if not at beginning AND not directly following \n */
  716. if(0 != pem_count && '\n' != pem[pem_count - 1])
  717. return CURLE_BAD_CONTENT_ENCODING;
  718. /* 26 is length of "-----BEGIN PUBLIC KEY-----" */
  719. pem_count += 26;
  720. /* Invalid if not directly following \n */
  721. end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----");
  722. if(!end_pos)
  723. return CURLE_BAD_CONTENT_ENCODING;
  724. pem_len = end_pos - pem;
  725. stripped_pem = malloc(pem_len - pem_count + 1);
  726. if(!stripped_pem)
  727. return CURLE_OUT_OF_MEMORY;
  728. /*
  729. * Here we loop through the pem array one character at a time between the
  730. * correct indices, and place each character that is not '\n' or '\r'
  731. * into the stripped_pem array, which should represent the raw base64 string
  732. */
  733. while(pem_count < pem_len) {
  734. if('\n' != pem[pem_count] && '\r' != pem[pem_count])
  735. stripped_pem[stripped_pem_count++] = pem[pem_count];
  736. ++pem_count;
  737. }
  738. /* Place the null terminator in the correct place */
  739. stripped_pem[stripped_pem_count] = '\0';
  740. result = Curl_base64_decode(stripped_pem, der, der_len);
  741. Curl_safefree(stripped_pem);
  742. return result;
  743. }
  744. /*
  745. * Generic pinned public key check.
  746. */
  747. CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data,
  748. const char *pinnedpubkey,
  749. const unsigned char *pubkey, size_t pubkeylen)
  750. {
  751. FILE *fp;
  752. unsigned char *buf = NULL, *pem_ptr = NULL;
  753. CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  754. /* if a path wasn't specified, don't pin */
  755. if(!pinnedpubkey)
  756. return CURLE_OK;
  757. if(!pubkey || !pubkeylen)
  758. return result;
  759. /* only do this if pinnedpubkey starts with "sha256//", length 8 */
  760. if(strncmp(pinnedpubkey, "sha256//", 8) == 0) {
  761. CURLcode encode;
  762. size_t encodedlen, pinkeylen;
  763. char *encoded, *pinkeycopy, *begin_pos, *end_pos;
  764. unsigned char *sha256sumdigest;
  765. if(!Curl_ssl->sha256sum) {
  766. /* without sha256 support, this cannot match */
  767. return result;
  768. }
  769. /* compute sha256sum of public key */
  770. sha256sumdigest = malloc(CURL_SHA256_DIGEST_LENGTH);
  771. if(!sha256sumdigest)
  772. return CURLE_OUT_OF_MEMORY;
  773. encode = Curl_ssl->sha256sum(pubkey, pubkeylen,
  774. sha256sumdigest, CURL_SHA256_DIGEST_LENGTH);
  775. if(encode != CURLE_OK)
  776. return encode;
  777. encode = Curl_base64_encode(data, (char *)sha256sumdigest,
  778. CURL_SHA256_DIGEST_LENGTH, &encoded,
  779. &encodedlen);
  780. Curl_safefree(sha256sumdigest);
  781. if(encode)
  782. return encode;
  783. infof(data, "\t public key hash: sha256//%s\n", encoded);
  784. /* it starts with sha256//, copy so we can modify it */
  785. pinkeylen = strlen(pinnedpubkey) + 1;
  786. pinkeycopy = malloc(pinkeylen);
  787. if(!pinkeycopy) {
  788. Curl_safefree(encoded);
  789. return CURLE_OUT_OF_MEMORY;
  790. }
  791. memcpy(pinkeycopy, pinnedpubkey, pinkeylen);
  792. /* point begin_pos to the copy, and start extracting keys */
  793. begin_pos = pinkeycopy;
  794. do {
  795. end_pos = strstr(begin_pos, ";sha256//");
  796. /*
  797. * if there is an end_pos, null terminate,
  798. * otherwise it'll go to the end of the original string
  799. */
  800. if(end_pos)
  801. end_pos[0] = '\0';
  802. /* compare base64 sha256 digests, 8 is the length of "sha256//" */
  803. if(encodedlen == strlen(begin_pos + 8) &&
  804. !memcmp(encoded, begin_pos + 8, encodedlen)) {
  805. result = CURLE_OK;
  806. break;
  807. }
  808. /*
  809. * change back the null-terminator we changed earlier,
  810. * and look for next begin
  811. */
  812. if(end_pos) {
  813. end_pos[0] = ';';
  814. begin_pos = strstr(end_pos, "sha256//");
  815. }
  816. } while(end_pos && begin_pos);
  817. Curl_safefree(encoded);
  818. Curl_safefree(pinkeycopy);
  819. return result;
  820. }
  821. fp = fopen(pinnedpubkey, "rb");
  822. if(!fp)
  823. return result;
  824. do {
  825. long filesize;
  826. size_t size, pem_len;
  827. CURLcode pem_read;
  828. /* Determine the file's size */
  829. if(fseek(fp, 0, SEEK_END))
  830. break;
  831. filesize = ftell(fp);
  832. if(fseek(fp, 0, SEEK_SET))
  833. break;
  834. if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE)
  835. break;
  836. /*
  837. * if the size of our certificate is bigger than the file
  838. * size then it can't match
  839. */
  840. size = curlx_sotouz((curl_off_t) filesize);
  841. if(pubkeylen > size)
  842. break;
  843. /*
  844. * Allocate buffer for the pinned key
  845. * With 1 additional byte for null terminator in case of PEM key
  846. */
  847. buf = malloc(size + 1);
  848. if(!buf)
  849. break;
  850. /* Returns number of elements read, which should be 1 */
  851. if((int) fread(buf, size, 1, fp) != 1)
  852. break;
  853. /* If the sizes are the same, it can't be base64 encoded, must be der */
  854. if(pubkeylen == size) {
  855. if(!memcmp(pubkey, buf, pubkeylen))
  856. result = CURLE_OK;
  857. break;
  858. }
  859. /*
  860. * Otherwise we will assume it's PEM and try to decode it
  861. * after placing null terminator
  862. */
  863. buf[size] = '\0';
  864. pem_read = pubkey_pem_to_der((const char *)buf, &pem_ptr, &pem_len);
  865. /* if it wasn't read successfully, exit */
  866. if(pem_read)
  867. break;
  868. /*
  869. * if the size of our certificate doesn't match the size of
  870. * the decoded file, they can't be the same, otherwise compare
  871. */
  872. if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen))
  873. result = CURLE_OK;
  874. } while(0);
  875. Curl_safefree(buf);
  876. Curl_safefree(pem_ptr);
  877. fclose(fp);
  878. return result;
  879. }
  880. #ifndef CURL_DISABLE_CRYPTO_AUTH
  881. CURLcode Curl_ssl_md5sum(unsigned char *tmp, /* input */
  882. size_t tmplen,
  883. unsigned char *md5sum, /* output */
  884. size_t md5len)
  885. {
  886. return Curl_ssl->md5sum(tmp, tmplen, md5sum, md5len);
  887. }
  888. #endif
  889. /*
  890. * Check whether the SSL backend supports the status_request extension.
  891. */
  892. bool Curl_ssl_cert_status_request(void)
  893. {
  894. return Curl_ssl->cert_status_request();
  895. }
  896. /*
  897. * Check whether the SSL backend supports false start.
  898. */
  899. bool Curl_ssl_false_start(void)
  900. {
  901. return Curl_ssl->false_start();
  902. }
  903. /*
  904. * Check whether the SSL backend supports setting TLS 1.3 cipher suites
  905. */
  906. bool Curl_ssl_tls13_ciphersuites(void)
  907. {
  908. return Curl_ssl->supports & SSLSUPP_TLS13_CIPHERSUITES;
  909. }
  910. /*
  911. * Default implementations for unsupported functions.
  912. */
  913. int Curl_none_init(void)
  914. {
  915. return 1;
  916. }
  917. void Curl_none_cleanup(void)
  918. { }
  919. int Curl_none_shutdown(struct connectdata *conn UNUSED_PARAM,
  920. int sockindex UNUSED_PARAM)
  921. {
  922. (void)conn;
  923. (void)sockindex;
  924. return 0;
  925. }
  926. int Curl_none_check_cxn(struct connectdata *conn UNUSED_PARAM)
  927. {
  928. (void)conn;
  929. return -1;
  930. }
  931. CURLcode Curl_none_random(struct Curl_easy *data UNUSED_PARAM,
  932. unsigned char *entropy UNUSED_PARAM,
  933. size_t length UNUSED_PARAM)
  934. {
  935. (void)data;
  936. (void)entropy;
  937. (void)length;
  938. return CURLE_NOT_BUILT_IN;
  939. }
  940. void Curl_none_close_all(struct Curl_easy *data UNUSED_PARAM)
  941. {
  942. (void)data;
  943. }
  944. void Curl_none_session_free(void *ptr UNUSED_PARAM)
  945. {
  946. (void)ptr;
  947. }
  948. bool Curl_none_data_pending(const struct connectdata *conn UNUSED_PARAM,
  949. int connindex UNUSED_PARAM)
  950. {
  951. (void)conn;
  952. (void)connindex;
  953. return 0;
  954. }
  955. bool Curl_none_cert_status_request(void)
  956. {
  957. return FALSE;
  958. }
  959. CURLcode Curl_none_set_engine(struct Curl_easy *data UNUSED_PARAM,
  960. const char *engine UNUSED_PARAM)
  961. {
  962. (void)data;
  963. (void)engine;
  964. return CURLE_NOT_BUILT_IN;
  965. }
  966. CURLcode Curl_none_set_engine_default(struct Curl_easy *data UNUSED_PARAM)
  967. {
  968. (void)data;
  969. return CURLE_NOT_BUILT_IN;
  970. }
  971. struct curl_slist *Curl_none_engines_list(struct Curl_easy *data UNUSED_PARAM)
  972. {
  973. (void)data;
  974. return (struct curl_slist *)NULL;
  975. }
  976. bool Curl_none_false_start(void)
  977. {
  978. return FALSE;
  979. }
  980. #ifndef CURL_DISABLE_CRYPTO_AUTH
  981. CURLcode Curl_none_md5sum(unsigned char *input, size_t inputlen,
  982. unsigned char *md5sum, size_t md5len UNUSED_PARAM)
  983. {
  984. struct MD5_context *MD5pw;
  985. (void)md5len;
  986. MD5pw = Curl_MD5_init(Curl_DIGEST_MD5);
  987. if(!MD5pw)
  988. return CURLE_OUT_OF_MEMORY;
  989. Curl_MD5_update(MD5pw, input, curlx_uztoui(inputlen));
  990. Curl_MD5_final(MD5pw, md5sum);
  991. return CURLE_OK;
  992. }
  993. #else
  994. CURLcode Curl_none_md5sum(unsigned char *input UNUSED_PARAM,
  995. size_t inputlen UNUSED_PARAM,
  996. unsigned char *md5sum UNUSED_PARAM,
  997. size_t md5len UNUSED_PARAM)
  998. {
  999. (void)input;
  1000. (void)inputlen;
  1001. (void)md5sum;
  1002. (void)md5len;
  1003. return CURLE_NOT_BUILT_IN;
  1004. }
  1005. #endif
  1006. static int Curl_multissl_init(void)
  1007. {
  1008. if(multissl_init(NULL))
  1009. return 1;
  1010. return Curl_ssl->init();
  1011. }
  1012. static CURLcode Curl_multissl_connect(struct connectdata *conn, int sockindex)
  1013. {
  1014. if(multissl_init(NULL))
  1015. return CURLE_FAILED_INIT;
  1016. return Curl_ssl->connect_blocking(conn, sockindex);
  1017. }
  1018. static CURLcode Curl_multissl_connect_nonblocking(struct connectdata *conn,
  1019. int sockindex, bool *done)
  1020. {
  1021. if(multissl_init(NULL))
  1022. return CURLE_FAILED_INIT;
  1023. return Curl_ssl->connect_nonblocking(conn, sockindex, done);
  1024. }
  1025. static void *Curl_multissl_get_internals(struct ssl_connect_data *connssl,
  1026. CURLINFO info)
  1027. {
  1028. if(multissl_init(NULL))
  1029. return NULL;
  1030. return Curl_ssl->get_internals(connssl, info);
  1031. }
  1032. static void Curl_multissl_close(struct connectdata *conn, int sockindex)
  1033. {
  1034. if(multissl_init(NULL))
  1035. return;
  1036. Curl_ssl->close_one(conn, sockindex);
  1037. }
  1038. static const struct Curl_ssl Curl_ssl_multi = {
  1039. { CURLSSLBACKEND_NONE, "multi" }, /* info */
  1040. 0, /* supports nothing */
  1041. (size_t)-1, /* something insanely large to be on the safe side */
  1042. Curl_multissl_init, /* init */
  1043. Curl_none_cleanup, /* cleanup */
  1044. Curl_multissl_version, /* version */
  1045. Curl_none_check_cxn, /* check_cxn */
  1046. Curl_none_shutdown, /* shutdown */
  1047. Curl_none_data_pending, /* data_pending */
  1048. Curl_none_random, /* random */
  1049. Curl_none_cert_status_request, /* cert_status_request */
  1050. Curl_multissl_connect, /* connect */
  1051. Curl_multissl_connect_nonblocking, /* connect_nonblocking */
  1052. Curl_multissl_get_internals, /* get_internals */
  1053. Curl_multissl_close, /* close_one */
  1054. Curl_none_close_all, /* close_all */
  1055. Curl_none_session_free, /* session_free */
  1056. Curl_none_set_engine, /* set_engine */
  1057. Curl_none_set_engine_default, /* set_engine_default */
  1058. Curl_none_engines_list, /* engines_list */
  1059. Curl_none_false_start, /* false_start */
  1060. Curl_none_md5sum, /* md5sum */
  1061. NULL /* sha256sum */
  1062. };
  1063. const struct Curl_ssl *Curl_ssl =
  1064. #if defined(CURL_WITH_MULTI_SSL)
  1065. &Curl_ssl_multi;
  1066. #elif defined(USE_WOLFSSL)
  1067. &Curl_ssl_wolfssl;
  1068. #elif defined(USE_SECTRANSP)
  1069. &Curl_ssl_sectransp;
  1070. #elif defined(USE_GNUTLS)
  1071. &Curl_ssl_gnutls;
  1072. #elif defined(USE_GSKIT)
  1073. &Curl_ssl_gskit;
  1074. #elif defined(USE_MBEDTLS)
  1075. &Curl_ssl_mbedtls;
  1076. #elif defined(USE_NSS)
  1077. &Curl_ssl_nss;
  1078. #elif defined(USE_OPENSSL)
  1079. &Curl_ssl_openssl;
  1080. #elif defined(USE_SCHANNEL)
  1081. &Curl_ssl_schannel;
  1082. #elif defined(USE_MESALINK)
  1083. &Curl_ssl_mesalink;
  1084. #elif defined(USE_BEARSSL)
  1085. &Curl_ssl_bearssl;
  1086. #else
  1087. #error "Missing struct Curl_ssl for selected SSL backend"
  1088. #endif
  1089. static const struct Curl_ssl *available_backends[] = {
  1090. #if defined(USE_WOLFSSL)
  1091. &Curl_ssl_wolfssl,
  1092. #endif
  1093. #if defined(USE_SECTRANSP)
  1094. &Curl_ssl_sectransp,
  1095. #endif
  1096. #if defined(USE_GNUTLS)
  1097. &Curl_ssl_gnutls,
  1098. #endif
  1099. #if defined(USE_GSKIT)
  1100. &Curl_ssl_gskit,
  1101. #endif
  1102. #if defined(USE_MBEDTLS)
  1103. &Curl_ssl_mbedtls,
  1104. #endif
  1105. #if defined(USE_NSS)
  1106. &Curl_ssl_nss,
  1107. #endif
  1108. #if defined(USE_OPENSSL)
  1109. &Curl_ssl_openssl,
  1110. #endif
  1111. #if defined(USE_SCHANNEL)
  1112. &Curl_ssl_schannel,
  1113. #endif
  1114. #if defined(USE_MESALINK)
  1115. &Curl_ssl_mesalink,
  1116. #endif
  1117. #if defined(USE_BEARSSL)
  1118. &Curl_ssl_bearssl,
  1119. #endif
  1120. NULL
  1121. };
  1122. static size_t Curl_multissl_version(char *buffer, size_t size)
  1123. {
  1124. static const struct Curl_ssl *selected;
  1125. static char backends[200];
  1126. static size_t backends_len;
  1127. const struct Curl_ssl *current;
  1128. current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl;
  1129. if(current != selected) {
  1130. char *p = backends;
  1131. char *end = backends + sizeof(backends);
  1132. int i;
  1133. selected = current;
  1134. backends[0] = '\0';
  1135. for(i = 0; available_backends[i]; ++i) {
  1136. char vb[200];
  1137. bool paren = (selected != available_backends[i]);
  1138. if(available_backends[i]->version(vb, sizeof(vb))) {
  1139. p += msnprintf(p, end - p, "%s%s%s%s", (p != backends ? " " : ""),
  1140. (paren ? "(" : ""), vb, (paren ? ")" : ""));
  1141. }
  1142. }
  1143. backends_len = p - backends;
  1144. }
  1145. if(!size)
  1146. return 0;
  1147. if(size <= backends_len) {
  1148. strncpy(buffer, backends, size - 1);
  1149. buffer[size - 1] = '\0';
  1150. return size - 1;
  1151. }
  1152. strcpy(buffer, backends);
  1153. return backends_len;
  1154. }
  1155. static int multissl_init(const struct Curl_ssl *backend)
  1156. {
  1157. const char *env;
  1158. char *env_tmp;
  1159. if(Curl_ssl != &Curl_ssl_multi)
  1160. return 1;
  1161. if(backend) {
  1162. Curl_ssl = backend;
  1163. return 0;
  1164. }
  1165. if(!available_backends[0])
  1166. return 1;
  1167. env = env_tmp = curl_getenv("CURL_SSL_BACKEND");
  1168. #ifdef CURL_DEFAULT_SSL_BACKEND
  1169. if(!env)
  1170. env = CURL_DEFAULT_SSL_BACKEND;
  1171. #endif
  1172. if(env) {
  1173. int i;
  1174. for(i = 0; available_backends[i]; i++) {
  1175. if(strcasecompare(env, available_backends[i]->info.name)) {
  1176. Curl_ssl = available_backends[i];
  1177. curl_free(env_tmp);
  1178. return 0;
  1179. }
  1180. }
  1181. }
  1182. /* Fall back to first available backend */
  1183. Curl_ssl = available_backends[0];
  1184. curl_free(env_tmp);
  1185. return 0;
  1186. }
  1187. CURLsslset curl_global_sslset(curl_sslbackend id, const char *name,
  1188. const curl_ssl_backend ***avail)
  1189. {
  1190. int i;
  1191. if(avail)
  1192. *avail = (const curl_ssl_backend **)&available_backends;
  1193. if(Curl_ssl != &Curl_ssl_multi)
  1194. return id == Curl_ssl->info.id ||
  1195. (name && strcasecompare(name, Curl_ssl->info.name)) ?
  1196. CURLSSLSET_OK :
  1197. #if defined(CURL_WITH_MULTI_SSL)
  1198. CURLSSLSET_TOO_LATE;
  1199. #else
  1200. CURLSSLSET_UNKNOWN_BACKEND;
  1201. #endif
  1202. for(i = 0; available_backends[i]; i++) {
  1203. if(available_backends[i]->info.id == id ||
  1204. (name && strcasecompare(available_backends[i]->info.name, name))) {
  1205. multissl_init(available_backends[i]);
  1206. return CURLSSLSET_OK;
  1207. }
  1208. }
  1209. return CURLSSLSET_UNKNOWN_BACKEND;
  1210. }
  1211. #else /* USE_SSL */
  1212. CURLsslset curl_global_sslset(curl_sslbackend id, const char *name,
  1213. const curl_ssl_backend ***avail)
  1214. {
  1215. (void)id;
  1216. (void)name;
  1217. (void)avail;
  1218. return CURLSSLSET_NO_BACKENDS;
  1219. }
  1220. #endif /* !USE_SSL */