sslgen.c 13 KB

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