sslgen.c 13 KB

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