sslgen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. * Store session id in the session cache. The ID passed on to this function
  236. * must already have been extracted and allocated the proper way for the SSL
  237. * layer. Curl_XXXX_session_free() will be called to free/kill the session ID
  238. * later on.
  239. */
  240. CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
  241. void *ssl_sessionid,
  242. size_t idsize)
  243. {
  244. long i;
  245. struct SessionHandle *data=conn->data; /* the mother of all structs */
  246. struct curl_ssl_session *store = &data->state.session[0];
  247. long oldest_age=data->state.session[0].age; /* zero if unused */
  248. char *clone_host;
  249. /* Even though session ID re-use might be disabled, that only disables USING
  250. IT. We still store it here in case the re-using is again enabled for an
  251. upcoming transfer */
  252. clone_host = strdup(conn->host.name);
  253. if(!clone_host)
  254. return CURLE_OUT_OF_MEMORY; /* bail out */
  255. /* Now we should add the session ID and the host name to the cache, (remove
  256. the oldest if necessary) */
  257. /* find an empty slot for us, or find the oldest */
  258. for(i=1; (i<data->set.ssl.numsessions) &&
  259. data->state.session[i].sessionid; i++) {
  260. if(data->state.session[i].age < oldest_age) {
  261. oldest_age = data->state.session[i].age;
  262. store = &data->state.session[i];
  263. }
  264. }
  265. if(i == data->set.ssl.numsessions)
  266. /* cache is full, we must "kill" the oldest entry! */
  267. kill_session(store);
  268. else
  269. store = &data->state.session[i]; /* use this slot */
  270. /* now init the session struct wisely */
  271. store->sessionid = ssl_sessionid;
  272. store->idsize = idsize;
  273. store->age = data->state.sessionage; /* set current age */
  274. if (store->name)
  275. /* free it if there's one already present */
  276. free(store->name);
  277. store->name = clone_host; /* clone host name */
  278. store->remote_port = conn->remote_port; /* port number */
  279. if(!Curl_clone_ssl_config(&conn->ssl_config, &store->ssl_config))
  280. return CURLE_OUT_OF_MEMORY;
  281. return CURLE_OK;
  282. }
  283. void Curl_ssl_close_all(struct SessionHandle *data)
  284. {
  285. long i;
  286. /* kill the session ID cache */
  287. if(data->state.session) {
  288. for(i=0; i< data->set.ssl.numsessions; i++)
  289. /* the single-killer function handles empty table slots */
  290. kill_session(&data->state.session[i]);
  291. /* free the cache data */
  292. free(data->state.session);
  293. data->state.session = NULL;
  294. }
  295. curlssl_close_all(data);
  296. }
  297. void Curl_ssl_close(struct connectdata *conn, int sockindex)
  298. {
  299. DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
  300. curlssl_close(conn, sockindex);
  301. }
  302. CURLcode Curl_ssl_shutdown(struct connectdata *conn, int sockindex)
  303. {
  304. if(curlssl_shutdown(conn, sockindex))
  305. return CURLE_SSL_SHUTDOWN_FAILED;
  306. conn->ssl[sockindex].use = FALSE; /* get back to ordinary socket usage */
  307. conn->ssl[sockindex].state = ssl_connection_none;
  308. return CURLE_OK;
  309. }
  310. /* Selects an SSL crypto engine
  311. */
  312. CURLcode Curl_ssl_set_engine(struct SessionHandle *data, const char *engine)
  313. {
  314. return curlssl_set_engine(data, engine);
  315. }
  316. /* Selects the default SSL crypto engine
  317. */
  318. CURLcode Curl_ssl_set_engine_default(struct SessionHandle *data)
  319. {
  320. return curlssl_set_engine_default(data);
  321. }
  322. /* Return list of OpenSSL crypto engine names. */
  323. struct curl_slist *Curl_ssl_engines_list(struct SessionHandle *data)
  324. {
  325. return curlssl_engines_list(data);
  326. }
  327. /* return number of sent (non-SSL) bytes */
  328. ssize_t Curl_ssl_send(struct connectdata *conn,
  329. int sockindex,
  330. const void *mem,
  331. size_t len)
  332. {
  333. return curlssl_send(conn, sockindex, mem, len);
  334. }
  335. /* return number of received (decrypted) bytes */
  336. /*
  337. * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
  338. * a regular CURLcode value.
  339. */
  340. ssize_t Curl_ssl_recv(struct connectdata *conn, /* connection data */
  341. int sockindex, /* socketindex */
  342. char *mem, /* store read data here */
  343. size_t len) /* max amount to read */
  344. {
  345. ssize_t nread;
  346. bool block = FALSE;
  347. nread = curlssl_recv(conn, sockindex, mem, len, &block);
  348. if(nread == -1) {
  349. if(!block)
  350. return 0; /* this is a true error, not EWOULDBLOCK */
  351. else
  352. return -1;
  353. }
  354. return nread;
  355. }
  356. /*
  357. * This sets up a session ID cache to the specified size. Make sure this code
  358. * is agnostic to what underlying SSL technology we use.
  359. */
  360. CURLcode Curl_ssl_initsessions(struct SessionHandle *data, long amount)
  361. {
  362. struct curl_ssl_session *session;
  363. if(data->state.session)
  364. /* this is just a precaution to prevent multiple inits */
  365. return CURLE_OK;
  366. session = calloc(sizeof(struct curl_ssl_session), amount);
  367. if(!session)
  368. return CURLE_OUT_OF_MEMORY;
  369. /* store the info in the SSL section */
  370. data->set.ssl.numsessions = amount;
  371. data->state.session = session;
  372. data->state.sessionage = 1; /* this is brand new */
  373. return CURLE_OK;
  374. }
  375. size_t Curl_ssl_version(char *buffer, size_t size)
  376. {
  377. return curlssl_version(buffer, size);
  378. }
  379. /*
  380. * This function tries to determine connection status.
  381. *
  382. * Return codes:
  383. * 1 means the connection is still in place
  384. * 0 means the connection has been closed
  385. * -1 means the connection status is unknown
  386. */
  387. int Curl_ssl_check_cxn(struct connectdata *conn)
  388. {
  389. return curlssl_check_cxn(conn);
  390. }
  391. bool Curl_ssl_data_pending(const struct connectdata *conn,
  392. int connindex)
  393. {
  394. return curlssl_data_pending(conn, connindex);
  395. }
  396. void Curl_ssl_free_certinfo(struct SessionHandle *data)
  397. {
  398. int i;
  399. struct curl_certinfo *ci = &data->info.certs;
  400. if(ci->num_of_certs) {
  401. /* free all individual lists used */
  402. for(i=0; i<ci->num_of_certs; i++)
  403. curl_slist_free_all(ci->certinfo[i]);
  404. free(ci->certinfo); /* free the actual array too */
  405. ci->num_of_certs = 0;
  406. }
  407. }
  408. #endif /* USE_SSL */