vtls.c 37 KB

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