诸暨麻将添加redis
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

677 rindas
18 KiB

  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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 http://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. Curl_ossl_ - prefix for OpenSSL ones
  29. Curl_gtls_ - prefix for GnuTLS ones
  30. Curl_nss_ - prefix for NSS ones
  31. Curl_qssl_ - prefix for QsoSSL ones
  32. Curl_gskit_ - prefix for GSKit ones
  33. Curl_polarssl_ - prefix for PolarSSL ones
  34. Curl_cyassl_ - prefix for CyaSSL ones
  35. Curl_schannel_ - prefix for Schannel SSPI ones
  36. Curl_darwinssl_ - prefix for SecureTransport (Darwin) ones
  37. Note that this source code uses curlssl_* functions, and they are all
  38. defines/macros #defined by the lib-specific header files.
  39. "SSL/TLS Strong Encryption: An Introduction"
  40. http://httpd.apache.org/docs-2.0/ssl/ssl_intro.html
  41. */
  42. #include "curl_setup.h"
  43. #ifdef HAVE_SYS_TYPES_H
  44. #include <sys/types.h>
  45. #endif
  46. #ifdef HAVE_SYS_STAT_H
  47. #include <sys/stat.h>
  48. #endif
  49. #ifdef HAVE_FCNTL_H
  50. #include <fcntl.h>
  51. #endif
  52. #include "urldata.h"
  53. #define SSLGEN_C
  54. #include "sslgen.h" /* generic SSL protos etc */
  55. #include "ssluse.h" /* OpenSSL versions */
  56. #include "gtls.h" /* GnuTLS versions */
  57. #include "nssg.h" /* NSS versions */
  58. #include "qssl.h" /* QSOSSL versions */
  59. #include "gskit.h" /* Global Secure ToolKit versions */
  60. #include "polarssl.h" /* PolarSSL versions */
  61. #include "axtls.h" /* axTLS versions */
  62. #include "cyassl.h" /* CyaSSL versions */
  63. #include "curl_schannel.h" /* Schannel SSPI version */
  64. #include "curl_darwinssl.h" /* SecureTransport (Darwin) version */
  65. #include "slist.h"
  66. #include "sendf.h"
  67. #include "rawstr.h"
  68. #include "url.h"
  69. #include "curl_memory.h"
  70. #include "progress.h"
  71. #include "share.h"
  72. #include "timeval.h"
  73. #define _MPRINTF_REPLACE /* use our functions only */
  74. #include <curl/mprintf.h>
  75. /* The last #include file should be: */
  76. #include "memdebug.h"
  77. /* convenience macro to check if this handle is using a shared SSL session */
  78. #define SSLSESSION_SHARED(data) (data->share && \
  79. (data->share->specifier & \
  80. (1<<CURL_LOCK_DATA_SSL_SESSION)))
  81. static bool safe_strequal(char* str1, char* str2)
  82. {
  83. if(str1 && str2)
  84. /* both pointers point to something then compare them */
  85. return (0 != Curl_raw_equal(str1, str2)) ? TRUE : FALSE;
  86. else
  87. /* if both pointers are NULL then treat them as equal */
  88. return (!str1 && !str2) ? TRUE : FALSE;
  89. }
  90. bool
  91. Curl_ssl_config_matches(struct ssl_config_data* data,
  92. struct ssl_config_data* needle)
  93. {
  94. if((data->version == needle->version) &&
  95. (data->verifypeer == needle->verifypeer) &&
  96. (data->verifyhost == needle->verifyhost) &&
  97. safe_strequal(data->CApath, needle->CApath) &&
  98. safe_strequal(data->CAfile, needle->CAfile) &&
  99. safe_strequal(data->random_file, needle->random_file) &&
  100. safe_strequal(data->egdsocket, needle->egdsocket) &&
  101. safe_strequal(data->cipher_list, needle->cipher_list))
  102. return TRUE;
  103. return FALSE;
  104. }
  105. bool
  106. Curl_clone_ssl_config(struct ssl_config_data *source,
  107. struct ssl_config_data *dest)
  108. {
  109. dest->sessionid = source->sessionid;
  110. dest->verifyhost = source->verifyhost;
  111. dest->verifypeer = source->verifypeer;
  112. dest->version = source->version;
  113. if(source->CAfile) {
  114. dest->CAfile = strdup(source->CAfile);
  115. if(!dest->CAfile)
  116. return FALSE;
  117. }
  118. else
  119. dest->CAfile = NULL;
  120. if(source->CApath) {
  121. dest->CApath = strdup(source->CApath);
  122. if(!dest->CApath)
  123. return FALSE;
  124. }
  125. else
  126. dest->CApath = NULL;
  127. if(source->cipher_list) {
  128. dest->cipher_list = strdup(source->cipher_list);
  129. if(!dest->cipher_list)
  130. return FALSE;
  131. }
  132. else
  133. dest->cipher_list = NULL;
  134. if(source->egdsocket) {
  135. dest->egdsocket = strdup(source->egdsocket);
  136. if(!dest->egdsocket)
  137. return FALSE;
  138. }
  139. else
  140. dest->egdsocket = NULL;
  141. if(source->random_file) {
  142. dest->random_file = strdup(source->random_file);
  143. if(!dest->random_file)
  144. return FALSE;
  145. }
  146. else
  147. dest->random_file = NULL;
  148. return TRUE;
  149. }
  150. void Curl_free_ssl_config(struct ssl_config_data* sslc)
  151. {
  152. Curl_safefree(sslc->CAfile);
  153. Curl_safefree(sslc->CApath);
  154. Curl_safefree(sslc->cipher_list);
  155. Curl_safefree(sslc->egdsocket);
  156. Curl_safefree(sslc->random_file);
  157. }
  158. /*
  159. * Curl_rand() returns a random unsigned integer, 32bit.
  160. *
  161. * This non-SSL function is put here only because this file is the only one
  162. * with knowledge of what the underlying SSL libraries provide in terms of
  163. * randomizers.
  164. *
  165. * NOTE: 'data' may be passed in as NULL when coming from external API without
  166. * easy handle!
  167. *
  168. */
  169. unsigned int Curl_rand(struct SessionHandle *data)
  170. {
  171. unsigned int r;
  172. static unsigned int randseed;
  173. static bool seeded = FALSE;
  174. #ifndef have_curlssl_random
  175. (void)data;
  176. #else
  177. if(data) {
  178. Curl_ssl_random(data, (unsigned char *)&r, sizeof(r));
  179. return r;
  180. }
  181. #endif
  182. #ifdef RANDOM_FILE
  183. if(!seeded) {
  184. /* if there's a random file to read a seed from, use it */
  185. int fd = open(RANDOM_FILE, O_RDONLY);
  186. if(fd > -1) {
  187. /* read random data into the randseed variable */
  188. ssize_t nread = read(fd, &randseed, sizeof(randseed));
  189. if(nread == sizeof(randseed))
  190. seeded = TRUE;
  191. close(fd);
  192. }
  193. }
  194. #endif
  195. if(!seeded) {
  196. struct timeval now = curlx_tvnow();
  197. randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
  198. randseed = randseed * 1103515245 + 12345;
  199. randseed = randseed * 1103515245 + 12345;
  200. randseed = randseed * 1103515245 + 12345;
  201. seeded = TRUE;
  202. }
  203. /* Return an unsigned 32-bit pseudo-random number. */
  204. r = randseed = randseed * 1103515245 + 12345;
  205. return (r << 16) | ((r >> 16) & 0xFFFF);
  206. }
  207. #ifdef USE_SSL
  208. /* "global" init done? */
  209. static bool init_ssl=FALSE;
  210. /**
  211. * Global SSL init
  212. *
  213. * @retval 0 error initializing SSL
  214. * @retval 1 SSL initialized successfully
  215. */
  216. int Curl_ssl_init(void)
  217. {
  218. /* make sure this is only done once */
  219. if(init_ssl)
  220. return 1;
  221. init_ssl = TRUE; /* never again */
  222. return curlssl_init();
  223. }
  224. /* Global cleanup */
  225. void Curl_ssl_cleanup(void)
  226. {
  227. if(init_ssl) {
  228. /* only cleanup if we did a previous init */
  229. curlssl_cleanup();
  230. init_ssl = FALSE;
  231. }
  232. }
  233. CURLcode
  234. Curl_ssl_connect(struct connectdata *conn, int sockindex)
  235. {
  236. CURLcode res;
  237. /* mark this is being ssl-enabled from here on. */
  238. conn->ssl[sockindex].use = TRUE;
  239. conn->ssl[sockindex].state = ssl_connection_negotiating;
  240. res = curlssl_connect(conn, sockindex);
  241. if(!res)
  242. Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
  243. return res;
  244. }
  245. CURLcode
  246. Curl_ssl_connect_nonblocking(struct connectdata *conn, int sockindex,
  247. bool *done)
  248. {
  249. CURLcode res;
  250. /* mark this is being ssl requested from here on. */
  251. conn->ssl[sockindex].use = TRUE;
  252. #ifdef curlssl_connect_nonblocking
  253. res = curlssl_connect_nonblocking(conn, sockindex, done);
  254. #else
  255. *done = TRUE; /* fallback to BLOCKING */
  256. res = curlssl_connect(conn, sockindex);
  257. #endif /* non-blocking connect support */
  258. if(!res && *done)
  259. Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
  260. return res;
  261. }
  262. /*
  263. * Check if there's a session ID for the given connection in the cache, and if
  264. * there's one suitable, it is provided. Returns TRUE when no entry matched.
  265. */
  266. int Curl_ssl_getsessionid(struct connectdata *conn,
  267. void **ssl_sessionid,
  268. size_t *idsize) /* set 0 if unknown */
  269. {
  270. struct curl_ssl_session *check;
  271. struct SessionHandle *data = conn->data;
  272. size_t i;
  273. long *general_age;
  274. bool no_match = TRUE;
  275. *ssl_sessionid = NULL;
  276. if(!conn->ssl_config.sessionid)
  277. /* session ID re-use is disabled */
  278. return TRUE;
  279. /* Lock if shared */
  280. if(SSLSESSION_SHARED(data)) {
  281. Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
  282. general_age = &data->share->sessionage;
  283. }
  284. else
  285. general_age = &data->state.sessionage;
  286. for(i = 0; i < data->set.ssl.max_ssl_sessions; i++) {
  287. check = &data->state.session[i];
  288. if(!check->sessionid)
  289. /* not session ID means blank entry */
  290. continue;
  291. if(Curl_raw_equal(conn->host.name, check->name) &&
  292. (conn->remote_port == check->remote_port) &&
  293. Curl_ssl_config_matches(&conn->ssl_config, &check->ssl_config)) {
  294. /* yes, we have a session ID! */
  295. (*general_age)++; /* increase general age */
  296. check->age = *general_age; /* set this as used in this age */
  297. *ssl_sessionid = check->sessionid;
  298. if(idsize)
  299. *idsize = check->idsize;
  300. no_match = FALSE;
  301. break;
  302. }
  303. }
  304. /* Unlock */
  305. if(SSLSESSION_SHARED(data))
  306. Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
  307. return no_match;
  308. }
  309. /*
  310. * Kill a single session ID entry in the cache.
  311. */
  312. void Curl_ssl_kill_session(struct curl_ssl_session *session)
  313. {
  314. if(session->sessionid) {
  315. /* defensive check */
  316. /* free the ID the SSL-layer specific way */
  317. curlssl_session_free(session->sessionid);
  318. session->sessionid = NULL;
  319. session->age = 0; /* fresh */
  320. Curl_free_ssl_config(&session->ssl_config);
  321. Curl_safefree(session->name);
  322. }
  323. }
  324. /*
  325. * Delete the given session ID from the cache.
  326. */
  327. void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid)
  328. {
  329. size_t i;
  330. struct SessionHandle *data=conn->data;
  331. if(SSLSESSION_SHARED(data))
  332. Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
  333. for(i = 0; i < data->set.ssl.max_ssl_sessions; i++) {
  334. struct curl_ssl_session *check = &data->state.session[i];
  335. if(check->sessionid == ssl_sessionid) {
  336. Curl_ssl_kill_session(check);
  337. break;
  338. }
  339. }
  340. if(SSLSESSION_SHARED(data))
  341. Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
  342. }
  343. /*
  344. * Store session id in the session cache. The ID passed on to this function
  345. * must already have been extracted and allocated the proper way for the SSL
  346. * layer. Curl_XXXX_session_free() will be called to free/kill the session ID
  347. * later on.
  348. */
  349. CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
  350. void *ssl_sessionid,
  351. size_t idsize)
  352. {
  353. size_t i;
  354. struct SessionHandle *data=conn->data; /* the mother of all structs */
  355. struct curl_ssl_session *store = &data->state.session[0];
  356. long oldest_age=data->state.session[0].age; /* zero if unused */
  357. char *clone_host;
  358. long *general_age;
  359. /* Even though session ID re-use might be disabled, that only disables USING
  360. IT. We still store it here in case the re-using is again enabled for an
  361. upcoming transfer */
  362. clone_host = strdup(conn->host.name);
  363. if(!clone_host)
  364. return CURLE_OUT_OF_MEMORY; /* bail out */
  365. /* Now we should add the session ID and the host name to the cache, (remove
  366. the oldest if necessary) */
  367. /* If using shared SSL session, lock! */
  368. if(SSLSESSION_SHARED(data)) {
  369. Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
  370. general_age = &data->share->sessionage;
  371. }
  372. else {
  373. general_age = &data->state.sessionage;
  374. }
  375. /* find an empty slot for us, or find the oldest */
  376. for(i = 1; (i < data->set.ssl.max_ssl_sessions) &&
  377. data->state.session[i].sessionid; i++) {
  378. if(data->state.session[i].age < oldest_age) {
  379. oldest_age = data->state.session[i].age;
  380. store = &data->state.session[i];
  381. }
  382. }
  383. if(i == data->set.ssl.max_ssl_sessions)
  384. /* cache is full, we must "kill" the oldest entry! */
  385. Curl_ssl_kill_session(store);
  386. else
  387. store = &data->state.session[i]; /* use this slot */
  388. /* now init the session struct wisely */
  389. store->sessionid = ssl_sessionid;
  390. store->idsize = idsize;
  391. store->age = *general_age; /* set current age */
  392. if(store->name)
  393. /* free it if there's one already present */
  394. free(store->name);
  395. store->name = clone_host; /* clone host name */
  396. store->remote_port = conn->remote_port; /* port number */
  397. /* Unlock */
  398. if(SSLSESSION_SHARED(data))
  399. Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
  400. if(!Curl_clone_ssl_config(&conn->ssl_config, &store->ssl_config)) {
  401. store->sessionid = NULL; /* let caller free sessionid */
  402. free(clone_host);
  403. return CURLE_OUT_OF_MEMORY;
  404. }
  405. return CURLE_OK;
  406. }
  407. void Curl_ssl_close_all(struct SessionHandle *data)
  408. {
  409. size_t i;
  410. /* kill the session ID cache if not shared */
  411. if(data->state.session && !SSLSESSION_SHARED(data)) {
  412. for(i = 0; i < data->set.ssl.max_ssl_sessions; i++)
  413. /* the single-killer function handles empty table slots */
  414. Curl_ssl_kill_session(&data->state.session[i]);
  415. /* free the cache data */
  416. Curl_safefree(data->state.session);
  417. }
  418. curlssl_close_all(data);
  419. }
  420. void Curl_ssl_close(struct connectdata *conn, int sockindex)
  421. {
  422. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  423. curlssl_close(conn, sockindex);
  424. }
  425. CURLcode Curl_ssl_shutdown(struct connectdata *conn, int sockindex)
  426. {
  427. if(curlssl_shutdown(conn, sockindex))
  428. return CURLE_SSL_SHUTDOWN_FAILED;
  429. conn->ssl[sockindex].use = FALSE; /* get back to ordinary socket usage */
  430. conn->ssl[sockindex].state = ssl_connection_none;
  431. conn->recv[sockindex] = Curl_recv_plain;
  432. conn->send[sockindex] = Curl_send_plain;
  433. return CURLE_OK;
  434. }
  435. /* Selects an SSL crypto engine
  436. */
  437. CURLcode Curl_ssl_set_engine(struct SessionHandle *data, const char *engine)
  438. {
  439. return curlssl_set_engine(data, engine);
  440. }
  441. /* Selects the default SSL crypto engine
  442. */
  443. CURLcode Curl_ssl_set_engine_default(struct SessionHandle *data)
  444. {
  445. return curlssl_set_engine_default(data);
  446. }
  447. /* Return list of OpenSSL crypto engine names. */
  448. struct curl_slist *Curl_ssl_engines_list(struct SessionHandle *data)
  449. {
  450. return curlssl_engines_list(data);
  451. }
  452. /*
  453. * This sets up a session ID cache to the specified size. Make sure this code
  454. * is agnostic to what underlying SSL technology we use.
  455. */
  456. CURLcode Curl_ssl_initsessions(struct SessionHandle *data, size_t amount)
  457. {
  458. struct curl_ssl_session *session;
  459. if(data->state.session)
  460. /* this is just a precaution to prevent multiple inits */
  461. return CURLE_OK;
  462. session = calloc(amount, sizeof(struct curl_ssl_session));
  463. if(!session)
  464. return CURLE_OUT_OF_MEMORY;
  465. /* store the info in the SSL section */
  466. data->set.ssl.max_ssl_sessions = amount;
  467. data->state.session = session;
  468. data->state.sessionage = 1; /* this is brand new */
  469. return CURLE_OK;
  470. }
  471. size_t Curl_ssl_version(char *buffer, size_t size)
  472. {
  473. return curlssl_version(buffer, size);
  474. }
  475. /*
  476. * This function tries to determine connection status.
  477. *
  478. * Return codes:
  479. * 1 means the connection is still in place
  480. * 0 means the connection has been closed
  481. * -1 means the connection status is unknown
  482. */
  483. int Curl_ssl_check_cxn(struct connectdata *conn)
  484. {
  485. return curlssl_check_cxn(conn);
  486. }
  487. bool Curl_ssl_data_pending(const struct connectdata *conn,
  488. int connindex)
  489. {
  490. return curlssl_data_pending(conn, connindex);
  491. }
  492. void Curl_ssl_free_certinfo(struct SessionHandle *data)
  493. {
  494. int i;
  495. struct curl_certinfo *ci = &data->info.certs;
  496. if(ci->num_of_certs) {
  497. /* free all individual lists used */
  498. for(i=0; i<ci->num_of_certs; i++) {
  499. curl_slist_free_all(ci->certinfo[i]);
  500. ci->certinfo[i] = NULL;
  501. }
  502. free(ci->certinfo); /* free the actual array too */
  503. ci->certinfo = NULL;
  504. ci->num_of_certs = 0;
  505. }
  506. }
  507. int Curl_ssl_init_certinfo(struct SessionHandle * data,
  508. int num)
  509. {
  510. struct curl_certinfo * ci = &data->info.certs;
  511. struct curl_slist * * table;
  512. /* Initialize the certificate information structures. Return 0 if OK, else 1.
  513. */
  514. Curl_ssl_free_certinfo(data);
  515. ci->num_of_certs = num;
  516. table = calloc((size_t) num, sizeof(struct curl_slist *));
  517. if(!table)
  518. return 1;
  519. ci->certinfo = table;
  520. return 0;
  521. }
  522. CURLcode Curl_ssl_push_certinfo_len(struct SessionHandle *data,
  523. int certnum,
  524. const char *label,
  525. const char *value,
  526. size_t valuelen)
  527. {
  528. struct curl_certinfo * ci = &data->info.certs;
  529. char * output;
  530. struct curl_slist * nl;
  531. CURLcode res = CURLE_OK;
  532. /* Add an information record for a particular certificate. */
  533. output = curl_maprintf("%s:%.*s", label, valuelen, value);
  534. if(!output)
  535. return CURLE_OUT_OF_MEMORY;
  536. nl = Curl_slist_append_nodup(ci->certinfo[certnum], output);
  537. if(!nl) {
  538. free(output);
  539. curl_slist_free_all(ci->certinfo[certnum]);
  540. res = CURLE_OUT_OF_MEMORY;
  541. }
  542. ci->certinfo[certnum] = nl;
  543. return res;
  544. }
  545. /*
  546. * This is a convenience function for push_certinfo_len that takes a zero
  547. * terminated value.
  548. */
  549. CURLcode Curl_ssl_push_certinfo(struct SessionHandle *data,
  550. int certnum,
  551. const char *label,
  552. const char *value)
  553. {
  554. size_t valuelen = strlen(value);
  555. return Curl_ssl_push_certinfo_len(data, certnum, label, value, valuelen);
  556. }
  557. /* these functions are only provided by some SSL backends */
  558. #ifdef have_curlssl_random
  559. void Curl_ssl_random(struct SessionHandle *data,
  560. unsigned char *entropy,
  561. size_t length)
  562. {
  563. curlssl_random(data, entropy, length);
  564. }
  565. #endif
  566. #ifdef have_curlssl_md5sum
  567. void Curl_ssl_md5sum(unsigned char *tmp, /* input */
  568. size_t tmplen,
  569. unsigned char *md5sum, /* output */
  570. size_t md5len)
  571. {
  572. curlssl_md5sum(tmp, tmplen, md5sum, md5len);
  573. }
  574. #endif
  575. #endif /* USE_SSL */