sslgen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. * $Id$
  22. ***************************************************************************/
  23. /* This file is for implementing all "generic" SSL functions that all libcurl
  24. internals should use. It is then responsible for calling the proper
  25. "backend" function.
  26. SSL-functions in libcurl should call functions in this source file, and not
  27. to any specific SSL-layer.
  28. Curl_ssl_ - prefix for generic ones
  29. Curl_ossl_ - prefix for OpenSSL ones
  30. Curl_gtls_ - prefix for GnuTLS ones
  31. Curl_nss_ - prefix for NSS ones
  32. Note that this source code uses curlssl_* functions, and they are all
  33. defines/macros #defined by the lib-specific header files.
  34. "SSL/TLS Strong Encryption: An Introduction"
  35. http://httpd.apache.org/docs-2.0/ssl/ssl_intro.html
  36. */
  37. #include "setup.h"
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <ctype.h>
  41. #ifdef HAVE_SYS_SOCKET_H
  42. #include <sys/socket.h>
  43. #endif
  44. #include "urldata.h"
  45. #define SSLGEN_C
  46. #include "sslgen.h" /* generic SSL protos etc */
  47. #include "ssluse.h" /* OpenSSL versions */
  48. #include "gtls.h" /* GnuTLS versions */
  49. #include "nssg.h" /* NSS versions */
  50. #include "qssl.h" /* QSOSSL versions */
  51. #include "sendf.h"
  52. #include "rawstr.h"
  53. #include "url.h"
  54. #include "curl_memory.h"
  55. #include "progress.h"
  56. /* The last #include file should be: */
  57. #include "memdebug.h"
  58. static bool safe_strequal(char* str1, char* str2)
  59. {
  60. if(str1 && str2)
  61. /* both pointers point to something then compare them */
  62. return (bool)(0 != Curl_raw_equal(str1, str2));
  63. else
  64. /* if both pointers are NULL then treat them as equal */
  65. return (bool)(!str1 && !str2);
  66. }
  67. bool
  68. Curl_ssl_config_matches(struct ssl_config_data* data,
  69. struct ssl_config_data* needle)
  70. {
  71. if((data->version == needle->version) &&
  72. (data->verifypeer == needle->verifypeer) &&
  73. (data->verifyhost == needle->verifyhost) &&
  74. safe_strequal(data->CApath, needle->CApath) &&
  75. safe_strequal(data->CAfile, needle->CAfile) &&
  76. safe_strequal(data->random_file, needle->random_file) &&
  77. safe_strequal(data->egdsocket, needle->egdsocket) &&
  78. safe_strequal(data->cipher_list, needle->cipher_list))
  79. return TRUE;
  80. return FALSE;
  81. }
  82. bool
  83. Curl_clone_ssl_config(struct ssl_config_data *source,
  84. struct ssl_config_data *dest)
  85. {
  86. dest->sessionid = source->sessionid;
  87. dest->verifyhost = source->verifyhost;
  88. dest->verifypeer = source->verifypeer;
  89. dest->version = source->version;
  90. if(source->CAfile) {
  91. dest->CAfile = strdup(source->CAfile);
  92. if(!dest->CAfile)
  93. return FALSE;
  94. }
  95. if(source->CApath) {
  96. dest->CApath = strdup(source->CApath);
  97. if(!dest->CApath)
  98. return FALSE;
  99. }
  100. if(source->cipher_list) {
  101. dest->cipher_list = strdup(source->cipher_list);
  102. if(!dest->cipher_list)
  103. return FALSE;
  104. }
  105. if(source->egdsocket) {
  106. dest->egdsocket = strdup(source->egdsocket);
  107. if(!dest->egdsocket)
  108. return FALSE;
  109. }
  110. if(source->random_file) {
  111. dest->random_file = strdup(source->random_file);
  112. if(!dest->random_file)
  113. return FALSE;
  114. }
  115. return TRUE;
  116. }
  117. void Curl_free_ssl_config(struct ssl_config_data* sslc)
  118. {
  119. Curl_safefree(sslc->CAfile);
  120. Curl_safefree(sslc->CApath);
  121. Curl_safefree(sslc->cipher_list);
  122. Curl_safefree(sslc->egdsocket);
  123. Curl_safefree(sslc->random_file);
  124. }
  125. #ifdef USE_SSL
  126. /* "global" init done? */
  127. static bool init_ssl=FALSE;
  128. /**
  129. * Global SSL init
  130. *
  131. * @retval 0 error initializing SSL
  132. * @retval 1 SSL initialized successfully
  133. */
  134. int Curl_ssl_init(void)
  135. {
  136. /* make sure this is only done once */
  137. if(init_ssl)
  138. return 1;
  139. init_ssl = TRUE; /* never again */
  140. return curlssl_init();
  141. }
  142. /* Global cleanup */
  143. void Curl_ssl_cleanup(void)
  144. {
  145. if(init_ssl) {
  146. /* only cleanup if we did a previous init */
  147. curlssl_cleanup();
  148. init_ssl = FALSE;
  149. }
  150. }
  151. CURLcode
  152. Curl_ssl_connect(struct connectdata *conn, int sockindex)
  153. {
  154. CURLcode res;
  155. /* mark this is being ssl-enabled from here on. */
  156. conn->ssl[sockindex].use = TRUE;
  157. conn->ssl[sockindex].state = ssl_connection_negotiating;
  158. res = curlssl_connect(conn, sockindex);
  159. if(!res)
  160. Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
  161. return res;
  162. }
  163. CURLcode
  164. Curl_ssl_connect_nonblocking(struct connectdata *conn, int sockindex,
  165. bool *done)
  166. {
  167. #ifdef curlssl_connect_nonblocking
  168. CURLcode res;
  169. /* mark this is being ssl requested from here on. */
  170. conn->ssl[sockindex].use = TRUE;
  171. res = curlssl_connect_nonblocking(conn, sockindex, done);
  172. if(!res && *done == TRUE)
  173. Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */
  174. return res;
  175. #else
  176. *done = TRUE; /* fallback to BLOCKING */
  177. conn->ssl[sockindex].use = TRUE;
  178. return curlssl_connect(conn, sockindex);
  179. #endif /* non-blocking connect support */
  180. }
  181. /*
  182. * Check if there's a session ID for the given connection in the cache, and if
  183. * there's one suitable, it is provided. Returns TRUE when no entry matched.
  184. */
  185. int Curl_ssl_getsessionid(struct connectdata *conn,
  186. void **ssl_sessionid,
  187. size_t *idsize) /* set 0 if unknown */
  188. {
  189. struct curl_ssl_session *check;
  190. struct SessionHandle *data = conn->data;
  191. long i;
  192. if(!conn->ssl_config.sessionid)
  193. /* session ID re-use is disabled */
  194. return TRUE;
  195. for(i=0; i< data->set.ssl.numsessions; i++) {
  196. check = &data->state.session[i];
  197. if(!check->sessionid)
  198. /* not session ID means blank entry */
  199. continue;
  200. if(Curl_raw_equal(conn->host.name, check->name) &&
  201. (conn->remote_port == check->remote_port) &&
  202. Curl_ssl_config_matches(&conn->ssl_config, &check->ssl_config)) {
  203. /* yes, we have a session ID! */
  204. data->state.sessionage++; /* increase general age */
  205. check->age = data->state.sessionage; /* set this as used in this age */
  206. *ssl_sessionid = check->sessionid;
  207. if(idsize)
  208. *idsize = check->idsize;
  209. return FALSE;
  210. }
  211. }
  212. *ssl_sessionid = NULL;
  213. return TRUE;
  214. }
  215. /*
  216. * Kill a single session ID entry in the cache.
  217. */
  218. static int kill_session(struct curl_ssl_session *session)
  219. {
  220. if(session->sessionid) {
  221. /* defensive check */
  222. /* free the ID the SSL-layer specific way */
  223. curlssl_session_free(session->sessionid);
  224. session->sessionid=NULL;
  225. session->age = 0; /* fresh */
  226. Curl_free_ssl_config(&session->ssl_config);
  227. Curl_safefree(session->name);
  228. session->name = NULL; /* no name */
  229. return 0; /* ok */
  230. }
  231. else
  232. return 1;
  233. }
  234. /*
  235. * Delete the given session ID from the cache.
  236. */
  237. void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid)
  238. {
  239. int i;
  240. for(i=0; i< conn->data->set.ssl.numsessions; i++) {
  241. struct curl_ssl_session *check = &conn->data->state.session[i];
  242. if (check->sessionid == ssl_sessionid) {
  243. kill_session(check);
  244. break;
  245. }
  246. }
  247. }
  248. /*
  249. * Store session id in the session cache. The ID passed on to this function
  250. * must already have been extracted and allocated the proper way for the SSL
  251. * layer. Curl_XXXX_session_free() will be called to free/kill the session ID
  252. * later on.
  253. */
  254. CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
  255. void *ssl_sessionid,
  256. size_t idsize)
  257. {
  258. long i;
  259. struct SessionHandle *data=conn->data; /* the mother of all structs */
  260. struct curl_ssl_session *store = &data->state.session[0];
  261. long oldest_age=data->state.session[0].age; /* zero if unused */
  262. char *clone_host;
  263. /* Even though session ID re-use might be disabled, that only disables USING
  264. IT. We still store it here in case the re-using is again enabled for an
  265. upcoming transfer */
  266. clone_host = strdup(conn->host.name);
  267. if(!clone_host)
  268. return CURLE_OUT_OF_MEMORY; /* bail out */
  269. /* Now we should add the session ID and the host name to the cache, (remove
  270. the oldest if necessary) */
  271. /* find an empty slot for us, or find the oldest */
  272. for(i=1; (i<data->set.ssl.numsessions) &&
  273. data->state.session[i].sessionid; i++) {
  274. if(data->state.session[i].age < oldest_age) {
  275. oldest_age = data->state.session[i].age;
  276. store = &data->state.session[i];
  277. }
  278. }
  279. if(i == data->set.ssl.numsessions)
  280. /* cache is full, we must "kill" the oldest entry! */
  281. kill_session(store);
  282. else
  283. store = &data->state.session[i]; /* use this slot */
  284. /* now init the session struct wisely */
  285. store->sessionid = ssl_sessionid;
  286. store->idsize = idsize;
  287. store->age = data->state.sessionage; /* set current age */
  288. if (store->name)
  289. /* free it if there's one already present */
  290. free(store->name);
  291. store->name = clone_host; /* clone host name */
  292. store->remote_port = conn->remote_port; /* port number */
  293. if(!Curl_clone_ssl_config(&conn->ssl_config, &store->ssl_config))
  294. return CURLE_OUT_OF_MEMORY;
  295. return CURLE_OK;
  296. }
  297. void Curl_ssl_close_all(struct SessionHandle *data)
  298. {
  299. long i;
  300. /* kill the session ID cache */
  301. if(data->state.session) {
  302. for(i=0; i< data->set.ssl.numsessions; i++)
  303. /* the single-killer function handles empty table slots */
  304. kill_session(&data->state.session[i]);
  305. /* free the cache data */
  306. free(data->state.session);
  307. data->state.session = NULL;
  308. }
  309. curlssl_close_all(data);
  310. }
  311. void Curl_ssl_close(struct connectdata *conn, int sockindex)
  312. {
  313. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  314. curlssl_close(conn, sockindex);
  315. }
  316. CURLcode Curl_ssl_shutdown(struct connectdata *conn, int sockindex)
  317. {
  318. if(curlssl_shutdown(conn, sockindex))
  319. return CURLE_SSL_SHUTDOWN_FAILED;
  320. conn->ssl[sockindex].use = FALSE; /* get back to ordinary socket usage */
  321. conn->ssl[sockindex].state = ssl_connection_none;
  322. return CURLE_OK;
  323. }
  324. /* Selects an SSL crypto engine
  325. */
  326. CURLcode Curl_ssl_set_engine(struct SessionHandle *data, const char *engine)
  327. {
  328. return curlssl_set_engine(data, engine);
  329. }
  330. /* Selects the default SSL crypto engine
  331. */
  332. CURLcode Curl_ssl_set_engine_default(struct SessionHandle *data)
  333. {
  334. return curlssl_set_engine_default(data);
  335. }
  336. /* Return list of OpenSSL crypto engine names. */
  337. struct curl_slist *Curl_ssl_engines_list(struct SessionHandle *data)
  338. {
  339. return curlssl_engines_list(data);
  340. }
  341. /* return number of sent (non-SSL) bytes */
  342. ssize_t Curl_ssl_send(struct connectdata *conn,
  343. int sockindex,
  344. const void *mem,
  345. size_t len)
  346. {
  347. return curlssl_send(conn, sockindex, mem, len);
  348. }
  349. /* return number of received (decrypted) bytes */
  350. /*
  351. * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
  352. * a regular CURLcode value.
  353. */
  354. ssize_t Curl_ssl_recv(struct connectdata *conn, /* connection data */
  355. int sockindex, /* socketindex */
  356. char *mem, /* store read data here */
  357. size_t len) /* max amount to read */
  358. {
  359. ssize_t nread;
  360. bool block = FALSE;
  361. nread = curlssl_recv(conn, sockindex, mem, len, &block);
  362. if(nread == -1) {
  363. if(!block)
  364. return 0; /* this is a true error, not EWOULDBLOCK */
  365. else
  366. return -1;
  367. }
  368. return nread;
  369. }
  370. /*
  371. * This sets up a session ID cache to the specified size. Make sure this code
  372. * is agnostic to what underlying SSL technology we use.
  373. */
  374. CURLcode Curl_ssl_initsessions(struct SessionHandle *data, long amount)
  375. {
  376. struct curl_ssl_session *session;
  377. if(data->state.session)
  378. /* this is just a precaution to prevent multiple inits */
  379. return CURLE_OK;
  380. session = calloc(amount, sizeof(struct curl_ssl_session));
  381. if(!session)
  382. return CURLE_OUT_OF_MEMORY;
  383. /* store the info in the SSL section */
  384. data->set.ssl.numsessions = amount;
  385. data->state.session = session;
  386. data->state.sessionage = 1; /* this is brand new */
  387. return CURLE_OK;
  388. }
  389. size_t Curl_ssl_version(char *buffer, size_t size)
  390. {
  391. return curlssl_version(buffer, size);
  392. }
  393. /*
  394. * This function tries to determine connection status.
  395. *
  396. * Return codes:
  397. * 1 means the connection is still in place
  398. * 0 means the connection has been closed
  399. * -1 means the connection status is unknown
  400. */
  401. int Curl_ssl_check_cxn(struct connectdata *conn)
  402. {
  403. return curlssl_check_cxn(conn);
  404. }
  405. bool Curl_ssl_data_pending(const struct connectdata *conn,
  406. int connindex)
  407. {
  408. return curlssl_data_pending(conn, connindex);
  409. }
  410. void Curl_ssl_free_certinfo(struct SessionHandle *data)
  411. {
  412. int i;
  413. struct curl_certinfo *ci = &data->info.certs;
  414. if(ci->num_of_certs) {
  415. /* free all individual lists used */
  416. for(i=0; i<ci->num_of_certs; i++)
  417. curl_slist_free_all(ci->certinfo[i]);
  418. free(ci->certinfo); /* free the actual array too */
  419. ci->num_of_certs = 0;
  420. }
  421. }
  422. #endif /* USE_SSL */